1 /* 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 17 package com.android.keyguard; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.test.suitebuilder.annotation.SmallTest; 22 import android.testing.AndroidTestingRunner; 23 import android.testing.TestableLooper; 24 import android.testing.TestableLooper.RunWithLooper; 25 26 import com.android.internal.telephony.IccCardConstants; 27 import com.android.internal.telephony.TelephonyIntents; 28 import com.android.systemui.SysuiTestCase; 29 30 import org.junit.Assert; 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 35 import java.util.concurrent.atomic.AtomicBoolean; 36 37 @SmallTest 38 @RunWith(AndroidTestingRunner.class) 39 // 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) 45 public 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 58 TestableKeyguardUpdateMonitor keyguardUpdateMonitor = 59 new TestableKeyguardUpdateMonitor(getContext()); 60 61 keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent); 62 mTestableLooper.processAllMessages(); 63 Assert.assertTrue("onSimStateChanged not called", 64 keyguardUpdateMonitor.hasSimStateJustChanged()); 65 66 intent.putExtra(TelephonyIntents.EXTRA_REBROADCAST_ON_UNLOCK, true); 67 keyguardUpdateMonitor.mBroadcastReceiver.onReceive(getContext(), intent); 68 mTestableLooper.processAllMessages(); 69 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 } 92 } 93 } 94