1 /*
2  * Copyright (C) 2023 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.telecom.cts.apps;
18 
19 import android.util.Log;
20 
21 import java.util.List;
22 import java.util.concurrent.CountDownLatch;
23 import java.util.concurrent.TimeUnit;
24 
25 /**
26  * This class should contain all CountDown latch helpers.
27  */
28 public class AssertOutcome {
29     private static final String TAG = AssertOutcome.class.getSimpleName();
30     private static final int DEFAULT_TIMEOUT = 5000;
31 
assertCountDownLatchWasCalled(String packageName, List<String> stackTrace, String message, CountDownLatch latch)32     public static void assertCountDownLatchWasCalled(String packageName,
33             List<String> stackTrace,
34             String message,
35             CountDownLatch latch) throws TestAppException {
36         boolean success = assertCountDownWasCalled(latch);
37         if (!success) {
38             throw new TestAppException(packageName, stackTrace, message);
39         }
40     }
41 
assertCountDownWasCalled(CountDownLatch latch)42     private static boolean assertCountDownWasCalled(CountDownLatch latch) {
43         Log.i(TAG, "assertOnResultWasReceived: waiting for latch");
44         try {
45             return latch.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
46         } catch (InterruptedException ie) {
47             return false;
48         }
49     }
50 }
51