import android.text.InputFilter;
import android.text.InputFilter.LengthFilter;
import android.text.InputType;
+import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
+import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
}
/**
+ * Helper method to return the text of the pin entry field - this exists primarily to help us
+ * simulate having existing text when the dialog is recreated, for example after a screen
+ * rotation.
+ */
+ @VisibleForTesting
+ CharSequence getPairingViewText() {
+ if (mPairingView != null) {
+ return mPairingView.getText();
+ }
+ return null;
+ }
+
+ /**
* Returns a dialog with UI elements that allow a user to provide input.
*/
private AlertDialog createUserEntryDialog() {
mBuilder.setNegativeButton(getString(android.R.string.cancel), this);
AlertDialog dialog = mBuilder.create();
dialog.setOnShowListener(d -> {
- mDialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);
+ if (TextUtils.isEmpty(getPairingViewText())) {
+ mDialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);
+ }
if (mPairingView != null && mPairingView.requestFocus()) {
InputMethodManager imm = (InputMethodManager)
getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
import static org.mockito.Mockito.when;
import android.app.AlertDialog;
+import android.app.Dialog;
import android.content.Context;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
+import org.robolectric.shadows.ShadowAlertDialog;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.util.FragmentTestUtil;
verify(dialogActivity, times(1)).dismiss();
}
+ @Test
+ public void rotateDialog_nullPinText_okButtonEnabled() {
+ userEntryDialogExistingTextTest(null);
+ }
+
+ @Test
+ public void rotateDialog_emptyPinText_okButtonEnabled() {
+ userEntryDialogExistingTextTest("");
+ }
+
+ @Test
+ public void rotateDialog_nonEmptyPinText_okButtonEnabled() {
+ userEntryDialogExistingTextTest("test");
+ }
+
+ // Runs a test simulating the user entry dialog type in a situation like device rotation, where
+ // the dialog fragment gets created and we already have some existing text entered into the
+ // pin field.
+ private void userEntryDialogExistingTextTest(CharSequence existingText) {
+ when(controller.getDialogType()).thenReturn(BluetoothPairingController.USER_ENTRY_DIALOG);
+ when(controller.getDeviceVariantMessageHintId())
+ .thenReturn(BluetoothPairingController.INVALID_DIALOG_TYPE);
+ when(controller.getDeviceVariantMessageId())
+ .thenReturn(BluetoothPairingController.INVALID_DIALOG_TYPE);
+
+ BluetoothPairingDialogFragment fragment = spy(new BluetoothPairingDialogFragment());
+ when(fragment.getPairingViewText()).thenReturn(existingText);
+ setupFragment(fragment);
+ AlertDialog dialog = ShadowAlertDialog.getLatestAlertDialog();
+ boolean expected = !TextUtils.isEmpty(existingText);
+ assertThat(dialog.getButton(Dialog.BUTTON_POSITIVE).isEnabled()).isEqualTo(expected);
+ }
+
private void setupFragment(BluetoothPairingDialogFragment frag) {
assertThat(frag.isPairingControllerSet()).isFalse();
frag.setPairingController(controller);