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