blob: 7b7bef22db9df526d11ecb0a1fc8b93e8d94e2f5 [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;
Brad Ebinger12ec7f22016-05-05 10:40:57 -070022import android.support.test.InstrumentationRegistry;
Sanket Padawed9a427e2017-06-08 12:03:07 -070023import android.util.Log;
Brad Ebinger12ec7f22016-05-05 10:40:57 -070024
Brad Ebinger12ec7f22016-05-05 10:40:57 -070025import org.mockito.MockitoAnnotations;
26
27import java.util.concurrent.CountDownLatch;
28import java.util.concurrent.TimeUnit;
29
30/**
31 * Helper class to load Mockito Resources into a test.
32 */
33public class TelephonyTestBase {
34
35 protected Context mContext;
Brad Ebinger12ec7f22016-05-05 10:40:57 -070036
37 public void setUp() throws Exception {
38 mContext = InstrumentationRegistry.getTargetContext();
Brad Ebinger12ec7f22016-05-05 10:40:57 -070039 MockitoAnnotations.initMocks(this);
Brad Ebinger32925b82016-12-02 13:01:49 -080040 // Set up the looper if it does not exist on the test thread.
41 if (Looper.myLooper() == null) {
42 Looper.prepare();
Brad Ebingered0b1372017-08-17 16:47:25 -070043 // Wait until the looper is not null anymore
44 for(int i = 0; i < 5; i++) {
45 if (Looper.myLooper() != null) {
46 break;
47 }
48 Looper.prepare();
49 Thread.sleep(100);
50 }
Brad Ebinger32925b82016-12-02 13:01:49 -080051 }
Brad Ebinger12ec7f22016-05-05 10:40:57 -070052 }
53
54 public void tearDown() throws Exception {
Brad Ebinger12ec7f22016-05-05 10:40:57 -070055 }
56
57 protected final void waitForHandlerAction(Handler h, long timeoutMillis) {
58 final CountDownLatch lock = new CountDownLatch(1);
59 h.post(lock::countDown);
60 while (lock.getCount() > 0) {
61 try {
62 lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
63 } catch (InterruptedException e) {
64 // do nothing
65 }
66 }
67 }
68
69 protected final void waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs) {
70 final CountDownLatch lock = new CountDownLatch(1);
71 h.postDelayed(lock::countDown, delayMs);
72 while (lock.getCount() > 0) {
73 try {
74 lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
75 } catch (InterruptedException e) {
76 // do nothing
77 }
78 }
79 }
Sanket Padawed9a427e2017-06-08 12:03:07 -070080
81 protected void waitForMs(long ms) {
82 try {
83 Thread.sleep(ms);
84 } catch (InterruptedException e) {
85 Log.e("TelephonyTestBase", "InterruptedException while waiting: " + e);
86 }
87 }
Brad Ebinger12ec7f22016-05-05 10:40:57 -070088}