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