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.adid;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import androidx.privacysandbox.ads.adservices.adid.AdId;
22 import androidx.privacysandbox.ads.adservices.java.VersionCompatUtil;
23 import androidx.privacysandbox.ads.adservices.java.adid.AdIdManagerFutures;
24 import androidx.privacysandbox.ads.adservices.java.endtoend.TestUtil;
25 import androidx.test.core.app.ApplicationProvider;
26 import androidx.test.filters.SdkSuppress;
27 import androidx.test.platform.app.InstrumentationRegistry;
28 
29 import org.junit.After;
30 import org.junit.AfterClass;
31 import org.junit.Assume;
32 import org.junit.Before;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.junit.runners.JUnit4;
37 
38 @RunWith(JUnit4.class)
39 @SdkSuppress(minSdkVersion = 28) // API 28 required for device_config used by this test
40 public class AdIdManagerTest {
41     private static final String TAG = "AdIdManagerTest";
42     private TestUtil mTestUtil = new TestUtil(InstrumentationRegistry.getInstrumentation(), TAG);
43 
44     @BeforeClass
presuite()45     public static void presuite() {
46         TestUtil testUtil = new TestUtil(InstrumentationRegistry.getInstrumentation(), TAG);
47         testUtil.disableDeviceConfigSyncForTests(true);
48         testUtil.enableVerboseLogging();
49     }
50 
51     @AfterClass
postsuite()52     public static void postsuite() {
53         TestUtil testUtil = new TestUtil(InstrumentationRegistry.getInstrumentation(), TAG);
54         testUtil.disableDeviceConfigSyncForTests(false);
55     }
56 
57     @Before
setup()58     public void setup() throws Exception {
59         mTestUtil.overrideAdIdKillSwitch(true);
60         mTestUtil.overrideKillSwitches(true);
61         mTestUtil.overrideConsentManagerDebugMode(true);
62         mTestUtil.overrideAllowlists(true);
63 
64         // Put in a short sleep to make sure the updated config propagates
65         // before starting the tests
66         Thread.sleep(100);
67     }
68 
69     @After
teardown()70     public void teardown() {
71         mTestUtil.overrideAdIdKillSwitch(false);
72         mTestUtil.overrideKillSwitches(false);
73         mTestUtil.overrideConsentManagerDebugMode(false);
74         mTestUtil.overrideAllowlists(false);
75     }
76 
77     @Test
testAdId()78     public void testAdId() throws Exception {
79         // Skip the test if the right SDK extension is not present.
80         Assume.assumeTrue(
81                 VersionCompatUtil.INSTANCE.isTestableVersion(
82                         /* minAdServicesVersion= */ 4,
83                         /* minExtServicesVersionS= */ 9));
84 
85         AdIdManagerFutures adIdManager =
86                 AdIdManagerFutures.from(ApplicationProvider.getApplicationContext());
87         AdId adId = adIdManager.getAdIdAsync().get();
88         assertThat(adId.getAdId()).isNotEmpty();
89         assertThat(adId.isLimitAdTrackingEnabled()).isNotNull();
90     }
91 }
92