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 org.mockito.MockitoAnnotations;
20 
21 import android.os.Handler;
22 import android.telecom.Log;
23 import android.test.AndroidTestCase;
24 
25 import java.util.concurrent.CountDownLatch;
26 import java.util.concurrent.TimeUnit;
27 
28 public abstract class TelecomTestCase extends AndroidTestCase {
29     protected static final String TESTING_TAG = "Telecom-TEST";
30 
31     MockitoHelper mMockitoHelper = new MockitoHelper();
32     ComponentContextFixture mComponentContextFixture;
33 
34     @Override
setUp()35     public void setUp() throws Exception {
36         Log.setTag(TESTING_TAG);
37         mMockitoHelper.setUp(getContext(), getClass());
38         mComponentContextFixture = new ComponentContextFixture();
39         Log.setSessionContext(mComponentContextFixture.getTestDouble().getApplicationContext());
40         MockitoAnnotations.initMocks(this);
41     }
42 
43     @Override
tearDown()44     public void tearDown() throws Exception {
45         mComponentContextFixture = null;
46         mMockitoHelper.tearDown();
47     }
48 
waitForHandlerAction(Handler h, long timeoutMillis)49     protected final void waitForHandlerAction(Handler h, long timeoutMillis) {
50         final CountDownLatch lock = new CountDownLatch(1);
51         h.post(lock::countDown);
52         while (lock.getCount() > 0) {
53             try {
54                 lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
55             } catch (InterruptedException e) {
56                 // do nothing
57             }
58         }
59     }
60 }
61