1 /*
2  * Copyright (C) 2015 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 com.android.server.telecom.tests;
18 
19 import android.content.Context;
20 import android.os.Handler;
21 import android.telecom.Log;
22 
23 import androidx.test.InstrumentationRegistry;
24 
25 import org.mockito.Mockito;
26 import org.mockito.MockitoAnnotations;
27 
28 import java.util.List;
29 import java.util.concurrent.CountDownLatch;
30 import java.util.concurrent.TimeUnit;
31 import java.util.function.Predicate;
32 
33 public abstract class TelecomTestCase {
34     protected static final String TESTING_TAG = "Telecom-TEST";
35     protected Context mContext;
36 
37     MockitoHelper mMockitoHelper = new MockitoHelper();
38     ComponentContextFixture mComponentContextFixture;
39 
setUp()40     public void setUp() throws Exception {
41         Log.setTag(TESTING_TAG);
42         Log.setIsExtendedLoggingEnabled(true);
43         mMockitoHelper.setUp(InstrumentationRegistry.getContext(), getClass());
44         mComponentContextFixture = new ComponentContextFixture();
45         mContext = mComponentContextFixture.getTestDouble().getApplicationContext();
46         Log.setSessionContext(mComponentContextFixture.getTestDouble().getApplicationContext());
47         Log.getSessionManager().mCleanStaleSessions = null;
48         MockitoAnnotations.initMocks(this);
49     }
50 
tearDown()51     public void tearDown() throws Exception {
52         mComponentContextFixture = null;
53         mMockitoHelper.tearDown();
54         Mockito.framework().clearInlineMocks();
55     }
56 
waitForHandlerAction(Handler h, long timeoutMillis)57     protected static void waitForHandlerAction(Handler h, long timeoutMillis) {
58         final CountDownLatch lock = new CountDownLatch(1);
59         h.post(lock::countDown);
60         while (lock.getCount() > 0) {
61             try {
62                 lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
63             } catch (InterruptedException e) {
64                 // do nothing
65             }
66         }
67     }
68 
waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs)69     protected final void waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs) {
70         final CountDownLatch lock = new CountDownLatch(1);
71         h.postDelayed(lock::countDown, delayMs);
72         while (lock.getCount() > 0) {
73             try {
74                 lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
75             } catch (InterruptedException e) {
76                 // do nothing
77             }
78         }
79     }
80 
findFirstIndexMatching(List<T> items, Predicate<T> matcher)81     protected static <T> int findFirstIndexMatching(List<T> items, Predicate<T> matcher) {
82         for (int i = 0; i < items.size(); i++) {
83             if (matcher.test(items.get(i))) {
84                 return i;
85             }
86         }
87         return -1;
88     }
89 }
90