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
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    @Mock private Context mContext;
52    private BroadcastInterceptingContext mServiceContext;
53    private Handler mHandler;
54    private SimChangeListener mSCL;
55    private int mCallbackCount;
56
57    private void doCallback() { mCallbackCount++; }
58
59    private class MockContext extends BroadcastInterceptingContext {
60        MockContext(Context base) {
61            super(base);
62        }
63    }
64
65    @BeforeClass
66    public static void setUpBeforeClass() throws Exception {
67        if (Looper.myLooper() == null) {
68            Looper.prepare();
69        }
70    }
71
72    @Before public void setUp() throws Exception {
73        MockitoAnnotations.initMocks(this);
74        reset(mContext);
75        mServiceContext = new MockContext(mContext);
76        mHandler = new Handler(Looper.myLooper());
77        mCallbackCount = 0;
78        mSCL = new SimChangeListener(mServiceContext, mHandler, () -> doCallback());
79    }
80
81    @After public void tearDown() throws Exception {
82        if (mSCL != null) {
83            mSCL.stopListening();
84            mSCL = null;
85        }
86    }
87
88    private void sendSimStateChangeIntent(String state) {
89        final Intent intent = new Intent(ACTION_SIM_STATE_CHANGED);
90        intent.putExtra(INTENT_KEY_ICC_STATE, state);
91        mServiceContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL);
92    }
93
94    @Test
95    public void testNotSeenFollowedBySeenCallsCallback() {
96        mSCL.startListening();
97
98        sendSimStateChangeIntent(INTENT_VALUE_ICC_ABSENT);
99        sendSimStateChangeIntent(INTENT_VALUE_ICC_LOADED);
100        assertEquals(1, mCallbackCount);
101
102        sendSimStateChangeIntent(INTENT_VALUE_ICC_ABSENT);
103        sendSimStateChangeIntent(INTENT_VALUE_ICC_LOADED);
104        assertEquals(2, mCallbackCount);
105
106        mSCL.stopListening();
107    }
108
109    @Test
110    public void testNotListeningDoesNotCallback() {
111        sendSimStateChangeIntent(INTENT_VALUE_ICC_ABSENT);
112        sendSimStateChangeIntent(INTENT_VALUE_ICC_LOADED);
113        assertEquals(0, mCallbackCount);
114
115        sendSimStateChangeIntent(INTENT_VALUE_ICC_ABSENT);
116        sendSimStateChangeIntent(INTENT_VALUE_ICC_LOADED);
117        assertEquals(0, mCallbackCount);
118    }
119
120    @Test
121    public void testSeenOnlyDoesNotCallback() {
122        mSCL.startListening();
123
124        sendSimStateChangeIntent(INTENT_VALUE_ICC_LOADED);
125        assertEquals(0, mCallbackCount);
126
127        sendSimStateChangeIntent(INTENT_VALUE_ICC_LOADED);
128        assertEquals(0, mCallbackCount);
129
130        mSCL.stopListening();
131    }
132}
133