• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.settings.bluetooth;
18 
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.ArgumentMatchers.anyInt;
21 import static org.mockito.ArgumentMatchers.eq;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25 
26 import android.content.Context;
27 import android.content.ContextWrapper;
28 import android.content.Intent;
29 import android.content.pm.ActivityInfo;
30 import android.content.pm.PackageInfo;
31 import android.content.pm.PackageManager;
32 import android.os.Process;
33 
34 import org.junit.Before;
35 import org.junit.Ignore;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.ArgumentCaptor;
39 import org.mockito.MockitoAnnotations;
40 import org.robolectric.RobolectricTestRunner;
41 import org.robolectric.util.ReflectionHelpers;
42 
43 @Ignore("b/313014781")
44 @RunWith(RobolectricTestRunner.class)
45 public class BluetoothPermissionActivityTest {
46 
47     private BluetoothPermissionActivity mActivity;
48     private Context mContext;
49 
50     @Before
setUp()51     public void setUp() {
52         MockitoAnnotations.initMocks(this);
53         mContext = mock(ContextWrapper.class);
54         mActivity = new BluetoothPermissionActivity();
55     }
56 
57     @Test
sendBroadcastWithPermission()58     public void sendBroadcastWithPermission() throws Exception {
59         final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
60         ReflectionHelpers.setField(mActivity, "mBase", mContext);
61         when(mContext.createContextAsUser(any(), anyInt())).thenReturn(mContext);
62 
63         final String btPkgName = "com.android.bluetooth";
64         ActivityInfo btOppLauncherActivityInfo = new ActivityInfo();
65         btOppLauncherActivityInfo.name = "com.android.bluetooth.opp.BluetoothOppLauncherActivity";
66 
67         PackageInfo btPkgInfo = new PackageInfo();
68         btPkgInfo.activities = new ActivityInfo[] {btOppLauncherActivityInfo};
69 
70         PackageManager pm = mock(PackageManager.class);
71         when(pm.getPackagesForUid(Process.BLUETOOTH_UID)).thenReturn(new String[] {btPkgName});
72         when(pm.getPackageInfo(eq(btPkgName), anyInt())).thenReturn(btPkgInfo);
73         when(mContext.getPackageManager()).thenReturn(pm);
74 
75         mActivity.sendReplyIntentToReceiver(true, true);
76 
77         verify(mContext).sendBroadcast(intentCaptor.capture(),
78                 eq("android.permission.BLUETOOTH_CONNECT"));
79     }
80 }
81