1 /* 2 * Copyright (C) 2016 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; 18 19 import static org.mockito.Mockito.spy; 20 21 import android.os.Handler; 22 import android.os.Looper; 23 import android.util.Log; 24 25 import com.android.internal.telephony.PhoneConfigurationManager; 26 27 import org.mockito.MockitoAnnotations; 28 29 import java.util.concurrent.CountDownLatch; 30 import java.util.concurrent.Executor; 31 import java.util.concurrent.TimeUnit; 32 33 /** 34 * Helper class to load Mockito Resources into a test. 35 */ 36 public class TelephonyTestBase { 37 38 protected TestContext mContext; 39 setUp()40 public void setUp() throws Exception { 41 MockitoAnnotations.initMocks(this); 42 mContext = spy(new TestContext()); 43 // Set up the looper if it does not exist on the test thread. 44 if (Looper.myLooper() == null) { 45 Looper.prepare(); 46 // Wait until the looper is not null anymore 47 for(int i = 0; i < 5; i++) { 48 if (Looper.myLooper() != null) { 49 break; 50 } 51 Looper.prepare(); 52 Thread.sleep(100); 53 } 54 } 55 } 56 tearDown()57 public void tearDown() throws Exception { 58 // Ensure there are no static references to handlers after test completes. 59 PhoneConfigurationManager.unregisterAllMultiSimConfigChangeRegistrants(); 60 } 61 waitForExecutorAction(Executor executor, long timeoutMillis)62 protected final boolean waitForExecutorAction(Executor executor, long timeoutMillis) { 63 final CountDownLatch lock = new CountDownLatch(1); 64 executor.execute(() -> { 65 lock.countDown(); 66 }); 67 while (lock.getCount() > 0) { 68 try { 69 return lock.await(timeoutMillis, TimeUnit.MILLISECONDS); 70 } catch (InterruptedException e) { 71 // do nothing 72 } 73 } 74 return true; 75 } 76 waitForHandlerAction(Handler h, long timeoutMillis)77 protected final void waitForHandlerAction(Handler h, long timeoutMillis) { 78 final CountDownLatch lock = new CountDownLatch(1); 79 h.post(lock::countDown); 80 while (lock.getCount() > 0) { 81 try { 82 lock.await(timeoutMillis, TimeUnit.MILLISECONDS); 83 } catch (InterruptedException e) { 84 // do nothing 85 } 86 } 87 } 88 waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs)89 protected final void waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs) { 90 final CountDownLatch lock = new CountDownLatch(1); 91 h.postDelayed(lock::countDown, delayMs); 92 while (lock.getCount() > 0) { 93 try { 94 lock.await(timeoutMillis, TimeUnit.MILLISECONDS); 95 } catch (InterruptedException e) { 96 // do nothing 97 } 98 } 99 } 100 waitForMs(long ms)101 protected void waitForMs(long ms) { 102 try { 103 Thread.sleep(ms); 104 } catch (InterruptedException e) { 105 Log.e("TelephonyTestBase", "InterruptedException while waiting: " + e); 106 } 107 } 108 getTestContext()109 protected TestContext getTestContext() { 110 return mContext; 111 } 112 } 113