1 /* 2 * Copyright (C) 2024 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 package com.android.settings.bluetooth; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import static org.mockito.Mockito.when; 21 import static org.robolectric.Shadows.shadowOf; 22 import static org.robolectric.shadows.ShadowLooper.shadowMainLooper; 23 24 import android.bluetooth.BluetoothDevice; 25 import android.content.Intent; 26 27 import androidx.appcompat.app.AlertDialog; 28 import androidx.fragment.app.FragmentActivity; 29 30 import com.android.settings.SettingsActivity; 31 import com.android.settings.SubSettings; 32 import com.android.settings.testutils.shadow.ShadowAlertDialogCompat; 33 import com.android.settings.testutils.shadow.ShadowBluetoothUtils; 34 import com.android.settingslib.bluetooth.LocalBluetoothManager; 35 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.mockito.Answers; 40 import org.mockito.Mock; 41 import org.mockito.MockitoAnnotations; 42 import org.robolectric.Robolectric; 43 import org.robolectric.RobolectricTestRunner; 44 import org.robolectric.annotation.Config; 45 import org.robolectric.shadows.ShadowIntent; 46 47 @RunWith(RobolectricTestRunner.class) 48 @Config(shadows = {ShadowAlertDialogCompat.class, ShadowBluetoothUtils.class}) 49 public class BluetoothKeyMissingDialogTest { 50 @Mock private BluetoothDevice mBluetoothDevice; 51 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 52 private LocalBluetoothManager mLocalBtManager; 53 54 private BluetoothKeyMissingDialogFragment mFragment = null; 55 private FragmentActivity mActivity = null; 56 57 private static final String MAC_ADDRESS = "12:34:56:78:90:12"; 58 59 @Before setUp()60 public void setUp() { 61 MockitoAnnotations.initMocks(this); 62 when(mBluetoothDevice.getAddress()).thenReturn(MAC_ADDRESS); 63 when(mLocalBtManager.getBluetoothAdapter().getRemoteDevice(MAC_ADDRESS)) 64 .thenReturn(mBluetoothDevice); 65 ShadowBluetoothUtils.sLocalBluetoothManager = mLocalBtManager; 66 mActivity = Robolectric.setupActivity(FragmentActivity.class); 67 mFragment = BluetoothKeyMissingDialogFragment.newInstance(mBluetoothDevice); 68 mActivity 69 .getSupportFragmentManager() 70 .beginTransaction() 71 .add(mFragment, null) 72 .commit(); 73 shadowMainLooper().idle(); 74 } 75 76 @Test clickDeviceSettings_launchDeviceDetails()77 public void clickDeviceSettings_launchDeviceDetails() { 78 mFragment.onClick(mFragment.getDialog(), AlertDialog.BUTTON_POSITIVE); 79 80 Intent startedIntent = shadowOf(mActivity).getNextStartedActivity(); 81 ShadowIntent shadowIntent = shadowOf(startedIntent); 82 assertThat(shadowIntent.getIntentClass()).isEqualTo(SubSettings.class); 83 assertThat(startedIntent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT)) 84 .isEqualTo(BluetoothDeviceDetailsFragment.class.getName()); 85 assertThat(mActivity.isFinishing()).isTrue(); 86 } 87 88 @Test clickCancel_notLaunchDeviceDetails()89 public void clickCancel_notLaunchDeviceDetails() { 90 mFragment.onClick(mFragment.getDialog(), AlertDialog.BUTTON_NEGATIVE); 91 92 Intent startedIntent = shadowOf(mActivity).getNextStartedActivity(); 93 assertThat(startedIntent).isNull(); 94 assertThat(mActivity.isFinishing()).isTrue(); 95 } 96 } 97