SubscriptionControllerTest.java revision d922de0ecf35acc967fc09166974354f6ab9e8d5
1/*
2 * Copyright (C) 2016 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 */
16package com.android.internal.telephony;
17
18import static org.junit.Assert.*;
19import static org.mockito.Mockito.*;
20
21import android.app.AppOpsManager;
22import android.content.ContentValues;
23import android.content.Intent;
24import android.database.Cursor;
25import android.database.MatrixCursor;
26import android.net.Uri;
27import android.os.Bundle;
28import android.os.UserHandle;
29import android.provider.Settings;
30import android.telephony.SubscriptionInfo;
31import android.telephony.SubscriptionManager;
32import android.test.mock.MockContentProvider;
33import android.test.mock.MockContentResolver;
34import android.test.suitebuilder.annotation.SmallTest;
35import android.util.Log;
36
37import org.junit.After;
38import org.junit.Before;
39import org.junit.Test;
40import org.mockito.ArgumentCaptor;
41import org.mockito.Mock;
42
43import java.util.ArrayList;
44import java.util.List;
45
46public class SubscriptionControllerTest extends TelephonyTest {
47
48    private static final int SINGLE_SIM = 1;
49    private String mCallingPackage;
50    private SubscriptionController mSubscriptionControllerUT;
51    private MockContentResolver mMockContentResolver;
52
53    @Mock private List<SubscriptionInfo> mSubList;
54    @Mock private AppOpsManager mAppOps;
55
56    public class FakeSubscriptionContentProvider extends MockContentProvider {
57
58        private ArrayList<ContentValues> mSubscriptionArray =
59                new ArrayList<ContentValues>();
60
61        private String[] mKeyMappingSet = new String[]{
62                SubscriptionManager.UNIQUE_KEY_SUBSCRIPTION_ID,
63                SubscriptionManager.ICC_ID, SubscriptionManager.SIM_SLOT_INDEX,
64                SubscriptionManager.DISPLAY_NAME, SubscriptionManager.CARRIER_NAME,
65                SubscriptionManager.NAME_SOURCE, SubscriptionManager.COLOR,
66                SubscriptionManager.NUMBER, SubscriptionManager.DISPLAY_NUMBER_FORMAT,
67                SubscriptionManager.DATA_ROAMING, SubscriptionManager.MCC,
68                SubscriptionManager.MNC, SubscriptionManager.CB_EXTREME_THREAT_ALERT,
69                SubscriptionManager.CB_SEVERE_THREAT_ALERT, SubscriptionManager.CB_AMBER_ALERT,
70                SubscriptionManager.CB_ALERT_SOUND_DURATION,
71                SubscriptionManager.CB_ALERT_REMINDER_INTERVAL,
72                SubscriptionManager.CB_ALERT_VIBRATE, SubscriptionManager.CB_ALERT_SPEECH,
73                SubscriptionManager.CB_ETWS_TEST_ALERT, SubscriptionManager.CB_CHANNEL_50_ALERT,
74                SubscriptionManager.CB_CMAS_TEST_ALERT, SubscriptionManager.CB_OPT_OUT_DIALOG,
75                SubscriptionManager.SIM_PROVISIONING_STATUS, SubscriptionManager.IS_EMBEDDED,
76                SubscriptionManager.ACCESS_RULES};
77
78        /* internal util function */
79        private MatrixCursor convertFromContentToCursor(ContentValues initialValues) {
80            MatrixCursor cursor = null;
81            ArrayList<Object> values = new ArrayList<Object>();
82
83            if (initialValues != null && mKeyMappingSet.length != 0) {
84                cursor = new MatrixCursor(mKeyMappingSet);
85                /* push value from contentValues to matrixCursors */
86                for (String key : mKeyMappingSet) {
87                    if (initialValues.containsKey(key)) {
88                        values.add(initialValues.get(key));
89                    } else {
90                        values.add(null);
91                    }
92                }
93                cursor.addRow(values.toArray());
94            }
95            return cursor;
96        }
97
98        @Override
99        public int delete(Uri uri, String selection, String[] selectionArgs) {
100            if (mSubscriptionArray.size() > 0) {
101                mSubscriptionArray.remove(0);
102                return 1;
103            }
104            return -1;
105        }
106
107        @Override
108        public Uri insert(Uri uri, ContentValues values) {
109            values.put(SubscriptionManager.UNIQUE_KEY_SUBSCRIPTION_ID, 0);
110            mSubscriptionArray.add(values);
111            return uri;
112        }
113
114        @Override
115        public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
116                            String sortOrder) {
117            if (mSubscriptionArray.size() > 0) {
118                return convertFromContentToCursor(mSubscriptionArray.get(0));
119            }
120            return null;
121        }
122
123        @Override
124        public Bundle call(String method, String request, Bundle args) {
125            return null;
126        }
127
128        @Override
129        public int update(android.net.Uri uri, android.content.ContentValues values,
130                          java.lang.String selection, java.lang.String[] selectionArgs) {
131            if (mSubscriptionArray.size() > 0) {
132                ContentValues val = mSubscriptionArray.get(0);
133                for (String key : values.keySet()) {
134                    val.put(key, values.getAsString(key));
135                    Log.d(TAG, "update the values..." + key + "..." + values.getAsString(key));
136                }
137                mSubscriptionArray.set(0, val);
138                return 1;
139            }
140            return -1;
141        }
142    }
143
144    @Before
145    public void setUp() throws Exception {
146        super.setUp("SubscriptionControllerTest");
147
148        doReturn(SINGLE_SIM).when(mTelephonyManager).getSimCount();
149        doReturn(SINGLE_SIM).when(mTelephonyManager).getPhoneCount();
150
151        replaceInstance(SubscriptionController.class, "sInstance", null, null);
152
153        SubscriptionController.init(mContext, null);
154        mSubscriptionControllerUT = SubscriptionController.getInstance();
155        mCallingPackage = mContext.getOpPackageName();
156
157        doReturn(1).when(mProxyController).getMaxRafSupported();
158        mContextFixture.putIntArrayResource(com.android.internal.R.array.sim_colors, new int[]{5});
159
160        mSubscriptionControllerUT.getInstance().updatePhonesAvailability(new Phone[]{mPhone});
161        mMockContentResolver = (MockContentResolver) mContext.getContentResolver();
162        mMockContentResolver.addProvider(SubscriptionManager.CONTENT_URI.getAuthority(),
163                new FakeSubscriptionContentProvider());
164    }
165
166    @After
167    public void tearDown() throws Exception {
168        /* should clear fake content provider and resolver here */
169        mContext.getContentResolver().delete(SubscriptionManager.CONTENT_URI, null, null);
170
171        /* clear settings for default voice/data/sms sub ID */
172        Settings.Global.putInt(mContext.getContentResolver(),
173                Settings.Global.MULTI_SIM_VOICE_CALL_SUBSCRIPTION,
174                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
175        Settings.Global.putInt(mContext.getContentResolver(),
176                Settings.Global.MULTI_SIM_DATA_CALL_SUBSCRIPTION,
177                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
178        Settings.Global.putInt(mContext.getContentResolver(),
179                Settings.Global.MULTI_SIM_SMS_SUBSCRIPTION,
180                SubscriptionManager.INVALID_SUBSCRIPTION_ID);
181
182        mSubscriptionControllerUT = null;
183        super.tearDown();
184    }
185
186    @Test @SmallTest
187    public void testInsertSim() {
188        int slotID = mSubscriptionControllerUT.getAllSubInfoCount(mCallingPackage);
189
190        //verify there is no sim inserted in the SubscriptionManager
191        assertEquals(0, slotID);
192
193        //insert one Subscription Info
194        mSubscriptionControllerUT.addSubInfoRecord("test", slotID);
195
196        //verify there is one sim
197        assertEquals(1, mSubscriptionControllerUT.getAllSubInfoCount(mCallingPackage));
198
199        //sanity for slot id and sub id
200        List<SubscriptionInfo> mSubList = mSubscriptionControllerUT
201                .getActiveSubscriptionInfoList(mCallingPackage);
202        assertTrue(mSubList != null && mSubList.size() > 0);
203        for (int i = 0; i < mSubList.size(); i++) {
204            assertTrue(SubscriptionManager.isValidSubscriptionId(
205                    mSubList.get(i).getSubscriptionId()));
206            assertTrue(SubscriptionManager.isValidSlotIndex(mSubList.get(i).getSimSlotIndex()));
207        }
208    }
209
210    @Test @SmallTest
211    public void testChangeSIMProperty() {
212        int dataRoaming = 1;
213        int iconTint = 1;
214        String disName = "TESTING";
215        String disNum = "12345";
216
217        testInsertSim();
218        /* Get SUB ID */
219        int[] subIds = mSubscriptionControllerUT.getActiveSubIdList();
220        assertTrue(subIds != null && subIds.length != 0);
221        int subID = subIds[0];
222
223        /* Setting */
224        mSubscriptionControllerUT.setDisplayName(disName, subID);
225        mSubscriptionControllerUT.setDataRoaming(dataRoaming, subID);
226        mSubscriptionControllerUT.setDisplayNumber(disNum, subID);
227        mSubscriptionControllerUT.setIconTint(iconTint, subID);
228
229        /* Getting, there is no direct getter function for each fields of property */
230        SubscriptionInfo subInfo = mSubscriptionControllerUT
231                .getActiveSubscriptionInfo(subID, mCallingPackage);
232        assertNotNull(subInfo);
233        assertEquals(dataRoaming, subInfo.getDataRoaming());
234        assertEquals(disName, subInfo.getDisplayName());
235        assertEquals(iconTint, subInfo.getIconTint());
236        assertEquals(disNum, subInfo.getNumber());
237
238        /* verify broadcast intent */
239        ArgumentCaptor<Intent> captorIntent = ArgumentCaptor.forClass(Intent.class);
240        verify(mContext, atLeast(1)).sendBroadcast(captorIntent.capture());
241        assertEquals(TelephonyIntents.ACTION_SUBINFO_RECORD_UPDATED,
242                captorIntent.getValue().getAction());
243    }
244
245    @Test @SmallTest
246    public void testSetGetDisplayNameSrc() {
247        testInsertSim();
248
249        /* Get SUB ID */
250        int[] subIds = mSubscriptionControllerUT.getActiveSubIdList();
251        assertTrue(subIds != null && subIds.length != 0);
252        int subID = subIds[0];
253
254        /* Setting */
255        String disName = "TESTING";
256        long nameSource = 1;
257        mSubscriptionControllerUT.setDisplayNameUsingSrc(disName, subID, nameSource);
258        SubscriptionInfo subInfo = mSubscriptionControllerUT
259                .getActiveSubscriptionInfo(subID, mCallingPackage);
260        assertNotNull(subInfo);
261        assertEquals(disName, subInfo.getDisplayName());
262        assertEquals(nameSource, subInfo.getNameSource());
263
264        /* verify broadcast intent */
265        ArgumentCaptor<Intent> captorIntent = ArgumentCaptor.forClass(Intent.class);
266        verify(mContext, atLeast(1)).sendBroadcast(captorIntent.capture());
267        assertEquals(TelephonyIntents.ACTION_SUBINFO_RECORD_UPDATED,
268                captorIntent.getValue().getAction());
269
270    }
271
272    @Test @SmallTest
273    public void testCleanUpSIM() {
274        testInsertSim();
275        assertFalse(mSubscriptionControllerUT.isActiveSubId(1));
276        mSubscriptionControllerUT.clearSubInfo();
277        assertFalse(mSubscriptionControllerUT.isActiveSubId(0));
278        assertEquals(SubscriptionManager.SIM_NOT_INSERTED,
279                mSubscriptionControllerUT.getSlotIndex(0));
280    }
281
282    @Test @SmallTest
283    public void testDefaultSubID() {
284        assertEquals(SubscriptionManager.INVALID_SUBSCRIPTION_ID,
285                mSubscriptionControllerUT.getDefaultDataSubId());
286        assertEquals(SubscriptionManager.INVALID_SUBSCRIPTION_ID,
287                mSubscriptionControllerUT.getDefaultSmsSubId());
288        assertEquals(SubscriptionManager.INVALID_SUBSCRIPTION_ID,
289                mSubscriptionControllerUT.getDefaultSmsSubId());
290        /* insert one sim */
291        testInsertSim();
292        // if support single sim, sms/data/voice default sub should be the same
293        assertNotSame(SubscriptionManager.INVALID_SUBSCRIPTION_ID,
294                mSubscriptionControllerUT.getDefaultSubId());
295        assertEquals(mSubscriptionControllerUT.getDefaultDataSubId(),
296                mSubscriptionControllerUT.getDefaultSmsSubId());
297        assertEquals(mSubscriptionControllerUT.getDefaultDataSubId(),
298                mSubscriptionControllerUT.getDefaultVoiceSubId());
299    }
300
301    @Test @SmallTest
302    public void testSetGetMCCMNC() {
303        testInsertSim();
304        String mCcMncVERIZON = "310004";
305        mSubscriptionControllerUT.setMccMnc(mCcMncVERIZON, 0);
306
307        SubscriptionInfo subInfo = mSubscriptionControllerUT
308                .getActiveSubscriptionInfo(0, mCallingPackage);
309        assertNotNull(subInfo);
310        assertEquals(Integer.parseInt(mCcMncVERIZON.substring(0, 3)), subInfo.getMcc());
311        assertEquals(Integer.parseInt(mCcMncVERIZON.substring(3)), subInfo.getMnc());
312
313         /* verify broadcast intent */
314        ArgumentCaptor<Intent> captorIntent = ArgumentCaptor.forClass(Intent.class);
315        verify(mContext, atLeast(1)).sendBroadcast(captorIntent.capture());
316        assertEquals(TelephonyIntents.ACTION_SUBINFO_RECORD_UPDATED,
317                captorIntent.getValue().getAction());
318    }
319
320    @Test
321    @SmallTest
322    public void testSetDefaultDataSubId() throws Exception {
323        doReturn(1).when(mPhone).getSubId();
324
325        mSubscriptionControllerUT.setDefaultDataSubId(1);
326
327        verify(mPhone, times(1)).updateDataConnectionTracker();
328        ArgumentCaptor<Intent> captorIntent = ArgumentCaptor.forClass(Intent.class);
329        verify(mContext, times(1)).sendStickyBroadcastAsUser(
330                captorIntent.capture(), eq(UserHandle.ALL));
331
332        Intent intent = captorIntent.getValue();
333        assertEquals(TelephonyIntents.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED, intent.getAction());
334
335        Bundle b = intent.getExtras();
336
337        assertTrue(b.containsKey(PhoneConstants.SUBSCRIPTION_KEY));
338        assertEquals(1, b.getInt(PhoneConstants.SUBSCRIPTION_KEY));
339    }
340}
341