blob: 01267d8c755654d460b716185d2c54e97a257d2e [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
Brad Ebinger12ec7f22016-05-05 10:40:57 -070019import android.os.Handler;
Brad Ebinger32925b82016-12-02 13:01:49 -080020import android.os.Looper;
Sanket Padawed9a427e2017-06-08 12:03:07 -070021import android.util.Log;
Brad Ebinger12ec7f22016-05-05 10:40:57 -070022
Brad Ebinger12ec7f22016-05-05 10:40:57 -070023import org.mockito.MockitoAnnotations;
24
25import java.util.concurrent.CountDownLatch;
26import java.util.concurrent.TimeUnit;
27
28/**
29 * Helper class to load Mockito Resources into a test.
30 */
31public class TelephonyTestBase {
32
Brad Ebinger792729e2019-12-09 16:12:57 -080033 protected TestContext mContext;
Brad Ebinger12ec7f22016-05-05 10:40:57 -070034
35 public void setUp() throws Exception {
Brad Ebinger12ec7f22016-05-05 10:40:57 -070036 MockitoAnnotations.initMocks(this);
Brad Ebinger792729e2019-12-09 16:12:57 -080037 mContext = new TestContext();
Brad Ebinger32925b82016-12-02 13:01:49 -080038 // Set up the looper if it does not exist on the test thread.
39 if (Looper.myLooper() == null) {
40 Looper.prepare();
Brad Ebinger5a803552017-08-17 16:47:25 -070041 // Wait until the looper is not null anymore
42 for(int i = 0; i < 5; i++) {
43 if (Looper.myLooper() != null) {
44 break;
45 }
46 Looper.prepare();
47 Thread.sleep(100);
48 }
Brad Ebinger32925b82016-12-02 13:01:49 -080049 }
Brad Ebinger12ec7f22016-05-05 10:40:57 -070050 }
51
52 public void tearDown() throws Exception {
Brad Ebinger12ec7f22016-05-05 10:40:57 -070053 }
54
55 protected final void waitForHandlerAction(Handler h, long timeoutMillis) {
56 final CountDownLatch lock = new CountDownLatch(1);
57 h.post(lock::countDown);
58 while (lock.getCount() > 0) {
59 try {
60 lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
61 } catch (InterruptedException e) {
62 // do nothing
63 }
64 }
65 }
66
67 protected final void waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs) {
68 final CountDownLatch lock = new CountDownLatch(1);
69 h.postDelayed(lock::countDown, delayMs);
70 while (lock.getCount() > 0) {
71 try {
72 lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
73 } catch (InterruptedException e) {
74 // do nothing
75 }
76 }
77 }
Sanket Padawed9a427e2017-06-08 12:03:07 -070078
79 protected void waitForMs(long ms) {
80 try {
81 Thread.sleep(ms);
82 } catch (InterruptedException e) {
83 Log.e("TelephonyTestBase", "InterruptedException while waiting: " + e);
84 }
85 }
Brad Ebinger792729e2019-12-09 16:12:57 -080086
87 protected TestContext getTestContext() {
88 return mContext;
89 }
Brad Ebinger12ec7f22016-05-05 10:40:57 -070090}