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