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