• 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.MockitoAnnotations;
26 
27 import java.util.concurrent.CountDownLatch;
28 import java.util.concurrent.TimeUnit;
29 
30 public abstract class TelecomTestCase {
31     protected static final String TESTING_TAG = "Telecom-TEST";
32     protected Context mContext;
33 
34     MockitoHelper mMockitoHelper = new MockitoHelper();
35     ComponentContextFixture mComponentContextFixture;
36 
setUp()37     public void setUp() throws Exception {
38         Log.setTag(TESTING_TAG);
39         mMockitoHelper.setUp(InstrumentationRegistry.getContext(), getClass());
40         mComponentContextFixture = new ComponentContextFixture();
41         mContext = mComponentContextFixture.getTestDouble().getApplicationContext();
42         Log.setSessionContext(mComponentContextFixture.getTestDouble().getApplicationContext());
43         Log.getSessionManager().mCleanStaleSessions = null;
44         MockitoAnnotations.initMocks(this);
45     }
46 
tearDown()47     public void tearDown() throws Exception {
48         mComponentContextFixture = null;
49         mMockitoHelper.tearDown();
50     }
51 
waitForHandlerAction(Handler h, long timeoutMillis)52     protected static void waitForHandlerAction(Handler h, long timeoutMillis) {
53         final CountDownLatch lock = new CountDownLatch(1);
54         h.post(lock::countDown);
55         while (lock.getCount() > 0) {
56             try {
57                 lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
58             } catch (InterruptedException e) {
59                 // do nothing
60             }
61         }
62     }
63 
waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs)64     protected final void waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs) {
65         final CountDownLatch lock = new CountDownLatch(1);
66         h.postDelayed(lock::countDown, delayMs);
67         while (lock.getCount() > 0) {
68             try {
69                 lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
70             } catch (InterruptedException e) {
71                 // do nothing
72             }
73         }
74     }
75 }
76