• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.bluetooth.map;
2 
3 import android.content.Context;
4 import android.content.ContentResolver;
5 import android.content.ContentProvider;
6 import android.database.sqlite.SQLiteException;
7 import android.database.Cursor;
8 import android.net.Uri;
9 import android.os.Looper;
10 import android.os.RemoteException;
11 import android.os.UserManager;
12 import android.provider.Telephony.Sms;
13 import android.test.AndroidTestCase;
14 import android.test.mock.MockContentResolver;
15 import android.test.mock.MockContentProvider;
16 import android.telephony.TelephonyManager;
17 import android.util.Log;
18 
19 import junit.framework.Assert;
20 
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.when;
23 
24 public class BluetoothMapContentObserverTest extends AndroidTestCase {
25 
26     class ExceptionTestProvider extends MockContentProvider {
ExceptionTestProvider(Context context)27         public ExceptionTestProvider(Context context) {
28             super(context);
29         }
30 
31         @Override
query(Uri uri, String[] b, String s, String[] c, String d)32         public Cursor query(Uri uri, String[] b, String s, String[] c, String d) {
33             throw new SQLiteException();
34         }
35     }
36 
testInitMsgList()37     public void testInitMsgList() {
38         if (Looper.myLooper() == null) Looper.prepare();
39 
40         Context mockContext = mock(Context.class);
41         MockContentResolver mockResolver = new MockContentResolver();
42         ExceptionTestProvider mockProvider = new ExceptionTestProvider(mockContext);
43         mockResolver.addProvider("sms", mockProvider);
44 
45         TelephonyManager mockTelephony = mock(TelephonyManager.class);
46         UserManager mockUserService = mock(UserManager.class);
47         BluetoothMapMasInstance mockMas = mock(BluetoothMapMasInstance.class);
48 
49         // Functions that get called when BluetoothMapContentObserver is created
50         when(mockUserService.isUserUnlocked()).thenReturn(true);
51         when(mockContext.getContentResolver()).thenReturn(mockResolver);
52         when(mockContext.getSystemService(Context.TELEPHONY_SERVICE))
53             .thenReturn(mockTelephony);
54         when(mockContext.getSystemService(Context.USER_SERVICE))
55             .thenReturn(mockUserService);
56 
57         BluetoothMapContentObserver observer;
58         try {
59             // The constructor of BluetoothMapContentObserver calls initMsgList
60             observer = new BluetoothMapContentObserver(mockContext, null,
61                                                        mockMas, null, true);
62         } catch(RemoteException e) {
63             fail("Failed to created BluetoothMapContentObserver object");
64         } catch(SQLiteException e) {
65             fail("Threw SQLiteException instead of failing cleanly");
66         }
67     }
68 }
69