1 /* 2 * Copyright 2016 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.bluetooth.map; 18 19 import static org.mockito.Mockito.*; 20 21 import android.content.Context; 22 import android.database.Cursor; 23 import android.database.sqlite.SQLiteException; 24 import android.net.Uri; 25 import android.os.Looper; 26 import android.os.RemoteException; 27 import android.os.UserManager; 28 import android.support.test.InstrumentationRegistry; 29 import android.support.test.filters.MediumTest; 30 import android.support.test.runner.AndroidJUnit4; 31 import android.telephony.TelephonyManager; 32 import android.test.mock.MockContentProvider; 33 import android.test.mock.MockContentResolver; 34 35 import com.android.bluetooth.R; 36 37 import org.junit.Assert; 38 import org.junit.Assume; 39 import org.junit.Before; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 43 @MediumTest 44 @RunWith(AndroidJUnit4.class) 45 public class BluetoothMapContentObserverTest { 46 private Context mTargetContext; 47 48 class ExceptionTestProvider extends MockContentProvider { 49 public ExceptionTestProvider(Context context) { 50 super(context); 51 } 52 53 @Override 54 public Cursor query(Uri uri, String[] b, String s, String[] c, String d) { 55 throw new SQLiteException(); 56 } 57 } 58 59 @Before 60 public void setUp() { 61 mTargetContext = InstrumentationRegistry.getTargetContext(); 62 Assume.assumeTrue("Ignore test when BluetoothMapService is not enabled", 63 mTargetContext.getResources().getBoolean(R.bool.profile_supported_map)); 64 } 65 66 @Test 67 public void testInitMsgList() { 68 if (Looper.myLooper() == null) { 69 Looper.prepare(); 70 } 71 Context mockContext = mock(Context.class); 72 MockContentResolver mockResolver = new MockContentResolver(); 73 ExceptionTestProvider mockProvider = new ExceptionTestProvider(mockContext); 74 mockResolver.addProvider("sms", mockProvider); 75 76 TelephonyManager mockTelephony = mock(TelephonyManager.class); 77 UserManager mockUserService = mock(UserManager.class); 78 BluetoothMapMasInstance mockMas = mock(BluetoothMapMasInstance.class); 79 80 // Functions that get called when BluetoothMapContentObserver is created 81 when(mockUserService.isUserUnlocked()).thenReturn(true); 82 when(mockContext.getContentResolver()).thenReturn(mockResolver); 83 when(mockContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mockTelephony); 84 when(mockContext.getSystemService(Context.USER_SERVICE)).thenReturn(mockUserService); 85 86 BluetoothMapContentObserver observer; 87 try { 88 // The constructor of BluetoothMapContentObserver calls initMsgList 89 observer = new BluetoothMapContentObserver(mockContext, null, mockMas, null, true); 90 } catch (RemoteException e) { 91 Assert.fail("Failed to created BluetoothMapContentObserver object"); 92 } catch (SQLiteException e) { 93 Assert.fail("Threw SQLiteException instead of Assert.failing cleanly"); 94 } 95 } 96 } 97