• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.content.Context;
20 import android.os.Handler;
21 import android.os.Looper;
22 import android.util.Log;
23 
24 import androidx.test.InstrumentationRegistry;
25 
26 import org.mockito.MockitoAnnotations;
27 
28 import java.util.concurrent.CountDownLatch;
29 import java.util.concurrent.TimeUnit;
30 
31 /**
32  * Helper class to load Mockito Resources into a test.
33  */
34 public class TelephonyTestBase {
35 
36     protected Context mContext;
37 
setUp()38     public void setUp() throws Exception {
39         mContext = InstrumentationRegistry.getTargetContext();
40         MockitoAnnotations.initMocks(this);
41         // Set up the looper if it does not exist on the test thread.
42         if (Looper.myLooper() == null) {
43             Looper.prepare();
44             // Wait until the looper is not null anymore
45             for(int i = 0; i < 5; i++) {
46                 if (Looper.myLooper() != null) {
47                     break;
48                 }
49                 Looper.prepare();
50                 Thread.sleep(100);
51             }
52         }
53     }
54 
tearDown()55     public void tearDown() throws Exception {
56     }
57 
waitForHandlerAction(Handler h, long timeoutMillis)58     protected final void waitForHandlerAction(Handler h, long timeoutMillis) {
59         final CountDownLatch lock = new CountDownLatch(1);
60         h.post(lock::countDown);
61         while (lock.getCount() > 0) {
62             try {
63                 lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
64             } catch (InterruptedException e) {
65                 // do nothing
66             }
67         }
68     }
69 
waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs)70     protected final void waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs) {
71         final CountDownLatch lock = new CountDownLatch(1);
72         h.postDelayed(lock::countDown, delayMs);
73         while (lock.getCount() > 0) {
74             try {
75                 lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
76             } catch (InterruptedException e) {
77                 // do nothing
78             }
79         }
80     }
81 
waitForMs(long ms)82     protected void waitForMs(long ms) {
83         try {
84             Thread.sleep(ms);
85         } catch (InterruptedException e) {
86             Log.e("TelephonyTestBase", "InterruptedException while waiting: " + e);
87         }
88     }
89 }
90