blob: 044f26be43610abea7224587f20d9012be21283e [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;
23
Brad Ebinger12ec7f22016-05-05 10:40:57 -070024import org.mockito.MockitoAnnotations;
25
26import java.util.concurrent.CountDownLatch;
27import java.util.concurrent.TimeUnit;
28
29/**
30 * Helper class to load Mockito Resources into a test.
31 */
32public class TelephonyTestBase {
33
34 protected Context mContext;
Brad Ebinger12ec7f22016-05-05 10:40:57 -070035
36 public void setUp() throws Exception {
37 mContext = InstrumentationRegistry.getTargetContext();
Brad Ebinger12ec7f22016-05-05 10:40:57 -070038 MockitoAnnotations.initMocks(this);
Brad Ebinger32925b82016-12-02 13:01:49 -080039 // Set up the looper if it does not exist on the test thread.
40 if (Looper.myLooper() == null) {
41 Looper.prepare();
42 }
Brad Ebinger12ec7f22016-05-05 10:40:57 -070043 }
44
45 public void tearDown() throws Exception {
Brad Ebinger12ec7f22016-05-05 10:40:57 -070046 }
47
48 protected final void waitForHandlerAction(Handler h, long timeoutMillis) {
49 final CountDownLatch lock = new CountDownLatch(1);
50 h.post(lock::countDown);
51 while (lock.getCount() > 0) {
52 try {
53 lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
54 } catch (InterruptedException e) {
55 // do nothing
56 }
57 }
58 }
59
60 protected final void waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs) {
61 final CountDownLatch lock = new CountDownLatch(1);
62 h.postDelayed(lock::countDown, delayMs);
63 while (lock.getCount() > 0) {
64 try {
65 lock.await(timeoutMillis, TimeUnit.MILLISECONDS);
66 } catch (InterruptedException e) {
67 // do nothing
68 }
69 }
70 }
71}