Ihab Awad | aa383cc | 2015-03-22 22:12:28 -0700 | [diff] [blame] | 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 | |
Brad Ebinger | 2c93c50 | 2015-10-28 00:23:54 +0000 | [diff] [blame] | 17 | package com.android.server.telecom.tests; |
| 18 | |
Ihab Awad | aa383cc | 2015-03-22 22:12:28 -0700 | [diff] [blame] | 19 | import org.mockito.MockitoAnnotations; |
| 20 | |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 21 | import android.content.Context; |
Hall Liu | 9ecbb1c | 2016-04-14 14:35:48 -0700 | [diff] [blame] | 22 | import android.os.Handler; |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 23 | import android.support.test.InstrumentationRegistry; |
Brad Ebinger | a3eccfe | 2016-10-05 15:45:22 -0700 | [diff] [blame] | 24 | import android.telecom.Log; |
Ihab Awad | aa383cc | 2015-03-22 22:12:28 -0700 | [diff] [blame] | 25 | |
Hall Liu | 9ecbb1c | 2016-04-14 14:35:48 -0700 | [diff] [blame] | 26 | import java.util.concurrent.CountDownLatch; |
| 27 | import java.util.concurrent.TimeUnit; |
| 28 | |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 29 | public abstract class TelecomTestCase { |
Brad Ebinger | 116b859 | 2015-11-18 11:40:11 -0800 | [diff] [blame] | 30 | protected static final String TESTING_TAG = "Telecom-TEST"; |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 31 | protected Context mContext; |
Ihab Awad | aa383cc | 2015-03-22 22:12:28 -0700 | [diff] [blame] | 32 | |
| 33 | MockitoHelper mMockitoHelper = new MockitoHelper(); |
| 34 | ComponentContextFixture mComponentContextFixture; |
| 35 | |
Ihab Awad | aa383cc | 2015-03-22 22:12:28 -0700 | [diff] [blame] | 36 | public void setUp() throws Exception { |
| 37 | Log.setTag(TESTING_TAG); |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 38 | mMockitoHelper.setUp(InstrumentationRegistry.getContext(), getClass()); |
Ihab Awad | aa383cc | 2015-03-22 22:12:28 -0700 | [diff] [blame] | 39 | mComponentContextFixture = new ComponentContextFixture(); |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 40 | mContext = mComponentContextFixture.getTestDouble().getApplicationContext(); |
Brad Ebinger | a3eccfe | 2016-10-05 15:45:22 -0700 | [diff] [blame] | 41 | Log.setSessionContext(mComponentContextFixture.getTestDouble().getApplicationContext()); |
Brad Ebinger | 7bba111 | 2017-06-08 13:57:28 -0700 | [diff] [blame] | 42 | Log.getSessionManager().mCleanStaleSessions = null; |
Ihab Awad | aa383cc | 2015-03-22 22:12:28 -0700 | [diff] [blame] | 43 | MockitoAnnotations.initMocks(this); |
| 44 | } |
| 45 | |
Ihab Awad | aa383cc | 2015-03-22 22:12:28 -0700 | [diff] [blame] | 46 | public void tearDown() throws Exception { |
| 47 | mComponentContextFixture = null; |
| 48 | mMockitoHelper.tearDown(); |
Ihab Awad | aa383cc | 2015-03-22 22:12:28 -0700 | [diff] [blame] | 49 | } |
Hall Liu | 9ecbb1c | 2016-04-14 14:35:48 -0700 | [diff] [blame] | 50 | |
Hall Liu | 2afe902 | 2018-01-03 15:38:38 -0800 | [diff] [blame] | 51 | protected static void waitForHandlerAction(Handler h, long timeoutMillis) { |
Hall Liu | 9ecbb1c | 2016-04-14 14:35:48 -0700 | [diff] [blame] | 52 | 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 Meng | b1a11c8 | 2017-11-21 17:52:42 -0800 | [diff] [blame] | 62 | |
| 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 Awad | aa383cc | 2015-03-22 22:12:28 -0700 | [diff] [blame] | 74 | } |