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.view.cts; 18 19 import com.android.cts.stub.R; 20 import com.android.internal.view.menu.ContextMenuBuilder; 21 import com.google.android.collect.Lists; 22 23 import dalvik.annotation.TestLevel; 24 import dalvik.annotation.TestTargetClass; 25 import dalvik.annotation.TestTargetNew; 26 import dalvik.annotation.TestTargets; 27 import dalvik.annotation.ToBeFixed; 28 29 import android.app.Activity; 30 import android.content.Context; 31 import android.content.res.Resources; 32 import android.content.res.XmlResourceParser; 33 import android.graphics.Bitmap; 34 import android.graphics.Point; 35 import android.graphics.Rect; 36 import android.graphics.drawable.BitmapDrawable; 37 import android.graphics.drawable.ColorDrawable; 38 import android.graphics.drawable.Drawable; 39 import android.graphics.drawable.StateListDrawable; 40 import android.os.Parcelable; 41 import android.os.SystemClock; 42 import android.test.ActivityInstrumentationTestCase2; 43 import android.test.TouchUtils; 44 import android.test.UiThreadTest; 45 import android.util.AttributeSet; 46 import android.util.SparseArray; 47 import android.util.Xml; 48 import android.view.ContextMenu; 49 import android.view.Display; 50 import android.view.HapticFeedbackConstants; 51 import android.view.KeyEvent; 52 import android.view.MotionEvent; 53 import android.view.SoundEffectConstants; 54 import android.view.TouchDelegate; 55 import android.view.View; 56 import android.view.ViewConfiguration; 57 import android.view.ViewGroup; 58 import android.view.ViewParent; 59 import android.view.WindowManagerImpl; 60 import android.view.ContextMenu.ContextMenuInfo; 61 import android.view.View.BaseSavedState; 62 import android.view.View.OnClickListener; 63 import android.view.View.OnCreateContextMenuListener; 64 import android.view.View.OnFocusChangeListener; 65 import android.view.View.OnKeyListener; 66 import android.view.View.OnLongClickListener; 67 import android.view.View.OnTouchListener; 68 import android.view.animation.AlphaAnimation; 69 import android.view.animation.Animation; 70 import android.view.animation.cts.DelayedCheck; 71 import android.view.inputmethod.EditorInfo; 72 import android.view.inputmethod.InputConnection; 73 import android.view.inputmethod.InputMethodManager; 74 import android.widget.ArrayAdapter; 75 import android.widget.Button; 76 import android.widget.EditText; 77 import android.widget.LinearLayout; 78 import android.widget.ListView; 79 import android.widget.cts.StubActivity; 80 81 import java.util.ArrayList; 82 import java.util.Arrays; 83 import java.util.List; 84 85 /** 86 * Test {@link View}. 87 */ 88 @TestTargetClass(View.class) 89 public class ViewTest extends ActivityInstrumentationTestCase2<ViewTestStubActivity> { ViewTest()90 public ViewTest() { 91 super("com.android.cts.stub", ViewTestStubActivity.class); 92 } 93 94 private Resources mResources; 95 private MockViewParent mMockParent; 96 private Activity mActivity; 97 98 /** timeout delta when wait in case the system is sluggish */ 99 private static final long TIMEOUT_DELTA = 1000; 100 101 @Override setUp()102 protected void setUp() throws Exception { 103 super.setUp(); 104 mActivity = getActivity(); 105 mResources = mActivity.getResources(); 106 mMockParent = new MockViewParent(mActivity); 107 } 108 109 @TestTargets({ 110 @TestTargetNew( 111 level = TestLevel.COMPLETE, 112 method = "View", 113 args = {android.content.Context.class} 114 ), 115 @TestTargetNew( 116 level = TestLevel.COMPLETE, 117 method = "View", 118 args = {android.content.Context.class, android.util.AttributeSet.class} 119 ), 120 @TestTargetNew( 121 level = TestLevel.COMPLETE, 122 method = "View", 123 args = {android.content.Context.class, android.util.AttributeSet.class, int.class} 124 ) 125 }) 126 @ToBeFixed(bug = "1695243", explanation = "the javadoc for constructors is incomplete." + 127 "1. not clear what is supposed to happen if context is null.") testConstructor()128 public void testConstructor() { 129 new View(mActivity); 130 131 new View(null); 132 133 final XmlResourceParser parser = mResources.getLayout(R.layout.view_layout); 134 final AttributeSet attrs = Xml.asAttributeSet(parser); 135 new View(mActivity, attrs); 136 137 new View(mActivity, null); 138 139 try { 140 new View(null, attrs); 141 fail("should throw NullPointerException"); 142 } catch (NullPointerException e) { 143 } 144 145 new View(mActivity, attrs, 0); 146 147 new View(mActivity, null, 1); 148 149 try { 150 new View(null, null, 1); 151 fail("should throw NullPointerException"); 152 } catch (NullPointerException e) { 153 } 154 } 155 156 @TestTargetNew( 157 level = TestLevel.COMPLETE, 158 method = "getContext", 159 args = {} 160 ) testGetContext()161 public void testGetContext() { 162 View view = new View(mActivity); 163 assertSame(mActivity, view.getContext()); 164 165 view = new View(null); 166 assertNull(view.getContext()); 167 } 168 169 @TestTargetNew( 170 level = TestLevel.COMPLETE, 171 method = "getResources", 172 args = {} 173 ) testGetResources()174 public void testGetResources() { 175 View view = new View(mActivity); 176 assertSame(mResources, view.getResources()); 177 178 view = new View(null); 179 assertNull(view.getResources()); 180 } 181 182 @TestTargetNew( 183 level = TestLevel.COMPLETE, 184 method = "getAnimation", 185 args = {} 186 ) testGetAnimation()187 public void testGetAnimation() { 188 Animation animation = new AlphaAnimation(0.0f, 1.0f); 189 View view = new View(mActivity); 190 assertNull(view.getAnimation()); 191 192 view.setAnimation(animation); 193 assertSame(animation, view.getAnimation()); 194 195 view.clearAnimation(); 196 assertNull(view.getAnimation()); 197 } 198 199 @TestTargetNew( 200 level = TestLevel.COMPLETE, 201 method = "setAnimation", 202 args = {android.view.animation.Animation.class} 203 ) testSetAnimation()204 public void testSetAnimation() { 205 Animation animation = new AlphaAnimation(0.0f, 1.0f); 206 View view = new View(mActivity); 207 assertNull(view.getAnimation()); 208 209 animation.initialize(100, 100, 100, 100); 210 assertTrue(animation.isInitialized()); 211 view.setAnimation(animation); 212 assertSame(animation, view.getAnimation()); 213 assertFalse(animation.isInitialized()); 214 215 view.setAnimation(null); 216 assertNull(view.getAnimation()); 217 } 218 219 @TestTargetNew( 220 level = TestLevel.COMPLETE, 221 method = "clearAnimation", 222 args = {} 223 ) testClearAnimation()224 public void testClearAnimation() { 225 Animation animation = new AlphaAnimation(0.0f, 1.0f); 226 View view = new View(mActivity); 227 228 assertNull(view.getAnimation()); 229 view.clearAnimation(); 230 assertNull(view.getAnimation()); 231 232 view.setAnimation(animation); 233 assertNotNull(view.getAnimation()); 234 view.clearAnimation(); 235 assertNull(view.getAnimation()); 236 } 237 238 @TestTargetNew( 239 level = TestLevel.COMPLETE, 240 method = "startAnimation", 241 args = {android.view.animation.Animation.class} 242 ) 243 @ToBeFixed(bug = "1695243", explanation = "the javadoc for startAnimation() is incomplete." + 244 "1. not clear what is supposed to happen if animation is null.") testStartAnimation()245 public void testStartAnimation() { 246 Animation animation = new AlphaAnimation(0.0f, 1.0f); 247 View view = new View(mActivity); 248 249 try { 250 view.startAnimation(null); 251 fail("should throw NullPointerException"); 252 } catch (NullPointerException e) { 253 } 254 255 animation.setStartTime(1L); 256 assertEquals(1L, animation.getStartTime()); 257 view.startAnimation(animation); 258 assertEquals(Animation.START_ON_FIRST_FRAME, animation.getStartTime()); 259 } 260 261 @TestTargets({ 262 @TestTargetNew( 263 level = TestLevel.COMPLETE, 264 method = "onAnimationStart", 265 args = {} 266 ), 267 @TestTargetNew( 268 level = TestLevel.COMPLETE, 269 method = "onAnimationEnd", 270 args = {} 271 ), 272 @TestTargetNew( 273 level = TestLevel.COMPLETE, 274 method = "onSetAlpha", 275 args = {int.class} 276 ) 277 }) testOnAnimation()278 public void testOnAnimation() throws Throwable { 279 final Animation animation = new AlphaAnimation(0.0f, 1.0f); 280 long duration = 2000L; 281 animation.setDuration(duration); 282 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 283 284 // check whether it has started 285 runTestOnUiThread(new Runnable() { 286 public void run() { 287 view.startAnimation(animation); 288 } 289 }); 290 getInstrumentation().waitForIdleSync(); 291 292 assertTrue(view.hasCalledOnAnimationStart()); 293 294 // check whether it has ended after duration, and alpha changed during this time. 295 new DelayedCheck(duration + TIMEOUT_DELTA) { 296 @Override 297 protected boolean check() { 298 return view.hasCalledOnSetAlpha() && view.hasCalledOnAnimationEnd(); 299 } 300 }.run(); 301 } 302 303 @TestTargetNew( 304 level = TestLevel.COMPLETE, 305 method = "getParent", 306 args = {} 307 ) testGetParent()308 public void testGetParent() { 309 MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 310 ViewGroup parent = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root); 311 assertSame(parent, view.getParent()); 312 } 313 314 @TestTargetNew( 315 level = TestLevel.COMPLETE, 316 method = "findViewById", 317 args = {int.class} 318 ) testFindViewById()319 public void testFindViewById() { 320 View parent = mActivity.findViewById(R.id.viewlayout_root); 321 assertSame(parent, parent.findViewById(R.id.viewlayout_root)); 322 323 View view = parent.findViewById(R.id.mock_view); 324 assertTrue(view instanceof MockView); 325 } 326 327 @TestTargets({ 328 @TestTargetNew( 329 level = TestLevel.COMPLETE, 330 method = "getTouchDelegate", 331 args = {} 332 ), 333 @TestTargetNew( 334 level = TestLevel.COMPLETE, 335 method = "setTouchDelegate", 336 args = {android.view.TouchDelegate.class} 337 ) 338 }) testAccessTouchDelegate()339 public void testAccessTouchDelegate() throws Throwable { 340 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 341 Rect rect = new Rect(); 342 final Button button = new Button(mActivity); 343 final int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT; 344 runTestOnUiThread(new Runnable() { 345 public void run() { 346 mActivity.addContentView(button, 347 new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT)); 348 } 349 }); 350 getInstrumentation().waitForIdleSync(); 351 button.getHitRect(rect); 352 MockTouchDelegate delegate = new MockTouchDelegate(rect, button); 353 354 assertNull(view.getTouchDelegate()); 355 356 view.setTouchDelegate(delegate); 357 assertSame(delegate, view.getTouchDelegate()); 358 assertFalse(delegate.hasCalledOnTouchEvent()); 359 TouchUtils.clickView(this, view); 360 assertTrue(view.hasCalledOnTouchEvent()); 361 assertTrue(delegate.hasCalledOnTouchEvent()); 362 363 view.setTouchDelegate(null); 364 assertNull(view.getTouchDelegate()); 365 } 366 367 @TestTargets({ 368 @TestTargetNew( 369 level = TestLevel.COMPLETE, 370 method = "getTag", 371 args = {} 372 ), 373 @TestTargetNew( 374 level = TestLevel.COMPLETE, 375 method = "setTag", 376 args = {java.lang.Object.class} 377 ), 378 @TestTargetNew( 379 level = TestLevel.COMPLETE, 380 method = "findViewWithTag", 381 args = {java.lang.Object.class} 382 ) 383 }) 384 @UiThreadTest testAccessTag()385 public void testAccessTag() { 386 ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root); 387 MockView mockView = (MockView) mActivity.findViewById(R.id.mock_view); 388 MockView scrollView = (MockView) mActivity.findViewById(R.id.scroll_view); 389 390 ViewData viewData = new ViewData(); 391 viewData.childCount = 3; 392 viewData.tag = "linearLayout"; 393 viewData.firstChild = mockView; 394 viewGroup.setTag(viewData); 395 viewGroup.setFocusable(true); 396 assertSame(viewData, viewGroup.getTag()); 397 398 final String tag = "mock"; 399 assertNull(mockView.getTag()); 400 mockView.setTag(tag); 401 assertEquals(tag, mockView.getTag()); 402 403 scrollView.setTag(viewGroup); 404 assertSame(viewGroup, scrollView.getTag()); 405 406 assertSame(viewGroup, viewGroup.findViewWithTag(viewData)); 407 assertSame(mockView, viewGroup.findViewWithTag(tag)); 408 assertSame(scrollView, viewGroup.findViewWithTag(viewGroup)); 409 410 mockView.setTag(null); 411 assertNull(mockView.getTag()); 412 } 413 414 @TestTargetNew( 415 level = TestLevel.COMPLETE, 416 method = "onSizeChanged", 417 args = {int.class, int.class, int.class, int.class} 418 ) testOnSizeChanged()419 public void testOnSizeChanged() throws Throwable { 420 final ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root); 421 final MockView mockView = new MockView(mActivity); 422 assertEquals(-1, mockView.getOldWOnSizeChanged()); 423 assertEquals(-1, mockView.getOldHOnSizeChanged()); 424 runTestOnUiThread(new Runnable() { 425 public void run() { 426 viewGroup.addView(mockView); 427 } 428 }); 429 getInstrumentation().waitForIdleSync(); 430 assertTrue(mockView.hasCalledOnSizeChanged()); 431 assertEquals(0, mockView.getOldWOnSizeChanged()); 432 assertEquals(0, mockView.getOldHOnSizeChanged()); 433 434 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 435 assertTrue(view.hasCalledOnSizeChanged()); 436 view.reset(); 437 assertEquals(-1, view.getOldWOnSizeChanged()); 438 assertEquals(-1, view.getOldHOnSizeChanged()); 439 int oldw = view.getWidth(); 440 int oldh = view.getHeight(); 441 final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(200, 100); 442 runTestOnUiThread(new Runnable() { 443 public void run() { 444 view.setLayoutParams(layoutParams); 445 } 446 }); 447 getInstrumentation().waitForIdleSync(); 448 assertTrue(view.hasCalledOnSizeChanged()); 449 assertEquals(oldw, view.getOldWOnSizeChanged()); 450 assertEquals(oldh, view.getOldHOnSizeChanged()); 451 } 452 453 @TestTargetNew( 454 level = TestLevel.COMPLETE, 455 method = "getHitRect", 456 args = {android.graphics.Rect.class} 457 ) 458 @ToBeFixed(bug = "1695243", explanation = "the javadoc for getHitRect() is incomplete." + 459 "1. not clear what is supposed to happen if outRect is null.") testGetHitRect()460 public void testGetHitRect() { 461 MockView view = new MockView(mActivity); 462 Rect outRect = new Rect(); 463 464 try { 465 view.getHitRect(null); 466 fail("should throw NullPointerException"); 467 } catch (NullPointerException e) { 468 } 469 470 View mockView = mActivity.findViewById(R.id.mock_view); 471 mockView.getHitRect(outRect); 472 assertEquals(0, outRect.left); 473 assertEquals(0, outRect.top); 474 assertEquals(mockView.getWidth(), outRect.right); 475 assertEquals(mockView.getHeight(), outRect.bottom); 476 } 477 478 @TestTargetNew( 479 level = TestLevel.COMPLETE, 480 method = "forceLayout", 481 args = {} 482 ) testForceLayout()483 public void testForceLayout() { 484 View view = new View(mActivity); 485 486 assertFalse(view.isLayoutRequested()); 487 view.forceLayout(); 488 assertTrue(view.isLayoutRequested()); 489 490 view.forceLayout(); 491 assertTrue(view.isLayoutRequested()); 492 } 493 494 @TestTargetNew( 495 level = TestLevel.COMPLETE, 496 method = "isLayoutRequested", 497 args = {} 498 ) testIsLayoutRequested()499 public void testIsLayoutRequested() { 500 View view = new View(mActivity); 501 502 assertFalse(view.isLayoutRequested()); 503 view.forceLayout(); 504 assertTrue(view.isLayoutRequested()); 505 506 view.layout(0, 0, 0, 0); 507 assertFalse(view.isLayoutRequested()); 508 } 509 510 @TestTargetNew( 511 level = TestLevel.COMPLETE, 512 method = "requestLayout", 513 args = {} 514 ) testRequestLayout()515 public void testRequestLayout() { 516 MockView view = new MockView(mActivity); 517 assertFalse(view.isLayoutRequested()); 518 assertNull(view.getParent()); 519 520 view.requestLayout(); 521 assertTrue(view.isLayoutRequested()); 522 523 view.setParent(mMockParent); 524 assertFalse(mMockParent.hasRequestLayout()); 525 view.requestLayout(); 526 assertTrue(view.isLayoutRequested()); 527 assertTrue(mMockParent.hasRequestLayout()); 528 } 529 530 @TestTargets({ 531 @TestTargetNew( 532 level = TestLevel.COMPLETE, 533 method = "layout", 534 args = {int.class, int.class, int.class, int.class} 535 ), 536 @TestTargetNew( 537 level = TestLevel.COMPLETE, 538 method = "onLayout", 539 args = {boolean.class, int.class, int.class, int.class, int.class} 540 ) 541 }) testLayout()542 public void testLayout() throws Throwable { 543 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 544 assertTrue(view.hasCalledOnLayout()); 545 546 view.reset(); 547 assertFalse(view.hasCalledOnLayout()); 548 runTestOnUiThread(new Runnable() { 549 public void run() { 550 view.requestLayout(); 551 } 552 }); 553 getInstrumentation().waitForIdleSync(); 554 assertTrue(view.hasCalledOnLayout()); 555 } 556 557 @TestTargetNew( 558 level = TestLevel.COMPLETE, 559 method = "getBaseline", 560 args = {} 561 ) testGetBaseline()562 public void testGetBaseline() { 563 View view = new View(mActivity); 564 565 assertEquals(-1, view.getBaseline()); 566 } 567 568 @TestTargetNew( 569 level = TestLevel.COMPLETE, 570 method = "computeScroll", 571 args = {} 572 ) testComputeScroll()573 public void testComputeScroll() throws Throwable { 574 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 575 assertTrue(view.hasCalledComputeScroll()); 576 577 view.reset(); 578 runTestOnUiThread(new Runnable() { 579 public void run() { 580 view.requestLayout(); 581 } 582 }); 583 getInstrumentation().waitForIdleSync(); 584 assertTrue(view.hasCalledComputeScroll()); 585 } 586 587 @TestTargets({ 588 @TestTargetNew( 589 level = TestLevel.COMPLETE, 590 method = "getBackground", 591 args = {} 592 ), 593 @TestTargetNew( 594 level = TestLevel.COMPLETE, 595 method = "setBackgroundDrawable", 596 args = {android.graphics.drawable.Drawable.class} 597 ) 598 }) testAccessBackground()599 public void testAccessBackground() { 600 View view = new View(mActivity); 601 Drawable d1 = mResources.getDrawable(R.drawable.scenery); 602 Drawable d2 = mResources.getDrawable(R.drawable.pass); 603 604 assertNull(view.getBackground()); 605 606 view.setBackgroundDrawable(d1); 607 assertEquals(d1, view.getBackground()); 608 609 view.setBackgroundDrawable(d2); 610 assertEquals(d2, view.getBackground()); 611 612 view.setBackgroundDrawable(null); 613 assertNull(view.getBackground()); 614 } 615 616 @TestTargetNew( 617 level = TestLevel.COMPLETE, 618 method = "setBackgroundResource", 619 args = {int.class} 620 ) testSetBackgroundResource()621 public void testSetBackgroundResource() { 622 View view = new View(mActivity); 623 624 assertNull(view.getBackground()); 625 626 view.setBackgroundResource(R.drawable.pass); 627 assertNotNull(view.getBackground()); 628 629 view.setBackgroundResource(0); 630 assertNull(view.getBackground()); 631 } 632 633 @TestTargets({ 634 @TestTargetNew( 635 level = TestLevel.COMPLETE, 636 method = "getDrawingCacheBackgroundColor", 637 args = {} 638 ), 639 @TestTargetNew( 640 level = TestLevel.COMPLETE, 641 method = "setDrawingCacheBackgroundColor", 642 args = {int.class} 643 ) 644 }) testAccessDrawingCacheBackgroundColor()645 public void testAccessDrawingCacheBackgroundColor() { 646 View view = new View(mActivity); 647 648 assertEquals(0, view.getDrawingCacheBackgroundColor()); 649 650 view.setDrawingCacheBackgroundColor(0xFF00FF00); 651 assertEquals(0xFF00FF00, view.getDrawingCacheBackgroundColor()); 652 653 view.setDrawingCacheBackgroundColor(-1); 654 assertEquals(-1, view.getDrawingCacheBackgroundColor()); 655 } 656 657 @TestTargetNew( 658 level = TestLevel.COMPLETE, 659 method = "setBackgroundColor", 660 args = {int.class} 661 ) testSetBackgroundColor()662 public void testSetBackgroundColor() { 663 View view = new View(mActivity); 664 ColorDrawable colorDrawable; 665 assertNull(view.getBackground()); 666 667 view.setBackgroundColor(0xFFFF0000); 668 colorDrawable = (ColorDrawable) view.getBackground(); 669 assertNotNull(colorDrawable); 670 assertEquals(0xFF, colorDrawable.getAlpha()); 671 672 view.setBackgroundColor(0); 673 colorDrawable = (ColorDrawable) view.getBackground(); 674 assertNotNull(colorDrawable); 675 assertEquals(0, colorDrawable.getAlpha()); 676 } 677 678 @TestTargetNew( 679 level = TestLevel.COMPLETE, 680 method = "verifyDrawable", 681 args = {android.graphics.drawable.Drawable.class} 682 ) testVerifyDrawable()683 public void testVerifyDrawable() { 684 MockView view = new MockView(mActivity); 685 Drawable d1 = mResources.getDrawable(R.drawable.scenery); 686 Drawable d2 = mResources.getDrawable(R.drawable.pass); 687 688 assertNull(view.getBackground()); 689 assertTrue(view.verifyDrawable(null)); 690 assertFalse(view.verifyDrawable(d1)); 691 692 view.setBackgroundDrawable(d1); 693 assertTrue(view.verifyDrawable(d1)); 694 assertFalse(view.verifyDrawable(d2)); 695 } 696 697 @TestTargetNew( 698 level = TestLevel.COMPLETE, 699 method = "getDrawingRect", 700 args = {android.graphics.Rect.class} 701 ) testGetDrawingRect()702 public void testGetDrawingRect() { 703 MockView view = new MockView(mActivity); 704 Rect outRect = new Rect(); 705 706 view.getDrawingRect(outRect); 707 assertEquals(0, outRect.left); 708 assertEquals(0, outRect.top); 709 assertEquals(0, outRect.right); 710 assertEquals(0, outRect.bottom); 711 712 view.scrollTo(10, 100); 713 view.getDrawingRect(outRect); 714 assertEquals(10, outRect.left); 715 assertEquals(100, outRect.top); 716 assertEquals(10, outRect.right); 717 assertEquals(100, outRect.bottom); 718 719 View mockView = mActivity.findViewById(R.id.mock_view); 720 mockView.getDrawingRect(outRect); 721 assertEquals(0, outRect.left); 722 assertEquals(0, outRect.top); 723 assertEquals(mockView.getWidth(), outRect.right); 724 assertEquals(mockView.getHeight(), outRect.bottom); 725 } 726 727 @TestTargetNew( 728 level = TestLevel.COMPLETE, 729 method = "getFocusedRect", 730 args = {android.graphics.Rect.class} 731 ) testGetFocusedRect()732 public void testGetFocusedRect() { 733 MockView view = new MockView(mActivity); 734 Rect outRect = new Rect(); 735 736 view.getFocusedRect(outRect); 737 assertEquals(0, outRect.left); 738 assertEquals(0, outRect.top); 739 assertEquals(0, outRect.right); 740 assertEquals(0, outRect.bottom); 741 742 view.scrollTo(10, 100); 743 view.getFocusedRect(outRect); 744 assertEquals(10, outRect.left); 745 assertEquals(100, outRect.top); 746 assertEquals(10, outRect.right); 747 assertEquals(100, outRect.bottom); 748 } 749 750 @TestTargetNew( 751 level = TestLevel.COMPLETE, 752 method = "getGlobalVisibleRect", 753 args = {android.graphics.Rect.class, android.graphics.Point.class} 754 ) testGetGlobalVisibleRectPoint()755 public void testGetGlobalVisibleRectPoint() throws Throwable { 756 final View view = mActivity.findViewById(R.id.mock_view); 757 final ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root); 758 Rect rect = new Rect(); 759 Point point = new Point(); 760 761 assertTrue(view.getGlobalVisibleRect(rect, point)); 762 Rect rcParent = new Rect(); 763 Point ptParent = new Point(); 764 viewGroup.getGlobalVisibleRect(rcParent, ptParent); 765 assertEquals(rcParent.left, rect.left); 766 assertEquals(rcParent.top, rect.top); 767 assertEquals(rect.left + view.getWidth(), rect.right); 768 assertEquals(rect.top + view.getHeight(), rect.bottom); 769 assertEquals(ptParent.x, point.x); 770 assertEquals(ptParent.y, point.y); 771 772 // width is 0 773 final LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(0, 300); 774 runTestOnUiThread(new Runnable() { 775 public void run() { 776 view.setLayoutParams(layoutParams1); 777 } 778 }); 779 getInstrumentation().waitForIdleSync(); 780 assertFalse(view.getGlobalVisibleRect(rect, point)); 781 782 // height is -10 783 final LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(200, -10); 784 runTestOnUiThread(new Runnable() { 785 public void run() { 786 view.setLayoutParams(layoutParams2); 787 } 788 }); 789 getInstrumentation().waitForIdleSync(); 790 assertFalse(view.getGlobalVisibleRect(rect, point)); 791 792 final LinearLayout.LayoutParams layoutParams3 = new LinearLayout.LayoutParams(200, 300); 793 runTestOnUiThread(new Runnable() { 794 public void run() { 795 view.setLayoutParams(layoutParams3); 796 } 797 }); 798 getInstrumentation().waitForIdleSync(); 799 assertTrue(view.getGlobalVisibleRect(rect, point)); 800 assertEquals(rcParent.left, rect.left); 801 assertEquals(rcParent.top, rect.top); 802 assertEquals(rect.left + 200, rect.right); 803 assertEquals(rect.top + 300, rect.bottom); 804 assertEquals(ptParent.x, point.x); 805 assertEquals(ptParent.y, point.y); 806 } 807 808 @TestTargetNew( 809 level = TestLevel.COMPLETE, 810 method = "getGlobalVisibleRect", 811 args = {android.graphics.Rect.class} 812 ) testGetGlobalVisibleRect()813 public void testGetGlobalVisibleRect() throws Throwable { 814 final View view = mActivity.findViewById(R.id.mock_view); 815 final ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root); 816 Rect rect = new Rect(); 817 818 assertTrue(view.getGlobalVisibleRect(rect)); 819 Rect rcParent = new Rect(); 820 viewGroup.getGlobalVisibleRect(rcParent); 821 assertEquals(rcParent.left, rect.left); 822 assertEquals(rcParent.top, rect.top); 823 assertEquals(rect.left + view.getWidth(), rect.right); 824 assertEquals(rect.top + view.getHeight(), rect.bottom); 825 826 // width is 0 827 final LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(0, 300); 828 runTestOnUiThread(new Runnable() { 829 public void run() { 830 view.setLayoutParams(layoutParams1); 831 } 832 }); 833 getInstrumentation().waitForIdleSync(); 834 assertFalse(view.getGlobalVisibleRect(rect)); 835 836 // height is -10 837 final LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(200, -10); 838 runTestOnUiThread(new Runnable() { 839 public void run() { 840 view.setLayoutParams(layoutParams2); 841 } 842 }); 843 getInstrumentation().waitForIdleSync(); 844 assertFalse(view.getGlobalVisibleRect(rect)); 845 846 final LinearLayout.LayoutParams layoutParams3 = new LinearLayout.LayoutParams(200, 300); 847 runTestOnUiThread(new Runnable() { 848 public void run() { 849 view.setLayoutParams(layoutParams3); 850 } 851 }); 852 getInstrumentation().waitForIdleSync(); 853 assertTrue(view.getGlobalVisibleRect(rect)); 854 assertEquals(rcParent.left, rect.left); 855 assertEquals(rcParent.top, rect.top); 856 assertEquals(rect.left + 200, rect.right); 857 assertEquals(rect.top + 300, rect.bottom); 858 } 859 860 @TestTargets({ 861 @TestTargetNew( 862 level = TestLevel.COMPLETE, 863 method = "computeHorizontalScrollOffset", 864 args = {} 865 ), 866 @TestTargetNew( 867 level = TestLevel.COMPLETE, 868 method = "computeHorizontalScrollRange", 869 args = {} 870 ), 871 @TestTargetNew( 872 level = TestLevel.COMPLETE, 873 method = "computeHorizontalScrollExtent", 874 args = {} 875 ) 876 }) testComputeHorizontalScroll()877 public void testComputeHorizontalScroll() throws Throwable { 878 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 879 880 assertEquals(0, view.computeHorizontalScrollOffset()); 881 assertEquals(view.getWidth(), view.computeHorizontalScrollRange()); 882 assertEquals(view.getWidth(), view.computeHorizontalScrollExtent()); 883 884 runTestOnUiThread(new Runnable() { 885 public void run() { 886 view.scrollTo(12, 0); 887 } 888 }); 889 getInstrumentation().waitForIdleSync(); 890 assertEquals(12, view.computeHorizontalScrollOffset()); 891 assertEquals(view.getWidth(), view.computeHorizontalScrollRange()); 892 assertEquals(view.getWidth(), view.computeHorizontalScrollExtent()); 893 894 runTestOnUiThread(new Runnable() { 895 public void run() { 896 view.scrollBy(12, 0); 897 } 898 }); 899 getInstrumentation().waitForIdleSync(); 900 assertEquals(24, view.computeHorizontalScrollOffset()); 901 assertEquals(view.getWidth(), view.computeHorizontalScrollRange()); 902 assertEquals(view.getWidth(), view.computeHorizontalScrollExtent()); 903 904 int newWidth = 200; 905 final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(newWidth, 100); 906 runTestOnUiThread(new Runnable() { 907 public void run() { 908 view.setLayoutParams(layoutParams); 909 } 910 }); 911 getInstrumentation().waitForIdleSync(); 912 assertEquals(24, view.computeHorizontalScrollOffset()); 913 assertEquals(newWidth, view.getWidth()); 914 assertEquals(view.getWidth(), view.computeHorizontalScrollRange()); 915 assertEquals(view.getWidth(), view.computeHorizontalScrollExtent()); 916 } 917 918 @TestTargets({ 919 @TestTargetNew( 920 level = TestLevel.COMPLETE, 921 method = "computeVerticalScrollOffset", 922 args = {} 923 ), 924 @TestTargetNew( 925 level = TestLevel.COMPLETE, 926 method = "computeVerticalScrollRange", 927 args = {} 928 ), 929 @TestTargetNew( 930 level = TestLevel.COMPLETE, 931 method = "computeVerticalScrollExtent", 932 args = {} 933 ) 934 }) testComputeVerticalScroll()935 public void testComputeVerticalScroll() throws Throwable { 936 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 937 938 assertEquals(0, view.computeVerticalScrollOffset()); 939 assertEquals(view.getHeight(), view.computeVerticalScrollRange()); 940 assertEquals(view.getHeight(), view.computeVerticalScrollExtent()); 941 942 final int scrollToY = 34; 943 runTestOnUiThread(new Runnable() { 944 public void run() { 945 view.scrollTo(0, scrollToY); 946 } 947 }); 948 getInstrumentation().waitForIdleSync(); 949 assertEquals(scrollToY, view.computeVerticalScrollOffset()); 950 assertEquals(view.getHeight(), view.computeVerticalScrollRange()); 951 assertEquals(view.getHeight(), view.computeVerticalScrollExtent()); 952 953 final int scrollByY = 200; 954 runTestOnUiThread(new Runnable() { 955 public void run() { 956 view.scrollBy(0, scrollByY); 957 } 958 }); 959 getInstrumentation().waitForIdleSync(); 960 assertEquals(scrollToY + scrollByY, view.computeVerticalScrollOffset()); 961 assertEquals(view.getHeight(), view.computeVerticalScrollRange()); 962 assertEquals(view.getHeight(), view.computeVerticalScrollExtent()); 963 964 int newHeight = 333; 965 final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(200, newHeight); 966 runTestOnUiThread(new Runnable() { 967 public void run() { 968 view.setLayoutParams(layoutParams); 969 } 970 }); 971 getInstrumentation().waitForIdleSync(); 972 assertEquals(scrollToY + scrollByY, view.computeVerticalScrollOffset()); 973 assertEquals(newHeight, view.getHeight()); 974 assertEquals(view.getHeight(), view.computeVerticalScrollRange()); 975 assertEquals(view.getHeight(), view.computeVerticalScrollExtent()); 976 } 977 978 @TestTargets({ 979 @TestTargetNew( 980 level = TestLevel.COMPLETE, 981 method = "getLeftFadingEdgeStrength", 982 args = {} 983 ), 984 @TestTargetNew( 985 level = TestLevel.COMPLETE, 986 method = "getRightFadingEdgeStrength", 987 args = {} 988 ), 989 @TestTargetNew( 990 level = TestLevel.COMPLETE, 991 method = "getTopFadingEdgeStrength", 992 args = {} 993 ), 994 @TestTargetNew( 995 level = TestLevel.COMPLETE, 996 method = "getBottomFadingEdgeStrength", 997 args = {} 998 ) 999 }) testGetFadingEdgeStrength()1000 public void testGetFadingEdgeStrength() throws Throwable { 1001 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 1002 1003 assertEquals(0f, view.getLeftFadingEdgeStrength()); 1004 assertEquals(0f, view.getRightFadingEdgeStrength()); 1005 assertEquals(0f, view.getTopFadingEdgeStrength()); 1006 assertEquals(0f, view.getBottomFadingEdgeStrength()); 1007 1008 runTestOnUiThread(new Runnable() { 1009 public void run() { 1010 view.scrollTo(10, 10); 1011 } 1012 }); 1013 getInstrumentation().waitForIdleSync(); 1014 assertEquals(1f, view.getLeftFadingEdgeStrength()); 1015 assertEquals(0f, view.getRightFadingEdgeStrength()); 1016 assertEquals(1f, view.getTopFadingEdgeStrength()); 1017 assertEquals(0f, view.getBottomFadingEdgeStrength()); 1018 1019 runTestOnUiThread(new Runnable() { 1020 public void run() { 1021 view.scrollTo(-10, -10); 1022 } 1023 }); 1024 getInstrumentation().waitForIdleSync(); 1025 assertEquals(0f, view.getLeftFadingEdgeStrength()); 1026 assertEquals(1f, view.getRightFadingEdgeStrength()); 1027 assertEquals(0f, view.getTopFadingEdgeStrength()); 1028 assertEquals(1f, view.getBottomFadingEdgeStrength()); 1029 } 1030 1031 @TestTargetNew( 1032 level = TestLevel.COMPLETE, 1033 method = "getLeftFadingEdgeStrength", 1034 args = {} 1035 ) testGetLeftFadingEdgeStrength()1036 public void testGetLeftFadingEdgeStrength() { 1037 MockView view = new MockView(mActivity); 1038 1039 assertEquals(0.0f, view.getLeftFadingEdgeStrength()); 1040 1041 view.scrollTo(1, 0); 1042 assertEquals(1.0f, view.getLeftFadingEdgeStrength()); 1043 } 1044 1045 @TestTargetNew( 1046 level = TestLevel.COMPLETE, 1047 method = "getRightFadingEdgeStrength", 1048 args = {} 1049 ) testGetRightFadingEdgeStrength()1050 public void testGetRightFadingEdgeStrength() { 1051 MockView view = new MockView(mActivity); 1052 1053 assertEquals(0.0f, view.getRightFadingEdgeStrength()); 1054 1055 view.scrollTo(-1, 0); 1056 assertEquals(1.0f, view.getRightFadingEdgeStrength()); 1057 } 1058 1059 @TestTargetNew( 1060 level = TestLevel.COMPLETE, 1061 method = "getBottomFadingEdgeStrength", 1062 args = {} 1063 ) testGetBottomFadingEdgeStrength()1064 public void testGetBottomFadingEdgeStrength() { 1065 MockView view = new MockView(mActivity); 1066 1067 assertEquals(0.0f, view.getBottomFadingEdgeStrength()); 1068 1069 view.scrollTo(0, -2); 1070 assertEquals(1.0f, view.getBottomFadingEdgeStrength()); 1071 } 1072 1073 @TestTargetNew( 1074 level = TestLevel.COMPLETE, 1075 method = "getTopFadingEdgeStrength", 1076 args = {} 1077 ) testGetTopFadingEdgeStrength()1078 public void testGetTopFadingEdgeStrength() { 1079 MockView view = new MockView(mActivity); 1080 1081 assertEquals(0.0f, view.getTopFadingEdgeStrength()); 1082 1083 view.scrollTo(0, 2); 1084 assertEquals(1.0f, view.getTopFadingEdgeStrength()); 1085 } 1086 1087 @TestTargetNew( 1088 level = TestLevel.COMPLETE, 1089 method = "resolveSize", 1090 args = {int.class, int.class} 1091 ) testResolveSize()1092 public void testResolveSize() { 1093 assertEquals(50, View.resolveSize(50, View.MeasureSpec.UNSPECIFIED)); 1094 1095 assertEquals(40, View.resolveSize(50, 40 | View.MeasureSpec.EXACTLY)); 1096 1097 assertEquals(30, View.resolveSize(50, 30 | View.MeasureSpec.AT_MOST)); 1098 1099 assertEquals(20, View.resolveSize(20, 30 | View.MeasureSpec.AT_MOST)); 1100 } 1101 1102 @TestTargetNew( 1103 level = TestLevel.COMPLETE, 1104 method = "getDefaultSize", 1105 args = {int.class, int.class} 1106 ) testGetDefaultSize()1107 public void testGetDefaultSize() { 1108 assertEquals(50, View.getDefaultSize(50, View.MeasureSpec.UNSPECIFIED)); 1109 1110 assertEquals(40, View.getDefaultSize(50, 40 | View.MeasureSpec.EXACTLY)); 1111 1112 assertEquals(30, View.getDefaultSize(50, 30 | View.MeasureSpec.AT_MOST)); 1113 1114 assertEquals(30, View.getDefaultSize(20, 30 | View.MeasureSpec.AT_MOST)); 1115 } 1116 1117 @TestTargets({ 1118 @TestTargetNew( 1119 level = TestLevel.COMPLETE, 1120 method = "getId", 1121 args = {} 1122 ), 1123 @TestTargetNew( 1124 level = TestLevel.COMPLETE, 1125 method = "setId", 1126 args = {int.class} 1127 ) 1128 }) testAccessId()1129 public void testAccessId() { 1130 View view = new View(mActivity); 1131 1132 assertEquals(View.NO_ID, view.getId()); 1133 1134 view.setId(10); 1135 assertEquals(10, view.getId()); 1136 1137 view.setId(0xFFFFFFFF); 1138 assertEquals(0xFFFFFFFF, view.getId()); 1139 } 1140 1141 @TestTargets({ 1142 @TestTargetNew( 1143 level = TestLevel.COMPLETE, 1144 method = "setLongClickable", 1145 args = {boolean.class} 1146 ), 1147 @TestTargetNew( 1148 level = TestLevel.COMPLETE, 1149 method = "isLongClickable", 1150 args = {} 1151 ) 1152 }) testAccessLongClickable()1153 public void testAccessLongClickable() { 1154 View view = new View(mActivity); 1155 1156 assertFalse(view.isLongClickable()); 1157 1158 view.setLongClickable(true); 1159 assertTrue(view.isLongClickable()); 1160 1161 view.setLongClickable(false); 1162 assertFalse(view.isLongClickable()); 1163 } 1164 1165 @TestTargets({ 1166 @TestTargetNew( 1167 level = TestLevel.COMPLETE, 1168 method = "setClickable", 1169 args = {boolean.class} 1170 ), 1171 @TestTargetNew( 1172 level = TestLevel.COMPLETE, 1173 method = "isClickable", 1174 args = {} 1175 ) 1176 }) testAccessClickable()1177 public void testAccessClickable() { 1178 View view = new View(mActivity); 1179 1180 assertFalse(view.isClickable()); 1181 1182 view.setClickable(true); 1183 assertTrue(view.isClickable()); 1184 1185 view.setClickable(false); 1186 assertFalse(view.isClickable()); 1187 } 1188 1189 @TestTargetNew( 1190 level = TestLevel.COMPLETE, 1191 method = "getContextMenuInfo", 1192 args = {} 1193 ) testGetContextMenuInfo()1194 public void testGetContextMenuInfo() { 1195 MockView view = new MockView(mActivity); 1196 1197 assertNull(view.getContextMenuInfo()); 1198 } 1199 1200 @TestTargetNew( 1201 level = TestLevel.COMPLETE, 1202 method = "setOnCreateContextMenuListener", 1203 args = {android.view.View.OnCreateContextMenuListener.class} 1204 ) testSetOnCreateContextMenuListener()1205 public void testSetOnCreateContextMenuListener() { 1206 View view = new View(mActivity); 1207 assertFalse(view.isLongClickable()); 1208 1209 view.setOnCreateContextMenuListener(null); 1210 assertTrue(view.isLongClickable()); 1211 1212 view.setOnCreateContextMenuListener(new OnCreateContextMenuListenerImpl()); 1213 assertTrue(view.isLongClickable()); 1214 } 1215 1216 @TestTargets({ 1217 @TestTargetNew( 1218 level = TestLevel.COMPLETE, 1219 notes = "parameter ContextMenu must be a MenuBuilder or its subclass instance, " + 1220 "but there is no document to indicate it. So there is a potential" + 1221 " ClassCastException", 1222 method = "createContextMenu", 1223 args = {android.view.ContextMenu.class} 1224 ), 1225 @TestTargetNew( 1226 level = TestLevel.COMPLETE, 1227 method = "onCreateContextMenu", 1228 args = {android.view.ContextMenu.class} 1229 ) 1230 }) 1231 @ToBeFixed(bug = "1695243", explanation = "the javadoc for createContextMenu() is incomplete." + 1232 "1. not clear what is supposed to happen if menu is null.") testCreateContextMenu()1233 public void testCreateContextMenu() { 1234 OnCreateContextMenuListenerImpl listener = new OnCreateContextMenuListenerImpl(); 1235 MockView view = new MockView(mActivity); 1236 ContextMenu contextMenu = new ContextMenuBuilder(mActivity); 1237 view.setParent(mMockParent); 1238 view.setOnCreateContextMenuListener(listener); 1239 assertFalse(view.hasCalledOnCreateContextMenu()); 1240 assertFalse(mMockParent.hasCreateContextMenu()); 1241 assertFalse(listener.hasOnCreateContextMenu()); 1242 1243 view.createContextMenu(contextMenu); 1244 assertTrue(view.hasCalledOnCreateContextMenu()); 1245 assertTrue(mMockParent.hasCreateContextMenu()); 1246 assertTrue(listener.hasOnCreateContextMenu()); 1247 1248 try { 1249 view.createContextMenu(null); 1250 fail("should throw NullPointerException"); 1251 } catch (NullPointerException e) { 1252 } 1253 } 1254 1255 @TestTargetNew( 1256 level = TestLevel.COMPLETE, 1257 method = "addFocusables", 1258 args = {java.util.ArrayList.class, int.class} 1259 ) 1260 @ToBeFixed(bug = "1695243", explanation = "the javadoc for addFocusables() is incomplete." + 1261 "1. not clear what is supposed to happen if the input ArrayList<View> is null.") testAddFocusables()1262 public void testAddFocusables() { 1263 View view = new View(mActivity); 1264 ArrayList<View> viewList = new ArrayList<View>(); 1265 1266 // view is not focusable 1267 assertFalse(view.isFocusable()); 1268 assertEquals(0, viewList.size()); 1269 view.addFocusables(viewList, 0); 1270 assertEquals(0, viewList.size()); 1271 1272 // view is focusable 1273 view.setFocusable(true); 1274 view.addFocusables(viewList, 0); 1275 assertEquals(1, viewList.size()); 1276 assertEquals(view, viewList.get(0)); 1277 1278 // null array should be ignored 1279 view.addFocusables(null, 0); 1280 } 1281 1282 @TestTargetNew( 1283 level = TestLevel.COMPLETE, 1284 method = "getFocusables", 1285 args = {int.class} 1286 ) testGetFocusables()1287 public void testGetFocusables() { 1288 View view = new View(mActivity); 1289 ArrayList<View> viewList; 1290 1291 // view is not focusable 1292 assertFalse(view.isFocusable()); 1293 viewList = view.getFocusables(0); 1294 assertEquals(0, viewList.size()); 1295 1296 // view is focusable 1297 view.setFocusable(true); 1298 viewList = view.getFocusables(0); 1299 assertEquals(1, viewList.size()); 1300 assertEquals(view, viewList.get(0)); 1301 1302 viewList = view.getFocusables(-1); 1303 assertEquals(1, viewList.size()); 1304 assertEquals(view, viewList.get(0)); 1305 } 1306 1307 @TestTargetNew( 1308 level = TestLevel.COMPLETE, 1309 method = "getRootView", 1310 args = {} 1311 ) testGetRootView()1312 public void testGetRootView() { 1313 MockView view = new MockView(mActivity); 1314 1315 assertNull(view.getParent()); 1316 assertEquals(view, view.getRootView()); 1317 1318 view.setParent(mMockParent); 1319 assertEquals(mMockParent, view.getRootView()); 1320 } 1321 1322 @TestTargetNew( 1323 level = TestLevel.COMPLETE, 1324 method = "getSolidColor", 1325 args = {} 1326 ) testGetSolidColor()1327 public void testGetSolidColor() { 1328 View view = new View(mActivity); 1329 1330 assertEquals(0, view.getSolidColor()); 1331 } 1332 1333 @TestTargetNew( 1334 level = TestLevel.COMPLETE, 1335 method = "setMinimumWidth", 1336 args = {int.class} 1337 ) testSetMinimumWidth()1338 public void testSetMinimumWidth() { 1339 MockView view = new MockView(mActivity); 1340 assertEquals(0, view.getSuggestedMinimumWidth()); 1341 1342 view.setMinimumWidth(100); 1343 assertEquals(100, view.getSuggestedMinimumWidth()); 1344 1345 view.setMinimumWidth(-100); 1346 assertEquals(-100, view.getSuggestedMinimumWidth()); 1347 } 1348 1349 @TestTargetNew( 1350 level = TestLevel.COMPLETE, 1351 method = "getSuggestedMinimumWidth", 1352 args = {} 1353 ) testGetSuggestedMinimumWidth()1354 public void testGetSuggestedMinimumWidth() { 1355 MockView view = new MockView(mActivity); 1356 Drawable d = mResources.getDrawable(R.drawable.scenery); 1357 int drawableMinimumWidth = d.getMinimumWidth(); 1358 1359 // drawable is null 1360 view.setMinimumWidth(100); 1361 assertNull(view.getBackground()); 1362 assertEquals(100, view.getSuggestedMinimumWidth()); 1363 1364 // drawable minimum width is larger than mMinWidth 1365 view.setBackgroundDrawable(d); 1366 view.setMinimumWidth(drawableMinimumWidth - 10); 1367 assertEquals(drawableMinimumWidth, view.getSuggestedMinimumWidth()); 1368 1369 // drawable minimum width is smaller than mMinWidth 1370 view.setMinimumWidth(drawableMinimumWidth + 10); 1371 assertEquals(drawableMinimumWidth + 10, view.getSuggestedMinimumWidth()); 1372 } 1373 1374 @TestTargetNew( 1375 level = TestLevel.COMPLETE, 1376 method = "setMinimumHeight", 1377 args = {int.class} 1378 ) testSetMinimumHeight()1379 public void testSetMinimumHeight() { 1380 MockView view = new MockView(mActivity); 1381 assertEquals(0, view.getSuggestedMinimumHeight()); 1382 1383 view.setMinimumHeight(100); 1384 assertEquals(100, view.getSuggestedMinimumHeight()); 1385 1386 view.setMinimumHeight(-100); 1387 assertEquals(-100, view.getSuggestedMinimumHeight()); 1388 } 1389 1390 @TestTargetNew( 1391 level = TestLevel.COMPLETE, 1392 method = "getSuggestedMinimumHeight", 1393 args = {} 1394 ) testGetSuggestedMinimumHeight()1395 public void testGetSuggestedMinimumHeight() { 1396 MockView view = new MockView(mActivity); 1397 Drawable d = mResources.getDrawable(R.drawable.scenery); 1398 int drawableMinimumHeight = d.getMinimumHeight(); 1399 1400 // drawable is null 1401 view.setMinimumHeight(100); 1402 assertNull(view.getBackground()); 1403 assertEquals(100, view.getSuggestedMinimumHeight()); 1404 1405 // drawable minimum height is larger than mMinHeight 1406 view.setBackgroundDrawable(d); 1407 view.setMinimumHeight(drawableMinimumHeight - 10); 1408 assertEquals(drawableMinimumHeight, view.getSuggestedMinimumHeight()); 1409 1410 // drawable minimum height is smaller than mMinHeight 1411 view.setMinimumHeight(drawableMinimumHeight + 10); 1412 assertEquals(drawableMinimumHeight + 10, view.getSuggestedMinimumHeight()); 1413 } 1414 1415 @TestTargets({ 1416 @TestTargetNew( 1417 level = TestLevel.COMPLETE, 1418 method = "setWillNotCacheDrawing", 1419 args = {boolean.class} 1420 ), 1421 @TestTargetNew( 1422 level = TestLevel.COMPLETE, 1423 method = "willNotCacheDrawing", 1424 args = {} 1425 ) 1426 }) testAccessWillNotCacheDrawing()1427 public void testAccessWillNotCacheDrawing() { 1428 View view = new View(mActivity); 1429 1430 assertFalse(view.willNotCacheDrawing()); 1431 1432 view.setWillNotCacheDrawing(true); 1433 assertTrue(view.willNotCacheDrawing()); 1434 } 1435 1436 @TestTargets({ 1437 @TestTargetNew( 1438 level = TestLevel.COMPLETE, 1439 method = "setDrawingCacheEnabled", 1440 args = {boolean.class} 1441 ), 1442 @TestTargetNew( 1443 level = TestLevel.COMPLETE, 1444 method = "isDrawingCacheEnabled", 1445 args = {} 1446 ) 1447 }) testAccessDrawingCacheEnabled()1448 public void testAccessDrawingCacheEnabled() { 1449 View view = new View(mActivity); 1450 1451 assertFalse(view.isDrawingCacheEnabled()); 1452 1453 view.setDrawingCacheEnabled(true); 1454 assertTrue(view.isDrawingCacheEnabled()); 1455 } 1456 1457 @TestTargetNew( 1458 level = TestLevel.COMPLETE, 1459 method = "getDrawingCache", 1460 args = {} 1461 ) testGetDrawingCache()1462 public void testGetDrawingCache() { 1463 MockView view = new MockView(mActivity); 1464 1465 // should not call buildDrawingCache when getDrawingCache 1466 assertNull(view.getDrawingCache()); 1467 1468 // should call buildDrawingCache when getDrawingCache 1469 view = (MockView) mActivity.findViewById(R.id.mock_view); 1470 view.setDrawingCacheEnabled(true); 1471 Bitmap bitmap1 = view.getDrawingCache(); 1472 assertNotNull(bitmap1); 1473 assertEquals(view.getWidth(), bitmap1.getWidth()); 1474 assertEquals(view.getHeight(), bitmap1.getHeight()); 1475 1476 view.setWillNotCacheDrawing(true); 1477 assertNull(view.getDrawingCache()); 1478 1479 view.setWillNotCacheDrawing(false); 1480 // build a new drawingcache 1481 Bitmap bitmap2 = view.getDrawingCache(); 1482 assertNotSame(bitmap1, bitmap2); 1483 } 1484 1485 @TestTargets({ 1486 @TestTargetNew( 1487 level = TestLevel.COMPLETE, 1488 method = "destroyDrawingCache", 1489 args = {} 1490 ), 1491 @TestTargetNew( 1492 level = TestLevel.COMPLETE, 1493 method = "buildDrawingCache", 1494 args = {} 1495 ) 1496 }) testBuildAndDestroyDrawingCache()1497 public void testBuildAndDestroyDrawingCache() { 1498 MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 1499 1500 assertNull(view.getDrawingCache()); 1501 1502 view.buildDrawingCache(); 1503 Bitmap bitmap = view.getDrawingCache(); 1504 assertNotNull(bitmap); 1505 assertEquals(view.getWidth(), bitmap.getWidth()); 1506 assertEquals(view.getHeight(), bitmap.getHeight()); 1507 1508 view.destroyDrawingCache(); 1509 assertNull(view.getDrawingCache()); 1510 } 1511 1512 @TestTargets({ 1513 @TestTargetNew( 1514 level = TestLevel.COMPLETE, 1515 method = "setWillNotDraw", 1516 args = {boolean.class} 1517 ), 1518 @TestTargetNew( 1519 level = TestLevel.COMPLETE, 1520 method = "willNotDraw", 1521 args = {} 1522 ) 1523 }) testAccessWillNotDraw()1524 public void testAccessWillNotDraw() { 1525 View view = new View(mActivity); 1526 1527 assertFalse(view.willNotDraw()); 1528 1529 view.setWillNotDraw(true); 1530 assertTrue(view.willNotDraw()); 1531 } 1532 1533 @TestTargets({ 1534 @TestTargetNew( 1535 level = TestLevel.COMPLETE, 1536 method = "getDrawingCacheQuality", 1537 args = {} 1538 ), 1539 @TestTargetNew( 1540 level = TestLevel.COMPLETE, 1541 method = "setDrawingCacheQuality", 1542 args = {int.class} 1543 ) 1544 }) testAccessDrawingCacheQuality()1545 public void testAccessDrawingCacheQuality() { 1546 View view = new View(mActivity); 1547 1548 assertEquals(0, view.getDrawingCacheQuality()); 1549 1550 view.setDrawingCacheQuality(1); 1551 assertEquals(0, view.getDrawingCacheQuality()); 1552 1553 view.setDrawingCacheQuality(0x00100000); 1554 assertEquals(0x00100000, view.getDrawingCacheQuality()); 1555 1556 view.setDrawingCacheQuality(0x00080000); 1557 assertEquals(0x00080000, view.getDrawingCacheQuality()); 1558 1559 view.setDrawingCacheQuality(0xffffffff); 1560 // 0x00180000 is View.DRAWING_CACHE_QUALITY_MASK 1561 assertEquals(0x00180000, view.getDrawingCacheQuality()); 1562 } 1563 1564 @TestTargetNew( 1565 level = TestLevel.COMPLETE, 1566 method = "dispatchSetSelected", 1567 args = {boolean.class} 1568 ) 1569 @ToBeFixed(bug = "1695243", explanation = "the java doc said it dispatch setSelected to" + 1570 " all of this View's children, but it doesn't.") testDispatchSetSelected()1571 public void testDispatchSetSelected() { 1572 MockView mockView1 = new MockView(mActivity); 1573 MockView mockView2 = new MockView(mActivity); 1574 mockView1.setParent(mMockParent); 1575 mockView2.setParent(mMockParent); 1576 1577 mMockParent.dispatchSetSelected(true); 1578 assertFalse(mockView1.isSelected()); 1579 assertFalse(mockView2.isSelected()); 1580 1581 mMockParent.dispatchSetSelected(false); 1582 assertFalse(mockView1.isSelected()); 1583 assertFalse(mockView2.isSelected()); 1584 } 1585 1586 @TestTargets({ 1587 @TestTargetNew( 1588 level = TestLevel.COMPLETE, 1589 method = "setSelected", 1590 args = {boolean.class} 1591 ), 1592 @TestTargetNew( 1593 level = TestLevel.COMPLETE, 1594 method = "isSelected", 1595 args = {} 1596 ) 1597 }) testAccessSelected()1598 public void testAccessSelected() { 1599 View view = new View(mActivity); 1600 1601 assertFalse(view.isSelected()); 1602 1603 view.setSelected(true); 1604 assertTrue(view.isSelected()); 1605 } 1606 1607 @TestTargetNew( 1608 level = TestLevel.COMPLETE, 1609 method = "dispatchSetPressed", 1610 args = {boolean.class} 1611 ) 1612 @ToBeFixed(bug = "1695243", explanation = "the java doc said it dispatch setPressed to" + 1613 " all of this View's children, but it doesn't.") testDispatchSetPressed()1614 public void testDispatchSetPressed() { 1615 MockView mockView1 = new MockView(mActivity); 1616 MockView mockView2 = new MockView(mActivity); 1617 mockView1.setParent(mMockParent); 1618 mockView2.setParent(mMockParent); 1619 1620 mMockParent.dispatchSetPressed(true); 1621 assertFalse(mockView1.isPressed()); 1622 assertFalse(mockView2.isPressed()); 1623 1624 mMockParent.dispatchSetPressed(false); 1625 assertFalse(mockView1.isPressed()); 1626 assertFalse(mockView2.isPressed()); 1627 } 1628 1629 @TestTargets({ 1630 @TestTargetNew( 1631 level = TestLevel.COMPLETE, 1632 method = "setPressed", 1633 args = {boolean.class} 1634 ), 1635 @TestTargetNew( 1636 level = TestLevel.COMPLETE, 1637 method = "isPressed", 1638 args = {} 1639 ) 1640 }) testAccessPressed()1641 public void testAccessPressed() { 1642 View view = new View(mActivity); 1643 1644 assertFalse(view.isPressed()); 1645 1646 view.setPressed(true); 1647 assertTrue(view.isPressed()); 1648 } 1649 1650 @TestTargets({ 1651 @TestTargetNew( 1652 level = TestLevel.COMPLETE, 1653 method = "setSoundEffectsEnabled", 1654 args = {boolean.class} 1655 ), 1656 @TestTargetNew( 1657 level = TestLevel.COMPLETE, 1658 method = "isSoundEffectsEnabled", 1659 args = {} 1660 ) 1661 }) testAccessSoundEffectsEnabled()1662 public void testAccessSoundEffectsEnabled() { 1663 View view = new View(mActivity); 1664 1665 assertTrue(view.isSoundEffectsEnabled()); 1666 1667 view.setSoundEffectsEnabled(false); 1668 assertFalse(view.isSoundEffectsEnabled()); 1669 } 1670 1671 @TestTargets({ 1672 @TestTargetNew( 1673 level = TestLevel.COMPLETE, 1674 method = "setKeepScreenOn", 1675 args = {boolean.class} 1676 ), 1677 @TestTargetNew( 1678 level = TestLevel.COMPLETE, 1679 method = "getKeepScreenOn", 1680 args = {} 1681 ) 1682 }) testAccessKeepScreenOn()1683 public void testAccessKeepScreenOn() { 1684 View view = new View(mActivity); 1685 1686 assertFalse(view.getKeepScreenOn()); 1687 1688 view.setKeepScreenOn(true); 1689 assertTrue(view.getKeepScreenOn()); 1690 } 1691 1692 @TestTargets({ 1693 @TestTargetNew( 1694 level = TestLevel.COMPLETE, 1695 method = "setDuplicateParentStateEnabled", 1696 args = {boolean.class} 1697 ), 1698 @TestTargetNew( 1699 level = TestLevel.COMPLETE, 1700 method = "isDuplicateParentStateEnabled", 1701 args = {} 1702 ) 1703 }) testAccessDuplicateParentStateEnabled()1704 public void testAccessDuplicateParentStateEnabled() { 1705 View view = new View(mActivity); 1706 1707 assertFalse(view.isDuplicateParentStateEnabled()); 1708 1709 view.setDuplicateParentStateEnabled(true); 1710 assertTrue(view.isDuplicateParentStateEnabled()); 1711 } 1712 1713 @TestTargets({ 1714 @TestTargetNew( 1715 level = TestLevel.COMPLETE, 1716 method = "setEnabled", 1717 args = {boolean.class} 1718 ), 1719 @TestTargetNew( 1720 level = TestLevel.COMPLETE, 1721 method = "isEnabled", 1722 args = {} 1723 ) 1724 }) testAccessEnabled()1725 public void testAccessEnabled() { 1726 View view = new View(mActivity); 1727 1728 assertTrue(view.isEnabled()); 1729 1730 view.setEnabled(false); 1731 assertFalse(view.isEnabled()); 1732 } 1733 1734 @TestTargets({ 1735 @TestTargetNew( 1736 level = TestLevel.COMPLETE, 1737 method = "setSaveEnabled", 1738 args = {boolean.class} 1739 ), 1740 @TestTargetNew( 1741 level = TestLevel.COMPLETE, 1742 method = "isSaveEnabled", 1743 args = {} 1744 ) 1745 }) testAccessSaveEnabled()1746 public void testAccessSaveEnabled() { 1747 View view = new View(mActivity); 1748 1749 assertTrue(view.isSaveEnabled()); 1750 1751 view.setSaveEnabled(false); 1752 assertFalse(view.isSaveEnabled()); 1753 } 1754 1755 @TestTargetNew( 1756 level = TestLevel.COMPLETE, 1757 method = "showContextMenu", 1758 args = {} 1759 ) 1760 @ToBeFixed(bug = "1695243", explanation = "the javadoc for showContextMenu() is incomplete." + 1761 "1. not clear what is supposed to happen if view hasn't a parent.") testShowContextMenu()1762 public void testShowContextMenu() { 1763 MockView view = new MockView(mActivity); 1764 1765 assertNull(view.getParent()); 1766 try { 1767 view.showContextMenu(); 1768 fail("should throw NullPointerException"); 1769 } catch (NullPointerException e) { 1770 } 1771 1772 view.setParent(mMockParent); 1773 assertFalse(mMockParent.hasShowContextMenuForChild()); 1774 1775 assertFalse(view.showContextMenu()); 1776 assertTrue(mMockParent.hasShowContextMenuForChild()); 1777 } 1778 1779 @TestTargetNew( 1780 level = TestLevel.COMPLETE, 1781 method = "fitSystemWindows", 1782 args = {android.graphics.Rect.class} 1783 ) 1784 @ToBeFixed(bug = "", explanation = "can not test the cast when fitSystemWindows return" + 1785 " true, because it's a protected method, we have to test it by MockView, but" + 1786 " we can not construct a MockView instance which is FITS_SYSTEM_WINDOWS") testFitSystemWindows()1787 public void testFitSystemWindows() { 1788 final XmlResourceParser parser = mResources.getLayout(R.layout.view_layout); 1789 final AttributeSet attrs = Xml.asAttributeSet(parser); 1790 Rect insets = new Rect(10, 20, 30, 50); 1791 1792 MockView view = new MockView(mActivity); 1793 assertFalse(view.fitSystemWindows(insets)); 1794 assertFalse(view.fitSystemWindows(null)); 1795 1796 view = new MockView(mActivity, attrs, com.android.internal.R.attr.fitsSystemWindows); 1797 assertFalse(view.fitSystemWindows(insets)); 1798 assertFalse(view.fitSystemWindows(null)); 1799 } 1800 1801 @TestTargetNew( 1802 level = TestLevel.COMPLETE, 1803 method = "performClick", 1804 args = {} 1805 ) testPerformClick()1806 public void testPerformClick() { 1807 View view = new View(mActivity); 1808 OnClickListenerImpl listener = new OnClickListenerImpl(); 1809 1810 assertFalse(view.performClick()); 1811 1812 assertFalse(listener.hasOnClick()); 1813 view.setOnClickListener(listener); 1814 1815 assertTrue(view.performClick()); 1816 assertTrue(listener.hasOnClick()); 1817 1818 view.setOnClickListener(null); 1819 assertFalse(view.performClick()); 1820 } 1821 1822 @TestTargetNew( 1823 level = TestLevel.COMPLETE, 1824 method = "setOnClickListener", 1825 args = {android.view.View.OnClickListener.class} 1826 ) testSetOnClickListener()1827 public void testSetOnClickListener() { 1828 View view = new View(mActivity); 1829 assertFalse(view.performClick()); 1830 assertFalse(view.isClickable()); 1831 1832 view.setOnClickListener(null); 1833 assertFalse(view.performClick()); 1834 assertTrue(view.isClickable()); 1835 1836 view.setOnClickListener(new OnClickListenerImpl()); 1837 assertTrue(view.performClick()); 1838 assertTrue(view.isClickable()); 1839 } 1840 1841 @TestTargetNew( 1842 level = TestLevel.COMPLETE, 1843 method = "performLongClick", 1844 args = {} 1845 ) 1846 @ToBeFixed(bug = "1695243", explanation = "the javadoc for performLongClick() is incomplete." + 1847 "1. not clear what is supposed to happen if view hasn't a parent.") testPerformLongClick()1848 public void testPerformLongClick() { 1849 MockView view = new MockView(mActivity); 1850 OnLongClickListenerImpl listener = new OnLongClickListenerImpl(); 1851 1852 try { 1853 view.performLongClick(); 1854 fail("should throw NullPointerException"); 1855 } catch (NullPointerException e) { 1856 } 1857 1858 view.setParent(mMockParent); 1859 assertFalse(mMockParent.hasShowContextMenuForChild()); 1860 assertFalse(view.performLongClick()); 1861 assertTrue(mMockParent.hasShowContextMenuForChild()); 1862 1863 view.setOnLongClickListener(listener); 1864 mMockParent.reset(); 1865 assertFalse(mMockParent.hasShowContextMenuForChild()); 1866 assertFalse(listener.hasOnLongClick()); 1867 assertTrue(view.performLongClick()); 1868 assertFalse(mMockParent.hasShowContextMenuForChild()); 1869 assertTrue(listener.hasOnLongClick()); 1870 } 1871 1872 @TestTargetNew( 1873 level = TestLevel.COMPLETE, 1874 method = "setOnLongClickListener", 1875 args = {android.view.View.OnLongClickListener.class} 1876 ) testSetOnLongClickListener()1877 public void testSetOnLongClickListener() { 1878 MockView view = new MockView(mActivity); 1879 view.setParent(mMockParent); 1880 assertFalse(view.performLongClick()); 1881 assertFalse(view.isLongClickable()); 1882 1883 view.setOnLongClickListener(null); 1884 assertFalse(view.performLongClick()); 1885 assertTrue(view.isLongClickable()); 1886 1887 view.setOnLongClickListener(new OnLongClickListenerImpl()); 1888 assertTrue(view.performLongClick()); 1889 assertTrue(view.isLongClickable()); 1890 } 1891 1892 @TestTargets({ 1893 @TestTargetNew( 1894 level = TestLevel.COMPLETE, 1895 method = "setOnFocusChangeListener", 1896 args = {android.view.View.OnFocusChangeListener.class} 1897 ), 1898 @TestTargetNew( 1899 level = TestLevel.COMPLETE, 1900 method = "getOnFocusChangeListener", 1901 args = {} 1902 ) 1903 }) testAccessOnFocusChangeListener()1904 public void testAccessOnFocusChangeListener() { 1905 View view = new View(mActivity); 1906 OnFocusChangeListener listener = new OnFocusChangeListenerImpl(); 1907 1908 assertNull(view.getOnFocusChangeListener()); 1909 1910 view.setOnFocusChangeListener(listener); 1911 assertSame(listener, view.getOnFocusChangeListener()); 1912 } 1913 1914 @TestTargets({ 1915 @TestTargetNew( 1916 level = TestLevel.COMPLETE, 1917 method = "setNextFocusUpId", 1918 args = {int.class} 1919 ), 1920 @TestTargetNew( 1921 level = TestLevel.COMPLETE, 1922 method = "getNextFocusUpId", 1923 args = {} 1924 ) 1925 }) testAccessNextFocusUpId()1926 public void testAccessNextFocusUpId() { 1927 View view = new View(mActivity); 1928 1929 assertEquals(View.NO_ID, view.getNextFocusUpId()); 1930 1931 view.setNextFocusUpId(1); 1932 assertEquals(1, view.getNextFocusUpId()); 1933 1934 view.setNextFocusUpId(Integer.MAX_VALUE); 1935 assertEquals(Integer.MAX_VALUE, view.getNextFocusUpId()); 1936 1937 view.setNextFocusUpId(Integer.MIN_VALUE); 1938 assertEquals(Integer.MIN_VALUE, view.getNextFocusUpId()); 1939 } 1940 1941 @TestTargets({ 1942 @TestTargetNew( 1943 level = TestLevel.COMPLETE, 1944 method = "setNextFocusDownId", 1945 args = {int.class} 1946 ), 1947 @TestTargetNew( 1948 level = TestLevel.COMPLETE, 1949 method = "getNextFocusDownId", 1950 args = {} 1951 ) 1952 }) testAccessNextFocusDownId()1953 public void testAccessNextFocusDownId() { 1954 View view = new View(mActivity); 1955 1956 assertEquals(View.NO_ID, view.getNextFocusDownId()); 1957 1958 view.setNextFocusDownId(1); 1959 assertEquals(1, view.getNextFocusDownId()); 1960 1961 view.setNextFocusDownId(Integer.MAX_VALUE); 1962 assertEquals(Integer.MAX_VALUE, view.getNextFocusDownId()); 1963 1964 view.setNextFocusDownId(Integer.MIN_VALUE); 1965 assertEquals(Integer.MIN_VALUE, view.getNextFocusDownId()); 1966 } 1967 1968 @TestTargets({ 1969 @TestTargetNew( 1970 level = TestLevel.COMPLETE, 1971 method = "setNextFocusLeftId", 1972 args = {int.class} 1973 ), 1974 @TestTargetNew( 1975 level = TestLevel.COMPLETE, 1976 method = "getNextFocusLeftId", 1977 args = {} 1978 ) 1979 }) testAccessNextFocusLeftId()1980 public void testAccessNextFocusLeftId() { 1981 View view = new View(mActivity); 1982 1983 assertEquals(View.NO_ID, view.getNextFocusLeftId()); 1984 1985 view.setNextFocusLeftId(1); 1986 assertEquals(1, view.getNextFocusLeftId()); 1987 1988 view.setNextFocusLeftId(Integer.MAX_VALUE); 1989 assertEquals(Integer.MAX_VALUE, view.getNextFocusLeftId()); 1990 1991 view.setNextFocusLeftId(Integer.MIN_VALUE); 1992 assertEquals(Integer.MIN_VALUE, view.getNextFocusLeftId()); 1993 } 1994 1995 @TestTargets({ 1996 @TestTargetNew( 1997 level = TestLevel.COMPLETE, 1998 method = "setNextFocusRightId", 1999 args = {int.class} 2000 ), 2001 @TestTargetNew( 2002 level = TestLevel.COMPLETE, 2003 method = "getNextFocusRightId", 2004 args = {} 2005 ) 2006 }) testAccessNextFocusRightId()2007 public void testAccessNextFocusRightId() { 2008 View view = new View(mActivity); 2009 2010 assertEquals(View.NO_ID, view.getNextFocusRightId()); 2011 2012 view.setNextFocusRightId(1); 2013 assertEquals(1, view.getNextFocusRightId()); 2014 2015 view.setNextFocusRightId(Integer.MAX_VALUE); 2016 assertEquals(Integer.MAX_VALUE, view.getNextFocusRightId()); 2017 2018 view.setNextFocusRightId(Integer.MIN_VALUE); 2019 assertEquals(Integer.MIN_VALUE, view.getNextFocusRightId()); 2020 } 2021 2022 @TestTargets({ 2023 @TestTargetNew( 2024 level = TestLevel.COMPLETE, 2025 method = "setMeasuredDimension", 2026 args = {int.class, int.class} 2027 ), 2028 @TestTargetNew( 2029 level = TestLevel.COMPLETE, 2030 method = "getMeasuredWidth", 2031 args = {} 2032 ), 2033 @TestTargetNew( 2034 level = TestLevel.COMPLETE, 2035 method = "getMeasuredHeight", 2036 args = {} 2037 ) 2038 }) testAccessMeasuredDimension()2039 public void testAccessMeasuredDimension() { 2040 MockView view = new MockView(mActivity); 2041 assertEquals(0, view.getMeasuredWidth()); 2042 assertEquals(0, view.getMeasuredHeight()); 2043 2044 view.setMeasuredDimensionWrapper(20, 30); 2045 assertEquals(20, view.getMeasuredWidth()); 2046 assertEquals(30, view.getMeasuredHeight()); 2047 2048 view.setMeasuredDimensionWrapper(-20, -30); 2049 assertEquals(-20, view.getMeasuredWidth()); 2050 assertEquals(-30, view.getMeasuredHeight()); 2051 } 2052 2053 @TestTargets({ 2054 @TestTargetNew( 2055 level = TestLevel.COMPLETE, 2056 method = "measure", 2057 args = {int.class, int.class} 2058 ), 2059 @TestTargetNew( 2060 level = TestLevel.COMPLETE, 2061 method = "onMeasure", 2062 args = {int.class, int.class} 2063 ) 2064 }) testMeasure()2065 public void testMeasure() throws Throwable { 2066 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 2067 assertTrue(view.hasCalledOnMeasure()); 2068 assertEquals(100, view.getMeasuredWidth()); 2069 assertEquals(200, view.getMeasuredHeight()); 2070 2071 view.reset(); 2072 runTestOnUiThread(new Runnable() { 2073 public void run() { 2074 view.requestLayout(); 2075 } 2076 }); 2077 getInstrumentation().waitForIdleSync(); 2078 assertTrue(view.hasCalledOnMeasure()); 2079 assertEquals(100, view.getMeasuredWidth()); 2080 assertEquals(200, view.getMeasuredHeight()); 2081 2082 view.reset(); 2083 final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(200, 100); 2084 runTestOnUiThread(new Runnable() { 2085 public void run() { 2086 view.setLayoutParams(layoutParams); 2087 } 2088 }); 2089 getInstrumentation().waitForIdleSync(); 2090 assertTrue(view.hasCalledOnMeasure()); 2091 assertEquals(200, view.getMeasuredWidth()); 2092 assertEquals(100, view.getMeasuredHeight()); 2093 } 2094 2095 @TestTargets({ 2096 @TestTargetNew( 2097 level = TestLevel.COMPLETE, 2098 method = "setLayoutParams", 2099 args = {android.view.ViewGroup.LayoutParams.class} 2100 ), 2101 @TestTargetNew( 2102 level = TestLevel.COMPLETE, 2103 method = "getLayoutParams", 2104 args = {} 2105 ) 2106 }) 2107 @ToBeFixed(bug = "1695243", explanation = "the javadoc for setLayoutParams() is incomplete." + 2108 "1. not clear what is supposed to happen if params is null.") testAccessLayoutParams()2109 public void testAccessLayoutParams() { 2110 View view = new View(mActivity); 2111 ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(10, 20); 2112 2113 assertNull(view.getLayoutParams()); 2114 2115 try { 2116 view.setLayoutParams(null); 2117 fail("should throw NullPointerException"); 2118 } catch (NullPointerException e) { 2119 } 2120 2121 assertFalse(view.isLayoutRequested()); 2122 view.setLayoutParams(params); 2123 assertSame(params, view.getLayoutParams()); 2124 assertTrue(view.isLayoutRequested()); 2125 } 2126 2127 @TestTargetNew( 2128 level = TestLevel.COMPLETE, 2129 method = "isShown", 2130 args = {} 2131 ) testIsShown()2132 public void testIsShown() { 2133 MockView view = new MockView(mActivity); 2134 2135 view.setVisibility(View.INVISIBLE); 2136 assertFalse(view.isShown()); 2137 2138 view.setVisibility(View.VISIBLE); 2139 assertNull(view.getParent()); 2140 assertFalse(view.isShown()); 2141 2142 view.setParent(mMockParent); 2143 // mMockParent is not a instance of ViewRoot 2144 assertFalse(view.isShown()); 2145 } 2146 2147 @TestTargetNew( 2148 level = TestLevel.COMPLETE, 2149 method = "getDrawingTime", 2150 args = {} 2151 ) testGetDrawingTime()2152 public void testGetDrawingTime() { 2153 View view = new View(mActivity); 2154 // mAttachInfo is null 2155 assertEquals(0, view.getDrawingTime()); 2156 2157 // mAttachInfo is not null 2158 view = mActivity.findViewById(R.id.fit_windows); 2159 assertEquals(SystemClock.uptimeMillis(), view.getDrawingTime(), 1000); 2160 } 2161 2162 @TestTargetNew( 2163 level = TestLevel.COMPLETE, 2164 method = "scheduleDrawable", 2165 args = {android.graphics.drawable.Drawable.class, java.lang.Runnable.class, long.class} 2166 ) testScheduleDrawable()2167 public void testScheduleDrawable() { 2168 View view = new View(mActivity); 2169 Drawable drawable = new StateListDrawable(); 2170 Runnable what = new Runnable() { 2171 public void run() { 2172 // do nothing 2173 } 2174 }; 2175 2176 // mAttachInfo is null 2177 view.scheduleDrawable(drawable, what, 1000); 2178 2179 view.setBackgroundDrawable(drawable); 2180 view.scheduleDrawable(drawable, what, 1000); 2181 2182 view.scheduleDrawable(null, null, -1000); 2183 2184 // mAttachInfo is not null 2185 view = mActivity.findViewById(R.id.fit_windows); 2186 view.scheduleDrawable(drawable, what, 1000); 2187 2188 view.scheduleDrawable(view.getBackground(), what, 1000); 2189 view.unscheduleDrawable(view.getBackground(), what); 2190 2191 view.scheduleDrawable(null, null, -1000); 2192 } 2193 2194 @TestTargets({ 2195 @TestTargetNew( 2196 level = TestLevel.COMPLETE, 2197 method = "unscheduleDrawable", 2198 args = {android.graphics.drawable.Drawable.class} 2199 ), 2200 @TestTargetNew( 2201 level = TestLevel.COMPLETE, 2202 method = "unscheduleDrawable", 2203 args = {android.graphics.drawable.Drawable.class, java.lang.Runnable.class} 2204 ) 2205 }) testUnscheduleDrawable()2206 public void testUnscheduleDrawable() { 2207 View view = new View(mActivity); 2208 Drawable drawable = new StateListDrawable(); 2209 Runnable what = new Runnable() { 2210 public void run() { 2211 // do nothing 2212 } 2213 }; 2214 2215 // mAttachInfo is null 2216 view.unscheduleDrawable(drawable, what); 2217 2218 view.setBackgroundDrawable(drawable); 2219 view.unscheduleDrawable(drawable); 2220 2221 view.unscheduleDrawable(null, null); 2222 view.unscheduleDrawable(null); 2223 2224 // mAttachInfo is not null 2225 view = mActivity.findViewById(R.id.fit_windows); 2226 view.unscheduleDrawable(drawable); 2227 2228 view.scheduleDrawable(view.getBackground(), what, 1000); 2229 view.unscheduleDrawable(view.getBackground(), what); 2230 2231 view.unscheduleDrawable(null); 2232 view.unscheduleDrawable(null, null); 2233 } 2234 2235 @TestTargetNew( 2236 level = TestLevel.COMPLETE, 2237 method = "getWindowVisibility", 2238 args = {} 2239 ) testGetWindowVisibility()2240 public void testGetWindowVisibility() { 2241 View view = new View(mActivity); 2242 // mAttachInfo is null 2243 assertEquals(View.GONE, view.getWindowVisibility()); 2244 2245 // mAttachInfo is not null 2246 view = mActivity.findViewById(R.id.fit_windows); 2247 assertEquals(View.VISIBLE, view.getWindowVisibility()); 2248 } 2249 2250 @TestTargetNew( 2251 level = TestLevel.COMPLETE, 2252 method = "getWindowToken", 2253 args = {} 2254 ) testGetWindowToken()2255 public void testGetWindowToken() { 2256 View view = new View(mActivity); 2257 // mAttachInfo is null 2258 assertNull(view.getWindowToken()); 2259 2260 // mAttachInfo is not null 2261 view = mActivity.findViewById(R.id.fit_windows); 2262 assertNotNull(view.getWindowToken()); 2263 } 2264 2265 @TestTargetNew( 2266 level = TestLevel.COMPLETE, 2267 method = "hasWindowFocus", 2268 args = {} 2269 ) testHasWindowFocus()2270 public void testHasWindowFocus() { 2271 View view = new View(mActivity); 2272 // mAttachInfo is null 2273 assertFalse(view.hasWindowFocus()); 2274 2275 // mAttachInfo is not null 2276 view = mActivity.findViewById(R.id.fit_windows); 2277 assertTrue(view.hasWindowFocus()); 2278 } 2279 2280 @TestTargetNew( 2281 level = TestLevel.COMPLETE, 2282 method = "getHandler", 2283 args = {} 2284 ) 2285 @ToBeFixed(bug = "1400249", explanation = "View#getHandler() is protected, so we should" + 2286 " test it by MockView, but we cannot access mAttachInfo which is package protected") testGetHandler()2287 public void testGetHandler() { 2288 MockView view = new MockView(mActivity); 2289 // mAttachInfo is null 2290 assertNull(view.getHandler()); 2291 } 2292 2293 @TestTargetNew( 2294 level = TestLevel.COMPLETE, 2295 method = "removeCallbacks", 2296 args = {java.lang.Runnable.class} 2297 ) testRemoveCallbacks()2298 public void testRemoveCallbacks() throws InterruptedException { 2299 final long delay = 500L; 2300 View view = mActivity.findViewById(R.id.mock_view); 2301 MockRunnable runner = new MockRunnable(); 2302 assertTrue(view.postDelayed(runner, delay)); 2303 assertTrue(view.removeCallbacks(runner)); 2304 assertTrue(view.removeCallbacks(null)); 2305 assertTrue(view.removeCallbacks(new MockRunnable())); 2306 Thread.sleep(delay * 2); 2307 assertFalse(runner.hasRun); 2308 // check that the runner actually works 2309 runner = new MockRunnable(); 2310 assertTrue(view.postDelayed(runner, delay)); 2311 Thread.sleep(delay * 2); 2312 assertTrue(runner.hasRun); 2313 } 2314 2315 @TestTargetNew( 2316 level = TestLevel.COMPLETE, 2317 method = "cancelLongPress", 2318 args = {} 2319 ) 2320 @ToBeFixed(bug = "1400249", explanation = "it's hard to do unit test for this method, " + 2321 "should be tested by functional test") testCancelLongPress()2322 public void testCancelLongPress() { 2323 View view = new View(mActivity); 2324 // mAttachInfo is null 2325 view.cancelLongPress(); 2326 2327 // mAttachInfo is not null 2328 view = mActivity.findViewById(R.id.fit_windows); 2329 view.cancelLongPress(); 2330 } 2331 2332 @TestTargetNew( 2333 level = TestLevel.COMPLETE, 2334 method = "getViewTreeObserver", 2335 args = {} 2336 ) 2337 @ToBeFixed(bug = "1400249", explanation = "we cannot access both mFloatingTreeObserver" + 2338 " and mAttachInfo which are package protected") testGetViewTreeObserver()2339 public void testGetViewTreeObserver() { 2340 View view = new View(mActivity); 2341 // mAttachInfo is null 2342 assertNotNull(view.getViewTreeObserver()); 2343 2344 // mAttachInfo is not null 2345 view = mActivity.findViewById(R.id.fit_windows); 2346 assertNotNull(view.getViewTreeObserver()); 2347 } 2348 2349 @TestTargetNew( 2350 level = TestLevel.COMPLETE, 2351 method = "getWindowAttachCount", 2352 args = {} 2353 ) 2354 @ToBeFixed(bug = "1400249", explanation = "View#getHandler() is protected, so we should" + 2355 " test it by MockView, but we cannot access method" + 2356 " View#dispatchAttachedToWindow(AttachInfo, int) which is package protected" + 2357 " to update mWindowAttachCount") testGetWindowAttachCount()2358 public void testGetWindowAttachCount() { 2359 MockView view = new MockView(mActivity); 2360 // mAttachInfo is null 2361 assertEquals(0, view.getWindowAttachCount()); 2362 } 2363 2364 @TestTargets({ 2365 @TestTargetNew( 2366 level = TestLevel.COMPLETE, 2367 method = "onAttachedToWindow", 2368 args = {} 2369 ), 2370 @TestTargetNew( 2371 level = TestLevel.COMPLETE, 2372 method = "onDetachedFromWindow", 2373 args = {} 2374 ) 2375 }) 2376 @UiThreadTest testOnAttachedToAndDetachedFromWindow()2377 public void testOnAttachedToAndDetachedFromWindow() { 2378 MockView mockView = new MockView(mActivity); 2379 ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root); 2380 2381 viewGroup.addView(mockView); 2382 assertTrue(mockView.hasCalledOnAttachedToWindow()); 2383 2384 viewGroup.removeView(mockView); 2385 assertTrue(mockView.hasCalledOnDetachedFromWindow()); 2386 2387 mockView.reset(); 2388 mActivity.setContentView(mockView); 2389 assertTrue(mockView.hasCalledOnAttachedToWindow()); 2390 2391 mActivity.setContentView(R.layout.view_layout); 2392 assertTrue(mockView.hasCalledOnDetachedFromWindow()); 2393 } 2394 2395 @TestTargetNew( 2396 level = TestLevel.COMPLETE, 2397 method = "getLocationInWindow", 2398 args = {int[].class} 2399 ) 2400 @ToBeFixed(bug = "1695243", explanation = 2401 "the javadoc for getLocationInWindow() is incomplete." + 2402 "1. not clear what is supposed to happen if the input int[] is null or short than 2.") testGetLocationInWindow()2403 public void testGetLocationInWindow() { 2404 int[] location = new int[] { -1, -1 }; 2405 2406 View layout = mActivity.findViewById(R.id.viewlayout_root); 2407 int[] layoutLocation = new int[] { -1, -1 }; 2408 layout.getLocationInWindow(layoutLocation); 2409 2410 final View mockView = mActivity.findViewById(R.id.mock_view); 2411 mockView.getLocationInWindow(location); 2412 assertEquals(layoutLocation[0], location[0]); 2413 assertEquals(layoutLocation[1], location[1]); 2414 2415 View scrollView = mActivity.findViewById(R.id.scroll_view); 2416 scrollView.getLocationInWindow(location); 2417 assertEquals(layoutLocation[0], location[0]); 2418 assertEquals(layoutLocation[1] + mockView.getHeight(), location[1]); 2419 2420 try { 2421 mockView.getLocationInWindow(null); 2422 fail("should throw IllegalArgumentException"); 2423 } catch (IllegalArgumentException e) { 2424 } 2425 2426 try { 2427 mockView.getLocationInWindow(new int[] { 0 }); 2428 fail("should throw IllegalArgumentException"); 2429 } catch (IllegalArgumentException e) { 2430 } 2431 } 2432 2433 @TestTargetNew( 2434 level = TestLevel.COMPLETE, 2435 method = "getLocationOnScreen", 2436 args = {int[].class} 2437 ) 2438 @ToBeFixed(bug = "1695243", explanation = 2439 "the javadoc for getLocationOnScreen() is incomplete." + 2440 "1. not clear what is supposed to happen if mAttachInfo is null.") testGetLocationOnScreen()2441 public void testGetLocationOnScreen() { 2442 View view = new View(mActivity); 2443 int[] location = new int[] { -1, -1 }; 2444 2445 // mAttachInfo is not null 2446 View layout = mActivity.findViewById(R.id.viewlayout_root); 2447 int[] layoutLocation = new int[] { -1, -1 }; 2448 layout.getLocationOnScreen(layoutLocation); 2449 2450 View mockView = mActivity.findViewById(R.id.mock_view); 2451 mockView.getLocationOnScreen(location); 2452 assertEquals(0, location[0]); 2453 assertEquals(layoutLocation[1], location[1]); 2454 2455 View scrollView = mActivity.findViewById(R.id.scroll_view); 2456 scrollView.getLocationOnScreen(location); 2457 assertEquals(0, location[0]); 2458 assertEquals(layoutLocation[1] + mockView.getHeight(), location[1]); 2459 2460 try { 2461 scrollView.getLocationOnScreen(null); 2462 fail("should throw IllegalArgumentException"); 2463 } catch (IllegalArgumentException e) { 2464 } 2465 2466 try { 2467 scrollView.getLocationOnScreen(new int[] { 0 }); 2468 fail("should throw IllegalArgumentException"); 2469 } catch (IllegalArgumentException e) { 2470 } 2471 } 2472 2473 @TestTargetNew( 2474 level = TestLevel.COMPLETE, 2475 method = "addTouchables", 2476 args = {java.util.ArrayList.class} 2477 ) 2478 @ToBeFixed(bug = "1695243", explanation = "the javadoc for addTouchables() is incomplete." + 2479 "1. not clear what is supposed to happen if the input ArrayList<View> is null.") testAddTouchables()2480 public void testAddTouchables() { 2481 View view = new View(mActivity); 2482 ArrayList<View> result = new ArrayList<View>(); 2483 assertEquals(0, result.size()); 2484 2485 view.addTouchables(result); 2486 assertEquals(0, result.size()); 2487 2488 view.setClickable(true); 2489 view.addTouchables(result); 2490 assertEquals(1, result.size()); 2491 assertSame(view, result.get(0)); 2492 2493 try { 2494 view.addTouchables(null); 2495 fail("should throw NullPointerException"); 2496 } catch (NullPointerException e) { 2497 } 2498 2499 result.clear(); 2500 view.setEnabled(false); 2501 assertTrue(view.isClickable()); 2502 view.addTouchables(result); 2503 assertEquals(0, result.size()); 2504 } 2505 2506 @TestTargetNew( 2507 level = TestLevel.COMPLETE, 2508 method = "getTouchables", 2509 args = {} 2510 ) testGetTouchables()2511 public void testGetTouchables() { 2512 View view = new View(mActivity); 2513 ArrayList<View> result; 2514 2515 result = view.getTouchables(); 2516 assertEquals(0, result.size()); 2517 2518 view.setClickable(true); 2519 result = view.getTouchables(); 2520 assertEquals(1, result.size()); 2521 assertSame(view, result.get(0)); 2522 2523 result.clear(); 2524 view.setEnabled(false); 2525 assertTrue(view.isClickable()); 2526 result = view.getTouchables(); 2527 assertEquals(0, result.size()); 2528 } 2529 2530 @TestTargets({ 2531 @TestTargetNew( 2532 level = TestLevel.COMPLETE, 2533 method = "inflate", 2534 args = {android.content.Context.class, int.class, android.view.ViewGroup.class} 2535 ), 2536 @TestTargetNew( 2537 level = TestLevel.COMPLETE, 2538 method = "onFinishInflate", 2539 args = {} 2540 ) 2541 }) testInflate()2542 public void testInflate() { 2543 View view = View.inflate(mActivity, R.layout.view_layout, null); 2544 assertNotNull(view); 2545 assertTrue(view instanceof LinearLayout); 2546 2547 MockView mockView = (MockView) view.findViewById(R.id.mock_view); 2548 assertNotNull(mockView); 2549 assertTrue(mockView.hasCalledOnFinishInflate()); 2550 } 2551 2552 @TestTargetNew( 2553 level = TestLevel.COMPLETE, 2554 method = "isInTouchMode", 2555 args = {} 2556 ) testIsInTouchMode()2557 public void testIsInTouchMode() { 2558 View view = new View(mActivity); 2559 // mAttachInfo is null 2560 assertFalse(view.isInTouchMode()); 2561 2562 // mAttachInfo is not null 2563 view = mActivity.findViewById(R.id.fit_windows); 2564 assertFalse(view.isInTouchMode()); 2565 } 2566 2567 @TestTargetNew( 2568 level = TestLevel.COMPLETE, 2569 method = "isInEditMode", 2570 args = {} 2571 ) testIsInEditMode()2572 public void testIsInEditMode() { 2573 View view = new View(mActivity); 2574 assertFalse(view.isInEditMode()); 2575 } 2576 2577 @TestTargetNew( 2578 level = TestLevel.COMPLETE, 2579 method = "postInvalidate", 2580 args = {} 2581 ) testPostInvalidate1()2582 public void testPostInvalidate1() { 2583 View view = new View(mActivity); 2584 // mAttachInfo is null 2585 view.postInvalidate(); 2586 2587 // mAttachInfo is not null 2588 view = mActivity.findViewById(R.id.fit_windows); 2589 view.postInvalidate(); 2590 } 2591 2592 @TestTargetNew( 2593 level = TestLevel.COMPLETE, 2594 method = "postInvalidate", 2595 args = {int.class, int.class, int.class, int.class} 2596 ) testPostInvalidate2()2597 public void testPostInvalidate2() { 2598 View view = new View(mActivity); 2599 // mAttachInfo is null 2600 view.postInvalidate(0, 1, 2, 3); 2601 2602 // mAttachInfo is not null 2603 view = mActivity.findViewById(R.id.fit_windows); 2604 view.postInvalidate(10, 20, 30, 40); 2605 view.postInvalidate(0, -20, -30, -40); 2606 } 2607 2608 @TestTargets({ 2609 @TestTargetNew( 2610 level = TestLevel.COMPLETE, 2611 method = "postInvalidateDelayed", 2612 args = {long.class} 2613 ), 2614 @TestTargetNew( 2615 level = TestLevel.COMPLETE, 2616 method = "postInvalidateDelayed", 2617 args = {long.class, int.class, int.class, int.class, int.class} 2618 ) 2619 }) testPostInvalidateDelayed()2620 public void testPostInvalidateDelayed() { 2621 View view = new View(mActivity); 2622 // mAttachInfo is null 2623 view.postInvalidateDelayed(1000); 2624 view.postInvalidateDelayed(500, 0, 0, 100, 200); 2625 2626 // mAttachInfo is not null 2627 view = mActivity.findViewById(R.id.fit_windows); 2628 view.postInvalidateDelayed(1000); 2629 view.postInvalidateDelayed(500, 0, 0, 100, 200); 2630 view.postInvalidateDelayed(-1); 2631 } 2632 2633 @TestTargetNew( 2634 level = TestLevel.COMPLETE, 2635 method = "post", 2636 args = {java.lang.Runnable.class} 2637 ) testPost()2638 public void testPost() { 2639 View view = new View(mActivity); 2640 MockRunnable action = new MockRunnable(); 2641 2642 // mAttachInfo is null 2643 assertTrue(view.post(action)); 2644 assertTrue(view.post(null)); 2645 2646 // mAttachInfo is not null 2647 view = mActivity.findViewById(R.id.fit_windows); 2648 assertTrue(view.post(action)); 2649 assertTrue(view.post(null)); 2650 } 2651 2652 @TestTargetNew( 2653 level = TestLevel.COMPLETE, 2654 method = "postDelayed", 2655 args = {java.lang.Runnable.class, long.class} 2656 ) testPostDelayed()2657 public void testPostDelayed() { 2658 View view = new View(mActivity); 2659 MockRunnable action = new MockRunnable(); 2660 2661 // mAttachInfo is null 2662 assertTrue(view.postDelayed(action, 1000)); 2663 assertTrue(view.postDelayed(null, -1)); 2664 2665 // mAttachInfo is not null 2666 view = mActivity.findViewById(R.id.fit_windows); 2667 assertTrue(view.postDelayed(action, 1000)); 2668 assertTrue(view.postDelayed(null, 0)); 2669 } 2670 2671 @TestTargetNew( 2672 level = TestLevel.NOT_FEASIBLE, 2673 method = "playSoundEffect", 2674 args = {int.class} 2675 ) 2676 @UiThreadTest testPlaySoundEffect()2677 public void testPlaySoundEffect() { 2678 MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 2679 // sound effect enabled 2680 view.playSoundEffect(SoundEffectConstants.CLICK); 2681 2682 // sound effect disabled 2683 view.setSoundEffectsEnabled(false); 2684 view.playSoundEffect(SoundEffectConstants.NAVIGATION_DOWN); 2685 2686 // no way to assert the soundConstant be really played. 2687 } 2688 2689 @TestTargetNew( 2690 level = TestLevel.COMPLETE, 2691 method = "onKeyShortcut", 2692 args = {int.class, android.view.KeyEvent.class} 2693 ) testOnKeyShortcut()2694 public void testOnKeyShortcut() throws Throwable { 2695 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 2696 runTestOnUiThread(new Runnable() { 2697 public void run() { 2698 view.setFocusable(true); 2699 } 2700 }); 2701 2702 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU); 2703 getInstrumentation().sendKeySync(event); 2704 event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0); 2705 getInstrumentation().sendKeySync(event); 2706 assertTrue(view.hasCalledOnKeyShortcut()); 2707 } 2708 2709 @TestTargetNew( 2710 level = TestLevel.COMPLETE, 2711 method = "onKeyMultiple", 2712 args = {int.class, int.class, android.view.KeyEvent.class} 2713 ) testOnKeyMultiple()2714 public void testOnKeyMultiple() throws Throwable { 2715 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 2716 runTestOnUiThread(new Runnable() { 2717 public void run() { 2718 view.setFocusable(true); 2719 } 2720 }); 2721 2722 assertFalse(view.hasCalledOnKeyMultiple()); 2723 KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_MULTIPLE, KeyEvent.KEYCODE_ENTER); 2724 getInstrumentation().sendKeySync(keyEvent); 2725 assertTrue(view.hasCalledOnKeyMultiple()); 2726 } 2727 2728 @TestTargetNew( 2729 level = TestLevel.COMPLETE, 2730 method = "dispatchKeyShortcutEvent", 2731 args = {android.view.KeyEvent.class} 2732 ) 2733 @ToBeFixed(bug = "1695243", explanation = "should add @throws clause into javadoc of " + 2734 "View#dispatchKeyShortcutEvent(KeyEvent) when the input KeyEvent is null") 2735 @UiThreadTest testDispatchKeyShortcutEvent()2736 public void testDispatchKeyShortcutEvent() { 2737 MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 2738 view.setFocusable(true); 2739 2740 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0); 2741 view.dispatchKeyShortcutEvent(event); 2742 assertTrue(view.hasCalledOnKeyShortcut()); 2743 2744 try { 2745 view.dispatchKeyShortcutEvent(null); 2746 fail("should throw NullPointerException"); 2747 } catch (NullPointerException e) { 2748 } 2749 } 2750 2751 @TestTargetNew( 2752 level = TestLevel.COMPLETE, 2753 method = "onTrackballEvent", 2754 args = {android.view.MotionEvent.class} 2755 ) testOnTrackballEvent()2756 public void testOnTrackballEvent() throws Throwable { 2757 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 2758 runTestOnUiThread(new Runnable() { 2759 public void run() { 2760 view.setEnabled(true); 2761 view.setFocusable(true); 2762 view.requestFocus(); 2763 } 2764 }); 2765 getInstrumentation().waitForIdleSync(); 2766 2767 int[] xy = new int[2]; 2768 view.getLocationOnScreen(xy); 2769 2770 final int viewWidth = view.getWidth(); 2771 final int viewHeight = view.getHeight(); 2772 final float x = xy[0] + viewWidth / 2.0f; 2773 final float y = xy[1] + viewHeight / 2.0f; 2774 2775 long downTime = SystemClock.uptimeMillis(); 2776 long eventTime = SystemClock.uptimeMillis(); 2777 MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, 2778 x, y, 0); 2779 getInstrumentation().sendTrackballEventSync(event); 2780 getInstrumentation().waitForIdleSync(); 2781 assertTrue(view.hasCalledOnTrackballEvent()); 2782 } 2783 2784 @TestTargetNew( 2785 level = TestLevel.COMPLETE, 2786 method = "dispatchTrackballEvent", 2787 args = {android.view.MotionEvent.class} 2788 ) 2789 @ToBeFixed(bug = "1695243", explanation = "the javadoc for dispatchTrackballEvent() is " + 2790 "incomplete. It passes a trackball motion event down to itself even if it is not " + 2791 "the focused view.") 2792 @UiThreadTest testDispatchTrackballEvent()2793 public void testDispatchTrackballEvent() { 2794 ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root); 2795 MockView mockView1 = new MockView(mActivity); 2796 MockView mockView2 = new MockView(mActivity); 2797 viewGroup.addView(mockView1); 2798 viewGroup.addView(mockView2); 2799 mockView1.setFocusable(true); 2800 mockView2.setFocusable(true); 2801 mockView2.requestFocus(); 2802 2803 long downTime = SystemClock.uptimeMillis(); 2804 long eventTime = SystemClock.uptimeMillis(); 2805 MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, 2806 1, 2, 0); 2807 mockView1.dispatchTrackballEvent(event); 2808 // issue 1695243 2809 // It passes a trackball motion event down to itself even if it is not the focused view. 2810 assertTrue(mockView1.hasCalledOnTrackballEvent()); 2811 assertFalse(mockView2.hasCalledOnTrackballEvent()); 2812 2813 mockView1.reset(); 2814 mockView2.reset(); 2815 downTime = SystemClock.uptimeMillis(); 2816 eventTime = SystemClock.uptimeMillis(); 2817 event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, 1, 2, 0); 2818 mockView2.dispatchTrackballEvent(event); 2819 assertFalse(mockView1.hasCalledOnTrackballEvent()); 2820 assertTrue(mockView2.hasCalledOnTrackballEvent()); 2821 } 2822 2823 @TestTargetNew( 2824 level = TestLevel.COMPLETE, 2825 method = "dispatchUnhandledMove", 2826 args = {android.view.View.class, int.class} 2827 ) testDispatchUnhandledMove()2828 public void testDispatchUnhandledMove() throws Throwable { 2829 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 2830 runTestOnUiThread(new Runnable() { 2831 public void run() { 2832 view.setFocusable(true); 2833 view.requestFocus(); 2834 } 2835 }); 2836 getInstrumentation().waitForIdleSync(); 2837 2838 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT); 2839 getInstrumentation().sendKeySync(event); 2840 2841 assertTrue(view.hasCalledDispatchUnhandledMove()); 2842 } 2843 2844 @TestTargets({ 2845 @TestTargetNew( 2846 level = TestLevel.COMPLETE, 2847 method = "onWindowVisibilityChanged", 2848 args = {int.class} 2849 ), 2850 @TestTargetNew( 2851 level = TestLevel.COMPLETE, 2852 method = "dispatchWindowVisibilityChanged", 2853 args = {int.class} 2854 ) 2855 }) testWindowVisibilityChanged()2856 public void testWindowVisibilityChanged() throws Throwable { 2857 final MockView mockView = new MockView(mActivity); 2858 final ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root); 2859 2860 runTestOnUiThread(new Runnable() { 2861 public void run() { 2862 viewGroup.addView(mockView); 2863 } 2864 }); 2865 getInstrumentation().waitForIdleSync(); 2866 assertTrue(mockView.hasCalledOnWindowVisibilityChanged()); 2867 2868 mockView.reset(); 2869 runTestOnUiThread(new Runnable() { 2870 public void run() { 2871 getActivity().setVisible(false); 2872 } 2873 }); 2874 getInstrumentation().waitForIdleSync(); 2875 assertTrue(mockView.hasCalledDispatchWindowVisibilityChanged()); 2876 assertTrue(mockView.hasCalledOnWindowVisibilityChanged()); 2877 2878 mockView.reset(); 2879 runTestOnUiThread(new Runnable() { 2880 public void run() { 2881 getActivity().setVisible(true); 2882 } 2883 }); 2884 getInstrumentation().waitForIdleSync(); 2885 assertTrue(mockView.hasCalledDispatchWindowVisibilityChanged()); 2886 assertTrue(mockView.hasCalledOnWindowVisibilityChanged()); 2887 2888 mockView.reset(); 2889 runTestOnUiThread(new Runnable() { 2890 public void run() { 2891 viewGroup.removeView(mockView); 2892 } 2893 }); 2894 getInstrumentation().waitForIdleSync(); 2895 assertTrue(mockView.hasCalledOnWindowVisibilityChanged()); 2896 } 2897 2898 @TestTargetNew( 2899 level = TestLevel.COMPLETE, 2900 method = "getLocalVisibleRect", 2901 args = {android.graphics.Rect.class} 2902 ) 2903 @ToBeFixed(bug = "1695243", explanation = 2904 "the javadoc for getLocalVisibleRect() is incomplete." + 2905 "1. not clear what is supposed to happen if the input Rect is null.") testGetLocalVisibleRect()2906 public void testGetLocalVisibleRect() throws Throwable { 2907 final View view = mActivity.findViewById(R.id.mock_view); 2908 Rect rect = new Rect(); 2909 2910 assertTrue(view.getLocalVisibleRect(rect)); 2911 assertEquals(0, rect.left); 2912 assertEquals(0, rect.top); 2913 assertEquals(100, rect.right); 2914 assertEquals(200, rect.bottom); 2915 2916 final LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(0, 300); 2917 runTestOnUiThread(new Runnable() { 2918 public void run() { 2919 view.setLayoutParams(layoutParams1); 2920 } 2921 }); 2922 getInstrumentation().waitForIdleSync(); 2923 assertFalse(view.getLocalVisibleRect(rect)); 2924 2925 final LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(200, -10); 2926 runTestOnUiThread(new Runnable() { 2927 public void run() { 2928 view.setLayoutParams(layoutParams2); 2929 } 2930 }); 2931 getInstrumentation().waitForIdleSync(); 2932 assertFalse(view.getLocalVisibleRect(rect)); 2933 2934 final LinearLayout.LayoutParams layoutParams3 = new LinearLayout.LayoutParams(200, 300); 2935 runTestOnUiThread(new Runnable() { 2936 public void run() { 2937 view.setLayoutParams(layoutParams3); 2938 view.scrollTo(20, -30); 2939 } 2940 }); 2941 getInstrumentation().waitForIdleSync(); 2942 assertTrue(view.getLocalVisibleRect(rect)); 2943 assertEquals(20, rect.left); 2944 assertEquals(-30, rect.top); 2945 assertEquals(200 + 20, rect.right); 2946 assertEquals(300 - 30, rect.bottom); 2947 2948 try { 2949 view.getLocalVisibleRect(null); 2950 fail("should throw NullPointerException"); 2951 } catch (NullPointerException e) { 2952 } 2953 } 2954 2955 @TestTargetNew( 2956 level = TestLevel.COMPLETE, 2957 method = "mergeDrawableStates", 2958 args = {int[].class, int[].class} 2959 ) 2960 @ToBeFixed(bug = "1695243", explanation = 2961 "the javadoc for mergeDrawableStates() is incomplete." + 2962 "1. not clear what is supposed to happen if the input int[] is null or" + 2963 " baseState is not long enough to append additionalState.") testMergeDrawableStates()2964 public void testMergeDrawableStates() { 2965 MockView view = new MockView(mActivity); 2966 2967 int[] states = view.mergeDrawableStatesWrapper(new int[] { 0, 1, 2, 0, 0 }, 2968 new int[] { 3 }); 2969 assertNotNull(states); 2970 assertEquals(5, states.length); 2971 assertEquals(0, states[0]); 2972 assertEquals(1, states[1]); 2973 assertEquals(2, states[2]); 2974 assertEquals(3, states[3]); 2975 assertEquals(0, states[4]); 2976 2977 try { 2978 view.mergeDrawableStatesWrapper(new int[] { 1, 2 }, new int[] { 3 }); 2979 fail("should throw IndexOutOfBoundsException"); 2980 } catch (IndexOutOfBoundsException e) { 2981 } 2982 2983 try { 2984 view.mergeDrawableStatesWrapper(null, new int[] { 0 }); 2985 fail("should throw NullPointerException"); 2986 } catch (NullPointerException e) { 2987 } 2988 2989 try { 2990 view.mergeDrawableStatesWrapper(new int [] { 0 }, null); 2991 fail("should throw NullPointerException"); 2992 } catch (NullPointerException e) { 2993 } 2994 } 2995 2996 @TestTargets({ 2997 @TestTargetNew( 2998 level = TestLevel.NOT_FEASIBLE, 2999 method = "onRestoreInstanceState", 3000 args = {android.os.Parcelable.class} 3001 ), 3002 @TestTargetNew( 3003 level = TestLevel.NOT_FEASIBLE, 3004 method = "onSaveInstanceState", 3005 args = {} 3006 ) 3007 }) testOnSaveAndRestoreInstanceState()3008 public void testOnSaveAndRestoreInstanceState() { 3009 // it is hard to simulate operation to make callback be called. 3010 } 3011 3012 @TestTargets({ 3013 @TestTargetNew( 3014 level = TestLevel.COMPLETE, 3015 method = "dispatchRestoreInstanceState", 3016 args = {android.util.SparseArray.class} 3017 ), 3018 @TestTargetNew( 3019 level = TestLevel.COMPLETE, 3020 method = "restoreHierarchyState", 3021 args = {android.util.SparseArray.class} 3022 ), 3023 @TestTargetNew( 3024 level = TestLevel.COMPLETE, 3025 method = "dispatchSaveInstanceState", 3026 args = {android.util.SparseArray.class} 3027 ), 3028 @TestTargetNew( 3029 level = TestLevel.COMPLETE, 3030 method = "saveHierarchyState", 3031 args = {android.util.SparseArray.class} 3032 ) 3033 }) 3034 @ToBeFixed(bug = "1695243", explanation = "the javadoc of saveHierarchyState() and " + 3035 "restoreHierarchyState() are incomplete." + 3036 "1, not clear what is supposed to happen if input SparseArray<Parcelable> is null." + 3037 "2, no description about IllegalArgumentException which thrown in a certain condition.") testSaveAndRestoreHierarchyState()3038 public void testSaveAndRestoreHierarchyState() { 3039 int viewId = R.id.mock_view; 3040 MockView view = (MockView) mActivity.findViewById(viewId); 3041 SparseArray<Parcelable> container = new SparseArray<Parcelable>(); 3042 view.saveHierarchyState(container); 3043 assertTrue(view.hasCalledDispatchSaveInstanceState()); 3044 assertTrue(view.hasCalledOnSaveInstanceState()); 3045 assertEquals(viewId, container.keyAt(0)); 3046 3047 view.reset(); 3048 container.put(R.id.mock_view, BaseSavedState.EMPTY_STATE); 3049 view.restoreHierarchyState(container); 3050 assertTrue(view.hasCalledDispatchRestoreInstanceState()); 3051 assertTrue(view.hasCalledOnRestoreInstanceState()); 3052 container.clear(); 3053 view.saveHierarchyState(container); 3054 assertTrue(view.hasCalledDispatchSaveInstanceState()); 3055 assertTrue(view.hasCalledOnSaveInstanceState()); 3056 assertEquals(viewId, container.keyAt(0)); 3057 3058 container.clear(); 3059 container.put(viewId, new BaseSavedState(BaseSavedState.EMPTY_STATE)); 3060 try { 3061 view.restoreHierarchyState(container); 3062 fail("should throw IllegalArgumentException"); 3063 } catch (IllegalArgumentException e) { 3064 // expected 3065 } 3066 3067 try { 3068 view.restoreHierarchyState(null); 3069 fail("should throw NullPointerException"); 3070 } catch (NullPointerException e) { 3071 // expected 3072 } 3073 3074 try { 3075 view.saveHierarchyState(null); 3076 fail("should throw NullPointerException"); 3077 } catch (NullPointerException e) { 3078 // expected 3079 } 3080 } 3081 3082 @TestTargets({ 3083 @TestTargetNew( 3084 level = TestLevel.COMPLETE, 3085 method = "onKeyDown", 3086 args = {int.class, android.view.KeyEvent.class} 3087 ), 3088 @TestTargetNew( 3089 level = TestLevel.COMPLETE, 3090 method = "onKeyUp", 3091 args = {int.class, android.view.KeyEvent.class} 3092 ) 3093 }) testOnKeyDownOrUp()3094 public void testOnKeyDownOrUp() throws Throwable { 3095 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 3096 runTestOnUiThread(new Runnable() { 3097 public void run() { 3098 view.setFocusable(true); 3099 } 3100 }); 3101 3102 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0); 3103 getInstrumentation().sendKeySync(event); 3104 assertTrue(view.hasCalledOnKeyDown()); 3105 3106 event = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_0); 3107 getInstrumentation().sendKeySync(event); 3108 assertTrue(view.hasCalledOnKeyUp()); 3109 3110 view.reset(); 3111 assertTrue(view.isEnabled()); 3112 assertFalse(view.isClickable()); 3113 assertFalse(view.isPressed()); 3114 event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER); 3115 getInstrumentation().sendKeySync(event); 3116 assertFalse(view.isPressed()); 3117 assertTrue(view.hasCalledOnKeyDown()); 3118 3119 runTestOnUiThread(new Runnable() { 3120 public void run() { 3121 view.setEnabled(true); 3122 view.setClickable(true); 3123 } 3124 }); 3125 view.reset(); 3126 OnClickListenerImpl listener = new OnClickListenerImpl(); 3127 view.setOnClickListener(listener); 3128 3129 assertFalse(view.isPressed()); 3130 event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER); 3131 getInstrumentation().sendKeySync(event); 3132 assertTrue(view.isPressed()); 3133 assertTrue(view.hasCalledOnKeyDown()); 3134 event = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_ENTER); 3135 getInstrumentation().sendKeySync(event); 3136 assertFalse(view.isPressed()); 3137 assertTrue(view.hasCalledOnKeyUp()); 3138 assertTrue(listener.hasOnClick()); 3139 3140 view.setPressed(false); 3141 listener.reset(); 3142 view.reset(); 3143 3144 assertFalse(view.isPressed()); 3145 event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER); 3146 getInstrumentation().sendKeySync(event); 3147 assertTrue(view.isPressed()); 3148 assertTrue(view.hasCalledOnKeyDown()); 3149 event = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER); 3150 getInstrumentation().sendKeySync(event); 3151 assertFalse(view.isPressed()); 3152 assertTrue(view.hasCalledOnKeyUp()); 3153 assertTrue(listener.hasOnClick()); 3154 } 3155 3156 @TestTargets({ 3157 @TestTargetNew( 3158 level = TestLevel.COMPLETE, 3159 method = "dispatchKeyEvent", 3160 args = {android.view.KeyEvent.class} 3161 ), 3162 @TestTargetNew( 3163 level = TestLevel.COMPLETE, 3164 method = "setOnKeyListener", 3165 args = {android.view.View.OnKeyListener.class} 3166 ) 3167 }) 3168 @ToBeFixed(bug = "1695243", explanation = "the javadoc for dispatchKeyEvent() is incomplete." + 3169 "1. not clear what is supposed to happen if the KeyEvent is null." + 3170 "2. When the view has NOT focus, it dispatchs to itself, which disobey the javadoc.") 3171 @UiThreadTest testDispatchKeyEvent()3172 public void testDispatchKeyEvent() { 3173 MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 3174 MockView mockView1 = new MockView(mActivity); 3175 MockView mockView2 = new MockView(mActivity); 3176 ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root); 3177 viewGroup.addView(mockView1); 3178 viewGroup.addView(mockView2); 3179 view.setFocusable(true); 3180 mockView1.setFocusable(true); 3181 mockView2.setFocusable(true); 3182 3183 assertFalse(view.hasCalledOnKeyDown()); 3184 assertFalse(mockView1.hasCalledOnKeyDown()); 3185 assertFalse(mockView2.hasCalledOnKeyDown()); 3186 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0); 3187 assertFalse(view.dispatchKeyEvent(event)); 3188 assertTrue(view.hasCalledOnKeyDown()); 3189 assertFalse(mockView1.hasCalledOnKeyDown()); 3190 assertFalse(mockView2.hasCalledOnKeyDown()); 3191 3192 view.reset(); 3193 mockView1.reset(); 3194 mockView2.reset(); 3195 assertFalse(view.hasCalledOnKeyDown()); 3196 assertFalse(mockView1.hasCalledOnKeyDown()); 3197 assertFalse(mockView2.hasCalledOnKeyDown()); 3198 event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0); 3199 assertFalse(mockView1.dispatchKeyEvent(event)); 3200 assertFalse(view.hasCalledOnKeyDown()); 3201 // issue 1695243 3202 // When the view has NOT focus, it dispatches to itself, which disobey the javadoc. 3203 assertTrue(mockView1.hasCalledOnKeyDown()); 3204 assertFalse(mockView2.hasCalledOnKeyDown()); 3205 3206 assertFalse(view.hasCalledOnKeyUp()); 3207 event = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_0); 3208 assertFalse(view.dispatchKeyEvent(event)); 3209 assertTrue(view.hasCalledOnKeyUp()); 3210 3211 assertFalse(view.hasCalledOnKeyMultiple()); 3212 event = new KeyEvent(1, 2, KeyEvent.ACTION_MULTIPLE, KeyEvent.KEYCODE_0, 2); 3213 assertFalse(view.dispatchKeyEvent(event)); 3214 assertTrue(view.hasCalledOnKeyMultiple()); 3215 3216 try { 3217 view.dispatchKeyEvent(null); 3218 fail("should throw NullPointerException"); 3219 } catch (NullPointerException e) { 3220 // expected 3221 } 3222 3223 view.reset(); 3224 event = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_0); 3225 OnKeyListenerImpl listener = new OnKeyListenerImpl(); 3226 view.setOnKeyListener(listener); 3227 assertFalse(listener.hasOnKey()); 3228 assertTrue(view.dispatchKeyEvent(event)); 3229 assertTrue(listener.hasOnKey()); 3230 assertFalse(view.hasCalledOnKeyUp()); 3231 } 3232 3233 @TestTargets({ 3234 @TestTargetNew( 3235 level = TestLevel.COMPLETE, 3236 method = "dispatchTouchEvent", 3237 args = {android.view.MotionEvent.class} 3238 ), 3239 @TestTargetNew( 3240 level = TestLevel.COMPLETE, 3241 method = "setOnTouchListener", 3242 args = {android.view.View.OnTouchListener.class} 3243 ) 3244 }) 3245 @ToBeFixed(bug = "1695243", explanation = "the javadoc for dispatchTouchEvent() is incomplete" + 3246 "1. it passes the touch screen motion event down to itself even if it is not " + 3247 " the target view.") 3248 @UiThreadTest testDispatchTouchEvent()3249 public void testDispatchTouchEvent() { 3250 ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root); 3251 MockView mockView1 = new MockView(mActivity); 3252 MockView mockView2 = new MockView(mActivity); 3253 viewGroup.addView(mockView1); 3254 viewGroup.addView(mockView2); 3255 3256 int[] xy = new int[2]; 3257 mockView1.getLocationOnScreen(xy); 3258 3259 final int viewWidth = mockView1.getWidth(); 3260 final int viewHeight = mockView1.getHeight(); 3261 final float x = xy[0] + viewWidth / 2.0f; 3262 final float y = xy[1] + viewHeight / 2.0f; 3263 3264 long downTime = SystemClock.uptimeMillis(); 3265 long eventTime = SystemClock.uptimeMillis(); 3266 MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, 3267 x, y, 0); 3268 3269 assertFalse(mockView1.hasCalledOnTouchEvent()); 3270 assertFalse(mockView1.dispatchTouchEvent(event)); 3271 assertTrue(mockView1.hasCalledOnTouchEvent()); 3272 3273 assertFalse(mockView2.hasCalledOnTouchEvent()); 3274 assertFalse(mockView2.dispatchTouchEvent(event)); 3275 // issue 1695243 3276 // it passes the touch screen motion event down to itself even if it is not the target view. 3277 assertTrue(mockView2.hasCalledOnTouchEvent()); 3278 3279 assertFalse(mockView1.dispatchTouchEvent(null)); 3280 3281 mockView1.reset(); 3282 OnTouchListenerImpl listener = new OnTouchListenerImpl(); 3283 mockView1.setOnTouchListener(listener); 3284 assertFalse(listener.hasOnTouch()); 3285 assertTrue(mockView1.dispatchTouchEvent(event)); 3286 assertTrue(listener.hasOnTouch()); 3287 assertFalse(mockView1.hasCalledOnTouchEvent()); 3288 } 3289 3290 @TestTargetNew( 3291 level = TestLevel.COMPLETE, 3292 method = "invalidate", 3293 args = {} 3294 ) testInvalidate1()3295 public void testInvalidate1() throws Throwable { 3296 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 3297 assertTrue(view.hasCalledOnDraw()); 3298 3299 view.reset(); 3300 runTestOnUiThread(new Runnable() { 3301 public void run() { 3302 view.invalidate(); 3303 } 3304 }); 3305 getInstrumentation().waitForIdleSync(); 3306 assertTrue(view.hasCalledOnDraw()); 3307 3308 view.reset(); 3309 runTestOnUiThread(new Runnable() { 3310 public void run() { 3311 view.setVisibility(View.INVISIBLE); 3312 view.invalidate(); 3313 } 3314 }); 3315 getInstrumentation().waitForIdleSync(); 3316 assertFalse(view.hasCalledOnDraw()); 3317 } 3318 3319 @TestTargetNew( 3320 level = TestLevel.COMPLETE, 3321 method = "invalidate", 3322 args = {android.graphics.Rect.class} 3323 ) 3324 @ToBeFixed(bug = "1695243", explanation = "the javadoc for invalidate() is incomplete." + 3325 "1. not clear what is supposed to happen if the input Rect is null.") testInvalidate2()3326 public void testInvalidate2() throws Throwable { 3327 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 3328 assertTrue(view.hasCalledOnDraw()); 3329 3330 try { 3331 view.invalidate(null); 3332 fail("should throw NullPointerException"); 3333 } catch (NullPointerException e) { 3334 } 3335 3336 view.reset(); 3337 final Rect dirty = new Rect(view.getLeft() + 1, view.getTop() + 1, 3338 view.getLeft() + view.getWidth() / 2, view.getTop() + view.getHeight() / 2); 3339 runTestOnUiThread(new Runnable() { 3340 public void run() { 3341 view.invalidate(dirty); 3342 } 3343 }); 3344 getInstrumentation().waitForIdleSync(); 3345 assertTrue(view.hasCalledOnDraw()); 3346 3347 view.reset(); 3348 runTestOnUiThread(new Runnable() { 3349 public void run() { 3350 view.setVisibility(View.INVISIBLE); 3351 view.invalidate(dirty); 3352 } 3353 }); 3354 getInstrumentation().waitForIdleSync(); 3355 assertFalse(view.hasCalledOnDraw()); 3356 } 3357 3358 @TestTargetNew( 3359 level = TestLevel.COMPLETE, 3360 method = "invalidate", 3361 args = {int.class, int.class, int.class, int.class} 3362 ) testInvalidate3()3363 public void testInvalidate3() throws Throwable { 3364 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 3365 assertTrue(view.hasCalledOnDraw()); 3366 3367 view.reset(); 3368 final Rect dirty = new Rect(view.getLeft() + 1, view.getTop() + 1, 3369 view.getLeft() + view.getWidth() / 2, view.getTop() + view.getHeight() / 2); 3370 runTestOnUiThread(new Runnable() { 3371 public void run() { 3372 view.invalidate(dirty.left, dirty.top, dirty.right, dirty.bottom); 3373 } 3374 }); 3375 getInstrumentation().waitForIdleSync(); 3376 assertTrue(view.hasCalledOnDraw()); 3377 3378 view.reset(); 3379 runTestOnUiThread(new Runnable() { 3380 public void run() { 3381 view.setVisibility(View.INVISIBLE); 3382 view.invalidate(dirty.left, dirty.top, dirty.right, dirty.bottom); 3383 } 3384 }); 3385 getInstrumentation().waitForIdleSync(); 3386 assertFalse(view.hasCalledOnDraw()); 3387 } 3388 3389 @TestTargetNew( 3390 level = TestLevel.COMPLETE, 3391 method = "invalidateDrawable", 3392 args = {android.graphics.drawable.Drawable.class} 3393 ) 3394 @ToBeFixed(bug = "1695243", explanation = 3395 "the javadoc for invalidateDrawable() is incomplete." + 3396 "1. not clear what is supposed to happen if the input Drawable is null.") testInvalidateDrawable()3397 public void testInvalidateDrawable() throws Throwable { 3398 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 3399 final Drawable d1 = mResources.getDrawable(R.drawable.scenery); 3400 final Drawable d2 = mResources.getDrawable(R.drawable.pass); 3401 3402 view.reset(); 3403 runTestOnUiThread(new Runnable() { 3404 public void run() { 3405 view.setBackgroundDrawable(d1); 3406 view.invalidateDrawable(d1); 3407 } 3408 }); 3409 getInstrumentation().waitForIdleSync(); 3410 assertTrue(view.hasCalledOnDraw()); 3411 3412 view.reset(); 3413 runTestOnUiThread(new Runnable() { 3414 public void run() { 3415 view.invalidateDrawable(d2); 3416 } 3417 }); 3418 getInstrumentation().waitForIdleSync(); 3419 assertFalse(view.hasCalledOnDraw()); 3420 3421 MockView viewTestNull = new MockView(mActivity); 3422 try { 3423 viewTestNull.invalidateDrawable(null); 3424 fail("should throw NullPointerException"); 3425 } catch (NullPointerException e) { 3426 } 3427 } 3428 3429 @TestTargetNew( 3430 level = TestLevel.COMPLETE, 3431 method = "onFocusChanged", 3432 args = {boolean.class, int.class, android.graphics.Rect.class} 3433 ) 3434 @UiThreadTest testOnFocusChanged()3435 public void testOnFocusChanged() { 3436 MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 3437 3438 mActivity.findViewById(R.id.fit_windows).setFocusable(true); 3439 view.setFocusable(true); 3440 assertFalse(view.hasCalledOnFocusChanged()); 3441 3442 view.requestFocus(); 3443 assertTrue(view.hasCalledOnFocusChanged()); 3444 3445 view.reset(); 3446 view.clearFocus(); 3447 assertTrue(view.hasCalledOnFocusChanged()); 3448 } 3449 3450 @TestTargets({ 3451 @TestTargetNew( 3452 level = TestLevel.COMPLETE, 3453 method = "onCreateDrawableState", 3454 args = {int.class} 3455 ), 3456 @TestTargetNew( 3457 level = TestLevel.COMPLETE, 3458 method = "getDrawableState", 3459 args = {} 3460 ), 3461 @TestTargetNew( 3462 level = TestLevel.COMPLETE, 3463 method = "drawableStateChanged", 3464 args = {} 3465 ), 3466 @TestTargetNew( 3467 level = TestLevel.COMPLETE, 3468 method = "refreshDrawableState", 3469 args = {} 3470 ) 3471 }) testDrawableState()3472 public void testDrawableState() { 3473 MockView view = new MockView(mActivity); 3474 view.setParent(mMockParent); 3475 3476 assertFalse(view.hasCalledOnCreateDrawableState()); 3477 assertTrue(Arrays.equals(MockView.getEnabledStateSet(), view.getDrawableState())); 3478 assertTrue(view.hasCalledOnCreateDrawableState()); 3479 3480 view.reset(); 3481 assertFalse(view.hasCalledOnCreateDrawableState()); 3482 assertTrue(Arrays.equals(MockView.getEnabledStateSet(), view.getDrawableState())); 3483 assertFalse(view.hasCalledOnCreateDrawableState()); 3484 3485 view.reset(); 3486 assertFalse(view.hasCalledDrawableStateChanged()); 3487 view.setPressed(true); 3488 assertTrue(view.hasCalledDrawableStateChanged()); 3489 assertFalse(view.hasCalledOnCreateDrawableState()); 3490 assertTrue(Arrays.equals(MockView.getPressedEnabledStateSet(), view.getDrawableState())); 3491 assertTrue(view.hasCalledOnCreateDrawableState()); 3492 3493 view.reset(); 3494 mMockParent.reset(); 3495 assertFalse(view.hasCalledDrawableStateChanged()); 3496 assertFalse(mMockParent.hasChildDrawableStateChanged()); 3497 view.refreshDrawableState(); 3498 assertTrue(view.hasCalledDrawableStateChanged()); 3499 assertTrue(mMockParent.hasChildDrawableStateChanged()); 3500 assertFalse(view.hasCalledOnCreateDrawableState()); 3501 assertTrue(Arrays.equals(MockView.getPressedEnabledStateSet(), view.getDrawableState())); 3502 assertTrue(view.hasCalledOnCreateDrawableState()); 3503 } 3504 3505 @TestTargets({ 3506 @TestTargetNew( 3507 level = TestLevel.COMPLETE, 3508 method = "onWindowFocusChanged", 3509 args = {boolean.class} 3510 ), 3511 @TestTargetNew( 3512 level = TestLevel.COMPLETE, 3513 method = "dispatchWindowFocusChanged", 3514 args = {boolean.class} 3515 ) 3516 }) testWindowFocusChanged()3517 public void testWindowFocusChanged() { 3518 MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 3519 assertTrue(view.hasCalledOnWindowFocusChanged()); 3520 assertTrue(view.hasCalledDispatchWindowFocusChanged()); 3521 3522 view.reset(); 3523 assertFalse(view.hasCalledOnWindowFocusChanged()); 3524 assertFalse(view.hasCalledDispatchWindowFocusChanged()); 3525 3526 StubActivity activity = launchActivity("com.android.cts.stub", StubActivity.class, null); 3527 assertTrue(view.hasCalledOnWindowFocusChanged()); 3528 assertTrue(view.hasCalledDispatchWindowFocusChanged()); 3529 3530 activity.finish(); 3531 } 3532 3533 @TestTargets({ 3534 @TestTargetNew( 3535 level = TestLevel.COMPLETE, 3536 method = "draw", 3537 args = {android.graphics.Canvas.class} 3538 ), 3539 @TestTargetNew( 3540 level = TestLevel.COMPLETE, 3541 method = "onDraw", 3542 args = {android.graphics.Canvas.class} 3543 ), 3544 @TestTargetNew( 3545 level = TestLevel.COMPLETE, 3546 method = "dispatchDraw", 3547 args = {android.graphics.Canvas.class} 3548 ) 3549 }) testDraw()3550 public void testDraw() throws Throwable { 3551 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 3552 runTestOnUiThread(new Runnable() { 3553 public void run() { 3554 view.requestLayout(); 3555 } 3556 }); 3557 getInstrumentation().waitForIdleSync(); 3558 3559 assertTrue(view.hasCalledOnDraw()); 3560 assertTrue(view.hasCalledDispatchDraw()); 3561 } 3562 3563 @TestTargetNew( 3564 level = TestLevel.COMPLETE, 3565 method = "requestFocusFromTouch", 3566 args = {} 3567 ) testRequestFocusFromTouch()3568 public void testRequestFocusFromTouch() { 3569 View view = new View(mActivity); 3570 view.setFocusable(true); 3571 assertFalse(view.isFocused()); 3572 3573 view.requestFocusFromTouch(); 3574 assertTrue(view.isFocused()); 3575 3576 view.requestFocusFromTouch(); 3577 assertTrue(view.isFocused()); 3578 } 3579 3580 @TestTargetNew( 3581 level = TestLevel.COMPLETE, 3582 method = "requestRectangleOnScreen", 3583 args = {android.graphics.Rect.class, boolean.class} 3584 ) 3585 @ToBeFixed(bug = "1695243", explanation = 3586 "the javadoc for requestRectangleOnScreen() is incomplete." + 3587 "1. not clear what is supposed to happen if the input Rect is null.") testRequestRectangleOnScreen1()3588 public void testRequestRectangleOnScreen1() { 3589 MockView view = new MockView(mActivity); 3590 Rect rectangle = new Rect(10, 10, 20, 30); 3591 MockViewGroupParent parent = new MockViewGroupParent(mActivity); 3592 3593 // parent is null 3594 assertFalse(view.requestRectangleOnScreen(rectangle, true)); 3595 assertFalse(view.requestRectangleOnScreen(rectangle, false)); 3596 assertFalse(view.requestRectangleOnScreen(null, true)); 3597 3598 view.setParent(parent); 3599 view.scrollTo(1, 2); 3600 assertFalse(parent.hasRequestChildRectangleOnScreen()); 3601 3602 assertFalse(view.requestRectangleOnScreen(rectangle, true)); 3603 assertTrue(parent.hasRequestChildRectangleOnScreen()); 3604 3605 parent.reset(); 3606 view.scrollTo(11, 22); 3607 assertFalse(parent.hasRequestChildRectangleOnScreen()); 3608 3609 assertFalse(view.requestRectangleOnScreen(rectangle, true)); 3610 assertTrue(parent.hasRequestChildRectangleOnScreen()); 3611 3612 try { 3613 view.requestRectangleOnScreen(null, true); 3614 fail("should throw NullPointerException"); 3615 } catch (NullPointerException e) { 3616 } 3617 } 3618 3619 @TestTargetNew( 3620 level = TestLevel.COMPLETE, 3621 method = "requestRectangleOnScreen", 3622 args = {android.graphics.Rect.class} 3623 ) 3624 @ToBeFixed(bug = "1695243", explanation = 3625 "the javadoc for requestRectangleOnScreen() is incomplete." + 3626 "1. not clear what is supposed to happen if the input Rect is null.") testRequestRectangleOnScreen2()3627 public void testRequestRectangleOnScreen2() { 3628 MockView view = new MockView(mActivity); 3629 Rect rectangle = new Rect(); 3630 MockViewGroupParent parent = new MockViewGroupParent(mActivity); 3631 3632 // parent is null 3633 assertFalse(view.requestRectangleOnScreen(rectangle)); 3634 assertFalse(view.requestRectangleOnScreen(null)); 3635 assertEquals(0, rectangle.left); 3636 assertEquals(0, rectangle.top); 3637 assertEquals(0, rectangle.right); 3638 assertEquals(0, rectangle.bottom); 3639 3640 view.setParent(parent); 3641 view.scrollTo(1, 2); 3642 assertFalse(parent.hasRequestChildRectangleOnScreen()); 3643 3644 assertFalse(view.requestRectangleOnScreen(rectangle)); 3645 assertTrue(parent.hasRequestChildRectangleOnScreen()); 3646 assertEquals(-1, rectangle.left); 3647 assertEquals(-2, rectangle.top); 3648 assertEquals(-1, rectangle.right); 3649 assertEquals(-2, rectangle.bottom); 3650 3651 try { 3652 view.requestRectangleOnScreen(null); 3653 fail("should throw NullPointerException"); 3654 } catch (NullPointerException e) { 3655 } 3656 } 3657 3658 @TestTargetNew( 3659 level = TestLevel.COMPLETE, 3660 method = "onTouchEvent", 3661 args = {android.view.MotionEvent.class} 3662 ) testOnTouchEvent()3663 public void testOnTouchEvent() throws Throwable { 3664 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 3665 3666 assertTrue(view.isEnabled()); 3667 assertFalse(view.isClickable()); 3668 assertFalse(view.isLongClickable()); 3669 3670 TouchUtils.clickView(this, view); 3671 assertTrue(view.hasCalledOnTouchEvent()); 3672 3673 runTestOnUiThread(new Runnable() { 3674 public void run() { 3675 view.setEnabled(true); 3676 view.setClickable(true); 3677 view.setLongClickable(true); 3678 } 3679 }); 3680 getInstrumentation().waitForIdleSync(); 3681 assertTrue(view.isEnabled()); 3682 assertTrue(view.isClickable()); 3683 assertTrue(view.isLongClickable()); 3684 3685 // MotionEvent.ACTION_DOWN 3686 int[] xy = new int[2]; 3687 view.getLocationOnScreen(xy); 3688 3689 final int viewWidth = view.getWidth(); 3690 final int viewHeight = view.getHeight(); 3691 float x = xy[0] + viewWidth / 2.0f; 3692 float y = xy[1] + viewHeight / 2.0f; 3693 3694 long downTime = SystemClock.uptimeMillis(); 3695 long eventTime = SystemClock.uptimeMillis(); 3696 MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, 3697 x, y, 0); 3698 assertFalse(view.isPressed()); 3699 getInstrumentation().sendPointerSync(event); 3700 assertTrue(view.hasCalledOnTouchEvent()); 3701 assertTrue(view.isPressed()); 3702 3703 // MotionEvent.ACTION_MOVE 3704 // move out of the bound. 3705 view.reset(); 3706 downTime = SystemClock.uptimeMillis(); 3707 eventTime = SystemClock.uptimeMillis(); 3708 int slop = ViewConfiguration.get(mActivity).getScaledTouchSlop(); 3709 x = xy[0] + viewWidth + slop; 3710 y = xy[1] + viewHeight + slop; 3711 event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, x, y, 0); 3712 getInstrumentation().sendPointerSync(event); 3713 assertTrue(view.hasCalledOnTouchEvent()); 3714 assertFalse(view.isPressed()); 3715 3716 // move into view 3717 view.reset(); 3718 downTime = SystemClock.uptimeMillis(); 3719 eventTime = SystemClock.uptimeMillis(); 3720 x = xy[0] + viewWidth - 1; 3721 y = xy[1] + viewHeight - 1; 3722 event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, x, y, 0); 3723 getInstrumentation().sendPointerSync(event); 3724 assertTrue(view.hasCalledOnTouchEvent()); 3725 assertTrue(view.isPressed()); 3726 3727 // MotionEvent.ACTION_UP 3728 OnClickListenerImpl listener = new OnClickListenerImpl(); 3729 view.setOnClickListener(listener); 3730 view.reset(); 3731 downTime = SystemClock.uptimeMillis(); 3732 eventTime = SystemClock.uptimeMillis(); 3733 event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x, y, 0); 3734 getInstrumentation().sendPointerSync(event); 3735 assertTrue(view.hasCalledOnTouchEvent()); 3736 assertTrue(listener.hasOnClick()); 3737 3738 view.reset(); 3739 x = xy[0] + viewWidth / 2.0f; 3740 y = xy[1] + viewHeight / 2.0f; 3741 downTime = SystemClock.uptimeMillis(); 3742 eventTime = SystemClock.uptimeMillis(); 3743 event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, x, y, 0); 3744 getInstrumentation().sendPointerSync(event); 3745 assertTrue(view.hasCalledOnTouchEvent()); 3746 3747 // MotionEvent.ACTION_CANCEL 3748 view.reset(); 3749 listener.reset(); 3750 downTime = SystemClock.uptimeMillis(); 3751 eventTime = SystemClock.uptimeMillis(); 3752 event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_CANCEL, x, y, 0); 3753 getInstrumentation().sendPointerSync(event); 3754 assertTrue(view.hasCalledOnTouchEvent()); 3755 assertFalse(view.isPressed()); 3756 assertFalse(listener.hasOnClick()); 3757 } 3758 3759 @TestTargetNew( 3760 level = TestLevel.COMPLETE, 3761 method = "bringToFront", 3762 args = {} 3763 ) testBringToFront()3764 public void testBringToFront() { 3765 MockView view = new MockView(mActivity); 3766 view.setParent(mMockParent); 3767 3768 assertFalse(mMockParent.hasBroughtChildToFront()); 3769 view.bringToFront(); 3770 assertTrue(mMockParent.hasBroughtChildToFront()); 3771 } 3772 3773 @TestTargetNew( 3774 level = TestLevel.COMPLETE, 3775 method = "getApplicationWindowToken", 3776 args = {} 3777 ) testGetApplicationWindowToken()3778 public void testGetApplicationWindowToken() { 3779 View view = new View(mActivity); 3780 // mAttachInfo is null 3781 assertNull(view.getApplicationWindowToken()); 3782 3783 // mAttachInfo is not null 3784 view = mActivity.findViewById(R.id.fit_windows); 3785 assertNotNull(view.getApplicationWindowToken()); 3786 } 3787 3788 @TestTargetNew( 3789 level = TestLevel.COMPLETE, 3790 method = "getBottomPaddingOffset", 3791 args = {} 3792 ) testGetBottomPaddingOffset()3793 public void testGetBottomPaddingOffset() { 3794 MockView view = new MockView(mActivity); 3795 assertEquals(0, view.getBottomPaddingOffset()); 3796 } 3797 3798 @TestTargetNew( 3799 level = TestLevel.COMPLETE, 3800 method = "getLeftPaddingOffset", 3801 args = {} 3802 ) testGetLeftPaddingOffset()3803 public void testGetLeftPaddingOffset() { 3804 MockView view = new MockView(mActivity); 3805 assertEquals(0, view.getLeftPaddingOffset()); 3806 } 3807 3808 @TestTargetNew( 3809 level = TestLevel.COMPLETE, 3810 method = "getRightPaddingOffset", 3811 args = {} 3812 ) testGetRightPaddingOffset()3813 public void testGetRightPaddingOffset() { 3814 MockView view = new MockView(mActivity); 3815 assertEquals(0, view.getRightPaddingOffset()); 3816 } 3817 3818 @TestTargetNew( 3819 level = TestLevel.COMPLETE, 3820 method = "getTopPaddingOffset", 3821 args = {} 3822 ) testGetTopPaddingOffset()3823 public void testGetTopPaddingOffset() { 3824 MockView view = new MockView(mActivity); 3825 assertEquals(0, view.getTopPaddingOffset()); 3826 } 3827 3828 @TestTargetNew( 3829 level = TestLevel.COMPLETE, 3830 method = "isPaddingOffsetRequired", 3831 args = {} 3832 ) testIsPaddingOffsetRequired()3833 public void testIsPaddingOffsetRequired() { 3834 MockView view = new MockView(mActivity); 3835 assertFalse(view.isPaddingOffsetRequired()); 3836 } 3837 3838 @TestTargetNew( 3839 level = TestLevel.COMPLETE, 3840 method = "getWindowVisibleDisplayFrame", 3841 args = {android.graphics.Rect.class} 3842 ) testGetWindowVisibleDisplayFrame()3843 public void testGetWindowVisibleDisplayFrame() { 3844 Rect outRect = new Rect(); 3845 View view = new View(mActivity); 3846 // mAttachInfo is null 3847 Display d = WindowManagerImpl.getDefault().getDefaultDisplay(); 3848 view.getWindowVisibleDisplayFrame(outRect); 3849 assertEquals(0, outRect.left); 3850 assertEquals(0, outRect.top); 3851 assertEquals(d.getWidth(), outRect.right); 3852 assertEquals(d.getHeight(), outRect.bottom); 3853 3854 // mAttachInfo is not null 3855 outRect = new Rect(); 3856 view = mActivity.findViewById(R.id.fit_windows); 3857 // it's implementation detail 3858 view.getWindowVisibleDisplayFrame(outRect); 3859 } 3860 3861 @TestTargetNew( 3862 level = TestLevel.PARTIAL, 3863 method = "setScrollContainer", 3864 args = {boolean.class} 3865 ) testSetScrollContainer()3866 public void testSetScrollContainer() throws Throwable { 3867 final MockView mockView = (MockView) mActivity.findViewById(R.id.mock_view); 3868 final MockView scrollView = (MockView) mActivity.findViewById(R.id.scroll_view); 3869 Bitmap bitmap = Bitmap.createBitmap(200, 300, Bitmap.Config.RGB_565); 3870 final BitmapDrawable d = new BitmapDrawable(bitmap); 3871 final InputMethodManager imm = InputMethodManager.getInstance(getActivity()); 3872 final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(300, 500); 3873 runTestOnUiThread(new Runnable() { 3874 public void run() { 3875 mockView.setBackgroundDrawable(d); 3876 mockView.setHorizontalFadingEdgeEnabled(true); 3877 mockView.setVerticalFadingEdgeEnabled(true); 3878 mockView.setLayoutParams(layoutParams); 3879 scrollView.setLayoutParams(layoutParams); 3880 3881 mockView.setFocusable(true); 3882 mockView.requestFocus(); 3883 mockView.setScrollContainer(true); 3884 scrollView.setScrollContainer(false); 3885 imm.showSoftInput(mockView, 0); 3886 } 3887 }); 3888 getInstrumentation().waitForIdleSync(); 3889 3890 // FIXME: why the size of view doesn't change? 3891 3892 runTestOnUiThread(new Runnable() { 3893 public void run() { 3894 imm.hideSoftInputFromInputMethod(mockView.getWindowToken(), 0); 3895 } 3896 }); 3897 getInstrumentation().waitForIdleSync(); 3898 } 3899 3900 @TestTargets({ 3901 @TestTargetNew( 3902 level = TestLevel.COMPLETE, 3903 method = "isFocusableInTouchMode", 3904 args = {} 3905 ), 3906 @TestTargetNew( 3907 level = TestLevel.COMPLETE, 3908 method = "setFocusableInTouchMode", 3909 args = {boolean.class} 3910 ), 3911 @TestTargetNew( 3912 level = TestLevel.COMPLETE, 3913 method = "isInTouchMode", 3914 args = {} 3915 ) 3916 }) testTouchMode()3917 public void testTouchMode() throws Throwable { 3918 final MockView mockView = (MockView) mActivity.findViewById(R.id.mock_view); 3919 final View fitWindowsView = mActivity.findViewById(R.id.fit_windows); 3920 runTestOnUiThread(new Runnable() { 3921 public void run() { 3922 mockView.setFocusableInTouchMode(true); 3923 fitWindowsView.setFocusable(true); 3924 fitWindowsView.requestFocus(); 3925 } 3926 }); 3927 assertTrue(mockView.isFocusableInTouchMode()); 3928 assertFalse(fitWindowsView.isFocusableInTouchMode()); 3929 assertTrue(mockView.isFocusable()); 3930 assertTrue(fitWindowsView.isFocusable()); 3931 assertFalse(mockView.isFocused()); 3932 assertTrue(fitWindowsView.isFocused()); 3933 assertFalse(mockView.isInTouchMode()); 3934 assertFalse(fitWindowsView.isInTouchMode()); 3935 3936 TouchUtils.tapView(this, mockView); 3937 assertFalse(fitWindowsView.isFocused()); 3938 assertFalse(mockView.isFocused()); 3939 runTestOnUiThread(new Runnable() { 3940 public void run() { 3941 mockView.requestFocus(); 3942 } 3943 }); 3944 assertTrue(mockView.isFocused()); 3945 runTestOnUiThread(new Runnable() { 3946 public void run() { 3947 fitWindowsView.requestFocus(); 3948 } 3949 }); 3950 assertFalse(fitWindowsView.isFocused()); 3951 assertTrue(mockView.isInTouchMode()); 3952 assertTrue(fitWindowsView.isInTouchMode()); 3953 3954 KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0); 3955 getInstrumentation().sendKeySync(keyEvent); 3956 assertTrue(mockView.isFocused()); 3957 assertFalse(fitWindowsView.isFocused()); 3958 runTestOnUiThread(new Runnable() { 3959 public void run() { 3960 fitWindowsView.requestFocus(); 3961 } 3962 }); 3963 assertFalse(mockView.isFocused()); 3964 assertTrue(fitWindowsView.isFocused()); 3965 assertFalse(mockView.isInTouchMode()); 3966 assertFalse(fitWindowsView.isInTouchMode()); 3967 } 3968 3969 @TestTargets({ 3970 @TestTargetNew( 3971 level = TestLevel.COMPLETE, 3972 method = "setHorizontalScrollBarEnabled", 3973 args = {boolean.class} 3974 ), 3975 @TestTargetNew( 3976 level = TestLevel.COMPLETE, 3977 method = "setVerticalScrollBarEnabled", 3978 args = {boolean.class} 3979 ), 3980 @TestTargetNew( 3981 level = TestLevel.COMPLETE, 3982 method = "isHorizontalScrollBarEnabled", 3983 args = {} 3984 ), 3985 @TestTargetNew( 3986 level = TestLevel.COMPLETE, 3987 method = "isVerticalScrollBarEnabled", 3988 args = {} 3989 ), 3990 @TestTargetNew( 3991 level = TestLevel.COMPLETE, 3992 method = "getVerticalScrollbarWidth", 3993 args = {} 3994 ), 3995 @TestTargetNew( 3996 level = TestLevel.COMPLETE, 3997 method = "getHorizontalScrollbarHeight", 3998 args = {} 3999 ), 4000 @TestTargetNew( 4001 level = TestLevel.PARTIAL, 4002 method = "setScrollBarStyle", 4003 args = {int.class} 4004 ), 4005 @TestTargetNew( 4006 level = TestLevel.COMPLETE, 4007 method = "getScrollBarStyle", 4008 args = {} 4009 ) 4010 }) 4011 @ToBeFixed(bug = "", explanation = "when scrollbar is with INSET style, the bottom padding" + 4012 "should be increased.") 4013 @UiThreadTest testScrollbarStyle()4014 public void testScrollbarStyle() { 4015 MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 4016 Bitmap bitmap = Bitmap.createBitmap(200, 300, Bitmap.Config.RGB_565); 4017 BitmapDrawable d = new BitmapDrawable(bitmap); 4018 view.setBackgroundDrawable(d); 4019 view.setHorizontalFadingEdgeEnabled(true); 4020 view.setVerticalFadingEdgeEnabled(true); 4021 4022 view.setHorizontalScrollBarEnabled(true); 4023 view.setVerticalScrollBarEnabled(true); 4024 view.initializeScrollbars(mActivity.obtainStyledAttributes(android.R.styleable.View)); 4025 assertTrue(view.isHorizontalScrollBarEnabled()); 4026 assertTrue(view.isVerticalScrollBarEnabled()); 4027 int verticalScrollBarWidth = view.getVerticalScrollbarWidth(); 4028 int horizontalScrollBarHeight = view.getHorizontalScrollbarHeight(); 4029 assertTrue(verticalScrollBarWidth > 0); 4030 assertTrue(horizontalScrollBarHeight > 0); 4031 assertEquals(0, view.getPaddingRight()); 4032 assertEquals(0, view.getPaddingBottom()); 4033 4034 view.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET); 4035 assertEquals(View.SCROLLBARS_INSIDE_INSET, view.getScrollBarStyle()); 4036 assertEquals(verticalScrollBarWidth, view.getPaddingRight()); 4037 // issue, mockView.getPaddingBottom() is expected to equal horizontalScrollBarHeight. 4038 assertEquals(0, view.getPaddingBottom()); 4039 4040 view.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY); 4041 assertEquals(View.SCROLLBARS_OUTSIDE_OVERLAY, view.getScrollBarStyle()); 4042 assertEquals(0, view.getPaddingRight()); 4043 assertEquals(0, view.getPaddingBottom()); 4044 4045 view.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_INSET); 4046 assertEquals(View.SCROLLBARS_OUTSIDE_INSET, view.getScrollBarStyle()); 4047 assertEquals(verticalScrollBarWidth, view.getPaddingRight()); 4048 // issue, mockView.getPaddingBottom() is expected to equal horizontalScrollBarHeight. 4049 assertEquals(0, view.getPaddingBottom()); 4050 4051 // TODO: how to get the position of the Scrollbar to assert it is inside or outside. 4052 } 4053 4054 @TestTargets({ 4055 @TestTargetNew( 4056 level = TestLevel.COMPLETE, 4057 method = "isHorizontalFadingEdgeEnabled", 4058 args = {} 4059 ), 4060 @TestTargetNew( 4061 level = TestLevel.COMPLETE, 4062 method = "isVerticalFadingEdgeEnabled", 4063 args = {} 4064 ), 4065 @TestTargetNew( 4066 level = TestLevel.COMPLETE, 4067 method = "getHorizontalFadingEdgeLength", 4068 args = {} 4069 ), 4070 @TestTargetNew( 4071 level = TestLevel.COMPLETE, 4072 method = "getVerticalFadingEdgeLength", 4073 args = {} 4074 ), 4075 @TestTargetNew( 4076 level = TestLevel.COMPLETE, 4077 method = "setHorizontalFadingEdgeEnabled", 4078 args = {boolean.class} 4079 ), 4080 @TestTargetNew( 4081 level = TestLevel.COMPLETE, 4082 method = "setVerticalFadingEdgeEnabled", 4083 args = {boolean.class} 4084 ), 4085 @TestTargetNew( 4086 level = TestLevel.COMPLETE, 4087 method = "setFadingEdgeLength", 4088 args = {int.class} 4089 ) 4090 }) 4091 @UiThreadTest testScrollFading()4092 public void testScrollFading() { 4093 MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 4094 Bitmap bitmap = Bitmap.createBitmap(200, 300, Bitmap.Config.RGB_565); 4095 BitmapDrawable d = new BitmapDrawable(bitmap); 4096 view.setBackgroundDrawable(d); 4097 4098 assertFalse(view.isHorizontalFadingEdgeEnabled()); 4099 assertFalse(view.isVerticalFadingEdgeEnabled()); 4100 assertEquals(0, view.getHorizontalFadingEdgeLength()); 4101 assertEquals(0, view.getVerticalFadingEdgeLength()); 4102 4103 view.setHorizontalFadingEdgeEnabled(true); 4104 view.setVerticalFadingEdgeEnabled(true); 4105 assertTrue(view.isHorizontalFadingEdgeEnabled()); 4106 assertTrue(view.isVerticalFadingEdgeEnabled()); 4107 assertTrue(view.getHorizontalFadingEdgeLength() > 0); 4108 assertTrue(view.getVerticalFadingEdgeLength() > 0); 4109 4110 final int fadingLength = 20; 4111 view.setFadingEdgeLength(fadingLength); 4112 assertEquals(fadingLength, view.getHorizontalFadingEdgeLength()); 4113 assertEquals(fadingLength, view.getVerticalFadingEdgeLength()); 4114 } 4115 4116 @TestTargets({ 4117 @TestTargetNew( 4118 level = TestLevel.COMPLETE, 4119 method = "scrollBy", 4120 args = {int.class, int.class} 4121 ), 4122 @TestTargetNew( 4123 level = TestLevel.COMPLETE, 4124 method = "scrollTo", 4125 args = {int.class, int.class} 4126 ), 4127 @TestTargetNew( 4128 level = TestLevel.COMPLETE, 4129 method = "getScrollX", 4130 args = {} 4131 ), 4132 @TestTargetNew( 4133 level = TestLevel.COMPLETE, 4134 method = "getScrollY", 4135 args = {} 4136 ), 4137 @TestTargetNew( 4138 level = TestLevel.COMPLETE, 4139 method = "onScrollChanged", 4140 args = {int.class, int.class, int.class, int.class} 4141 ) 4142 }) 4143 @UiThreadTest testScrolling()4144 public void testScrolling() { 4145 MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 4146 4147 view.reset(); 4148 assertEquals(0, view.getScrollX()); 4149 assertEquals(0, view.getScrollY()); 4150 assertFalse(view.hasCalledOnScrollChanged()); 4151 assertFalse(view.hasCalledInvalidate()); 4152 4153 view.scrollTo(0, 0); 4154 assertEquals(0, view.getScrollX()); 4155 assertEquals(0, view.getScrollY()); 4156 assertFalse(view.hasCalledOnScrollChanged()); 4157 assertFalse(view.hasCalledInvalidate()); 4158 4159 view.scrollBy(0, 0); 4160 assertEquals(0, view.getScrollX()); 4161 assertEquals(0, view.getScrollY()); 4162 assertFalse(view.hasCalledOnScrollChanged()); 4163 assertFalse(view.hasCalledInvalidate()); 4164 4165 view.scrollTo(10, 100); 4166 assertEquals(10, view.getScrollX()); 4167 assertEquals(100, view.getScrollY()); 4168 assertTrue(view.hasCalledOnScrollChanged()); 4169 assertTrue(view.hasCalledInvalidate()); 4170 4171 view.reset(); 4172 assertFalse(view.hasCalledOnScrollChanged()); 4173 view.scrollBy(-10, -100); 4174 assertEquals(0, view.getScrollX()); 4175 assertEquals(0, view.getScrollY()); 4176 assertTrue(view.hasCalledOnScrollChanged()); 4177 assertTrue(view.hasCalledInvalidate()); 4178 4179 view.reset(); 4180 assertFalse(view.hasCalledOnScrollChanged()); 4181 view.scrollTo(-1, -2); 4182 assertEquals(-1, view.getScrollX()); 4183 assertEquals(-2, view.getScrollY()); 4184 assertTrue(view.hasCalledOnScrollChanged()); 4185 assertTrue(view.hasCalledInvalidate()); 4186 } 4187 4188 @TestTargets({ 4189 @TestTargetNew( 4190 level = TestLevel.COMPLETE, 4191 method = "initializeScrollbars", 4192 args = {android.content.res.TypedArray.class} 4193 ), 4194 @TestTargetNew( 4195 level = TestLevel.COMPLETE, 4196 method = "initializeFadingEdge", 4197 args = {android.content.res.TypedArray.class} 4198 ) 4199 }) testInitializeScrollbarsAndFadingEdge()4200 public void testInitializeScrollbarsAndFadingEdge() { 4201 MockView view = (MockView) mActivity.findViewById(R.id.scroll_view); 4202 final int fadingEdgeLength = 20; 4203 4204 assertTrue(view.isHorizontalScrollBarEnabled()); 4205 assertTrue(view.isVerticalScrollBarEnabled()); 4206 assertTrue(view.isHorizontalFadingEdgeEnabled()); 4207 assertTrue(view.isVerticalFadingEdgeEnabled()); 4208 assertEquals(fadingEdgeLength, view.getHorizontalFadingEdgeLength()); 4209 assertEquals(fadingEdgeLength, view.getVerticalFadingEdgeLength()); 4210 } 4211 4212 @TestTargets({ 4213 @TestTargetNew( 4214 level = TestLevel.COMPLETE, 4215 method = "onStartTemporaryDetach", 4216 args = {} 4217 ), 4218 @TestTargetNew( 4219 level = TestLevel.COMPLETE, 4220 method = "onFinishTemporaryDetach", 4221 args = {} 4222 ) 4223 }) 4224 @ToBeFixed(bug = "", explanation = "onStartTemporaryDetach and onFinishTemporaryDetach" + 4225 " is not called when ListView is going to temporarily detach a child that currently" + 4226 " has focus, with detachViewFromParent.") testOnStartAndFinishTemporaryDetach()4227 public void testOnStartAndFinishTemporaryDetach() throws Throwable { 4228 final MockListView listView = new MockListView(mActivity); 4229 List<String> items = Lists.newArrayList("1", "2", "3"); 4230 final Adapter<String> adapter = new Adapter<String>(mActivity, 0, items); 4231 4232 runTestOnUiThread(new Runnable() { 4233 public void run() { 4234 mActivity.setContentView(listView); 4235 listView.setAdapter(adapter); 4236 } 4237 }); 4238 getInstrumentation().waitForIdleSync(); 4239 final MockView focusChild = (MockView) listView.getChildAt(0); 4240 4241 runTestOnUiThread(new Runnable() { 4242 public void run() { 4243 focusChild.requestFocus(); 4244 } 4245 }); 4246 getInstrumentation().waitForIdleSync(); 4247 assertTrue(listView.getChildAt(0).isFocused()); 4248 4249 runTestOnUiThread(new Runnable() { 4250 public void run() { 4251 listView.detachViewFromParent(focusChild); 4252 } 4253 }); 4254 getInstrumentation().waitForIdleSync(); 4255 assertFalse(listView.hasCalledOnStartTemporaryDetach()); 4256 assertFalse(listView.hasCalledOnFinishTemporaryDetach()); 4257 } 4258 4259 private static class MockListView extends ListView { 4260 private boolean mCalledOnStartTemporaryDetach = false; 4261 private boolean mCalledOnFinishTemporaryDetach = false; 4262 MockListView(Context context)4263 public MockListView(Context context) { 4264 super(context); 4265 } 4266 4267 @Override detachViewFromParent(View child)4268 protected void detachViewFromParent(View child) { 4269 super.detachViewFromParent(child); 4270 } 4271 4272 @Override onFinishTemporaryDetach()4273 public void onFinishTemporaryDetach() { 4274 super.onFinishTemporaryDetach(); 4275 mCalledOnFinishTemporaryDetach = true; 4276 } 4277 hasCalledOnFinishTemporaryDetach()4278 public boolean hasCalledOnFinishTemporaryDetach() { 4279 return mCalledOnFinishTemporaryDetach; 4280 } 4281 4282 @Override onStartTemporaryDetach()4283 public void onStartTemporaryDetach() { 4284 super.onStartTemporaryDetach(); 4285 mCalledOnStartTemporaryDetach = true; 4286 } 4287 hasCalledOnStartTemporaryDetach()4288 public boolean hasCalledOnStartTemporaryDetach() { 4289 return mCalledOnStartTemporaryDetach; 4290 } 4291 reset()4292 public void reset() { 4293 mCalledOnStartTemporaryDetach = false; 4294 mCalledOnFinishTemporaryDetach = false; 4295 } 4296 } 4297 4298 private static class Adapter<T> extends ArrayAdapter<T> { 4299 ArrayList<MockView> views = new ArrayList<MockView>(); 4300 Adapter(Context context, int textViewResourceId, List<T> objects)4301 public Adapter(Context context, int textViewResourceId, List<T> objects) { 4302 super(context, textViewResourceId, objects); 4303 for (int i = 0; i < objects.size(); i++) { 4304 views.add(new MockView(context)); 4305 views.get(i).setFocusable(true); 4306 } 4307 } 4308 4309 @Override getCount()4310 public int getCount() { 4311 return views.size(); 4312 } 4313 4314 @Override getItemId(int position)4315 public long getItemId(int position) { 4316 return position; 4317 } 4318 4319 @Override getView(int position, View convertView, ViewGroup parent)4320 public View getView(int position, View convertView, ViewGroup parent) { 4321 return views.get(position); 4322 } 4323 } 4324 4325 @TestTargets({ 4326 @TestTargetNew( 4327 level = TestLevel.COMPLETE, 4328 method = "dispatchKeyEventPreIme", 4329 args = {KeyEvent.class} 4330 ), 4331 @TestTargetNew( 4332 level = TestLevel.COMPLETE, 4333 method = "onKeyPreIme", 4334 args = {int.class, KeyEvent.class} 4335 ) 4336 }) testKeyPreIme()4337 public void testKeyPreIme() throws Throwable { 4338 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 4339 4340 runTestOnUiThread(new Runnable() { 4341 public void run() { 4342 view.setFocusable(true); 4343 view.requestFocus(); 4344 } 4345 }); 4346 getInstrumentation().waitForIdleSync(); 4347 4348 getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK)); 4349 assertTrue(view.hasCalledDispatchKeyEventPreIme()); 4350 assertTrue(view.hasCalledOnKeyPreIme()); 4351 } 4352 4353 @TestTargets({ 4354 @TestTargetNew( 4355 level = TestLevel.COMPLETE, 4356 method = "isHapticFeedbackEnabled", 4357 args = {} 4358 ), 4359 @TestTargetNew( 4360 level = TestLevel.COMPLETE, 4361 method = "setHapticFeedbackEnabled", 4362 args = {boolean.class} 4363 ), 4364 @TestTargetNew( 4365 level = TestLevel.COMPLETE, 4366 method = "performHapticFeedback", 4367 args = {int.class} 4368 ), 4369 @TestTargetNew( 4370 level = TestLevel.COMPLETE, 4371 method = "performHapticFeedback", 4372 args = {int.class, int.class} 4373 ) 4374 }) testHapticFeedback()4375 public void testHapticFeedback() { 4376 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 4377 final int LONG_PRESS = HapticFeedbackConstants.LONG_PRESS; 4378 final int FLAG_IGNORE_VIEW_SETTING = HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING; 4379 final int FLAG_IGNORE_GLOBAL_SETTING = HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING; 4380 final int ALWAYS = FLAG_IGNORE_VIEW_SETTING | FLAG_IGNORE_GLOBAL_SETTING; 4381 4382 view.setHapticFeedbackEnabled(false); 4383 assertFalse(view.isHapticFeedbackEnabled()); 4384 assertFalse(view.performHapticFeedback(LONG_PRESS)); 4385 assertFalse(view.performHapticFeedback(LONG_PRESS, FLAG_IGNORE_GLOBAL_SETTING)); 4386 assertTrue(view.performHapticFeedback(LONG_PRESS, ALWAYS)); 4387 4388 view.setHapticFeedbackEnabled(true); 4389 assertTrue(view.isHapticFeedbackEnabled()); 4390 assertTrue(view.performHapticFeedback(LONG_PRESS, FLAG_IGNORE_GLOBAL_SETTING)); 4391 } 4392 4393 @TestTargets({ 4394 @TestTargetNew( 4395 level = TestLevel.COMPLETE, 4396 method = "onCreateInputConnection", 4397 args = {EditorInfo.class} 4398 ), 4399 @TestTargetNew( 4400 level = TestLevel.COMPLETE, 4401 method = "checkInputConnectionProxy", 4402 args = {View.class} 4403 ), 4404 @TestTargetNew( 4405 level = TestLevel.COMPLETE, 4406 method = "onCheckIsTextEditor", 4407 args = {} 4408 ) 4409 }) 4410 @UiThreadTest testInputConnection()4411 public void testInputConnection() { 4412 final InputMethodManager imm = InputMethodManager.getInstance(getActivity()); 4413 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 4414 final ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root); 4415 final MockEditText editText = new MockEditText(mActivity); 4416 4417 viewGroup.addView(editText); 4418 editText.requestFocus(); 4419 imm.showSoftInput(editText, 0); 4420 assertTrue(editText.hasCalledOnCreateInputConnection()); 4421 assertTrue(editText.hasCalledOnCheckIsTextEditor()); 4422 assertTrue(imm.isActive(editText)); 4423 4424 assertFalse(editText.hasCalledCheckInputConnectionProxy()); 4425 imm.isActive(view); 4426 assertTrue(editText.hasCalledCheckInputConnectionProxy()); 4427 } 4428 4429 private static class MockEditText extends EditText { 4430 private boolean mCalledCheckInputConnectionProxy = false; 4431 private boolean mCalledOnCreateInputConnection = false; 4432 private boolean mCalledOnCheckIsTextEditor = false; 4433 MockEditText(Context context)4434 public MockEditText(Context context) { 4435 super(context); 4436 } 4437 4438 @Override checkInputConnectionProxy(View view)4439 public boolean checkInputConnectionProxy(View view) { 4440 mCalledCheckInputConnectionProxy = true; 4441 return super.checkInputConnectionProxy(view); 4442 } 4443 hasCalledCheckInputConnectionProxy()4444 public boolean hasCalledCheckInputConnectionProxy() { 4445 return mCalledCheckInputConnectionProxy; 4446 } 4447 4448 @Override onCreateInputConnection(EditorInfo outAttrs)4449 public InputConnection onCreateInputConnection(EditorInfo outAttrs) { 4450 mCalledOnCreateInputConnection = true; 4451 return super.onCreateInputConnection(outAttrs); 4452 } 4453 hasCalledOnCreateInputConnection()4454 public boolean hasCalledOnCreateInputConnection() { 4455 return mCalledOnCreateInputConnection; 4456 } 4457 4458 @Override onCheckIsTextEditor()4459 public boolean onCheckIsTextEditor() { 4460 mCalledOnCheckIsTextEditor = true; 4461 return super.onCheckIsTextEditor(); 4462 } 4463 hasCalledOnCheckIsTextEditor()4464 public boolean hasCalledOnCheckIsTextEditor() { 4465 return mCalledOnCheckIsTextEditor; 4466 } 4467 reset()4468 public void reset() { 4469 mCalledCheckInputConnectionProxy = false; 4470 mCalledOnCreateInputConnection = false; 4471 mCalledOnCheckIsTextEditor = false; 4472 } 4473 } 4474 4475 private final static class MockViewParent extends View implements ViewParent { 4476 private boolean mHasClearChildFocus = false; 4477 private boolean mHasRequestLayout = false; 4478 private boolean mHasCreateContextMenu = false; 4479 private boolean mHasShowContextMenuForChild = false; 4480 private boolean mHasGetChildVisibleRect = false; 4481 private boolean mHasInvalidateChild = false; 4482 private boolean mHasOnCreateDrawableState = false; 4483 private boolean mHasChildDrawableStateChanged = false; 4484 private boolean mHasBroughtChildToFront = false; 4485 private Rect mTempRect; 4486 4487 private final static int[] DEFAULT_PARENT_STATE_SET = new int[] { 789 }; 4488 requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate)4489 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, 4490 boolean immediate) { 4491 return false; 4492 } 4493 MockViewParent(Context context)4494 public MockViewParent(Context context) { 4495 super(context); 4496 } 4497 bringChildToFront(View child)4498 public void bringChildToFront(View child) { 4499 mHasBroughtChildToFront = true; 4500 } 4501 hasBroughtChildToFront()4502 public boolean hasBroughtChildToFront() { 4503 return mHasBroughtChildToFront; 4504 } 4505 childDrawableStateChanged(View child)4506 public void childDrawableStateChanged(View child) { 4507 mHasChildDrawableStateChanged = true; 4508 } 4509 hasChildDrawableStateChanged()4510 public boolean hasChildDrawableStateChanged() { 4511 return mHasChildDrawableStateChanged; 4512 } 4513 4514 @Override dispatchSetPressed(boolean pressed)4515 protected void dispatchSetPressed(boolean pressed) { 4516 super.dispatchSetPressed(pressed); 4517 } 4518 4519 @Override dispatchSetSelected(boolean selected)4520 protected void dispatchSetSelected(boolean selected) { 4521 super.dispatchSetSelected(selected); 4522 } 4523 clearChildFocus(View child)4524 public void clearChildFocus(View child) { 4525 mHasClearChildFocus = true; 4526 } 4527 hasClearChildFocus()4528 public boolean hasClearChildFocus() { 4529 return mHasClearChildFocus; 4530 } 4531 createContextMenu(ContextMenu menu)4532 public void createContextMenu(ContextMenu menu) { 4533 mHasCreateContextMenu = true; 4534 } 4535 hasCreateContextMenu()4536 public boolean hasCreateContextMenu() { 4537 return mHasCreateContextMenu; 4538 } 4539 focusSearch(View v, int direction)4540 public View focusSearch(View v, int direction) { 4541 return v; 4542 } 4543 focusableViewAvailable(View v)4544 public void focusableViewAvailable(View v) { 4545 4546 } 4547 getChildVisibleRect(View child, Rect r, Point offset)4548 public boolean getChildVisibleRect(View child, Rect r, Point offset) { 4549 mHasGetChildVisibleRect = true; 4550 return false; 4551 } 4552 hasGetChildVisibleRect()4553 public boolean hasGetChildVisibleRect() { 4554 return mHasGetChildVisibleRect; 4555 } 4556 invalidateChild(View child, Rect r)4557 public void invalidateChild(View child, Rect r) { 4558 mTempRect = new Rect(r); 4559 mHasInvalidateChild = true; 4560 } 4561 getTempRect()4562 public Rect getTempRect() { 4563 return mTempRect; 4564 } 4565 hasInvalidateChild()4566 public boolean hasInvalidateChild() { 4567 return mHasInvalidateChild; 4568 } 4569 invalidateChildInParent(int[] location, Rect r)4570 public ViewParent invalidateChildInParent(int[] location, Rect r) { 4571 return null; 4572 } 4573 isLayoutRequested()4574 public boolean isLayoutRequested() { 4575 return false; 4576 } 4577 recomputeViewAttributes(View child)4578 public void recomputeViewAttributes(View child) { 4579 4580 } 4581 requestChildFocus(View child, View focused)4582 public void requestChildFocus(View child, View focused) { 4583 4584 } 4585 requestDisallowInterceptTouchEvent(boolean disallowIntercept)4586 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { 4587 4588 } 4589 requestLayout()4590 public void requestLayout() { 4591 mHasRequestLayout = true; 4592 } 4593 hasRequestLayout()4594 public boolean hasRequestLayout() { 4595 return mHasRequestLayout; 4596 } 4597 requestTransparentRegion(View child)4598 public void requestTransparentRegion(View child) { 4599 4600 } 4601 showContextMenuForChild(View originalView)4602 public boolean showContextMenuForChild(View originalView) { 4603 mHasShowContextMenuForChild = true; 4604 return false; 4605 } 4606 hasShowContextMenuForChild()4607 public boolean hasShowContextMenuForChild() { 4608 return mHasShowContextMenuForChild; 4609 } 4610 4611 @Override onCreateDrawableState(int extraSpace)4612 protected int[] onCreateDrawableState(int extraSpace) { 4613 mHasOnCreateDrawableState = true; 4614 return DEFAULT_PARENT_STATE_SET; 4615 } 4616 getDefaultParentStateSet()4617 public static int[] getDefaultParentStateSet() { 4618 return DEFAULT_PARENT_STATE_SET; 4619 } 4620 hasOnCreateDrawableState()4621 public boolean hasOnCreateDrawableState() { 4622 return mHasOnCreateDrawableState; 4623 } 4624 reset()4625 public void reset() { 4626 mHasClearChildFocus = false; 4627 mHasRequestLayout = false; 4628 mHasCreateContextMenu = false; 4629 mHasShowContextMenuForChild = false; 4630 mHasGetChildVisibleRect = false; 4631 mHasInvalidateChild = false; 4632 mHasOnCreateDrawableState = false; 4633 mHasChildDrawableStateChanged = false; 4634 mHasBroughtChildToFront = false; 4635 } 4636 } 4637 4638 private final class OnCreateContextMenuListenerImpl implements OnCreateContextMenuListener { 4639 private boolean mHasOnCreateContextMenu = false; 4640 onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)4641 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 4642 mHasOnCreateContextMenu = true; 4643 } 4644 hasOnCreateContextMenu()4645 public boolean hasOnCreateContextMenu() { 4646 return mHasOnCreateContextMenu; 4647 } 4648 reset()4649 public void reset() { 4650 mHasOnCreateContextMenu = false; 4651 } 4652 } 4653 4654 private final static class MockViewGroupParent extends ViewGroup implements ViewParent { 4655 private boolean mHasRequestChildRectangleOnScreen = false; 4656 MockViewGroupParent(Context context)4657 public MockViewGroupParent(Context context) { 4658 super(context); 4659 } 4660 4661 @Override onLayout(boolean changed, int l, int t, int r, int b)4662 protected void onLayout(boolean changed, int l, int t, int r, int b) { 4663 4664 } 4665 4666 @Override requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate)4667 public boolean requestChildRectangleOnScreen(View child, 4668 Rect rectangle, boolean immediate) { 4669 mHasRequestChildRectangleOnScreen = true; 4670 return super.requestChildRectangleOnScreen(child, rectangle, immediate); 4671 } 4672 hasRequestChildRectangleOnScreen()4673 public boolean hasRequestChildRectangleOnScreen() { 4674 return mHasRequestChildRectangleOnScreen; 4675 } 4676 4677 @Override detachViewFromParent(View child)4678 protected void detachViewFromParent(View child) { 4679 super.detachViewFromParent(child); 4680 } 4681 reset()4682 public void reset() { 4683 mHasRequestChildRectangleOnScreen = false; 4684 } 4685 } 4686 4687 private static final class OnClickListenerImpl implements OnClickListener { 4688 private boolean mHasOnClick = false; 4689 onClick(View v)4690 public void onClick(View v) { 4691 mHasOnClick = true; 4692 } 4693 hasOnClick()4694 public boolean hasOnClick() { 4695 return mHasOnClick; 4696 } 4697 reset()4698 public void reset() { 4699 mHasOnClick = false; 4700 } 4701 } 4702 4703 private static final class OnLongClickListenerImpl implements OnLongClickListener { 4704 private boolean mHasOnLongClick = false; 4705 hasOnLongClick()4706 public boolean hasOnLongClick() { 4707 return mHasOnLongClick; 4708 } 4709 reset()4710 public void reset() { 4711 mHasOnLongClick = false; 4712 } 4713 onLongClick(View v)4714 public boolean onLongClick(View v) { 4715 mHasOnLongClick = true; 4716 return true; 4717 } 4718 } 4719 4720 private static final class OnFocusChangeListenerImpl implements OnFocusChangeListener { 4721 private boolean mHasOnFocusChange = false; 4722 onFocusChange(View v, boolean hasFocus)4723 public void onFocusChange(View v, boolean hasFocus) { 4724 mHasOnFocusChange = true; 4725 } 4726 hasOnFocusChange()4727 public boolean hasOnFocusChange() { 4728 return mHasOnFocusChange; 4729 } 4730 reset()4731 public void reset() { 4732 mHasOnFocusChange = false; 4733 } 4734 } 4735 4736 private static final class OnKeyListenerImpl implements OnKeyListener { 4737 private boolean mHasOnKey = false; 4738 onKey(View v, int keyCode, KeyEvent event)4739 public boolean onKey(View v, int keyCode, KeyEvent event) { 4740 mHasOnKey = true; 4741 return true; 4742 } 4743 reset()4744 public void reset() { 4745 mHasOnKey = false; 4746 } 4747 hasOnKey()4748 public boolean hasOnKey() { 4749 return mHasOnKey; 4750 } 4751 } 4752 4753 private static final class OnTouchListenerImpl implements OnTouchListener { 4754 private boolean mHasOnTouch = false; 4755 onTouch(View v, MotionEvent event)4756 public boolean onTouch(View v, MotionEvent event) { 4757 mHasOnTouch = true; 4758 return true; 4759 } 4760 reset()4761 public void reset() { 4762 mHasOnTouch = false; 4763 } 4764 hasOnTouch()4765 public boolean hasOnTouch() { 4766 return mHasOnTouch; 4767 } 4768 } 4769 4770 private static final class MockTouchDelegate extends TouchDelegate { MockTouchDelegate(Rect bounds, View delegateView)4771 public MockTouchDelegate(Rect bounds, View delegateView) { 4772 super(bounds, delegateView); 4773 } 4774 4775 private boolean mCalledOnTouchEvent = false; 4776 4777 @Override onTouchEvent(MotionEvent event)4778 public boolean onTouchEvent(MotionEvent event) { 4779 mCalledOnTouchEvent = true; 4780 return super.onTouchEvent(event); 4781 } 4782 hasCalledOnTouchEvent()4783 public boolean hasCalledOnTouchEvent() { 4784 return mCalledOnTouchEvent; 4785 } 4786 reset()4787 public void reset() { 4788 mCalledOnTouchEvent = false; 4789 } 4790 }; 4791 4792 private static final class ViewData { 4793 public int childCount; 4794 public String tag; 4795 public View firstChild; 4796 } 4797 4798 private static final class MockRunnable implements Runnable { 4799 public boolean hasRun = false; 4800 run()4801 public void run() { 4802 hasRun = true; 4803 } 4804 } 4805 } 4806