• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         Log.setUnitTestingEnabled(true);
44         mMockitoHelper.setUp(InstrumentationRegistry.getContext(), getClass());
45         mComponentContextFixture = new ComponentContextFixture();
46         mContext = mComponentContextFixture.getTestDouble().getApplicationContext();
47         Log.setSessionContext(mComponentContextFixture.getTestDouble().getApplicationContext());
48         Log.getSessionManager().mCleanStaleSessions = null;
49         MockitoAnnotations.initMocks(this);
50     }
51 
tearDown()52     public void tearDown() throws Exception {
53         mComponentContextFixture = null;
54         mMockitoHelper.tearDown();
55         Mockito.framework().clearInlineMocks();
56     }
57 
waitForHandlerAction(Handler h, long timeoutMillis)58     protected static void waitForHandlerAction(Handler h, long timeoutMillis) {
59         final CountDownLatch lock = new CountDownLatch(1);
60         h.post(lock::countDown);
61         while (lock.getCount() > 0) {
62             try {
63                 lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
64             } catch (InterruptedException e) {
65                 // do nothing
66             }
67         }
68     }
69 
waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs)70     protected final void waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs) {
71         final CountDownLatch lock = new CountDownLatch(1);
72         h.postDelayed(lock::countDown, delayMs);
73         while (lock.getCount() > 0) {
74             try {
75                 lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
76             } catch (InterruptedException e) {
77                 // do nothing
78             }
79         }
80     }
81 
findFirstIndexMatching(List<T> items, Predicate<T> matcher)82     protected static <T> int findFirstIndexMatching(List<T> items, Predicate<T> matcher) {
83         for (int i = 0; i < items.size(); i++) {
84             if (matcher.test(items.get(i))) {
85                 return i;
86             }
87         }
88         return -1;
89     }
90 }
91