1/*
2 * Copyright (C) 2015 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.providers.telephony;
17
18import android.content.Context;
19import android.database.sqlite.SQLiteDatabase;
20import android.database.sqlite.SQLiteOpenHelper;
21import android.provider.Telephony;
22import android.support.test.InstrumentationRegistry;
23import android.telephony.SubscriptionManager;
24import android.text.TextUtils;
25import android.util.Log;
26
27import java.util.List;
28import java.util.ArrayList;
29import com.android.internal.annotations.VisibleForTesting;
30import com.android.internal.telephony.uicc.IccRecords;
31import com.android.providers.telephony.TelephonyProvider;
32import static android.provider.Telephony.Carriers.*;
33import static org.mockito.Mockito.doReturn;
34import static org.mockito.Mockito.mock;
35
36/**
37 * A subclass of TelephonyProvider used for testing on an in-memory database
38 */
39public class TelephonyProviderTestable extends TelephonyProvider {
40    private static final String TAG = "TelephonyProviderTestable";
41
42    @VisibleForTesting
43    public static final String TEST_SPN = "testspn";
44
45    private InMemoryTelephonyProviderDbHelper mDbHelper;
46    private MockInjector mMockInjector;
47
48    public TelephonyProviderTestable() {
49        this(new MockInjector());
50    }
51
52    private TelephonyProviderTestable(MockInjector mockInjector) {
53        super(mockInjector);
54        mMockInjector = mockInjector;
55    }
56
57    @Override
58    public boolean onCreate() {
59        Log.d(TAG, "onCreate called: mDbHelper = new InMemoryTelephonyProviderDbHelper()");
60        mDbHelper = new InMemoryTelephonyProviderDbHelper();
61        s_apnSourceServiceExists = false;
62        return true;
63    }
64
65    // close mDbHelper database object
66    protected void closeDatabase() {
67        mDbHelper.close();
68    }
69
70    @Override
71    SQLiteDatabase getReadableDatabase() {
72        Log.d(TAG, "getReadableDatabase called");
73        return mDbHelper.getReadableDatabase();
74    }
75
76    @Override
77    SQLiteDatabase getWritableDatabase() {
78        Log.d(TAG, "getWritableDatabase called");
79        return mDbHelper.getWritableDatabase();
80    }
81
82    @Override
83    void initDatabaseWithDatabaseHelper(SQLiteDatabase db) {
84        Log.d(TAG, "initDatabaseWithDatabaseHelper called; doing nothing");
85    }
86
87    @Override
88    boolean needApnDbUpdate() {
89        Log.d(TAG, "needApnDbUpdate called; returning false");
90        return false;
91    }
92
93    @Override
94    IccRecords getIccRecords(int subId) {
95        Log.d(TAG, "getIccRecords called");
96        IccRecords iccRecords = mock(IccRecords.class);
97        doReturn(TEST_SPN).when(iccRecords).getServiceProviderName();
98        return iccRecords;
99    }
100
101    public void fakeCallingUid(int uid) {
102        mMockInjector.fakeCallingUid(uid);
103    }
104
105    /**
106     * An in memory DB for TelephonyProviderTestable to use
107     */
108    public static class InMemoryTelephonyProviderDbHelper extends SQLiteOpenHelper {
109
110
111        public InMemoryTelephonyProviderDbHelper() {
112            super(InstrumentationRegistry.getTargetContext(),
113                    null,    // db file name is null for in-memory db
114                    null,    // CursorFactory is null by default
115                    1);      // db version is no-op for tests
116            Log.d(TAG, "InMemoryTelephonyProviderDbHelper creating in-memory database");
117        }
118
119        @Override
120        public void onCreate(SQLiteDatabase db) {
121            // Set up the carriers table
122            Log.d(TAG, "InMemoryTelephonyProviderDbHelper onCreate creating the carriers table");
123            db.execSQL(getStringForCarrierTableCreation("carriers"));
124
125            // set up the siminfo table
126            Log.d(TAG, "InMemoryTelephonyProviderDbHelper onCreate creating the siminfo table");
127            db.execSQL(getStringForSimInfoTableCreation("siminfo"));
128        }
129
130        @Override
131        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
132            Log.d(TAG, "InMemoryTelephonyProviderDbHelper onUpgrade doing nothing");
133            return;
134        }
135    }
136
137    static class MockInjector extends Injector {
138        private int callingUid = 0;
139
140        @Override
141        int binderGetCallingUid() {
142            return callingUid;
143        }
144
145        void fakeCallingUid(int uid) {
146            callingUid = uid;
147        }
148    }
149}
150