blob: 132d893965e94708525780538bbc46476c4a4599 [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;
30import java.util.concurrent.TimeUnit;
31
32/**
33 * Helper class to load Mockito Resources into a test.
34 */
35public class TelephonyTestBase {
36
Brad Ebinger792729e2019-12-09 16:12:57 -080037 protected TestContext mContext;
Brad Ebinger12ec7f22016-05-05 10:40:57 -070038
39 public void setUp() throws Exception {
Brad Ebinger12ec7f22016-05-05 10:40:57 -070040 MockitoAnnotations.initMocks(this);
Brad Ebingera68a4972020-01-30 17:31:23 -080041 mContext = spy(new TestContext());
Brad Ebinger32925b82016-12-02 13:01:49 -080042 // Set up the looper if it does not exist on the test thread.
43 if (Looper.myLooper() == null) {
44 Looper.prepare();
Brad Ebinger5a803552017-08-17 16:47:25 -070045 // Wait until the looper is not null anymore
46 for(int i = 0; i < 5; i++) {
47 if (Looper.myLooper() != null) {
48 break;
49 }
50 Looper.prepare();
51 Thread.sleep(100);
52 }
Brad Ebinger32925b82016-12-02 13:01:49 -080053 }
Brad Ebinger12ec7f22016-05-05 10:40:57 -070054 }
55
56 public void tearDown() throws Exception {
Brad Ebingera15005b2020-04-29 15:20:38 -070057 // Ensure there are no static references to handlers after test completes.
58 PhoneConfigurationManager.unregisterAllMultiSimConfigChangeRegistrants();
Brad Ebinger12ec7f22016-05-05 10:40:57 -070059 }
60
61 protected final void waitForHandlerAction(Handler h, long timeoutMillis) {
62 final CountDownLatch lock = new CountDownLatch(1);
63 h.post(lock::countDown);
64 while (lock.getCount() > 0) {
65 try {
66 lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
67 } catch (InterruptedException e) {
68 // do nothing
69 }
70 }
71 }
72
73 protected final void waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs) {
74 final CountDownLatch lock = new CountDownLatch(1);
75 h.postDelayed(lock::countDown, delayMs);
76 while (lock.getCount() > 0) {
77 try {
78 lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
79 } catch (InterruptedException e) {
80 // do nothing
81 }
82 }
83 }
Sanket Padawed9a427e2017-06-08 12:03:07 -070084
85 protected void waitForMs(long ms) {
86 try {
87 Thread.sleep(ms);
88 } catch (InterruptedException e) {
89 Log.e("TelephonyTestBase", "InterruptedException while waiting: " + e);
90 }
91 }
Brad Ebinger792729e2019-12-09 16:12:57 -080092
93 protected TestContext getTestContext() {
94 return mContext;
95 }
Brad Ebinger12ec7f22016-05-05 10:40:57 -070096}