Erik Kline | 227648f | 2017-01-20 20:26:10 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.server.connectivity.tethering; |
| 18 | |
| 19 | import static com.android.internal.telephony.IccCardConstants.INTENT_VALUE_ICC_ABSENT; |
| 20 | import static com.android.internal.telephony.IccCardConstants.INTENT_VALUE_ICC_LOADED; |
| 21 | import static com.android.internal.telephony.IccCardConstants.INTENT_KEY_ICC_STATE; |
| 22 | import static com.android.internal.telephony.TelephonyIntents.ACTION_SIM_STATE_CHANGED; |
| 23 | |
| 24 | import static org.junit.Assert.assertEquals; |
| 25 | import static org.mockito.Mockito.reset; |
| 26 | |
| 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
| 29 | import android.os.Handler; |
| 30 | import android.os.Looper; |
| 31 | import android.os.UserHandle; |
| 32 | |
| 33 | import android.support.test.filters.SmallTest; |
| 34 | import android.support.test.runner.AndroidJUnit4; |
| 35 | |
| 36 | import com.android.internal.util.test.BroadcastInterceptingContext; |
| 37 | |
| 38 | import org.junit.After; |
| 39 | import org.junit.Before; |
| 40 | import org.junit.BeforeClass; |
| 41 | import org.junit.runner.RunWith; |
| 42 | import org.junit.Test; |
| 43 | import org.mockito.Mock; |
| 44 | import org.mockito.Mockito; |
| 45 | import org.mockito.MockitoAnnotations; |
| 46 | |
| 47 | |
| 48 | @RunWith(AndroidJUnit4.class) |
| 49 | @SmallTest |
| 50 | public class SimChangeListenerTest { |
| 51 | private static final int EVENT_UNM_UPDATE = 1; |
| 52 | |
| 53 | @Mock private Context mContext; |
| 54 | private BroadcastInterceptingContext mServiceContext; |
| 55 | private Handler mHandler; |
| 56 | private SimChangeListener mSCL; |
| 57 | private int mCallbackCount; |
| 58 | |
| 59 | private void doCallback() { mCallbackCount++; } |
| 60 | |
| 61 | private class MockContext extends BroadcastInterceptingContext { |
| 62 | MockContext(Context base) { |
| 63 | super(base); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | @BeforeClass |
| 68 | public static void setUpBeforeClass() throws Exception { |
| 69 | if (Looper.myLooper() == null) { |
| 70 | Looper.prepare(); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | @Before public void setUp() throws Exception { |
| 75 | MockitoAnnotations.initMocks(this); |
| 76 | reset(mContext); |
| 77 | mServiceContext = new MockContext(mContext); |
| 78 | mHandler = new Handler(Looper.myLooper()); |
| 79 | mCallbackCount = 0; |
| 80 | mSCL = new SimChangeListener(mServiceContext, mHandler, () -> doCallback()); |
| 81 | } |
| 82 | |
| 83 | @After public void tearDown() throws Exception { |
| 84 | if (mSCL != null) { |
| 85 | mSCL.stopListening(); |
| 86 | mSCL = null; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private void sendSimStateChangeIntent(String state) { |
| 91 | final Intent intent = new Intent(ACTION_SIM_STATE_CHANGED); |
| 92 | intent.putExtra(INTENT_KEY_ICC_STATE, state); |
| 93 | mServiceContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 94 | } |
| 95 | |
| 96 | @Test |
| 97 | public void testNotSeenFollowedBySeenCallsCallback() { |
| 98 | mSCL.startListening(); |
| 99 | |
| 100 | sendSimStateChangeIntent(INTENT_VALUE_ICC_ABSENT); |
| 101 | sendSimStateChangeIntent(INTENT_VALUE_ICC_LOADED); |
| 102 | assertEquals(1, mCallbackCount); |
| 103 | |
| 104 | sendSimStateChangeIntent(INTENT_VALUE_ICC_ABSENT); |
| 105 | sendSimStateChangeIntent(INTENT_VALUE_ICC_LOADED); |
| 106 | assertEquals(2, mCallbackCount); |
| 107 | |
| 108 | mSCL.stopListening(); |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void testNotListeningDoesNotCallback() { |
| 113 | sendSimStateChangeIntent(INTENT_VALUE_ICC_ABSENT); |
| 114 | sendSimStateChangeIntent(INTENT_VALUE_ICC_LOADED); |
| 115 | assertEquals(0, mCallbackCount); |
| 116 | |
| 117 | sendSimStateChangeIntent(INTENT_VALUE_ICC_ABSENT); |
| 118 | sendSimStateChangeIntent(INTENT_VALUE_ICC_LOADED); |
| 119 | assertEquals(0, mCallbackCount); |
| 120 | } |
| 121 | |
| 122 | @Test |
| 123 | public void testSeenOnlyDoesNotCallback() { |
| 124 | mSCL.startListening(); |
| 125 | |
| 126 | sendSimStateChangeIntent(INTENT_VALUE_ICC_LOADED); |
| 127 | assertEquals(0, mCallbackCount); |
| 128 | |
| 129 | sendSimStateChangeIntent(INTENT_VALUE_ICC_LOADED); |
| 130 | assertEquals(0, mCallbackCount); |
| 131 | |
| 132 | mSCL.stopListening(); |
| 133 | } |
| 134 | } |