1 /* 2 * Copyright (C) 2018 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.app.AppOpsManager; 19 import android.content.Context; 20 import android.content.pm.PackageManager; 21 import android.content.pm.ProviderInfo; 22 import android.database.sqlite.SQLiteDatabase; 23 import android.database.sqlite.SQLiteOpenHelper; 24 import android.telephony.TelephonyManager; 25 import android.test.mock.MockContentResolver; 26 import android.test.mock.MockContext; 27 28 import org.mockito.Mockito; 29 30 /** 31 * A subclass of RcsProvider used for testing on an in-memory database 32 */ 33 public class RcsProviderTestable extends RcsProvider { 34 private MockContextWithProvider mockContextWithProvider; 35 36 @Override onCreate()37 public boolean onCreate() { 38 mockContextWithProvider = new MockContextWithProvider(this); 39 mDbOpenHelper = new InMemoryRcsDatabase(); 40 mParticipantHelper = new RcsProviderParticipantHelper(mDbOpenHelper); 41 mThreadHelper = new RcsProviderThreadHelper(mDbOpenHelper); 42 mMessageHelper = new RcsProviderMessageHelper(mDbOpenHelper); 43 mEventHelper = new RcsProviderEventHelper(mDbOpenHelper); 44 return true; 45 } 46 tearDown()47 protected void tearDown() { 48 mDbOpenHelper.close(); 49 } 50 getWritableDatabase()51 public SQLiteDatabase getWritableDatabase() { 52 return mDbOpenHelper.getWritableDatabase(); 53 } 54 55 class InMemoryRcsDatabase extends SQLiteOpenHelper { InMemoryRcsDatabase()56 InMemoryRcsDatabase() { 57 super(null, // no context is needed for in-memory db 58 null, // db file name is null for in-memory db 59 null, // CursorFactory is null by default 60 1); // db version is no-op for tests 61 } 62 63 @Override onCreate(SQLiteDatabase db)64 public void onCreate(SQLiteDatabase db) { 65 MmsSmsDatabaseHelper mmsSmsDatabaseHelper = new MmsSmsDatabaseHelper( 66 mockContextWithProvider, null); 67 mmsSmsDatabaseHelper.createMmsTables(db); 68 mmsSmsDatabaseHelper.createSmsTables(db); 69 mmsSmsDatabaseHelper.createCommonTables(db); 70 71 RcsProviderThreadHelper.createThreadTables(db); 72 RcsProviderParticipantHelper.createParticipantTables(db); 73 RcsProviderMessageHelper.createRcsMessageTables(db); 74 RcsProviderEventHelper.createRcsEventTables(db); 75 } 76 77 @Override onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)78 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 79 // no-op 80 } 81 } 82 83 static class MockContextWithProvider extends MockContext { 84 private final MockContentResolver mResolver; 85 MockContextWithProvider(RcsProvider rcsProvider)86 MockContextWithProvider(RcsProvider rcsProvider) { 87 mResolver = new MockContentResolver(); 88 89 // Add authority="rcs" to given smsProvider 90 ProviderInfo providerInfo = new ProviderInfo(); 91 providerInfo.authority = RcsProvider.AUTHORITY; 92 rcsProvider.attachInfoForTesting(this, providerInfo); 93 mResolver.addProvider(RcsProvider.AUTHORITY, rcsProvider); 94 } 95 96 @Override getContentResolver()97 public MockContentResolver getContentResolver() { 98 return mResolver; 99 } 100 101 @Override getPackageManager()102 public PackageManager getPackageManager() { 103 return Mockito.mock(PackageManager.class); 104 } 105 106 @Override getSystemService(String name)107 public Object getSystemService(String name) { 108 switch (name) { 109 case Context.APP_OPS_SERVICE: 110 return Mockito.mock(AppOpsManager.class); 111 case Context.TELEPHONY_SERVICE: 112 return Mockito.mock(TelephonyManager.class); 113 default: 114 return null; 115 } 116 } 117 118 @Override isCredentialProtectedStorage()119 public boolean isCredentialProtectedStorage() { 120 return false; 121 } 122 } 123 } 124