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.stub.R; 20 import com.android.internal.util.FastMath; 21 22 import org.xmlpull.v1.XmlPullParserException; 23 24 import android.app.Activity; 25 import android.app.Instrumentation; 26 import android.app.Instrumentation.ActivityMonitor; 27 import android.content.Intent; 28 import android.content.res.ColorStateList; 29 import android.content.res.Resources.NotFoundException; 30 import android.cts.util.PollingCheck; 31 import android.graphics.Bitmap; 32 import android.graphics.Color; 33 import android.graphics.Paint; 34 import android.graphics.Path; 35 import android.graphics.Rect; 36 import android.graphics.RectF; 37 import android.graphics.Typeface; 38 import android.graphics.drawable.BitmapDrawable; 39 import android.graphics.drawable.Drawable; 40 import android.net.Uri; 41 import android.os.Bundle; 42 import android.test.ActivityInstrumentationTestCase2; 43 import android.test.TouchUtils; 44 import android.test.UiThreadTest; 45 import android.text.Editable; 46 import android.text.InputFilter; 47 import android.text.InputType; 48 import android.text.Layout; 49 import android.text.Selection; 50 import android.text.Spannable; 51 import android.text.SpannableString; 52 import android.text.TextPaint; 53 import android.text.TextUtils; 54 import android.text.TextUtils.TruncateAt; 55 import android.text.TextWatcher; 56 import android.text.method.ArrowKeyMovementMethod; 57 import android.text.method.DateKeyListener; 58 import android.text.method.DateTimeKeyListener; 59 import android.text.method.DialerKeyListener; 60 import android.text.method.DigitsKeyListener; 61 import android.text.method.KeyListener; 62 import android.text.method.LinkMovementMethod; 63 import android.text.method.MovementMethod; 64 import android.text.method.PasswordTransformationMethod; 65 import android.text.method.QwertyKeyListener; 66 import android.text.method.SingleLineTransformationMethod; 67 import android.text.method.TextKeyListener; 68 import android.text.method.TextKeyListener.Capitalize; 69 import android.text.method.TimeKeyListener; 70 import android.text.method.TransformationMethod; 71 import android.text.style.URLSpan; 72 import android.text.style.cts.MockURLSpanTestActivity; 73 import android.text.util.Linkify; 74 import android.util.DisplayMetrics; 75 import android.util.TypedValue; 76 import android.view.ContextMenu; 77 import android.view.ContextMenu.ContextMenuInfo; 78 import android.view.Gravity; 79 import android.view.KeyEvent; 80 import android.view.View; 81 import android.view.View.OnCreateContextMenuListener; 82 import android.view.View.OnLongClickListener; 83 import android.view.ViewGroup; 84 import android.view.inputmethod.BaseInputConnection; 85 import android.view.inputmethod.EditorInfo; 86 import android.view.inputmethod.ExtractedText; 87 import android.view.inputmethod.ExtractedTextRequest; 88 import android.widget.EditText; 89 import android.widget.FrameLayout; 90 import android.widget.LinearLayout; 91 import android.widget.Scroller; 92 import android.widget.TextView; 93 import android.widget.TextView.BufferType; 94 import android.widget.TextView.OnEditorActionListener; 95 96 import java.io.IOException; 97 98 /** 99 * Test {@link TextView}. 100 */ 101 public class TextViewTest extends ActivityInstrumentationTestCase2<TextViewStubActivity> { 102 103 private TextView mTextView; 104 private Activity mActivity; 105 private Instrumentation mInstrumentation; 106 private static final String LONG_TEXT = "This is a really long string which exceeds " 107 + "the width of the view. New devices have a much larger screen which " 108 + "actually enables long strings to be displayed with no fading. " 109 + "I have made this string longer to fix this case. If you are correcting " 110 + "this text, I would love to see the kind of devices you guys now use!"; 111 private static final long TIMEOUT = 5000; 112 private CharSequence mTransformedText; 113 114 public TextViewTest() { 115 super("com.android.cts.stub", TextViewStubActivity.class); 116 } 117 118 @Override 119 protected void setUp() throws Exception { 120 super.setUp(); 121 mActivity = getActivity(); 122 mInstrumentation = getInstrumentation(); 123 } 124 125 public void testConstructor() { 126 new TextView(mActivity); 127 128 new TextView(mActivity, null); 129 130 new TextView(mActivity, null, 0); 131 } 132 133 @UiThreadTest 134 public void testAccessText() { 135 TextView tv = findTextView(R.id.textview_text); 136 137 String expected = mActivity.getResources().getString(R.string.text_view_hello); 138 tv.setText(expected); 139 assertEquals(expected, tv.getText().toString()); 140 141 tv.setText(null); 142 assertEquals("", tv.getText().toString()); 143 } 144 145 public void testGetLineHeight() { 146 mTextView = new TextView(mActivity); 147 assertTrue(mTextView.getLineHeight() > 0); 148 149 mTextView.setLineSpacing(1.2f, 1.5f); 150 assertTrue(mTextView.getLineHeight() > 0); 151 } 152 153 public void testGetLayout() { 154 mActivity.runOnUiThread(new Runnable() { 155 public void run() { 156 mTextView = findTextView(R.id.textview_text); 157 mTextView.setGravity(Gravity.CENTER); 158 } 159 }); 160 mInstrumentation.waitForIdleSync(); 161 assertNotNull(mTextView.getLayout()); 162 163 TestLayoutRunnable runnable = new TestLayoutRunnable(mTextView) { 164 public void run() { 165 // change the text of TextView. 166 mTextView.setText("Hello, Android!"); 167 saveLayout(); 168 } 169 }; 170 mActivity.runOnUiThread(runnable); 171 mInstrumentation.waitForIdleSync(); 172 assertNull(runnable.getLayout()); 173 assertNotNull(mTextView.getLayout()); 174 } 175 176 public void testAccessKeyListener() { 177 mActivity.runOnUiThread(new Runnable() { 178 public void run() { 179 mTextView = findTextView(R.id.textview_text); 180 } 181 }); 182 mInstrumentation.waitForIdleSync(); 183 184 assertNull(mTextView.getKeyListener()); 185 186 final KeyListener digitsKeyListener = DigitsKeyListener.getInstance(); 187 188 mActivity.runOnUiThread(new Runnable() { 189 public void run() { 190 mTextView.setKeyListener(digitsKeyListener); 191 } 192 }); 193 mInstrumentation.waitForIdleSync(); 194 assertSame(digitsKeyListener, mTextView.getKeyListener()); 195 196 final QwertyKeyListener qwertyKeyListener 197 = QwertyKeyListener.getInstance(false, Capitalize.NONE); 198 mActivity.runOnUiThread(new Runnable() { 199 public void run() { 200 mTextView.setKeyListener(qwertyKeyListener); 201 } 202 }); 203 mInstrumentation.waitForIdleSync(); 204 assertSame(qwertyKeyListener, mTextView.getKeyListener()); 205 } 206 207 public void testAccessMovementMethod() { 208 final CharSequence LONG_TEXT = "Scrolls the specified widget to the specified " 209 + "coordinates, except constrains the X scrolling position to the horizontal " 210 + "regions of the text that will be visible after scrolling to " 211 + "the specified Y position."; 212 final int selectionStart = 10; 213 final int selectionEnd = LONG_TEXT.length(); 214 final MovementMethod movementMethod = ArrowKeyMovementMethod.getInstance(); 215 mActivity.runOnUiThread(new Runnable() { 216 public void run() { 217 mTextView = findTextView(R.id.textview_text); 218 mTextView.setMovementMethod(movementMethod); 219 mTextView.setText(LONG_TEXT, BufferType.EDITABLE); 220 Selection.setSelection((Editable) mTextView.getText(), 221 selectionStart, selectionEnd); 222 mTextView.requestFocus(); 223 } 224 }); 225 mInstrumentation.waitForIdleSync(); 226 227 assertSame(movementMethod, mTextView.getMovementMethod()); 228 assertEquals(selectionStart, Selection.getSelectionStart(mTextView.getText())); 229 assertEquals(selectionEnd, Selection.getSelectionEnd(mTextView.getText())); 230 sendKeys(KeyEvent.KEYCODE_SHIFT_LEFT, KeyEvent.KEYCODE_ALT_LEFT, 231 KeyEvent.KEYCODE_DPAD_UP); 232 // the selection has been removed. 233 assertEquals(selectionStart, Selection.getSelectionStart(mTextView.getText())); 234 assertEquals(selectionStart, Selection.getSelectionEnd(mTextView.getText())); 235 236 mActivity.runOnUiThread(new Runnable() { 237 public void run() { 238 mTextView.setMovementMethod(null); 239 Selection.setSelection((Editable) mTextView.getText(), 240 selectionStart, selectionEnd); 241 mTextView.requestFocus(); 242 } 243 }); 244 mInstrumentation.waitForIdleSync(); 245 246 assertNull(mTextView.getMovementMethod()); 247 assertEquals(selectionStart, Selection.getSelectionStart(mTextView.getText())); 248 assertEquals(selectionEnd, Selection.getSelectionEnd(mTextView.getText())); 249 sendKeys(KeyEvent.KEYCODE_SHIFT_LEFT, KeyEvent.KEYCODE_ALT_LEFT, 250 KeyEvent.KEYCODE_DPAD_UP); 251 // the selection will not be changed. 252 assertEquals(selectionStart, Selection.getSelectionStart(mTextView.getText())); 253 assertEquals(selectionEnd, Selection.getSelectionEnd(mTextView.getText())); 254 } 255 256 @UiThreadTest 257 public void testLength() { 258 mTextView = findTextView(R.id.textview_text); 259 260 String content = "This is content"; 261 mTextView.setText(content); 262 assertEquals(content.length(), mTextView.length()); 263 264 mTextView.setText(""); 265 assertEquals(0, mTextView.length()); 266 267 mTextView.setText(null); 268 assertEquals(0, mTextView.length()); 269 } 270 271 @UiThreadTest 272 public void testAccessGravity() { 273 mActivity.setContentView(R.layout.textview_gravity); 274 275 mTextView = findTextView(R.id.gravity_default); 276 assertEquals(Gravity.TOP | Gravity.START, mTextView.getGravity()); 277 278 mTextView = findTextView(R.id.gravity_bottom); 279 assertEquals(Gravity.BOTTOM | Gravity.START, mTextView.getGravity()); 280 281 mTextView = findTextView(R.id.gravity_right); 282 assertEquals(Gravity.TOP | Gravity.RIGHT, mTextView.getGravity()); 283 284 mTextView = findTextView(R.id.gravity_center); 285 assertEquals(Gravity.CENTER, mTextView.getGravity()); 286 287 mTextView = findTextView(R.id.gravity_fill); 288 assertEquals(Gravity.FILL, mTextView.getGravity()); 289 290 mTextView = findTextView(R.id.gravity_center_vertical_right); 291 assertEquals(Gravity.CENTER_VERTICAL | Gravity.RIGHT, mTextView.getGravity()); 292 293 mTextView.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL); 294 assertEquals(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, mTextView.getGravity()); 295 mTextView.setGravity(Gravity.FILL); 296 assertEquals(Gravity.FILL, mTextView.getGravity()); 297 mTextView.setGravity(Gravity.CENTER); 298 assertEquals(Gravity.CENTER, mTextView.getGravity()); 299 300 mTextView.setGravity(Gravity.NO_GRAVITY); 301 assertEquals(Gravity.TOP | Gravity.START, mTextView.getGravity()); 302 303 mTextView.setGravity(Gravity.RIGHT); 304 assertEquals(Gravity.TOP | Gravity.RIGHT, mTextView.getGravity()); 305 306 mTextView.setGravity(Gravity.FILL_VERTICAL); 307 assertEquals(Gravity.FILL_VERTICAL | Gravity.START, mTextView.getGravity()); 308 309 //test negative input value. 310 mTextView.setGravity(-1); 311 assertEquals(-1, mTextView.getGravity()); 312 } 313 314 public void testAccessAutoLinkMask() { 315 mTextView = findTextView(R.id.textview_text); 316 final CharSequence text1 = 317 new SpannableString("URL: http://www.google.com. mailto: account (at) gmail.com"); 318 mActivity.runOnUiThread(new Runnable() { 319 public void run() { 320 mTextView.setAutoLinkMask(Linkify.ALL); 321 mTextView.setText(text1, BufferType.EDITABLE); 322 } 323 }); 324 mInstrumentation.waitForIdleSync(); 325 assertEquals(Linkify.ALL, mTextView.getAutoLinkMask()); 326 327 Spannable spanString = (Spannable) mTextView.getText(); 328 URLSpan[] spans = spanString.getSpans(0, spanString.length(), URLSpan.class); 329 assertNotNull(spans); 330 assertEquals(2, spans.length); 331 assertEquals("http://www.google.com", spans[0].getURL()); 332 assertEquals("mailto:account (at) gmail.com", spans[1].getURL()); 333 334 final CharSequence text2 = 335 new SpannableString("name: Jack. tel: +41 44 800 8999"); 336 mActivity.runOnUiThread(new Runnable() { 337 public void run() { 338 mTextView.setAutoLinkMask(Linkify.PHONE_NUMBERS); 339 mTextView.setText(text2, BufferType.EDITABLE); 340 } 341 }); 342 mInstrumentation.waitForIdleSync(); 343 assertEquals(Linkify.PHONE_NUMBERS, mTextView.getAutoLinkMask()); 344 345 spanString = (Spannable) mTextView.getText(); 346 spans = spanString.getSpans(0, spanString.length(), URLSpan.class); 347 assertNotNull(spans); 348 assertEquals(1, spans.length); 349 assertEquals("tel:+41448008999", spans[0].getURL()); 350 351 layout(R.layout.textview_autolink); 352 // 1 for web, 2 for email, 4 for phone, 7 for all(web|email|phone) 353 assertEquals(0, getAutoLinkMask(R.id.autolink_default)); 354 assertEquals(Linkify.WEB_URLS, getAutoLinkMask(R.id.autolink_web)); 355 assertEquals(Linkify.EMAIL_ADDRESSES, getAutoLinkMask(R.id.autolink_email)); 356 assertEquals(Linkify.PHONE_NUMBERS, getAutoLinkMask(R.id.autolink_phone)); 357 assertEquals(Linkify.ALL, getAutoLinkMask(R.id.autolink_all)); 358 assertEquals(Linkify.WEB_URLS | Linkify.EMAIL_ADDRESSES, 359 getAutoLinkMask(R.id.autolink_compound1)); 360 assertEquals(Linkify.WEB_URLS | Linkify.PHONE_NUMBERS, 361 getAutoLinkMask(R.id.autolink_compound2)); 362 assertEquals(Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS, 363 getAutoLinkMask(R.id.autolink_compound3)); 364 assertEquals(Linkify.PHONE_NUMBERS | Linkify.ALL, 365 getAutoLinkMask(R.id.autolink_compound4)); 366 } 367 368 public void testAccessTextSize() { 369 DisplayMetrics metrics = mActivity.getResources().getDisplayMetrics(); 370 371 mTextView = new TextView(mActivity); 372 mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 20f); 373 assertEquals(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 20f, metrics), 374 mTextView.getTextSize(), 0.01f); 375 376 mTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20f); 377 assertEquals(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20f, metrics), 378 mTextView.getTextSize(), 0.01f); 379 380 mTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20f); 381 assertEquals(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20f, metrics), 382 mTextView.getTextSize(), 0.01f); 383 384 // setTextSize by default unit "sp" 385 mTextView.setTextSize(20f); 386 assertEquals(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20f, metrics), 387 mTextView.getTextSize(), 0.01f); 388 389 mTextView.setTextSize(200f); 390 assertEquals(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 200f, metrics), 391 mTextView.getTextSize(), 0.01f); 392 } 393 394 public void testAccessTextColor() { 395 mTextView = new TextView(mActivity); 396 397 mTextView.setTextColor(Color.GREEN); 398 assertEquals(Color.GREEN, mTextView.getCurrentTextColor()); 399 assertSame(ColorStateList.valueOf(Color.GREEN), mTextView.getTextColors()); 400 401 mTextView.setTextColor(Color.BLACK); 402 assertEquals(Color.BLACK, mTextView.getCurrentTextColor()); 403 assertSame(ColorStateList.valueOf(Color.BLACK), mTextView.getTextColors()); 404 405 mTextView.setTextColor(Color.RED); 406 assertEquals(Color.RED, mTextView.getCurrentTextColor()); 407 assertSame(ColorStateList.valueOf(Color.RED), mTextView.getTextColors()); 408 409 // using ColorStateList 410 // normal 411 ColorStateList colors = new ColorStateList(new int[][] { 412 new int[] { android.R.attr.state_focused}, new int[0] }, 413 new int[] { Color.rgb(0, 255, 0), Color.BLACK }); 414 mTextView.setTextColor(colors); 415 assertSame(colors, mTextView.getTextColors()); 416 assertEquals(Color.BLACK, mTextView.getCurrentTextColor()); 417 418 // exceptional 419 try { 420 mTextView.setTextColor(null); 421 fail("Should thrown exception if the colors is null"); 422 } catch (NullPointerException e){ 423 } 424 } 425 426 public void testGetTextColor() { 427 // TODO: How to get a suitable TypedArray to test this method. 428 429 try { 430 TextView.getTextColor(mActivity, null, -1); 431 fail("There should be a NullPointerException thrown out."); 432 } catch (NullPointerException e) { 433 } 434 } 435 436 public void testSetHighlightColor() { 437 mTextView = new TextView(mActivity); 438 439 mTextView.setHighlightColor(0x00ff00ff); 440 } 441 442 public void testSetShadowLayer() { 443 MockTextView textView = new MockTextView(mActivity); 444 445 // shadow is placed to the left and below the text 446 textView.setShadowLayer(1.0f, 0.3f, 0.3f, Color.CYAN); 447 assertTrue(textView.isPaddingOffsetRequired()); 448 assertEquals(0, textView.getLeftPaddingOffset()); 449 assertEquals(0, textView.getTopPaddingOffset()); 450 assertEquals(1, textView.getRightPaddingOffset()); 451 assertEquals(1, textView.getBottomPaddingOffset()); 452 453 // shadow is placed to the right and above the text 454 textView.setShadowLayer(1.0f, -0.8f, -0.8f, Color.CYAN); 455 assertTrue(textView.isPaddingOffsetRequired()); 456 assertEquals(-1, textView.getLeftPaddingOffset()); 457 assertEquals(-1, textView.getTopPaddingOffset()); 458 assertEquals(0, textView.getRightPaddingOffset()); 459 assertEquals(0, textView.getBottomPaddingOffset()); 460 461 // no shadow 462 textView.setShadowLayer(0.0f, 0.0f, 0.0f, Color.CYAN); 463 assertFalse(textView.isPaddingOffsetRequired()); 464 assertEquals(0, textView.getLeftPaddingOffset()); 465 assertEquals(0, textView.getTopPaddingOffset()); 466 assertEquals(0, textView.getRightPaddingOffset()); 467 assertEquals(0, textView.getBottomPaddingOffset()); 468 } 469 470 @UiThreadTest 471 public void testSetSelectAllOnFocus() { 472 mActivity.setContentView(R.layout.textview_selectallonfocus); 473 String content = "This is the content"; 474 String blank = ""; 475 mTextView = findTextView(R.id.selectAllOnFocus_default); 476 mTextView.setText(blank, BufferType.SPANNABLE); 477 // change the focus 478 findTextView(R.id.selectAllOnFocus_dummy).requestFocus(); 479 assertFalse(mTextView.isFocused()); 480 mTextView.requestFocus(); 481 assertTrue(mTextView.isFocused()); 482 483 assertEquals(-1, mTextView.getSelectionStart()); 484 assertEquals(-1, mTextView.getSelectionEnd()); 485 486 mTextView.setText(content, BufferType.SPANNABLE); 487 mTextView.setSelectAllOnFocus(true); 488 // change the focus 489 findTextView(R.id.selectAllOnFocus_dummy).requestFocus(); 490 assertFalse(mTextView.isFocused()); 491 mTextView.requestFocus(); 492 assertTrue(mTextView.isFocused()); 493 494 assertEquals(0, mTextView.getSelectionStart()); 495 assertEquals(content.length(), mTextView.getSelectionEnd()); 496 497 Selection.setSelection((Spannable) mTextView.getText(), 0); 498 mTextView.setSelectAllOnFocus(false); 499 // change the focus 500 findTextView(R.id.selectAllOnFocus_dummy).requestFocus(); 501 assertFalse(mTextView.isFocused()); 502 mTextView.requestFocus(); 503 assertTrue(mTextView.isFocused()); 504 505 assertEquals(0, mTextView.getSelectionStart()); 506 assertEquals(0, mTextView.getSelectionEnd()); 507 508 mTextView.setText(blank, BufferType.SPANNABLE); 509 mTextView.setSelectAllOnFocus(true); 510 // change the focus 511 findTextView(R.id.selectAllOnFocus_dummy).requestFocus(); 512 assertFalse(mTextView.isFocused()); 513 mTextView.requestFocus(); 514 assertTrue(mTextView.isFocused()); 515 516 assertEquals(0, mTextView.getSelectionStart()); 517 assertEquals(blank.length(), mTextView.getSelectionEnd()); 518 519 Selection.setSelection((Spannable) mTextView.getText(), 0); 520 mTextView.setSelectAllOnFocus(false); 521 // change the focus 522 findTextView(R.id.selectAllOnFocus_dummy).requestFocus(); 523 assertFalse(mTextView.isFocused()); 524 mTextView.requestFocus(); 525 assertTrue(mTextView.isFocused()); 526 527 assertEquals(0, mTextView.getSelectionStart()); 528 assertEquals(0, mTextView.getSelectionEnd()); 529 } 530 531 public void testGetPaint() { 532 mTextView = new TextView(mActivity); 533 TextPaint tp = mTextView.getPaint(); 534 assertNotNull(tp); 535 536 assertEquals(mTextView.getPaintFlags(), tp.getFlags()); 537 } 538 539 @UiThreadTest 540 public void testAccessLinksClickable() { 541 mActivity.setContentView(R.layout.textview_hint_linksclickable_freezestext); 542 543 mTextView = findTextView(R.id.hint_linksClickable_freezesText_default); 544 assertTrue(mTextView.getLinksClickable()); 545 546 mTextView = findTextView(R.id.linksClickable_true); 547 assertTrue(mTextView.getLinksClickable()); 548 549 mTextView = findTextView(R.id.linksClickable_false); 550 assertFalse(mTextView.getLinksClickable()); 551 552 mTextView.setLinksClickable(false); 553 assertFalse(mTextView.getLinksClickable()); 554 555 mTextView.setLinksClickable(true); 556 assertTrue(mTextView.getLinksClickable()); 557 558 assertNull(mTextView.getMovementMethod()); 559 560 final CharSequence text = new SpannableString("name: Jack. tel: +41 44 800 8999"); 561 562 mTextView.setAutoLinkMask(Linkify.PHONE_NUMBERS); 563 mTextView.setText(text, BufferType.EDITABLE); 564 565 // Movement method will be automatically set to LinkMovementMethod 566 assertTrue(mTextView.getMovementMethod() instanceof LinkMovementMethod); 567 } 568 569 public void testAccessHintTextColor() { 570 mTextView = new TextView(mActivity); 571 // using int values 572 // normal 573 mTextView.setHintTextColor(Color.GREEN); 574 assertEquals(Color.GREEN, mTextView.getCurrentHintTextColor()); 575 assertSame(ColorStateList.valueOf(Color.GREEN), mTextView.getHintTextColors()); 576 577 mTextView.setHintTextColor(Color.BLUE); 578 assertSame(ColorStateList.valueOf(Color.BLUE), mTextView.getHintTextColors()); 579 assertEquals(Color.BLUE, mTextView.getCurrentHintTextColor()); 580 581 mTextView.setHintTextColor(Color.RED); 582 assertSame(ColorStateList.valueOf(Color.RED), mTextView.getHintTextColors()); 583 assertEquals(Color.RED, mTextView.getCurrentHintTextColor()); 584 585 // using ColorStateList 586 // normal 587 ColorStateList colors = new ColorStateList(new int[][] { 588 new int[] { android.R.attr.state_focused}, new int[0] }, 589 new int[] { Color.rgb(0, 255, 0), Color.BLACK }); 590 mTextView.setHintTextColor(colors); 591 assertSame(colors, mTextView.getHintTextColors()); 592 assertEquals(Color.BLACK, mTextView.getCurrentHintTextColor()); 593 594 // exceptional 595 mTextView.setHintTextColor(null); 596 assertNull(mTextView.getHintTextColors()); 597 assertEquals(mTextView.getCurrentTextColor(), mTextView.getCurrentHintTextColor()); 598 } 599 600 public void testAccessLinkTextColor() { 601 mTextView = new TextView(mActivity); 602 // normal 603 mTextView.setLinkTextColor(Color.GRAY); 604 assertSame(ColorStateList.valueOf(Color.GRAY), mTextView.getLinkTextColors()); 605 assertEquals(Color.GRAY, mTextView.getPaint().linkColor); 606 607 mTextView.setLinkTextColor(Color.YELLOW); 608 assertSame(ColorStateList.valueOf(Color.YELLOW), mTextView.getLinkTextColors()); 609 assertEquals(Color.YELLOW, mTextView.getPaint().linkColor); 610 611 mTextView.setLinkTextColor(Color.WHITE); 612 assertSame(ColorStateList.valueOf(Color.WHITE), mTextView.getLinkTextColors()); 613 assertEquals(Color.WHITE, mTextView.getPaint().linkColor); 614 615 ColorStateList colors = new ColorStateList(new int[][] { 616 new int[] { android.R.attr.state_expanded}, new int[0] }, 617 new int[] { Color.rgb(0, 255, 0), Color.BLACK }); 618 mTextView.setLinkTextColor(colors); 619 assertSame(colors, mTextView.getLinkTextColors()); 620 assertEquals(Color.BLACK, mTextView.getPaint().linkColor); 621 622 mTextView.setLinkTextColor(null); 623 assertNull(mTextView.getLinkTextColors()); 624 assertEquals(Color.BLACK, mTextView.getPaint().linkColor); 625 } 626 627 public void testAccessPaintFlags() { 628 mTextView = new TextView(mActivity); 629 assertEquals(Paint.DEV_KERN_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG, mTextView.getPaintFlags()); 630 631 mTextView.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG | Paint.FAKE_BOLD_TEXT_FLAG); 632 assertEquals(Paint.UNDERLINE_TEXT_FLAG | Paint.FAKE_BOLD_TEXT_FLAG, 633 mTextView.getPaintFlags()); 634 635 mTextView.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.LINEAR_TEXT_FLAG); 636 assertEquals(Paint.STRIKE_THRU_TEXT_FLAG | Paint.LINEAR_TEXT_FLAG, 637 mTextView.getPaintFlags()); 638 } 639 640 public void testHeightAndWidth() { 641 mTextView = findTextView(R.id.textview_text); 642 int originalWidth = mTextView.getWidth(); 643 setWidth(mTextView.getWidth() >> 3); 644 int originalHeight = mTextView.getHeight(); 645 646 setMaxHeight(originalHeight + 1); 647 assertEquals(originalHeight, mTextView.getHeight()); 648 649 setMaxHeight(originalHeight - 1); 650 assertEquals(originalHeight - 1, mTextView.getHeight()); 651 652 setMaxHeight(-1); 653 assertEquals(0, mTextView.getHeight()); 654 655 setMaxHeight(Integer.MAX_VALUE); 656 assertEquals(originalHeight, mTextView.getHeight()); 657 658 setMinHeight(originalHeight + 1); 659 assertEquals(originalHeight + 1, mTextView.getHeight()); 660 661 setMinHeight(originalHeight - 1); 662 assertEquals(originalHeight, mTextView.getHeight()); 663 664 setMinHeight(-1); 665 assertEquals(originalHeight, mTextView.getHeight()); 666 667 setMinHeight(0); 668 setMaxHeight(Integer.MAX_VALUE); 669 670 setHeight(originalHeight + 1); 671 assertEquals(originalHeight + 1, mTextView.getHeight()); 672 673 setHeight(originalHeight - 1); 674 assertEquals(originalHeight - 1, mTextView.getHeight()); 675 676 setHeight(-1); 677 assertEquals(0, mTextView.getHeight()); 678 679 setHeight(originalHeight); 680 assertEquals(originalHeight, mTextView.getHeight()); 681 682 assertEquals(originalWidth >> 3, mTextView.getWidth()); 683 684 // Min Width 685 setMinWidth(originalWidth + 1); 686 assertEquals(1, mTextView.getLineCount()); 687 assertEquals(originalWidth + 1, mTextView.getWidth()); 688 689 setMinWidth(originalWidth - 1); 690 assertEquals(2, mTextView.getLineCount()); 691 assertEquals(originalWidth - 1, mTextView.getWidth()); 692 693 // Width 694 setWidth(originalWidth + 1); 695 assertEquals(1, mTextView.getLineCount()); 696 assertEquals(originalWidth + 1, mTextView.getWidth()); 697 698 setWidth(originalWidth - 1); 699 assertEquals(2, mTextView.getLineCount()); 700 assertEquals(originalWidth - 1, mTextView.getWidth()); 701 } 702 703 public void testSetMinEms() { 704 mTextView = findTextView(R.id.textview_text); 705 assertEquals(1, mTextView.getLineCount()); 706 707 int originalWidth = mTextView.getWidth(); 708 int originalEms = originalWidth / mTextView.getLineHeight(); 709 710 setMinEms(originalEms + 1); 711 assertEquals((originalEms + 1) * mTextView.getLineHeight(), mTextView.getWidth()); 712 713 setMinEms(originalEms - 1); 714 assertEquals(originalWidth, mTextView.getWidth()); 715 } 716 717 public void testSetMaxEms() { 718 mTextView = findTextView(R.id.textview_text); 719 assertEquals(1, mTextView.getLineCount()); 720 int originalWidth = mTextView.getWidth(); 721 int originalEms = originalWidth / mTextView.getLineHeight(); 722 723 setMaxEms(originalEms + 1); 724 assertEquals(1, mTextView.getLineCount()); 725 assertEquals(originalWidth, mTextView.getWidth()); 726 727 setMaxEms(originalEms - 1); 728 assertTrue(1 < mTextView.getLineCount()); 729 assertEquals((originalEms - 1) * mTextView.getLineHeight(), 730 mTextView.getWidth()); 731 } 732 733 public void testSetEms() { 734 mTextView = findTextView(R.id.textview_text); 735 assertEquals("check height", 1, mTextView.getLineCount()); 736 int originalWidth = mTextView.getWidth(); 737 int originalEms = originalWidth / mTextView.getLineHeight(); 738 739 setEms(originalEms + 1); 740 assertEquals(1, mTextView.getLineCount()); 741 assertEquals((originalEms + 1) * mTextView.getLineHeight(), 742 mTextView.getWidth()); 743 744 setEms(originalEms - 1); 745 assertTrue((1 < mTextView.getLineCount())); 746 assertEquals((originalEms - 1) * mTextView.getLineHeight(), 747 mTextView.getWidth()); 748 } 749 750 public void testSetLineSpacing() { 751 mTextView = new TextView(mActivity); 752 int originalLineHeight = mTextView.getLineHeight(); 753 754 // normal 755 float add = 1.2f; 756 float mult = 1.4f; 757 setLineSpacing(add, mult); 758 assertEquals(FastMath.round(originalLineHeight * mult + add), mTextView.getLineHeight()); 759 add = 0.0f; 760 mult = 1.4f; 761 setLineSpacing(add, mult); 762 assertEquals(FastMath.round(originalLineHeight * mult + add), mTextView.getLineHeight()); 763 764 // abnormal 765 add = -1.2f; 766 mult = 1.4f; 767 setLineSpacing(add, mult); 768 assertEquals(FastMath.round(originalLineHeight * mult + add), mTextView.getLineHeight()); 769 add = -1.2f; 770 mult = -1.4f; 771 setLineSpacing(add, mult); 772 assertEquals(FastMath.round(originalLineHeight * mult + add), mTextView.getLineHeight()); 773 add = 1.2f; 774 mult = 0.0f; 775 setLineSpacing(add, mult); 776 assertEquals(FastMath.round(originalLineHeight * mult + add), mTextView.getLineHeight()); 777 778 // edge 779 add = Float.MIN_VALUE; 780 mult = Float.MIN_VALUE; 781 setLineSpacing(add, mult); 782 float expected = originalLineHeight * mult + add; 783 assertEquals(FastMath.round(expected), mTextView.getLineHeight()); 784 add = Float.MAX_VALUE; 785 mult = Float.MAX_VALUE; 786 setLineSpacing(add, mult); 787 expected = originalLineHeight * mult + add; 788 assertEquals(FastMath.round(expected), mTextView.getLineHeight()); 789 } 790 791 public void testInstanceState() { 792 // Do not test. Implementation details. 793 } 794 795 public void testAccessFreezesText() throws Throwable { 796 layout(R.layout.textview_hint_linksclickable_freezestext); 797 798 mTextView = findTextView(R.id.hint_linksClickable_freezesText_default); 799 assertFalse(mTextView.getFreezesText()); 800 801 mTextView = findTextView(R.id.freezesText_true); 802 assertTrue(mTextView.getFreezesText()); 803 804 mTextView = findTextView(R.id.freezesText_false); 805 assertFalse(mTextView.getFreezesText()); 806 807 mTextView.setFreezesText(false); 808 assertFalse(mTextView.getFreezesText()); 809 810 final CharSequence text = "Hello, TextView."; 811 mActivity.runOnUiThread(new Runnable() { 812 public void run() { 813 mTextView.setText(text); 814 } 815 }); 816 mInstrumentation.waitForIdleSync(); 817 818 final URLSpan urlSpan = new URLSpan("ctstest://TextView/test"); 819 // TODO: How to simulate the TextView in frozen icicles. 820 Instrumentation instrumentation = getInstrumentation(); 821 ActivityMonitor am = instrumentation.addMonitor(MockURLSpanTestActivity.class.getName(), 822 null, false); 823 824 mActivity.runOnUiThread(new Runnable() { 825 public void run() { 826 Uri uri = Uri.parse(urlSpan.getURL()); 827 Intent intent = new Intent(Intent.ACTION_VIEW, uri); 828 mActivity.startActivity(intent); 829 } 830 }); 831 832 Activity newActivity = am.waitForActivityWithTimeout(TIMEOUT); 833 assertNotNull(newActivity); 834 newActivity.finish(); 835 instrumentation.removeMonitor(am); 836 // the text of TextView is removed. 837 mTextView = findTextView(R.id.freezesText_false); 838 839 assertEquals(text.toString(), mTextView.getText().toString()); 840 841 mTextView.setFreezesText(true); 842 assertTrue(mTextView.getFreezesText()); 843 844 mActivity.runOnUiThread(new Runnable() { 845 public void run() { 846 mTextView.setText(text); 847 } 848 }); 849 mInstrumentation.waitForIdleSync(); 850 // TODO: How to simulate the TextView in frozen icicles. 851 am = instrumentation.addMonitor(MockURLSpanTestActivity.class.getName(), 852 null, false); 853 854 mActivity.runOnUiThread(new Runnable() { 855 public void run() { 856 Uri uri = Uri.parse(urlSpan.getURL()); 857 Intent intent = new Intent(Intent.ACTION_VIEW, uri); 858 mActivity.startActivity(intent); 859 } 860 }); 861 862 Activity oldActivity = newActivity; 863 while (true) { 864 newActivity = am.waitForActivityWithTimeout(TIMEOUT); 865 assertNotNull(newActivity); 866 if (newActivity != oldActivity) { 867 break; 868 } 869 } 870 newActivity.finish(); 871 instrumentation.removeMonitor(am); 872 // the text of TextView is still there. 873 mTextView = findTextView(R.id.freezesText_false); 874 assertEquals(text.toString(), mTextView.getText().toString()); 875 } 876 877 public void testSetEditableFactory() { 878 mTextView = new TextView(mActivity); 879 String text = "sample"; 880 MockEditableFactory factory = new MockEditableFactory(); 881 mTextView.setEditableFactory(factory); 882 883 factory.reset(); 884 mTextView.setText(text); 885 assertFalse(factory.hasCalledNewEditable()); 886 887 factory.reset(); 888 mTextView.setText(text, BufferType.SPANNABLE); 889 assertFalse(factory.hasCalledNewEditable()); 890 891 factory.reset(); 892 mTextView.setText(text, BufferType.NORMAL); 893 assertFalse(factory.hasCalledNewEditable()); 894 895 factory.reset(); 896 mTextView.setText(text, BufferType.EDITABLE); 897 assertTrue(factory.hasCalledNewEditable()); 898 assertEquals(text, factory.getSource()); 899 900 mTextView.setKeyListener(DigitsKeyListener.getInstance()); 901 factory.reset(); 902 mTextView.setText(text, BufferType.EDITABLE); 903 assertTrue(factory.hasCalledNewEditable()); 904 assertEquals(text, factory.getSource()); 905 906 try { 907 mTextView.setEditableFactory(null); 908 fail("The factory can not set to null!"); 909 } catch (NullPointerException e) { 910 } 911 } 912 913 public void testSetSpannableFactory() { 914 mTextView = new TextView(mActivity); 915 String text = "sample"; 916 MockSpannableFactory factory = new MockSpannableFactory(); 917 mTextView.setSpannableFactory(factory); 918 919 factory.reset(); 920 mTextView.setText(text); 921 assertFalse(factory.hasCalledNewSpannable()); 922 923 factory.reset(); 924 mTextView.setText(text, BufferType.EDITABLE); 925 assertFalse(factory.hasCalledNewSpannable()); 926 927 factory.reset(); 928 mTextView.setText(text, BufferType.NORMAL); 929 assertFalse(factory.hasCalledNewSpannable()); 930 931 factory.reset(); 932 mTextView.setText(text, BufferType.SPANNABLE); 933 assertTrue(factory.hasCalledNewSpannable()); 934 assertEquals(text, factory.getSource()); 935 936 mTextView.setMovementMethod(LinkMovementMethod.getInstance()); 937 factory.reset(); 938 mTextView.setText(text, BufferType.NORMAL); 939 assertTrue(factory.hasCalledNewSpannable()); 940 assertEquals(text, factory.getSource()); 941 942 try { 943 mTextView.setSpannableFactory(null); 944 fail("The factory can not set to null!"); 945 } catch (NullPointerException e) { 946 } 947 } 948 949 public void testTextChangedListener() { 950 mTextView = new TextView(mActivity); 951 MockTextWatcher watcher0 = new MockTextWatcher(); 952 MockTextWatcher watcher1 = new MockTextWatcher(); 953 954 mTextView.addTextChangedListener(watcher0); 955 mTextView.addTextChangedListener(watcher1); 956 957 watcher0.reset(); 958 watcher1.reset(); 959 mTextView.setText("Changed"); 960 assertTrue(watcher0.hasCalledBeforeTextChanged()); 961 assertTrue(watcher0.hasCalledOnTextChanged()); 962 assertTrue(watcher0.hasCalledAfterTextChanged()); 963 assertTrue(watcher1.hasCalledBeforeTextChanged()); 964 assertTrue(watcher1.hasCalledOnTextChanged()); 965 assertTrue(watcher1.hasCalledAfterTextChanged()); 966 967 watcher0.reset(); 968 watcher1.reset(); 969 // BeforeTextChanged and OnTextChanged are called though the strings are same 970 mTextView.setText("Changed"); 971 assertTrue(watcher0.hasCalledBeforeTextChanged()); 972 assertTrue(watcher0.hasCalledOnTextChanged()); 973 assertTrue(watcher0.hasCalledAfterTextChanged()); 974 assertTrue(watcher1.hasCalledBeforeTextChanged()); 975 assertTrue(watcher1.hasCalledOnTextChanged()); 976 assertTrue(watcher1.hasCalledAfterTextChanged()); 977 978 watcher0.reset(); 979 watcher1.reset(); 980 // BeforeTextChanged and OnTextChanged are called twice (The text is not 981 // Editable, so in Append() it calls setText() first) 982 mTextView.append("and appended"); 983 assertTrue(watcher0.hasCalledBeforeTextChanged()); 984 assertTrue(watcher0.hasCalledOnTextChanged()); 985 assertTrue(watcher0.hasCalledAfterTextChanged()); 986 assertTrue(watcher1.hasCalledBeforeTextChanged()); 987 assertTrue(watcher1.hasCalledOnTextChanged()); 988 assertTrue(watcher1.hasCalledAfterTextChanged()); 989 990 watcher0.reset(); 991 watcher1.reset(); 992 // Methods are not called if the string does not change 993 mTextView.append(""); 994 assertFalse(watcher0.hasCalledBeforeTextChanged()); 995 assertFalse(watcher0.hasCalledOnTextChanged()); 996 assertFalse(watcher0.hasCalledAfterTextChanged()); 997 assertFalse(watcher1.hasCalledBeforeTextChanged()); 998 assertFalse(watcher1.hasCalledOnTextChanged()); 999 assertFalse(watcher1.hasCalledAfterTextChanged()); 1000 1001 watcher0.reset(); 1002 watcher1.reset(); 1003 mTextView.removeTextChangedListener(watcher1); 1004 mTextView.setText(null); 1005 assertTrue(watcher0.hasCalledBeforeTextChanged()); 1006 assertTrue(watcher0.hasCalledOnTextChanged()); 1007 assertTrue(watcher0.hasCalledAfterTextChanged()); 1008 assertFalse(watcher1.hasCalledBeforeTextChanged()); 1009 assertFalse(watcher1.hasCalledOnTextChanged()); 1010 assertFalse(watcher1.hasCalledAfterTextChanged()); 1011 } 1012 1013 public void testSetTextKeepState1() { 1014 mTextView = new TextView(mActivity); 1015 1016 String longString = "very long content"; 1017 String shortString = "short"; 1018 1019 // selection is at the exact place which is inside the short string 1020 mTextView.setText(longString, BufferType.SPANNABLE); 1021 Selection.setSelection((Spannable) mTextView.getText(), 3); 1022 mTextView.setTextKeepState(shortString); 1023 assertEquals(shortString, mTextView.getText().toString()); 1024 assertEquals(3, mTextView.getSelectionStart()); 1025 assertEquals(3, mTextView.getSelectionEnd()); 1026 1027 // selection is at the exact place which is outside the short string 1028 mTextView.setText(longString); 1029 Selection.setSelection((Spannable) mTextView.getText(), shortString.length() + 1); 1030 mTextView.setTextKeepState(shortString); 1031 assertEquals(shortString, mTextView.getText().toString()); 1032 assertEquals(shortString.length(), mTextView.getSelectionStart()); 1033 assertEquals(shortString.length(), mTextView.getSelectionEnd()); 1034 1035 // select the sub string which is inside the short string 1036 mTextView.setText(longString); 1037 Selection.setSelection((Spannable) mTextView.getText(), 1, 4); 1038 mTextView.setTextKeepState(shortString); 1039 assertEquals(shortString, mTextView.getText().toString()); 1040 assertEquals(1, mTextView.getSelectionStart()); 1041 assertEquals(4, mTextView.getSelectionEnd()); 1042 1043 // select the sub string which ends outside the short string 1044 mTextView.setText(longString); 1045 Selection.setSelection((Spannable) mTextView.getText(), 2, shortString.length() + 1); 1046 mTextView.setTextKeepState(shortString); 1047 assertEquals(shortString, mTextView.getText().toString()); 1048 assertEquals(2, mTextView.getSelectionStart()); 1049 assertEquals(shortString.length(), mTextView.getSelectionEnd()); 1050 1051 // select the sub string which is outside the short string 1052 mTextView.setText(longString); 1053 Selection.setSelection((Spannable) mTextView.getText(), 1054 shortString.length() + 1, shortString.length() + 3); 1055 mTextView.setTextKeepState(shortString); 1056 assertEquals(shortString, mTextView.getText().toString()); 1057 assertEquals(shortString.length(), mTextView.getSelectionStart()); 1058 assertEquals(shortString.length(), mTextView.getSelectionEnd()); 1059 } 1060 1061 @UiThreadTest 1062 public void testGetEditableText() { 1063 TextView tv = findTextView(R.id.textview_text); 1064 1065 String text = "Hello"; 1066 tv.setText(text, BufferType.EDITABLE); 1067 assertEquals(text, tv.getText().toString()); 1068 assertTrue(tv.getText() instanceof Editable); 1069 assertEquals(text, tv.getEditableText().toString()); 1070 1071 tv.setText(text, BufferType.SPANNABLE); 1072 assertEquals(text, tv.getText().toString()); 1073 assertTrue(tv.getText() instanceof Spannable); 1074 assertNull(tv.getEditableText()); 1075 1076 tv.setText(null, BufferType.EDITABLE); 1077 assertEquals("", tv.getText().toString()); 1078 assertTrue(tv.getText() instanceof Editable); 1079 assertEquals("", tv.getEditableText().toString()); 1080 1081 tv.setText(null, BufferType.SPANNABLE); 1082 assertEquals("", tv.getText().toString()); 1083 assertTrue(tv.getText() instanceof Spannable); 1084 assertNull(tv.getEditableText()); 1085 } 1086 1087 @UiThreadTest 1088 public void testSetText2() { 1089 String string = "This is a test for setting text content by char array"; 1090 char[] input = string.toCharArray(); 1091 TextView tv = findTextView(R.id.textview_text); 1092 1093 tv.setText(input, 0, input.length); 1094 assertEquals(string, tv.getText().toString()); 1095 1096 tv.setText(input, 0, 5); 1097 assertEquals(string.substring(0, 5), tv.getText().toString()); 1098 1099 try { 1100 tv.setText(input, -1, input.length); 1101 fail("Should throw exception if the start position is negative!"); 1102 } catch (IndexOutOfBoundsException exception) { 1103 } 1104 1105 try { 1106 tv.setText(input, 0, -1); 1107 fail("Should throw exception if the length is negative!"); 1108 } catch (IndexOutOfBoundsException exception) { 1109 } 1110 1111 try { 1112 tv.setText(input, 1, input.length); 1113 fail("Should throw exception if the end position is out of index!"); 1114 } catch (IndexOutOfBoundsException exception) { 1115 } 1116 1117 tv.setText(input, 1, 0); 1118 assertEquals("", tv.getText().toString()); 1119 } 1120 1121 @UiThreadTest 1122 public void testSetText1() { 1123 mTextView = findTextView(R.id.textview_text); 1124 1125 String longString = "very long content"; 1126 String shortString = "short"; 1127 1128 // selection is at the exact place which is inside the short string 1129 mTextView.setText(longString, BufferType.SPANNABLE); 1130 Selection.setSelection((Spannable) mTextView.getText(), 3); 1131 mTextView.setTextKeepState(shortString, BufferType.EDITABLE); 1132 assertTrue(mTextView.getText() instanceof Editable); 1133 assertEquals(shortString, mTextView.getText().toString()); 1134 assertEquals(shortString, mTextView.getEditableText().toString()); 1135 assertEquals(3, mTextView.getSelectionStart()); 1136 assertEquals(3, mTextView.getSelectionEnd()); 1137 1138 mTextView.setText(shortString, BufferType.EDITABLE); 1139 assertTrue(mTextView.getText() instanceof Editable); 1140 assertEquals(shortString, mTextView.getText().toString()); 1141 assertEquals(shortString, mTextView.getEditableText().toString()); 1142 // there is no selection. 1143 assertEquals(-1, mTextView.getSelectionStart()); 1144 assertEquals(-1, mTextView.getSelectionEnd()); 1145 1146 // selection is at the exact place which is outside the short string 1147 mTextView.setText(longString); 1148 Selection.setSelection((Spannable) mTextView.getText(), longString.length()); 1149 mTextView.setTextKeepState(shortString, BufferType.EDITABLE); 1150 assertTrue(mTextView.getText() instanceof Editable); 1151 assertEquals(shortString, mTextView.getText().toString()); 1152 assertEquals(shortString, mTextView.getEditableText().toString()); 1153 assertEquals(shortString.length(), mTextView.getSelectionStart()); 1154 assertEquals(shortString.length(), mTextView.getSelectionEnd()); 1155 1156 mTextView.setText(shortString, BufferType.EDITABLE); 1157 assertTrue(mTextView.getText() instanceof Editable); 1158 assertEquals(shortString, mTextView.getText().toString()); 1159 assertEquals(shortString, mTextView.getEditableText().toString()); 1160 // there is no selection. 1161 assertEquals(-1, mTextView.getSelectionStart()); 1162 assertEquals(-1, mTextView.getSelectionEnd()); 1163 1164 // select the sub string which is inside the short string 1165 mTextView.setText(longString); 1166 Selection.setSelection((Spannable) mTextView.getText(), 1, shortString.length() - 1); 1167 mTextView.setTextKeepState(shortString, BufferType.EDITABLE); 1168 assertTrue(mTextView.getText() instanceof Editable); 1169 assertEquals(shortString, mTextView.getText().toString()); 1170 assertEquals(shortString, mTextView.getEditableText().toString()); 1171 assertEquals(1, mTextView.getSelectionStart()); 1172 assertEquals(shortString.length() - 1, mTextView.getSelectionEnd()); 1173 1174 mTextView.setText(shortString, BufferType.EDITABLE); 1175 assertTrue(mTextView.getText() instanceof Editable); 1176 assertEquals(shortString, mTextView.getText().toString()); 1177 assertEquals(shortString, mTextView.getEditableText().toString()); 1178 // there is no selection. 1179 assertEquals(-1, mTextView.getSelectionStart()); 1180 assertEquals(-1, mTextView.getSelectionEnd()); 1181 1182 // select the sub string which ends outside the short string 1183 mTextView.setText(longString); 1184 Selection.setSelection((Spannable) mTextView.getText(), 2, longString.length()); 1185 mTextView.setTextKeepState(shortString, BufferType.EDITABLE); 1186 assertTrue(mTextView.getText() instanceof Editable); 1187 assertEquals(shortString, mTextView.getText().toString()); 1188 assertEquals(shortString, mTextView.getEditableText().toString()); 1189 assertEquals(2, mTextView.getSelectionStart()); 1190 assertEquals(shortString.length(), mTextView.getSelectionEnd()); 1191 1192 mTextView.setText(shortString, BufferType.EDITABLE); 1193 assertTrue(mTextView.getText() instanceof Editable); 1194 assertEquals(shortString, mTextView.getText().toString()); 1195 assertEquals(shortString, mTextView.getEditableText().toString()); 1196 // there is no selection. 1197 assertEquals(-1, mTextView.getSelectionStart()); 1198 assertEquals(-1, mTextView.getSelectionEnd()); 1199 1200 // select the sub string which is outside the short string 1201 mTextView.setText(longString); 1202 Selection.setSelection((Spannable) mTextView.getText(), 1203 shortString.length() + 1, shortString.length() + 3); 1204 mTextView.setTextKeepState(shortString, BufferType.EDITABLE); 1205 assertTrue(mTextView.getText() instanceof Editable); 1206 assertEquals(shortString, mTextView.getText().toString()); 1207 assertEquals(shortString, mTextView.getEditableText().toString()); 1208 assertEquals(shortString.length(), mTextView.getSelectionStart()); 1209 assertEquals(shortString.length(), mTextView.getSelectionEnd()); 1210 1211 mTextView.setText(shortString, BufferType.EDITABLE); 1212 assertTrue(mTextView.getText() instanceof Editable); 1213 assertEquals(shortString, mTextView.getText().toString()); 1214 assertEquals(shortString, mTextView.getEditableText().toString()); 1215 // there is no selection. 1216 assertEquals(-1, mTextView.getSelectionStart()); 1217 assertEquals(-1, mTextView.getSelectionEnd()); 1218 } 1219 1220 @UiThreadTest 1221 public void testSetText3() { 1222 TextView tv = findTextView(R.id.textview_text); 1223 1224 int resId = R.string.text_view_hint; 1225 String result = mActivity.getResources().getString(resId); 1226 1227 tv.setText(resId); 1228 assertEquals(result, tv.getText().toString()); 1229 1230 try { 1231 tv.setText(-1); 1232 fail("Should throw exception with illegal id"); 1233 } catch (NotFoundException e) { 1234 } 1235 } 1236 1237 @UiThreadTest 1238 public void testSetText() { 1239 TextView tv = findTextView(R.id.textview_text); 1240 1241 int resId = R.string.text_view_hint; 1242 String result = mActivity.getResources().getString(resId); 1243 1244 tv.setText(resId, BufferType.EDITABLE); 1245 assertEquals(result, tv.getText().toString()); 1246 assertTrue(tv.getText() instanceof Editable); 1247 1248 tv.setText(resId, BufferType.SPANNABLE); 1249 assertEquals(result, tv.getText().toString()); 1250 assertTrue(tv.getText() instanceof Spannable); 1251 1252 try { 1253 tv.setText(-1, BufferType.EDITABLE); 1254 fail("Should throw exception with illegal id"); 1255 } catch (NotFoundException e) { 1256 } 1257 } 1258 1259 @UiThreadTest 1260 public void testAccessHint() { 1261 mActivity.setContentView(R.layout.textview_hint_linksclickable_freezestext); 1262 1263 mTextView = findTextView(R.id.hint_linksClickable_freezesText_default); 1264 assertNull(mTextView.getHint()); 1265 1266 mTextView = findTextView(R.id.hint_blank); 1267 assertEquals("", mTextView.getHint()); 1268 1269 mTextView = findTextView(R.id.hint_string); 1270 assertEquals(mActivity.getResources().getString(R.string.text_view_simple_hint), 1271 mTextView.getHint()); 1272 1273 mTextView = findTextView(R.id.hint_resid); 1274 assertEquals(mActivity.getResources().getString(R.string.text_view_hint), 1275 mTextView.getHint()); 1276 1277 mTextView.setHint("This is hint"); 1278 assertEquals("This is hint", mTextView.getHint().toString()); 1279 1280 mTextView.setHint(R.string.text_view_hello); 1281 assertEquals(mActivity.getResources().getString(R.string.text_view_hello), 1282 mTextView.getHint().toString()); 1283 1284 // Non-exist resid 1285 try { 1286 mTextView.setHint(-1); 1287 fail("Should throw exception if id is illegal"); 1288 } catch (NotFoundException e) { 1289 } 1290 } 1291 1292 public void testAccessError() { 1293 mTextView = findTextView(R.id.textview_text); 1294 assertNull(mTextView.getError()); 1295 1296 final String errorText = "Opps! There is an error"; 1297 1298 mActivity.runOnUiThread(new Runnable() { 1299 public void run() { 1300 mTextView.setError(null); 1301 } 1302 }); 1303 mInstrumentation.waitForIdleSync(); 1304 assertNull(mTextView.getError()); 1305 1306 final Drawable icon = mActivity.getResources().getDrawable(R.drawable.failed); 1307 mActivity.runOnUiThread(new Runnable() { 1308 public void run() { 1309 mTextView.setError(errorText, icon); 1310 } 1311 }); 1312 mInstrumentation.waitForIdleSync(); 1313 assertEquals(errorText, mTextView.getError().toString()); 1314 // can not check whether the drawable is set correctly 1315 1316 mActivity.runOnUiThread(new Runnable() { 1317 public void run() { 1318 mTextView.setError(null, null); 1319 } 1320 }); 1321 mInstrumentation.waitForIdleSync(); 1322 assertNull(mTextView.getError()); 1323 1324 mActivity.runOnUiThread(new Runnable() { 1325 public void run() { 1326 mTextView.setKeyListener(DigitsKeyListener.getInstance("")); 1327 mTextView.setText("", BufferType.EDITABLE); 1328 mTextView.setError(errorText); 1329 mTextView.requestFocus(); 1330 } 1331 }); 1332 mInstrumentation.waitForIdleSync(); 1333 1334 assertEquals(errorText, mTextView.getError().toString()); 1335 1336 mInstrumentation.sendStringSync("a"); 1337 // a key event that will not change the TextView's text 1338 assertEquals("", mTextView.getText().toString()); 1339 // The icon and error message will not be reset to null 1340 assertNull(mTextView.getError()); 1341 1342 mActivity.runOnUiThread(new Runnable() { 1343 public void run() { 1344 mTextView.setKeyListener(DigitsKeyListener.getInstance()); 1345 mTextView.setText("", BufferType.EDITABLE); 1346 mTextView.setError(errorText); 1347 mTextView.requestFocus(); 1348 } 1349 }); 1350 mInstrumentation.waitForIdleSync(); 1351 1352 mInstrumentation.sendStringSync("1"); 1353 // a key event cause changes to the TextView's text 1354 assertEquals("1", mTextView.getText().toString()); 1355 // the error message and icon will be cleared. 1356 assertNull(mTextView.getError()); 1357 } 1358 1359 public void testAccessFilters() { 1360 final InputFilter[] expected = { new InputFilter.AllCaps(), 1361 new InputFilter.LengthFilter(2) }; 1362 1363 final QwertyKeyListener qwertyKeyListener 1364 = QwertyKeyListener.getInstance(false, Capitalize.NONE); 1365 mActivity.runOnUiThread(new Runnable() { 1366 public void run() { 1367 mTextView = findTextView(R.id.textview_text); 1368 mTextView.setKeyListener(qwertyKeyListener); 1369 mTextView.setText("", BufferType.EDITABLE); 1370 mTextView.setFilters(expected); 1371 mTextView.requestFocus(); 1372 } 1373 }); 1374 mInstrumentation.waitForIdleSync(); 1375 1376 assertSame(expected, mTextView.getFilters()); 1377 1378 mInstrumentation.sendStringSync("a"); 1379 // the text is capitalized by InputFilter.AllCaps 1380 assertEquals("A", mTextView.getText().toString()); 1381 mInstrumentation.sendStringSync("b"); 1382 // the text is capitalized by InputFilter.AllCaps 1383 assertEquals("AB", mTextView.getText().toString()); 1384 mInstrumentation.sendStringSync("c"); 1385 // 'C' could not be accepted, because there is a length filter. 1386 assertEquals("AB", mTextView.getText().toString()); 1387 1388 try { 1389 mTextView.setFilters(null); 1390 fail("Should throw IllegalArgumentException!"); 1391 } catch (IllegalArgumentException e) { 1392 } 1393 } 1394 1395 public void testGetFocusedRect() { 1396 Rect rc = new Rect(); 1397 1398 // Basic 1399 mTextView = new TextView(mActivity); 1400 mTextView.getFocusedRect(rc); 1401 assertEquals(mTextView.getScrollX(), rc.left); 1402 assertEquals(mTextView.getScrollX() + mTextView.getWidth(), rc.right); 1403 assertEquals(mTextView.getScrollY(), rc.top); 1404 assertEquals(mTextView.getScrollY() + mTextView.getHeight(), rc.bottom); 1405 1406 // Single line 1407 mTextView = findTextView(R.id.textview_text); 1408 mTextView.getFocusedRect(rc); 1409 assertEquals(mTextView.getScrollX(), rc.left); 1410 assertEquals(mTextView.getScrollX() + mTextView.getWidth(), rc.right); 1411 assertEquals(mTextView.getScrollY(), rc.top); 1412 assertEquals(mTextView.getScrollY() + mTextView.getHeight(), rc.bottom); 1413 1414 mActivity.runOnUiThread(new Runnable() { 1415 public void run() { 1416 mTextView.setSelected(true); 1417 SpannableString text = new SpannableString(mTextView.getText()); 1418 Selection.setSelection(text, 3, 13); 1419 mTextView.setText(text); 1420 } 1421 }); 1422 mInstrumentation.waitForIdleSync(); 1423 mTextView.getFocusedRect(rc); 1424 assertNotNull(mTextView.getLayout()); 1425 assertEquals(mTextView.getLayout().getPrimaryHorizontal(3), (float) rc.left); 1426 assertEquals(mTextView.getLayout().getPrimaryHorizontal(13), (float) rc.right); 1427 assertEquals(mTextView.getLayout().getLineTop(0), rc.top); 1428 assertEquals(mTextView.getLayout().getLineBottom(0), rc.bottom); 1429 1430 mActivity.runOnUiThread(new Runnable() { 1431 public void run() { 1432 mTextView.setSelected(true); 1433 SpannableString text = new SpannableString(mTextView.getText()); 1434 Selection.setSelection(text, 13, 3); 1435 mTextView.setText(text); 1436 } 1437 }); 1438 mInstrumentation.waitForIdleSync(); 1439 mTextView.getFocusedRect(rc); 1440 assertNotNull(mTextView.getLayout()); 1441 assertEquals(mTextView.getLayout().getPrimaryHorizontal(3) - 2, (float) rc.left); 1442 assertEquals(mTextView.getLayout().getPrimaryHorizontal(3) + 2, (float) rc.right); 1443 assertEquals(mTextView.getLayout().getLineTop(0), rc.top); 1444 assertEquals(mTextView.getLayout().getLineBottom(0), rc.bottom); 1445 1446 // Multi lines 1447 mTextView = findTextView(R.id.textview_text_two_lines); 1448 mTextView.getFocusedRect(rc); 1449 assertEquals(mTextView.getScrollX(), rc.left); 1450 assertEquals(mTextView.getScrollX() + mTextView.getWidth(), rc.right); 1451 assertEquals(mTextView.getScrollY(), rc.top); 1452 assertEquals(mTextView.getScrollY() + mTextView.getHeight(), rc.bottom); 1453 1454 mActivity.runOnUiThread(new Runnable() { 1455 public void run() { 1456 mTextView.setSelected(true); 1457 SpannableString text = new SpannableString(mTextView.getText()); 1458 Selection.setSelection(text, 2, 4); 1459 mTextView.setText(text); 1460 } 1461 }); 1462 mInstrumentation.waitForIdleSync(); 1463 mTextView.getFocusedRect(rc); 1464 assertNotNull(mTextView.getLayout()); 1465 assertEquals(mTextView.getLayout().getPrimaryHorizontal(2), (float) rc.left); 1466 assertEquals(mTextView.getLayout().getPrimaryHorizontal(4), (float) rc.right); 1467 assertEquals(mTextView.getLayout().getLineTop(0), rc.top); 1468 assertEquals(mTextView.getLayout().getLineBottom(0), rc.bottom); 1469 1470 mActivity.runOnUiThread(new Runnable() { 1471 public void run() { 1472 mTextView.setSelected(true); 1473 SpannableString text = new SpannableString(mTextView.getText()); 1474 Selection.setSelection(text, 2, 10); // cross the "\n" and two lines 1475 mTextView.setText(text); 1476 } 1477 }); 1478 mInstrumentation.waitForIdleSync(); 1479 mTextView.getFocusedRect(rc); 1480 Path path = new Path(); 1481 mTextView.getLayout().getSelectionPath(2, 10, path); 1482 RectF rcf = new RectF(); 1483 path.computeBounds(rcf, true); 1484 assertNotNull(mTextView.getLayout()); 1485 assertEquals(rcf.left - 1, (float) rc.left); 1486 assertEquals(rcf.right + 1, (float) rc.right); 1487 assertEquals(mTextView.getLayout().getLineTop(0), rc.top); 1488 assertEquals(mTextView.getLayout().getLineBottom(1), rc.bottom); 1489 1490 // Exception 1491 try { 1492 mTextView.getFocusedRect(null); 1493 fail("Should throw NullPointerException!"); 1494 } catch (NullPointerException e) { 1495 } 1496 } 1497 1498 public void testGetLineCount() { 1499 mTextView = findTextView(R.id.textview_text); 1500 // this is an one line text with default setting. 1501 assertEquals(1, mTextView.getLineCount()); 1502 1503 // make it multi-lines 1504 setMaxWidth(mTextView.getWidth() / 3); 1505 assertTrue(1 < mTextView.getLineCount()); 1506 1507 // make it to an one line 1508 setMaxWidth(Integer.MAX_VALUE); 1509 assertEquals(1, mTextView.getLineCount()); 1510 1511 // set min lines don't effect the lines count for actual text. 1512 setMinLines(12); 1513 assertEquals(1, mTextView.getLineCount()); 1514 1515 mTextView = new TextView(mActivity); 1516 // the internal Layout has not been built. 1517 assertNull(mTextView.getLayout()); 1518 assertEquals(0, mTextView.getLineCount()); 1519 } 1520 1521 public void testGetLineBounds() { 1522 Rect rc = new Rect(); 1523 mTextView = new TextView(mActivity); 1524 assertEquals(0, mTextView.getLineBounds(0, null)); 1525 1526 assertEquals(0, mTextView.getLineBounds(0, rc)); 1527 assertEquals(0, rc.left); 1528 assertEquals(0, rc.right); 1529 assertEquals(0, rc.top); 1530 assertEquals(0, rc.bottom); 1531 1532 mTextView = findTextView(R.id.textview_text); 1533 assertEquals(mTextView.getBaseline(), mTextView.getLineBounds(0, null)); 1534 1535 assertEquals(mTextView.getBaseline(), mTextView.getLineBounds(0, rc)); 1536 assertEquals(0, rc.left); 1537 assertEquals(mTextView.getWidth(), rc.right); 1538 assertEquals(0, rc.top); 1539 assertEquals(mTextView.getHeight(), rc.bottom); 1540 1541 mActivity.runOnUiThread(new Runnable() { 1542 public void run() { 1543 mTextView.setPadding(1, 2, 3, 4); 1544 mTextView.setGravity(Gravity.BOTTOM); 1545 } 1546 }); 1547 mInstrumentation.waitForIdleSync(); 1548 assertEquals(mTextView.getBaseline(), mTextView.getLineBounds(0, rc)); 1549 assertEquals(mTextView.getTotalPaddingLeft(), rc.left); 1550 assertEquals(mTextView.getWidth() - mTextView.getTotalPaddingRight(), rc.right); 1551 assertEquals(mTextView.getTotalPaddingTop(), rc.top); 1552 assertEquals(mTextView.getHeight() - mTextView.getTotalPaddingBottom(), rc.bottom); 1553 } 1554 1555 public void testGetBaseLine() { 1556 mTextView = new TextView(mActivity); 1557 assertEquals(-1, mTextView.getBaseline()); 1558 1559 mTextView = findTextView(R.id.textview_text); 1560 assertEquals(mTextView.getLayout().getLineBaseline(0), mTextView.getBaseline()); 1561 1562 mActivity.runOnUiThread(new Runnable() { 1563 public void run() { 1564 mTextView.setPadding(1, 2, 3, 4); 1565 mTextView.setGravity(Gravity.BOTTOM); 1566 } 1567 }); 1568 mInstrumentation.waitForIdleSync(); 1569 int expected = mTextView.getTotalPaddingTop() + mTextView.getLayout().getLineBaseline(0); 1570 assertEquals(expected, mTextView.getBaseline()); 1571 } 1572 1573 public void testPressKey() { 1574 final QwertyKeyListener qwertyKeyListener 1575 = QwertyKeyListener.getInstance(false, Capitalize.NONE); 1576 mActivity.runOnUiThread(new Runnable() { 1577 public void run() { 1578 mTextView = findTextView(R.id.textview_text); 1579 mTextView.setKeyListener(qwertyKeyListener); 1580 mTextView.setText("", BufferType.EDITABLE); 1581 mTextView.requestFocus(); 1582 } 1583 }); 1584 mInstrumentation.waitForIdleSync(); 1585 1586 mInstrumentation.sendStringSync("a"); 1587 assertEquals("a", mTextView.getText().toString()); 1588 mInstrumentation.sendStringSync("b"); 1589 assertEquals("ab", mTextView.getText().toString()); 1590 sendKeys(KeyEvent.KEYCODE_DEL); 1591 assertEquals("a", mTextView.getText().toString()); 1592 } 1593 1594 public void testSetIncludeFontPadding() { 1595 mTextView = findTextView(R.id.textview_text); 1596 mActivity.runOnUiThread(new Runnable() { 1597 public void run() { 1598 mTextView.setWidth(mTextView.getWidth() / 3); 1599 mTextView.setPadding(1, 2, 3, 4); 1600 mTextView.setGravity(Gravity.BOTTOM); 1601 } 1602 }); 1603 mInstrumentation.waitForIdleSync(); 1604 1605 int oldHeight = mTextView.getHeight(); 1606 mActivity.runOnUiThread(new Runnable() { 1607 public void run() { 1608 mTextView.setIncludeFontPadding(false); 1609 } 1610 }); 1611 mInstrumentation.waitForIdleSync(); 1612 1613 assertTrue(mTextView.getHeight() < oldHeight); 1614 } 1615 1616 public void testScroll() { 1617 mTextView = new TextView(mActivity); 1618 1619 assertEquals(0, mTextView.getScrollX()); 1620 assertEquals(0, mTextView.getScrollY()); 1621 1622 //don't set the Scroller, nothing changed. 1623 mTextView.computeScroll(); 1624 assertEquals(0, mTextView.getScrollX()); 1625 assertEquals(0, mTextView.getScrollY()); 1626 1627 //set the Scroller 1628 Scroller s = new Scroller(mActivity); 1629 assertNotNull(s); 1630 s.startScroll(0, 0, 320, 480, 0); 1631 s.abortAnimation(); 1632 s.forceFinished(false); 1633 mTextView.setScroller(s); 1634 1635 mTextView.computeScroll(); 1636 assertEquals(320, mTextView.getScrollX()); 1637 assertEquals(480, mTextView.getScrollY()); 1638 } 1639 1640 public void testDebug() { 1641 mTextView = new TextView(mActivity); 1642 mTextView.debug(0); 1643 1644 mTextView.setText("Hello!"); 1645 layout(mTextView); 1646 mTextView.debug(1); 1647 } 1648 1649 public void testSelection() { 1650 mTextView = new TextView(mActivity); 1651 String text = "This is the content"; 1652 mTextView.setText(text, BufferType.SPANNABLE); 1653 assertFalse(mTextView.hasSelection()); 1654 1655 Selection.selectAll((Spannable) mTextView.getText()); 1656 assertEquals(0, mTextView.getSelectionStart()); 1657 assertEquals(text.length(), mTextView.getSelectionEnd()); 1658 assertTrue(mTextView.hasSelection()); 1659 1660 int selectionStart = 5; 1661 int selectionEnd = 7; 1662 Selection.setSelection((Spannable) mTextView.getText(), selectionStart); 1663 assertEquals(selectionStart, mTextView.getSelectionStart()); 1664 assertEquals(selectionStart, mTextView.getSelectionEnd()); 1665 assertFalse(mTextView.hasSelection()); 1666 1667 Selection.setSelection((Spannable) mTextView.getText(), selectionStart, selectionEnd); 1668 assertEquals(selectionStart, mTextView.getSelectionStart()); 1669 assertEquals(selectionEnd, mTextView.getSelectionEnd()); 1670 assertTrue(mTextView.hasSelection()); 1671 } 1672 1673 @UiThreadTest 1674 public void testAccessEllipsize() { 1675 mActivity.setContentView(R.layout.textview_ellipsize); 1676 1677 mTextView = findTextView(R.id.ellipsize_default); 1678 assertNull(mTextView.getEllipsize()); 1679 1680 mTextView = findTextView(R.id.ellipsize_none); 1681 assertNull(mTextView.getEllipsize()); 1682 1683 mTextView = findTextView(R.id.ellipsize_start); 1684 assertSame(TruncateAt.START, mTextView.getEllipsize()); 1685 1686 mTextView = findTextView(R.id.ellipsize_middle); 1687 assertSame(TruncateAt.MIDDLE, mTextView.getEllipsize()); 1688 1689 mTextView = findTextView(R.id.ellipsize_end); 1690 assertSame(TruncateAt.END, mTextView.getEllipsize()); 1691 1692 mTextView.setEllipsize(TextUtils.TruncateAt.START); 1693 assertSame(TextUtils.TruncateAt.START, mTextView.getEllipsize()); 1694 1695 mTextView.setEllipsize(TextUtils.TruncateAt.MIDDLE); 1696 assertSame(TextUtils.TruncateAt.MIDDLE, mTextView.getEllipsize()); 1697 1698 mTextView.setEllipsize(TextUtils.TruncateAt.END); 1699 assertSame(TextUtils.TruncateAt.END, mTextView.getEllipsize()); 1700 1701 mTextView.setEllipsize(null); 1702 assertNull(mTextView.getEllipsize()); 1703 1704 mTextView.setWidth(10); 1705 mTextView.setEllipsize(TextUtils.TruncateAt.START); 1706 mTextView.setText("ThisIsAVeryLongVeryLongVeryLongVeryLongVeryLongWord"); 1707 mTextView.invalidate(); 1708 1709 assertSame(TextUtils.TruncateAt.START, mTextView.getEllipsize()); 1710 // there is no method to check if '...yLongVeryLongWord' is painted in the screen. 1711 } 1712 1713 public void testSetCursorVisible() { 1714 mTextView = new TextView(mActivity); 1715 1716 mTextView.setCursorVisible(true); 1717 mTextView.setCursorVisible(false); 1718 } 1719 1720 public void testOnWindowFocusChanged() { 1721 // Do not test. Implementation details. 1722 } 1723 1724 public void testOnTouchEvent() { 1725 // Do not test. Implementation details. 1726 } 1727 1728 public void testOnTrackballEvent() { 1729 // Do not test. Implementation details. 1730 } 1731 1732 public void testGetTextColors() { 1733 // TODO: How to get a suitable TypedArray to test this method. 1734 } 1735 1736 public void testOnKeyShortcut() { 1737 // Do not test. Implementation details. 1738 } 1739 1740 @UiThreadTest 1741 public void testPerformLongClick() { 1742 mTextView = findTextView(R.id.textview_text); 1743 mTextView.setText("This is content"); 1744 MockOnLongClickListener onLongClickListener = new MockOnLongClickListener(true); 1745 MockOnCreateContextMenuListener onCreateContextMenuListener 1746 = new MockOnCreateContextMenuListener(false); 1747 mTextView.setOnLongClickListener(onLongClickListener); 1748 mTextView.setOnCreateContextMenuListener(onCreateContextMenuListener); 1749 assertTrue(mTextView.performLongClick()); 1750 assertTrue(onLongClickListener.hasLongClicked()); 1751 assertFalse(onCreateContextMenuListener.hasCreatedContextMenu()); 1752 1753 onLongClickListener = new MockOnLongClickListener(false); 1754 mTextView.setOnLongClickListener(onLongClickListener); 1755 mTextView.setOnCreateContextMenuListener(onCreateContextMenuListener); 1756 assertTrue(mTextView.performLongClick()); 1757 assertTrue(onLongClickListener.hasLongClicked()); 1758 assertTrue(onCreateContextMenuListener.hasCreatedContextMenu()); 1759 1760 mTextView.setOnLongClickListener(null); 1761 onCreateContextMenuListener = new MockOnCreateContextMenuListener(true); 1762 mTextView.setOnCreateContextMenuListener(onCreateContextMenuListener); 1763 assertFalse(mTextView.performLongClick()); 1764 assertTrue(onCreateContextMenuListener.hasCreatedContextMenu()); 1765 } 1766 1767 @UiThreadTest 1768 public void testTextAttr() { 1769 mTextView = findTextView(R.id.textview_textAttr); 1770 // getText 1771 assertEquals(mActivity.getString(R.string.text_view_hello), mTextView.getText().toString()); 1772 1773 // getCurrentTextColor 1774 assertEquals(mActivity.getResources().getColor(R.drawable.black), 1775 mTextView.getCurrentTextColor()); 1776 assertEquals(mActivity.getResources().getColor(R.drawable.red), 1777 mTextView.getCurrentHintTextColor()); 1778 assertEquals(mActivity.getResources().getColor(R.drawable.red), 1779 mTextView.getHintTextColors().getDefaultColor()); 1780 assertEquals(mActivity.getResources().getColor(R.drawable.blue), 1781 mTextView.getLinkTextColors().getDefaultColor()); 1782 1783 // getTextScaleX 1784 assertEquals(1.2f, mTextView.getTextScaleX(), 0.01f); 1785 1786 // setTextScaleX 1787 mTextView.setTextScaleX(2.4f); 1788 assertEquals(2.4f, mTextView.getTextScaleX(), 0.01f); 1789 1790 mTextView.setTextScaleX(0f); 1791 assertEquals(0f, mTextView.getTextScaleX(), 0.01f); 1792 1793 mTextView.setTextScaleX(- 2.4f); 1794 assertEquals(- 2.4f, mTextView.getTextScaleX(), 0.01f); 1795 1796 // getTextSize 1797 assertEquals(20f, mTextView.getTextSize(), 0.01f); 1798 1799 // getTypeface 1800 // getTypeface will be null if android:typeface is set to normal, 1801 // and android:style is not set or is set to normal 1802 assertNull(mTextView.getTypeface()); 1803 1804 mTextView.setTypeface(Typeface.DEFAULT); 1805 assertSame(Typeface.DEFAULT, mTextView.getTypeface()); 1806 // null type face 1807 mTextView.setTypeface(null); 1808 assertNull(mTextView.getTypeface()); 1809 1810 // default type face, bold style, note: the type face will be changed 1811 // after call set method 1812 mTextView.setTypeface(Typeface.DEFAULT, Typeface.BOLD); 1813 assertSame(Typeface.BOLD, mTextView.getTypeface().getStyle()); 1814 1815 // null type face, BOLD style 1816 mTextView.setTypeface(null, Typeface.BOLD); 1817 assertSame(Typeface.BOLD, mTextView.getTypeface().getStyle()); 1818 1819 // old type face, null style 1820 mTextView.setTypeface(Typeface.DEFAULT, 0); 1821 assertEquals(Typeface.NORMAL, mTextView.getTypeface().getStyle()); 1822 } 1823 1824 @UiThreadTest 1825 public void testAppend() { 1826 mTextView = new TextView(mActivity); 1827 1828 // 1: check the original length, should be blank as initialised. 1829 assertEquals(0, mTextView.getText().length()); 1830 1831 // 2: append a string use append(CharSquence) into the original blank 1832 // buffer, check the content. And upgrading it to BufferType.EDITABLE if it was 1833 // not already editable. 1834 assertFalse(mTextView.getText() instanceof Editable); 1835 mTextView.append("Append."); 1836 assertEquals("Append.", mTextView.getText().toString()); 1837 assertTrue(mTextView.getText() instanceof Editable); 1838 1839 // 3: append a string from 0~3. 1840 mTextView.append("Append", 0, 3); 1841 assertEquals("Append.App", mTextView.getText().toString()); 1842 assertTrue(mTextView.getText() instanceof Editable); 1843 1844 // 4: append a string from 0~0, nothing will be append as expected. 1845 mTextView.append("Append", 0, 0); 1846 assertEquals("Append.App", mTextView.getText().toString()); 1847 assertTrue(mTextView.getText() instanceof Editable); 1848 1849 // 5: append a string from -3~3. check the wrong left edge. 1850 try { 1851 mTextView.append("Append", -3, 3); 1852 fail("Should throw StringIndexOutOfBoundsException"); 1853 } catch (StringIndexOutOfBoundsException e) { 1854 } 1855 1856 // 6: append a string from 3~10. check the wrong right edge. 1857 try { 1858 mTextView.append("Append", 3, 10); 1859 fail("Should throw StringIndexOutOfBoundsException"); 1860 } catch (StringIndexOutOfBoundsException e) { 1861 } 1862 1863 // 7: append a null string. 1864 try { 1865 mTextView.append(null); 1866 fail("Should throw NullPointerException"); 1867 } catch (NullPointerException e) { 1868 } 1869 } 1870 1871 public void testAccessTransformationMethod() { 1872 // check the password attribute in xml 1873 mTextView = findTextView(R.id.textview_password); 1874 assertNotNull(mTextView); 1875 assertSame(PasswordTransformationMethod.getInstance(), 1876 mTextView.getTransformationMethod()); 1877 1878 // check the singleLine attribute in xml 1879 mTextView = findTextView(R.id.textview_singleLine); 1880 assertNotNull(mTextView); 1881 assertSame(SingleLineTransformationMethod.getInstance(), 1882 mTextView.getTransformationMethod()); 1883 1884 final QwertyKeyListener qwertyKeyListener = QwertyKeyListener.getInstance(false, 1885 Capitalize.NONE); 1886 final TransformationMethod method = PasswordTransformationMethod.getInstance(); 1887 // change transformation method by function 1888 mActivity.runOnUiThread(new Runnable() { 1889 public void run() { 1890 mTextView.setKeyListener(qwertyKeyListener); 1891 mTextView.setTransformationMethod(method); 1892 mTransformedText = method.getTransformation(mTextView.getText(), mTextView); 1893 1894 mTextView.requestFocus(); 1895 } 1896 }); 1897 mInstrumentation.waitForIdleSync(); 1898 assertSame(PasswordTransformationMethod.getInstance(), 1899 mTextView.getTransformationMethod()); 1900 1901 sendKeys("H E 2*L O"); 1902 mActivity.runOnUiThread(new Runnable() { 1903 public void run() { 1904 mTextView.append(" "); 1905 } 1906 }); 1907 mInstrumentation.waitForIdleSync(); 1908 1909 // it will get transformed after a while 1910 new PollingCheck(TIMEOUT) { 1911 @Override 1912 protected boolean check() { 1913 // "******" 1914 return mTransformedText.toString() 1915 .equals("\u2022\u2022\u2022\u2022\u2022\u2022"); 1916 } 1917 }.run(); 1918 1919 // set null 1920 mActivity.runOnUiThread(new Runnable() { 1921 public void run() { 1922 mTextView.setTransformationMethod(null); 1923 } 1924 }); 1925 mInstrumentation.waitForIdleSync(); 1926 assertNull(mTextView.getTransformationMethod()); 1927 } 1928 1929 @UiThreadTest 1930 public void testCompound() { 1931 mTextView = new TextView(mActivity); 1932 int padding = 3; 1933 Drawable[] drawables = mTextView.getCompoundDrawables(); 1934 assertNull(drawables[0]); 1935 assertNull(drawables[1]); 1936 assertNull(drawables[2]); 1937 assertNull(drawables[3]); 1938 1939 // test setCompoundDrawablePadding and getCompoundDrawablePadding 1940 mTextView.setCompoundDrawablePadding(padding); 1941 assertEquals(padding, mTextView.getCompoundDrawablePadding()); 1942 1943 // using resid, 0 represents null 1944 mTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.start, R.drawable.pass, 1945 R.drawable.failed, 0); 1946 drawables = mTextView.getCompoundDrawables(); 1947 1948 // drawableLeft 1949 WidgetTestUtils.assertEquals(getBitmap(R.drawable.start), 1950 ((BitmapDrawable) drawables[0]).getBitmap()); 1951 // drawableTop 1952 WidgetTestUtils.assertEquals(getBitmap(R.drawable.pass), 1953 ((BitmapDrawable) drawables[1]).getBitmap()); 1954 // drawableRight 1955 WidgetTestUtils.assertEquals(getBitmap(R.drawable.failed), 1956 ((BitmapDrawable) drawables[2]).getBitmap()); 1957 // drawableBottom 1958 assertNull(drawables[3]); 1959 1960 Drawable left = mActivity.getResources().getDrawable(R.drawable.blue); 1961 Drawable right = mActivity.getResources().getDrawable(R.drawable.yellow); 1962 Drawable top = mActivity.getResources().getDrawable(R.drawable.red); 1963 1964 // using drawables directly 1965 mTextView.setCompoundDrawablesWithIntrinsicBounds(left, top, right, null); 1966 drawables = mTextView.getCompoundDrawables(); 1967 1968 // drawableLeft 1969 assertSame(left, drawables[0]); 1970 // drawableTop 1971 assertSame(top, drawables[1]); 1972 // drawableRight 1973 assertSame(right, drawables[2]); 1974 // drawableBottom 1975 assertNull(drawables[3]); 1976 1977 // check compound padding 1978 assertEquals(mTextView.getPaddingLeft() + padding + left.getIntrinsicWidth(), 1979 mTextView.getCompoundPaddingLeft()); 1980 assertEquals(mTextView.getPaddingTop() + padding + top.getIntrinsicHeight(), 1981 mTextView.getCompoundPaddingTop()); 1982 assertEquals(mTextView.getPaddingRight() + padding + right.getIntrinsicWidth(), 1983 mTextView.getCompoundPaddingRight()); 1984 assertEquals(mTextView.getPaddingBottom(), mTextView.getCompoundPaddingBottom()); 1985 1986 // set bounds to drawables and set them again. 1987 left.setBounds(0, 0, 1, 2); 1988 right.setBounds(0, 0, 3, 4); 1989 top.setBounds(0, 0, 5, 6); 1990 // usinf drawables 1991 mTextView.setCompoundDrawables(left, top, right, null); 1992 drawables = mTextView.getCompoundDrawables(); 1993 1994 // drawableLeft 1995 assertSame(left, drawables[0]); 1996 // drawableTop 1997 assertSame(top, drawables[1]); 1998 // drawableRight 1999 assertSame(right, drawables[2]); 2000 // drawableBottom 2001 assertNull(drawables[3]); 2002 2003 // check compound padding 2004 assertEquals(mTextView.getPaddingLeft() + padding + left.getBounds().width(), 2005 mTextView.getCompoundPaddingLeft()); 2006 assertEquals(mTextView.getPaddingTop() + padding + top.getBounds().height(), 2007 mTextView.getCompoundPaddingTop()); 2008 assertEquals(mTextView.getPaddingRight() + padding + right.getBounds().width(), 2009 mTextView.getCompoundPaddingRight()); 2010 assertEquals(mTextView.getPaddingBottom(), mTextView.getCompoundPaddingBottom()); 2011 } 2012 2013 public void testSingleLine() { 2014 final TextView textView = new TextView(mActivity); 2015 setSpannableText(textView, "This is a really long sentence" 2016 + " which can not be placed in one line on the screen."); 2017 2018 // Narrow layout assures that the text will get wrapped. 2019 FrameLayout innerLayout = new FrameLayout(mActivity); 2020 innerLayout.setLayoutParams(new ViewGroup.LayoutParams(100, 100)); 2021 innerLayout.addView(textView); 2022 2023 final FrameLayout layout = new FrameLayout(mActivity); 2024 layout.addView(innerLayout); 2025 2026 mActivity.runOnUiThread(new Runnable() { 2027 public void run() { 2028 mActivity.setContentView(layout); 2029 textView.setSingleLine(true); 2030 } 2031 }); 2032 mInstrumentation.waitForIdleSync(); 2033 2034 assertEquals(SingleLineTransformationMethod.getInstance(), 2035 textView.getTransformationMethod()); 2036 int singleLineWidth = textView.getLayout().getWidth(); 2037 int singleLineHeight = textView.getLayout().getHeight(); 2038 2039 mActivity.runOnUiThread(new Runnable() { 2040 public void run() { 2041 textView.setSingleLine(false); 2042 } 2043 }); 2044 mInstrumentation.waitForIdleSync(); 2045 assertEquals(null, textView.getTransformationMethod()); 2046 assertTrue(textView.getLayout().getHeight() > singleLineHeight); 2047 assertTrue(textView.getLayout().getWidth() < singleLineWidth); 2048 2049 // same behaviours as setSingLine(true) 2050 mActivity.runOnUiThread(new Runnable() { 2051 public void run() { 2052 textView.setSingleLine(); 2053 } 2054 }); 2055 mInstrumentation.waitForIdleSync(); 2056 assertEquals(SingleLineTransformationMethod.getInstance(), 2057 textView.getTransformationMethod()); 2058 assertEquals(singleLineHeight, textView.getLayout().getHeight()); 2059 assertEquals(singleLineWidth, textView.getLayout().getWidth()); 2060 } 2061 2062 @UiThreadTest 2063 public void testSetMaxLines() { 2064 mTextView = findTextView(R.id.textview_text); 2065 2066 float[] widths = new float[LONG_TEXT.length()]; 2067 mTextView.getPaint().getTextWidths(LONG_TEXT, widths); 2068 float totalWidth = 0.0f; 2069 for (float f : widths) { 2070 totalWidth += f; 2071 } 2072 final int stringWidth = (int) totalWidth; 2073 mTextView.setWidth(stringWidth >> 2); 2074 mTextView.setText(LONG_TEXT); 2075 2076 final int maxLines = 2; 2077 assertTrue(mTextView.getLineCount() > maxLines); 2078 2079 mTextView.setMaxLines(maxLines); 2080 mTextView.requestLayout(); 2081 2082 assertTrue(mTextView.getHeight() <= maxLines * mTextView.getLineHeight()); 2083 } 2084 2085 @UiThreadTest 2086 public void testSetMaxLinesException() { 2087 mTextView = new TextView(mActivity); 2088 mActivity.setContentView(mTextView); 2089 mTextView.setWidth(mTextView.getWidth() >> 3); 2090 mTextView.setMaxLines(-1); 2091 } 2092 2093 public void testSetMinLines() { 2094 mTextView = findTextView(R.id.textview_text); 2095 setWidth(mTextView.getWidth() >> 3); 2096 int originalHeight = mTextView.getHeight(); 2097 int originalLines = mTextView.getLineCount(); 2098 2099 setMinLines(originalLines - 1); 2100 assertTrue((originalLines - 1) * mTextView.getLineHeight() <= mTextView.getHeight()); 2101 2102 setMinLines(originalLines + 1); 2103 assertTrue((originalLines + 1) * mTextView.getLineHeight() <= mTextView.getHeight()); 2104 } 2105 2106 public void testSetLines() { 2107 mTextView = findTextView(R.id.textview_text); 2108 // make it multiple lines 2109 setWidth(mTextView.getWidth() >> 3); 2110 int originalLines = mTextView.getLineCount(); 2111 2112 setLines(originalLines - 1); 2113 assertTrue((originalLines - 1) * mTextView.getLineHeight() <= mTextView.getHeight()); 2114 2115 setLines(originalLines + 1); 2116 assertTrue((originalLines + 1) * mTextView.getLineHeight() <= mTextView.getHeight()); 2117 } 2118 2119 @UiThreadTest 2120 public void testSetLinesException() { 2121 mTextView = new TextView(mActivity); 2122 mActivity.setContentView(mTextView); 2123 mTextView.setWidth(mTextView.getWidth() >> 3); 2124 mTextView.setLines(-1); 2125 } 2126 2127 @UiThreadTest 2128 public void testGetExtendedPaddingTop() { 2129 mTextView = findTextView(R.id.textview_text); 2130 // Initialized value 2131 assertEquals(0, mTextView.getExtendedPaddingTop()); 2132 2133 // After Set a Drawable 2134 final Drawable top = mActivity.getResources().getDrawable(R.drawable.red); 2135 top.setBounds(0, 0, 100, 10); 2136 mTextView.setCompoundDrawables(null, top, null, null); 2137 assertEquals(mTextView.getCompoundPaddingTop(), mTextView.getExtendedPaddingTop()); 2138 2139 // Change line count 2140 mTextView.setLines(mTextView.getLineCount() - 1); 2141 mTextView.setGravity(Gravity.BOTTOM); 2142 2143 assertTrue(mTextView.getExtendedPaddingTop() > 0); 2144 } 2145 2146 @UiThreadTest 2147 public void testGetExtendedPaddingBottom() { 2148 mTextView = findTextView(R.id.textview_text); 2149 // Initialized value 2150 assertEquals(0, mTextView.getExtendedPaddingBottom()); 2151 2152 // After Set a Drawable 2153 final Drawable bottom = mActivity.getResources().getDrawable(R.drawable.red); 2154 bottom.setBounds(0, 0, 100, 10); 2155 mTextView.setCompoundDrawables(null, null, null, bottom); 2156 assertEquals(mTextView.getCompoundPaddingBottom(), mTextView.getExtendedPaddingBottom()); 2157 2158 // Change line count 2159 mTextView.setLines(mTextView.getLineCount() - 1); 2160 mTextView.setGravity(Gravity.CENTER_VERTICAL); 2161 2162 assertTrue(mTextView.getExtendedPaddingBottom() > 0); 2163 } 2164 2165 public void testGetTotalPaddingTop() { 2166 mTextView = findTextView(R.id.textview_text); 2167 // Initialized value 2168 assertEquals(0, mTextView.getTotalPaddingTop()); 2169 2170 // After Set a Drawable 2171 final Drawable top = mActivity.getResources().getDrawable(R.drawable.red); 2172 top.setBounds(0, 0, 100, 10); 2173 mActivity.runOnUiThread(new Runnable() { 2174 public void run() { 2175 mTextView.setCompoundDrawables(null, top, null, null); 2176 mTextView.setLines(mTextView.getLineCount() - 1); 2177 mTextView.setGravity(Gravity.BOTTOM); 2178 } 2179 }); 2180 mInstrumentation.waitForIdleSync(); 2181 assertEquals(mTextView.getExtendedPaddingTop(), mTextView.getTotalPaddingTop()); 2182 2183 // Change line count 2184 setLines(mTextView.getLineCount() + 1); 2185 int expected = mTextView.getHeight() 2186 - mTextView.getExtendedPaddingBottom() 2187 - mTextView.getLayout().getLineTop(mTextView.getLineCount()); 2188 assertEquals(expected, mTextView.getTotalPaddingTop()); 2189 } 2190 2191 public void testGetTotalPaddingBottom() { 2192 mTextView = findTextView(R.id.textview_text); 2193 // Initialized value 2194 assertEquals(0, mTextView.getTotalPaddingBottom()); 2195 2196 // After Set a Drawable 2197 final Drawable bottom = mActivity.getResources().getDrawable(R.drawable.red); 2198 bottom.setBounds(0, 0, 100, 10); 2199 mActivity.runOnUiThread(new Runnable() { 2200 public void run() { 2201 mTextView.setCompoundDrawables(null, null, null, bottom); 2202 mTextView.setLines(mTextView.getLineCount() - 1); 2203 mTextView.setGravity(Gravity.CENTER_VERTICAL); 2204 } 2205 }); 2206 mInstrumentation.waitForIdleSync(); 2207 assertEquals(mTextView.getExtendedPaddingBottom(), mTextView.getTotalPaddingBottom()); 2208 2209 // Change line count 2210 setLines(mTextView.getLineCount() + 1); 2211 int expected = ((mTextView.getHeight() 2212 - mTextView.getExtendedPaddingBottom() 2213 - mTextView.getExtendedPaddingTop() 2214 - mTextView.getLayout().getLineBottom(mTextView.getLineCount())) >> 1) 2215 + mTextView.getExtendedPaddingBottom(); 2216 assertEquals(expected, mTextView.getTotalPaddingBottom()); 2217 } 2218 2219 @UiThreadTest 2220 public void testGetTotalPaddingLeft() { 2221 mTextView = findTextView(R.id.textview_text); 2222 // Initialized value 2223 assertEquals(0, mTextView.getTotalPaddingLeft()); 2224 2225 // After Set a Drawable 2226 Drawable left = mActivity.getResources().getDrawable(R.drawable.red); 2227 left.setBounds(0, 0, 10, 100); 2228 mTextView.setCompoundDrawables(left, null, null, null); 2229 mTextView.setGravity(Gravity.RIGHT); 2230 assertEquals(mTextView.getCompoundPaddingLeft(), mTextView.getTotalPaddingLeft()); 2231 2232 // Change width 2233 mTextView.setWidth(Integer.MAX_VALUE); 2234 assertEquals(mTextView.getCompoundPaddingLeft(), mTextView.getTotalPaddingLeft()); 2235 } 2236 2237 @UiThreadTest 2238 public void testGetTotalPaddingRight() { 2239 mTextView = findTextView(R.id.textview_text); 2240 // Initialized value 2241 assertEquals(0, mTextView.getTotalPaddingRight()); 2242 2243 // After Set a Drawable 2244 Drawable right = mActivity.getResources().getDrawable(R.drawable.red); 2245 right.setBounds(0, 0, 10, 100); 2246 mTextView.setCompoundDrawables(null, null, right, null); 2247 mTextView.setGravity(Gravity.CENTER_HORIZONTAL); 2248 assertEquals(mTextView.getCompoundPaddingRight(), mTextView.getTotalPaddingRight()); 2249 2250 // Change width 2251 mTextView.setWidth(Integer.MAX_VALUE); 2252 assertEquals(mTextView.getCompoundPaddingRight(), mTextView.getTotalPaddingRight()); 2253 } 2254 2255 public void testGetUrls() { 2256 mTextView = new TextView(mActivity); 2257 2258 URLSpan[] spans = mTextView.getUrls(); 2259 assertEquals(0, spans.length); 2260 2261 String url = "http://www.google.com"; 2262 String email = "name (at) gmail.com"; 2263 String string = url + " mailto:" + email; 2264 SpannableString spannable = new SpannableString(string); 2265 spannable.setSpan(new URLSpan(url), 0, url.length(), 0); 2266 mTextView.setText(spannable, BufferType.SPANNABLE); 2267 spans = mTextView.getUrls(); 2268 assertEquals(1, spans.length); 2269 assertEquals(url, spans[0].getURL()); 2270 2271 spannable.setSpan(new URLSpan(email), 0, email.length(), 0); 2272 mTextView.setText(spannable, BufferType.SPANNABLE); 2273 2274 spans = mTextView.getUrls(); 2275 assertEquals(2, spans.length); 2276 assertEquals(url, spans[0].getURL()); 2277 assertEquals(email, spans[1].getURL()); 2278 2279 // test the situation that param what is not a URLSpan 2280 spannable.setSpan(new Object(), 0, 9, 0); 2281 mTextView.setText(spannable, BufferType.SPANNABLE); 2282 spans = mTextView.getUrls(); 2283 assertEquals(2, spans.length); 2284 } 2285 2286 public void testSetPadding() { 2287 mTextView = new TextView(mActivity); 2288 2289 mTextView.setPadding(0, 1, 2, 4); 2290 assertEquals(0, mTextView.getPaddingLeft()); 2291 assertEquals(1, mTextView.getPaddingTop()); 2292 assertEquals(2, mTextView.getPaddingRight()); 2293 assertEquals(4, mTextView.getPaddingBottom()); 2294 2295 mTextView.setPadding(10, 20, 30, 40); 2296 assertEquals(10, mTextView.getPaddingLeft()); 2297 assertEquals(20, mTextView.getPaddingTop()); 2298 assertEquals(30, mTextView.getPaddingRight()); 2299 assertEquals(40, mTextView.getPaddingBottom()); 2300 } 2301 2302 public void testSetTextAppearance() { 2303 mTextView = new TextView(mActivity); 2304 2305 mTextView.setTextAppearance(mActivity, R.style.TextAppearance_All); 2306 assertEquals(mActivity.getResources().getColor(R.drawable.black), 2307 mTextView.getCurrentTextColor()); 2308 assertEquals(20f, mTextView.getTextSize(), 0.01f); 2309 assertEquals(Typeface.BOLD, mTextView.getTypeface().getStyle()); 2310 assertEquals(mActivity.getResources().getColor(R.drawable.red), 2311 mTextView.getCurrentHintTextColor()); 2312 assertEquals(mActivity.getResources().getColor(R.drawable.blue), 2313 mTextView.getLinkTextColors().getDefaultColor()); 2314 2315 mTextView.setTextAppearance(mActivity, R.style.TextAppearance_Colors); 2316 assertEquals(mActivity.getResources().getColor(R.drawable.black), 2317 mTextView.getCurrentTextColor()); 2318 assertEquals(mActivity.getResources().getColor(R.drawable.blue), 2319 mTextView.getCurrentHintTextColor()); 2320 assertEquals(mActivity.getResources().getColor(R.drawable.yellow), 2321 mTextView.getLinkTextColors().getDefaultColor()); 2322 2323 mTextView.setTextAppearance(mActivity, R.style.TextAppearance_NotColors); 2324 assertEquals(17f, mTextView.getTextSize(), 0.01f); 2325 assertEquals(Typeface.NORMAL, mTextView.getTypeface().getStyle()); 2326 2327 mTextView.setTextAppearance(mActivity, R.style.TextAppearance_Style); 2328 assertEquals(null, mTextView.getTypeface()); 2329 } 2330 2331 public void testOnPreDraw() { 2332 // Do not test. Implementation details. 2333 } 2334 2335 public void testSetHorizontallyScrolling() { 2336 // make the text view has more than one line 2337 mTextView = findTextView(R.id.textview_text); 2338 setWidth(mTextView.getWidth() >> 1); 2339 assertTrue(mTextView.getLineCount() > 1); 2340 2341 setHorizontallyScrolling(true); 2342 assertEquals(1, mTextView.getLineCount()); 2343 2344 setHorizontallyScrolling(false); 2345 assertTrue(mTextView.getLineCount() > 1); 2346 } 2347 2348 public void testComputeHorizontalScrollRange() { 2349 MockTextView textView = new MockTextView(mActivity); 2350 // test when layout is null 2351 assertNull(textView.getLayout()); 2352 assertEquals(textView.getWidth(), textView.computeHorizontalScrollRange()); 2353 2354 textView.setFrame(0, 0, 40, 50); 2355 assertEquals(textView.getWidth(), textView.computeHorizontalScrollRange()); 2356 2357 // set the layout 2358 layout(textView); 2359 assertEquals(textView.getLayout().getWidth(), textView.computeHorizontalScrollRange()); 2360 } 2361 2362 public void testComputeVerticalScrollRange() { 2363 MockTextView textView = new MockTextView(mActivity); 2364 // test when layout is null 2365 assertNull(textView.getLayout()); 2366 assertEquals(0, textView.computeVerticalScrollRange()); 2367 2368 textView.setFrame(0, 0, 40, 50); 2369 assertEquals(textView.getHeight(), textView.computeVerticalScrollRange()); 2370 2371 //set the layout 2372 layout(textView); 2373 assertEquals(textView.getLayout().getHeight(), textView.computeVerticalScrollRange()); 2374 } 2375 2376 public void testDrawableStateChanged() { 2377 MockTextView textView = new MockTextView(mActivity); 2378 2379 textView.reset(); 2380 textView.refreshDrawableState(); 2381 assertTrue(textView.hasCalledDrawableStateChanged()); 2382 } 2383 2384 public void testGetDefaultEditable() { 2385 MockTextView textView = new MockTextView(mActivity); 2386 2387 //the TextView#getDefaultEditable() does nothing, and always return false. 2388 assertFalse(textView.getDefaultEditable()); 2389 } 2390 2391 public void testGetDefaultMovementMethod() { 2392 MockTextView textView = new MockTextView(mActivity); 2393 2394 //the TextView#getDefaultMovementMethod() does nothing, and always return null. 2395 assertNull(textView.getDefaultMovementMethod()); 2396 } 2397 2398 public void testOnCreateContextMenu() { 2399 // Do not test. Implementation details. 2400 } 2401 2402 public void testOnDetachedFromWindow() { 2403 // Do not test. Implementation details. 2404 } 2405 2406 public void testOnDraw() { 2407 // Do not test. Implementation details. 2408 } 2409 2410 public void testOnFocusChanged() { 2411 // Do not test. Implementation details. 2412 } 2413 2414 public void testOnMeasure() { 2415 // Do not test. Implementation details. 2416 } 2417 2418 public void testOnTextChanged() { 2419 // Do not test. Implementation details. 2420 } 2421 2422 public void testSetFrame() { 2423 MockTextView textView = new MockTextView(mActivity); 2424 2425 //Assign a new size to this view 2426 assertTrue(textView.setFrame(0, 0, 320, 480)); 2427 assertEquals(0, textView.getFrameLeft()); 2428 assertEquals(0, textView.getFrameTop()); 2429 assertEquals(320, textView.getFrameRight()); 2430 assertEquals(480, textView.getFrameBottom()); 2431 2432 //Assign a same size to this view 2433 assertFalse(textView.setFrame(0, 0, 320, 480)); 2434 2435 //negative input 2436 assertTrue(textView.setFrame(-1, -1, -1, -1)); 2437 assertEquals(-1, textView.getFrameLeft()); 2438 assertEquals(-1, textView.getFrameTop()); 2439 assertEquals(-1, textView.getFrameRight()); 2440 assertEquals(-1, textView.getFrameBottom()); 2441 } 2442 2443 public void testGetFadingEdgeStrength() { 2444 final MockTextView textViewLeft = (MockTextView) mActivity.findViewById( 2445 R.id.mock_textview_left); 2446 mActivity.runOnUiThread(new Runnable() { 2447 public void run() { 2448 textViewLeft.setEllipsize(null); 2449 } 2450 }); 2451 mInstrumentation.waitForIdleSync(); 2452 2453 // fading is shown on right side if the text aligns left 2454 assertEquals(0.0f, textViewLeft.getLeftFadingEdgeStrength(), 0.01f); 2455 assertEquals(1.0f, textViewLeft.getRightFadingEdgeStrength(), 0.01f); 2456 2457 final MockTextView textViewRight = (MockTextView) mActivity.findViewById( 2458 R.id.mock_textview_right); 2459 mActivity.runOnUiThread(new Runnable() { 2460 public void run() { 2461 textViewRight.setEllipsize(null); 2462 } 2463 }); 2464 mInstrumentation.waitForIdleSync(); 2465 // fading is shown on left side if the text aligns right 2466 assertEquals(1.0f, textViewRight.getLeftFadingEdgeStrength(), 0.01f); 2467 assertEquals(0.0f, textViewRight.getRightFadingEdgeStrength(), 0.01f); 2468 2469 final MockTextView textViewCenter = (MockTextView) mActivity.findViewById( 2470 R.id.mock_textview_center); 2471 mActivity.runOnUiThread(new Runnable() { 2472 public void run() { 2473 textViewCenter.setEllipsize(null); 2474 } 2475 }); 2476 mInstrumentation.waitForIdleSync(); 2477 // fading is shown on both sides if the text aligns center 2478 assertEquals(1.0f, textViewCenter.getLeftFadingEdgeStrength(), 0.01f); 2479 assertEquals(1.0f, textViewCenter.getRightFadingEdgeStrength(), 0.01f); 2480 } 2481 2482 2483 public void testMarquee() { 2484 final MockTextView textView = new MockTextView(mActivity); 2485 textView.setText(LONG_TEXT); 2486 textView.setSingleLine(); 2487 textView.setEllipsize(TruncateAt.MARQUEE); 2488 textView.setLayoutParams(new ViewGroup.LayoutParams(100, 100)); 2489 2490 final FrameLayout layout = new FrameLayout(mActivity); 2491 layout.addView(textView); 2492 2493 // make the fading to be shown 2494 textView.setHorizontalFadingEdgeEnabled(true); 2495 2496 mActivity.runOnUiThread(new Runnable() { 2497 public void run() { 2498 mActivity.setContentView(layout); 2499 } 2500 }); 2501 mInstrumentation.waitForIdleSync(); 2502 2503 TestSelectedRunnable runnable = new TestSelectedRunnable(textView) { 2504 public void run() { 2505 textView.setMarqueeRepeatLimit(-1); 2506 // force the marquee to start 2507 saveIsSelected1(); 2508 textView.setSelected(true); 2509 saveIsSelected2(); 2510 } 2511 }; 2512 mActivity.runOnUiThread(runnable); 2513 2514 // wait for the marquee to run 2515 // fading is shown on both sides if the marquee runs for a while 2516 new PollingCheck(TIMEOUT) { 2517 @Override 2518 protected boolean check() { 2519 return textView.getLeftFadingEdgeStrength() > 0.0f 2520 && textView.getRightFadingEdgeStrength() > 0.0f; 2521 } 2522 }.run(); 2523 2524 final float leftFadingEdgeStrength = textView.getLeftFadingEdgeStrength(); 2525 final float rightFadingEdgeStrength = textView.getRightFadingEdgeStrength(); 2526 2527 // wait for the marquee to continue 2528 // the left fading becomes thicker while the right fading becomes thiner 2529 // as the text moves towards left 2530 new PollingCheck(TIMEOUT) { 2531 @Override 2532 protected boolean check() { 2533 return leftFadingEdgeStrength < textView.getLeftFadingEdgeStrength() 2534 && rightFadingEdgeStrength > textView.getRightFadingEdgeStrength(); 2535 } 2536 }.run(); 2537 assertFalse(runnable.getIsSelected1()); 2538 assertTrue(runnable.getIsSelected2()); 2539 2540 runnable = new TestSelectedRunnable(textView) { 2541 public void run() { 2542 textView.setMarqueeRepeatLimit(0); 2543 // force the marquee to stop 2544 saveIsSelected1(); 2545 textView.setSelected(false); 2546 saveIsSelected2(); 2547 textView.setGravity(Gravity.LEFT); 2548 } 2549 }; 2550 // force the marquee to stop 2551 mActivity.runOnUiThread(runnable); 2552 mInstrumentation.waitForIdleSync(); 2553 assertTrue(runnable.getIsSelected1()); 2554 assertFalse(runnable.getIsSelected2()); 2555 assertEquals(0.0f, textView.getLeftFadingEdgeStrength(), 0.01f); 2556 assertTrue(textView.getRightFadingEdgeStrength() > 0.0f); 2557 2558 mActivity.runOnUiThread(new Runnable() { 2559 public void run() { 2560 textView.setGravity(Gravity.RIGHT); 2561 } 2562 }); 2563 mInstrumentation.waitForIdleSync(); 2564 assertTrue(textView.getLeftFadingEdgeStrength() > 0.0f); 2565 assertEquals(0.0f, textView.getRightFadingEdgeStrength(), 0.01f); 2566 2567 mActivity.runOnUiThread(new Runnable() { 2568 public void run() { 2569 textView.setGravity(Gravity.CENTER_HORIZONTAL); 2570 } 2571 }); 2572 mInstrumentation.waitForIdleSync(); 2573 // there is no left fading (Is it correct?) 2574 assertEquals(0.0f, textView.getLeftFadingEdgeStrength(), 0.01f); 2575 assertTrue(textView.getRightFadingEdgeStrength() > 0.0f); 2576 } 2577 2578 public void testOnKeyMultiple() { 2579 // Do not test. Implementation details. 2580 } 2581 2582 public void testAccessInputExtras() throws XmlPullParserException, IOException { 2583 TextView textView = new TextView(mActivity); 2584 textView.setText(null, BufferType.EDITABLE); 2585 textView.setInputType(InputType.TYPE_CLASS_TEXT); 2586 2587 // do not create the extras 2588 assertNull(textView.getInputExtras(false)); 2589 2590 // create if it does not exist 2591 Bundle inputExtras = textView.getInputExtras(true); 2592 assertNotNull(inputExtras); 2593 assertTrue(inputExtras.isEmpty()); 2594 2595 // it is created already 2596 assertNotNull(textView.getInputExtras(false)); 2597 2598 try { 2599 textView.setInputExtras(R.xml.input_extras); 2600 fail("Should throw NullPointerException!"); 2601 } catch (NullPointerException e) { 2602 } 2603 } 2604 2605 public void testAccessContentType() { 2606 TextView textView = new TextView(mActivity); 2607 textView.setText(null, BufferType.EDITABLE); 2608 textView.setKeyListener(null); 2609 textView.setTransformationMethod(null); 2610 2611 textView.setInputType(InputType.TYPE_CLASS_DATETIME 2612 | InputType.TYPE_DATETIME_VARIATION_NORMAL); 2613 assertEquals(InputType.TYPE_CLASS_DATETIME 2614 | InputType.TYPE_DATETIME_VARIATION_NORMAL, textView.getInputType()); 2615 assertTrue(textView.getKeyListener() instanceof DateTimeKeyListener); 2616 2617 textView.setInputType(InputType.TYPE_CLASS_DATETIME 2618 | InputType.TYPE_DATETIME_VARIATION_DATE); 2619 assertEquals(InputType.TYPE_CLASS_DATETIME 2620 | InputType.TYPE_DATETIME_VARIATION_DATE, textView.getInputType()); 2621 assertTrue(textView.getKeyListener() instanceof DateKeyListener); 2622 2623 textView.setInputType(InputType.TYPE_CLASS_DATETIME 2624 | InputType.TYPE_DATETIME_VARIATION_TIME); 2625 assertEquals(InputType.TYPE_CLASS_DATETIME 2626 | InputType.TYPE_DATETIME_VARIATION_TIME, textView.getInputType()); 2627 assertTrue(textView.getKeyListener() instanceof TimeKeyListener); 2628 2629 textView.setInputType(InputType.TYPE_CLASS_NUMBER 2630 | InputType.TYPE_NUMBER_FLAG_DECIMAL 2631 | InputType.TYPE_NUMBER_FLAG_SIGNED); 2632 assertEquals(InputType.TYPE_CLASS_NUMBER 2633 | InputType.TYPE_NUMBER_FLAG_DECIMAL 2634 | InputType.TYPE_NUMBER_FLAG_SIGNED, textView.getInputType()); 2635 assertSame(textView.getKeyListener(), DigitsKeyListener.getInstance(true, true)); 2636 2637 textView.setInputType(InputType.TYPE_CLASS_PHONE); 2638 assertEquals(InputType.TYPE_CLASS_PHONE, textView.getInputType()); 2639 assertTrue(textView.getKeyListener() instanceof DialerKeyListener); 2640 2641 textView.setInputType(InputType.TYPE_CLASS_TEXT 2642 | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT); 2643 assertEquals(InputType.TYPE_CLASS_TEXT 2644 | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT, textView.getInputType()); 2645 assertSame(textView.getKeyListener(), TextKeyListener.getInstance(true, Capitalize.NONE)); 2646 2647 textView.setSingleLine(); 2648 assertTrue(textView.getTransformationMethod() instanceof SingleLineTransformationMethod); 2649 textView.setInputType(InputType.TYPE_CLASS_TEXT 2650 | InputType.TYPE_TEXT_FLAG_MULTI_LINE 2651 | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS); 2652 assertEquals(InputType.TYPE_CLASS_TEXT 2653 | InputType.TYPE_TEXT_FLAG_MULTI_LINE 2654 | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS, textView.getInputType()); 2655 assertSame(textView.getKeyListener(), 2656 TextKeyListener.getInstance(false, Capitalize.CHARACTERS)); 2657 assertNull(textView.getTransformationMethod()); 2658 2659 textView.setInputType(InputType.TYPE_CLASS_TEXT 2660 | InputType.TYPE_TEXT_FLAG_CAP_WORDS); 2661 assertEquals(InputType.TYPE_CLASS_TEXT 2662 | InputType.TYPE_TEXT_FLAG_CAP_WORDS, textView.getInputType()); 2663 assertSame(textView.getKeyListener(), 2664 TextKeyListener.getInstance(false, Capitalize.WORDS)); 2665 assertTrue(textView.getTransformationMethod() instanceof SingleLineTransformationMethod); 2666 2667 textView.setInputType(InputType.TYPE_CLASS_TEXT 2668 | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES); 2669 assertEquals(InputType.TYPE_CLASS_TEXT 2670 | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES, textView.getInputType()); 2671 assertSame(textView.getKeyListener(), 2672 TextKeyListener.getInstance(false, Capitalize.SENTENCES)); 2673 2674 textView.setInputType(InputType.TYPE_NULL); 2675 assertEquals(InputType.TYPE_NULL, textView.getInputType()); 2676 assertTrue(textView.getKeyListener() instanceof TextKeyListener); 2677 } 2678 2679 public void testAccessRawContentType() { 2680 TextView textView = new TextView(mActivity); 2681 textView.setText(null, BufferType.EDITABLE); 2682 textView.setKeyListener(null); 2683 textView.setTransformationMethod(null); 2684 2685 textView.setRawInputType(InputType.TYPE_CLASS_DATETIME 2686 | InputType.TYPE_DATETIME_VARIATION_NORMAL); 2687 assertEquals(InputType.TYPE_CLASS_DATETIME 2688 | InputType.TYPE_DATETIME_VARIATION_NORMAL, textView.getInputType()); 2689 assertNull(textView.getTransformationMethod()); 2690 assertNull(textView.getKeyListener()); 2691 2692 textView.setRawInputType(InputType.TYPE_CLASS_DATETIME 2693 | InputType.TYPE_DATETIME_VARIATION_DATE); 2694 assertEquals(InputType.TYPE_CLASS_DATETIME 2695 | InputType.TYPE_DATETIME_VARIATION_DATE, textView.getInputType()); 2696 assertNull(textView.getTransformationMethod()); 2697 assertNull(textView.getKeyListener()); 2698 2699 textView.setRawInputType(InputType.TYPE_CLASS_DATETIME 2700 | InputType.TYPE_DATETIME_VARIATION_TIME); 2701 assertEquals(InputType.TYPE_CLASS_DATETIME 2702 | InputType.TYPE_DATETIME_VARIATION_TIME, textView.getInputType()); 2703 assertNull(textView.getTransformationMethod()); 2704 assertNull(textView.getKeyListener()); 2705 2706 textView.setRawInputType(InputType.TYPE_CLASS_NUMBER 2707 | InputType.TYPE_NUMBER_FLAG_DECIMAL 2708 | InputType.TYPE_NUMBER_FLAG_SIGNED); 2709 assertEquals(InputType.TYPE_CLASS_NUMBER 2710 | InputType.TYPE_NUMBER_FLAG_DECIMAL 2711 | InputType.TYPE_NUMBER_FLAG_SIGNED, textView.getInputType()); 2712 assertNull(textView.getTransformationMethod()); 2713 assertNull(textView.getKeyListener()); 2714 2715 textView.setRawInputType(InputType.TYPE_CLASS_PHONE); 2716 assertEquals(InputType.TYPE_CLASS_PHONE, textView.getInputType()); 2717 assertNull(textView.getTransformationMethod()); 2718 assertNull(textView.getKeyListener()); 2719 2720 textView.setRawInputType(InputType.TYPE_CLASS_TEXT 2721 | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT); 2722 assertEquals(InputType.TYPE_CLASS_TEXT 2723 | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT, textView.getInputType()); 2724 assertNull(textView.getTransformationMethod()); 2725 assertNull(textView.getKeyListener()); 2726 2727 textView.setSingleLine(); 2728 assertTrue(textView.getTransformationMethod() instanceof SingleLineTransformationMethod); 2729 textView.setRawInputType(InputType.TYPE_CLASS_TEXT 2730 | InputType.TYPE_TEXT_FLAG_MULTI_LINE 2731 | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS); 2732 assertEquals(InputType.TYPE_CLASS_TEXT 2733 | InputType.TYPE_TEXT_FLAG_MULTI_LINE 2734 | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS, textView.getInputType()); 2735 assertTrue(textView.getTransformationMethod() instanceof SingleLineTransformationMethod); 2736 assertNull(textView.getKeyListener()); 2737 2738 textView.setRawInputType(InputType.TYPE_CLASS_TEXT 2739 | InputType.TYPE_TEXT_FLAG_CAP_WORDS); 2740 assertEquals(InputType.TYPE_CLASS_TEXT 2741 | InputType.TYPE_TEXT_FLAG_CAP_WORDS, textView.getInputType()); 2742 assertTrue(textView.getTransformationMethod() instanceof SingleLineTransformationMethod); 2743 assertNull(textView.getKeyListener()); 2744 2745 textView.setRawInputType(InputType.TYPE_CLASS_TEXT 2746 | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES); 2747 assertEquals(InputType.TYPE_CLASS_TEXT 2748 | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES, textView.getInputType()); 2749 assertTrue(textView.getTransformationMethod() instanceof SingleLineTransformationMethod); 2750 assertNull(textView.getKeyListener()); 2751 2752 textView.setRawInputType(InputType.TYPE_NULL); 2753 assertTrue(textView.getTransformationMethod() instanceof SingleLineTransformationMethod); 2754 assertNull(textView.getKeyListener()); 2755 } 2756 2757 public void testOnPrivateIMECommand() { 2758 // Do not test. Implementation details. 2759 } 2760 2761 public void testFoo() { 2762 // Do not test. Implementation details. 2763 } 2764 2765 public void testVerifyDrawable() { 2766 MockTextView textView = new MockTextView(mActivity); 2767 2768 Drawable d = mActivity.getResources().getDrawable(R.drawable.pass); 2769 assertFalse(textView.verifyDrawable(d)); 2770 2771 textView.setCompoundDrawables(null, d, null, null); 2772 assertTrue(textView.verifyDrawable(d)); 2773 } 2774 2775 public void testAccessPrivateImeOptions() { 2776 mTextView = findTextView(R.id.textview_text); 2777 assertNull(mTextView.getPrivateImeOptions()); 2778 2779 mTextView.setPrivateImeOptions("com.example.myapp.SpecialMode=3"); 2780 assertEquals("com.example.myapp.SpecialMode=3", mTextView.getPrivateImeOptions()); 2781 2782 mTextView.setPrivateImeOptions(null); 2783 assertNull(mTextView.getPrivateImeOptions()); 2784 } 2785 2786 public void testSetOnEditorActionListener() { 2787 mTextView = findTextView(R.id.textview_text); 2788 2789 MockOnEditorActionListener listener = new MockOnEditorActionListener(); 2790 assertFalse(listener.isOnEditorActionCalled()); 2791 2792 mTextView.setOnEditorActionListener(listener); 2793 assertFalse(listener.isOnEditorActionCalled()); 2794 2795 mTextView.onEditorAction(EditorInfo.IME_ACTION_DONE); 2796 assertTrue(listener.isOnEditorActionCalled()); 2797 } 2798 2799 public void testAccessImeOptions() { 2800 mTextView = findTextView(R.id.textview_text); 2801 assertEquals(EditorInfo.IME_NULL, mTextView.getImeOptions()); 2802 2803 mTextView.setImeOptions(EditorInfo.IME_ACTION_GO); 2804 assertEquals(EditorInfo.IME_ACTION_GO, mTextView.getImeOptions()); 2805 2806 mTextView.setImeOptions(EditorInfo.IME_ACTION_DONE); 2807 assertEquals(EditorInfo.IME_ACTION_DONE, mTextView.getImeOptions()); 2808 2809 mTextView.setImeOptions(EditorInfo.IME_NULL); 2810 assertEquals(EditorInfo.IME_NULL, mTextView.getImeOptions()); 2811 } 2812 2813 public void testAccessImeActionLabel() { 2814 mTextView = findTextView(R.id.textview_text); 2815 assertNull(mTextView.getImeActionLabel()); 2816 assertEquals(0, mTextView.getImeActionId()); 2817 2818 mTextView.setImeActionLabel("pinyin", 1); 2819 assertEquals("pinyin", mTextView.getImeActionLabel().toString()); 2820 assertEquals(1, mTextView.getImeActionId()); 2821 } 2822 2823 @UiThreadTest 2824 public void testSetExtractedText() { 2825 mTextView = findTextView(R.id.textview_text); 2826 assertEquals(mActivity.getResources().getString(R.string.text_view_hello), 2827 mTextView.getText().toString()); 2828 2829 ExtractedText et = new ExtractedText(); 2830 et.text = "test"; 2831 2832 mTextView.setExtractedText(et); 2833 assertEquals("test", mTextView.getText().toString()); 2834 } 2835 2836 public void testMoveCursorToVisibleOffset() throws Throwable { 2837 mTextView = findTextView(R.id.textview_text); 2838 2839 // not a spannable text 2840 runTestOnUiThread(new Runnable() { 2841 public void run() { 2842 assertFalse(mTextView.moveCursorToVisibleOffset()); 2843 } 2844 }); 2845 mInstrumentation.waitForIdleSync(); 2846 2847 // a selection range 2848 final String spannableText = "text"; 2849 mTextView = new TextView(mActivity); 2850 2851 runTestOnUiThread(new Runnable() { 2852 public void run() { 2853 mTextView.setText(spannableText, BufferType.SPANNABLE); 2854 } 2855 }); 2856 mInstrumentation.waitForIdleSync(); 2857 Selection.setSelection((Spannable) mTextView.getText(), 0, spannableText.length()); 2858 2859 assertEquals(0, mTextView.getSelectionStart()); 2860 assertEquals(spannableText.length(), mTextView.getSelectionEnd()); 2861 runTestOnUiThread(new Runnable() { 2862 public void run() { 2863 assertFalse(mTextView.moveCursorToVisibleOffset()); 2864 } 2865 }); 2866 mInstrumentation.waitForIdleSync(); 2867 2868 // a spannable without range 2869 runTestOnUiThread(new Runnable() { 2870 public void run() { 2871 mTextView = findTextView(R.id.textview_text); 2872 mTextView.setText(spannableText, BufferType.SPANNABLE); 2873 } 2874 }); 2875 mInstrumentation.waitForIdleSync(); 2876 2877 runTestOnUiThread(new Runnable() { 2878 public void run() { 2879 assertTrue(mTextView.moveCursorToVisibleOffset()); 2880 } 2881 }); 2882 mInstrumentation.waitForIdleSync(); 2883 } 2884 2885 public void testIsInputMethodTarget() throws Throwable { 2886 mTextView = findTextView(R.id.textview_text); 2887 assertFalse(mTextView.isInputMethodTarget()); 2888 2889 assertFalse(mTextView.isFocused()); 2890 runTestOnUiThread(new Runnable() { 2891 @Override 2892 public void run() { 2893 mTextView.setFocusable(true); 2894 mTextView.requestFocus(); 2895 } 2896 }); 2897 mInstrumentation.waitForIdleSync(); 2898 assertTrue(mTextView.isFocused()); 2899 2900 new PollingCheck() { 2901 @Override 2902 protected boolean check() { 2903 return mTextView.isInputMethodTarget(); 2904 } 2905 }.run(); 2906 } 2907 2908 public void testBeginEndBatchEdit() { 2909 mTextView = findTextView(R.id.textview_text); 2910 2911 mTextView.beginBatchEdit(); 2912 mTextView.endBatchEdit(); 2913 } 2914 2915 @UiThreadTest 2916 public void testBringPointIntoView() throws Throwable { 2917 mTextView = findTextView(R.id.textview_text); 2918 assertFalse(mTextView.bringPointIntoView(1)); 2919 2920 mTextView.layout(0, 0, 100, 100); 2921 assertFalse(mTextView.bringPointIntoView(2)); 2922 } 2923 2924 public void testCancelLongPress() { 2925 mTextView = findTextView(R.id.textview_text); 2926 TouchUtils.longClickView(this, mTextView); 2927 mTextView.cancelLongPress(); 2928 } 2929 2930 @UiThreadTest 2931 public void testClearComposingText() { 2932 mTextView = findTextView(R.id.textview_text); 2933 mTextView.setText("Hello world!", BufferType.SPANNABLE); 2934 Spannable text = (Spannable) mTextView.getText(); 2935 2936 assertEquals(-1, BaseInputConnection.getComposingSpanStart(text)); 2937 assertEquals(-1, BaseInputConnection.getComposingSpanStart(text)); 2938 2939 BaseInputConnection.setComposingSpans((Spannable) mTextView.getText()); 2940 assertEquals(0, BaseInputConnection.getComposingSpanStart(text)); 2941 assertEquals(0, BaseInputConnection.getComposingSpanStart(text)); 2942 2943 mTextView.clearComposingText(); 2944 assertEquals(-1, BaseInputConnection.getComposingSpanStart(text)); 2945 assertEquals(-1, BaseInputConnection.getComposingSpanStart(text)); 2946 } 2947 2948 public void testComputeVerticalScrollExtent() { 2949 MockTextView textView = new MockTextView(mActivity); 2950 assertEquals(0, textView.computeVerticalScrollExtent()); 2951 2952 Drawable d = mActivity.getResources().getDrawable(R.drawable.pass); 2953 textView.setCompoundDrawables(null, d, null, d); 2954 2955 assertEquals(0, textView.computeVerticalScrollExtent()); 2956 } 2957 2958 @UiThreadTest 2959 public void testDidTouchFocusSelect() { 2960 mTextView = new EditText(mActivity); 2961 assertFalse(mTextView.didTouchFocusSelect()); 2962 2963 mTextView.setFocusable(true); 2964 mTextView.requestFocus(); 2965 assertTrue(mTextView.didTouchFocusSelect()); 2966 } 2967 2968 public void testExtractText() { 2969 mTextView = new TextView(mActivity); 2970 2971 ExtractedTextRequest request = new ExtractedTextRequest(); 2972 ExtractedText outText = new ExtractedText(); 2973 2974 request.token = 0; 2975 request.flags = 10; 2976 request.hintMaxLines = 2; 2977 request.hintMaxChars = 20; 2978 assertTrue(mTextView.extractText(request, outText)); 2979 2980 mTextView = findTextView(R.id.textview_text); 2981 assertTrue(mTextView.extractText(request, outText)); 2982 2983 assertEquals(mActivity.getResources().getString(R.string.text_view_hello), 2984 outText.text.toString()); 2985 } 2986 2987 @UiThreadTest 2988 public void testTextDirectionDefault() { 2989 TextView tv = new TextView(mActivity); 2990 assertEquals(View.TEXT_DIRECTION_INHERIT, tv.getRawTextDirection()); 2991 } 2992 2993 @UiThreadTest 2994 public void testSetGetTextDirection() { 2995 TextView tv = new TextView(mActivity); 2996 2997 tv.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG); 2998 assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getRawTextDirection()); 2999 3000 tv.setTextDirection(View.TEXT_DIRECTION_ANY_RTL); 3001 assertEquals(View.TEXT_DIRECTION_ANY_RTL, tv.getRawTextDirection()); 3002 3003 tv.setTextDirection(View.TEXT_DIRECTION_INHERIT); 3004 assertEquals(View.TEXT_DIRECTION_INHERIT, tv.getRawTextDirection()); 3005 3006 tv.setTextDirection(View.TEXT_DIRECTION_LTR); 3007 assertEquals(View.TEXT_DIRECTION_LTR, tv.getRawTextDirection()); 3008 3009 tv.setTextDirection(View.TEXT_DIRECTION_RTL); 3010 assertEquals(View.TEXT_DIRECTION_RTL, tv.getRawTextDirection()); 3011 3012 tv.setTextDirection(View.TEXT_DIRECTION_LOCALE); 3013 assertEquals(View.TEXT_DIRECTION_LOCALE, tv.getRawTextDirection()); 3014 } 3015 3016 @UiThreadTest 3017 public void testGetResolvedTextDirectionLtr() { 3018 TextView tv = new TextView(mActivity); 3019 tv.setText("this is a test"); 3020 3021 assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection()); 3022 3023 tv.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG); 3024 assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection()); 3025 3026 tv.setTextDirection(View.TEXT_DIRECTION_ANY_RTL); 3027 assertEquals(View.TEXT_DIRECTION_ANY_RTL, tv.getTextDirection()); 3028 3029 tv.setTextDirection(View.TEXT_DIRECTION_INHERIT); 3030 assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection()); 3031 3032 tv.setTextDirection(View.TEXT_DIRECTION_LTR); 3033 assertEquals(View.TEXT_DIRECTION_LTR, tv.getTextDirection()); 3034 3035 tv.setTextDirection(View.TEXT_DIRECTION_RTL); 3036 assertEquals(View.TEXT_DIRECTION_RTL, tv.getTextDirection()); 3037 3038 tv.setTextDirection(View.TEXT_DIRECTION_LOCALE); 3039 assertEquals(View.TEXT_DIRECTION_LOCALE, tv.getTextDirection()); 3040 } 3041 3042 @UiThreadTest 3043 public void testGetResolvedTextDirectionLtrWithInheritance() { 3044 LinearLayout ll = new LinearLayout(mActivity); 3045 ll.setTextDirection(View.TEXT_DIRECTION_ANY_RTL); 3046 3047 TextView tv = new TextView(mActivity); 3048 tv.setText("this is a test"); 3049 ll.addView(tv); 3050 3051 tv.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG); 3052 assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection()); 3053 3054 tv.setTextDirection(View.TEXT_DIRECTION_ANY_RTL); 3055 assertEquals(View.TEXT_DIRECTION_ANY_RTL, tv.getTextDirection()); 3056 3057 tv.setTextDirection(View.TEXT_DIRECTION_INHERIT); 3058 assertEquals(View.TEXT_DIRECTION_ANY_RTL, tv.getTextDirection()); 3059 3060 tv.setTextDirection(View.TEXT_DIRECTION_LTR); 3061 assertEquals(View.TEXT_DIRECTION_LTR, tv.getTextDirection()); 3062 3063 tv.setTextDirection(View.TEXT_DIRECTION_RTL); 3064 assertEquals(View.TEXT_DIRECTION_RTL, tv.getTextDirection()); 3065 3066 tv.setTextDirection(View.TEXT_DIRECTION_LOCALE); 3067 assertEquals(View.TEXT_DIRECTION_LOCALE, tv.getTextDirection()); 3068 } 3069 3070 @UiThreadTest 3071 public void testGetResolvedTextDirectionRtl() { 3072 TextView tv = new TextView(mActivity); 3073 tv.setText("\u05DD\u05DE"); // hebrew 3074 3075 assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection()); 3076 3077 tv.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG); 3078 assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection()); 3079 3080 tv.setTextDirection(View.TEXT_DIRECTION_ANY_RTL); 3081 assertEquals(View.TEXT_DIRECTION_ANY_RTL, tv.getTextDirection()); 3082 3083 tv.setTextDirection(View.TEXT_DIRECTION_INHERIT); 3084 assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection()); 3085 3086 tv.setTextDirection(View.TEXT_DIRECTION_LTR); 3087 assertEquals(View.TEXT_DIRECTION_LTR, tv.getTextDirection()); 3088 3089 tv.setTextDirection(View.TEXT_DIRECTION_RTL); 3090 assertEquals(View.TEXT_DIRECTION_RTL, tv.getTextDirection()); 3091 3092 tv.setTextDirection(View.TEXT_DIRECTION_LOCALE); 3093 assertEquals(View.TEXT_DIRECTION_LOCALE, tv.getTextDirection()); 3094 } 3095 3096 @UiThreadTest 3097 public void testGetResolvedTextDirectionRtlWithInheritance() { 3098 LinearLayout ll = new LinearLayout(mActivity); 3099 ll.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG); 3100 3101 TextView tv = new TextView(mActivity); 3102 tv.setText("\u05DD\u05DE"); // hebrew 3103 ll.addView(tv); 3104 3105 tv.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG); 3106 assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection()); 3107 3108 tv.setTextDirection(View.TEXT_DIRECTION_ANY_RTL); 3109 assertEquals(View.TEXT_DIRECTION_ANY_RTL, tv.getTextDirection()); 3110 3111 tv.setTextDirection(View.TEXT_DIRECTION_INHERIT); 3112 assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection()); 3113 3114 tv.setTextDirection(View.TEXT_DIRECTION_LTR); 3115 assertEquals(View.TEXT_DIRECTION_LTR, tv.getTextDirection()); 3116 3117 tv.setTextDirection(View.TEXT_DIRECTION_RTL); 3118 assertEquals(View.TEXT_DIRECTION_RTL, tv.getTextDirection()); 3119 3120 tv.setTextDirection(View.TEXT_DIRECTION_LOCALE); 3121 assertEquals(View.TEXT_DIRECTION_LOCALE, tv.getTextDirection()); 3122 3123 // Force to RTL text direction on the layout 3124 ll.setTextDirection(View.TEXT_DIRECTION_RTL); 3125 3126 tv.setTextDirection(View.TEXT_DIRECTION_FIRST_STRONG); 3127 assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection()); 3128 3129 tv.setTextDirection(View.TEXT_DIRECTION_ANY_RTL); 3130 assertEquals(View.TEXT_DIRECTION_ANY_RTL, tv.getTextDirection()); 3131 3132 tv.setTextDirection(View.TEXT_DIRECTION_INHERIT); 3133 assertEquals(View.TEXT_DIRECTION_RTL, tv.getTextDirection()); 3134 3135 tv.setTextDirection(View.TEXT_DIRECTION_LTR); 3136 assertEquals(View.TEXT_DIRECTION_LTR, tv.getTextDirection()); 3137 3138 tv.setTextDirection(View.TEXT_DIRECTION_RTL); 3139 assertEquals(View.TEXT_DIRECTION_RTL, tv.getTextDirection()); 3140 3141 tv.setTextDirection(View.TEXT_DIRECTION_LOCALE); 3142 assertEquals(View.TEXT_DIRECTION_LOCALE, tv.getTextDirection()); 3143 } 3144 3145 @UiThreadTest 3146 public void testResetTextDirection() { 3147 LinearLayout ll = (LinearLayout) mActivity.findViewById(R.id.layout_textviewtest); 3148 TextView tv = (TextView) mActivity.findViewById(R.id.textview_rtl); 3149 3150 ll.setTextDirection(View.TEXT_DIRECTION_RTL); 3151 tv.setTextDirection(View.TEXT_DIRECTION_INHERIT); 3152 assertEquals(View.TEXT_DIRECTION_RTL, tv.getTextDirection()); 3153 3154 ll.removeView(tv); 3155 assertEquals(View.TEXT_DIRECTION_FIRST_STRONG, tv.getTextDirection()); 3156 } 3157 3158 @UiThreadTest 3159 public void testTextAlignmentDefault() { 3160 TextView tv = new TextView(getActivity()); 3161 assertEquals(View.TEXT_ALIGNMENT_GRAVITY, tv.getRawTextAlignment()); 3162 // resolved default text alignment is GRAVITY 3163 assertEquals(View.TEXT_ALIGNMENT_GRAVITY, tv.getTextAlignment()); 3164 } 3165 3166 @UiThreadTest 3167 public void testSetGetTextAlignment() { 3168 TextView tv = new TextView(getActivity()); 3169 3170 tv.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY); 3171 assertEquals(View.TEXT_ALIGNMENT_GRAVITY, tv.getRawTextAlignment()); 3172 3173 tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 3174 assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getRawTextAlignment()); 3175 3176 tv.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START); 3177 assertEquals(View.TEXT_ALIGNMENT_TEXT_START, tv.getRawTextAlignment()); 3178 3179 tv.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END); 3180 assertEquals(View.TEXT_ALIGNMENT_TEXT_END, tv.getRawTextAlignment()); 3181 3182 tv.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START); 3183 assertEquals(View.TEXT_ALIGNMENT_VIEW_START, tv.getRawTextAlignment()); 3184 3185 tv.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END); 3186 assertEquals(View.TEXT_ALIGNMENT_VIEW_END, tv.getRawTextAlignment()); 3187 } 3188 3189 @UiThreadTest 3190 public void testGetResolvedTextAlignment() { 3191 TextView tv = new TextView(getActivity()); 3192 3193 assertEquals(View.TEXT_ALIGNMENT_GRAVITY, tv.getTextAlignment()); 3194 3195 // Test center alignment first so that we dont hit the default case 3196 tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 3197 assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getTextAlignment()); 3198 3199 // Test the default case too 3200 tv.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY); 3201 assertEquals(View.TEXT_ALIGNMENT_GRAVITY, tv.getTextAlignment()); 3202 3203 tv.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START); 3204 assertEquals(View.TEXT_ALIGNMENT_TEXT_START, tv.getTextAlignment()); 3205 3206 tv.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END); 3207 assertEquals(View.TEXT_ALIGNMENT_TEXT_END, tv.getTextAlignment()); 3208 3209 tv.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START); 3210 assertEquals(View.TEXT_ALIGNMENT_VIEW_START, tv.getTextAlignment()); 3211 3212 tv.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END); 3213 assertEquals(View.TEXT_ALIGNMENT_VIEW_END, tv.getTextAlignment()); 3214 } 3215 3216 @UiThreadTest 3217 public void testGetResolvedTextAlignmentWithInheritance() { 3218 LinearLayout ll = new LinearLayout(getActivity()); 3219 ll.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY); 3220 3221 TextView tv = new TextView(getActivity()); 3222 ll.addView(tv); 3223 3224 // check defaults 3225 assertEquals(View.TEXT_ALIGNMENT_GRAVITY, tv.getRawTextAlignment()); 3226 assertEquals(View.TEXT_ALIGNMENT_GRAVITY, tv.getTextAlignment()); 3227 3228 // set inherit and check that child is following parent 3229 tv.setTextAlignment(View.TEXT_ALIGNMENT_INHERIT); 3230 assertEquals(View.TEXT_ALIGNMENT_INHERIT, tv.getRawTextAlignment()); 3231 3232 ll.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 3233 assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getTextAlignment()); 3234 3235 ll.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START); 3236 assertEquals(View.TEXT_ALIGNMENT_TEXT_START, tv.getTextAlignment()); 3237 3238 ll.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END); 3239 assertEquals(View.TEXT_ALIGNMENT_TEXT_END, tv.getTextAlignment()); 3240 3241 ll.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START); 3242 assertEquals(View.TEXT_ALIGNMENT_VIEW_START, tv.getTextAlignment()); 3243 3244 ll.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END); 3245 assertEquals(View.TEXT_ALIGNMENT_VIEW_END, tv.getTextAlignment()); 3246 3247 // now get rid of the inheritance but still change the parent 3248 tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 3249 3250 ll.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 3251 assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getTextAlignment()); 3252 3253 ll.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START); 3254 assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getTextAlignment()); 3255 3256 ll.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END); 3257 assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getTextAlignment()); 3258 3259 ll.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START); 3260 assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getTextAlignment()); 3261 3262 ll.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_END); 3263 assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getTextAlignment()); 3264 } 3265 3266 @UiThreadTest 3267 public void testResetTextAlignment() { 3268 TextViewStubActivity activity = getActivity(); 3269 3270 LinearLayout ll = (LinearLayout) activity.findViewById(R.id.layout_textviewtest); 3271 TextView tv = (TextView) activity.findViewById(R.id.textview_rtl); 3272 3273 ll.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 3274 tv.setTextAlignment(View.TEXT_ALIGNMENT_INHERIT); 3275 assertEquals(View.TEXT_ALIGNMENT_CENTER, tv.getTextAlignment()); 3276 3277 ll.removeView(tv); 3278 // default text alignment is GRAVITY 3279 assertEquals(View.TEXT_ALIGNMENT_GRAVITY, tv.getTextAlignment()); 3280 } 3281 3282 @UiThreadTest 3283 public void testDrawableResolution() { 3284 final int LEFT = 0; 3285 final int TOP = 1; 3286 final int RIGHT = 2; 3287 final int BOTTOM = 3; 3288 3289 TextViewStubActivity activity = getActivity(); 3290 3291 // Case 1.1: left / right drawable defined in default LTR mode 3292 TextView tv = (TextView) activity.findViewById(R.id.textview_drawable_1_1); 3293 Drawable[] drawables = tv.getCompoundDrawables(); 3294 3295 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue), 3296 ((BitmapDrawable) drawables[LEFT]).getBitmap()); 3297 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red), 3298 ((BitmapDrawable) drawables[RIGHT]).getBitmap()); 3299 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green), 3300 ((BitmapDrawable) drawables[TOP]).getBitmap()); 3301 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow), 3302 ((BitmapDrawable) drawables[BOTTOM]).getBitmap()); 3303 3304 // Case 1.2: left / right drawable defined in default RTL mode 3305 tv = (TextView) activity.findViewById(R.id.textview_drawable_1_2); 3306 drawables = tv.getCompoundDrawables(); 3307 3308 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue), 3309 ((BitmapDrawable) drawables[LEFT]).getBitmap()); 3310 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red), 3311 ((BitmapDrawable) drawables[RIGHT]).getBitmap()); 3312 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green), 3313 ((BitmapDrawable) drawables[TOP]).getBitmap()); 3314 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow), 3315 ((BitmapDrawable) drawables[BOTTOM]).getBitmap()); 3316 3317 // Case 2.1: start / end drawable defined in LTR mode 3318 tv = (TextView) activity.findViewById(R.id.textview_drawable_2_1); 3319 drawables = tv.getCompoundDrawables(); 3320 3321 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue), 3322 ((BitmapDrawable) drawables[LEFT]).getBitmap()); 3323 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red), 3324 ((BitmapDrawable) drawables[RIGHT]).getBitmap()); 3325 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green), 3326 ((BitmapDrawable) drawables[TOP]).getBitmap()); 3327 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow), 3328 ((BitmapDrawable) drawables[BOTTOM]).getBitmap()); 3329 3330 // Case 2.2: start / end drawable defined in RTL mode 3331 tv = (TextView) activity.findViewById(R.id.textview_drawable_2_2); 3332 drawables = tv.getCompoundDrawables(); 3333 3334 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red), 3335 ((BitmapDrawable) drawables[LEFT]).getBitmap()); 3336 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue), 3337 ((BitmapDrawable) drawables[RIGHT]).getBitmap()); 3338 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green), 3339 ((BitmapDrawable) drawables[TOP]).getBitmap()); 3340 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow), 3341 ((BitmapDrawable) drawables[BOTTOM]).getBitmap()); 3342 3343 // Case 3.1: left / right / start / end drawable defined in LTR mode 3344 tv = (TextView) activity.findViewById(R.id.textview_drawable_3_1); 3345 drawables = tv.getCompoundDrawables(); 3346 3347 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue), 3348 ((BitmapDrawable) drawables[LEFT]).getBitmap()); 3349 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red), 3350 ((BitmapDrawable) drawables[RIGHT]).getBitmap()); 3351 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green), 3352 ((BitmapDrawable) drawables[TOP]).getBitmap()); 3353 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow), 3354 ((BitmapDrawable) drawables[BOTTOM]).getBitmap()); 3355 3356 // Case 3.2: left / right / start / end drawable defined in RTL mode 3357 tv = (TextView) activity.findViewById(R.id.textview_drawable_3_2); 3358 drawables = tv.getCompoundDrawables(); 3359 3360 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red), 3361 ((BitmapDrawable) drawables[LEFT]).getBitmap()); 3362 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue), 3363 ((BitmapDrawable) drawables[RIGHT]).getBitmap()); 3364 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green), 3365 ((BitmapDrawable) drawables[TOP]).getBitmap()); 3366 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow), 3367 ((BitmapDrawable) drawables[BOTTOM]).getBitmap()); 3368 3369 // Case 4.1: start / end drawable defined in LTR mode inside a layout 3370 // that defines the layout direction 3371 tv = (TextView) activity.findViewById(R.id.textview_drawable_4_1); 3372 drawables = tv.getCompoundDrawables(); 3373 3374 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue), 3375 ((BitmapDrawable) drawables[LEFT]).getBitmap()); 3376 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red), 3377 ((BitmapDrawable) drawables[RIGHT]).getBitmap()); 3378 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green), 3379 ((BitmapDrawable) drawables[TOP]).getBitmap()); 3380 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow), 3381 ((BitmapDrawable) drawables[BOTTOM]).getBitmap()); 3382 3383 // Case 4.2: start / end drawable defined in RTL mode inside a layout 3384 // that defines the layout direction 3385 tv = (TextView) activity.findViewById(R.id.textview_drawable_4_2); 3386 drawables = tv.getCompoundDrawables(); 3387 3388 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red), 3389 ((BitmapDrawable) drawables[LEFT]).getBitmap()); 3390 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue), 3391 ((BitmapDrawable) drawables[RIGHT]).getBitmap()); 3392 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green), 3393 ((BitmapDrawable) drawables[TOP]).getBitmap()); 3394 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow), 3395 ((BitmapDrawable) drawables[BOTTOM]).getBitmap()); 3396 3397 // Case 5.1: left / right / start / end drawable defined in LTR mode inside a layout 3398 // that defines the layout direction 3399 tv = (TextView) activity.findViewById(R.id.textview_drawable_3_1); 3400 drawables = tv.getCompoundDrawables(); 3401 3402 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue), 3403 ((BitmapDrawable) drawables[LEFT]).getBitmap()); 3404 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red), 3405 ((BitmapDrawable) drawables[RIGHT]).getBitmap()); 3406 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green), 3407 ((BitmapDrawable) drawables[TOP]).getBitmap()); 3408 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow), 3409 ((BitmapDrawable) drawables[BOTTOM]).getBitmap()); 3410 3411 // Case 5.2: left / right / start / end drawable defined in RTL mode inside a layout 3412 // that defines the layout direction 3413 tv = (TextView) activity.findViewById(R.id.textview_drawable_3_2); 3414 drawables = tv.getCompoundDrawables(); 3415 3416 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_red), 3417 ((BitmapDrawable) drawables[LEFT]).getBitmap()); 3418 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_blue), 3419 ((BitmapDrawable) drawables[RIGHT]).getBitmap()); 3420 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_green), 3421 ((BitmapDrawable) drawables[TOP]).getBitmap()); 3422 WidgetTestUtils.assertEquals(getBitmap(R.drawable.icon_yellow), 3423 ((BitmapDrawable) drawables[BOTTOM]).getBitmap()); 3424 } 3425 3426 private static class MockOnEditorActionListener implements OnEditorActionListener { 3427 private boolean isOnEditorActionCalled; 3428 3429 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 3430 isOnEditorActionCalled = true; 3431 return true; 3432 } 3433 3434 public boolean isOnEditorActionCalled() { 3435 return isOnEditorActionCalled; 3436 } 3437 } 3438 3439 private void layout(final TextView textView) { 3440 mActivity.runOnUiThread(new Runnable() { 3441 public void run() { 3442 mActivity.setContentView(textView); 3443 } 3444 }); 3445 mInstrumentation.waitForIdleSync(); 3446 } 3447 3448 private void layout(final int layoutId) { 3449 mActivity.runOnUiThread(new Runnable() { 3450 public void run() { 3451 mActivity.setContentView(layoutId); 3452 } 3453 }); 3454 mInstrumentation.waitForIdleSync(); 3455 } 3456 3457 private TextView findTextView(int id) { 3458 return (TextView) mActivity.findViewById(id); 3459 } 3460 3461 private int getAutoLinkMask(int id) { 3462 return findTextView(id).getAutoLinkMask(); 3463 } 3464 3465 private Bitmap getBitmap(int resid) { 3466 return ((BitmapDrawable) mActivity.getResources().getDrawable(resid)).getBitmap(); 3467 } 3468 3469 private void setMaxWidth(final int pixels) { 3470 mActivity.runOnUiThread(new Runnable() { 3471 public void run() { 3472 mTextView.setMaxWidth(pixels); 3473 } 3474 }); 3475 mInstrumentation.waitForIdleSync(); 3476 } 3477 3478 private void setMinWidth(final int pixels) { 3479 mActivity.runOnUiThread(new Runnable() { 3480 public void run() { 3481 mTextView.setMinWidth(pixels); 3482 } 3483 }); 3484 mInstrumentation.waitForIdleSync(); 3485 } 3486 3487 private void setMaxHeight(final int pixels) { 3488 mActivity.runOnUiThread(new Runnable() { 3489 public void run() { 3490 mTextView.setMaxHeight(pixels); 3491 } 3492 }); 3493 mInstrumentation.waitForIdleSync(); 3494 } 3495 3496 private void setMinHeight(final int pixels) { 3497 mActivity.runOnUiThread(new Runnable() { 3498 public void run() { 3499 mTextView.setMinHeight(pixels); 3500 } 3501 }); 3502 mInstrumentation.waitForIdleSync(); 3503 } 3504 3505 private void setMinLines(final int minlines) { 3506 mActivity.runOnUiThread(new Runnable() { 3507 public void run() { 3508 mTextView.setMinLines(minlines); 3509 } 3510 }); 3511 mInstrumentation.waitForIdleSync(); 3512 } 3513 3514 /** 3515 * Convenience for {@link TextView#setText(CharSequence, BufferType)}. And 3516 * the buffer type is fixed to SPANNABLE. 3517 * 3518 * @param tv the text view 3519 * @param content the content 3520 */ 3521 private void setSpannableText(final TextView tv, final String content) { 3522 mActivity.runOnUiThread(new Runnable() { 3523 public void run() { 3524 tv.setText(content, BufferType.SPANNABLE); 3525 } 3526 }); 3527 mInstrumentation.waitForIdleSync(); 3528 } 3529 3530 private void setLines(final int lines) { 3531 mActivity.runOnUiThread(new Runnable() { 3532 public void run() { 3533 mTextView.setLines(lines); 3534 } 3535 }); 3536 mInstrumentation.waitForIdleSync(); 3537 } 3538 3539 private void setHorizontallyScrolling(final boolean whether) { 3540 mActivity.runOnUiThread(new Runnable() { 3541 public void run() { 3542 mTextView.setHorizontallyScrolling(whether); 3543 } 3544 }); 3545 mInstrumentation.waitForIdleSync(); 3546 } 3547 3548 private void setWidth(final int pixels) { 3549 mActivity.runOnUiThread(new Runnable() { 3550 public void run() { 3551 mTextView.setWidth(pixels); 3552 } 3553 }); 3554 mInstrumentation.waitForIdleSync(); 3555 } 3556 3557 private void setHeight(final int pixels) { 3558 mActivity.runOnUiThread(new Runnable() { 3559 public void run() { 3560 mTextView.setHeight(pixels); 3561 } 3562 }); 3563 mInstrumentation.waitForIdleSync(); 3564 } 3565 3566 private void setMinEms(final int ems) { 3567 mActivity.runOnUiThread(new Runnable() { 3568 public void run() { 3569 mTextView.setMinEms(ems); 3570 } 3571 }); 3572 mInstrumentation.waitForIdleSync(); 3573 } 3574 3575 private void setMaxEms(final int ems) { 3576 mActivity.runOnUiThread(new Runnable() { 3577 public void run() { 3578 mTextView.setMaxEms(ems); 3579 } 3580 }); 3581 mInstrumentation.waitForIdleSync(); 3582 } 3583 3584 private void setEms(final int ems) { 3585 mActivity.runOnUiThread(new Runnable() { 3586 public void run() { 3587 mTextView.setEms(ems); 3588 } 3589 }); 3590 mInstrumentation.waitForIdleSync(); 3591 } 3592 3593 private void setLineSpacing(final float add, final float mult) { 3594 mActivity.runOnUiThread(new Runnable() { 3595 public void run() { 3596 mTextView.setLineSpacing(add, mult); 3597 } 3598 }); 3599 mInstrumentation.waitForIdleSync(); 3600 } 3601 3602 private static abstract class TestSelectedRunnable implements Runnable { 3603 private TextView mTextView; 3604 private boolean mIsSelected1; 3605 private boolean mIsSelected2; 3606 3607 public TestSelectedRunnable(TextView textview) { 3608 mTextView = textview; 3609 } 3610 3611 public boolean getIsSelected1() { 3612 return mIsSelected1; 3613 } 3614 3615 public boolean getIsSelected2() { 3616 return mIsSelected2; 3617 } 3618 3619 public void saveIsSelected1() { 3620 mIsSelected1 = mTextView.isSelected(); 3621 } 3622 3623 public void saveIsSelected2() { 3624 mIsSelected2 = mTextView.isSelected(); 3625 } 3626 } 3627 3628 private static abstract class TestLayoutRunnable implements Runnable { 3629 private TextView mTextView; 3630 private Layout mLayout; 3631 3632 public TestLayoutRunnable(TextView textview) { 3633 mTextView = textview; 3634 } 3635 3636 public Layout getLayout() { 3637 return mLayout; 3638 } 3639 3640 public void saveLayout() { 3641 mLayout = mTextView.getLayout(); 3642 } 3643 } 3644 3645 private class MockEditableFactory extends Editable.Factory { 3646 private boolean mhasCalledNewEditable; 3647 private CharSequence mSource; 3648 3649 public boolean hasCalledNewEditable() { 3650 return mhasCalledNewEditable; 3651 } 3652 3653 public void reset() { 3654 mhasCalledNewEditable = false; 3655 mSource = null; 3656 } 3657 3658 public CharSequence getSource() { 3659 return mSource; 3660 } 3661 3662 @Override 3663 public Editable newEditable(CharSequence source) { 3664 mhasCalledNewEditable = true; 3665 mSource = source; 3666 return super.newEditable(source); 3667 } 3668 } 3669 3670 private class MockSpannableFactory extends Spannable.Factory { 3671 private boolean mHasCalledNewSpannable; 3672 private CharSequence mSource; 3673 3674 public boolean hasCalledNewSpannable() { 3675 return mHasCalledNewSpannable; 3676 } 3677 3678 public void reset() { 3679 mHasCalledNewSpannable = false; 3680 mSource = null; 3681 } 3682 3683 public CharSequence getSource() { 3684 return mSource; 3685 } 3686 3687 @Override 3688 public Spannable newSpannable(CharSequence source) { 3689 mHasCalledNewSpannable = true; 3690 mSource = source; 3691 return super.newSpannable(source); 3692 } 3693 } 3694 3695 private static class MockTextWatcher implements TextWatcher { 3696 private boolean mHasCalledAfterTextChanged; 3697 private boolean mHasCalledBeforeTextChanged; 3698 private boolean mHasOnTextChanged; 3699 3700 public void reset(){ 3701 mHasCalledAfterTextChanged = false; 3702 mHasCalledBeforeTextChanged = false; 3703 mHasOnTextChanged = false; 3704 } 3705 3706 public boolean hasCalledAfterTextChanged() { 3707 return mHasCalledAfterTextChanged; 3708 } 3709 3710 public boolean hasCalledBeforeTextChanged() { 3711 return mHasCalledBeforeTextChanged; 3712 } 3713 3714 public boolean hasCalledOnTextChanged() { 3715 return mHasOnTextChanged; 3716 } 3717 3718 public void afterTextChanged(Editable s) { 3719 mHasCalledAfterTextChanged = true; 3720 } 3721 3722 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 3723 mHasCalledBeforeTextChanged = true; 3724 } 3725 3726 public void onTextChanged(CharSequence s, int start, int before, int count) { 3727 mHasOnTextChanged = true; 3728 } 3729 } 3730 3731 /** 3732 * The listener interface for receiving mockOnLongClick events. The class 3733 * that is interested in processing a mockOnLongClick event implements this 3734 * interface, and the object created with that class is registered with a 3735 * component using the component's 3736 * <code>addMockOnLongClickListener<code> method. When 3737 * the mockOnLongClick event occurs, that object's appropriate 3738 * method is invoked. 3739 * 3740 * @see MockOnLongClickEvent 3741 */ 3742 private static class MockOnLongClickListener implements OnLongClickListener { 3743 private boolean mExpectedOnLongClickResult; 3744 private boolean mHasLongClicked; 3745 3746 MockOnLongClickListener(boolean result) { 3747 mExpectedOnLongClickResult = result; 3748 } 3749 3750 public boolean hasLongClicked() { 3751 return mHasLongClicked; 3752 } 3753 3754 public boolean onLongClick(View v) { 3755 mHasLongClicked = true; 3756 return mExpectedOnLongClickResult; 3757 } 3758 } 3759 3760 /** 3761 * The listener interface for receiving mockOnCreateContextMenu events. The 3762 * class that is interested in processing a mockOnCreateContextMenu event 3763 * implements this interface, and the object created with that class is 3764 * registered with a component using the component's 3765 * <code>addMockOnCreateContextMenuListener<code> method. When the 3766 * mockOnCreateContextMenu event occurs, that object's appropriate method is 3767 * invoked. 3768 * 3769 * @see MockOnCreateContextMenuEvent 3770 */ 3771 private static class MockOnCreateContextMenuListener implements OnCreateContextMenuListener { 3772 private boolean mIsMenuItemsBlank; 3773 private boolean mHasCreatedContextMenu; 3774 3775 MockOnCreateContextMenuListener(boolean isBlank) { 3776 this.mIsMenuItemsBlank = isBlank; 3777 } 3778 3779 public boolean hasCreatedContextMenu() { 3780 return mHasCreatedContextMenu; 3781 } 3782 3783 public void reset() { 3784 mHasCreatedContextMenu = false; 3785 } 3786 3787 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 3788 mHasCreatedContextMenu = true; 3789 if (!mIsMenuItemsBlank) { 3790 menu.add("menu item"); 3791 } 3792 } 3793 } 3794 } 3795