Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d9ecc3ae authored by Lei Yu's avatar Lei Yu
Browse files

Update dialog text for untethered BT device

Fixes: 122672631
Test: RunSettingsRoboTests
Change-Id: I380308acbb7025461ef34aeec68e535189714bf3
parent 7c802e55
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -1786,6 +1786,9 @@
    <!--  Bluetooth device details. The body of a confirmation dialog for unpairing a paired device. -->
    <string name="bluetooth_unpair_dialog_body" product="device">Your device will no longer be paired with <xliff:g id="device_name">%1$s</xliff:g></string>
    <!--  Bluetooth device details. The body of a confirmation dialog for unpairing a paired device. [CHAR LIMIT=NONE] -->
    <string name="bluetooth_untethered_unpair_dialog_body"><xliff:g id="device_name" example="Jack's headphone">%1$s</xliff:g> will no longer be paired with any device linked to this account</string>
    <!--  Bluetooth device details. In the confirmation dialog for unpairing a paired device, this is the label on the button that will complete the unpairing action. -->
    <string name="bluetooth_unpair_dialog_forget_confirm_button">Forget device</string>
+7 −1
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import androidx.appcompat.app.AlertDialog;

import com.android.settings.R;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;

@@ -72,13 +73,18 @@ public class ForgetDeviceDialogFragment extends InstrumentedDialogFragment {
        };
        Context context = getContext();
        mDevice = getDevice(context);
        final boolean untetheredHeadset = BluetoothUtils.getBooleanMetaData(
                mDevice.getDevice(), BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET);

        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,
        dialog.setMessage(context.getString(untetheredHeadset
                        ? R.string.bluetooth_untethered_unpair_dialog_body
                        : R.string.bluetooth_unpair_dialog_body,
                mDevice.getName()));
        return dialog;
    }
+55 −2
Original line number Diff line number Diff line
@@ -25,10 +25,15 @@ import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.bluetooth.BluetoothDevice;
import android.content.Context;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.FragmentActivity;

import com.android.settings.R;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;

import org.junit.Before;
@@ -39,33 +44,46 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowDialog;
import org.robolectric.shadows.androidx.fragment.FragmentController;

@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowAlertDialogCompat.class})
public class ForgetDeviceDialogFragmentTest {

    private static final String DEVICE_NAME = "Nightshade";

    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
    private CachedBluetoothDevice mCachedDevice;
    @Mock
    private BluetoothDevice mBluetoothDevice;

    private ForgetDeviceDialogFragment mFragment;
    private FragmentActivity mActivity;
    private AlertDialog mDialog;
    private Context mContext;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        mContext = RuntimeEnvironment.application;
        FakeFeatureFactory.setupForTest();
        String deviceAddress = "55:66:77:88:99:AA";
        when(mCachedDevice.getAddress()).thenReturn(deviceAddress);
        when(mCachedDevice.getDevice()).thenReturn(mBluetoothDevice);
        when(mCachedDevice.getName()).thenReturn(DEVICE_NAME);
        mFragment = spy(ForgetDeviceDialogFragment.newInstance(deviceAddress));
        doReturn(mCachedDevice).when(mFragment).getDevice(any());
        mActivity = Robolectric.setupActivity(FragmentActivity.class);
        mActivity.getSupportFragmentManager().beginTransaction().add(mFragment, null).commit();
        mDialog = (AlertDialog) ShadowDialog.getLatestDialog();
    }

    @Test
    public void cancelDialog() {
        initDialog();

        mDialog.getButton(AlertDialog.BUTTON_NEGATIVE).performClick();
        verify(mCachedDevice, never()).unpair();
        assertThat(mActivity.isFinishing()).isFalse();
@@ -73,8 +91,43 @@ public class ForgetDeviceDialogFragmentTest {

    @Test
    public void confirmDialog() {
        initDialog();

        mDialog.getButton(AlertDialog.BUTTON_POSITIVE).performClick();
        verify(mCachedDevice).unpair();
        assertThat(mActivity.isFinishing()).isTrue();
    }

    @Test
    public void createDialog_untetheredDevice_showUntetheredMessage() {
        when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET))
                .thenReturn("true".getBytes());

        FragmentController.setupFragment(mFragment, FragmentActivity.class,
                0 /* containerViewId */, null /* bundle */);
        final AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
        ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);

        assertThat(shadowDialog.getMessage()).isEqualTo(
                mContext.getString(R.string.bluetooth_untethered_unpair_dialog_body, DEVICE_NAME));
    }

    @Test
    public void createDialog_normalDevice_showNormalMessage() {
        when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_IS_UNTETHERED_HEADSET))
                .thenReturn("false".getBytes());

        FragmentController.setupFragment(mFragment, FragmentActivity.class,
                0 /* containerViewId */, null /* bundle */);
        final AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
        ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);

        assertThat(shadowDialog.getMessage()).isEqualTo(
                mContext.getString(R.string.bluetooth_unpair_dialog_body, DEVICE_NAME));
    }

    private void initDialog() {
        mActivity.getSupportFragmentManager().beginTransaction().add(mFragment, null).commit();
        mDialog = (AlertDialog) ShadowDialog.getLatestDialog();
    }
}