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