• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 org.junit.Assert.assertTrue;
20 import static org.junit.Assert.fail;
21 
22 import android.content.Context;
23 import android.media.session.MediaSessionManager;
24 import android.os.Bundle;
25 import android.os.Handler;
26 import android.os.Looper;
27 
28 import java.io.FileDescriptor;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Objects;
32 import java.util.concurrent.CountDownLatch;
33 import java.util.concurrent.TimeUnit;
34 
35 /**
36  * Utilities for tests.
37  */
38 public final class TestUtils {
39     private static final int WAIT_TIME_MS = 1000;
40     private static final int WAIT_SERVICE_TIME_MS = 5000;
41 
42     /**
43      * Compares contents of two bundles.
44      *
45      * @param a a bundle
46      * @param b another bundle
47      * @return {@code true} if two bundles are the same. {@code false} otherwise. This may be
48      *     incorrect if any bundle contains a bundle.
49      */
equals(Bundle a, Bundle b)50     public static boolean equals(Bundle a, Bundle b) {
51         if (a == b) {
52             return true;
53         }
54         if (a == null || b == null) {
55             return false;
56         }
57         if (!a.keySet().containsAll(b.keySet())
58                 || !b.keySet().containsAll(a.keySet())) {
59             return false;
60         }
61         for (String key : a.keySet()) {
62             if (!Objects.equals(a.get(key), b.get(key))) {
63                 return false;
64             }
65         }
66         return true;
67     }
68 
69     public static class Monitor {
70         private int mNumSignal;
71 
reset()72         public synchronized void reset() {
73             mNumSignal = 0;
74         }
75 
signal()76         public synchronized void signal() {
77             mNumSignal++;
78             notifyAll();
79         }
80 
waitForSignal()81         public synchronized boolean waitForSignal() throws InterruptedException {
82             return waitForCountedSignals(1) > 0;
83         }
84 
waitForCountedSignals(int targetCount)85         public synchronized int waitForCountedSignals(int targetCount) throws InterruptedException {
86             while (mNumSignal < targetCount) {
87                 wait();
88             }
89             return mNumSignal;
90         }
91 
waitForSignal(long timeoutMs)92         public synchronized boolean waitForSignal(long timeoutMs) throws InterruptedException {
93             return waitForCountedSignals(1, timeoutMs) > 0;
94         }
95 
waitForCountedSignals(int targetCount, long timeoutMs)96         public synchronized int waitForCountedSignals(int targetCount, long timeoutMs)
97                 throws InterruptedException {
98             if (timeoutMs == 0) {
99                 return waitForCountedSignals(targetCount);
100             }
101             long deadline = System.currentTimeMillis() + timeoutMs;
102             while (mNumSignal < targetCount) {
103                 long delay = deadline - System.currentTimeMillis();
104                 if (delay <= 0) {
105                     break;
106                 }
107                 wait(delay);
108             }
109             return mNumSignal;
110         }
111 
isSignalled()112         public synchronized boolean isSignalled() {
113             return mNumSignal >= 1;
114         }
115 
getNumSignal()116         public synchronized int getNumSignal() {
117             return mNumSignal;
118         }
119     }
120 }
121