Merge "Fix focus behavior of ValidatedEditTextPreference" into oc-mr1-dev

am: b8ff97fbd5

Change-Id: I559aa49ff0ef4aa82a6a1fb989d05c81ac7575fe
This commit is contained in:
Antony Sargent
2017-09-11 20:09:25 +00:00
committed by android-build-merger
2 changed files with 43 additions and 13 deletions

View File

@@ -21,8 +21,10 @@ import android.content.Context;
import android.support.annotation.VisibleForTesting; import android.support.annotation.VisibleForTesting;
import android.text.Editable; import android.text.Editable;
import android.text.InputType; import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.EditText; import android.widget.EditText;
@@ -61,17 +63,18 @@ public class ValidatedEditTextPreference extends CustomEditTextPreference {
@Override @Override
protected void onBindDialogView(View view) { protected void onBindDialogView(View view) {
super.onBindDialogView(view); super.onBindDialogView(view);
if (mValidator != null) { final EditText editText = view.findViewById(android.R.id.edit);
final EditText editText = view.findViewById(android.R.id.edit); if (editText != null && !TextUtils.isEmpty(editText.getText())) {
if (editText != null) { editText.setSelection(editText.getText().length());
editText.removeTextChangedListener(mTextWatcher); }
if (mIsPassword) { if (mValidator != null && editText != null) {
editText.setInputType( editText.removeTextChangedListener(mTextWatcher);
InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); if (mIsPassword) {
editText.setMaxLines(1); editText.setInputType(
} InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
editText.addTextChangedListener(mTextWatcher); editText.setMaxLines(1);
} }
editText.addTextChangedListener(mTextWatcher);
} }
} }

View File

@@ -35,9 +35,10 @@ import org.robolectric.annotation.Config;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@@ -58,10 +59,36 @@ public class ValidatedEditTextPreferenceTest {
} }
@Test @Test
public void bindDialogView_noTextWatcher_shouldDoNothing() { public void bindDialogView_nullEditText_shouldNotCrash() {
when(mView.findViewById(android.R.id.edit)).thenReturn(null);
// should not crash trying to get the EditText text
mPreference.onBindDialogView(mView);
}
@Test
public void bindDialogView_emptyEditText_shouldNotSetSelection() {
final String testText = "";
final EditText editText = spy(new EditText(RuntimeEnvironment.application));
editText.setText(testText);
when(mView.findViewById(android.R.id.edit)).thenReturn(editText);
mPreference.onBindDialogView(mView); mPreference.onBindDialogView(mView);
verifyZeroInteractions(mView); // no need to setSelection if text was empty
verify(editText, never()).setSelection(anyInt());
}
@Test
public void bindDialogView_nonemptyEditText_shouldSetSelection() {
final String testText = "whatever";
final EditText editText = spy(new EditText(RuntimeEnvironment.application));
editText.setText(testText);
when(mView.findViewById(android.R.id.edit)).thenReturn(editText);
mPreference.onBindDialogView(mView);
// selection should be set to end of string
verify(editText).setSelection(testText.length());
} }
@Test @Test