1 /*
2  * Copyright 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.privacysandbox.ads.adservices.java.endtoend.topics;
18 
19 import static com.google.common.truth.Truth.assertWithMessage;
20 
21 import androidx.privacysandbox.ads.adservices.java.VersionCompatUtil;
22 import androidx.privacysandbox.ads.adservices.java.endtoend.TestUtil;
23 import androidx.privacysandbox.ads.adservices.java.topics.TopicsManagerFutures;
24 import androidx.privacysandbox.ads.adservices.topics.GetTopicsRequest;
25 import androidx.privacysandbox.ads.adservices.topics.GetTopicsResponse;
26 import androidx.test.core.app.ApplicationProvider;
27 import androidx.test.filters.SdkSuppress;
28 import androidx.test.platform.app.InstrumentationRegistry;
29 
30 import org.junit.After;
31 import org.junit.AfterClass;
32 import org.junit.Assume;
33 import org.junit.Before;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.junit.runners.JUnit4;
38 
39 @RunWith(JUnit4.class)
40 @SdkSuppress(minSdkVersion = 28) // API 28 required for device_config used by this test
41 // TODO: Consider refactoring so that we're not duplicating code.
42 public class TopicsManagerTest {
43     private static final String TAG = "TopicsManagerTest";
44     TestUtil mTestUtil = new TestUtil(InstrumentationRegistry.getInstrumentation(), TAG);
45 
46     // Override the Epoch Job Period to this value to speed up the epoch computation.
47     private static final long TEST_EPOCH_JOB_PERIOD_MS = 3000;
48 
49     // Default Epoch Period.
50     private static final long TOPICS_EPOCH_JOB_PERIOD_MS = 7 * 86_400_000; // 7 days.
51 
52     // Use 0 percent for random topic in the test so that we can verify the returned topic.
53     private static final int TEST_TOPICS_PERCENTAGE_FOR_RANDOM_TOPIC = 0;
54     private static final int TOPICS_PERCENTAGE_FOR_RANDOM_TOPIC = 5;
55 
56     @BeforeClass
presuite()57     public static void presuite() {
58         TestUtil testUtil = new TestUtil(InstrumentationRegistry.getInstrumentation(), TAG);
59         testUtil.disableDeviceConfigSyncForTests(true);
60     }
61 
62     @AfterClass
postsuite()63     public static void postsuite() {
64         TestUtil testUtil = new TestUtil(InstrumentationRegistry.getInstrumentation(), TAG);
65         testUtil.disableDeviceConfigSyncForTests(false);
66     }
67 
68     @Before
setup()69     public void setup() throws Exception {
70         mTestUtil.overrideKillSwitches(true);
71         // We need to skip 3 epochs so that if there is any usage from other test runs, it will
72         // not be used for epoch retrieval.
73         Thread.sleep(3 * TEST_EPOCH_JOB_PERIOD_MS);
74 
75         mTestUtil.overrideEpochPeriod(TEST_EPOCH_JOB_PERIOD_MS);
76         // We need to turn off random topic so that we can verify the returned topic.
77         mTestUtil.overridePercentageForRandomTopic(TEST_TOPICS_PERCENTAGE_FOR_RANDOM_TOPIC);
78         mTestUtil.overrideConsentManagerDebugMode(true);
79         mTestUtil.overrideAllowlists(true);
80         // TODO: Remove this override.
81         mTestUtil.enableEnrollmentCheck(true);
82         // Force to use bundled files to make test result deterministic.
83         mTestUtil.shouldForceUseBundledFiles(true);
84         // Enable verbose logging.
85         mTestUtil.enableVerboseLogging();
86 
87         if (VersionCompatUtil.INSTANCE.isSWithMinExtServicesVersion(9)) {
88             mTestUtil.enableBackCompatOnS();
89         }
90     }
91 
92     @After
teardown()93     public void teardown() {
94         mTestUtil.disableDeviceConfigSyncForTests(false);
95         mTestUtil.overrideKillSwitches(false);
96         mTestUtil.overrideEpochPeriod(TOPICS_EPOCH_JOB_PERIOD_MS);
97         mTestUtil.overridePercentageForRandomTopic(TOPICS_PERCENTAGE_FOR_RANDOM_TOPIC);
98         mTestUtil.overrideConsentManagerDebugMode(false);
99         mTestUtil.overrideAllowlists(false);
100         mTestUtil.enableEnrollmentCheck(false);
101         mTestUtil.shouldForceUseBundledFiles(false);
102         if (VersionCompatUtil.INSTANCE.isSWithMinExtServicesVersion(9)) {
103             mTestUtil.disableBackCompatOnS();
104         }
105     }
106 
107     @Test
testGetTopics_initialCall_returnsEmptyTopics()108     public void testGetTopics_initialCall_returnsEmptyTopics() throws Exception {
109         // Skip the test if the right SDK extension is not present.
110         Assume.assumeTrue(
111                 VersionCompatUtil.INSTANCE.isTestableVersion(
112                         /* minAdServicesVersion= */ 4, /* minExtServicesVersion= */ 9));
113 
114         TopicsManagerFutures topicsManager =
115                 TopicsManagerFutures.from(ApplicationProvider.getApplicationContext());
116         GetTopicsResponse response1 =
117                 topicsManager
118                         .getTopicsAsync(
119                                 new GetTopicsRequest.Builder()
120                                         .setAdsSdkName("sdk1")
121                                         .setShouldRecordObservation(true)
122                                         .build())
123                         .get();
124 
125         // At beginning, Sdk1 receives no topic.
126         assertWithMessage("Initial topics returned for sdk1").that(response1.getTopics()).isEmpty();
127     }
128 }
129