1 /*
2  * Copyright (C) 2019 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 
17 package com.android.cellbroadcastservice.tests;
18 
19 import android.content.Context;
20 import android.content.pm.ProviderInfo;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.database.sqlite.SQLiteOpenHelper;
23 import android.util.Log;
24 
25 import androidx.test.InstrumentationRegistry;
26 
27 import com.android.cellbroadcastservice.CellBroadcastProvider;
28 
29 public class CellBroadcastProviderTestable extends CellBroadcastProvider {
30     private static final String TAG = CellBroadcastProviderTestable.class.getSimpleName();
31 
CellBroadcastProviderTestable(CellBroadcastPermissionChecker permissionChecker)32     public CellBroadcastProviderTestable(CellBroadcastPermissionChecker permissionChecker) {
33         super(permissionChecker);
34     }
35 
36     @Override
onCreate()37     public boolean onCreate() {
38         // DO NOT call super.onCreate(), otherwise the permission checker will be override.
39         Log.d(TAG, "CellBroadcastProviderTestable onCreate");
40         mDbHelper = new InMemoryCellBroadcastProviderDbHelper();
41         return true;
42     }
43 
closeDatabase()44     public void closeDatabase() {
45         mDbHelper.close();
46     }
47 
48     public static class InMemoryCellBroadcastProviderDbHelper extends SQLiteOpenHelper {
InMemoryCellBroadcastProviderDbHelper()49         public InMemoryCellBroadcastProviderDbHelper() {
50             super(InstrumentationRegistry.getTargetContext(),
51                     null,    // db file name is null for in-memory db
52                     null,    // CursorFactory is null by default
53                     1);      // db version is no-op for tests
54         }
55 
56         @Override
onCreate(SQLiteDatabase db)57         public void onCreate(SQLiteDatabase db) {
58             Log.d(TAG, "IN MEMORY DB CREATED");
59             db.execSQL(getStringForCellBroadcastTableCreation(CELL_BROADCASTS_TABLE_NAME));
60         }
61 
62         @Override
onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)63         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
64     }
65 
initializeForTesting(Context context)66     public void initializeForTesting(Context context) {
67         ProviderInfo providerInfo = new ProviderInfo();
68         providerInfo.authority = CellBroadcastProvider.AUTHORITY;
69 
70         // Add context to given carrierIdProvider
71         attachInfoForTesting(context, providerInfo);
72     }
73 }
74