blob: b5d333b8b230ed4dcf62e72cb9d1fdbd7f811ed4 [file] [log] [blame]
Erik Kline227648f2017-01-20 20:26:10 +09001/*
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
17package com.android.server.connectivity.tethering;
18
19import static com.android.internal.telephony.IccCardConstants.INTENT_VALUE_ICC_ABSENT;
20import static com.android.internal.telephony.IccCardConstants.INTENT_VALUE_ICC_LOADED;
21import static com.android.internal.telephony.IccCardConstants.INTENT_KEY_ICC_STATE;
22import static com.android.internal.telephony.TelephonyIntents.ACTION_SIM_STATE_CHANGED;
23
24import static org.junit.Assert.assertEquals;
25import static org.mockito.Mockito.reset;
26
27import android.content.Context;
28import android.content.Intent;
29import android.os.Handler;
30import android.os.Looper;
31import android.os.UserHandle;
32
33import android.support.test.filters.SmallTest;
34import android.support.test.runner.AndroidJUnit4;
35
36import com.android.internal.util.test.BroadcastInterceptingContext;
37
38import org.junit.After;
39import org.junit.Before;
40import org.junit.BeforeClass;
41import org.junit.runner.RunWith;
42import org.junit.Test;
43import org.mockito.Mock;
44import org.mockito.Mockito;
45import org.mockito.MockitoAnnotations;
46
47
48@RunWith(AndroidJUnit4.class)
49@SmallTest
50public 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}