summaryrefslogtreecommitdiff
path: root/adservices/apk/unittest/src/com/android/adservices/ui/settings/viewmodels/TopicsViewModelTest.java
blob: 9a4c4c53528e7409e42926c7c76a709537cdd687 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.adservices.ui.settings.viewmodels;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import androidx.test.core.app.ApplicationProvider;

import com.android.adservices.data.topics.Topic;
import com.android.adservices.service.consent.ConsentManager;

import com.google.common.collect.ImmutableList;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.io.IOException;

/** Tests for {@link TopicsViewModel}. */
public class TopicsViewModelTest {

    private TopicsViewModel mTopicsViewModel;
    private BlockedTopicsViewModel mBlockedTopicsViewModel;
    @Mock private ConsentManager mConsentManager;

    /** Setup needed before every test in this class. */
    @Before
    public void setup() throws IOException {
        MockitoAnnotations.initMocks(this);
        mTopicsViewModel =
                new TopicsViewModel(ApplicationProvider.getApplicationContext(), mConsentManager);
        mBlockedTopicsViewModel =
                new BlockedTopicsViewModel(
                        ApplicationProvider.getApplicationContext(), mConsentManager);
    }

    /** Test if getTopics returns no topics if {@link ConsentManager} returns no topics. */
    @Test
    public void testGetTopicsReturnsNoTopics() {
        ImmutableList<Topic> topicsList = ImmutableList.of();
        doReturn(topicsList).when(mConsentManager).getKnownTopicsWithConsent();

        mTopicsViewModel =
                new TopicsViewModel(ApplicationProvider.getApplicationContext(), mConsentManager);

        assertThat(mTopicsViewModel.getTopics().getValue()).containsExactlyElementsIn(topicsList);
    }

    /**
     * Test if getTopics returns correct topics if {@link ConsentManager} returns correct topics.
     */
    @Test
    public void testGetTopicsReturnsTopics() throws IOException {
        Topic topic1 = Topic.create(1, 1, 1);
        Topic topic2 = Topic.create(2, 1, 1);
        ImmutableList<Topic> topicsList = ImmutableList.copyOf(new Topic[] {topic1, topic2});
        doReturn(topicsList).when(mConsentManager).getKnownTopicsWithConsent();

        mTopicsViewModel =
                new TopicsViewModel(ApplicationProvider.getApplicationContext(), mConsentManager);

        assertThat(mTopicsViewModel.getTopics().getValue()).containsExactlyElementsIn(topicsList);
    }

    /** Test if revokeTopicConsent blocks a topic with a call to {@link ConsentManager}. */
    @Test
    public void testBlockTopic() {
        Topic topic1 = Topic.create(1, 1, 1);
        mTopicsViewModel.revokeTopicConsent(topic1);

        verify(mConsentManager, times(1)).revokeConsentForTopic(topic1);
    }

    /** Test if restoreTopicConsent blocks a topic with a call to {@link ConsentManager}. */
    @Test
    public void testRestoreTopic() {
        Topic topic1 = Topic.create(1, 1, 1);
        mBlockedTopicsViewModel.restoreTopicConsent(topic1);

        verify(mConsentManager, times(1)).restoreConsentForTopic(topic1);
    }
}