1 /*
2  * Copyright 2018 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.media.cts;
18 
19 import static android.content.pm.PackageManager.MATCH_APEX;
20 
21 import static org.junit.Assume.assumeTrue;
22 
23 import android.content.Context;
24 import android.content.pm.PackageInfo;
25 import android.content.pm.PackageManager;
26 import android.os.Bundle;
27 import android.util.Log;
28 
29 import androidx.test.core.app.ApplicationProvider;
30 
31 import org.junit.Assert;
32 import org.junit.AssumptionViolatedException;
33 
34 import java.util.Objects;
35 
36 /**
37  * Utilities for tests.
38  */
39 public final class TestUtils {
40     private static String TAG = "TestUtils";
41     private static final int WAIT_TIME_MS = 1000;
42     private static final int WAIT_SERVICE_TIME_MS = 5000;
43 
44     /**
45      * Compares contents of two bundles.
46      *
47      * @param a a bundle
48      * @param b another bundle
49      * @return {@code true} if two bundles are the same. {@code false} otherwise. This may be
50      *     incorrect if any bundle contains a bundle.
51      */
equals(Bundle a, Bundle b)52     public static boolean equals(Bundle a, Bundle b) {
53         if (a == b) {
54             return true;
55         }
56         if (a == null || b == null) {
57             return false;
58         }
59         if (!a.keySet().containsAll(b.keySet())
60                 || !b.keySet().containsAll(a.keySet())) {
61             return false;
62         }
63         for (String key : a.keySet()) {
64             if (!Objects.equals(a.get(key), b.get(key))) {
65                 return false;
66             }
67         }
68         return true;
69     }
70 
71     /**
72      * Checks {@code module} is at least {@code minVersion}
73      *
74      * The tests are skipped by throwing a {@link AssumptionViolatedException}.  CTS test runners
75      * will report this as a {@code ASSUMPTION_FAILED}.
76      *
77      * @param module     the apex module name
78      * @param minVersion the minimum version
79      * @throws AssumptionViolatedException if module version < minVersion
80      */
assumeMainlineModuleAtLeast(String module, long minVersion)81     static void assumeMainlineModuleAtLeast(String module, long minVersion) {
82         try {
83             long actualVersion = getModuleVersion(module);
84             assumeTrue("Assume  module  " + module + " version " + actualVersion + " < minVersion"
85                     + minVersion, actualVersion >= minVersion);
86         } catch (PackageManager.NameNotFoundException e) {
87             Assert.fail(e.getMessage());
88         }
89     }
90 
91     /**
92      * Checks if {@code module} is < {@code minVersion}
93      *
94      * <p>
95      * {@link AssumptionViolatedException} is not handled properly by {@code JUnit3} so just return
96      * the test
97      * early instead.
98      *
99      * @param module     the apex module name
100      * @param minVersion the minimum version
101      * @deprecated convert test to JUnit4 and use
102      * {@link #assumeMainlineModuleAtLeast(String, long)} instead.
103      */
104     @Deprecated
skipTestIfMainlineLessThan(String module, long minVersion)105     static boolean skipTestIfMainlineLessThan(String module, long minVersion) {
106         try {
107             long actualVersion = getModuleVersion(module);
108             if (actualVersion < minVersion) {
109                 Log.i(TAG, "Skipping test because Module  " + module + " minVersion " + minVersion
110                         + " > "
111                         + minVersion
112                 );
113                 return true;
114             } else {
115                 return false;
116             }
117         } catch (PackageManager.NameNotFoundException e) {
118             Assert.fail(e.getMessage());
119             return false;
120         }
121     }
122 
getModuleVersion(String module)123     private static long getModuleVersion(String module)
124             throws PackageManager.NameNotFoundException {
125         Context context = ApplicationProvider.getApplicationContext();
126         PackageInfo info;
127         info = context.getPackageManager().getPackageInfo(module,
128                 MATCH_APEX);
129         return info.getLongVersionCode();
130     }
131 
132 
TestUtils()133     private TestUtils() {
134     }
135 
136     public static class Monitor {
137         private int mNumSignal;
138 
reset()139         public synchronized void reset() {
140             mNumSignal = 0;
141         }
142 
signal()143         public synchronized void signal() {
144             mNumSignal++;
145             notifyAll();
146         }
147 
waitForSignal()148         public synchronized boolean waitForSignal() throws InterruptedException {
149             return waitForCountedSignals(1) > 0;
150         }
151 
waitForCountedSignals(int targetCount)152         public synchronized int waitForCountedSignals(int targetCount) throws InterruptedException {
153             while (mNumSignal < targetCount) {
154                 wait();
155             }
156             return mNumSignal;
157         }
158 
waitForSignal(long timeoutMs)159         public synchronized boolean waitForSignal(long timeoutMs) throws InterruptedException {
160             return waitForCountedSignals(1, timeoutMs) > 0;
161         }
162 
waitForCountedSignals(int targetCount, long timeoutMs)163         public synchronized int waitForCountedSignals(int targetCount, long timeoutMs)
164                 throws InterruptedException {
165             if (timeoutMs == 0) {
166                 return waitForCountedSignals(targetCount);
167             }
168             long deadline = System.currentTimeMillis() + timeoutMs;
169             while (mNumSignal < targetCount) {
170                 long delay = deadline - System.currentTimeMillis();
171                 if (delay <= 0) {
172                     break;
173                 }
174                 wait(delay);
175             }
176             return mNumSignal;
177         }
178 
isSignalled()179         public synchronized boolean isSignalled() {
180             return mNumSignal >= 1;
181         }
182 
getNumSignal()183         public synchronized int getNumSignal() {
184             return mNumSignal;
185         }
186     }
187 }
188