1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.widget; 18 19 20 import android.text.InputType; 21 import android.text.TextWatcher; 22 import android.view.View; 23 import android.widget.EditText; 24 25 import com.android.settings.TestConfig; 26 import com.android.settings.testutils.SettingsRobolectricTestRunner; 27 28 import org.junit.Before; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 import org.mockito.Mock; 32 import org.mockito.MockitoAnnotations; 33 import org.robolectric.RuntimeEnvironment; 34 import org.robolectric.annotation.Config; 35 36 import static com.google.common.truth.Truth.assertThat; 37 import static org.mockito.Matchers.any; 38 import static org.mockito.Matchers.anyInt; 39 import static org.mockito.Mockito.never; 40 import static org.mockito.Mockito.spy; 41 import static org.mockito.Mockito.verify; 42 import static org.mockito.Mockito.when; 43 44 @RunWith(SettingsRobolectricTestRunner.class) 45 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 46 public class ValidatedEditTextPreferenceTest { 47 48 @Mock 49 private View mView; 50 @Mock 51 private ValidatedEditTextPreference.Validator mValidator; 52 53 private ValidatedEditTextPreference mPreference; 54 55 @Before setUp()56 public void setUp() { 57 MockitoAnnotations.initMocks(this); 58 mPreference = new ValidatedEditTextPreference(RuntimeEnvironment.application); 59 } 60 61 @Test bindDialogView_nullEditText_shouldNotCrash()62 public void bindDialogView_nullEditText_shouldNotCrash() { 63 when(mView.findViewById(android.R.id.edit)).thenReturn(null); 64 // should not crash trying to get the EditText text 65 mPreference.onBindDialogView(mView); 66 } 67 68 @Test bindDialogView_emptyEditText_shouldNotSetSelection()69 public void bindDialogView_emptyEditText_shouldNotSetSelection() { 70 final String testText = ""; 71 final EditText editText = spy(new EditText(RuntimeEnvironment.application)); 72 editText.setText(testText); 73 when(mView.findViewById(android.R.id.edit)).thenReturn(editText); 74 75 mPreference.onBindDialogView(mView); 76 77 // no need to setSelection if text was empty 78 verify(editText, never()).setSelection(anyInt()); 79 } 80 81 @Test bindDialogView_nonemptyEditText_shouldSetSelection()82 public void bindDialogView_nonemptyEditText_shouldSetSelection() { 83 final String testText = "whatever"; 84 final EditText editText = spy(new EditText(RuntimeEnvironment.application)); 85 editText.setText(testText); 86 when(mView.findViewById(android.R.id.edit)).thenReturn(editText); 87 88 mPreference.onBindDialogView(mView); 89 90 // selection should be set to end of string 91 verify(editText).setSelection(testText.length()); 92 } 93 94 @Test bindDialogView_hasValidator_shouldBindToEditText()95 public void bindDialogView_hasValidator_shouldBindToEditText() { 96 final EditText editText = spy(new EditText(RuntimeEnvironment.application)); 97 when(mView.findViewById(android.R.id.edit)).thenReturn(editText); 98 99 mPreference.setValidator(mValidator); 100 mPreference.onBindDialogView(mView); 101 102 verify(editText).addTextChangedListener(any(TextWatcher.class)); 103 } 104 105 @Test bindDialogView_isPassword_shouldSetInputType()106 public void bindDialogView_isPassword_shouldSetInputType() { 107 final EditText editText = spy(new EditText(RuntimeEnvironment.application)); 108 when(mView.findViewById(android.R.id.edit)).thenReturn(editText); 109 110 mPreference.setValidator(mValidator); 111 mPreference.setIsPassword(true); 112 mPreference.onBindDialogView(mView); 113 114 assertThat(editText.getInputType() 115 & (InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT)) 116 .isNotEqualTo(0); 117 } 118 } 119