1 /*
2  * Copyright (C) 2008 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 android.widget.cts;
18 
19 import com.android.cts.widget.R;
20 
21 
22 import org.xmlpull.v1.XmlPullParser;
23 
24 import android.app.Activity;
25 import android.app.Instrumentation;
26 import android.content.Context;
27 import android.cts.util.PollingCheck;
28 import android.test.ActivityInstrumentationTestCase2;
29 import android.test.UiThreadTest;
30 import android.text.Editable;
31 import android.text.Spannable;
32 import android.text.TextWatcher;
33 import android.util.AttributeSet;
34 import android.util.Log;
35 import android.util.Xml;
36 import android.view.KeyCharacterMap;
37 import android.view.KeyEvent;
38 import android.widget.DialerFilter;
39 import android.widget.EditText;
40 import android.widget.RelativeLayout;
41 
42 public class DialerFilterTest extends ActivityInstrumentationTestCase2<DialerFilterCtsActivity> {
43     private Activity mActivity;
44     private Instrumentation mInstrumentation;
45     private DialerFilter mDialerFilter;
46 
DialerFilterTest()47     public DialerFilterTest() {
48         super("com.android.cts.widget", DialerFilterCtsActivity.class);
49     }
50 
51     @Override
setUp()52     protected void setUp() throws Exception {
53         super.setUp();
54 
55         mActivity = getActivity();
56         new PollingCheck() {
57             @Override
58                 protected boolean check() {
59                 return mActivity.hasWindowFocus();
60             }
61         }.run();
62         mInstrumentation = getInstrumentation();
63 
64         mDialerFilter = (DialerFilter) mActivity.findViewById(R.id.dialer_filter);
65     }
66 
67     @UiThreadTest
testConstructor()68     public void testConstructor() {
69         final XmlPullParser parser = mActivity.getResources().getXml(R.layout.dialerfilter_layout);
70         final AttributeSet attrs = Xml.asAttributeSet(parser);
71 
72         new DialerFilter(mActivity);
73         new DialerFilter(mActivity, attrs);
74     }
75 
76     @UiThreadTest
testIsQwertyKeyboard()77     public void testIsQwertyKeyboard() {
78         // Simply call the method. Return value may depend on the default keyboard.
79         mDialerFilter.isQwertyKeyboard();
80     }
81 
testOnKeyUpDown()82     public void testOnKeyUpDown() {
83         // The exact behavior depends on the implementation of DialerKeyListener and
84         // TextKeyListener, but even that may be changed. Simply assert basic scenarios.
85 
86         mActivity.runOnUiThread(new Runnable() {
87             public void run() {
88                 mDialerFilter.setMode(DialerFilter.DIGITS_ONLY);
89                 mDialerFilter.requestFocus();
90             }
91         });
92         mInstrumentation.waitForIdleSync();
93 
94         assertTrue(mDialerFilter.hasFocus());
95 
96         mInstrumentation.sendStringSync("123");
97         assertEquals("", mDialerFilter.getLetters().toString());
98         assertEquals("123", mDialerFilter.getDigits().toString());
99 
100         mActivity.runOnUiThread(new Runnable() {
101             public void run() {
102                 mDialerFilter.clearText();
103                 mDialerFilter.setMode(DialerFilter.LETTERS_ONLY);
104             }
105         });
106         mInstrumentation.waitForIdleSync();
107 
108         // 12-key support
109         KeyCharacterMap keymap
110                 = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
111         if (keymap.getKeyboardType() == KeyCharacterMap.NUMERIC) {
112             // "adg" in case of 12-key(NUMERIC) keyboard
113             mInstrumentation.sendStringSync("234");
114         }
115         else {
116             mInstrumentation.sendStringSync("adg");
117         }
118         assertEquals("ADG", mDialerFilter.getLetters().toString());
119         assertEquals("", mDialerFilter.getDigits().toString());
120 
121         mActivity.runOnUiThread(new Runnable() {
122             public void run() {
123                 mDialerFilter.clearText();
124                 mDialerFilter.setMode(DialerFilter.DIGITS_AND_LETTERS);
125             }
126         });
127         mInstrumentation.waitForIdleSync();
128 
129         // 12-key support
130         if (keymap.getKeyboardType() == KeyCharacterMap.NUMERIC) {
131             // "adg" in case of 12-key(NUMERIC) keyboard
132             mInstrumentation.sendStringSync("234");
133         }
134         else {
135             mInstrumentation.sendStringSync("adg");
136         }
137         assertEquals("ADG", mDialerFilter.getLetters().toString());
138         // A, D, K may map to numbers on some keyboards. Don't test.
139 
140         mActivity.runOnUiThread(new Runnable() {
141             public void run() {
142                 mDialerFilter.clearText();
143                 mDialerFilter.setMode(DialerFilter.DIGITS_AND_LETTERS);
144             }
145         });
146         mInstrumentation.waitForIdleSync();
147 
148         mInstrumentation.sendStringSync("123");
149         // 1, 2, 3 may map to letters on some keyboards. Don't test.
150         assertEquals("123", mDialerFilter.getDigits().toString());
151     }
152 
153     @UiThreadTest
testAccessMode()154     public void testAccessMode() {
155         mDialerFilter.setMode(DialerFilter.DIGITS_AND_LETTERS_NO_LETTERS);
156         assertEquals(DialerFilter.DIGITS_AND_LETTERS_NO_LETTERS, mDialerFilter.getMode());
157 
158         mDialerFilter.setMode(DialerFilter.DIGITS_AND_LETTERS);
159         assertEquals(DialerFilter.DIGITS_AND_LETTERS, mDialerFilter.getMode());
160 
161         mDialerFilter.setMode(-1);
162         assertEquals(-1, mDialerFilter.getMode());
163     }
164 
165     @UiThreadTest
testGetLetters()166     public void testGetLetters() {
167         assertEquals("", mDialerFilter.getLetters().toString());
168 
169         mDialerFilter.setMode(DialerFilter.DIGITS_AND_LETTERS);
170         mDialerFilter.append("ANDROID");
171         assertEquals("ANDROID", mDialerFilter.getLetters().toString());
172     }
173 
174     @UiThreadTest
testGetDigits()175     public void testGetDigits() {
176         assertEquals("", mDialerFilter.getDigits().toString());
177 
178         mDialerFilter.setMode(DialerFilter.DIGITS_AND_LETTERS);
179         mDialerFilter.append("12345");
180         assertEquals("12345", mDialerFilter.getDigits().toString());
181     }
182 
183     @UiThreadTest
testGetFilterText()184     public void testGetFilterText() {
185         assertEquals("", mDialerFilter.getFilterText().toString());
186 
187         mDialerFilter.setMode(DialerFilter.DIGITS_AND_LETTERS);
188         mDialerFilter.append("CTS12345");
189         assertEquals("CTS12345", mDialerFilter.getFilterText().toString());
190 
191         mDialerFilter.setMode(DialerFilter.DIGITS_ONLY);
192         assertEquals("12345", mDialerFilter.getFilterText().toString());
193 
194         mDialerFilter.setMode(DialerFilter.LETTERS_ONLY);
195         assertEquals("CTS12345", mDialerFilter.getFilterText().toString());
196     }
197 
198     @UiThreadTest
testAppend()199     public void testAppend() {
200         mDialerFilter.setMode(DialerFilter.LETTERS_ONLY);
201         mDialerFilter.append("ANDROID");
202         assertEquals("ANDROID", mDialerFilter.getLetters().toString());
203         assertEquals("", mDialerFilter.getDigits().toString());
204         assertEquals("ANDROID", mDialerFilter.getFilterText().toString());
205 
206         mDialerFilter.setMode(DialerFilter.DIGITS_ONLY);
207         mDialerFilter.append("123");
208         assertEquals("", mDialerFilter.getLetters().toString());
209         assertEquals("123", mDialerFilter.getDigits().toString());
210         assertEquals("123", mDialerFilter.getFilterText().toString());
211 
212         mDialerFilter.clearText();
213         mDialerFilter.setMode(DialerFilter.DIGITS_AND_LETTERS);
214         mDialerFilter.append("ABC123DEF456GHI789");
215         assertEquals("ABC123DEF456GHI789", mDialerFilter.getLetters().toString());
216         assertEquals("123456789", mDialerFilter.getDigits().toString());
217         assertEquals("ABC123DEF456GHI789", mDialerFilter.getFilterText().toString());
218 
219         mDialerFilter.clearText();
220         mDialerFilter.setMode(DialerFilter.DIGITS_AND_LETTERS_NO_DIGITS);
221         mDialerFilter.append("ABC123DEF456GHI789");
222         assertEquals("ABC123DEF456GHI789", mDialerFilter.getLetters().toString());
223         assertEquals("", mDialerFilter.getDigits().toString());
224         assertEquals("ABC123DEF456GHI789", mDialerFilter.getFilterText().toString());
225 
226         mDialerFilter.clearText();
227         mDialerFilter.setMode(DialerFilter.DIGITS_AND_LETTERS_NO_LETTERS);
228         mDialerFilter.append("ABC123DEF456GHI789");
229         assertEquals("", mDialerFilter.getLetters().toString());
230         assertEquals("123456789", mDialerFilter.getDigits().toString());
231         assertEquals("", mDialerFilter.getFilterText().toString());
232 
233         mDialerFilter.clearText();
234         mDialerFilter.setMode(DialerFilter.DIGITS_AND_LETTERS);
235         mDialerFilter.append("");
236         assertEquals("", mDialerFilter.getLetters().toString());
237         assertEquals("", mDialerFilter.getDigits().toString());
238         assertEquals("", mDialerFilter.getFilterText().toString());
239 
240         try {
241             mDialerFilter.append(null);
242             fail("A NullPointerException should be thrown out.");
243         } catch (final NullPointerException e) {
244             // expected, test success.
245         }
246     }
247 
248     @UiThreadTest
testClearText()249     public void testClearText() {
250         assertEquals("", mDialerFilter.getLetters().toString());
251         assertEquals("", mDialerFilter.getDigits().toString());
252 
253         mDialerFilter.setMode(DialerFilter.DIGITS_AND_LETTERS);
254         mDialerFilter.append("CTS12345");
255         assertEquals("CTS12345", mDialerFilter.getLetters().toString());
256         assertEquals("12345", mDialerFilter.getDigits().toString());
257 
258         mDialerFilter.clearText();
259         assertEquals("", mDialerFilter.getLetters().toString());
260         assertEquals("", mDialerFilter.getDigits().toString());
261         assertEquals(DialerFilter.DIGITS_AND_LETTERS, mDialerFilter.getMode());
262     }
263 
264     @UiThreadTest
testSetLettersWatcher()265     public void testSetLettersWatcher() {
266         MockTextWatcher tw = new MockTextWatcher("A");
267 
268         Spannable span = (Spannable) mDialerFilter.getLetters();
269         assertEquals(-1, span.getSpanStart(tw));
270         assertEquals(-1, span.getSpanEnd(tw));
271 
272         mDialerFilter.setMode(DialerFilter.LETTERS_ONLY);
273         mDialerFilter.setLettersWatcher(tw);
274         mDialerFilter.append("ANDROID");
275         assertEquals("ANDROID", tw.getText());
276 
277         span = (Spannable) mDialerFilter.getLetters();
278         assertEquals(0, span.getSpanStart(tw));
279         assertEquals(mDialerFilter.getLetters().length(), span.getSpanEnd(tw));
280         assertEquals("ANDROID", span.toString());
281 
282         tw = new MockTextWatcher("");
283         mDialerFilter.setLettersWatcher(tw);
284         mDialerFilter.append("");
285         assertEquals("", tw.getText());
286 
287         try {
288             mDialerFilter.setLettersWatcher(new MockTextWatcher(null));
289             mDialerFilter.append(null);
290             fail("A NullPointerException should be thrown out.");
291         } catch (final NullPointerException e) {
292             // expected, test success.
293         }
294     }
295 
296     @UiThreadTest
testSetDigitsWatcher()297     public void testSetDigitsWatcher() {
298         final MockTextWatcher tw = new MockTextWatcher("9");
299 
300         Spannable span = (Spannable) mDialerFilter.getDigits();
301         assertEquals(-1, span.getSpanStart(tw));
302         assertEquals(-1, span.getSpanEnd(tw));
303 
304         mDialerFilter.setDigitsWatcher(tw);
305         assertEquals(0, span.getSpanStart(tw));
306         assertEquals(mDialerFilter.getDigits().length(), span.getSpanEnd(tw));
307 
308         mDialerFilter.setMode(DialerFilter.DIGITS_ONLY);
309         mDialerFilter.append("12345");
310         assertEquals("12345", tw.getText());
311     }
312 
313     @UiThreadTest
testSetFilterWatcher()314     public void testSetFilterWatcher() {
315         final MockTextWatcher tw = new MockTextWatcher("A");
316 
317         Spannable span = (Spannable) mDialerFilter.getLetters();
318         assertEquals(-1, span.getSpanStart(tw));
319         assertEquals(-1, span.getSpanEnd(tw));
320 
321         mDialerFilter.setMode(DialerFilter.LETTERS_ONLY);
322         mDialerFilter.setFilterWatcher(tw);
323         mDialerFilter.append("ANDROID");
324         assertEquals("ANDROID", tw.getText());
325         span = (Spannable) mDialerFilter.getLetters();
326 
327         assertEquals(0, span.getSpanStart(tw));
328         assertEquals(mDialerFilter.getLetters().length(), span.getSpanEnd(tw));
329 
330         mDialerFilter.setMode(DialerFilter.DIGITS_ONLY);
331         mDialerFilter.setFilterWatcher(tw);
332         mDialerFilter.append("12345");
333         assertEquals("12345", tw.getText());
334     }
335 
336     @UiThreadTest
testRemoveFilterWatcher()337     public void testRemoveFilterWatcher() {
338         final MockTextWatcher tw = new MockTextWatcher("A");
339 
340         Spannable span = (Spannable) mDialerFilter.getLetters();
341         assertEquals(-1, span.getSpanStart(tw));
342         assertEquals(-1, span.getSpanEnd(tw));
343 
344         mDialerFilter.setMode(DialerFilter.LETTERS_ONLY);
345         mDialerFilter.setFilterWatcher(tw);
346         mDialerFilter.append("ANDROID");
347         assertEquals("ANDROID", tw.getText());
348 
349         span = (Spannable) mDialerFilter.getLetters();
350         assertEquals(0, span.getSpanStart(tw));
351         assertEquals(mDialerFilter.getLetters().length(), span.getSpanEnd(tw));
352 
353         mDialerFilter.removeFilterWatcher(tw);
354         mDialerFilter.append("GOLF");
355         assertEquals("ANDROID", tw.getText());
356 
357         assertEquals(-1, span.getSpanStart(tw));
358         assertEquals(-1, span.getSpanEnd(tw));
359     }
360 
testOnFinishInflate()361     public void testOnFinishInflate() {
362         // onFinishInflate() is implementation details, do NOT test
363     }
364 
testOnFocusChanged()365     public void testOnFocusChanged() {
366         // onFocusChanged() is implementation details, do NOT test
367     }
368 
369     @UiThreadTest
testOnModechange()370     public void testOnModechange() {
371         final MockDialerFilter dialerFilter = createMyDialerFilter();
372         dialerFilter.onFinishInflate();
373 
374         assertEquals(0, dialerFilter.getOldMode());
375         assertEquals(MockDialerFilter.DIGITS_AND_LETTERS, dialerFilter.getNewMode());
376 
377         dialerFilter.setMode(MockDialerFilter.DIGITS_AND_LETTERS_NO_LETTERS);
378         assertEquals(MockDialerFilter.DIGITS_AND_LETTERS, dialerFilter.getOldMode());
379         assertEquals(MockDialerFilter.DIGITS_AND_LETTERS_NO_LETTERS, dialerFilter.getNewMode());
380 
381         dialerFilter.setMode(MockDialerFilter.DIGITS_AND_LETTERS_NO_DIGITS);
382         assertEquals(MockDialerFilter.DIGITS_AND_LETTERS_NO_LETTERS, dialerFilter.getOldMode());
383         assertEquals(MockDialerFilter.DIGITS_AND_LETTERS_NO_DIGITS, dialerFilter.getNewMode());
384     }
385 
createMyDialerFilter()386     private MockDialerFilter createMyDialerFilter() {
387         final MockDialerFilter dialerFilter = new MockDialerFilter(mActivity);
388 
389         final EditText text1 = new EditText(mActivity);
390         text1.setId(android.R.id.hint);
391         final EditText text2 = new EditText(mActivity);
392         text2.setId(android.R.id.primary);
393 
394         dialerFilter.addView(text1, new RelativeLayout.LayoutParams(
395                 RelativeLayout.LayoutParams.WRAP_CONTENT,
396                 RelativeLayout.LayoutParams.WRAP_CONTENT));
397         dialerFilter.addView(text2, new RelativeLayout.LayoutParams(
398                 RelativeLayout.LayoutParams.WRAP_CONTENT,
399                 RelativeLayout.LayoutParams.WRAP_CONTENT));
400 
401         return dialerFilter;
402     }
403 
404     private class MockTextWatcher implements TextWatcher {
405         private String mString;
406 
MockTextWatcher(final String s)407         public MockTextWatcher(final String s) {
408             mString = s;
409         }
410 
beforeTextChanged(final CharSequence s, final int start, final int count, final int after)411         public void beforeTextChanged(final CharSequence s, final int start, final int count,
412                 final int after) {
413             Log.d("DialerFilterTest", "MockTextWatcher beforeTextChanged");
414         }
415 
onTextChanged(final CharSequence s, final int start, final int before, final int count)416         public void onTextChanged(final CharSequence s, final int start, final int before,
417                 final int count) {
418             Log.d("DialerFilterTest", "MockTextWatcher onTextChanged");
419             mString = s.toString();
420         }
421 
afterTextChanged(final Editable s)422         public void afterTextChanged(final Editable s) {
423             Log.d("DialerFilterTest", "MockTextWatcher afterTextChanged");
424         }
425 
getText()426         public String getText() {
427             return mString;
428         }
429     }
430 
431     /**
432      * MockDialerFilter for test
433      */
434     private class MockDialerFilter extends DialerFilter {
435         private int mOldMode = 0;
436         private int mNewMode = 0;
437 
MockDialerFilter(Context context)438         public MockDialerFilter(Context context) {
439             super(context);
440         }
441 
442         @Override
onFinishInflate()443         protected void onFinishInflate() {
444             super.onFinishInflate();
445         }
446 
447         @Override
onModeChange(final int oldMode, final int newMode)448         protected void onModeChange(final int oldMode, final int newMode) {
449             super.onModeChange(oldMode, newMode);
450             mOldMode = oldMode;
451             mNewMode = newMode;
452         }
453 
getOldMode()454         public int getOldMode() {
455             return mOldMode;
456         }
457 
getNewMode()458         public int getNewMode() {
459             return mNewMode;
460         }
461     }
462 }
463