blob: 09abb15bcac7fb188a9c137da74931afd336c47a [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 Ebingera68a4972020-01-30 17:31:23 -080019import static org.mockito.Mockito.spy;
20
Brad Ebinger12ec7f22016-05-05 10:40:57 -070021import android.os.Handler;
Brad Ebinger32925b82016-12-02 13:01:49 -080022import android.os.Looper;
Sanket Padawed9a427e2017-06-08 12:03:07 -070023import android.util.Log;
Brad Ebinger12ec7f22016-05-05 10:40:57 -070024
Brad Ebingera15005b2020-04-29 15:20:38 -070025import com.android.internal.telephony.PhoneConfigurationManager;
26
Brad Ebinger12ec7f22016-05-05 10:40:57 -070027import org.mockito.MockitoAnnotations;
28
29import java.util.concurrent.CountDownLatch;
Brad Ebinger63b6f5a2020-10-27 11:43:35 -070030import java.util.concurrent.Executor;
Brad Ebinger12ec7f22016-05-05 10:40:57 -070031import java.util.concurrent.TimeUnit;
32
33/**
34 * Helper class to load Mockito Resources into a test.
35 */
36public class TelephonyTestBase {
37
Brad Ebinger792729e2019-12-09 16:12:57 -080038 protected TestContext mContext;
Brad Ebinger12ec7f22016-05-05 10:40:57 -070039
40 public void setUp() throws Exception {
Brad Ebinger12ec7f22016-05-05 10:40:57 -070041 MockitoAnnotations.initMocks(this);
Brad Ebingera68a4972020-01-30 17:31:23 -080042 mContext = spy(new TestContext());
Brad Ebinger32925b82016-12-02 13:01:49 -080043 // Set up the looper if it does not exist on the test thread.
44 if (Looper.myLooper() == null) {
45 Looper.prepare();
Brad Ebinger5a803552017-08-17 16:47:25 -070046 // Wait until the looper is not null anymore
47 for(int i = 0; i < 5; i++) {
48 if (Looper.myLooper() != null) {
49 break;
50 }
51 Looper.prepare();
52 Thread.sleep(100);
53 }
Brad Ebinger32925b82016-12-02 13:01:49 -080054 }
Brad Ebinger12ec7f22016-05-05 10:40:57 -070055 }
56
57 public void tearDown() throws Exception {
Brad Ebingera15005b2020-04-29 15:20:38 -070058 // Ensure there are no static references to handlers after test completes.
59 PhoneConfigurationManager.unregisterAllMultiSimConfigChangeRegistrants();
Brad Ebinger12ec7f22016-05-05 10:40:57 -070060 }
61
Brad Ebinger63b6f5a2020-10-27 11:43:35 -070062 protected final boolean waitForExecutorAction(Executor executor, long timeoutMillis) {
63 final CountDownLatch lock = new CountDownLatch(1);
Brad Ebinger63b6f5a2020-10-27 11:43:35 -070064 executor.execute(() -> {
Brad Ebinger63b6f5a2020-10-27 11:43:35 -070065 lock.countDown();
66 });
67 while (lock.getCount() > 0) {
68 try {
69 return lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
70 } catch (InterruptedException e) {
71 // do nothing
72 }
73 }
74 return true;
75 }
76
Brad Ebinger12ec7f22016-05-05 10:40:57 -070077 protected final void waitForHandlerAction(Handler h, long timeoutMillis) {
78 final CountDownLatch lock = new CountDownLatch(1);
79 h.post(lock::countDown);
80 while (lock.getCount() > 0) {
81 try {
82 lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
83 } catch (InterruptedException e) {
84 // do nothing
85 }
86 }
87 }
88
89 protected final void waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs) {
90 final CountDownLatch lock = new CountDownLatch(1);
91 h.postDelayed(lock::countDown, delayMs);
92 while (lock.getCount() > 0) {
93 try {
94 lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
95 } catch (InterruptedException e) {
96 // do nothing
97 }
98 }
99 }
Sanket Padawed9a427e2017-06-08 12:03:07 -0700100
101 protected void waitForMs(long ms) {
102 try {
103 Thread.sleep(ms);
104 } catch (InterruptedException e) {
105 Log.e("TelephonyTestBase", "InterruptedException while waiting: " + e);
106 }
107 }
Brad Ebinger792729e2019-12-09 16:12:57 -0800108
109 protected TestContext getTestContext() {
110 return mContext;
111 }
Brad Ebinger12ec7f22016-05-05 10:40:57 -0700112}