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 package com.android.providers.telephony;
17 
18 import android.content.Context;
19 import android.content.pm.ProviderInfo;
20 import android.database.sqlite.SQLiteDatabase;
21 import android.database.sqlite.SQLiteOpenHelper;
22 import android.util.Log;
23 
24 /**
25  * A subclass of CarrierIdProvider used for testing on an in-memory database
26  */
27 public class CarrierIdProviderTestable extends CarrierIdProvider {
28     private static final String TAG = CarrierIdProviderTestable.class.getSimpleName();
29 
30     private InMemoryCarrierIdProviderDbHelper mDbHelper;
31 
32     @Override
onCreate()33     public boolean onCreate() {
34         Log.d(TAG, "onCreate called: mDbHelper = new InMemoryCarrierIdProviderDbHelper()");
35         mDbHelper = new InMemoryCarrierIdProviderDbHelper();
36         return true;
37     }
38 
39     // close mDbHelper database object
closeDatabase()40     protected void closeDatabase() {
41         mDbHelper.close();
42     }
43 
44     @Override
getReadableDatabase()45     SQLiteDatabase getReadableDatabase() {
46         Log.d(TAG, "getReadableDatabase called" + mDbHelper.getReadableDatabase());
47         return mDbHelper.getReadableDatabase();
48     }
49 
50     @Override
getWritableDatabase()51     SQLiteDatabase getWritableDatabase() {
52         Log.d(TAG, "getWritableDatabase called" + mDbHelper.getWritableDatabase());
53         return mDbHelper.getWritableDatabase();
54     }
55 
initializeForTesting(Context context)56     void initializeForTesting(Context context) {
57         ProviderInfo providerInfo = new ProviderInfo();
58         providerInfo.authority = CarrierIdProvider.AUTHORITY;
59 
60         // Add context to given carrierIdProvider
61         attachInfoForTesting(context, providerInfo);
62     }
63 
64     /**
65      * An in memory DB for CarrierIdProviderTestable to use
66      */
67     public static class InMemoryCarrierIdProviderDbHelper extends SQLiteOpenHelper {
InMemoryCarrierIdProviderDbHelper()68         public InMemoryCarrierIdProviderDbHelper() {
69             super(null,      // no context is needed for in-memory db
70                   null,      // db file name is null for in-memory db
71                   null,      // CursorFactory is null by default
72                   1);        // db version is no-op for tests
73             Log.d(TAG, "InMemoryCarrierIdProviderDbHelper creating in-memory database");
74         }
75 
76         @Override
onCreate(SQLiteDatabase db)77         public void onCreate(SQLiteDatabase db) {
78             //set up the Carrier id table
79             Log.d(TAG, "InMemoryCarrierIdProviderDbHelper onCreate creating the carrier infp table");
80             db.execSQL(CarrierIdProvider.getStringForCarrierIdTableCreation(
81                     CarrierIdProvider.CARRIER_ID_TABLE));
82             db.execSQL(CarrierIdProvider.getStringForIndexCreation(
83                     CarrierIdProvider.CARRIER_ID_TABLE));
84         }
85 
86         @Override
onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)87         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
88             Log.d(TAG, "InMemoryCarrierIdProviderDbHelper onUpgrade doing nothing");
89             return;
90         }
91     }
92 }
93