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.text.method.cts; 18 19 import com.android.cts.stub.R; 20 21 import dalvik.annotation.TestLevel; 22 import dalvik.annotation.TestTargetClass; 23 import dalvik.annotation.TestTargetNew; 24 import dalvik.annotation.ToBeFixed; 25 26 import android.app.Activity; 27 import android.app.Instrumentation; 28 import android.test.ActivityInstrumentationTestCase2; 29 import android.text.Editable; 30 import android.text.Selection; 31 import android.text.Spannable; 32 import android.text.SpannableString; 33 import android.text.Spanned; 34 import android.text.method.NumberKeyListener; 35 import android.view.KeyEvent; 36 import android.view.View; 37 import android.widget.TextView; 38 import android.widget.TextView.BufferType; 39 40 @TestTargetClass(NumberKeyListener.class) 41 public class NumberKeyListenerTest extends 42 ActivityInstrumentationTestCase2<KeyListenerStubActivity> { 43 private MockNumberKeyListener mNumberKeyListener; 44 private Activity mActivity; 45 private Instrumentation mInstrumentation; 46 private TextView mTextView; 47 NumberKeyListenerTest()48 public NumberKeyListenerTest(){ 49 super("com.android.cts.stub", KeyListenerStubActivity.class); 50 } 51 52 @Override setUp()53 protected void setUp() throws Exception { 54 super.setUp(); 55 mActivity = getActivity(); 56 mInstrumentation = getInstrumentation(); 57 mTextView = (TextView) mActivity.findViewById(R.id.keylistener_textview); 58 } 59 60 /** 61 * Check point: 62 * 1. Filter "Android test", return "". 63 * 2. Filter "12345", return null. 64 * 3. Filter "", return null. 65 * 4. Filter "12345 Android", return "12345". 66 * 5. Filter Spanned("12345 Android"), return Spanned("12345") and copy spans. 67 */ 68 @TestTargetNew( 69 level = TestLevel.COMPLETE, 70 notes = "paramters dest, dstart, dend are never read in the function", 71 method = "filter", 72 args = {CharSequence.class, int.class, int.class, Spanned.class, int.class, int.class} 73 ) 74 @ToBeFixed(bug = "1695243", explanation = "Android API javadocs are incomplete, " + 75 "should add NPE description in javadoc.") testFilter()76 public void testFilter() { 77 mNumberKeyListener = new MockNumberKeyListener(); 78 String source = "Android test"; 79 SpannableString dest = new SpannableString("012345"); 80 assertEquals("", mNumberKeyListener.filter(source, 0, source.length(), 81 dest, 0, dest.length()).toString()); 82 83 source = "12345"; 84 dest = new SpannableString("012345"); 85 assertNull(mNumberKeyListener.filter(source, 0, source.length(), dest, 0, dest.length())); 86 87 source = ""; 88 dest = new SpannableString("012345"); 89 assertNull(mNumberKeyListener.filter(source, 0, source.length(), dest, 0, dest.length())); 90 91 source = "12345 Android"; 92 dest = new SpannableString("012345 Android-test"); 93 assertEquals("12345", mNumberKeyListener.filter(source, 0, source.length(), 94 dest, 0, dest.length()).toString()); 95 96 Object what = new Object(); 97 Spannable spannableSource = new SpannableString("12345 Android"); 98 spannableSource.setSpan(what, 0, spannableSource.length(), Spanned.SPAN_POINT_POINT); 99 Spanned filtered = (Spanned) mNumberKeyListener.filter(spannableSource, 100 0, spannableSource.length(), dest, 0, dest.length()); 101 assertEquals("12345", filtered.toString()); 102 assertEquals(Spanned.SPAN_POINT_POINT, filtered.getSpanFlags(what)); 103 assertEquals(0, filtered.getSpanStart(what)); 104 assertEquals("12345".length(), filtered.getSpanEnd(what)); 105 106 try { 107 mNumberKeyListener.filter(null, 0, 1, dest, 0, dest.length()); 108 fail("should throw NullPointerException."); 109 } catch (NullPointerException e) { 110 } 111 } 112 113 /** 114 * Check point: 115 * If one of the chars in the getAcceptedChars() can be generated by the keyCode of this 116 * key event, return the char; otherwise return '\0'. 117 */ 118 @TestTargetNew( 119 level = TestLevel.COMPLETE, 120 method = "lookup", 121 args = {android.view.KeyEvent.class, android.text.Spannable.class} 122 ) 123 @ToBeFixed(bug = "1695243", explanation = "Android API javadocs are incomplete.") testLookup()124 public void testLookup() { 125 mNumberKeyListener = new MockNumberKeyListener(); 126 127 KeyEvent event1 = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0); 128 SpannableString str = new SpannableString("012345"); 129 assertEquals('0', mNumberKeyListener.lookup(event1, str)); 130 131 KeyEvent event2 = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_A); 132 str = new SpannableString("ABCD"); 133 assertEquals('\0', mNumberKeyListener.lookup(event2, str)); 134 135 try { 136 mNumberKeyListener.lookup(null, str); 137 fail("should throw NullPointerException."); 138 } catch (NullPointerException e) { 139 // expected. 140 } 141 } 142 143 @TestTargetNew( 144 level = TestLevel.COMPLETE, 145 method = "ok", 146 args = {char[].class, char.class} 147 ) 148 @ToBeFixed(bug = "1695243", explanation = "Android API javadocs are incomplete.") testOk()149 public void testOk() { 150 mNumberKeyListener = new MockNumberKeyListener(); 151 152 assertTrue(mNumberKeyListener.callOk(mNumberKeyListener.getAcceptedChars(), '3')); 153 assertFalse(mNumberKeyListener.callOk(mNumberKeyListener.getAcceptedChars(), 'e')); 154 155 try { 156 mNumberKeyListener.callOk(null, 'm'); 157 fail("should throw NullPointerException."); 158 } catch (NullPointerException e) { 159 } 160 } 161 162 /** 163 * Check point: 164 * 1. Press '0' key, '0' will be added to the text. 165 * 2. Press an unaccepted key if it exists, it will not be added. 166 * 3. remove NumberKeyListener and press '0' key, '0' will not be added. 167 */ 168 @TestTargetNew( 169 level = TestLevel.COMPLETE, 170 method = "onKeyDown", 171 args = {View.class, Editable.class, int.class, KeyEvent.class} 172 ) testPressKey()173 public void testPressKey() { 174 final CharSequence text = "123456"; 175 final MockNumberKeyListener numberKeyListener = new MockNumberKeyListener(); 176 177 mActivity.runOnUiThread(new Runnable() { 178 public void run() { 179 mTextView.setText(text, BufferType.EDITABLE); 180 mTextView.setKeyListener(numberKeyListener); 181 mTextView.requestFocus(); 182 Selection.setSelection((Editable) mTextView.getText(), 0, 0); 183 } 184 }); 185 mInstrumentation.waitForIdleSync(); 186 assertEquals("123456", mTextView.getText().toString()); 187 // press '0' key. 188 sendKeys(KeyEvent.KEYCODE_0); 189 assertEquals("0123456", mTextView.getText().toString()); 190 191 // an unaccepted key if it exists. 192 int keyCode = TextMethodUtils.getUnacceptedKeyCode(MockNumberKeyListener.CHARACTERS); 193 if (-1 != keyCode) { 194 sendKeys(keyCode); 195 // text of TextView will not be changed. 196 assertEquals("0123456", mTextView.getText().toString()); 197 } 198 199 mActivity.runOnUiThread(new Runnable() { 200 public void run() { 201 mTextView.setKeyListener(null); 202 mTextView.requestFocus(); 203 } 204 }); 205 mInstrumentation.waitForIdleSync(); 206 // press '0' key. 207 sendKeys(KeyEvent.KEYCODE_0); 208 assertEquals("0123456", mTextView.getText().toString()); 209 } 210 211 private static class MockNumberKeyListener extends NumberKeyListener { 212 static final char[] CHARACTERS = new char[] {'0', '1', '2', 213 '3', '4', '5', '6', '7', '8', '9'}; 214 215 @Override getAcceptedChars()216 protected char[] getAcceptedChars() { 217 return CHARACTERS; 218 } 219 220 @Override lookup(KeyEvent event, Spannable content)221 protected int lookup(KeyEvent event, Spannable content) { 222 return super.lookup(event, content); 223 } 224 callOk(char[] accept, char c)225 public boolean callOk(char[] accept, char c) { 226 return NumberKeyListener.ok(accept, c); 227 } 228 getInputType()229 public int getInputType() { 230 return 0; 231 } 232 } 233 } 234