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