Merge "Don't disable "Done" button when it cannot be pressed" into main
This commit is contained in:
committed by
Android (Google) Code Review
commit
7aabe039f4
@@ -17,7 +17,9 @@
|
|||||||
package com.android.settings.notification.modes;
|
package com.android.settings.notification.modes;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
@@ -32,6 +34,7 @@ class ZenModeEditDonePreferenceController extends AbstractZenModePreferenceContr
|
|||||||
|
|
||||||
private final Runnable mConfirmSave;
|
private final Runnable mConfirmSave;
|
||||||
@Nullable private Button mButton;
|
@Nullable private Button mButton;
|
||||||
|
private boolean mHasValidName;
|
||||||
|
|
||||||
ZenModeEditDonePreferenceController(@NonNull Context context, @NonNull String key,
|
ZenModeEditDonePreferenceController(@NonNull Context context, @NonNull String key,
|
||||||
Runnable confirmSave) {
|
Runnable confirmSave) {
|
||||||
@@ -46,15 +49,22 @@ class ZenModeEditDonePreferenceController extends AbstractZenModePreferenceContr
|
|||||||
if (pref != null) {
|
if (pref != null) {
|
||||||
mButton = pref.findViewById(R.id.done);
|
mButton = pref.findViewById(R.id.done);
|
||||||
if (mButton != null) {
|
if (mButton != null) {
|
||||||
mButton.setOnClickListener(v -> mConfirmSave.run());
|
mButton.setOnClickListener(this::onButtonClick);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void onButtonClick(View view) {
|
||||||
|
if (mHasValidName) {
|
||||||
|
mConfirmSave.run();
|
||||||
|
} else {
|
||||||
|
Toast.makeText(mContext, R.string.zen_mode_edit_name_empty_error, Toast.LENGTH_SHORT)
|
||||||
|
.show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
void updateState(Preference preference, @NonNull ZenMode zenMode) {
|
void updateState(Preference preference, @NonNull ZenMode zenMode) {
|
||||||
if (mButton != null) {
|
mHasValidName = !zenMode.getName().isBlank();
|
||||||
mButton.setEnabled(!zenMode.getName().isBlank());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ import org.mockito.Mock;
|
|||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.shadows.ShadowToast;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
@EnableFlags(Flags.FLAG_MODES_UI)
|
@EnableFlags(Flags.FLAG_MODES_UI)
|
||||||
@@ -74,29 +75,23 @@ public class ZenModeEditDonePreferenceControllerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void updateState_nameNonEmpty_buttonEnabled() {
|
public void buttonClick_nameNonEmpty_buttonSaves() {
|
||||||
ZenMode mode = new TestModeBuilder().setName("Such a nice name").build();
|
ZenMode mode = new TestModeBuilder().setName("Such a nice name").build();
|
||||||
|
|
||||||
mController.updateState(mPreference, mode);
|
mController.updateState(mPreference, mode);
|
||||||
|
|
||||||
assertThat(mButton.isEnabled()).isTrue();
|
|
||||||
verifyNoMoreInteractions(mConfirmSave);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void updateState_nameEmpty_buttonDisabled() {
|
|
||||||
ZenMode aModeHasNoName = new TestModeBuilder().setName("").build();
|
|
||||||
|
|
||||||
mController.updateState(mPreference, aModeHasNoName);
|
|
||||||
|
|
||||||
assertThat(mButton.isEnabled()).isFalse();
|
|
||||||
verifyNoMoreInteractions(mConfirmSave);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void onButtonClick_callsConfirmSave() {
|
|
||||||
mButton.performClick();
|
mButton.performClick();
|
||||||
|
|
||||||
verify(mConfirmSave).run();
|
verify(mConfirmSave).run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void buttonClick_nameEmpty_buttonErrors() {
|
||||||
|
ZenMode aModeHasNoName = new TestModeBuilder().setName("").build();
|
||||||
|
|
||||||
|
mController.updateState(mPreference, aModeHasNoName);
|
||||||
|
mButton.performClick();
|
||||||
|
|
||||||
|
verifyNoMoreInteractions(mConfirmSave);
|
||||||
|
assertThat(ShadowToast.getTextOfLatestToast()).isEqualTo("Mode name cannot be empty");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user