blob: 8e061cc84396226683d33fe48269b2cfde18b4a4 [file] [log] [blame]
Fabian Kozynskib176f422019-02-05 09:36:59 -05001/*
2 * Copyright (C) 2019 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
17package com.android.keyguard;
18
19
20import static android.telephony.SubscriptionManager.DATA_ROAMING_DISABLE;
21import static android.telephony.SubscriptionManager.DATA_ROAMING_ENABLE;
22import static android.telephony.SubscriptionManager.NAME_SOURCE_DEFAULT_SOURCE;
23
24import static junit.framework.Assert.assertTrue;
25
26import static org.junit.Assert.assertEquals;
27import static org.mockito.ArgumentMatchers.any;
28import static org.mockito.ArgumentMatchers.anyBoolean;
29import static org.mockito.ArgumentMatchers.anyInt;
30import static org.mockito.Mockito.never;
31import static org.mockito.Mockito.reset;
32import static org.mockito.Mockito.verify;
33import static org.mockito.Mockito.when;
34
35import android.content.Context;
36import android.net.ConnectivityManager;
37import android.net.wifi.WifiManager;
38import android.os.Handler;
39import android.telephony.SubscriptionInfo;
40import android.telephony.TelephonyManager;
41import android.test.suitebuilder.annotation.SmallTest;
42import android.testing.AndroidTestingRunner;
43import android.testing.TestableLooper;
44
45import com.android.internal.telephony.IccCardConstants;
46import com.android.systemui.Dependency;
47import com.android.systemui.SysuiTestCase;
48import com.android.systemui.keyguard.WakefulnessLifecycle;
49
50import org.junit.Before;
51import org.junit.Test;
52import org.junit.runner.RunWith;
53import org.mockito.ArgumentCaptor;
54import org.mockito.Mock;
55import org.mockito.MockitoAnnotations;
56
57import java.util.ArrayList;
58import java.util.HashMap;
59import java.util.List;
60
61@SmallTest
62@RunWith(AndroidTestingRunner.class)
63@TestableLooper.RunWithLooper
64public class CarrierTextControllerTest extends SysuiTestCase {
65
66 private static final CharSequence SEPARATOR = " \u2014 ";
67 private static final String TEST_CARRIER = "TEST_CARRIER";
68 private static final SubscriptionInfo TEST_SUBSCRIPTION = new SubscriptionInfo(0, "", 0,
69 TEST_CARRIER, TEST_CARRIER, NAME_SOURCE_DEFAULT_SOURCE, 0xFFFFFF, "",
70 DATA_ROAMING_DISABLE, null, null, null, null, false, null, "");
71 private static final SubscriptionInfo TEST_SUBSCRIPTION_ROAMING = new SubscriptionInfo(0, "", 0,
72 TEST_CARRIER, TEST_CARRIER, NAME_SOURCE_DEFAULT_SOURCE, 0xFFFFFF, "",
73 DATA_ROAMING_ENABLE, null, null, null, null, false, null, "");
74 @Mock
75 private WifiManager mWifiManager;
76 @Mock
77 private CarrierTextController.CarrierTextCallback mCarrierTextCallback;
78 @Mock
79 private KeyguardUpdateMonitor mKeyguardUpdateMonitor;
80 @Mock
81 private ConnectivityManager mConnectivityManager;
82 @Mock
83 private TelephonyManager mTelephonyManager;
84 private CarrierTextController.CarrierTextCallbackInfo mCarrierTextCallbackInfo;
85
86 private CarrierTextController mCarrierTextController;
87 private TestableLooper mTestableLooper;
88
89 @Before
90 public void setUp() {
91 MockitoAnnotations.initMocks(this);
92 mTestableLooper = TestableLooper.get(this);
93
94 mContext.addMockSystemService(WifiManager.class, mWifiManager);
95 mContext.addMockSystemService(ConnectivityManager.class, mConnectivityManager);
96 mContext.addMockSystemService(TelephonyManager.class, mTelephonyManager);
97 mDependency.injectMockDependency(WakefulnessLifecycle.class);
98 mDependency.injectTestDependency(Dependency.MAIN_HANDLER,
99 new Handler(mTestableLooper.getLooper()));
100
101 mCarrierTextCallbackInfo = new CarrierTextController.CarrierTextCallbackInfo("",
102 new CharSequence[]{}, false, new int[]{});
103 when(mTelephonyManager.getPhoneCount()).thenReturn(2);
104 mCarrierTextController = new TestCarrierTextController(mContext, SEPARATOR, true, true,
105 mKeyguardUpdateMonitor);
106 // This should not start listening on any of the real dependencies
107 mCarrierTextController.setListening(mCarrierTextCallback);
108 }
109
110 @Test
111 public void testWrongSlots() {
112 reset(mCarrierTextCallback);
113 when(mKeyguardUpdateMonitor.getSubscriptionInfo(anyBoolean())).thenReturn(
114 new ArrayList<>());
115 when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(
116 IccCardConstants.State.CARD_IO_ERROR);
117 // This should not produce an out of bounds error, even though there are no subscriptions
118 mCarrierTextController.mCallback.onSimStateChanged(0, -3,
119 IccCardConstants.State.CARD_IO_ERROR);
120 mCarrierTextController.mCallback.onSimStateChanged(0, 3, IccCardConstants.State.READY);
121 verify(mCarrierTextCallback, never()).updateCarrierInfo(any());
122 }
123
124 @Test
125 public void testMoreSlotsThanSubs() {
126 reset(mCarrierTextCallback);
127 when(mKeyguardUpdateMonitor.getSubscriptionInfo(anyBoolean())).thenReturn(
128 new ArrayList<>());
129 when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(
130 IccCardConstants.State.CARD_IO_ERROR);
131 // This should not produce an out of bounds error, even though there are no subscriptions
132 mCarrierTextController.mCallback.onSimStateChanged(0, 1,
133 IccCardConstants.State.CARD_IO_ERROR);
134
135 mTestableLooper.processAllMessages();
136 verify(mCarrierTextCallback).updateCarrierInfo(
137 any(CarrierTextController.CarrierTextCallbackInfo.class));
138 }
139
140 @Test
141 public void testCallback() {
142 reset(mCarrierTextCallback);
143 mCarrierTextController.postToCallback(mCarrierTextCallbackInfo);
144 mTestableLooper.processAllMessages();
145
146 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
147 ArgumentCaptor.forClass(
148 CarrierTextController.CarrierTextCallbackInfo.class);
149 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
150 assertEquals(mCarrierTextCallbackInfo, captor.getValue());
151 }
152
153 @Test
154 public void testNullingCallback() {
155 reset(mCarrierTextCallback);
156
157 mCarrierTextController.postToCallback(mCarrierTextCallbackInfo);
158 mCarrierTextController.setListening(null);
159
160 // This shouldn't produce NPE
161 mTestableLooper.processAllMessages();
162 verify(mCarrierTextCallback).updateCarrierInfo(any());
163 }
164
165 @Test
166 public void testCreateInfo_OneValidSubscription() {
167 reset(mCarrierTextCallback);
168 List<SubscriptionInfo> list = new ArrayList<>();
169 list.add(TEST_SUBSCRIPTION);
170 when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(IccCardConstants.State.READY);
171 when(mKeyguardUpdateMonitor.getSubscriptionInfo(anyBoolean())).thenReturn(list);
172 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
173
174 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
175 ArgumentCaptor.forClass(
176 CarrierTextController.CarrierTextCallbackInfo.class);
177
178 mCarrierTextController.updateCarrierText();
179 mTestableLooper.processAllMessages();
180 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
181
182 CarrierTextController.CarrierTextCallbackInfo info = captor.getValue();
183 assertEquals(1, info.listOfCarriers.length);
184 assertEquals(TEST_CARRIER, info.listOfCarriers[0]);
185 assertEquals(1, info.subscriptionIds.length);
186 }
187
188 @Test
189 public void testCreateInfo_OneValidSubscriptionWithRoaming() {
190 reset(mCarrierTextCallback);
191 List<SubscriptionInfo> list = new ArrayList<>();
192 list.add(TEST_SUBSCRIPTION_ROAMING);
193 when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(IccCardConstants.State.READY);
194 when(mKeyguardUpdateMonitor.getSubscriptionInfo(anyBoolean())).thenReturn(list);
195 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
196
197 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
198 ArgumentCaptor.forClass(
199 CarrierTextController.CarrierTextCallbackInfo.class);
200
201 mCarrierTextController.updateCarrierText();
202 mTestableLooper.processAllMessages();
203 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
204
205 CarrierTextController.CarrierTextCallbackInfo info = captor.getValue();
206 assertEquals(1, info.listOfCarriers.length);
207 assertTrue(info.listOfCarriers[0].toString().contains(TEST_CARRIER));
208 assertEquals(1, info.subscriptionIds.length);
209 }
210
211 @Test
212 public void testCreateInfo_noSubscriptions() {
213 reset(mCarrierTextCallback);
214 when(mKeyguardUpdateMonitor.getSubscriptionInfo(anyBoolean())).thenReturn(
215 new ArrayList<>());
216 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
217 ArgumentCaptor.forClass(
218 CarrierTextController.CarrierTextCallbackInfo.class);
219
220 mCarrierTextController.updateCarrierText();
221 mTestableLooper.processAllMessages();
222 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
223
224 CarrierTextController.CarrierTextCallbackInfo info = captor.getValue();
225 assertEquals(0, info.listOfCarriers.length);
226 assertEquals(0, info.subscriptionIds.length);
227
228 }
229
230 public static class TestCarrierTextController extends CarrierTextController {
231 private KeyguardUpdateMonitor mKUM;
232
233 public TestCarrierTextController(Context context, CharSequence separator,
234 boolean showAirplaneMode, boolean showMissingSim, KeyguardUpdateMonitor kum) {
235 super(context, separator, showAirplaneMode, showMissingSim);
236 mKUM = kum;
237 }
238
239 @Override
240 public void setListening(CarrierTextCallback callback) {
241 super.setListening(callback);
242 mKeyguardUpdateMonitor = mKUM;
243 }
244 }
245}