blob: 2055519a1622564859d3d0b540340bb382a15a10 [file] [log] [blame]
Lucas Dupin5e0f0d22018-02-26 13:32:16 -08001/*
2 * Copyright (C) 2018 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
Bill Lin6c7ccab2018-07-05 17:48:49 +080019import static com.google.common.truth.Truth.assertThat;
20
21import com.google.common.truth.Truth.*;
22
Lucas Dupin7ff82b02018-05-16 12:14:10 -070023import android.content.Context;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080024import android.content.Intent;
Bill Lin6c7ccab2018-07-05 17:48:49 +080025import android.os.Bundle;
26import android.telephony.ServiceState;
27import android.telephony.SubscriptionManager;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080028import android.test.suitebuilder.annotation.SmallTest;
29import android.testing.AndroidTestingRunner;
30import android.testing.TestableLooper;
31import android.testing.TestableLooper.RunWithLooper;
32
33import com.android.internal.telephony.IccCardConstants;
Bill Lin6c7ccab2018-07-05 17:48:49 +080034import com.android.internal.telephony.PhoneConstants;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080035import com.android.internal.telephony.TelephonyIntents;
36import com.android.systemui.SysuiTestCase;
37
38import org.junit.Assert;
39import org.junit.Before;
40import org.junit.Test;
41import org.junit.runner.RunWith;
42
43import java.util.concurrent.atomic.AtomicBoolean;
44
45@SmallTest
46@RunWith(AndroidTestingRunner.class)
Lucas Dupin7ff82b02018-05-16 12:14:10 -070047// We must run on the main looper because KeyguardUpdateMonitor#mHandler is initialized with
48// new Handler(Looper.getMainLooper()).
49//
50// Using the main looper should be avoided whenever possible, please don't copy this over to
51// new tests.
52@RunWithLooper(setAsMainLooper = true)
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080053public class KeyguardUpdateMonitorTest extends SysuiTestCase {
54
55 private TestableLooper mTestableLooper;
56
57 @Before
58 public void setup() {
59 mTestableLooper = TestableLooper.get(this);
60 }
61
62 @Test
63 public void testIgnoresSimStateCallback_rebroadcast() {
64 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
65
Lucas Dupin7ff82b02018-05-16 12:14:10 -070066 TestableKeyguardUpdateMonitor keyguardUpdateMonitor =
67 new TestableKeyguardUpdateMonitor(getContext());
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080068
69 keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent);
70 mTestableLooper.processAllMessages();
Lucas Dupin7ff82b02018-05-16 12:14:10 -070071 Assert.assertTrue("onSimStateChanged not called",
72 keyguardUpdateMonitor.hasSimStateJustChanged());
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080073
74 intent.putExtra(TelephonyIntents.EXTRA_REBROADCAST_ON_UNLOCK, true);
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080075 keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent);
76 mTestableLooper.processAllMessages();
Lucas Dupin7ff82b02018-05-16 12:14:10 -070077 Assert.assertFalse("onSimStateChanged should have been skipped",
78 keyguardUpdateMonitor.hasSimStateJustChanged());
79 }
80
Bill Lin6c7ccab2018-07-05 17:48:49 +080081 @Test
82 public void testTelephonyCapable_BootInitState() {
83 TestableKeyguardUpdateMonitor keyguardUpdateMonitor =
84 new TestableKeyguardUpdateMonitor(getContext());
85 assertThat(keyguardUpdateMonitor.mTelephonyCapable).isFalse();
86 }
87
88 @Test
89 public void testTelephonyCapable_SimState_Absent() {
90 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
91 intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE
92 , IccCardConstants.INTENT_VALUE_ICC_ABSENT);
93 TestableKeyguardUpdateMonitor keyguardUpdateMonitor =
94 new TestableKeyguardUpdateMonitor(getContext());
95 keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
96 , putPhoneInfo(intent,null, false));
97 mTestableLooper.processAllMessages();
98 assertThat(keyguardUpdateMonitor.mTelephonyCapable).isTrue();
99 }
100
101 @Test
102 public void testTelephonyCapable_SimInvalid_ServiceState_InService() {
103 // SERVICE_STATE - IN_SERVICE, but SIM_STATE is invalid TelephonyCapable should be False
104 TestableKeyguardUpdateMonitor keyguardUpdateMonitor =
105 new TestableKeyguardUpdateMonitor(getContext());
106 Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
107 Bundle data = new Bundle();
108 ServiceState state = new ServiceState();
109 state.setState(ServiceState.STATE_IN_SERVICE);
110 state.fillInNotifierBundle(data);
111 keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
112 , putPhoneInfo(intent, data, false));
113 mTestableLooper.processAllMessages();
114 assertThat(keyguardUpdateMonitor.mTelephonyCapable).isFalse();
115 }
116
117 @Test
118 public void testTelephonyCapable_SimValid_ServiceState_PowerOff() {
119 // Simulate AirplaneMode case, SERVICE_STATE - POWER_OFF, check TelephonyCapable False
120 // Only receive ServiceState callback IN_SERVICE -> OUT_OF_SERVICE -> POWER_OFF
121 TestableKeyguardUpdateMonitor keyguardUpdateMonitor =
122 new TestableKeyguardUpdateMonitor(getContext());
123 Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
124 intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE
125 , IccCardConstants.INTENT_VALUE_ICC_LOADED);
126 Bundle data = new Bundle();
127 ServiceState state = new ServiceState();
128 state.setState(ServiceState.STATE_POWER_OFF);
129 state.fillInNotifierBundle(data);
130 keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
131 , putPhoneInfo(intent, data, true));
132 mTestableLooper.processAllMessages();
133 assertThat(keyguardUpdateMonitor.mTelephonyCapable).isTrue();
134 }
135
136 /* Normal SIM inserted flow
137 * ServiceState: ---OutOfServie----->PowerOff->OutOfServie--->InService
138 * SimState: ----NOT_READY---->READY----------------------LOADED>>>
139 * Subscription: --------null---->null--->"Chunghwa Telecom"-------->>>
140 * System: -------------------------------BOOT_COMPLETED------>>>
141 * TelephonyCapable:(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)-(F)------(T)-(T)>>
142 */
143 @Test
144 public void testTelephonyCapable_BootInitState_ServiceState_OutOfService() {
145 TestableKeyguardUpdateMonitor keyguardUpdateMonitor =
146 new TestableKeyguardUpdateMonitor(getContext());
147 Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
148 Bundle data = new Bundle();
149 ServiceState state = new ServiceState();
150 state.setState(ServiceState.STATE_OUT_OF_SERVICE);
151 state.fillInNotifierBundle(data);
152 intent.putExtras(data);
153 keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
154 , putPhoneInfo(intent, data, false));
155 mTestableLooper.processAllMessages();
156 assertThat(keyguardUpdateMonitor.mTelephonyCapable).isFalse();
157 }
158
159 @Test
160 public void testTelephonyCapable_BootInitState_SimState_NotReady() {
161 TestableKeyguardUpdateMonitor keyguardUpdateMonitor =
162 new TestableKeyguardUpdateMonitor(getContext());
163 Bundle data = new Bundle();
164 ServiceState state = new ServiceState();
165 state.setState(ServiceState.STATE_OUT_OF_SERVICE);
166 state.fillInNotifierBundle(data);
167 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
168 intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE
169 , IccCardConstants.INTENT_VALUE_ICC_NOT_READY);
170 keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
171 , putPhoneInfo(intent, data, false));
172 mTestableLooper.processAllMessages();
173 assertThat(keyguardUpdateMonitor.mTelephonyCapable).isFalse();
174 }
175
176 @Test
177 public void testTelephonyCapable_BootInitState_SimState_Ready() {
178 TestableKeyguardUpdateMonitor keyguardUpdateMonitor =
179 new TestableKeyguardUpdateMonitor(getContext());
180 Bundle data = new Bundle();
181 ServiceState state = new ServiceState();
182 state.setState(ServiceState.STATE_OUT_OF_SERVICE);
183 state.fillInNotifierBundle(data);
184 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
185 intent.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE
186 , IccCardConstants.INTENT_VALUE_ICC_READY);
187 keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
188 , putPhoneInfo(intent, data, false));
189 mTestableLooper.processAllMessages();
190 assertThat(keyguardUpdateMonitor.mTelephonyCapable).isFalse();
191 }
192
193 @Test
194 public void testTelephonyCapable_BootInitState_ServiceState_PowerOff() {
195 TestableKeyguardUpdateMonitor keyguardUpdateMonitor =
196 new TestableKeyguardUpdateMonitor(getContext());
197 Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
198 Bundle data = new Bundle();
199 ServiceState state = new ServiceState();
200 state.setState(ServiceState.STATE_POWER_OFF);
201 state.fillInNotifierBundle(data);
202 keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
203 , putPhoneInfo(intent, data, false));
204 mTestableLooper.processAllMessages();
205 assertThat(keyguardUpdateMonitor.mTelephonyCapable).isFalse();
206 }
207
208 @Test
209 public void testTelephonyCapable_SimValid_ServiceState_InService() {
210 TestableKeyguardUpdateMonitor keyguardUpdateMonitor =
211 new TestableKeyguardUpdateMonitor(getContext());
212 Intent intent = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
213 Bundle data = new Bundle();
214 ServiceState state = new ServiceState();
215 state.setState(ServiceState.STATE_IN_SERVICE);
216 state.fillInNotifierBundle(data);
217 keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
218 , putPhoneInfo(intent, data, true));
219 mTestableLooper.processAllMessages();
220 assertThat(keyguardUpdateMonitor.mTelephonyCapable).isTrue();
221 }
222
223 @Test
224 public void testTelephonyCapable_SimValid_SimState_Loaded() {
225 TestableKeyguardUpdateMonitor keyguardUpdateMonitor =
226 new TestableKeyguardUpdateMonitor(getContext());
227 Bundle data = new Bundle();
228 ServiceState state = new ServiceState();
229 state.setState(ServiceState.STATE_IN_SERVICE);
230 state.fillInNotifierBundle(data);
231 Intent intentSimState = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
232 intentSimState.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE
233 , IccCardConstants.INTENT_VALUE_ICC_LOADED);
234 keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
235 , putPhoneInfo(intentSimState, data, true));
236 mTestableLooper.processAllMessages();
237 // Even SimState Loaded, still need ACTION_SERVICE_STATE_CHANGED turn on mTelephonyCapable
238 assertThat(keyguardUpdateMonitor.mTelephonyCapable).isFalse();
239
240 Intent intentServiceState = new Intent(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED);
241 intentSimState.putExtra(IccCardConstants.INTENT_KEY_ICC_STATE
242 , IccCardConstants.INTENT_VALUE_ICC_LOADED);
243 keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext()
244 , putPhoneInfo(intentServiceState, data, true));
245 mTestableLooper.processAllMessages();
246 assertThat(keyguardUpdateMonitor.mTelephonyCapable).isTrue();
247 }
248
249 private Intent putPhoneInfo(Intent intent, Bundle data, Boolean simInited) {
250 int subscription = simInited
251 ? 1/* mock subid=1 */ : SubscriptionManager.DUMMY_SUBSCRIPTION_ID_BASE;
252 if (data != null) intent.putExtras(data);
253 intent.putExtra(PhoneConstants.PHONE_NAME_KEY, "Phone");
254 intent.putExtra("subscription", subscription);
255 intent.putExtra("slot", 0/* SLOT 1 */);
256 return intent;
257 }
258
Lucas Dupin7ff82b02018-05-16 12:14:10 -0700259 private class TestableKeyguardUpdateMonitor extends KeyguardUpdateMonitor {
260 AtomicBoolean mSimStateChanged = new AtomicBoolean(false);
261
262 protected TestableKeyguardUpdateMonitor(Context context) {
263 super(context);
264 // Avoid race condition when unexpected broadcast could be received.
265 context.unregisterReceiver(mBroadcastReceiver);
266 }
267
268 public boolean hasSimStateJustChanged() {
269 return mSimStateChanged.getAndSet(false);
270 }
271
272 @Override
273 protected void handleSimStateChange(int subId, int slotId,
274 IccCardConstants.State state) {
275 mSimStateChanged.set(true);
276 super.handleSimStateChange(subId, slotId, state);
277 }
Lucas Dupin5e0f0d22018-02-26 13:32:16 -0800278 }
279}