Fix popup dialog show wrong device name

- Current EditText value will set to preference text if we click
OK in EditTextPreferenceDialogFragment. We will set preference
text to default when click cancel in DeviceNameWarningDialog.

Change-Id: Iab9561953b58276e98ee68d9196fa18e0dc3d78c
Fixes: 115693838
Test: make RunSettingsRoboTests
This commit is contained in:
Raff Tsai
2018-09-14 23:17:51 +08:00
parent e5791f3c99
commit b20bfc949a
5 changed files with 42 additions and 19 deletions

View File

@@ -39,10 +39,10 @@ import com.android.settingslib.core.lifecycle.events.OnSaveInstanceState;
public class DeviceNamePreferenceController extends BasePreferenceController
implements ValidatedEditTextPreference.Validator,
Preference.OnPreferenceChangeListener,
LifecycleObserver,
OnSaveInstanceState,
OnCreate {
Preference.OnPreferenceChangeListener,
LifecycleObserver,
OnSaveInstanceState,
OnCreate {
private static final String PREF_KEY = "device_name";
public static final int DEVICE_NAME_SET_WARNING_ID = 1;
private static final String KEY_PENDING_DEVICE_NAME = "key_pending_device_name";
@@ -116,9 +116,11 @@ public class DeviceNamePreferenceController extends BasePreferenceController
return mWifiDeviceNameTextValidator.isTextValid(deviceName);
}
public void confirmDeviceName() {
if (mPendingDeviceName != null) {
public void updateDeviceName(boolean update) {
if (update && mPendingDeviceName != null) {
setDeviceName(mPendingDeviceName);
} else {
mPreference.setText(getSummary().toString());
}
}
@@ -153,7 +155,8 @@ public class DeviceNamePreferenceController extends BasePreferenceController
* For more information, see {@link com.android.settings.bluetooth.BluetoothNameDialogFragment}.
*/
private static final String getFilteredBluetoothString(final String deviceName) {
CharSequence filteredSequence = new BluetoothLengthDeviceNameFilter().filter(deviceName, 0, deviceName.length(),
CharSequence filteredSequence = new BluetoothLengthDeviceNameFilter().filter(deviceName, 0,
deviceName.length(),
new SpannedString(""),
0, 0);
// null -> use the original

View File

@@ -66,7 +66,9 @@ public class DeviceNameWarningDialog extends InstrumentedDialogFragment
public void onClick(DialogInterface dialog, int which) {
final MyDeviceInfoFragment host = (MyDeviceInfoFragment) getTargetFragment();
if (which == DialogInterface.BUTTON_POSITIVE) {
host.onSetDeviceNameConfirm();
host.onSetDeviceNameConfirm(true);
} else {
host.onSetDeviceNameConfirm(false);
}
}
}

View File

@@ -178,9 +178,9 @@ public class MyDeviceInfoFragment extends DashboardFragment
DeviceNameWarningDialog.show(this);
}
public void onSetDeviceNameConfirm() {
public void onSetDeviceNameConfirm(boolean confirm) {
final DeviceNamePreferenceController controller = use(DeviceNamePreferenceController.class);
controller.confirmDeviceName();
controller.updateDeviceName(confirm);
}
private static class SummaryProvider implements SummaryLoader.SummaryProvider {

View File

@@ -114,7 +114,7 @@ public class DeviceNamePreferenceControllerTest {
@Test
public void setDeviceName_preferenceUpdatedWhenDeviceNameUpdated() {
forceAcceptDeviceName();
acceptDeviceName(true);
mController.displayPreference(mScreen);
mController.onPreferenceChange(mPreference, TESTING_STRING);
@@ -123,7 +123,7 @@ public class DeviceNamePreferenceControllerTest {
@Test
public void setDeviceName_bluetoothNameUpdatedWhenDeviceNameUpdated() {
forceAcceptDeviceName();
acceptDeviceName(true);
mController.displayPreference(mScreen);
mController.onPreferenceChange(mPreference, TESTING_STRING);
@@ -132,7 +132,7 @@ public class DeviceNamePreferenceControllerTest {
@Test
public void setDeviceName_wifiTetherNameUpdatedWhenDeviceNameUpdated() {
forceAcceptDeviceName();
acceptDeviceName(true);
mController.displayPreference(mScreen);
mController.onPreferenceChange(mPreference, TESTING_STRING);
@@ -150,21 +150,39 @@ public class DeviceNamePreferenceControllerTest {
@Test
public void setDeviceName_ignoresIfCancelPressed() {
forceAcceptDeviceName();
acceptDeviceName(true);
mController.displayPreference(mScreen);
mController.onPreferenceChange(mPreference, TESTING_STRING);
assertThat(mBluetoothAdapter.getName()).isEqualTo(TESTING_STRING);
}
private void forceAcceptDeviceName() {
@Test
public void setDeviceName_okInDeviceNameWarningDialog_shouldChangePreferenceText() {
acceptDeviceName(true);
mController.displayPreference(mScreen);
mController.onPreferenceChange(mPreference, TESTING_STRING);
assertThat(mPreference.getSummary()).isEqualTo(TESTING_STRING);
}
@Test
public void setDeviceName_cancelInDeviceNameWarningDialog_shouldNotChangePreferenceText() {
acceptDeviceName(false);
mController.displayPreference(mScreen);
mController.onPreferenceChange(mPreference, TESTING_STRING);
assertThat(mPreference.getSummary()).isNotEqualTo(TESTING_STRING);
assertThat(mPreference.getText()).isEqualTo(mPreference.getSummary());
}
private void acceptDeviceName(boolean accept) {
mController.setHost(
new DeviceNamePreferenceController.DeviceNamePreferenceHost() {
@Override
public void showDeviceNameWarningDialog(String deviceName) {
mController.confirmDeviceName();
mController.updateDeviceName(accept);
}
});
}
}

View File

@@ -30,7 +30,7 @@ public class DeviceNameWarningDialogTest {
fragmentController.create().start().resume();
fragment.onClick(null, DialogInterface.BUTTON_POSITIVE);
verify(deviceInfoFragment).onSetDeviceNameConfirm();
verify(deviceInfoFragment).onSetDeviceNameConfirm(true);
}
@Test
@@ -43,6 +43,6 @@ public class DeviceNameWarningDialogTest {
fragmentController.create().start().resume();
fragment.onClick(null, DialogInterface.BUTTON_NEGATIVE);
verify(deviceInfoFragment, never()).onSetDeviceNameConfirm();
verify(deviceInfoFragment).onSetDeviceNameConfirm(false);
}
}