• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Flags;
27 import com.android.adservices.service.FlagsFactory;
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 = FlagsFactory.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 Clock FIXED_CLOCK_TRUNCATED_TO_MILLI =
57             Clock.fixed(FIXED_NOW.truncatedTo(ChronoUnit.MILLIS), ZoneOffset.UTC);
58     public static final AdTechIdentifier NOT_ENROLLED_BUYER =
59             AdTechIdentifier.fromString("notenrolled.com");
60     public static final AdTechIdentifier VALID_BUYER_1 = AdTechIdentifier.fromString("test.com");
61     public static final AdTechIdentifier VALID_BUYER_2 = AdTechIdentifier.fromString("test2.com");
62     public static final AdTechIdentifier INVALID_EMPTY_BUYER = AdTechIdentifier.fromString("");
63     public static final Set<AdTechIdentifier> BUYER_SET =
64             new HashSet<>(Arrays.asList(VALID_BUYER_1, VALID_BUYER_2));
65 
getUri(String authority, String path)66     public static Uri getUri(String authority, String path) {
67         return Uri.parse(ValidatorUtil.HTTPS_SCHEME + "://" + authority + path);
68     }
69 
getUri(AdTechIdentifier authority, String path)70     public static Uri getUri(AdTechIdentifier authority, String path) {
71         return getUri(authority.toString(), path);
72     }
73 
74     @SafeVarargs
assertHaveSameHashCode(T... objs)75     public static <T> void assertHaveSameHashCode(T... objs) {
76         Set<T> helperSet = new HashSet<>(Arrays.asList(objs));
77         Truth.assertThat(helperSet).hasSize(1);
78     }
79 
80     @SafeVarargs
assertDifferentHashCode(T... objs)81     public static <T> void assertDifferentHashCode(T... objs) {
82         Set<T> helperSet = new HashSet<>(Arrays.asList(objs));
83         Truth.assertThat(helperSet).hasSize(objs.length);
84     }
85 
doSleep(long timeout)86     public static void doSleep(long timeout) {
87         Log.i(LOG_TAG, "Starting to sleep for " + timeout + " ms");
88         long currentTime = System.currentTimeMillis();
89         long wakeupTime = currentTime + timeout;
90         while (wakeupTime - currentTime > 0) {
91             Log.i(LOG_TAG, "Time left to sleep: " + (wakeupTime - currentTime) + " ms");
92             try {
93                 Thread.sleep(wakeupTime - currentTime);
94             } catch (InterruptedException ignored) {
95 
96             }
97             currentTime = System.currentTimeMillis();
98         }
99         Log.i(LOG_TAG, "Done sleeping");
100     }
101 
processName()102     private static String processName() {
103         if (SdkLevel.isAtLeastT()) {
104             return Process.myProcessName();
105         } else {
106             try {
107                 return ApplicationProvider.getApplicationContext().getPackageName();
108             } catch (IllegalStateException e) {
109                 // TODO(b/275062019): Remove this try/catch once instrumentation context can be
110                 // passed in AppConsentSettingsUiAutomatorTest
111                 LogUtil.e(e, "Failed to get package name from Instrumentation context");
112                 return "android.adservices.tests";
113             }
114         }
115     }
116 }
117