Merge "Switch the buttons in bt detail page"

This commit is contained in:
TreeHugger Robot
2017-10-20 17:55:37 +00:00
committed by Android (Google) Code Review
2 changed files with 45 additions and 45 deletions

View File

@@ -33,7 +33,7 @@ public class BluetoothDetailsButtonsController extends BluetoothDetailsControlle
private static final String KEY_ACTION_BUTTONS = "action_buttons"; private static final String KEY_ACTION_BUTTONS = "action_buttons";
private boolean mIsConnected; private boolean mIsConnected;
private boolean mButton1Initialized; private boolean mConnectButtonInitialized;
private ActionButtonPreference mActionButtons; private ActionButtonPreference mActionButtons;
public BluetoothDetailsButtonsController(Context context, PreferenceFragment fragment, public BluetoothDetailsButtonsController(Context context, PreferenceFragment fragment,
@@ -51,34 +51,34 @@ public class BluetoothDetailsButtonsController extends BluetoothDetailsControlle
@Override @Override
protected void init(PreferenceScreen screen) { protected void init(PreferenceScreen screen) {
mActionButtons = ((ActionButtonPreference) screen.findPreference(getPreferenceKey())) mActionButtons = ((ActionButtonPreference) screen.findPreference(getPreferenceKey()))
.setButton2Text(R.string.forget) .setButton1Text(R.string.forget)
.setButton2OnClickListener((view) -> onForgetButtonPressed()) .setButton1OnClickListener((view) -> onForgetButtonPressed())
.setButton2Positive(false) .setButton1Positive(false)
.setButton2Enabled(true); .setButton1Enabled(true);
} }
@Override @Override
protected void refresh() { protected void refresh() {
mActionButtons.setButton1Enabled(!mCachedDevice.isBusy()); mActionButtons.setButton2Enabled(!mCachedDevice.isBusy());
boolean previouslyConnected = mIsConnected; boolean previouslyConnected = mIsConnected;
mIsConnected = mCachedDevice.isConnected(); mIsConnected = mCachedDevice.isConnected();
if (mIsConnected) { if (mIsConnected) {
if (!mButton1Initialized || !previouslyConnected) { if (!mConnectButtonInitialized || !previouslyConnected) {
mActionButtons mActionButtons
.setButton1Text(R.string.bluetooth_device_context_disconnect) .setButton2Text(R.string.bluetooth_device_context_disconnect)
.setButton1OnClickListener(view -> mCachedDevice.disconnect()) .setButton2OnClickListener(view -> mCachedDevice.disconnect())
.setButton1Positive(false); .setButton2Positive(false);
mButton1Initialized = true; mConnectButtonInitialized = true;
} }
} else { } else {
if (!mButton1Initialized || previouslyConnected) { if (!mConnectButtonInitialized || previouslyConnected) {
mActionButtons mActionButtons
.setButton1Text(R.string.bluetooth_device_context_connect) .setButton2Text(R.string.bluetooth_device_context_connect)
.setButton1OnClickListener( .setButton2OnClickListener(
view -> mCachedDevice.connect(true /* connectAllProfiles */)) view -> mCachedDevice.connect(true /* connectAllProfiles */))
.setButton1Positive(true); .setButton2Positive(true);
mButton1Initialized = true; mConnectButtonInitialized = true;
} }
} }
} }

View File

@@ -48,30 +48,30 @@ import org.robolectric.annotation.Config;
public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsControllerTestBase { public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsControllerTestBase {
private BluetoothDetailsButtonsController mController; private BluetoothDetailsButtonsController mController;
private ActionButtonPreference mButtonsPref; private ActionButtonPreference mButtonsPref;
private Button mLeftButton; private Button mConnectButton;
private Button mRightButton; private Button mForgetButton;
@Override @Override
public void setUp() { public void setUp() {
super.setUp(); super.setUp();
final View buttons = View.inflate( final View buttons = View.inflate(
RuntimeEnvironment.application, R.layout.two_action_buttons, null /* parent */); RuntimeEnvironment.application, R.layout.two_action_buttons, null /* parent */);
mLeftButton = buttons.findViewById(R.id.button1_positive); mConnectButton = buttons.findViewById(R.id.button2_positive);
mRightButton = buttons.findViewById(R.id.button2_positive); mForgetButton = buttons.findViewById(R.id.button1_positive);
mController = new BluetoothDetailsButtonsController(mContext, mFragment, mCachedDevice, mController = new BluetoothDetailsButtonsController(mContext, mFragment, mCachedDevice,
mLifecycle); mLifecycle);
mButtonsPref = ActionButtonPreferenceTest.createMock(); mButtonsPref = ActionButtonPreferenceTest.createMock();
when(mButtonsPref.getKey()).thenReturn(mController.getPreferenceKey()); when(mButtonsPref.getKey()).thenReturn(mController.getPreferenceKey());
when(mButtonsPref.setButton1OnClickListener(any(View.OnClickListener.class)))
.thenAnswer(invocation -> {
final Object[] args = invocation.getArguments();
mLeftButton.setOnClickListener((View.OnClickListener) args[0]);
return mButtonsPref;
});
when(mButtonsPref.setButton2OnClickListener(any(View.OnClickListener.class))) when(mButtonsPref.setButton2OnClickListener(any(View.OnClickListener.class)))
.thenAnswer(invocation -> { .thenAnswer(invocation -> {
final Object[] args = invocation.getArguments(); final Object[] args = invocation.getArguments();
mRightButton.setOnClickListener((View.OnClickListener) args[0]); mConnectButton.setOnClickListener((View.OnClickListener) args[0]);
return mButtonsPref;
});
when(mButtonsPref.setButton1OnClickListener(any(View.OnClickListener.class)))
.thenAnswer(invocation -> {
final Object[] args = invocation.getArguments();
mForgetButton.setOnClickListener((View.OnClickListener) args[0]);
return mButtonsPref; return mButtonsPref;
}); });
mScreen.addPreference(mButtonsPref); mScreen.addPreference(mButtonsPref);
@@ -83,14 +83,14 @@ public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsContr
public void connected() { public void connected() {
showScreen(mController); showScreen(mController);
verify(mButtonsPref).setButton1Text(R.string.bluetooth_device_context_disconnect); verify(mButtonsPref).setButton2Text(R.string.bluetooth_device_context_disconnect);
verify(mButtonsPref).setButton2Text(R.string.forget); verify(mButtonsPref).setButton1Text(R.string.forget);
} }
@Test @Test
public void clickOnDisconnect() { public void clickOnDisconnect() {
showScreen(mController); showScreen(mController);
mLeftButton.callOnClick(); mConnectButton.callOnClick();
verify(mCachedDevice).disconnect(); verify(mCachedDevice).disconnect();
} }
@@ -100,9 +100,9 @@ public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsContr
when(mCachedDevice.isConnected()).thenReturn(false); when(mCachedDevice.isConnected()).thenReturn(false);
showScreen(mController); 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)); verify(mCachedDevice).connect(eq(true));
} }
@@ -110,15 +110,15 @@ public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsContr
public void becomeDisconnected() { public void becomeDisconnected() {
showScreen(mController); showScreen(mController);
// By default we start out with the device connected. // 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. // Now make the device appear to have changed to disconnected.
when(mCachedDevice.isConnected()).thenReturn(false); when(mCachedDevice.isConnected()).thenReturn(false);
mController.onDeviceAttributesChanged(); 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. // Click the button and make sure that connect (not disconnect) gets called.
mLeftButton.callOnClick(); mConnectButton.callOnClick();
verify(mCachedDevice).connect(eq(true)); verify(mCachedDevice).connect(eq(true));
} }
@@ -128,16 +128,16 @@ public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsContr
when(mCachedDevice.isConnected()).thenReturn(false); when(mCachedDevice.isConnected()).thenReturn(false);
showScreen(mController); 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. // Now make the device appear to have changed to connected.
when(mCachedDevice.isConnected()).thenReturn(true); when(mCachedDevice.isConnected()).thenReturn(true);
mController.onDeviceAttributesChanged(); 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. // Click the button and make sure that disconnnect (not connect) gets called.
mLeftButton.callOnClick(); mConnectButton.callOnClick();
verify(mCachedDevice).disconnect(); verify(mCachedDevice).disconnect();
} }
@@ -148,7 +148,7 @@ public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsContr
when(mFragment.getFragmentManager()).thenReturn(fragmentManager); when(mFragment.getFragmentManager()).thenReturn(fragmentManager);
FragmentTransaction ft = mock(FragmentTransaction.class); FragmentTransaction ft = mock(FragmentTransaction.class);
when(fragmentManager.beginTransaction()).thenReturn(ft); when(fragmentManager.beginTransaction()).thenReturn(ft);
mRightButton.callOnClick(); mForgetButton.callOnClick();
ArgumentCaptor<ForgetDeviceDialogFragment> dialogCaptor = ArgumentCaptor<ForgetDeviceDialogFragment> dialogCaptor =
ArgumentCaptor.forClass(ForgetDeviceDialogFragment.class); ArgumentCaptor.forClass(ForgetDeviceDialogFragment.class);
@@ -163,26 +163,26 @@ public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsContr
when(mCachedDevice.isBusy()).thenReturn(true); when(mCachedDevice.isBusy()).thenReturn(true);
showScreen(mController); showScreen(mController);
verify(mButtonsPref).setButton1Text(R.string.bluetooth_device_context_disconnect); verify(mButtonsPref).setButton2Text(R.string.bluetooth_device_context_disconnect);
verify(mButtonsPref).setButton1Enabled(false); verify(mButtonsPref).setButton2Enabled(false);
verify(mButtonsPref).setButton2Text(R.string.forget); verify(mButtonsPref).setButton1Text(R.string.forget);
// Now pretend the device became non-busy. // Now pretend the device became non-busy.
when(mCachedDevice.isBusy()).thenReturn(false); when(mCachedDevice.isBusy()).thenReturn(false);
mController.onDeviceAttributesChanged(); mController.onDeviceAttributesChanged();
verify(mButtonsPref).setButton1Enabled(true); verify(mButtonsPref).setButton2Enabled(true);
} }
@Test @Test
public void becomesBusy() { public void becomesBusy() {
showScreen(mController); showScreen(mController);
verify(mButtonsPref).setButton1Enabled(true); verify(mButtonsPref).setButton2Enabled(true);
// Now pretend the device became busy. // Now pretend the device became busy.
when(mCachedDevice.isBusy()).thenReturn(true); when(mCachedDevice.isBusy()).thenReturn(true);
mController.onDeviceAttributesChanged(); mController.onDeviceAttributesChanged();
verify(mButtonsPref).setButton1Enabled(false); verify(mButtonsPref).setButton2Enabled(false);
} }
} }