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; |
Evan Laird | 83c87e5 | 2019-09-24 19:14:05 -0400 | [diff] [blame] | 118 | CharSequence typeDescription = "Test 1"; |
| 119 | CharSequence typeDescriptionHtml = "<b>Test 1</b>"; |
| 120 | CharSequence description = "Test 2"; |
Amin Shaikh | d64e626 | 2018-03-08 10:08:13 -0500 | [diff] [blame] | 121 | int type = TelephonyIcons.ICON_1X; |
| 122 | int qsType = TelephonyIcons.ICON_1X; |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 123 | boolean wide = true; |
| 124 | int subId = 5; |
Jason Monk | 0f0de13 | 2016-12-19 15:36:13 -0500 | [diff] [blame] | 125 | boolean roaming = true; |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 126 | mHandler.setMobileDataIndicators(status, qs, type, qsType, in, out, typeDescription, |
Evan Laird | 83c87e5 | 2019-09-24 19:14:05 -0400 | [diff] [blame] | 127 | typeDescriptionHtml, description, wide, subId, roaming); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 128 | waitForCallbacks(); |
| 129 | |
| 130 | ArgumentCaptor<IconState> statusArg = ArgumentCaptor.forClass(IconState.class); |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 131 | ArgumentCaptor<IconState> qsArg = ArgumentCaptor.forClass(IconState.class); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 132 | ArgumentCaptor<Integer> typeIconArg = ArgumentCaptor.forClass(Integer.class); |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 133 | ArgumentCaptor<Integer> qsTypeIconArg = ArgumentCaptor.forClass(Integer.class); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 134 | ArgumentCaptor<Boolean> inArg = ArgumentCaptor.forClass(Boolean.class); |
| 135 | ArgumentCaptor<Boolean> outArg = ArgumentCaptor.forClass(Boolean.class); |
Evan Laird | 83c87e5 | 2019-09-24 19:14:05 -0400 | [diff] [blame] | 136 | ArgumentCaptor<CharSequence> typeContentArg = ArgumentCaptor.forClass(CharSequence.class); |
| 137 | ArgumentCaptor<CharSequence> typeContentHtmlArg = |
| 138 | ArgumentCaptor.forClass(CharSequence.class); |
| 139 | ArgumentCaptor<CharSequence> descArg = ArgumentCaptor.forClass(CharSequence.class); |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 140 | ArgumentCaptor<Boolean> wideArg = ArgumentCaptor.forClass(Boolean.class); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 141 | ArgumentCaptor<Integer> subIdArg = ArgumentCaptor.forClass(Integer.class); |
Andrew Flynn | 2fdbe12 | 2015-06-01 16:34:21 -0400 | [diff] [blame] | 142 | Mockito.verify(mSignalCallback).setMobileDataIndicators(statusArg.capture(), |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 143 | qsArg.capture(), typeIconArg.capture(), qsTypeIconArg.capture(), inArg.capture(), |
Evan Laird | 83c87e5 | 2019-09-24 19:14:05 -0400 | [diff] [blame] | 144 | outArg.capture(), typeContentArg.capture(), typeContentHtmlArg.capture(), |
| 145 | descArg.capture(), wideArg.capture(), subIdArg.capture(), eq(roaming)); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 146 | assertEquals(status, statusArg.getValue()); |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 147 | assertEquals(qs, qsArg.getValue()); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 148 | assertEquals(type, (int) typeIconArg.getValue()); |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 149 | assertEquals(qsType, (int) qsTypeIconArg.getValue()); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 150 | assertEquals(in, (boolean) inArg.getValue()); |
| 151 | assertEquals(out, (boolean) outArg.getValue()); |
| 152 | assertEquals(typeDescription, typeContentArg.getValue()); |
Evan Laird | 83c87e5 | 2019-09-24 19:14:05 -0400 | [diff] [blame] | 153 | assertEquals(typeDescriptionHtml, typeContentHtmlArg.getValue()); |
Jason Monk | 7e6c83c | 2017-04-26 14:35:24 -0400 | [diff] [blame] | 154 | assertEquals(description, descArg.getValue()); |
| 155 | assertEquals(wide, (boolean) wideArg.getValue()); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 156 | assertEquals(subId, (int) subIdArg.getValue()); |
| 157 | } |
| 158 | |
| 159 | @SuppressWarnings("unchecked") |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 160 | @Test |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 161 | public void testSignalCallback_setSubs() { |
| 162 | List<SubscriptionInfo> subs = new ArrayList<>(); |
| 163 | mHandler.setSubs(subs); |
| 164 | waitForCallbacks(); |
| 165 | |
| 166 | ArgumentCaptor<ArrayList> subsArg = ArgumentCaptor.forClass(ArrayList.class); |
| 167 | Mockito.verify(mSignalCallback).setSubs(subsArg.capture()); |
| 168 | assertTrue(subs == subsArg.getValue()); |
| 169 | } |
| 170 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 171 | @Test |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 172 | public void testSignalCallback_setNoSims() { |
| 173 | boolean noSims = true; |
Jason Monk | 1ff7766 | 2017-09-29 11:17:05 -0400 | [diff] [blame] | 174 | boolean simDetected = false; |
| 175 | mHandler.setNoSims(noSims, simDetected); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 176 | waitForCallbacks(); |
| 177 | |
Jason Monk | 1ff7766 | 2017-09-29 11:17:05 -0400 | [diff] [blame] | 178 | Mockito.verify(mSignalCallback).setNoSims(eq(noSims), eq(simDetected)); |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 179 | } |
| 180 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 181 | @Test |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 182 | public void testSignalCallback_setEthernetIndicators() { |
| 183 | IconState state = new IconState(true, R.drawable.stat_sys_ethernet, "Test Description"); |
| 184 | mHandler.setEthernetIndicators(state); |
| 185 | waitForCallbacks(); |
| 186 | |
| 187 | ArgumentCaptor<IconState> iconArg = ArgumentCaptor.forClass(IconState.class); |
| 188 | Mockito.verify(mSignalCallback).setEthernetIndicators(iconArg.capture()); |
| 189 | assertEquals(state, iconArg.getValue()); |
| 190 | } |
| 191 | |
Geoffrey Pitsch | 2c403db | 2016-08-26 09:09:39 -0400 | [diff] [blame] | 192 | @Test |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 193 | public void testSignalCallback_setIsAirplaneMode() { |
| 194 | IconState state = new IconState(true, R.drawable.stat_sys_airplane_mode, "Test Description"); |
| 195 | mHandler.setIsAirplaneMode(state); |
| 196 | waitForCallbacks(); |
| 197 | |
| 198 | ArgumentCaptor<IconState> iconArg = ArgumentCaptor.forClass(IconState.class); |
| 199 | Mockito.verify(mSignalCallback).setIsAirplaneMode(iconArg.capture()); |
| 200 | assertEquals(state, iconArg.getValue()); |
| 201 | } |
| 202 | |
| 203 | private void waitForCallbacks() { |
| 204 | mHandlerThread.quitSafely(); |
| 205 | try { |
| 206 | mHandlerThread.join(); |
| 207 | } catch (InterruptedException e) { |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | } |