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