From b6fc6e31a5a528e330b1830fa48a6c07d2122205 Mon Sep 17 00:00:00 2001 From: Antony Sargent Date: Fri, 30 Jun 2017 15:23:01 -0700 Subject: [PATCH] Add a confirmation dialog when forgetting BT device This adds a confirmation dialog for when you hit the "Forget" button to unpair with a device from the Bluetooth device details page. Bug: 37955181 Test: make RunSettingsRoboTests Change-Id: I7643ed09bf363c48078d6de8a47583bf91fc7729 --- .../BluetoothDetailsButtonsController.java | 9 +- .../bluetooth/ForgetDeviceDialogFragment.java | 84 ++++++++++++++++++ ...BluetoothDetailsButtonsControllerTest.java | 25 ++++-- .../ForgetDeviceDialogFragmentTest.java | 88 +++++++++++++++++++ 4 files changed, 198 insertions(+), 8 deletions(-) create mode 100644 src/com/android/settings/bluetooth/ForgetDeviceDialogFragment.java create mode 100644 tests/robotests/src/com/android/settings/bluetooth/ForgetDeviceDialogFragmentTest.java diff --git a/src/com/android/settings/bluetooth/BluetoothDetailsButtonsController.java b/src/com/android/settings/bluetooth/BluetoothDetailsButtonsController.java index 41cd28d8926..bbc8d81ebcb 100644 --- a/src/com/android/settings/bluetooth/BluetoothDetailsButtonsController.java +++ b/src/com/android/settings/bluetooth/BluetoothDetailsButtonsController.java @@ -43,14 +43,19 @@ public class BluetoothDetailsButtonsController extends BluetoothDetailsControlle mIsConnected = device.isConnected(); } + private void onForgetButtonPressed() { + ForgetDeviceDialogFragment fragment = + ForgetDeviceDialogFragment.newInstance(mCachedDevice.getAddress()); + fragment.show(mFragment.getFragmentManager(), ForgetDeviceDialogFragment.TAG); + } + @Override protected void init(PreferenceScreen screen) { mActionButtons = (LayoutPreference) screen.findPreference(getPreferenceKey()); Button rightButton = (Button) mActionButtons.findViewById(R.id.right_button); rightButton.setText(R.string.forget); rightButton.setOnClickListener((view) -> { - mCachedDevice.unpair(); - mFragment.getActivity().finish(); + onForgetButtonPressed(); }); } diff --git a/src/com/android/settings/bluetooth/ForgetDeviceDialogFragment.java b/src/com/android/settings/bluetooth/ForgetDeviceDialogFragment.java new file mode 100644 index 00000000000..1f3a689da66 --- /dev/null +++ b/src/com/android/settings/bluetooth/ForgetDeviceDialogFragment.java @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.bluetooth; + +import android.app.Activity; +import android.app.AlertDialog; +import android.app.Dialog; +import android.bluetooth.BluetoothDevice; +import android.content.Context; +import android.content.DialogInterface; +import android.os.Bundle; + +import com.android.internal.annotations.VisibleForTesting; +import com.android.internal.logging.nano.MetricsProto; +import com.android.settings.R; +import com.android.settings.core.instrumentation.InstrumentedDialogFragment; +import com.android.settingslib.bluetooth.CachedBluetoothDevice; +import com.android.settingslib.bluetooth.LocalBluetoothManager; + +/** Implements an AlertDialog for confirming that a user wishes to unpair or "forget" a paired + * device*/ +public class ForgetDeviceDialogFragment extends InstrumentedDialogFragment { + public static final String TAG = "ForgetBluetoothDevice"; + private static final String KEY_DEVICE_ADDRESS = "device_address"; + + private CachedBluetoothDevice mDevice; + + public static ForgetDeviceDialogFragment newInstance(String deviceAddress) { + Bundle args = new Bundle(1); + args.putString(KEY_DEVICE_ADDRESS, deviceAddress); + ForgetDeviceDialogFragment dialog = new ForgetDeviceDialogFragment(); + dialog.setArguments(args); + return dialog; + } + + @VisibleForTesting + CachedBluetoothDevice getDevice(Context context) { + String deviceAddress = getArguments().getString(KEY_DEVICE_ADDRESS); + LocalBluetoothManager manager = Utils.getLocalBtManager(context); + BluetoothDevice device = manager.getBluetoothAdapter().getRemoteDevice(deviceAddress); + return manager.getCachedDeviceManager().findDevice(device); + } + + @Override + public int getMetricsCategory() { + return MetricsProto.MetricsEvent.DIALOG_BLUETOOTH_PAIRED_DEVICE_FORGET; + } + + @Override + public Dialog onCreateDialog(Bundle inState) { + DialogInterface.OnClickListener onConfirm = (dialog, which) -> { + mDevice.unpair(); + Activity activity = getActivity(); + if (activity != null) { + activity.finish(); + } + }; + Context context = getContext(); + mDevice = getDevice(context); + AlertDialog dialog = new AlertDialog.Builder(context) + .setPositiveButton(R.string.bluetooth_unpair_dialog_forget_confirm_button, + onConfirm) + .setNegativeButton(android.R.string.cancel, null) + .create(); + dialog.setTitle(R.string.bluetooth_unpair_dialog_title); + dialog.setMessage(context.getString(R.string.bluetooth_unpair_dialog_body, + mDevice.getName())); + return dialog; + } +} diff --git a/tests/robotests/src/com/android/settings/bluetooth/BluetoothDetailsButtonsControllerTest.java b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDetailsButtonsControllerTest.java index f2dc8517b51..7af2b851953 100644 --- a/tests/robotests/src/com/android/settings/bluetooth/BluetoothDetailsButtonsControllerTest.java +++ b/tests/robotests/src/com/android/settings/bluetooth/BluetoothDetailsButtonsControllerTest.java @@ -18,25 +18,30 @@ package com.android.settings.bluetooth; import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import android.app.FragmentManager; +import android.app.FragmentTransaction; import android.widget.Button; import com.android.settings.R; -import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.TestConfig; import com.android.settings.applications.LayoutPreference; +import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.testutils.shadow.SettingsShadowBluetoothDevice; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; import org.robolectric.annotation.Config; @RunWith(SettingsRobolectricTestRunner.class) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, - shadows=SettingsShadowBluetoothDevice.class) + shadows = SettingsShadowBluetoothDevice.class) public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsControllerTestBase { private BluetoothDetailsButtonsController mController; private LayoutPreference mLayoutPreference; @@ -125,13 +130,21 @@ public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsContr } @Test - public void forget() { + public void forgetDialog() { showScreen(mController); + FragmentManager fragmentManager = mock(FragmentManager.class); + when(mFragment.getFragmentManager()).thenReturn(fragmentManager); + FragmentTransaction ft = mock(FragmentTransaction.class); + when(fragmentManager.beginTransaction()).thenReturn(ft); mRightButton.callOnClick(); - verify(mCachedDevice).unpair(); - verify(mActivity).finish(); - } + ArgumentCaptor dialogCaptor = + ArgumentCaptor.forClass(ForgetDeviceDialogFragment.class); + verify(ft).add(dialogCaptor.capture(), anyString()); + + ForgetDeviceDialogFragment dialogFragment = dialogCaptor.getValue(); + assertThat(dialogFragment).isNotNull(); + } @Test public void startsOutBusy() { diff --git a/tests/robotests/src/com/android/settings/bluetooth/ForgetDeviceDialogFragmentTest.java b/tests/robotests/src/com/android/settings/bluetooth/ForgetDeviceDialogFragmentTest.java new file mode 100644 index 00000000000..21bc868ed49 --- /dev/null +++ b/tests/robotests/src/com/android/settings/bluetooth/ForgetDeviceDialogFragmentTest.java @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.bluetooth; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.app.Activity; +import android.app.AlertDialog; +import android.content.Context; + +import com.android.settings.TestConfig; +import com.android.settings.testutils.FakeFeatureFactory; +import com.android.settings.testutils.SettingsRobolectricTestRunner; +import com.android.settingslib.bluetooth.CachedBluetoothDevice; +import com.android.settingslib.bluetooth.LocalBluetoothManager; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Answers; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.Robolectric; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; +import org.robolectric.shadows.ShadowDialog; + +@RunWith(SettingsRobolectricTestRunner.class) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +public class ForgetDeviceDialogFragmentTest { + + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + private CachedBluetoothDevice mCachedDevice; + + private ForgetDeviceDialogFragment mFragment; + private Context mContext; + private Activity mActivity; + private AlertDialog mDialog; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mContext = spy(RuntimeEnvironment.application); + FakeFeatureFactory.setupForTest(mContext); + String deviceAddress = "55:66:77:88:99:AA"; + when(mCachedDevice.getAddress()).thenReturn(deviceAddress); + mFragment = spy(ForgetDeviceDialogFragment.newInstance(deviceAddress)); + doReturn(mCachedDevice).when(mFragment).getDevice(any()); + mActivity = Robolectric.setupActivity(Activity.class); + mActivity.getFragmentManager().beginTransaction().add(mFragment, null).commit(); + mDialog = (AlertDialog) ShadowDialog.getLatestDialog(); + } + + @Test + public void cancelDialog() { + mDialog.getButton(AlertDialog.BUTTON_NEGATIVE).performClick(); + verify(mCachedDevice, never()).unpair(); + assertThat(mActivity.isFinishing()).isFalse(); + } + + @Test + public void confirmDialog() { + mDialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick(); + verify(mCachedDevice).unpair(); + assertThat(mActivity.isFinishing()).isTrue(); + } +}