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.os.HandlerThread; 22 import android.os.Looper; 23 import android.telecom.Log; 24 25 import androidx.test.InstrumentationRegistry; 26 27 import com.android.server.telecom.flags.FeatureFlags; 28 29 import org.mockito.Mock; 30 import org.mockito.Mockito; 31 import org.mockito.MockitoAnnotations; 32 33 import java.util.List; 34 import java.util.concurrent.CountDownLatch; 35 import java.util.concurrent.TimeUnit; 36 import java.util.function.Predicate; 37 38 public abstract class TelecomTestCase { 39 protected static final String TESTING_TAG = "Telecom-TEST"; 40 protected Context mContext; 41 @Mock 42 FeatureFlags mFeatureFlags; 43 private HandlerThread mHandlerThread; 44 45 MockitoHelper mMockitoHelper = new MockitoHelper(); 46 ComponentContextFixture mComponentContextFixture; 47 setUp()48 public void setUp() throws Exception { 49 Log.setTag(TESTING_TAG); 50 Log.setIsExtendedLoggingEnabled(true); 51 Log.setUnitTestingEnabled(true); 52 mMockitoHelper.setUp(InstrumentationRegistry.getContext(), getClass()); 53 MockitoAnnotations.initMocks(this); 54 55 Mockito.when(mFeatureFlags.callAudioCommunicationDeviceRefactor()).thenReturn(true); 56 mComponentContextFixture = new ComponentContextFixture(mFeatureFlags); 57 mContext = mComponentContextFixture.getTestDouble().getApplicationContext(); 58 Log.setSessionManager(mComponentContextFixture.getTestDouble().getApplicationContext(), 59 null); 60 } 61 tearDown()62 public void tearDown() throws Exception { 63 if (mHandlerThread != null) { 64 mHandlerThread.quit(); 65 mHandlerThread.join(); 66 mHandlerThread = null; 67 } 68 mComponentContextFixture.destroy(); 69 mComponentContextFixture = null; 70 mMockitoHelper.tearDown(); 71 Mockito.framework().clearInlineMocks(); 72 } 73 getLooper()74 protected Looper getLooper() { 75 if (mHandlerThread == null) { 76 mHandlerThread = new HandlerThread("TelecomTestCase"); 77 mHandlerThread.start(); 78 } 79 return mHandlerThread.getLooper(); 80 } 81 waitForHandlerAction(Handler h, long timeoutMillis)82 protected static void waitForHandlerAction(Handler h, long timeoutMillis) { 83 final CountDownLatch lock = new CountDownLatch(1); 84 h.post(lock::countDown); 85 while (lock.getCount() > 0) { 86 try { 87 lock.await(timeoutMillis, TimeUnit.MILLISECONDS); 88 } catch (InterruptedException e) { 89 // do nothing 90 } 91 } 92 } 93 waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs)94 protected final void waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs) { 95 final CountDownLatch lock = new CountDownLatch(1); 96 h.postDelayed(lock::countDown, delayMs); 97 while (lock.getCount() > 0) { 98 try { 99 lock.await(timeoutMillis, TimeUnit.MILLISECONDS); 100 } catch (InterruptedException e) { 101 // do nothing 102 } 103 } 104 } 105 findFirstIndexMatching(List<T> items, Predicate<T> matcher)106 protected static <T> int findFirstIndexMatching(List<T> items, Predicate<T> matcher) { 107 for (int i = 0; i < items.size(); i++) { 108 if (matcher.test(items.get(i))) { 109 return i; 110 } 111 } 112 return -1; 113 } 114 } 115