TelecomTestCase.java revision 7bba1112556e122254013562650aac6a6af80ac6
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
17package com.android.server.telecom.tests;
18
19import org.mockito.MockitoAnnotations;
20
21import android.os.Handler;
22import android.telecom.Log;
23import android.test.AndroidTestCase;
24
25import java.util.concurrent.CountDownLatch;
26import java.util.concurrent.TimeUnit;
27
28public 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
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        Log.getSessionManager().mCleanStaleSessions = null;
41        MockitoAnnotations.initMocks(this);
42    }
43
44    @Override
45    public void tearDown() throws Exception {
46        mComponentContextFixture = null;
47        mMockitoHelper.tearDown();
48    }
49
50    protected final void waitForHandlerAction(Handler h, long timeoutMillis) {
51        final CountDownLatch lock = new CountDownLatch(1);
52        h.post(lock::countDown);
53        while (lock.getCount() > 0) {
54            try {
55                lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
56            } catch (InterruptedException e) {
57                // do nothing
58            }
59        }
60    }
61}
62