blob: d46a9747ac240243e433bc95330de00c718eee6d [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
Lucas Dupin7ff82b02018-05-16 12:14:10 -070019import android.content.Context;
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080020import android.content.Intent;
21import android.test.suitebuilder.annotation.SmallTest;
22import android.testing.AndroidTestingRunner;
23import android.testing.TestableLooper;
24import android.testing.TestableLooper.RunWithLooper;
25
26import com.android.internal.telephony.IccCardConstants;
27import com.android.internal.telephony.TelephonyIntents;
28import com.android.systemui.SysuiTestCase;
29
30import org.junit.Assert;
31import org.junit.Before;
32import org.junit.Test;
33import org.junit.runner.RunWith;
34
35import java.util.concurrent.atomic.AtomicBoolean;
36
37@SmallTest
38@RunWith(AndroidTestingRunner.class)
Lucas Dupin7ff82b02018-05-16 12:14:10 -070039// We must run on the main looper because KeyguardUpdateMonitor#mHandler is initialized with
40// new Handler(Looper.getMainLooper()).
41//
42// Using the main looper should be avoided whenever possible, please don't copy this over to
43// new tests.
44@RunWithLooper(setAsMainLooper = true)
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080045public class KeyguardUpdateMonitorTest extends SysuiTestCase {
46
47 private TestableLooper mTestableLooper;
48
49 @Before
50 public void setup() {
51 mTestableLooper = TestableLooper.get(this);
52 }
53
54 @Test
55 public void testIgnoresSimStateCallback_rebroadcast() {
56 Intent intent = new Intent(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
57
Lucas Dupin7ff82b02018-05-16 12:14:10 -070058 TestableKeyguardUpdateMonitor keyguardUpdateMonitor =
59 new TestableKeyguardUpdateMonitor(getContext());
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080060
61 keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent);
62 mTestableLooper.processAllMessages();
Lucas Dupin7ff82b02018-05-16 12:14:10 -070063 Assert.assertTrue("onSimStateChanged not called",
64 keyguardUpdateMonitor.hasSimStateJustChanged());
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080065
66 intent.putExtra(TelephonyIntents.EXTRA_REBROADCAST_ON_UNLOCK, true);
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080067 keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent);
68 mTestableLooper.processAllMessages();
Lucas Dupin7ff82b02018-05-16 12:14:10 -070069 Assert.assertFalse("onSimStateChanged should have been skipped",
70 keyguardUpdateMonitor.hasSimStateJustChanged());
71 }
72
73 private class TestableKeyguardUpdateMonitor extends KeyguardUpdateMonitor {
74 AtomicBoolean mSimStateChanged = new AtomicBoolean(false);
75
76 protected TestableKeyguardUpdateMonitor(Context context) {
77 super(context);
78 // Avoid race condition when unexpected broadcast could be received.
79 context.unregisterReceiver(mBroadcastReceiver);
80 }
81
82 public boolean hasSimStateJustChanged() {
83 return mSimStateChanged.getAndSet(false);
84 }
85
86 @Override
87 protected void handleSimStateChange(int subId, int slotId,
88 IccCardConstants.State state) {
89 mSimStateChanged.set(true);
90 super.handleSimStateChange(subId, slotId, state);
91 }
Lucas Dupin5e0f0d22018-02-26 13:32:16 -080092 }
93}