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 android.content.Context; 20 import android.os.Handler; 21 import android.telecom.Log; 22 23 import androidx.test.InstrumentationRegistry; 24 25 import org.mockito.Mockito; 26 import org.mockito.MockitoAnnotations; 27 28 import java.util.concurrent.CountDownLatch; 29 import java.util.concurrent.TimeUnit; 30 31 public abstract class TelecomTestCase { 32 protected static final String TESTING_TAG = "Telecom-TEST"; 33 protected Context mContext; 34 35 MockitoHelper mMockitoHelper = new MockitoHelper(); 36 ComponentContextFixture mComponentContextFixture; 37 setUp()38 public void setUp() throws Exception { 39 Log.setTag(TESTING_TAG); 40 Log.setIsExtendedLoggingEnabled(true); 41 mMockitoHelper.setUp(InstrumentationRegistry.getContext(), getClass()); 42 mComponentContextFixture = new ComponentContextFixture(); 43 mContext = mComponentContextFixture.getTestDouble().getApplicationContext(); 44 Log.setSessionContext(mComponentContextFixture.getTestDouble().getApplicationContext()); 45 Log.getSessionManager().mCleanStaleSessions = null; 46 MockitoAnnotations.initMocks(this); 47 } 48 tearDown()49 public void tearDown() throws Exception { 50 mComponentContextFixture = null; 51 mMockitoHelper.tearDown(); 52 Mockito.framework().clearInlineMocks(); 53 } 54 waitForHandlerAction(Handler h, long timeoutMillis)55 protected static void waitForHandlerAction(Handler h, long timeoutMillis) { 56 final CountDownLatch lock = new CountDownLatch(1); 57 h.post(lock::countDown); 58 while (lock.getCount() > 0) { 59 try { 60 lock.await(timeoutMillis, TimeUnit.MILLISECONDS); 61 } catch (InterruptedException e) { 62 // do nothing 63 } 64 } 65 } 66 waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs)67 protected final void waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs) { 68 final CountDownLatch lock = new CountDownLatch(1); 69 h.postDelayed(lock::countDown, delayMs); 70 while (lock.getCount() > 0) { 71 try { 72 lock.await(timeoutMillis, TimeUnit.MILLISECONDS); 73 } catch (InterruptedException e) { 74 // do nothing 75 } 76 } 77 } 78 } 79