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 org.xmlpull.v1.XmlPullParser; 20 21 import android.content.Context; 22 import android.test.AndroidTestCase; 23 import android.test.suitebuilder.annotation.SmallTest; 24 import android.text.TextUtils; 25 import android.text.method.ArrowKeyMovementMethod; 26 import android.text.method.MovementMethod; 27 import android.util.AttributeSet; 28 import android.util.Xml; 29 import android.widget.EditText; 30 import android.widget.TextView.BufferType; 31 32 import android.widget.cts.R; 33 34 35 public class EditTextTest extends AndroidTestCase { 36 private Context mContext; 37 private AttributeSet mAttributeSet; 38 39 @Override 40 protected void setUp() throws Exception { 41 super.setUp(); 42 43 mContext = getContext(); 44 XmlPullParser parser = mContext.getResources().getXml(R.layout.edittext_layout); 45 mAttributeSet = Xml.asAttributeSet(parser); 46 } 47 48 public void testConstructor() { 49 new EditText(mContext); 50 51 new EditText(mContext, null); 52 53 new EditText(mContext, null, 0); 54 55 new EditText(mContext, mAttributeSet); 56 57 new EditText(mContext, mAttributeSet, 0); 58 59 try { 60 new EditText(null); 61 fail("should throw NullPointerException."); 62 } catch (NullPointerException e) { 63 } 64 65 try { 66 new EditText(null, null); 67 fail("should throw NullPointerException."); 68 } catch (NullPointerException e) { 69 } 70 71 try { 72 new EditText(null, null); 73 fail("should throw NullPointerException."); 74 } catch (NullPointerException e) { 75 } 76 } 77 78 public void testAccessText() { 79 EditText editText = new EditText(mContext, mAttributeSet); 80 81 editText.setText("android", BufferType.NORMAL); 82 assertEquals("android", editText.getText().toString()); 83 84 editText.setText("", BufferType.SPANNABLE); 85 assertEquals("", editText.getText().toString()); 86 87 editText.setText(null, BufferType.EDITABLE); 88 assertEquals("", editText.getText().toString()); 89 } 90 91 public void testSetSelectionIndex() { 92 EditText editText = new EditText(mContext, mAttributeSet); 93 94 String string = "android"; 95 editText.setText(string, BufferType.EDITABLE); 96 int position = 4; 97 editText.setSelection(position); 98 assertEquals(position, editText.getSelectionStart()); 99 assertEquals(position, editText.getSelectionEnd()); 100 101 position = 0; 102 editText.setSelection(position); 103 assertEquals(position, editText.getSelectionStart()); 104 assertEquals(position, editText.getSelectionEnd()); 105 106 try { 107 editText.setSelection(-1); 108 fail("An IndexOutOfBoundsException should be thrown out."); 109 } catch (IndexOutOfBoundsException e) { 110 //expected, test success. 111 } 112 113 try { 114 editText.setSelection(string.length() + 1); 115 fail("An IndexOutOfBoundsException should be thrown out."); 116 } catch (IndexOutOfBoundsException e) { 117 //expected, test success. 118 } 119 } 120 121 public void testSetSelectionStartstop() { 122 EditText editText = new EditText(mContext, mAttributeSet); 123 124 String string = "android"; 125 editText.setText(string, BufferType.EDITABLE); 126 int start = 1; 127 int end = 2; 128 editText.setSelection(start, end); 129 assertEquals(start, editText.getSelectionStart()); 130 assertEquals(end, editText.getSelectionEnd()); 131 132 start = 0; 133 end = 0; 134 editText.setSelection(start, end); 135 assertEquals(start, editText.getSelectionStart()); 136 assertEquals(end, editText.getSelectionEnd()); 137 138 start = 7; 139 end = 1; 140 editText.setSelection(start, end); 141 assertEquals(start, editText.getSelectionStart()); 142 assertEquals(end, editText.getSelectionEnd()); 143 144 try { 145 editText.setSelection(-5, -1); 146 fail("An IndexOutOfBoundsException should be thrown out."); 147 } catch (IndexOutOfBoundsException e) { 148 //expected, test success. 149 } 150 151 try { 152 editText.setSelection(5, string.length() + 1); 153 fail("An IndexOutOfBoundsException should be thrown out."); 154 } catch (IndexOutOfBoundsException e) { 155 //expected, test success. 156 } 157 } 158 159 public void testSelectAll() { 160 EditText editText = new EditText(mContext, mAttributeSet); 161 162 String string = "android"; 163 editText.setText(string, BufferType.EDITABLE); 164 editText.selectAll(); 165 assertEquals(0, editText.getSelectionStart()); 166 assertEquals(string.length(), editText.getSelectionEnd()); 167 168 editText.setText("", BufferType.EDITABLE); 169 editText.selectAll(); 170 assertEquals(0, editText.getSelectionStart()); 171 assertEquals(0, editText.getSelectionEnd()); 172 173 editText.setText(null, BufferType.EDITABLE); 174 editText.selectAll(); 175 assertEquals(0, editText.getSelectionStart()); 176 assertEquals(0, editText.getSelectionEnd()); 177 } 178 179 public void testExtendSelection() { 180 EditText editText = new EditText(mContext, mAttributeSet); 181 182 editText.setText("android", BufferType.EDITABLE); 183 int start = 0; 184 int end = 0; 185 editText.setSelection(start, end); 186 assertEquals(start, editText.getSelectionStart()); 187 assertEquals(end, editText.getSelectionEnd()); 188 189 end = 6; 190 editText.extendSelection(end); 191 assertEquals(start, editText.getSelectionStart()); 192 assertEquals(end, editText.getSelectionEnd()); 193 194 start = 0; 195 end = 0; 196 editText.setSelection(start); 197 editText.extendSelection(end); 198 assertEquals(start, editText.getSelectionStart()); 199 assertEquals(end, editText.getSelectionEnd()); 200 201 try { 202 editText.setSelection(0, 4); 203 editText.extendSelection(10); 204 fail("An IndexOutOfBoundsException should be thrown out."); 205 } catch (IndexOutOfBoundsException e) { 206 //expected, test success. 207 } 208 } 209 210 public void testGetDefaultEditable() { 211 MockEditText mockEditText = new MockEditText(mContext, mAttributeSet); 212 213 assertTrue(mockEditText.getDefaultEditable()); 214 } 215 216 public void testGetDefaultMovementMethod() { 217 MockEditText mockEditText = new MockEditText(mContext, mAttributeSet); 218 MovementMethod method1 = mockEditText.getDefaultMovementMethod(); 219 MovementMethod method2 = mockEditText.getDefaultMovementMethod(); 220 221 assertNotNull(method1); 222 assertTrue(method1 instanceof ArrowKeyMovementMethod); 223 224 assertSame(method1, method2); 225 } 226 227 public void testSetEllipsize() { 228 EditText editText = new EditText(mContext); 229 assertNull(editText.getEllipsize()); 230 231 editText.setEllipsize(TextUtils.TruncateAt.START); 232 assertSame(TextUtils.TruncateAt.START, editText.getEllipsize()); 233 234 try { 235 editText.setEllipsize(TextUtils.TruncateAt.MARQUEE); 236 fail("Should throw IllegalArgumentException."); 237 } catch (IllegalArgumentException e) { 238 // expected, test success. 239 } 240 } 241 242 @SmallTest 243 public void testOnSaveInstanceState_savesTextStateWhenFreezesTextIsTrue() { 244 // prepare TextView for before saveInstanceState 245 final String testStr = "This is a test str"; 246 EditText editText1 = new EditText(mContext); 247 editText1.setFreezesText(true); 248 editText1.setText(testStr); 249 250 // prepare TextView for after saveInstanceState 251 EditText editText2 = new EditText(mContext); 252 editText2.setFreezesText(true); 253 254 editText2.onRestoreInstanceState(editText1.onSaveInstanceState()); 255 256 assertEquals(editText1.getText().toString(), editText2.getText().toString()); 257 } 258 259 @SmallTest 260 public void testOnSaveInstanceState_savesTextStateWhenFreezesTextIfFalse() { 261 // prepare TextView for before saveInstanceState 262 final String testStr = "This is a test str"; 263 EditText editText1 = new EditText(mContext); 264 editText1.setFreezesText(false); 265 editText1.setText(testStr); 266 267 // prepare TextView for after saveInstanceState 268 EditText editText2 = new EditText(mContext); 269 editText2.setFreezesText(false); 270 271 editText2.onRestoreInstanceState(editText1.onSaveInstanceState()); 272 273 assertEquals(editText1.getText().toString(), editText2.getText().toString()); 274 } 275 276 @SmallTest 277 public void testOnSaveInstanceState_savesSelectionStateWhenFreezesTextIsFalse() { 278 // prepare TextView for before saveInstanceState 279 final String testStr = "This is a test str"; 280 EditText editText1 = new EditText(mContext); 281 editText1.setFreezesText(false); 282 editText1.setText(testStr); 283 editText1.setSelection(2, testStr.length() - 2); 284 285 // prepare TextView for after saveInstanceState 286 EditText editText2 = new EditText(mContext); 287 editText2.setFreezesText(false); 288 289 editText2.onRestoreInstanceState(editText1.onSaveInstanceState()); 290 291 assertEquals(editText1.getSelectionStart(), editText2.getSelectionStart()); 292 assertEquals(editText1.getSelectionEnd(), editText2.getSelectionEnd()); 293 } 294 295 @SmallTest 296 public void testOnSaveInstanceState_savesSelectionStateWhenFreezesTextIsTrue() { 297 // prepare TextView for before saveInstanceState 298 final String testStr = "This is a test str"; 299 EditText editText1 = new EditText(mContext); 300 editText1.setFreezesText(true); 301 editText1.setText(testStr); 302 editText1.setSelection(2, testStr.length() - 2); 303 304 // prepare TextView for after saveInstanceState 305 EditText editText2 = new EditText(mContext); 306 editText2.setFreezesText(true); 307 308 editText2.onRestoreInstanceState(editText1.onSaveInstanceState()); 309 310 assertEquals(editText1.getSelectionStart(), editText2.getSelectionStart()); 311 assertEquals(editText1.getSelectionEnd(), editText2.getSelectionEnd()); 312 } 313 314 private class MockEditText extends EditText { 315 public MockEditText(Context context) { 316 super(context); 317 } 318 319 public MockEditText(Context context, AttributeSet attrs) { 320 super(context, attrs); 321 } 322 323 public MockEditText(Context context, AttributeSet attrs, int defStyle) { 324 super(context, attrs, defStyle); 325 } 326 327 @Override 328 protected boolean getDefaultEditable() { 329 return super.getDefaultEditable(); 330 } 331 332 @Override 333 protected MovementMethod getDefaultMovementMethod() { 334 return super.getDefaultMovementMethod(); 335 } 336 } 337 } 338