1 /*
2  * Copyright (C) 2017 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 com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.eq;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.never;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27 
28 import android.bluetooth.BluetoothDevice;
29 import android.content.Context;
30 import android.content.Intent;
31 
32 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.ArgumentCaptor;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.robolectric.RobolectricTestRunner;
41 import org.robolectric.RuntimeEnvironment;
42 
43 @RunWith(RobolectricTestRunner.class)
44 public class DevicePickerFragmentTest {
45 
46     @Mock
47     private BluetoothProgressCategory mAvailableDevicesCategory;
48 
49     private DevicePickerFragment mFragment;
50     private Context mContext;
51 
52     @Before
setUp()53     public void setUp() {
54         MockitoAnnotations.initMocks(this);
55 
56         mFragment = new DevicePickerFragment();
57         mContext = spy(RuntimeEnvironment.application);
58         mFragment.mContext = mContext;
59         mFragment.mAvailableDevicesCategory = mAvailableDevicesCategory;
60     }
61 
62     @Test
testScanningStateChanged_started_setProgressStarted()63     public void testScanningStateChanged_started_setProgressStarted() {
64         mFragment.mScanEnabled = true;
65 
66         mFragment.onScanningStateChanged(true);
67 
68         verify(mAvailableDevicesCategory).setProgress(true);
69     }
70 
71     @Test
callingPackageIsEqualToLaunchPackage_sendBroadcastToLaunchPackage()72     public void callingPackageIsEqualToLaunchPackage_sendBroadcastToLaunchPackage() {
73         final CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class);
74         final BluetoothDevice bluetoothDevice = mock(BluetoothDevice.class);
75         final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
76         when(cachedDevice.getDevice()).thenReturn(bluetoothDevice);
77         mFragment.mSelectedDevice = bluetoothDevice;
78         mFragment.mLaunchPackage = "com.android.settings";
79         mFragment.mLaunchClass = "com.android.settings.bluetooth.BluetoothPermissionActivity";
80         mFragment.mCallingAppPackageName = "com.android.settings";
81 
82         mFragment.onDeviceBondStateChanged(cachedDevice, BluetoothDevice.BOND_BONDED);
83 
84         verify(mContext).sendBroadcast(intentCaptor.capture(),
85                 eq("android.permission.BLUETOOTH_CONNECT"));
86         assertThat(intentCaptor.getValue().getComponent().getPackageName())
87                 .isEqualTo(mFragment.mLaunchPackage);
88     }
89 
90     @Test
callingPackageIsNotEqualToLaunchPackage_broadcastNotSend()91     public void callingPackageIsNotEqualToLaunchPackage_broadcastNotSend() {
92         final CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class);
93         final BluetoothDevice bluetoothDevice = mock(BluetoothDevice.class);
94         final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
95         when(cachedDevice.getDevice()).thenReturn(bluetoothDevice);
96         mFragment.mSelectedDevice = bluetoothDevice;
97         mFragment.mLaunchPackage = "com.fake.settings";
98         mFragment.mLaunchClass = "com.android.settings.bluetooth.BluetoothPermissionActivity";
99         mFragment.mCallingAppPackageName = "com.android.settings";
100 
101         mFragment.onDeviceBondStateChanged(cachedDevice, BluetoothDevice.BOND_BONDED);
102 
103         verify(mContext, never()).sendBroadcast(intentCaptor.capture());
104     }
105 }
106