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 com.android.settings.bluetooth.BluetoothPairingService.ACTION_DISMISS_PAIRING; 20 import static com.android.settings.bluetooth.BluetoothPairingService.ACTION_PAIRING_DIALOG; 21 22 import static org.mockito.ArgumentMatchers.any; 23 import static org.mockito.ArgumentMatchers.eq; 24 import static org.mockito.Mockito.verify; 25 import static org.mockito.Mockito.when; 26 27 import android.app.Application; 28 import android.app.NotificationManager; 29 import android.bluetooth.BluetoothDevice; 30 import android.content.Context; 31 import android.content.Intent; 32 import android.content.pm.ApplicationInfo; 33 import android.content.pm.PackageManager; 34 import android.content.res.Resources; 35 import android.util.DisplayMetrics; 36 37 import com.android.settings.R; 38 39 import org.junit.Before; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.mockito.Mock; 43 import org.mockito.MockitoAnnotations; 44 import org.robolectric.RobolectricTestRunner; 45 import org.robolectric.RuntimeEnvironment; 46 import org.robolectric.util.ReflectionHelpers; 47 48 @RunWith(RobolectricTestRunner.class) 49 public class BluetoothPairingServiceTest { 50 51 private final String mFakeTicker = "fake_ticker"; 52 53 @Mock 54 private NotificationManager mNm; 55 @Mock 56 private BluetoothDevice mDevice; 57 @Mock 58 private Context mContext; 59 @Mock 60 private Resources mResources; 61 @Mock 62 private PackageManager mPackageManager; 63 @Mock 64 private DisplayMetrics mDisplayMetrics; 65 66 private BluetoothPairingService mBluetoothPairingService; 67 private Application mApplication; 68 69 @Before setUp()70 public void setUp() { 71 MockitoAnnotations.initMocks(this); 72 73 mBluetoothPairingService = new BluetoothPairingService(); 74 mBluetoothPairingService.mNm = mNm; 75 mApplication = RuntimeEnvironment.application; 76 77 ReflectionHelpers.setField(mBluetoothPairingService, "mBase", mContext); 78 when(mContext.getResources()).thenReturn(mResources); 79 when(mResources.getString(R.string.bluetooth_notif_ticker)).thenReturn(mFakeTicker); 80 when(mContext.getApplicationInfo()).thenReturn(new ApplicationInfo()); 81 when(mContext.getPackageManager()).thenReturn(mPackageManager); 82 when(mResources.getDisplayMetrics()).thenReturn(mDisplayMetrics); 83 mDisplayMetrics.density = 1.5f; 84 } 85 86 @Test receivePairingRequestAction_notificationShown()87 public void receivePairingRequestAction_notificationShown() { 88 Intent intent = new Intent(); 89 intent.setAction(BluetoothDevice.ACTION_PAIRING_REQUEST); 90 intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice); 91 intent.putExtra(BluetoothDevice.EXTRA_NAME, "fake_name"); 92 when(mDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDING); 93 94 mBluetoothPairingService.onStartCommand(intent, /* flags */ 0, /* startId */ 0); 95 96 verify(mNm).notify(eq(mBluetoothPairingService.NOTIFICATION_ID), any()); 97 } 98 99 @Test receiveDismissPairingAction_cancelPairing()100 public void receiveDismissPairingAction_cancelPairing() { 101 Intent intent = new Intent(); 102 intent.setAction(ACTION_DISMISS_PAIRING); 103 intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice); 104 intent.putExtra(BluetoothDevice.EXTRA_NAME, "fake_name"); 105 when(mDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDING); 106 107 mBluetoothPairingService.onStartCommand(intent, /* flags */ 0, /* startId */ 0); 108 109 verify(mDevice).cancelPairing(); 110 verify(mNm).cancel(mBluetoothPairingService.NOTIFICATION_ID); 111 } 112 113 @Test receivePairingDialogAction_startActivity()114 public void receivePairingDialogAction_startActivity() { 115 Intent intent = new Intent(); 116 intent.setAction(ACTION_PAIRING_DIALOG); 117 intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice); 118 intent.putExtra(BluetoothDevice.EXTRA_NAME, "fake_name"); 119 when(mDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDING); 120 121 mBluetoothPairingService.onStartCommand(intent, /* flags */ 0, /* startId */ 0); 122 123 verify(mContext).startActivity(any()); 124 } 125 } 126