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 com.android.server.telecom.Log;
20 
21 import org.mockito.MockitoAnnotations;
22 
23 import android.os.Handler;
24 import android.test.AndroidTestCase;
25 
26 import java.util.concurrent.CountDownLatch;
27 import java.util.concurrent.TimeUnit;
28 
29 public abstract class TelecomTestCase extends AndroidTestCase {
30     protected static final String TESTING_TAG = "Telecom-TEST";
31 
32     MockitoHelper mMockitoHelper = new MockitoHelper();
33     ComponentContextFixture mComponentContextFixture;
34 
35     @Override
setUp()36     public void setUp() throws Exception {
37         Log.setTag(TESTING_TAG);
38         mMockitoHelper.setUp(getContext(), getClass());
39         mComponentContextFixture = new ComponentContextFixture();
40         Log.setContext(mComponentContextFixture.getTestDouble().getApplicationContext());
41         Log.sCleanStaleSessions = null;
42         MockitoAnnotations.initMocks(this);
43     }
44 
45     @Override
tearDown()46     public void tearDown() throws Exception {
47         mComponentContextFixture = null;
48         mMockitoHelper.tearDown();
49         Log.setTag(com.android.server.telecom.Log.TAG);
50     }
51 
waitForHandlerAction(Handler h, long timeoutMillis)52     protected final 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 }
64