blob: d30ae6b15ecb3e50109b0706effd37bb6bb1e07d [file] [log] [blame]
Brad Ebinger12ec7f22016-05-05 10:40:57 -07001/*
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
17package com.android;
18
19import android.content.Context;
20import android.os.Handler;
Brad Ebinger32925b82016-12-02 13:01:49 -080021import android.os.Looper;
Sanket Padawed9a427e2017-06-08 12:03:07 -070022import android.util.Log;
Brad Ebinger12ec7f22016-05-05 10:40:57 -070023
KOUSHIK PANUGANTI6dc0dd82019-03-14 00:20:51 -070024import androidx.test.InstrumentationRegistry;
25
Brad Ebinger12ec7f22016-05-05 10:40:57 -070026import org.mockito.MockitoAnnotations;
27
28import java.util.concurrent.CountDownLatch;
29import java.util.concurrent.TimeUnit;
30
31/**
32 * Helper class to load Mockito Resources into a test.
33 */
34public class TelephonyTestBase {
35
36 protected Context mContext;
Brad Ebinger12ec7f22016-05-05 10:40:57 -070037
38 public void setUp() throws Exception {
39 mContext = InstrumentationRegistry.getTargetContext();
Brad Ebinger12ec7f22016-05-05 10:40:57 -070040 MockitoAnnotations.initMocks(this);
Brad Ebinger32925b82016-12-02 13:01:49 -080041 // Set up the looper if it does not exist on the test thread.
42 if (Looper.myLooper() == null) {
43 Looper.prepare();
Brad Ebinger5a803552017-08-17 16:47:25 -070044 // 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 }
Brad Ebinger32925b82016-12-02 13:01:49 -080052 }
Brad Ebinger12ec7f22016-05-05 10:40:57 -070053 }
54
55 public void tearDown() throws Exception {
Brad Ebinger12ec7f22016-05-05 10:40:57 -070056 }
57
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
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 }
Sanket Padawed9a427e2017-06-08 12:03:07 -070081
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 }
Brad Ebinger12ec7f22016-05-05 10:40:57 -070089}