Convert more pages to use ActionButtonPreference

- text to speech page
- blue tooth detail page
- installed app page

Change-Id: Ib743a144cc6025055cd496ec077f13fce4db0c69
Bug: 63991885
Test: robotests
This commit is contained in:
Fan Zhang
2017-07-28 17:18:44 -07:00
parent 3eb22656eb
commit 1105a1a465
11 changed files with 248 additions and 296 deletions

View File

@@ -17,7 +17,7 @@
package com.android.settings.bluetooth;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
@@ -26,17 +26,20 @@ import static org.mockito.Mockito.when;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import com.android.settings.R;
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 com.android.settings.widget.ActionButtonPreference;
import com.android.settings.widget.ActionButtonPreferenceTest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@@ -44,22 +47,34 @@ import org.robolectric.annotation.Config;
shadows = SettingsShadowBluetoothDevice.class)
public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsControllerTestBase {
private BluetoothDetailsButtonsController mController;
private LayoutPreference mLayoutPreference;
private ActionButtonPreference mButtonsPref;
private Button mLeftButton;
private Button mRightButton;
@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);
mController = new BluetoothDetailsButtonsController(mContext, mFragment, mCachedDevice,
mLifecycle);
mLeftButton = new Button(mContext);
mRightButton = new Button(mContext);
mLayoutPreference = new LayoutPreference(mContext, R.layout.app_action_buttons);
mLayoutPreference.setKey(mController.getPreferenceKey());
mScreen.addPreference(mLayoutPreference);
mLeftButton = (Button) mLayoutPreference.findViewById(R.id.left_button);
mRightButton = (Button) mLayoutPreference.findViewById(R.id.right_button);
mButtonsPref = ActionButtonPreferenceTest.createMock();
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)))
.thenAnswer(invocation -> {
final Object[] args = invocation.getArguments();
mRightButton.setOnClickListener((View.OnClickListener) args[0]);
return mButtonsPref;
});
mScreen.addPreference(mButtonsPref);
setupDevice(mDeviceConfig);
when(mCachedDevice.isBusy()).thenReturn(false);
}
@@ -67,15 +82,16 @@ public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsContr
@Test
public void connected() {
showScreen(mController);
assertThat(mLeftButton.getText()).isEqualTo(
mContext.getString(R.string.bluetooth_device_context_disconnect));
assertThat(mRightButton.getText()).isEqualTo(mContext.getString(R.string.forget));
verify(mButtonsPref).setButton1Text(R.string.bluetooth_device_context_disconnect);
verify(mButtonsPref).setButton2Text(R.string.forget);
}
@Test
public void clickOnDisconnect() {
showScreen(mController);
mLeftButton.callOnClick();
verify(mCachedDevice).disconnect();
}
@@ -84,8 +100,7 @@ public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsContr
when(mCachedDevice.isConnected()).thenReturn(false);
showScreen(mController);
assertThat(mLeftButton.getText()).isEqualTo(
mContext.getString(R.string.bluetooth_device_context_connect));
verify(mButtonsPref).setButton1Text(R.string.bluetooth_device_context_connect);
mLeftButton.callOnClick();
verify(mCachedDevice).connect(eq(true));
@@ -95,14 +110,12 @@ public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsContr
public void becomeDisconnected() {
showScreen(mController);
// By default we start out with the device connected.
assertThat(mLeftButton.getText()).isEqualTo(
mContext.getString(R.string.bluetooth_device_context_disconnect));
verify(mButtonsPref).setButton1Text(R.string.bluetooth_device_context_disconnect);
// Now make the device appear to have changed to disconnected.
when(mCachedDevice.isConnected()).thenReturn(false);
mController.onDeviceAttributesChanged();
assertThat(mLeftButton.getText()).isEqualTo(
mContext.getString(R.string.bluetooth_device_context_connect));
verify(mButtonsPref).setButton1Text(R.string.bluetooth_device_context_connect);
// Click the button and make sure that connect (not disconnect) gets called.
mLeftButton.callOnClick();
@@ -115,14 +128,13 @@ public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsContr
when(mCachedDevice.isConnected()).thenReturn(false);
showScreen(mController);
assertThat(mLeftButton.getText()).isEqualTo(
mContext.getString(R.string.bluetooth_device_context_connect));
verify(mButtonsPref).setButton1Text(R.string.bluetooth_device_context_connect);
// Now make the device appear to have changed to connected.
when(mCachedDevice.isConnected()).thenReturn(true);
mController.onDeviceAttributesChanged();
assertThat(mLeftButton.getText()).isEqualTo(
mContext.getString(R.string.bluetooth_device_context_disconnect));
verify(mButtonsPref).setButton1Text(R.string.bluetooth_device_context_disconnect);
// Click the button and make sure that disconnnect (not connect) gets called.
mLeftButton.callOnClick();
@@ -150,24 +162,27 @@ public class BluetoothDetailsButtonsControllerTest extends BluetoothDetailsContr
public void startsOutBusy() {
when(mCachedDevice.isBusy()).thenReturn(true);
showScreen(mController);
assertThat(mLeftButton.getText()).isEqualTo(
mContext.getString(R.string.bluetooth_device_context_disconnect));
assertThat(mRightButton.getText()).isEqualTo(mContext.getString(R.string.forget));
assertThat(mLeftButton.isEnabled()).isFalse();
verify(mButtonsPref).setButton1Text(R.string.bluetooth_device_context_disconnect);
verify(mButtonsPref).setButton1Enabled(false);
verify(mButtonsPref).setButton2Text(R.string.forget);
// Now pretend the device became non-busy.
when(mCachedDevice.isBusy()).thenReturn(false);
mController.onDeviceAttributesChanged();
assertThat(mLeftButton.isEnabled()).isTrue();
verify(mButtonsPref).setButton1Enabled(true);
}
@Test
public void becomesBusy() {
showScreen(mController);
assertThat(mLeftButton.isEnabled()).isTrue();
verify(mButtonsPref).setButton1Enabled(true);
// Now pretend the device became busy.
when(mCachedDevice.isBusy()).thenReturn(true);
mController.onDeviceAttributesChanged();
assertThat(mLeftButton.isEnabled()).isFalse();
verify(mButtonsPref).setButton1Enabled(false);
}
}