1 /*
2  * Copyright (C) 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 android.adservices.common;
18 
19 import android.net.Uri;
20 import android.os.Process;
21 import android.util.Log;
22 
23 import androidx.test.core.app.ApplicationProvider;
24 
25 import com.android.adservices.LogUtil;
26 import com.android.adservices.service.FakeFlagsFactory;
27 import com.android.adservices.service.Flags;
28 import com.android.adservices.service.common.ValidatorUtil;
29 import com.android.modules.utils.build.SdkLevel;
30 
31 import com.google.common.truth.Truth;
32 
33 import java.time.Clock;
34 import java.time.Instant;
35 import java.time.ZoneOffset;
36 import java.time.temporal.ChronoUnit;
37 import java.util.Arrays;
38 import java.util.HashSet;
39 import java.util.Set;
40 
41 public class CommonFixture {
42     private static final String LOG_TAG = "adservices";
43 
44     public static final String TEST_PACKAGE_NAME = processName();
45     public static final String TEST_PACKAGE_NAME_1 = "android.adservices.tests1";
46     public static final String TEST_PACKAGE_NAME_2 = "android.adservices.tests2";
47     public static final Set<String> PACKAGE_SET =
48             new HashSet<>(Arrays.asList(TEST_PACKAGE_NAME_1, TEST_PACKAGE_NAME_2));
49 
50     public static final Flags FLAGS_FOR_TEST = FakeFlagsFactory.getFlagsForTest();
51 
52     public static final Instant FIXED_NOW = Instant.now();
53     public static final Instant FIXED_NOW_TRUNCATED_TO_MILLI =
54             FIXED_NOW.truncatedTo(ChronoUnit.MILLIS);
55     public static final Instant FIXED_EARLIER_ONE_DAY = FIXED_NOW.minus(1, ChronoUnit.DAYS);
56     public static final Instant FIXED_NEXT_ONE_DAY = FIXED_NOW.plus(1, ChronoUnit.DAYS);
57     public static final Clock FIXED_CLOCK_TRUNCATED_TO_MILLI =
58             Clock.fixed(FIXED_NOW.truncatedTo(ChronoUnit.MILLIS), ZoneOffset.UTC);
59     public static final AdTechIdentifier NOT_ENROLLED_BUYER =
60             AdTechIdentifier.fromString("notenrolled.com");
61     public static final AdTechIdentifier VALID_BUYER_1 = AdTechIdentifier.fromString("test.com");
62     public static final AdTechIdentifier VALID_BUYER_2 = AdTechIdentifier.fromString("test2.com");
63     public static final AdTechIdentifier INVALID_EMPTY_BUYER = AdTechIdentifier.fromString("");
64     public static final Set<AdTechIdentifier> BUYER_SET =
65             new HashSet<>(Arrays.asList(VALID_BUYER_1, VALID_BUYER_2));
66 
getUri(String authority, String path)67     public static Uri getUri(String authority, String path) {
68         return Uri.parse(ValidatorUtil.HTTPS_SCHEME + "://" + authority + path);
69     }
70 
getUriWithGivenSubdomain(String subdomain, String authority, String path)71     public static Uri getUriWithGivenSubdomain(String subdomain, String authority, String path) {
72         return Uri.parse(ValidatorUtil.HTTPS_SCHEME + "://" + subdomain + "." + authority + path);
73     }
74 
getUriWithValidSubdomain(String authority, String path)75     public static Uri getUriWithValidSubdomain(String authority, String path) {
76         return getUriWithGivenSubdomain("valid.subdomain", authority, path);
77     }
78 
getUri(AdTechIdentifier authority, String path)79     public static Uri getUri(AdTechIdentifier authority, String path) {
80         return getUri(authority.toString(), path);
81     }
82 
83     @SafeVarargs
assertHaveSameHashCode(T... objs)84     public static <T> void assertHaveSameHashCode(T... objs) {
85         Set<T> helperSet = new HashSet<>(Arrays.asList(objs));
86         Truth.assertThat(helperSet).hasSize(1);
87     }
88 
89     @SafeVarargs
assertDifferentHashCode(T... objs)90     public static <T> void assertDifferentHashCode(T... objs) {
91         Set<T> helperSet = new HashSet<>(Arrays.asList(objs));
92         Truth.assertThat(helperSet).hasSize(objs.length);
93     }
94 
doSleep(long timeout)95     public static void doSleep(long timeout) {
96         Log.i(LOG_TAG, "Starting to sleep for " + timeout + " ms");
97         long currentTime = System.currentTimeMillis();
98         long wakeupTime = currentTime + timeout;
99         while (wakeupTime - currentTime > 0) {
100             Log.i(LOG_TAG, "Time left to sleep: " + (wakeupTime - currentTime) + " ms");
101             try {
102                 Thread.sleep(wakeupTime - currentTime);
103             } catch (InterruptedException ignored) {
104                 Log.w(LOG_TAG, "Interrupted while sleeping");
105                 Thread.currentThread().interrupt();
106             }
107             currentTime = System.currentTimeMillis();
108         }
109         Log.i(LOG_TAG, "Done sleeping");
110     }
111 
processName()112     private static String processName() {
113         if (SdkLevel.isAtLeastT()) {
114             return Process.myProcessName();
115         } else {
116             try {
117                 return ApplicationProvider.getApplicationContext().getPackageName();
118             } catch (IllegalStateException e) {
119                 // TODO(b/275062019): Remove this try/catch once instrumentation context can be
120                 // passed in AppConsentSettingsUiAutomatorTest
121                 LogUtil.e(e, "Failed to get package name from Instrumentation context");
122                 return "android.adservices.tests";
123             }
124         }
125     }
126 }
127