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

Commit 04d9769c authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Switch the buttons in bt detail page"

parents 822e666f d3ffdc56
Loading
Loading
Loading
Loading
+16 −16
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ public class BluetoothDetailsButtonsController extends BluetoothDetailsControlle
    private static final String KEY_ACTION_BUTTONS = "action_buttons";
    private boolean mIsConnected;

    private boolean mButton1Initialized;
    private boolean mConnectButtonInitialized;
    private ActionButtonPreference mActionButtons;

    public BluetoothDetailsButtonsController(Context context, PreferenceFragment fragment,
@@ -51,34 +51,34 @@ public class BluetoothDetailsButtonsController extends BluetoothDetailsControlle
    @Override
    protected void init(PreferenceScreen screen) {
        mActionButtons = ((ActionButtonPreference) screen.findPreference(getPreferenceKey()))
                .setButton2Text(R.string.forget)
                .setButton2OnClickListener((view) -> onForgetButtonPressed())
                .setButton2Positive(false)
                .setButton2Enabled(true);
                .setButton1Text(R.string.forget)
                .setButton1OnClickListener((view) -> onForgetButtonPressed())
                .setButton1Positive(false)
                .setButton1Enabled(true);
    }

    @Override
    protected void refresh() {
        mActionButtons.setButton1Enabled(!mCachedDevice.isBusy());
        mActionButtons.setButton2Enabled(!mCachedDevice.isBusy());

        boolean previouslyConnected = mIsConnected;
        mIsConnected = mCachedDevice.isConnected();
        if (mIsConnected) {
            if (!mButton1Initialized || !previouslyConnected) {
            if (!mConnectButtonInitialized || !previouslyConnected) {
                mActionButtons
                        .setButton1Text(R.string.bluetooth_device_context_disconnect)
                        .setButton1OnClickListener(view -> mCachedDevice.disconnect())
                        .setButton1Positive(false);
                mButton1Initialized = true;
                        .setButton2Text(R.string.bluetooth_device_context_disconnect)
                        .setButton2OnClickListener(view -> mCachedDevice.disconnect())
                        .setButton2Positive(false);
                mConnectButtonInitialized = true;
            }
        } else {
            if (!mButton1Initialized || previouslyConnected) {
            if (!mConnectButtonInitialized || previouslyConnected) {
                mActionButtons
                        .setButton1Text(R.string.bluetooth_device_context_connect)
                        .setButton1OnClickListener(
                        .setButton2Text(R.string.bluetooth_device_context_connect)
                        .setButton2OnClickListener(
                                view -> mCachedDevice.connect(true /* connectAllProfiles */))
                        .setButton1Positive(true);
                mButton1Initialized = true;
                        .setButton2Positive(true);
                mConnectButtonInitialized = true;
            }
        }
    }
+26 −26
Original line number Diff line number Diff line
@@ -48,30 +48,30 @@ import org.robolectric.annotation.Config;
public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsControllerTestBase {
    private BluetoothDetailsButtonsController mController;
    private ActionButtonPreference mButtonsPref;
    private Button mLeftButton;
    private Button mRightButton;
    private Button mConnectButton;
    private Button mForgetButton;

    @Override
    public void setUp() {
        super.setUp();
        final View buttons = View.inflate(
                RuntimeEnvironment.application, R.layout.two_action_buttons, null /* parent */);
        mLeftButton = buttons.findViewById(R.id.button1_positive);
        mRightButton = buttons.findViewById(R.id.button2_positive);
        mConnectButton = buttons.findViewById(R.id.button2_positive);
        mForgetButton = buttons.findViewById(R.id.button1_positive);
        mController = new BluetoothDetailsButtonsController(mContext, mFragment, mCachedDevice,
                mLifecycle);
        mButtonsPref = ActionButtonPreferenceTest.createMock();
        when(mButtonsPref.getKey()).thenReturn(mController.getPreferenceKey());
        when(mButtonsPref.setButton1OnClickListener(any(View.OnClickListener.class)))
        when(mButtonsPref.setButton2OnClickListener(any(View.OnClickListener.class)))
                .thenAnswer(invocation -> {
                    final Object[] args = invocation.getArguments();
                    mLeftButton.setOnClickListener((View.OnClickListener) args[0]);
                    mConnectButton.setOnClickListener((View.OnClickListener) args[0]);
                    return mButtonsPref;
                });
        when(mButtonsPref.setButton2OnClickListener(any(View.OnClickListener.class)))
        when(mButtonsPref.setButton1OnClickListener(any(View.OnClickListener.class)))
                .thenAnswer(invocation -> {
                    final Object[] args = invocation.getArguments();
                    mRightButton.setOnClickListener((View.OnClickListener) args[0]);
                    mForgetButton.setOnClickListener((View.OnClickListener) args[0]);
                    return mButtonsPref;
                });
        mScreen.addPreference(mButtonsPref);
@@ -83,14 +83,14 @@ public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsContr
    public void connected() {
        showScreen(mController);

        verify(mButtonsPref).setButton1Text(R.string.bluetooth_device_context_disconnect);
        verify(mButtonsPref).setButton2Text(R.string.forget);
        verify(mButtonsPref).setButton2Text(R.string.bluetooth_device_context_disconnect);
        verify(mButtonsPref).setButton1Text(R.string.forget);
    }

    @Test
    public void clickOnDisconnect() {
        showScreen(mController);
        mLeftButton.callOnClick();
        mConnectButton.callOnClick();

        verify(mCachedDevice).disconnect();
    }
@@ -100,9 +100,9 @@ public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsContr
        when(mCachedDevice.isConnected()).thenReturn(false);
        showScreen(mController);

        verify(mButtonsPref).setButton1Text(R.string.bluetooth_device_context_connect);
        verify(mButtonsPref).setButton2Text(R.string.bluetooth_device_context_connect);

        mLeftButton.callOnClick();
        mConnectButton.callOnClick();
        verify(mCachedDevice).connect(eq(true));
    }

@@ -110,15 +110,15 @@ public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsContr
    public void becomeDisconnected() {
        showScreen(mController);
        // By default we start out with the device connected.
        verify(mButtonsPref).setButton1Text(R.string.bluetooth_device_context_disconnect);
        verify(mButtonsPref).setButton2Text(R.string.bluetooth_device_context_disconnect);

        // Now make the device appear to have changed to disconnected.
        when(mCachedDevice.isConnected()).thenReturn(false);
        mController.onDeviceAttributesChanged();
        verify(mButtonsPref).setButton1Text(R.string.bluetooth_device_context_connect);
        verify(mButtonsPref).setButton2Text(R.string.bluetooth_device_context_connect);

        // Click the button and make sure that connect (not disconnect) gets called.
        mLeftButton.callOnClick();
        mConnectButton.callOnClick();
        verify(mCachedDevice).connect(eq(true));
    }

@@ -128,16 +128,16 @@ public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsContr
        when(mCachedDevice.isConnected()).thenReturn(false);
        showScreen(mController);

        verify(mButtonsPref).setButton1Text(R.string.bluetooth_device_context_connect);
        verify(mButtonsPref).setButton2Text(R.string.bluetooth_device_context_connect);


        // Now make the device appear to have changed to connected.
        when(mCachedDevice.isConnected()).thenReturn(true);
        mController.onDeviceAttributesChanged();
        verify(mButtonsPref).setButton1Text(R.string.bluetooth_device_context_disconnect);
        verify(mButtonsPref).setButton2Text(R.string.bluetooth_device_context_disconnect);

        // Click the button and make sure that disconnnect (not connect) gets called.
        mLeftButton.callOnClick();
        mConnectButton.callOnClick();
        verify(mCachedDevice).disconnect();
    }

@@ -148,7 +148,7 @@ public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsContr
        when(mFragment.getFragmentManager()).thenReturn(fragmentManager);
        FragmentTransaction ft = mock(FragmentTransaction.class);
        when(fragmentManager.beginTransaction()).thenReturn(ft);
        mRightButton.callOnClick();
        mForgetButton.callOnClick();

        ArgumentCaptor<ForgetDeviceDialogFragment> dialogCaptor =
                ArgumentCaptor.forClass(ForgetDeviceDialogFragment.class);
@@ -163,26 +163,26 @@ public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsContr
        when(mCachedDevice.isBusy()).thenReturn(true);
        showScreen(mController);

        verify(mButtonsPref).setButton1Text(R.string.bluetooth_device_context_disconnect);
        verify(mButtonsPref).setButton1Enabled(false);
        verify(mButtonsPref).setButton2Text(R.string.forget);
        verify(mButtonsPref).setButton2Text(R.string.bluetooth_device_context_disconnect);
        verify(mButtonsPref).setButton2Enabled(false);
        verify(mButtonsPref).setButton1Text(R.string.forget);

        // Now pretend the device became non-busy.
        when(mCachedDevice.isBusy()).thenReturn(false);
        mController.onDeviceAttributesChanged();

        verify(mButtonsPref).setButton1Enabled(true);
        verify(mButtonsPref).setButton2Enabled(true);
    }

    @Test
    public void becomesBusy() {
        showScreen(mController);
        verify(mButtonsPref).setButton1Enabled(true);
        verify(mButtonsPref).setButton2Enabled(true);

        // Now pretend the device became busy.
        when(mCachedDevice.isBusy()).thenReturn(true);
        mController.onDeviceAttributesChanged();

        verify(mButtonsPref).setButton1Enabled(false);
        verify(mButtonsPref).setButton2Enabled(false);
    }
}