blob: c356cb4070924268cb0a044bf689a6d944474b0c [file] [log] [blame]
Ihab Awadaa383cc2015-03-22 22:12:28 -07001/*
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
Brad Ebinger2c93c502015-10-28 00:23:54 +000017package com.android.server.telecom.tests;
18
Ihab Awadaa383cc2015-03-22 22:12:28 -070019import org.mockito.MockitoAnnotations;
20
Hall Liuc8a396b2017-12-27 18:23:28 -080021import android.content.Context;
Hall Liu9ecbb1c2016-04-14 14:35:48 -070022import android.os.Handler;
Hall Liuc8a396b2017-12-27 18:23:28 -080023import android.support.test.InstrumentationRegistry;
Brad Ebingera3eccfe2016-10-05 15:45:22 -070024import android.telecom.Log;
Ihab Awadaa383cc2015-03-22 22:12:28 -070025
Hall Liu9ecbb1c2016-04-14 14:35:48 -070026import java.util.concurrent.CountDownLatch;
27import java.util.concurrent.TimeUnit;
28
Hall Liuc8a396b2017-12-27 18:23:28 -080029public abstract class TelecomTestCase {
Brad Ebinger116b8592015-11-18 11:40:11 -080030 protected static final String TESTING_TAG = "Telecom-TEST";
Hall Liuc8a396b2017-12-27 18:23:28 -080031 protected Context mContext;
Ihab Awadaa383cc2015-03-22 22:12:28 -070032
33 MockitoHelper mMockitoHelper = new MockitoHelper();
34 ComponentContextFixture mComponentContextFixture;
35
Ihab Awadaa383cc2015-03-22 22:12:28 -070036 public void setUp() throws Exception {
37 Log.setTag(TESTING_TAG);
Hall Liuc8a396b2017-12-27 18:23:28 -080038 mMockitoHelper.setUp(InstrumentationRegistry.getContext(), getClass());
Ihab Awadaa383cc2015-03-22 22:12:28 -070039 mComponentContextFixture = new ComponentContextFixture();
Hall Liuc8a396b2017-12-27 18:23:28 -080040 mContext = mComponentContextFixture.getTestDouble().getApplicationContext();
Brad Ebingera3eccfe2016-10-05 15:45:22 -070041 Log.setSessionContext(mComponentContextFixture.getTestDouble().getApplicationContext());
Brad Ebinger7bba1112017-06-08 13:57:28 -070042 Log.getSessionManager().mCleanStaleSessions = null;
Ihab Awadaa383cc2015-03-22 22:12:28 -070043 MockitoAnnotations.initMocks(this);
44 }
45
Ihab Awadaa383cc2015-03-22 22:12:28 -070046 public void tearDown() throws Exception {
47 mComponentContextFixture = null;
48 mMockitoHelper.tearDown();
Ihab Awadaa383cc2015-03-22 22:12:28 -070049 }
Hall Liu9ecbb1c2016-04-14 14:35:48 -070050
Hall Liu2afe9022018-01-03 15:38:38 -080051 protected static void waitForHandlerAction(Handler h, long timeoutMillis) {
Hall Liu9ecbb1c2016-04-14 14:35:48 -070052 final CountDownLatch lock = new CountDownLatch(1);
53 h.post(lock::countDown);
54 while (lock.getCount() > 0) {
55 try {
56 lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
57 } catch (InterruptedException e) {
58 // do nothing
59 }
60 }
61 }
Pengquan Mengb1a11c82017-11-21 17:52:42 -080062
63 protected final void waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs) {
64 final CountDownLatch lock = new CountDownLatch(1);
65 h.postDelayed(lock::countDown, delayMs);
66 while (lock.getCount() > 0) {
67 try {
68 lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
69 } catch (InterruptedException e) {
70 // do nothing
71 }
72 }
73 }
Ihab Awadaa383cc2015-03-22 22:12:28 -070074}