Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 57c48bb7 authored by Xiangyu/Malcolm Chen's avatar Xiangyu/Malcolm Chen Committed by Gerrit Code Review
Browse files

Merge "Use InMemory DB in SubscriptionControllerTest"

parents 795595a6 918640bd
Loading
Loading
Loading
Loading
+146 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.internal.telephony;


import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.os.Bundle;
import android.support.test.InstrumentationRegistry;
import android.telephony.SubscriptionManager;
import android.test.mock.MockContentProvider;
import android.util.Log;

public class FakeTelephonyProvider extends MockContentProvider {
    static final String TAG = "FakeTelephonyProvider";

    private InMemoryTelephonyProviderDbHelper mDbHelper =
            new InMemoryTelephonyProviderDbHelper();

    /**
     * An in memory DB.
     */
    private class InMemoryTelephonyProviderDbHelper extends SQLiteOpenHelper {
        InMemoryTelephonyProviderDbHelper() {
            super(InstrumentationRegistry.getTargetContext(),
                    null,    // db file name is null for in-memory db
                    null,    // CursorFactory is null by default
                    1);      // db version is no-op for tests
            Log.d(TAG, "InMemoryTelephonyProviderDbHelper creating in-memory database");
        }

        // This should always be consistent with TelephonyProvider#getStringForSimInfoTableCreation.
        private String getStringForSimInfoTableCreation(String tableName) {
            return "CREATE TABLE " + tableName + "("
                    + SubscriptionManager.UNIQUE_KEY_SUBSCRIPTION_ID
                    + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                    + SubscriptionManager.ICC_ID + " TEXT NOT NULL,"
                    + SubscriptionManager.SIM_SLOT_INDEX
                    + " INTEGER DEFAULT " + SubscriptionManager.SIM_NOT_INSERTED + ","
                    + SubscriptionManager.DISPLAY_NAME + " TEXT,"
                    + SubscriptionManager.CARRIER_NAME + " TEXT,"
                    + SubscriptionManager.NAME_SOURCE
                    + " INTEGER DEFAULT " + SubscriptionManager.NAME_SOURCE_DEFAULT_SOURCE + ","
                    + SubscriptionManager.COLOR + " INTEGER DEFAULT "
                    + SubscriptionManager.COLOR_DEFAULT + ","
                    + SubscriptionManager.NUMBER + " TEXT,"
                    + SubscriptionManager.DISPLAY_NUMBER_FORMAT
                    + " INTEGER NOT NULL DEFAULT "
                    + SubscriptionManager.DISPLAY_NUMBER_DEFAULT + ","
                    + SubscriptionManager.DATA_ROAMING
                    + " INTEGER DEFAULT " + SubscriptionManager.DATA_ROAMING_DEFAULT + ","
                    + SubscriptionManager.MCC + " INTEGER DEFAULT 0,"
                    + SubscriptionManager.MNC + " INTEGER DEFAULT 0,"
                    + SubscriptionManager.MCC_STRING + " TEXT,"
                    + SubscriptionManager.MNC_STRING + " TEXT,"
                    + SubscriptionManager.SIM_PROVISIONING_STATUS
                    + " INTEGER DEFAULT " + SubscriptionManager.SIM_PROVISIONED + ","
                    + SubscriptionManager.IS_EMBEDDED + " INTEGER DEFAULT 0,"
                    + SubscriptionManager.CARD_ID + " TEXT NOT NULL,"
                    + SubscriptionManager.ACCESS_RULES + " BLOB,"
                    + SubscriptionManager.IS_REMOVABLE + " INTEGER DEFAULT 0,"
                    + SubscriptionManager.CB_EXTREME_THREAT_ALERT + " INTEGER DEFAULT 1,"
                    + SubscriptionManager.CB_SEVERE_THREAT_ALERT + " INTEGER DEFAULT 1,"
                    + SubscriptionManager.CB_AMBER_ALERT + " INTEGER DEFAULT 1,"
                    + SubscriptionManager.CB_EMERGENCY_ALERT + " INTEGER DEFAULT 1,"
                    + SubscriptionManager.CB_ALERT_SOUND_DURATION + " INTEGER DEFAULT 4,"
                    + SubscriptionManager.CB_ALERT_REMINDER_INTERVAL + " INTEGER DEFAULT 0,"
                    + SubscriptionManager.CB_ALERT_VIBRATE + " INTEGER DEFAULT 1,"
                    + SubscriptionManager.CB_ALERT_SPEECH + " INTEGER DEFAULT 1,"
                    + SubscriptionManager.CB_ETWS_TEST_ALERT + " INTEGER DEFAULT 0,"
                    + SubscriptionManager.CB_CHANNEL_50_ALERT + " INTEGER DEFAULT 1,"
                    + SubscriptionManager.CB_CMAS_TEST_ALERT + " INTEGER DEFAULT 0,"
                    + SubscriptionManager.CB_OPT_OUT_DIALOG + " INTEGER DEFAULT 1,"
                    + SubscriptionManager.ENHANCED_4G_MODE_ENABLED + " INTEGER DEFAULT -1,"
                    + SubscriptionManager.VT_IMS_ENABLED + " INTEGER DEFAULT -1,"
                    + SubscriptionManager.WFC_IMS_ENABLED + " INTEGER DEFAULT -1,"
                    + SubscriptionManager.WFC_IMS_MODE + " INTEGER DEFAULT -1,"
                    + SubscriptionManager.WFC_IMS_ROAMING_MODE + " INTEGER DEFAULT -1,"
                    + SubscriptionManager.WFC_IMS_ROAMING_ENABLED + " INTEGER DEFAULT -1,"
                    + SubscriptionManager.IS_OPPORTUNISTIC + " INTEGER DEFAULT 0,"
                    + SubscriptionManager.PARENT_SUB_ID + " INTEGER DEFAULT -1"
                    + ");";
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO: set up other tables when needed.
            // set up the siminfo table
            Log.d(TAG, "InMemoryTelephonyProviderDbHelper onCreate creating the siminfo table");
            db.execSQL(getStringForSimInfoTableCreation("siminfo"));
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.d(TAG, "InMemoryTelephonyProviderDbHelper onUpgrade doing nothing");
            return;
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        SQLiteDatabase db = mDbHelper.getWritableDatabase();
        long id = db.insert("siminfo", null, values);
        return ContentUris.withAppendedId(SubscriptionManager.CONTENT_URI, id);
    }

    @Override
    public synchronized int delete(Uri url, String where, String[] whereArgs) {
        return mDbHelper.getWritableDatabase().delete("siminfo", where, whereArgs);
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
            String sortOrder) {
        return mDbHelper.getReadableDatabase().query("siminfo", projection, selection,
                selectionArgs, null, null, sortOrder);
    }

    @Override
    public Bundle call(String method, String request, Bundle args) {
        return null;
    }

    @Override
    public final int update(Uri uri, ContentValues values, String where,
            String[] selectionArgs) {
        return mDbHelper.getWritableDatabase().update("siminfo", values, where, selectionArgs);
    }
}
+21 −119
Original line number Diff line number Diff line
@@ -15,32 +15,31 @@
 */
package com.android.internal.telephony;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import android.app.AppOpsManager;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.UserHandle;
import android.provider.Settings;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.test.mock.MockContentProvider;
import android.test.mock.MockContentResolver;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Log;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;

import java.util.ArrayList;
import java.util.List;

public class SubscriptionControllerTest extends TelephonyTest {
@@ -50,108 +49,6 @@ public class SubscriptionControllerTest extends TelephonyTest {
    private SubscriptionController mSubscriptionControllerUT;
    private MockContentResolver mMockContentResolver;

    @Mock private List<SubscriptionInfo> mSubList;
    @Mock private AppOpsManager mAppOps;

    public class FakeSubscriptionContentProvider extends MockContentProvider {

        private ArrayList<ContentValues> mSubscriptionArray =
                new ArrayList<ContentValues>();

        private String[] mKeyMappingSet = new String[]{
                SubscriptionManager.UNIQUE_KEY_SUBSCRIPTION_ID,
                SubscriptionManager.ICC_ID, SubscriptionManager.SIM_SLOT_INDEX,
                SubscriptionManager.DISPLAY_NAME, SubscriptionManager.CARRIER_NAME,
                SubscriptionManager.NAME_SOURCE, SubscriptionManager.COLOR,
                SubscriptionManager.NUMBER, SubscriptionManager.DISPLAY_NUMBER_FORMAT,
                SubscriptionManager.DATA_ROAMING, SubscriptionManager.MCC,
                SubscriptionManager.MNC, SubscriptionManager.MCC_STRING,
                SubscriptionManager.MNC_STRING,
                SubscriptionManager.CB_EXTREME_THREAT_ALERT,
                SubscriptionManager.CB_SEVERE_THREAT_ALERT, SubscriptionManager.CB_AMBER_ALERT,
                SubscriptionManager.CB_ALERT_SOUND_DURATION,
                SubscriptionManager.CB_ALERT_REMINDER_INTERVAL,
                SubscriptionManager.CB_ALERT_VIBRATE, SubscriptionManager.CB_ALERT_SPEECH,
                SubscriptionManager.CB_ETWS_TEST_ALERT, SubscriptionManager.CB_CHANNEL_50_ALERT,
                SubscriptionManager.CB_CMAS_TEST_ALERT, SubscriptionManager.CB_OPT_OUT_DIALOG,
                SubscriptionManager.SIM_PROVISIONING_STATUS, SubscriptionManager.IS_EMBEDDED,
                SubscriptionManager.ACCESS_RULES, SubscriptionManager.ENHANCED_4G_MODE_ENABLED,
                SubscriptionManager.VT_IMS_ENABLED, SubscriptionManager.WFC_IMS_ENABLED,
                SubscriptionManager.WFC_IMS_MODE, SubscriptionManager.WFC_IMS_ROAMING_MODE,
                SubscriptionManager.WFC_IMS_ROAMING_ENABLED,
                SubscriptionManager.CARD_ID, SubscriptionManager.IS_OPPORTUNISTIC,
                SubscriptionManager.PARENT_SUB_ID
        };

        /* internal util function */
        private MatrixCursor convertFromContentToCursor(ContentValues initialValues,
                String[] projection) {
            MatrixCursor cursor = null;
            ArrayList<Object> values = new ArrayList<Object>();
            if (projection == null) {
                projection = mKeyMappingSet;
            }
            if (initialValues != null && projection.length != 0) {
                cursor = new MatrixCursor(projection);
                /* push value from contentValues to matrixCursors */
                for (String key : projection) {
                    if (initialValues.containsKey(key)) {
                        values.add(initialValues.get(key));
                    } else {
                        values.add(null);
                    }
                }
            }
            cursor.addRow(values.toArray());
            return cursor;
        }

        @Override
        public int delete(Uri uri, String selection, String[] selectionArgs) {
            if (mSubscriptionArray.size() > 0) {
                mSubscriptionArray.remove(0);
                return 1;
            }
            return -1;
        }

        @Override
        public Uri insert(Uri uri, ContentValues values) {
            values.put(SubscriptionManager.UNIQUE_KEY_SUBSCRIPTION_ID, 0);
            mSubscriptionArray.add(values);
            return uri;
        }

        @Override
        public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
                            String sortOrder) {
            if (mSubscriptionArray.size() > 0) {
                return convertFromContentToCursor(mSubscriptionArray.get(0), projection);
            }
            return null;
        }

        @Override
        public Bundle call(String method, String request, Bundle args) {
            return null;
        }

        @Override
        public int update(android.net.Uri uri, android.content.ContentValues values,
                          java.lang.String selection, java.lang.String[] selectionArgs) {
            if (mSubscriptionArray.size() > 0) {
                ContentValues val = mSubscriptionArray.get(0);
                for (String key : values.keySet()) {
                    val.put(key, values.getAsString(key));
                    Log.d(TAG, "update the values..." + key + "..." + values.getAsString(key));
                }
                mSubscriptionArray.set(0, val);
                return 1;
            }
            return -1;
        }
    }

    @Before
    public void setUp() throws Exception {
        super.setUp("SubscriptionControllerTest");
@@ -171,7 +68,8 @@ public class SubscriptionControllerTest extends TelephonyTest {
        mSubscriptionControllerUT.getInstance().updatePhonesAvailability(new Phone[]{mPhone});
        mMockContentResolver = (MockContentResolver) mContext.getContentResolver();
        mMockContentResolver.addProvider(SubscriptionManager.CONTENT_URI.getAuthority(),
                new FakeSubscriptionContentProvider());
                new FakeTelephonyProvider());

    }

    @After
@@ -179,6 +77,10 @@ public class SubscriptionControllerTest extends TelephonyTest {
        /* should clear fake content provider and resolver here */
        mContext.getContentResolver().delete(SubscriptionManager.CONTENT_URI, null, null);

        /* Clear sSlotIndexToSubId since they will otherwise be persistent
         * between each test case. */
        mSubscriptionControllerUT.clearSubInfo();

        /* clear settings for default voice/data/sms sub ID */
        Settings.Global.putInt(mContext.getContentResolver(),
                Settings.Global.MULTI_SIM_VOICE_CALL_SUBSCRIPTION,
@@ -286,11 +188,11 @@ public class SubscriptionControllerTest extends TelephonyTest {
    @Test @SmallTest
    public void testCleanUpSIM() {
        testInsertSim();
        assertFalse(mSubscriptionControllerUT.isActiveSubId(1));
        assertFalse(mSubscriptionControllerUT.isActiveSubId(2));
        mSubscriptionControllerUT.clearSubInfo();
        assertFalse(mSubscriptionControllerUT.isActiveSubId(0));
        assertFalse(mSubscriptionControllerUT.isActiveSubId(1));
        assertEquals(SubscriptionManager.SIM_NOT_INSERTED,
                mSubscriptionControllerUT.getSlotIndex(0));
                mSubscriptionControllerUT.getSlotIndex(1));
    }

    @Test @SmallTest
@@ -316,10 +218,10 @@ public class SubscriptionControllerTest extends TelephonyTest {
    public void testSetGetMCCMNC() {
        testInsertSim();
        String mCcMncVERIZON = "310004";
        mSubscriptionControllerUT.setMccMnc(mCcMncVERIZON, 0);
        mSubscriptionControllerUT.setMccMnc(mCcMncVERIZON, 1);

        SubscriptionInfo subInfo = mSubscriptionControllerUT
                .getActiveSubscriptionInfo(0, mCallingPackage);
                .getActiveSubscriptionInfo(1, mCallingPackage);
        assertNotNull(subInfo);
        assertEquals(Integer.parseInt(mCcMncVERIZON.substring(0, 3)), subInfo.getMcc());
        assertEquals(Integer.parseInt(mCcMncVERIZON.substring(3)), subInfo.getMnc());