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 | a68a497 | 2020-01-30 17:31:23 -0800 | [diff] [blame] | 19 | import static org.mockito.Mockito.spy; |
| 20 | |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 21 | import android.os.Handler; |
Brad Ebinger | 32925b8 | 2016-12-02 13:01:49 -0800 | [diff] [blame] | 22 | import android.os.Looper; |
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 | a15005b | 2020-04-29 15:20:38 -0700 | [diff] [blame] | 25 | import com.android.internal.telephony.PhoneConfigurationManager; |
| 26 | |
tom hsu | 0b59d29 | 2022-09-29 23:49:21 +0800 | [diff] [blame^] | 27 | import org.junit.Rule; |
| 28 | import org.mockito.junit.MockitoJUnit; |
| 29 | import org.mockito.junit.MockitoRule; |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 30 | |
| 31 | import java.util.concurrent.CountDownLatch; |
Brad Ebinger | 63b6f5a | 2020-10-27 11:43:35 -0700 | [diff] [blame] | 32 | import java.util.concurrent.Executor; |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 33 | import java.util.concurrent.TimeUnit; |
| 34 | |
| 35 | /** |
| 36 | * Helper class to load Mockito Resources into a test. |
| 37 | */ |
| 38 | public class TelephonyTestBase { |
tom hsu | 0b59d29 | 2022-09-29 23:49:21 +0800 | [diff] [blame^] | 39 | @Rule public final MockitoRule mocks = MockitoJUnit.rule(); |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 40 | |
Brad Ebinger | 792729e | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 41 | protected TestContext mContext; |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 42 | |
| 43 | public void setUp() throws Exception { |
Brad Ebinger | a68a497 | 2020-01-30 17:31:23 -0800 | [diff] [blame] | 44 | mContext = spy(new TestContext()); |
Brad Ebinger | 32925b8 | 2016-12-02 13:01:49 -0800 | [diff] [blame] | 45 | // Set up the looper if it does not exist on the test thread. |
| 46 | if (Looper.myLooper() == null) { |
| 47 | Looper.prepare(); |
Brad Ebinger | 5a80355 | 2017-08-17 16:47:25 -0700 | [diff] [blame] | 48 | // Wait until the looper is not null anymore |
| 49 | for(int i = 0; i < 5; i++) { |
| 50 | if (Looper.myLooper() != null) { |
| 51 | break; |
| 52 | } |
| 53 | Looper.prepare(); |
| 54 | Thread.sleep(100); |
| 55 | } |
Brad Ebinger | 32925b8 | 2016-12-02 13:01:49 -0800 | [diff] [blame] | 56 | } |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | public void tearDown() throws Exception { |
Brad Ebinger | a15005b | 2020-04-29 15:20:38 -0700 | [diff] [blame] | 60 | // Ensure there are no static references to handlers after test completes. |
| 61 | PhoneConfigurationManager.unregisterAllMultiSimConfigChangeRegistrants(); |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Brad Ebinger | 63b6f5a | 2020-10-27 11:43:35 -0700 | [diff] [blame] | 64 | protected final boolean waitForExecutorAction(Executor executor, long timeoutMillis) { |
| 65 | final CountDownLatch lock = new CountDownLatch(1); |
Brad Ebinger | 63b6f5a | 2020-10-27 11:43:35 -0700 | [diff] [blame] | 66 | executor.execute(() -> { |
Brad Ebinger | 63b6f5a | 2020-10-27 11:43:35 -0700 | [diff] [blame] | 67 | lock.countDown(); |
| 68 | }); |
| 69 | while (lock.getCount() > 0) { |
| 70 | try { |
| 71 | return lock.await(timeoutMillis, TimeUnit.MILLISECONDS); |
| 72 | } catch (InterruptedException e) { |
| 73 | // do nothing |
| 74 | } |
| 75 | } |
| 76 | return true; |
| 77 | } |
| 78 | |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 79 | protected final void waitForHandlerAction(Handler h, long timeoutMillis) { |
| 80 | final CountDownLatch lock = new CountDownLatch(1); |
| 81 | h.post(lock::countDown); |
| 82 | while (lock.getCount() > 0) { |
| 83 | try { |
| 84 | lock.await(timeoutMillis, TimeUnit.MILLISECONDS); |
| 85 | } catch (InterruptedException e) { |
| 86 | // do nothing |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | protected final void waitForHandlerActionDelayed(Handler h, long timeoutMillis, long delayMs) { |
| 92 | final CountDownLatch lock = new CountDownLatch(1); |
| 93 | h.postDelayed(lock::countDown, delayMs); |
| 94 | while (lock.getCount() > 0) { |
| 95 | try { |
| 96 | lock.await(timeoutMillis, TimeUnit.MILLISECONDS); |
| 97 | } catch (InterruptedException e) { |
| 98 | // do nothing |
| 99 | } |
| 100 | } |
| 101 | } |
Sanket Padawe | d9a427e | 2017-06-08 12:03:07 -0700 | [diff] [blame] | 102 | |
| 103 | protected void waitForMs(long ms) { |
| 104 | try { |
| 105 | Thread.sleep(ms); |
| 106 | } catch (InterruptedException e) { |
| 107 | Log.e("TelephonyTestBase", "InterruptedException while waiting: " + e); |
| 108 | } |
| 109 | } |
Brad Ebinger | 792729e | 2019-12-09 16:12:57 -0800 | [diff] [blame] | 110 | |
| 111 | protected TestContext getTestContext() { |
| 112 | return mContext; |
| 113 | } |
Brad Ebinger | 12ec7f2 | 2016-05-05 10:40:57 -0700 | [diff] [blame] | 114 | } |