Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.os.Handler; |
Brad Ebinger | 32925b8 | 2016-12-02 13:01:49 -0800 | [diff] [blame] | 21 | import android.os.Looper; |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 22 | import android.support.test.InstrumentationRegistry; |
Sanket Padawe | d9a427e | 2017-06-08 12:03:07 -0700 | [diff] [blame^] | 23 | import android.util.Log; |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 24 | |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 25 | import org.mockito.MockitoAnnotations; |
| 26 | |
| 27 | import java.util.concurrent.CountDownLatch; |
| 28 | import java.util.concurrent.TimeUnit; |
| 29 | |
| 30 | /** |
| 31 | * Helper class to load Mockito Resources into a test. |
| 32 | */ |
| 33 | public class TelephonyTestBase { |
| 34 | |
| 35 | protected Context mContext; |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 36 | |
| 37 | public void setUp() throws Exception { |
| 38 | mContext = InstrumentationRegistry.getTargetContext(); |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 39 | MockitoAnnotations.initMocks(this); |
Brad Ebinger | 32925b8 | 2016-12-02 13:01:49 -0800 | [diff] [blame] | 40 | // Set up the looper if it does not exist on the test thread. |
| 41 | if (Looper.myLooper() == null) { |
| 42 | Looper.prepare(); |
| 43 | } |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | public void tearDown() throws Exception { |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | protected final void waitForHandlerAction(Handler h, long timeoutMillis) { |
| 50 | final CountDownLatch lock = new CountDownLatch(1); |
| 51 | h.post(lock::countDown); |
| 52 | while (lock.getCount() > 0) { |
| 53 | try { |
| 54 | lock.await(timeoutMillis, TimeUnit.MILLISECONDS); |
| 55 | } catch (InterruptedException e) { |
| 56 | // do nothing |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | protected final void waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs) { |
| 62 | final CountDownLatch lock = new CountDownLatch(1); |
| 63 | h.postDelayed(lock::countDown, delayMs); |
| 64 | while (lock.getCount() > 0) { |
| 65 | try { |
| 66 | lock.await(timeoutMillis, TimeUnit.MILLISECONDS); |
| 67 | } catch (InterruptedException e) { |
| 68 | // do nothing |
| 69 | } |
| 70 | } |
| 71 | } |
Sanket Padawe | d9a427e | 2017-06-08 12:03:07 -0700 | [diff] [blame^] | 72 | |
| 73 | protected void waitForMs(long ms) { |
| 74 | try { |
| 75 | Thread.sleep(ms); |
| 76 | } catch (InterruptedException e) { |
| 77 | Log.e("TelephonyTestBase", "InterruptedException while waiting: " + e); |
| 78 | } |
| 79 | } |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 80 | } |