Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | package com.android.systemui.statusbar.policy; |
| 17 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 18 | import static junit.framework.Assert.assertEquals; |
| 19 | import static junit.framework.Assert.assertTrue; |
| 20 | |
| 21 | import static org.mockito.Matchers.eq; |
| 22 | |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 23 | import android.os.HandlerThread; |
| 24 | import android.telephony.SubscriptionInfo; |
Jason Monk | e427cb8 | 2015-07-28 09:18:08 -0400 | [diff] [blame] | 25 | import android.test.suitebuilder.annotation.SmallTest; |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 26 | |
| 27 | import androidx.test.runner.AndroidJUnit4; |
| 28 | |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 29 | import com.android.systemui.R; |
Jason Monk | 25a52b6 | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 30 | import com.android.systemui.SysuiTestCase; |
Jason Monk | 46dbfb4 | 2016-02-25 14:59:20 -0500 | [diff] [blame] | 31 | import com.android.systemui.statusbar.policy.NetworkController.EmergencyListener; |
Jason Monk | 33f8ae7 | 2015-05-08 10:45:15 -0400 | [diff] [blame] | 32 | import com.android.systemui.statusbar.policy.NetworkController.IconState; |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 33 | import com.android.systemui.statusbar.policy.NetworkController.SignalCallback; |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 34 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 35 | import org.junit.Before; |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 36 | import org.junit.Test; |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 37 | import org.junit.runner.RunWith; |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 38 | import org.mockito.ArgumentCaptor; |
| 39 | import org.mockito.Mock; |
| 40 | import org.mockito.Mockito; |
| 41 | import org.mockito.MockitoAnnotations; |
| 42 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 43 | import java.util.ArrayList; |
| 44 | import java.util.List; |
Jason Monk | 0f0de13 | 2016-12-19 15:36:13 -0500 | [diff] [blame] | 45 | |
Jason Monk | e427cb8 | 2015-07-28 09:18:08 -0400 | [diff] [blame] | 46 | @SmallTest |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 47 | @RunWith(AndroidJUnit4.class) |
Jason Monk | 25a52b6 | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 48 | public class CallbackHandlerTest extends SysuiTestCase { |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 49 | |
| 50 | private CallbackHandler mHandler; |
| 51 | private HandlerThread mHandlerThread; |
| 52 | |
| 53 | @Mock |
| 54 | private EmergencyListener mEmengencyListener; |
| 55 | @Mock |
| 56 | private SignalCallback mSignalCallback; |
| 57 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 58 | @Before |
| 59 | public void setUp() throws Exception { |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 60 | mHandlerThread = new HandlerThread("TestThread"); |
| 61 | mHandlerThread.start(); |
| 62 | mHandler = new CallbackHandler(mHandlerThread.getLooper()); |
| 63 | |
| 64 | MockitoAnnotations.initMocks(this); |
| 65 | mHandler.setListening(mEmengencyListener, true); |
| 66 | mHandler.setListening(mSignalCallback, true); |
| 67 | } |
| 68 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 69 | @Test |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 70 | public void testEmergencyListener() { |
| 71 | mHandler.setEmergencyCallsOnly(true); |
| 72 | waitForCallbacks(); |
| 73 | |
| 74 | ArgumentCaptor<Boolean> captor = ArgumentCaptor.forClass(Boolean.class); |
| 75 | Mockito.verify(mEmengencyListener).setEmergencyCallsOnly(captor.capture()); |
| 76 | assertTrue(captor.getValue()); |
| 77 | } |
| 78 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 79 | @Test |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 80 | public void testSignalCallback_setWifiIndicators() { |
| 81 | boolean enabled = true; |
| 82 | IconState status = new IconState(true, 0, ""); |
| 83 | IconState qs = new IconState(true, 1, ""); |
| 84 | boolean in = true; |
| 85 | boolean out = true; |
| 86 | String description = "Test"; |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 87 | String secondaryLabel = "Secondary label"; |
| 88 | mHandler.setWifiIndicators(enabled, status, qs, in, out, description, true, secondaryLabel); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 89 | waitForCallbacks(); |
| 90 | |
| 91 | ArgumentCaptor<Boolean> enableArg = ArgumentCaptor.forClass(Boolean.class); |
| 92 | ArgumentCaptor<IconState> statusArg = ArgumentCaptor.forClass(IconState.class); |
| 93 | ArgumentCaptor<IconState> qsArg = ArgumentCaptor.forClass(IconState.class); |
| 94 | ArgumentCaptor<Boolean> inArg = ArgumentCaptor.forClass(Boolean.class); |
| 95 | ArgumentCaptor<Boolean> outArg = ArgumentCaptor.forClass(Boolean.class); |
| 96 | ArgumentCaptor<String> descArg = ArgumentCaptor.forClass(String.class); |
Jason Monk | 110e5f6 | 2017-03-31 13:45:12 -0400 | [diff] [blame] | 97 | ArgumentCaptor<Boolean> isTransient = ArgumentCaptor.forClass(Boolean.class); |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 98 | ArgumentCaptor<String> secondary = ArgumentCaptor.forClass(String.class); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 99 | Mockito.verify(mSignalCallback).setWifiIndicators(enableArg.capture(), |
| 100 | statusArg.capture(), qsArg.capture(), inArg.capture(), outArg.capture(), |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 101 | descArg.capture(), isTransient.capture(), secondary.capture()); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 102 | assertEquals(enabled, (boolean) enableArg.getValue()); |
| 103 | assertEquals(status, statusArg.getValue()); |
| 104 | assertEquals(qs, qsArg.getValue()); |
| 105 | assertEquals(in, (boolean) inArg.getValue()); |
| 106 | assertEquals(out, (boolean) outArg.getValue()); |
| 107 | assertEquals(description, descArg.getValue()); |
Jason Monk | 110e5f6 | 2017-03-31 13:45:12 -0400 | [diff] [blame] | 108 | assertTrue(isTransient.getValue()); |
Amin Shaikh | e74dbdd | 2018-03-09 16:05:30 -0500 | [diff] [blame] | 109 | assertEquals(secondaryLabel, secondary.getValue()); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 110 | } |
| 111 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 112 | @Test |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 113 | public void testSignalCallback_setMobileDataIndicators() { |
| 114 | IconState status = new IconState(true, 0, ""); |
| 115 | IconState qs = new IconState(true, 1, ""); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 116 | boolean in = true; |
| 117 | boolean out = true; |
| 118 | String typeDescription = "Test 1"; |
| 119 | String description = "Test 2"; |
Amin Shaikh | d64e626 | 2018-03-08 10:08:13 -0500 | [diff] [blame] | 120 | int type = TelephonyIcons.ICON_1X; |
| 121 | int qsType = TelephonyIcons.ICON_1X; |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 122 | boolean wide = true; |
| 123 | int subId = 5; |
Jason Monk | 0f0de13 | 2016-12-19 15:36:13 -0500 | [diff] [blame] | 124 | boolean roaming = true; |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 125 | mHandler.setMobileDataIndicators(status, qs, type, qsType, in, out, typeDescription, |
| 126 | description, wide, subId, roaming); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 127 | waitForCallbacks(); |
| 128 | |
| 129 | ArgumentCaptor<IconState> statusArg = ArgumentCaptor.forClass(IconState.class); |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 130 | ArgumentCaptor<IconState> qsArg = ArgumentCaptor.forClass(IconState.class); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 131 | ArgumentCaptor<Integer> typeIconArg = ArgumentCaptor.forClass(Integer.class); |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 132 | ArgumentCaptor<Integer> qsTypeIconArg = ArgumentCaptor.forClass(Integer.class); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 133 | ArgumentCaptor<Boolean> inArg = ArgumentCaptor.forClass(Boolean.class); |
| 134 | ArgumentCaptor<Boolean> outArg = ArgumentCaptor.forClass(Boolean.class); |
| 135 | ArgumentCaptor<String> typeContentArg = ArgumentCaptor.forClass(String.class); |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 136 | ArgumentCaptor<String> descArg = ArgumentCaptor.forClass(String.class); |
| 137 | ArgumentCaptor<Boolean> wideArg = ArgumentCaptor.forClass(Boolean.class); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 138 | ArgumentCaptor<Integer> subIdArg = ArgumentCaptor.forClass(Integer.class); |
Andrew Flynn | 2fdbe12 | 2015-06-01 16:34:21 -0400 | [diff] [blame] | 139 | Mockito.verify(mSignalCallback).setMobileDataIndicators(statusArg.capture(), |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 140 | qsArg.capture(), typeIconArg.capture(), qsTypeIconArg.capture(), inArg.capture(), |
| 141 | outArg.capture(), typeContentArg.capture(), descArg.capture(), wideArg.capture(), |
| 142 | subIdArg.capture(), eq(roaming)); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 143 | assertEquals(status, statusArg.getValue()); |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 144 | assertEquals(qs, qsArg.getValue()); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 145 | assertEquals(type, (int) typeIconArg.getValue()); |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 146 | assertEquals(qsType, (int) qsTypeIconArg.getValue()); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 147 | assertEquals(in, (boolean) inArg.getValue()); |
| 148 | assertEquals(out, (boolean) outArg.getValue()); |
| 149 | assertEquals(typeDescription, typeContentArg.getValue()); |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 150 | assertEquals(description, descArg.getValue()); |
| 151 | assertEquals(wide, (boolean) wideArg.getValue()); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 152 | assertEquals(subId, (int) subIdArg.getValue()); |
| 153 | } |
| 154 | |
| 155 | @SuppressWarnings("unchecked") |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 156 | @Test |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 157 | public void testSignalCallback_setSubs() { |
| 158 | List<SubscriptionInfo> subs = new ArrayList<>(); |
| 159 | mHandler.setSubs(subs); |
| 160 | waitForCallbacks(); |
| 161 | |
| 162 | ArgumentCaptor<ArrayList> subsArg = ArgumentCaptor.forClass(ArrayList.class); |
| 163 | Mockito.verify(mSignalCallback).setSubs(subsArg.capture()); |
| 164 | assertTrue(subs == subsArg.getValue()); |
| 165 | } |
| 166 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 167 | @Test |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 168 | public void testSignalCallback_setNoSims() { |
| 169 | boolean noSims = true; |
Jason Monk | 1ff7766 | 2017-09-29 11:17:05 -0400 | [diff] [blame] | 170 | boolean simDetected = false; |
| 171 | mHandler.setNoSims(noSims, simDetected); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 172 | waitForCallbacks(); |
| 173 | |
Jason Monk | 1ff7766 | 2017-09-29 11:17:05 -0400 | [diff] [blame] | 174 | Mockito.verify(mSignalCallback).setNoSims(eq(noSims), eq(simDetected)); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 175 | } |
| 176 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 177 | @Test |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 178 | public void testSignalCallback_setEthernetIndicators() { |
| 179 | IconState state = new IconState(true, R.drawable.stat_sys_ethernet, "Test Description"); |
| 180 | mHandler.setEthernetIndicators(state); |
| 181 | waitForCallbacks(); |
| 182 | |
| 183 | ArgumentCaptor<IconState> iconArg = ArgumentCaptor.forClass(IconState.class); |
| 184 | Mockito.verify(mSignalCallback).setEthernetIndicators(iconArg.capture()); |
| 185 | assertEquals(state, iconArg.getValue()); |
| 186 | } |
| 187 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 188 | @Test |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 189 | public void testSignalCallback_setIsAirplaneMode() { |
| 190 | IconState state = new IconState(true, R.drawable.stat_sys_airplane_mode, "Test Description"); |
| 191 | mHandler.setIsAirplaneMode(state); |
| 192 | waitForCallbacks(); |
| 193 | |
| 194 | ArgumentCaptor<IconState> iconArg = ArgumentCaptor.forClass(IconState.class); |
| 195 | Mockito.verify(mSignalCallback).setIsAirplaneMode(iconArg.capture()); |
| 196 | assertEquals(state, iconArg.getValue()); |
| 197 | } |
| 198 | |
| 199 | private void waitForCallbacks() { |
| 200 | mHandlerThread.quitSafely(); |
| 201 | try { |
| 202 | mHandlerThread.join(); |
| 203 | } catch (InterruptedException e) { |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | } |