1 /* 2 * Copyright (C) 2018 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 package com.android.ons; 17 18 import android.content.Context; 19 import android.telephony.Rlog; 20 import android.telephony.SubscriptionManager; 21 import android.telephony.TelephonyManager; 22 import android.test.AndroidTestCase; 23 24 import androidx.test.InstrumentationRegistry; 25 26 import org.mockito.Mock; 27 import org.mockito.MockitoAnnotations; 28 29 public abstract class ONSBaseTest extends AndroidTestCase { 30 protected TelephonyManager mTelephonyManager; 31 @Mock 32 protected SubscriptionManager mSubscriptionManager; 33 @Mock 34 protected TelephonyManager mMockTelephonyManager; 35 protected Context mContext; 36 protected boolean mReady = false; 37 private Object mLock = new Object(); 38 private static final int MAX_INIT_WAIT_MS = 5000; // 5 seconds 39 private static final String TAG = "ONSBaseTest"; 40 waitUntilReady()41 protected void waitUntilReady() { 42 waitUntilReady(MAX_INIT_WAIT_MS); 43 } 44 waitUntilReady(int time)45 protected void waitUntilReady(int time) { 46 synchronized (mLock) { 47 if (!mReady) { 48 try { 49 mLock.wait(time); 50 } catch (InterruptedException ie) { 51 } 52 53 if (!mReady) { 54 Rlog.d(TAG, "ONS tests failed to set ready state"); 55 } 56 } 57 } 58 } 59 setReady(boolean ready)60 protected void setReady(boolean ready) { 61 synchronized (mLock) { 62 mReady = ready; 63 mLock.notifyAll(); 64 } 65 } 66 setUp(String tag)67 protected void setUp(String tag) throws Exception { 68 mContext = InstrumentationRegistry.getTargetContext(); 69 mTelephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); 70 MockitoAnnotations.initMocks(this); 71 setReady(false); 72 } 73 }