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 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Matchers.any; 22 import static org.mockito.Matchers.anyInt; 23 import static org.mockito.Mockito.never; 24 import static org.mockito.Mockito.spy; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import android.support.v7.preference.PreferenceViewHolder; 29 import android.text.InputType; 30 import android.text.TextWatcher; 31 import android.view.View; 32 import android.widget.EditText; 33 import android.widget.TextView; 34 35 import com.android.settings.testutils.SettingsRobolectricTestRunner; 36 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.mockito.Mock; 41 import org.mockito.MockitoAnnotations; 42 import org.robolectric.RuntimeEnvironment; 43 import org.robolectric.util.ReflectionHelpers; 44 45 @RunWith(SettingsRobolectricTestRunner.class) 46 public class ValidatedEditTextPreferenceTest { 47 48 @Mock 49 private View mView; 50 @Mock 51 private ValidatedEditTextPreference.Validator mValidator; 52 53 private PreferenceViewHolder mViewHolder; 54 private ValidatedEditTextPreference mPreference; 55 56 @Before setUp()57 public void setUp() { 58 MockitoAnnotations.initMocks(this); 59 60 mViewHolder = spy(PreferenceViewHolder.createInstanceForTests( 61 new View(RuntimeEnvironment.application))); 62 mPreference = new ValidatedEditTextPreference(RuntimeEnvironment.application); 63 } 64 65 @Test bindDialogView_nullEditText_shouldNotCrash()66 public void bindDialogView_nullEditText_shouldNotCrash() { 67 when(mView.findViewById(android.R.id.edit)).thenReturn(null); 68 // should not crash trying to get the EditText text 69 mPreference.onBindDialogView(mView); 70 } 71 72 @Test bindDialogView_emptyEditText_shouldNotSetSelection()73 public void bindDialogView_emptyEditText_shouldNotSetSelection() { 74 final String testText = ""; 75 final EditText editText = spy(new EditText(RuntimeEnvironment.application)); 76 editText.setText(testText); 77 when(mView.findViewById(android.R.id.edit)).thenReturn(editText); 78 79 mPreference.onBindDialogView(mView); 80 81 // no need to setSelection if text was empty 82 verify(editText, never()).setSelection(anyInt()); 83 } 84 85 @Test bindDialogView_nonemptyEditText_shouldSetSelection()86 public void bindDialogView_nonemptyEditText_shouldSetSelection() { 87 final String testText = "whatever"; 88 final EditText editText = spy(new EditText(RuntimeEnvironment.application)); 89 editText.setText(testText); 90 when(mView.findViewById(android.R.id.edit)).thenReturn(editText); 91 92 mPreference.onBindDialogView(mView); 93 94 // selection should be set to end of string 95 verify(editText).setSelection(testText.length()); 96 } 97 98 @Test bindDialogView_hasValidator_shouldBindToEditText()99 public void bindDialogView_hasValidator_shouldBindToEditText() { 100 final EditText editText = spy(new EditText(RuntimeEnvironment.application)); 101 when(mView.findViewById(android.R.id.edit)).thenReturn(editText); 102 103 mPreference.setValidator(mValidator); 104 mPreference.onBindDialogView(mView); 105 106 verify(editText).addTextChangedListener(any(TextWatcher.class)); 107 } 108 109 @Test bindDialogView_isPassword_shouldSetInputType()110 public void bindDialogView_isPassword_shouldSetInputType() { 111 final EditText editText = spy(new EditText(RuntimeEnvironment.application)); 112 when(mView.findViewById(android.R.id.edit)).thenReturn(editText); 113 114 mPreference.setValidator(mValidator); 115 mPreference.setIsPassword(true); 116 mPreference.onBindDialogView(mView); 117 118 assertThat(editText.getInputType() 119 & (InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD | InputType.TYPE_CLASS_TEXT)) 120 .isNotEqualTo(0); 121 } 122 123 @Test bindViewHolder_isPassword_shouldSetInputType()124 public void bindViewHolder_isPassword_shouldSetInputType() { 125 final TextView textView = spy(new TextView(RuntimeEnvironment.application)); 126 when(mViewHolder.findViewById(android.R.id.summary)).thenReturn(textView); 127 128 mPreference.setIsSummaryPassword(true); 129 mPreference.onBindViewHolder(mViewHolder); 130 131 assertThat(textView.getInputType() 132 & (InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT)) 133 .isNotEqualTo(0); 134 } 135 } 136