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 android.app.Activity; 24 import android.content.Context; 25 import android.content.res.Resources; 26 import android.content.res.XmlResourceParser; 27 import android.cts.util.PollingCheck; 28 import android.graphics.Bitmap; 29 import android.graphics.Point; 30 import android.graphics.Rect; 31 import android.graphics.drawable.BitmapDrawable; 32 import android.graphics.drawable.ColorDrawable; 33 import android.graphics.drawable.Drawable; 34 import android.graphics.drawable.StateListDrawable; 35 import android.os.Parcelable; 36 import android.os.SystemClock; 37 import android.os.Vibrator; 38 import android.test.ActivityInstrumentationTestCase2; 39 import android.test.TouchUtils; 40 import android.test.UiThreadTest; 41 import android.text.format.DateUtils; 42 import android.util.AttributeSet; 43 import android.util.Log; 44 import android.util.SparseArray; 45 import android.util.Xml; 46 import android.view.ActionMode; 47 import android.view.ContextMenu; 48 import android.view.ContextMenu.ContextMenuInfo; 49 import android.view.Display; 50 import android.view.HapticFeedbackConstants; 51 import android.view.InputDevice; 52 import android.view.KeyEvent; 53 import android.view.MotionEvent; 54 import android.view.SoundEffectConstants; 55 import android.view.TouchDelegate; 56 import android.view.View; 57 import android.view.MotionEvent.PointerProperties; 58 import android.view.View.BaseSavedState; 59 import android.view.View.OnClickListener; 60 import android.view.View.OnCreateContextMenuListener; 61 import android.view.View.OnFocusChangeListener; 62 import android.view.View.OnKeyListener; 63 import android.view.View.OnLongClickListener; 64 import android.view.View.OnTouchListener; 65 import android.view.ViewConfiguration; 66 import android.view.ViewGroup; 67 import android.view.ViewParent; 68 import android.view.WindowManager; 69 import android.view.accessibility.AccessibilityEvent; 70 import android.view.animation.AlphaAnimation; 71 import android.view.animation.Animation; 72 import android.view.inputmethod.EditorInfo; 73 import android.view.inputmethod.InputConnection; 74 import android.view.inputmethod.InputMethodManager; 75 import android.widget.ArrayAdapter; 76 import android.widget.Button; 77 import android.widget.EditText; 78 import android.widget.LinearLayout; 79 import android.widget.ListView; 80 import android.widget.cts.StubActivity; 81 82 import java.util.ArrayList; 83 import java.util.Arrays; 84 import java.util.List; 85 86 /** 87 * Test {@link View}. 88 */ 89 public class ViewTest extends ActivityInstrumentationTestCase2<ViewTestStubActivity> { 90 public ViewTest() { 91 super(ViewTestStubActivity.class); 92 } 93 94 private Resources mResources; 95 private MockViewParent mMockParent; 96 private ViewTestStubActivity mActivity; 97 98 /** timeout delta when wait in case the system is sluggish */ 99 private static final long TIMEOUT_DELTA = 10000; 100 101 private static final String LOG_TAG = "ViewTest"; 102 103 @Override 104 protected void setUp() throws Exception { 105 super.setUp(); 106 mActivity = getActivity(); 107 new PollingCheck() { 108 @Override 109 protected boolean check() { 110 return mActivity.hasWindowFocus(); 111 } 112 }.run(); 113 mResources = mActivity.getResources(); 114 mMockParent = new MockViewParent(mActivity); 115 assertTrue(mActivity.waitForWindowFocus(5 * DateUtils.SECOND_IN_MILLIS)); 116 } 117 118 public void testConstructor() { 119 new View(mActivity); 120 121 final XmlResourceParser parser = mResources.getLayout(R.layout.view_layout); 122 final AttributeSet attrs = Xml.asAttributeSet(parser); 123 new View(mActivity, attrs); 124 125 new View(mActivity, null); 126 127 try { 128 new View(null, attrs); 129 fail("should throw NullPointerException"); 130 } catch (NullPointerException e) { 131 } 132 133 new View(mActivity, attrs, 0); 134 135 new View(mActivity, null, 1); 136 137 try { 138 new View(null, null, 1); 139 fail("should throw NullPointerException"); 140 } catch (NullPointerException e) { 141 } 142 } 143 144 public void testGetContext() { 145 View view = new View(mActivity); 146 assertSame(mActivity, view.getContext()); 147 } 148 149 public void testGetResources() { 150 View view = new View(mActivity); 151 assertSame(mResources, view.getResources()); 152 } 153 154 public void testGetAnimation() { 155 Animation animation = new AlphaAnimation(0.0f, 1.0f); 156 View view = new View(mActivity); 157 assertNull(view.getAnimation()); 158 159 view.setAnimation(animation); 160 assertSame(animation, view.getAnimation()); 161 162 view.clearAnimation(); 163 assertNull(view.getAnimation()); 164 } 165 166 public void testSetAnimation() { 167 Animation animation = new AlphaAnimation(0.0f, 1.0f); 168 View view = new View(mActivity); 169 assertNull(view.getAnimation()); 170 171 animation.initialize(100, 100, 100, 100); 172 assertTrue(animation.isInitialized()); 173 view.setAnimation(animation); 174 assertSame(animation, view.getAnimation()); 175 assertFalse(animation.isInitialized()); 176 177 view.setAnimation(null); 178 assertNull(view.getAnimation()); 179 } 180 181 public void testClearAnimation() { 182 Animation animation = new AlphaAnimation(0.0f, 1.0f); 183 View view = new View(mActivity); 184 185 assertNull(view.getAnimation()); 186 view.clearAnimation(); 187 assertNull(view.getAnimation()); 188 189 view.setAnimation(animation); 190 assertNotNull(view.getAnimation()); 191 view.clearAnimation(); 192 assertNull(view.getAnimation()); 193 } 194 195 public void testStartAnimation() { 196 Animation animation = new AlphaAnimation(0.0f, 1.0f); 197 View view = new View(mActivity); 198 199 try { 200 view.startAnimation(null); 201 fail("should throw NullPointerException"); 202 } catch (NullPointerException e) { 203 } 204 205 animation.setStartTime(1L); 206 assertEquals(1L, animation.getStartTime()); 207 view.startAnimation(animation); 208 assertEquals(Animation.START_ON_FIRST_FRAME, animation.getStartTime()); 209 } 210 211 public void testOnAnimation() throws Throwable { 212 final Animation animation = new AlphaAnimation(0.0f, 1.0f); 213 long duration = 2000L; 214 animation.setDuration(duration); 215 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 216 217 // check whether it has started 218 runTestOnUiThread(new Runnable() { 219 public void run() { 220 view.startAnimation(animation); 221 } 222 }); 223 getInstrumentation().waitForIdleSync(); 224 225 new PollingCheck() { 226 @Override 227 protected boolean check() { 228 return view.hasCalledOnAnimationStart(); 229 } 230 }.run(); 231 232 // check whether it has ended after duration, and alpha changed during this time. 233 new PollingCheck(duration + TIMEOUT_DELTA) { 234 @Override 235 protected boolean check() { 236 return view.hasCalledOnSetAlpha() && view.hasCalledOnAnimationEnd(); 237 } 238 }.run(); 239 } 240 241 public void testGetParent() { 242 MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 243 ViewGroup parent = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root); 244 assertSame(parent, view.getParent()); 245 } 246 247 public void testFindViewById() { 248 View parent = mActivity.findViewById(R.id.viewlayout_root); 249 assertSame(parent, parent.findViewById(R.id.viewlayout_root)); 250 251 View view = parent.findViewById(R.id.mock_view); 252 assertTrue(view instanceof MockView); 253 } 254 255 public void testAccessTouchDelegate() throws Throwable { 256 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 257 Rect rect = new Rect(); 258 final Button button = new Button(mActivity); 259 final int WRAP_CONTENT = ViewGroup.LayoutParams.WRAP_CONTENT; 260 final int btnHeight = view.getHeight()/3; 261 runTestOnUiThread(new Runnable() { 262 public void run() { 263 mActivity.addContentView(button, 264 new LinearLayout.LayoutParams(WRAP_CONTENT, btnHeight)); 265 } 266 }); 267 getInstrumentation().waitForIdleSync(); 268 button.getHitRect(rect); 269 MockTouchDelegate delegate = new MockTouchDelegate(rect, button); 270 271 assertNull(view.getTouchDelegate()); 272 273 view.setTouchDelegate(delegate); 274 assertSame(delegate, view.getTouchDelegate()); 275 assertFalse(delegate.hasCalledOnTouchEvent()); 276 TouchUtils.clickView(this, view); 277 assertTrue(view.hasCalledOnTouchEvent()); 278 assertTrue(delegate.hasCalledOnTouchEvent()); 279 280 view.setTouchDelegate(null); 281 assertNull(view.getTouchDelegate()); 282 } 283 284 @UiThreadTest 285 public void testAccessTag() { 286 ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root); 287 MockView mockView = (MockView) mActivity.findViewById(R.id.mock_view); 288 MockView scrollView = (MockView) mActivity.findViewById(R.id.scroll_view); 289 290 ViewData viewData = new ViewData(); 291 viewData.childCount = 3; 292 viewData.tag = "linearLayout"; 293 viewData.firstChild = mockView; 294 viewGroup.setTag(viewData); 295 viewGroup.setFocusable(true); 296 assertSame(viewData, viewGroup.getTag()); 297 298 final String tag = "mock"; 299 assertNull(mockView.getTag()); 300 mockView.setTag(tag); 301 assertEquals(tag, mockView.getTag()); 302 303 scrollView.setTag(viewGroup); 304 assertSame(viewGroup, scrollView.getTag()); 305 306 assertSame(viewGroup, viewGroup.findViewWithTag(viewData)); 307 assertSame(mockView, viewGroup.findViewWithTag(tag)); 308 assertSame(scrollView, viewGroup.findViewWithTag(viewGroup)); 309 310 mockView.setTag(null); 311 assertNull(mockView.getTag()); 312 } 313 314 public void testOnSizeChanged() throws Throwable { 315 final ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root); 316 final MockView mockView = new MockView(mActivity); 317 assertEquals(-1, mockView.getOldWOnSizeChanged()); 318 assertEquals(-1, mockView.getOldHOnSizeChanged()); 319 runTestOnUiThread(new Runnable() { 320 public void run() { 321 viewGroup.addView(mockView); 322 } 323 }); 324 getInstrumentation().waitForIdleSync(); 325 assertTrue(mockView.hasCalledOnSizeChanged()); 326 assertEquals(0, mockView.getOldWOnSizeChanged()); 327 assertEquals(0, mockView.getOldHOnSizeChanged()); 328 329 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 330 assertTrue(view.hasCalledOnSizeChanged()); 331 view.reset(); 332 assertEquals(-1, view.getOldWOnSizeChanged()); 333 assertEquals(-1, view.getOldHOnSizeChanged()); 334 int oldw = view.getWidth(); 335 int oldh = view.getHeight(); 336 final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(200, 100); 337 runTestOnUiThread(new Runnable() { 338 public void run() { 339 view.setLayoutParams(layoutParams); 340 } 341 }); 342 getInstrumentation().waitForIdleSync(); 343 assertTrue(view.hasCalledOnSizeChanged()); 344 assertEquals(oldw, view.getOldWOnSizeChanged()); 345 assertEquals(oldh, view.getOldHOnSizeChanged()); 346 } 347 348 public void testGetHitRect() { 349 MockView view = new MockView(mActivity); 350 Rect outRect = new Rect(); 351 352 try { 353 view.getHitRect(null); 354 fail("should throw NullPointerException"); 355 } catch (NullPointerException e) { 356 } 357 358 View mockView = mActivity.findViewById(R.id.mock_view); 359 mockView.getHitRect(outRect); 360 assertEquals(0, outRect.left); 361 assertEquals(0, outRect.top); 362 assertEquals(mockView.getWidth(), outRect.right); 363 assertEquals(mockView.getHeight(), outRect.bottom); 364 } 365 366 public void testForceLayout() { 367 View view = new View(mActivity); 368 369 assertFalse(view.isLayoutRequested()); 370 view.forceLayout(); 371 assertTrue(view.isLayoutRequested()); 372 373 view.forceLayout(); 374 assertTrue(view.isLayoutRequested()); 375 } 376 377 public void testIsLayoutRequested() { 378 View view = new View(mActivity); 379 380 assertFalse(view.isLayoutRequested()); 381 view.forceLayout(); 382 assertTrue(view.isLayoutRequested()); 383 384 view.layout(0, 0, 0, 0); 385 assertFalse(view.isLayoutRequested()); 386 } 387 388 public void testRequestLayout() { 389 MockView view = new MockView(mActivity); 390 assertFalse(view.isLayoutRequested()); 391 assertNull(view.getParent()); 392 393 view.requestLayout(); 394 assertTrue(view.isLayoutRequested()); 395 396 view.setParent(mMockParent); 397 assertFalse(mMockParent.hasRequestLayout()); 398 view.requestLayout(); 399 assertTrue(view.isLayoutRequested()); 400 assertTrue(mMockParent.hasRequestLayout()); 401 } 402 403 public void testLayout() throws Throwable { 404 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 405 assertTrue(view.hasCalledOnLayout()); 406 407 view.reset(); 408 assertFalse(view.hasCalledOnLayout()); 409 runTestOnUiThread(new Runnable() { 410 public void run() { 411 view.requestLayout(); 412 } 413 }); 414 getInstrumentation().waitForIdleSync(); 415 assertTrue(view.hasCalledOnLayout()); 416 } 417 418 public void testGetBaseline() { 419 View view = new View(mActivity); 420 421 assertEquals(-1, view.getBaseline()); 422 } 423 424 public void testAccessBackground() { 425 View view = new View(mActivity); 426 Drawable d1 = mResources.getDrawable(R.drawable.scenery); 427 Drawable d2 = mResources.getDrawable(R.drawable.pass); 428 429 assertNull(view.getBackground()); 430 431 view.setBackgroundDrawable(d1); 432 assertEquals(d1, view.getBackground()); 433 434 view.setBackgroundDrawable(d2); 435 assertEquals(d2, view.getBackground()); 436 437 view.setBackgroundDrawable(null); 438 assertNull(view.getBackground()); 439 } 440 441 public void testSetBackgroundResource() { 442 View view = new View(mActivity); 443 444 assertNull(view.getBackground()); 445 446 view.setBackgroundResource(R.drawable.pass); 447 assertNotNull(view.getBackground()); 448 449 view.setBackgroundResource(0); 450 assertNull(view.getBackground()); 451 } 452 453 public void testAccessDrawingCacheBackgroundColor() { 454 View view = new View(mActivity); 455 456 assertEquals(0, view.getDrawingCacheBackgroundColor()); 457 458 view.setDrawingCacheBackgroundColor(0xFF00FF00); 459 assertEquals(0xFF00FF00, view.getDrawingCacheBackgroundColor()); 460 461 view.setDrawingCacheBackgroundColor(-1); 462 assertEquals(-1, view.getDrawingCacheBackgroundColor()); 463 } 464 465 public void testSetBackgroundColor() { 466 View view = new View(mActivity); 467 ColorDrawable colorDrawable; 468 assertNull(view.getBackground()); 469 470 view.setBackgroundColor(0xFFFF0000); 471 colorDrawable = (ColorDrawable) view.getBackground(); 472 assertNotNull(colorDrawable); 473 assertEquals(0xFF, colorDrawable.getAlpha()); 474 475 view.setBackgroundColor(0); 476 colorDrawable = (ColorDrawable) view.getBackground(); 477 assertNotNull(colorDrawable); 478 assertEquals(0, colorDrawable.getAlpha()); 479 } 480 481 public void testVerifyDrawable() { 482 MockView view = new MockView(mActivity); 483 Drawable d1 = mResources.getDrawable(R.drawable.scenery); 484 Drawable d2 = mResources.getDrawable(R.drawable.pass); 485 486 assertNull(view.getBackground()); 487 assertTrue(view.verifyDrawable(null)); 488 assertFalse(view.verifyDrawable(d1)); 489 490 view.setBackgroundDrawable(d1); 491 assertTrue(view.verifyDrawable(d1)); 492 assertFalse(view.verifyDrawable(d2)); 493 } 494 495 public void testGetDrawingRect() { 496 MockView view = new MockView(mActivity); 497 Rect outRect = new Rect(); 498 499 view.getDrawingRect(outRect); 500 assertEquals(0, outRect.left); 501 assertEquals(0, outRect.top); 502 assertEquals(0, outRect.right); 503 assertEquals(0, outRect.bottom); 504 505 view.scrollTo(10, 100); 506 view.getDrawingRect(outRect); 507 assertEquals(10, outRect.left); 508 assertEquals(100, outRect.top); 509 assertEquals(10, outRect.right); 510 assertEquals(100, outRect.bottom); 511 512 View mockView = mActivity.findViewById(R.id.mock_view); 513 mockView.getDrawingRect(outRect); 514 assertEquals(0, outRect.left); 515 assertEquals(0, outRect.top); 516 assertEquals(mockView.getWidth(), outRect.right); 517 assertEquals(mockView.getHeight(), outRect.bottom); 518 } 519 520 public void testGetFocusedRect() { 521 MockView view = new MockView(mActivity); 522 Rect outRect = new Rect(); 523 524 view.getFocusedRect(outRect); 525 assertEquals(0, outRect.left); 526 assertEquals(0, outRect.top); 527 assertEquals(0, outRect.right); 528 assertEquals(0, outRect.bottom); 529 530 view.scrollTo(10, 100); 531 view.getFocusedRect(outRect); 532 assertEquals(10, outRect.left); 533 assertEquals(100, outRect.top); 534 assertEquals(10, outRect.right); 535 assertEquals(100, outRect.bottom); 536 } 537 538 public void testGetGlobalVisibleRectPoint() throws Throwable { 539 final View view = mActivity.findViewById(R.id.mock_view); 540 final ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root); 541 Rect rect = new Rect(); 542 Point point = new Point(); 543 544 assertTrue(view.getGlobalVisibleRect(rect, point)); 545 Rect rcParent = new Rect(); 546 Point ptParent = new Point(); 547 viewGroup.getGlobalVisibleRect(rcParent, ptParent); 548 assertEquals(rcParent.left, rect.left); 549 assertEquals(rcParent.top, rect.top); 550 assertEquals(rect.left + view.getWidth(), rect.right); 551 assertEquals(rect.top + view.getHeight(), rect.bottom); 552 assertEquals(ptParent.x, point.x); 553 assertEquals(ptParent.y, point.y); 554 555 // width is 0 556 final LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(0, 300); 557 runTestOnUiThread(new Runnable() { 558 public void run() { 559 view.setLayoutParams(layoutParams1); 560 } 561 }); 562 getInstrumentation().waitForIdleSync(); 563 assertFalse(view.getGlobalVisibleRect(rect, point)); 564 565 // height is -10 566 final LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(200, -10); 567 runTestOnUiThread(new Runnable() { 568 public void run() { 569 view.setLayoutParams(layoutParams2); 570 } 571 }); 572 getInstrumentation().waitForIdleSync(); 573 assertFalse(view.getGlobalVisibleRect(rect, point)); 574 575 Display display = getActivity().getWindowManager().getDefaultDisplay(); 576 int halfWidth = display.getWidth() / 2; 577 int halfHeight = display.getHeight() /2; 578 579 final LinearLayout.LayoutParams layoutParams3 = 580 new LinearLayout.LayoutParams(halfWidth, halfHeight); 581 runTestOnUiThread(new Runnable() { 582 public void run() { 583 view.setLayoutParams(layoutParams3); 584 } 585 }); 586 getInstrumentation().waitForIdleSync(); 587 assertTrue(view.getGlobalVisibleRect(rect, point)); 588 assertEquals(rcParent.left, rect.left); 589 assertEquals(rcParent.top, rect.top); 590 assertEquals(rect.left + halfWidth, rect.right); 591 assertEquals(rect.top + halfHeight, rect.bottom); 592 assertEquals(ptParent.x, point.x); 593 assertEquals(ptParent.y, point.y); 594 } 595 596 public void testGetGlobalVisibleRect() throws Throwable { 597 final View view = mActivity.findViewById(R.id.mock_view); 598 final ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root); 599 Rect rect = new Rect(); 600 601 assertTrue(view.getGlobalVisibleRect(rect)); 602 Rect rcParent = new Rect(); 603 viewGroup.getGlobalVisibleRect(rcParent); 604 assertEquals(rcParent.left, rect.left); 605 assertEquals(rcParent.top, rect.top); 606 assertEquals(rect.left + view.getWidth(), rect.right); 607 assertEquals(rect.top + view.getHeight(), rect.bottom); 608 609 // width is 0 610 final LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(0, 300); 611 runTestOnUiThread(new Runnable() { 612 public void run() { 613 view.setLayoutParams(layoutParams1); 614 } 615 }); 616 getInstrumentation().waitForIdleSync(); 617 assertFalse(view.getGlobalVisibleRect(rect)); 618 619 // height is -10 620 final LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(200, -10); 621 runTestOnUiThread(new Runnable() { 622 public void run() { 623 view.setLayoutParams(layoutParams2); 624 } 625 }); 626 getInstrumentation().waitForIdleSync(); 627 assertFalse(view.getGlobalVisibleRect(rect)); 628 629 Display display = getActivity().getWindowManager().getDefaultDisplay(); 630 int halfWidth = display.getWidth() / 2; 631 int halfHeight = display.getHeight() /2; 632 633 final LinearLayout.LayoutParams layoutParams3 = 634 new LinearLayout.LayoutParams(halfWidth, halfHeight); 635 runTestOnUiThread(new Runnable() { 636 public void run() { 637 view.setLayoutParams(layoutParams3); 638 } 639 }); 640 getInstrumentation().waitForIdleSync(); 641 assertTrue(view.getGlobalVisibleRect(rect)); 642 assertEquals(rcParent.left, rect.left); 643 assertEquals(rcParent.top, rect.top); 644 assertEquals(rect.left + halfWidth, rect.right); 645 assertEquals(rect.top + halfHeight, rect.bottom); 646 } 647 648 public void testComputeHorizontalScroll() throws Throwable { 649 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 650 651 assertEquals(0, view.computeHorizontalScrollOffset()); 652 assertEquals(view.getWidth(), view.computeHorizontalScrollRange()); 653 assertEquals(view.getWidth(), view.computeHorizontalScrollExtent()); 654 655 runTestOnUiThread(new Runnable() { 656 public void run() { 657 view.scrollTo(12, 0); 658 } 659 }); 660 getInstrumentation().waitForIdleSync(); 661 assertEquals(12, view.computeHorizontalScrollOffset()); 662 assertEquals(view.getWidth(), view.computeHorizontalScrollRange()); 663 assertEquals(view.getWidth(), view.computeHorizontalScrollExtent()); 664 665 runTestOnUiThread(new Runnable() { 666 public void run() { 667 view.scrollBy(12, 0); 668 } 669 }); 670 getInstrumentation().waitForIdleSync(); 671 assertEquals(24, view.computeHorizontalScrollOffset()); 672 assertEquals(view.getWidth(), view.computeHorizontalScrollRange()); 673 assertEquals(view.getWidth(), view.computeHorizontalScrollExtent()); 674 675 int newWidth = 200; 676 final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(newWidth, 100); 677 runTestOnUiThread(new Runnable() { 678 public void run() { 679 view.setLayoutParams(layoutParams); 680 } 681 }); 682 getInstrumentation().waitForIdleSync(); 683 assertEquals(24, view.computeHorizontalScrollOffset()); 684 assertEquals(newWidth, view.getWidth()); 685 assertEquals(view.getWidth(), view.computeHorizontalScrollRange()); 686 assertEquals(view.getWidth(), view.computeHorizontalScrollExtent()); 687 } 688 689 public void testComputeVerticalScroll() throws Throwable { 690 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 691 692 assertEquals(0, view.computeVerticalScrollOffset()); 693 assertEquals(view.getHeight(), view.computeVerticalScrollRange()); 694 assertEquals(view.getHeight(), view.computeVerticalScrollExtent()); 695 696 final int scrollToY = 34; 697 runTestOnUiThread(new Runnable() { 698 public void run() { 699 view.scrollTo(0, scrollToY); 700 } 701 }); 702 getInstrumentation().waitForIdleSync(); 703 assertEquals(scrollToY, view.computeVerticalScrollOffset()); 704 assertEquals(view.getHeight(), view.computeVerticalScrollRange()); 705 assertEquals(view.getHeight(), view.computeVerticalScrollExtent()); 706 707 final int scrollByY = 200; 708 runTestOnUiThread(new Runnable() { 709 public void run() { 710 view.scrollBy(0, scrollByY); 711 } 712 }); 713 getInstrumentation().waitForIdleSync(); 714 assertEquals(scrollToY + scrollByY, view.computeVerticalScrollOffset()); 715 assertEquals(view.getHeight(), view.computeVerticalScrollRange()); 716 assertEquals(view.getHeight(), view.computeVerticalScrollExtent()); 717 718 int newHeight = 333; 719 final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(200, newHeight); 720 runTestOnUiThread(new Runnable() { 721 public void run() { 722 view.setLayoutParams(layoutParams); 723 } 724 }); 725 getInstrumentation().waitForIdleSync(); 726 assertEquals(scrollToY + scrollByY, view.computeVerticalScrollOffset()); 727 assertEquals(newHeight, view.getHeight()); 728 assertEquals(view.getHeight(), view.computeVerticalScrollRange()); 729 assertEquals(view.getHeight(), view.computeVerticalScrollExtent()); 730 } 731 732 public void testGetFadingEdgeStrength() throws Throwable { 733 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 734 735 assertEquals(0f, view.getLeftFadingEdgeStrength()); 736 assertEquals(0f, view.getRightFadingEdgeStrength()); 737 assertEquals(0f, view.getTopFadingEdgeStrength()); 738 assertEquals(0f, view.getBottomFadingEdgeStrength()); 739 740 runTestOnUiThread(new Runnable() { 741 public void run() { 742 view.scrollTo(10, 10); 743 } 744 }); 745 getInstrumentation().waitForIdleSync(); 746 assertEquals(1f, view.getLeftFadingEdgeStrength()); 747 assertEquals(0f, view.getRightFadingEdgeStrength()); 748 assertEquals(1f, view.getTopFadingEdgeStrength()); 749 assertEquals(0f, view.getBottomFadingEdgeStrength()); 750 751 runTestOnUiThread(new Runnable() { 752 public void run() { 753 view.scrollTo(-10, -10); 754 } 755 }); 756 getInstrumentation().waitForIdleSync(); 757 assertEquals(0f, view.getLeftFadingEdgeStrength()); 758 assertEquals(1f, view.getRightFadingEdgeStrength()); 759 assertEquals(0f, view.getTopFadingEdgeStrength()); 760 assertEquals(1f, view.getBottomFadingEdgeStrength()); 761 } 762 763 public void testGetLeftFadingEdgeStrength() { 764 MockView view = new MockView(mActivity); 765 766 assertEquals(0.0f, view.getLeftFadingEdgeStrength()); 767 768 view.scrollTo(1, 0); 769 assertEquals(1.0f, view.getLeftFadingEdgeStrength()); 770 } 771 772 public void testGetRightFadingEdgeStrength() { 773 MockView view = new MockView(mActivity); 774 775 assertEquals(0.0f, view.getRightFadingEdgeStrength()); 776 777 view.scrollTo(-1, 0); 778 assertEquals(1.0f, view.getRightFadingEdgeStrength()); 779 } 780 781 public void testGetBottomFadingEdgeStrength() { 782 MockView view = new MockView(mActivity); 783 784 assertEquals(0.0f, view.getBottomFadingEdgeStrength()); 785 786 view.scrollTo(0, -2); 787 assertEquals(1.0f, view.getBottomFadingEdgeStrength()); 788 } 789 790 public void testGetTopFadingEdgeStrength() { 791 MockView view = new MockView(mActivity); 792 793 assertEquals(0.0f, view.getTopFadingEdgeStrength()); 794 795 view.scrollTo(0, 2); 796 assertEquals(1.0f, view.getTopFadingEdgeStrength()); 797 } 798 799 public void testResolveSize() { 800 assertEquals(50, View.resolveSize(50, View.MeasureSpec.UNSPECIFIED)); 801 802 assertEquals(40, View.resolveSize(50, 40 | View.MeasureSpec.EXACTLY)); 803 804 assertEquals(30, View.resolveSize(50, 30 | View.MeasureSpec.AT_MOST)); 805 806 assertEquals(20, View.resolveSize(20, 30 | View.MeasureSpec.AT_MOST)); 807 } 808 809 public void testGetDefaultSize() { 810 assertEquals(50, View.getDefaultSize(50, View.MeasureSpec.UNSPECIFIED)); 811 812 assertEquals(40, View.getDefaultSize(50, 40 | View.MeasureSpec.EXACTLY)); 813 814 assertEquals(30, View.getDefaultSize(50, 30 | View.MeasureSpec.AT_MOST)); 815 816 assertEquals(30, View.getDefaultSize(20, 30 | View.MeasureSpec.AT_MOST)); 817 } 818 819 public void testAccessId() { 820 View view = new View(mActivity); 821 822 assertEquals(View.NO_ID, view.getId()); 823 824 view.setId(10); 825 assertEquals(10, view.getId()); 826 827 view.setId(0xFFFFFFFF); 828 assertEquals(0xFFFFFFFF, view.getId()); 829 } 830 831 public void testAccessLongClickable() { 832 View view = new View(mActivity); 833 834 assertFalse(view.isLongClickable()); 835 836 view.setLongClickable(true); 837 assertTrue(view.isLongClickable()); 838 839 view.setLongClickable(false); 840 assertFalse(view.isLongClickable()); 841 } 842 843 public void testAccessClickable() { 844 View view = new View(mActivity); 845 846 assertFalse(view.isClickable()); 847 848 view.setClickable(true); 849 assertTrue(view.isClickable()); 850 851 view.setClickable(false); 852 assertFalse(view.isClickable()); 853 } 854 855 public void testGetContextMenuInfo() { 856 MockView view = new MockView(mActivity); 857 858 assertNull(view.getContextMenuInfo()); 859 } 860 861 public void testSetOnCreateContextMenuListener() { 862 View view = new View(mActivity); 863 assertFalse(view.isLongClickable()); 864 865 view.setOnCreateContextMenuListener(null); 866 assertTrue(view.isLongClickable()); 867 868 view.setOnCreateContextMenuListener(new OnCreateContextMenuListenerImpl()); 869 assertTrue(view.isLongClickable()); 870 } 871 872 public void testCreateContextMenu() { 873 OnCreateContextMenuListenerImpl listener = new OnCreateContextMenuListenerImpl(); 874 MockView view = new MockView(mActivity); 875 ContextMenu contextMenu = new ContextMenuBuilder(mActivity); 876 view.setParent(mMockParent); 877 view.setOnCreateContextMenuListener(listener); 878 assertFalse(view.hasCalledOnCreateContextMenu()); 879 assertFalse(mMockParent.hasCreateContextMenu()); 880 assertFalse(listener.hasOnCreateContextMenu()); 881 882 view.createContextMenu(contextMenu); 883 assertTrue(view.hasCalledOnCreateContextMenu()); 884 assertTrue(mMockParent.hasCreateContextMenu()); 885 assertTrue(listener.hasOnCreateContextMenu()); 886 887 try { 888 view.createContextMenu(null); 889 fail("should throw NullPointerException"); 890 } catch (NullPointerException e) { 891 } 892 } 893 894 public void testAddFocusables() { 895 View view = new View(mActivity); 896 ArrayList<View> viewList = new ArrayList<View>(); 897 898 // view is not focusable 899 assertFalse(view.isFocusable()); 900 assertEquals(0, viewList.size()); 901 view.addFocusables(viewList, 0); 902 assertEquals(0, viewList.size()); 903 904 // view is focusable 905 view.setFocusable(true); 906 view.addFocusables(viewList, 0); 907 assertEquals(1, viewList.size()); 908 assertEquals(view, viewList.get(0)); 909 910 // null array should be ignored 911 view.addFocusables(null, 0); 912 } 913 914 public void testGetFocusables() { 915 View view = new View(mActivity); 916 ArrayList<View> viewList; 917 918 // view is not focusable 919 assertFalse(view.isFocusable()); 920 viewList = view.getFocusables(0); 921 assertEquals(0, viewList.size()); 922 923 // view is focusable 924 view.setFocusable(true); 925 viewList = view.getFocusables(0); 926 assertEquals(1, viewList.size()); 927 assertEquals(view, viewList.get(0)); 928 929 viewList = view.getFocusables(-1); 930 assertEquals(1, viewList.size()); 931 assertEquals(view, viewList.get(0)); 932 } 933 934 public void testGetRootView() { 935 MockView view = new MockView(mActivity); 936 937 assertNull(view.getParent()); 938 assertEquals(view, view.getRootView()); 939 940 view.setParent(mMockParent); 941 assertEquals(mMockParent, view.getRootView()); 942 } 943 944 public void testGetSolidColor() { 945 View view = new View(mActivity); 946 947 assertEquals(0, view.getSolidColor()); 948 } 949 950 public void testSetMinimumWidth() { 951 MockView view = new MockView(mActivity); 952 assertEquals(0, view.getSuggestedMinimumWidth()); 953 954 view.setMinimumWidth(100); 955 assertEquals(100, view.getSuggestedMinimumWidth()); 956 957 view.setMinimumWidth(-100); 958 assertEquals(-100, view.getSuggestedMinimumWidth()); 959 } 960 961 public void testGetSuggestedMinimumWidth() { 962 MockView view = new MockView(mActivity); 963 Drawable d = mResources.getDrawable(R.drawable.scenery); 964 int drawableMinimumWidth = d.getMinimumWidth(); 965 966 // drawable is null 967 view.setMinimumWidth(100); 968 assertNull(view.getBackground()); 969 assertEquals(100, view.getSuggestedMinimumWidth()); 970 971 // drawable minimum width is larger than mMinWidth 972 view.setBackgroundDrawable(d); 973 view.setMinimumWidth(drawableMinimumWidth - 10); 974 assertEquals(drawableMinimumWidth, view.getSuggestedMinimumWidth()); 975 976 // drawable minimum width is smaller than mMinWidth 977 view.setMinimumWidth(drawableMinimumWidth + 10); 978 assertEquals(drawableMinimumWidth + 10, view.getSuggestedMinimumWidth()); 979 } 980 981 public void testSetMinimumHeight() { 982 MockView view = new MockView(mActivity); 983 assertEquals(0, view.getSuggestedMinimumHeight()); 984 985 view.setMinimumHeight(100); 986 assertEquals(100, view.getSuggestedMinimumHeight()); 987 988 view.setMinimumHeight(-100); 989 assertEquals(-100, view.getSuggestedMinimumHeight()); 990 } 991 992 public void testGetSuggestedMinimumHeight() { 993 MockView view = new MockView(mActivity); 994 Drawable d = mResources.getDrawable(R.drawable.scenery); 995 int drawableMinimumHeight = d.getMinimumHeight(); 996 997 // drawable is null 998 view.setMinimumHeight(100); 999 assertNull(view.getBackground()); 1000 assertEquals(100, view.getSuggestedMinimumHeight()); 1001 1002 // drawable minimum height is larger than mMinHeight 1003 view.setBackgroundDrawable(d); 1004 view.setMinimumHeight(drawableMinimumHeight - 10); 1005 assertEquals(drawableMinimumHeight, view.getSuggestedMinimumHeight()); 1006 1007 // drawable minimum height is smaller than mMinHeight 1008 view.setMinimumHeight(drawableMinimumHeight + 10); 1009 assertEquals(drawableMinimumHeight + 10, view.getSuggestedMinimumHeight()); 1010 } 1011 1012 public void testAccessWillNotCacheDrawing() { 1013 View view = new View(mActivity); 1014 1015 assertFalse(view.willNotCacheDrawing()); 1016 1017 view.setWillNotCacheDrawing(true); 1018 assertTrue(view.willNotCacheDrawing()); 1019 } 1020 1021 public void testAccessDrawingCacheEnabled() { 1022 View view = new View(mActivity); 1023 1024 assertFalse(view.isDrawingCacheEnabled()); 1025 1026 view.setDrawingCacheEnabled(true); 1027 assertTrue(view.isDrawingCacheEnabled()); 1028 } 1029 1030 public void testGetDrawingCache() { 1031 MockView view = new MockView(mActivity); 1032 1033 // should not call buildDrawingCache when getDrawingCache 1034 assertNull(view.getDrawingCache()); 1035 1036 // should call buildDrawingCache when getDrawingCache 1037 view = (MockView) mActivity.findViewById(R.id.mock_view); 1038 view.setDrawingCacheEnabled(true); 1039 Bitmap bitmap1 = view.getDrawingCache(); 1040 assertNotNull(bitmap1); 1041 assertEquals(view.getWidth(), bitmap1.getWidth()); 1042 assertEquals(view.getHeight(), bitmap1.getHeight()); 1043 1044 view.setWillNotCacheDrawing(true); 1045 assertNull(view.getDrawingCache()); 1046 1047 view.setWillNotCacheDrawing(false); 1048 // build a new drawingcache 1049 Bitmap bitmap2 = view.getDrawingCache(); 1050 assertNotSame(bitmap1, bitmap2); 1051 } 1052 1053 public void testBuildAndDestroyDrawingCache() { 1054 MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 1055 1056 assertNull(view.getDrawingCache()); 1057 1058 view.buildDrawingCache(); 1059 Bitmap bitmap = view.getDrawingCache(); 1060 assertNotNull(bitmap); 1061 assertEquals(view.getWidth(), bitmap.getWidth()); 1062 assertEquals(view.getHeight(), bitmap.getHeight()); 1063 1064 view.destroyDrawingCache(); 1065 assertNull(view.getDrawingCache()); 1066 } 1067 1068 public void testAccessWillNotDraw() { 1069 View view = new View(mActivity); 1070 1071 assertFalse(view.willNotDraw()); 1072 1073 view.setWillNotDraw(true); 1074 assertTrue(view.willNotDraw()); 1075 } 1076 1077 public void testAccessDrawingCacheQuality() { 1078 View view = new View(mActivity); 1079 1080 assertEquals(0, view.getDrawingCacheQuality()); 1081 1082 view.setDrawingCacheQuality(1); 1083 assertEquals(0, view.getDrawingCacheQuality()); 1084 1085 view.setDrawingCacheQuality(0x00100000); 1086 assertEquals(0x00100000, view.getDrawingCacheQuality()); 1087 1088 view.setDrawingCacheQuality(0x00080000); 1089 assertEquals(0x00080000, view.getDrawingCacheQuality()); 1090 1091 view.setDrawingCacheQuality(0xffffffff); 1092 // 0x00180000 is View.DRAWING_CACHE_QUALITY_MASK 1093 assertEquals(0x00180000, view.getDrawingCacheQuality()); 1094 } 1095 1096 public void testDispatchSetSelected() { 1097 MockView mockView1 = new MockView(mActivity); 1098 MockView mockView2 = new MockView(mActivity); 1099 mockView1.setParent(mMockParent); 1100 mockView2.setParent(mMockParent); 1101 1102 mMockParent.dispatchSetSelected(true); 1103 assertFalse(mockView1.isSelected()); 1104 assertFalse(mockView2.isSelected()); 1105 1106 mMockParent.dispatchSetSelected(false); 1107 assertFalse(mockView1.isSelected()); 1108 assertFalse(mockView2.isSelected()); 1109 } 1110 1111 public void testAccessSelected() { 1112 View view = new View(mActivity); 1113 1114 assertFalse(view.isSelected()); 1115 1116 view.setSelected(true); 1117 assertTrue(view.isSelected()); 1118 } 1119 1120 public void testDispatchSetPressed() { 1121 MockView mockView1 = new MockView(mActivity); 1122 MockView mockView2 = new MockView(mActivity); 1123 mockView1.setParent(mMockParent); 1124 mockView2.setParent(mMockParent); 1125 1126 mMockParent.dispatchSetPressed(true); 1127 assertFalse(mockView1.isPressed()); 1128 assertFalse(mockView2.isPressed()); 1129 1130 mMockParent.dispatchSetPressed(false); 1131 assertFalse(mockView1.isPressed()); 1132 assertFalse(mockView2.isPressed()); 1133 } 1134 1135 public void testAccessPressed() { 1136 View view = new View(mActivity); 1137 1138 assertFalse(view.isPressed()); 1139 1140 view.setPressed(true); 1141 assertTrue(view.isPressed()); 1142 } 1143 1144 public void testAccessSoundEffectsEnabled() { 1145 View view = new View(mActivity); 1146 1147 assertTrue(view.isSoundEffectsEnabled()); 1148 1149 view.setSoundEffectsEnabled(false); 1150 assertFalse(view.isSoundEffectsEnabled()); 1151 } 1152 1153 public void testAccessKeepScreenOn() { 1154 View view = new View(mActivity); 1155 1156 assertFalse(view.getKeepScreenOn()); 1157 1158 view.setKeepScreenOn(true); 1159 assertTrue(view.getKeepScreenOn()); 1160 } 1161 1162 public void testAccessDuplicateParentStateEnabled() { 1163 View view = new View(mActivity); 1164 1165 assertFalse(view.isDuplicateParentStateEnabled()); 1166 1167 view.setDuplicateParentStateEnabled(true); 1168 assertTrue(view.isDuplicateParentStateEnabled()); 1169 } 1170 1171 public void testAccessEnabled() { 1172 View view = new View(mActivity); 1173 1174 assertTrue(view.isEnabled()); 1175 1176 view.setEnabled(false); 1177 assertFalse(view.isEnabled()); 1178 } 1179 1180 public void testAccessSaveEnabled() { 1181 View view = new View(mActivity); 1182 1183 assertTrue(view.isSaveEnabled()); 1184 1185 view.setSaveEnabled(false); 1186 assertFalse(view.isSaveEnabled()); 1187 } 1188 1189 public void testShowContextMenu() { 1190 MockView view = new MockView(mActivity); 1191 1192 assertNull(view.getParent()); 1193 try { 1194 view.showContextMenu(); 1195 fail("should throw NullPointerException"); 1196 } catch (NullPointerException e) { 1197 } 1198 1199 view.setParent(mMockParent); 1200 assertFalse(mMockParent.hasShowContextMenuForChild()); 1201 1202 assertFalse(view.showContextMenu()); 1203 assertTrue(mMockParent.hasShowContextMenuForChild()); 1204 } 1205 1206 public void testFitSystemWindows() { 1207 final XmlResourceParser parser = mResources.getLayout(R.layout.view_layout); 1208 final AttributeSet attrs = Xml.asAttributeSet(parser); 1209 Rect insets = new Rect(10, 20, 30, 50); 1210 1211 MockView view = new MockView(mActivity); 1212 assertFalse(view.fitSystemWindows(insets)); 1213 assertFalse(view.fitSystemWindows(null)); 1214 1215 view = new MockView(mActivity, attrs, com.android.internal.R.attr.fitsSystemWindows); 1216 assertFalse(view.fitSystemWindows(insets)); 1217 assertFalse(view.fitSystemWindows(null)); 1218 } 1219 1220 public void testPerformClick() { 1221 View view = new View(mActivity); 1222 OnClickListenerImpl listener = new OnClickListenerImpl(); 1223 1224 assertFalse(view.performClick()); 1225 1226 assertFalse(listener.hasOnClick()); 1227 view.setOnClickListener(listener); 1228 1229 assertTrue(view.performClick()); 1230 assertTrue(listener.hasOnClick()); 1231 1232 view.setOnClickListener(null); 1233 assertFalse(view.performClick()); 1234 } 1235 1236 public void testSetOnClickListener() { 1237 View view = new View(mActivity); 1238 assertFalse(view.performClick()); 1239 assertFalse(view.isClickable()); 1240 1241 view.setOnClickListener(null); 1242 assertFalse(view.performClick()); 1243 assertTrue(view.isClickable()); 1244 1245 view.setOnClickListener(new OnClickListenerImpl()); 1246 assertTrue(view.performClick()); 1247 assertTrue(view.isClickable()); 1248 } 1249 1250 public void testPerformLongClick() { 1251 MockView view = new MockView(mActivity); 1252 OnLongClickListenerImpl listener = new OnLongClickListenerImpl(); 1253 1254 try { 1255 view.performLongClick(); 1256 fail("should throw NullPointerException"); 1257 } catch (NullPointerException e) { 1258 } 1259 1260 view.setParent(mMockParent); 1261 assertFalse(mMockParent.hasShowContextMenuForChild()); 1262 assertFalse(view.performLongClick()); 1263 assertTrue(mMockParent.hasShowContextMenuForChild()); 1264 1265 view.setOnLongClickListener(listener); 1266 mMockParent.reset(); 1267 assertFalse(mMockParent.hasShowContextMenuForChild()); 1268 assertFalse(listener.hasOnLongClick()); 1269 assertTrue(view.performLongClick()); 1270 assertFalse(mMockParent.hasShowContextMenuForChild()); 1271 assertTrue(listener.hasOnLongClick()); 1272 } 1273 1274 public void testSetOnLongClickListener() { 1275 MockView view = new MockView(mActivity); 1276 view.setParent(mMockParent); 1277 assertFalse(view.performLongClick()); 1278 assertFalse(view.isLongClickable()); 1279 1280 view.setOnLongClickListener(null); 1281 assertFalse(view.performLongClick()); 1282 assertTrue(view.isLongClickable()); 1283 1284 view.setOnLongClickListener(new OnLongClickListenerImpl()); 1285 assertTrue(view.performLongClick()); 1286 assertTrue(view.isLongClickable()); 1287 } 1288 1289 public void testAccessOnFocusChangeListener() { 1290 View view = new View(mActivity); 1291 OnFocusChangeListener listener = new OnFocusChangeListenerImpl(); 1292 1293 assertNull(view.getOnFocusChangeListener()); 1294 1295 view.setOnFocusChangeListener(listener); 1296 assertSame(listener, view.getOnFocusChangeListener()); 1297 } 1298 1299 public void testAccessNextFocusUpId() { 1300 View view = new View(mActivity); 1301 1302 assertEquals(View.NO_ID, view.getNextFocusUpId()); 1303 1304 view.setNextFocusUpId(1); 1305 assertEquals(1, view.getNextFocusUpId()); 1306 1307 view.setNextFocusUpId(Integer.MAX_VALUE); 1308 assertEquals(Integer.MAX_VALUE, view.getNextFocusUpId()); 1309 1310 view.setNextFocusUpId(Integer.MIN_VALUE); 1311 assertEquals(Integer.MIN_VALUE, view.getNextFocusUpId()); 1312 } 1313 1314 public void testAccessNextFocusDownId() { 1315 View view = new View(mActivity); 1316 1317 assertEquals(View.NO_ID, view.getNextFocusDownId()); 1318 1319 view.setNextFocusDownId(1); 1320 assertEquals(1, view.getNextFocusDownId()); 1321 1322 view.setNextFocusDownId(Integer.MAX_VALUE); 1323 assertEquals(Integer.MAX_VALUE, view.getNextFocusDownId()); 1324 1325 view.setNextFocusDownId(Integer.MIN_VALUE); 1326 assertEquals(Integer.MIN_VALUE, view.getNextFocusDownId()); 1327 } 1328 1329 public void testAccessNextFocusLeftId() { 1330 View view = new View(mActivity); 1331 1332 assertEquals(View.NO_ID, view.getNextFocusLeftId()); 1333 1334 view.setNextFocusLeftId(1); 1335 assertEquals(1, view.getNextFocusLeftId()); 1336 1337 view.setNextFocusLeftId(Integer.MAX_VALUE); 1338 assertEquals(Integer.MAX_VALUE, view.getNextFocusLeftId()); 1339 1340 view.setNextFocusLeftId(Integer.MIN_VALUE); 1341 assertEquals(Integer.MIN_VALUE, view.getNextFocusLeftId()); 1342 } 1343 1344 public void testAccessNextFocusRightId() { 1345 View view = new View(mActivity); 1346 1347 assertEquals(View.NO_ID, view.getNextFocusRightId()); 1348 1349 view.setNextFocusRightId(1); 1350 assertEquals(1, view.getNextFocusRightId()); 1351 1352 view.setNextFocusRightId(Integer.MAX_VALUE); 1353 assertEquals(Integer.MAX_VALUE, view.getNextFocusRightId()); 1354 1355 view.setNextFocusRightId(Integer.MIN_VALUE); 1356 assertEquals(Integer.MIN_VALUE, view.getNextFocusRightId()); 1357 } 1358 1359 public void testAccessMeasuredDimension() { 1360 MockView view = new MockView(mActivity); 1361 assertEquals(0, view.getMeasuredWidth()); 1362 assertEquals(0, view.getMeasuredHeight()); 1363 1364 view.setMeasuredDimensionWrapper(20, 30); 1365 assertEquals(20, view.getMeasuredWidth()); 1366 assertEquals(30, view.getMeasuredHeight()); 1367 } 1368 1369 public void testMeasure() throws Throwable { 1370 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 1371 assertTrue(view.hasCalledOnMeasure()); 1372 assertEquals(100, view.getMeasuredWidth()); 1373 assertEquals(200, view.getMeasuredHeight()); 1374 1375 view.reset(); 1376 runTestOnUiThread(new Runnable() { 1377 public void run() { 1378 view.requestLayout(); 1379 } 1380 }); 1381 getInstrumentation().waitForIdleSync(); 1382 assertTrue(view.hasCalledOnMeasure()); 1383 assertEquals(100, view.getMeasuredWidth()); 1384 assertEquals(200, view.getMeasuredHeight()); 1385 1386 view.reset(); 1387 final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(200, 100); 1388 runTestOnUiThread(new Runnable() { 1389 public void run() { 1390 view.setLayoutParams(layoutParams); 1391 } 1392 }); 1393 getInstrumentation().waitForIdleSync(); 1394 assertTrue(view.hasCalledOnMeasure()); 1395 assertEquals(200, view.getMeasuredWidth()); 1396 assertEquals(100, view.getMeasuredHeight()); 1397 } 1398 1399 public void testAccessLayoutParams() { 1400 View view = new View(mActivity); 1401 ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(10, 20); 1402 1403 assertNull(view.getLayoutParams()); 1404 1405 try { 1406 view.setLayoutParams(null); 1407 fail("should throw NullPointerException"); 1408 } catch (NullPointerException e) { 1409 } 1410 1411 assertFalse(view.isLayoutRequested()); 1412 view.setLayoutParams(params); 1413 assertSame(params, view.getLayoutParams()); 1414 assertTrue(view.isLayoutRequested()); 1415 } 1416 1417 public void testIsShown() { 1418 MockView view = new MockView(mActivity); 1419 1420 view.setVisibility(View.INVISIBLE); 1421 assertFalse(view.isShown()); 1422 1423 view.setVisibility(View.VISIBLE); 1424 assertNull(view.getParent()); 1425 assertFalse(view.isShown()); 1426 1427 view.setParent(mMockParent); 1428 // mMockParent is not a instance of ViewRoot 1429 assertFalse(view.isShown()); 1430 } 1431 1432 public void testGetDrawingTime() { 1433 View view = new View(mActivity); 1434 // mAttachInfo is null 1435 assertEquals(0, view.getDrawingTime()); 1436 1437 // mAttachInfo is not null 1438 view = mActivity.findViewById(R.id.fit_windows); 1439 assertEquals(SystemClock.uptimeMillis(), view.getDrawingTime(), 1000); 1440 } 1441 1442 public void testScheduleDrawable() { 1443 View view = new View(mActivity); 1444 Drawable drawable = new StateListDrawable(); 1445 Runnable what = new Runnable() { 1446 public void run() { 1447 // do nothing 1448 } 1449 }; 1450 1451 // mAttachInfo is null 1452 view.scheduleDrawable(drawable, what, 1000); 1453 1454 view.setBackgroundDrawable(drawable); 1455 view.scheduleDrawable(drawable, what, 1000); 1456 1457 view.scheduleDrawable(null, null, -1000); 1458 1459 // mAttachInfo is not null 1460 view = mActivity.findViewById(R.id.fit_windows); 1461 view.scheduleDrawable(drawable, what, 1000); 1462 1463 view.scheduleDrawable(view.getBackground(), what, 1000); 1464 view.unscheduleDrawable(view.getBackground(), what); 1465 1466 view.scheduleDrawable(null, null, -1000); 1467 } 1468 1469 public void testUnscheduleDrawable() { 1470 View view = new View(mActivity); 1471 Drawable drawable = new StateListDrawable(); 1472 Runnable what = new Runnable() { 1473 public void run() { 1474 // do nothing 1475 } 1476 }; 1477 1478 // mAttachInfo is null 1479 view.unscheduleDrawable(drawable, what); 1480 1481 view.setBackgroundDrawable(drawable); 1482 view.unscheduleDrawable(drawable); 1483 1484 view.unscheduleDrawable(null, null); 1485 view.unscheduleDrawable(null); 1486 1487 // mAttachInfo is not null 1488 view = mActivity.findViewById(R.id.fit_windows); 1489 view.unscheduleDrawable(drawable); 1490 1491 view.scheduleDrawable(view.getBackground(), what, 1000); 1492 view.unscheduleDrawable(view.getBackground(), what); 1493 1494 view.unscheduleDrawable(null); 1495 view.unscheduleDrawable(null, null); 1496 } 1497 1498 public void testGetWindowVisibility() { 1499 View view = new View(mActivity); 1500 // mAttachInfo is null 1501 assertEquals(View.GONE, view.getWindowVisibility()); 1502 1503 // mAttachInfo is not null 1504 view = mActivity.findViewById(R.id.fit_windows); 1505 assertEquals(View.VISIBLE, view.getWindowVisibility()); 1506 } 1507 1508 public void testGetWindowToken() { 1509 View view = new View(mActivity); 1510 // mAttachInfo is null 1511 assertNull(view.getWindowToken()); 1512 1513 // mAttachInfo is not null 1514 view = mActivity.findViewById(R.id.fit_windows); 1515 assertNotNull(view.getWindowToken()); 1516 } 1517 1518 public void testHasWindowFocus() { 1519 View view = new View(mActivity); 1520 // mAttachInfo is null 1521 assertFalse(view.hasWindowFocus()); 1522 1523 // mAttachInfo is not null 1524 final View view2 = mActivity.findViewById(R.id.fit_windows); 1525 // Wait until the window has been focused. 1526 new PollingCheck(TIMEOUT_DELTA) { 1527 @Override 1528 protected boolean check() { 1529 return view2.hasWindowFocus(); 1530 } 1531 }.run(); 1532 } 1533 1534 public void testGetHandler() { 1535 MockView view = new MockView(mActivity); 1536 // mAttachInfo is null 1537 assertNull(view.getHandler()); 1538 } 1539 1540 public void testRemoveCallbacks() throws InterruptedException { 1541 final long delay = 500L; 1542 View view = mActivity.findViewById(R.id.mock_view); 1543 MockRunnable runner = new MockRunnable(); 1544 assertTrue(view.postDelayed(runner, delay)); 1545 assertTrue(view.removeCallbacks(runner)); 1546 assertTrue(view.removeCallbacks(null)); 1547 assertTrue(view.removeCallbacks(new MockRunnable())); 1548 Thread.sleep(delay * 2); 1549 assertFalse(runner.hasRun); 1550 // check that the runner actually works 1551 runner = new MockRunnable(); 1552 assertTrue(view.postDelayed(runner, delay)); 1553 Thread.sleep(delay * 2); 1554 assertTrue(runner.hasRun); 1555 } 1556 1557 public void testCancelLongPress() { 1558 View view = new View(mActivity); 1559 // mAttachInfo is null 1560 view.cancelLongPress(); 1561 1562 // mAttachInfo is not null 1563 view = mActivity.findViewById(R.id.fit_windows); 1564 view.cancelLongPress(); 1565 } 1566 1567 public void testGetViewTreeObserver() { 1568 View view = new View(mActivity); 1569 // mAttachInfo is null 1570 assertNotNull(view.getViewTreeObserver()); 1571 1572 // mAttachInfo is not null 1573 view = mActivity.findViewById(R.id.fit_windows); 1574 assertNotNull(view.getViewTreeObserver()); 1575 } 1576 1577 public void testGetWindowAttachCount() { 1578 MockView view = new MockView(mActivity); 1579 // mAttachInfo is null 1580 assertEquals(0, view.getWindowAttachCount()); 1581 } 1582 1583 @UiThreadTest 1584 public void testOnAttachedToAndDetachedFromWindow() { 1585 MockView mockView = new MockView(mActivity); 1586 ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root); 1587 1588 viewGroup.addView(mockView); 1589 assertTrue(mockView.hasCalledOnAttachedToWindow()); 1590 1591 viewGroup.removeView(mockView); 1592 assertTrue(mockView.hasCalledOnDetachedFromWindow()); 1593 1594 mockView.reset(); 1595 mActivity.setContentView(mockView); 1596 assertTrue(mockView.hasCalledOnAttachedToWindow()); 1597 1598 mActivity.setContentView(R.layout.view_layout); 1599 assertTrue(mockView.hasCalledOnDetachedFromWindow()); 1600 } 1601 1602 public void testGetLocationInWindow() { 1603 int[] location = new int[] { -1, -1 }; 1604 1605 View layout = mActivity.findViewById(R.id.viewlayout_root); 1606 int[] layoutLocation = new int[] { -1, -1 }; 1607 layout.getLocationInWindow(layoutLocation); 1608 1609 final View mockView = mActivity.findViewById(R.id.mock_view); 1610 mockView.getLocationInWindow(location); 1611 assertEquals(layoutLocation[0], location[0]); 1612 assertEquals(layoutLocation[1], location[1]); 1613 1614 View scrollView = mActivity.findViewById(R.id.scroll_view); 1615 scrollView.getLocationInWindow(location); 1616 assertEquals(layoutLocation[0], location[0]); 1617 assertEquals(layoutLocation[1] + mockView.getHeight(), location[1]); 1618 1619 try { 1620 mockView.getLocationInWindow(null); 1621 fail("should throw IllegalArgumentException"); 1622 } catch (IllegalArgumentException e) { 1623 } 1624 1625 try { 1626 mockView.getLocationInWindow(new int[] { 0 }); 1627 fail("should throw IllegalArgumentException"); 1628 } catch (IllegalArgumentException e) { 1629 } 1630 } 1631 1632 public void testGetLocationOnScreen() { 1633 View view = new View(mActivity); 1634 int[] location = new int[] { -1, -1 }; 1635 1636 // mAttachInfo is not null 1637 View layout = mActivity.findViewById(R.id.viewlayout_root); 1638 int[] layoutLocation = new int[] { -1, -1 }; 1639 layout.getLocationOnScreen(layoutLocation); 1640 1641 View mockView = mActivity.findViewById(R.id.mock_view); 1642 mockView.getLocationOnScreen(location); 1643 assertEquals(layoutLocation[0], location[0]); 1644 assertEquals(layoutLocation[1], location[1]); 1645 1646 View scrollView = mActivity.findViewById(R.id.scroll_view); 1647 scrollView.getLocationOnScreen(location); 1648 assertEquals(layoutLocation[0], location[0]); 1649 assertEquals(layoutLocation[1] + mockView.getHeight(), location[1]); 1650 1651 try { 1652 scrollView.getLocationOnScreen(null); 1653 fail("should throw IllegalArgumentException"); 1654 } catch (IllegalArgumentException e) { 1655 } 1656 1657 try { 1658 scrollView.getLocationOnScreen(new int[] { 0 }); 1659 fail("should throw IllegalArgumentException"); 1660 } catch (IllegalArgumentException e) { 1661 } 1662 } 1663 1664 public void testAddTouchables() { 1665 View view = new View(mActivity); 1666 ArrayList<View> result = new ArrayList<View>(); 1667 assertEquals(0, result.size()); 1668 1669 view.addTouchables(result); 1670 assertEquals(0, result.size()); 1671 1672 view.setClickable(true); 1673 view.addTouchables(result); 1674 assertEquals(1, result.size()); 1675 assertSame(view, result.get(0)); 1676 1677 try { 1678 view.addTouchables(null); 1679 fail("should throw NullPointerException"); 1680 } catch (NullPointerException e) { 1681 } 1682 1683 result.clear(); 1684 view.setEnabled(false); 1685 assertTrue(view.isClickable()); 1686 view.addTouchables(result); 1687 assertEquals(0, result.size()); 1688 } 1689 1690 public void testGetTouchables() { 1691 View view = new View(mActivity); 1692 ArrayList<View> result; 1693 1694 result = view.getTouchables(); 1695 assertEquals(0, result.size()); 1696 1697 view.setClickable(true); 1698 result = view.getTouchables(); 1699 assertEquals(1, result.size()); 1700 assertSame(view, result.get(0)); 1701 1702 result.clear(); 1703 view.setEnabled(false); 1704 assertTrue(view.isClickable()); 1705 result = view.getTouchables(); 1706 assertEquals(0, result.size()); 1707 } 1708 1709 public void testInflate() { 1710 View view = View.inflate(mActivity, R.layout.view_layout, null); 1711 assertNotNull(view); 1712 assertTrue(view instanceof LinearLayout); 1713 1714 MockView mockView = (MockView) view.findViewById(R.id.mock_view); 1715 assertNotNull(mockView); 1716 assertTrue(mockView.hasCalledOnFinishInflate()); 1717 } 1718 1719 public void testIsInTouchMode() { 1720 View view = new View(mActivity); 1721 // mAttachInfo is null 1722 assertFalse(view.isInTouchMode()); 1723 1724 // mAttachInfo is not null 1725 view = mActivity.findViewById(R.id.fit_windows); 1726 assertFalse(view.isInTouchMode()); 1727 } 1728 1729 public void testIsInEditMode() { 1730 View view = new View(mActivity); 1731 assertFalse(view.isInEditMode()); 1732 } 1733 1734 public void testPostInvalidate1() { 1735 View view = new View(mActivity); 1736 // mAttachInfo is null 1737 view.postInvalidate(); 1738 1739 // mAttachInfo is not null 1740 view = mActivity.findViewById(R.id.fit_windows); 1741 view.postInvalidate(); 1742 } 1743 1744 public void testPostInvalidate2() { 1745 View view = new View(mActivity); 1746 // mAttachInfo is null 1747 view.postInvalidate(0, 1, 2, 3); 1748 1749 // mAttachInfo is not null 1750 view = mActivity.findViewById(R.id.fit_windows); 1751 view.postInvalidate(10, 20, 30, 40); 1752 view.postInvalidate(0, -20, -30, -40); 1753 } 1754 1755 public void testPostInvalidateDelayed() { 1756 View view = new View(mActivity); 1757 // mAttachInfo is null 1758 view.postInvalidateDelayed(1000); 1759 view.postInvalidateDelayed(500, 0, 0, 100, 200); 1760 1761 // mAttachInfo is not null 1762 view = mActivity.findViewById(R.id.fit_windows); 1763 view.postInvalidateDelayed(1000); 1764 view.postInvalidateDelayed(500, 0, 0, 100, 200); 1765 view.postInvalidateDelayed(-1); 1766 } 1767 1768 public void testPost() { 1769 View view = new View(mActivity); 1770 MockRunnable action = new MockRunnable(); 1771 1772 // mAttachInfo is null 1773 assertTrue(view.post(action)); 1774 assertTrue(view.post(null)); 1775 1776 // mAttachInfo is not null 1777 view = mActivity.findViewById(R.id.fit_windows); 1778 assertTrue(view.post(action)); 1779 assertTrue(view.post(null)); 1780 } 1781 1782 public void testPostDelayed() { 1783 View view = new View(mActivity); 1784 MockRunnable action = new MockRunnable(); 1785 1786 // mAttachInfo is null 1787 assertTrue(view.postDelayed(action, 1000)); 1788 assertTrue(view.postDelayed(null, -1)); 1789 1790 // mAttachInfo is not null 1791 view = mActivity.findViewById(R.id.fit_windows); 1792 assertTrue(view.postDelayed(action, 1000)); 1793 assertTrue(view.postDelayed(null, 0)); 1794 } 1795 1796 @UiThreadTest 1797 public void testPlaySoundEffect() { 1798 MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 1799 // sound effect enabled 1800 view.playSoundEffect(SoundEffectConstants.CLICK); 1801 1802 // sound effect disabled 1803 view.setSoundEffectsEnabled(false); 1804 view.playSoundEffect(SoundEffectConstants.NAVIGATION_DOWN); 1805 1806 // no way to assert the soundConstant be really played. 1807 } 1808 1809 public void testOnKeyShortcut() throws Throwable { 1810 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 1811 runTestOnUiThread(new Runnable() { 1812 public void run() { 1813 view.setFocusable(true); 1814 view.requestFocus(); 1815 } 1816 }); 1817 getInstrumentation().waitForIdleSync(); 1818 assertTrue(view.isFocused()); 1819 1820 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU); 1821 getInstrumentation().sendKeySync(event); 1822 event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0); 1823 getInstrumentation().sendKeySync(event); 1824 assertTrue(view.hasCalledOnKeyShortcut()); 1825 } 1826 1827 public void testOnKeyMultiple() throws Throwable { 1828 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 1829 runTestOnUiThread(new Runnable() { 1830 public void run() { 1831 view.setFocusable(true); 1832 } 1833 }); 1834 1835 assertFalse(view.hasCalledOnKeyMultiple()); 1836 view.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_MULTIPLE, KeyEvent.KEYCODE_ENTER)); 1837 assertTrue(view.hasCalledOnKeyMultiple()); 1838 } 1839 1840 @UiThreadTest 1841 public void testDispatchKeyShortcutEvent() { 1842 MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 1843 view.setFocusable(true); 1844 1845 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0); 1846 view.dispatchKeyShortcutEvent(event); 1847 assertTrue(view.hasCalledOnKeyShortcut()); 1848 1849 try { 1850 view.dispatchKeyShortcutEvent(null); 1851 fail("should throw NullPointerException"); 1852 } catch (NullPointerException e) { 1853 } 1854 } 1855 1856 public void testOnTrackballEvent() throws Throwable { 1857 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 1858 runTestOnUiThread(new Runnable() { 1859 public void run() { 1860 view.setEnabled(true); 1861 view.setFocusable(true); 1862 view.requestFocus(); 1863 } 1864 }); 1865 getInstrumentation().waitForIdleSync(); 1866 1867 long downTime = SystemClock.uptimeMillis(); 1868 long eventTime = downTime; 1869 MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, 1870 1, 2, 0); 1871 getInstrumentation().sendTrackballEventSync(event); 1872 getInstrumentation().waitForIdleSync(); 1873 assertTrue(view.hasCalledOnTrackballEvent()); 1874 } 1875 1876 @UiThreadTest 1877 public void testDispatchTrackballMoveEvent() { 1878 ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root); 1879 MockView mockView1 = new MockView(mActivity); 1880 MockView mockView2 = new MockView(mActivity); 1881 viewGroup.addView(mockView1); 1882 viewGroup.addView(mockView2); 1883 mockView1.setFocusable(true); 1884 mockView2.setFocusable(true); 1885 mockView2.requestFocus(); 1886 1887 long downTime = SystemClock.uptimeMillis(); 1888 long eventTime = downTime; 1889 MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, 1890 1, 2, 0); 1891 mockView1.dispatchTrackballEvent(event); 1892 // issue 1695243 1893 // It passes a trackball motion event down to itself even if it is not the focused view. 1894 assertTrue(mockView1.hasCalledOnTrackballEvent()); 1895 assertFalse(mockView2.hasCalledOnTrackballEvent()); 1896 1897 mockView1.reset(); 1898 mockView2.reset(); 1899 downTime = SystemClock.uptimeMillis(); 1900 eventTime = downTime; 1901 event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, 1, 2, 0); 1902 mockView2.dispatchTrackballEvent(event); 1903 assertFalse(mockView1.hasCalledOnTrackballEvent()); 1904 assertTrue(mockView2.hasCalledOnTrackballEvent()); 1905 } 1906 1907 public void testDispatchUnhandledMove() throws Throwable { 1908 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 1909 runTestOnUiThread(new Runnable() { 1910 public void run() { 1911 view.setFocusable(true); 1912 view.requestFocus(); 1913 } 1914 }); 1915 getInstrumentation().waitForIdleSync(); 1916 1917 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT); 1918 getInstrumentation().sendKeySync(event); 1919 1920 assertTrue(view.hasCalledDispatchUnhandledMove()); 1921 } 1922 1923 public void testWindowVisibilityChanged() throws Throwable { 1924 final MockView mockView = new MockView(mActivity); 1925 final ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root); 1926 1927 runTestOnUiThread(new Runnable() { 1928 public void run() { 1929 viewGroup.addView(mockView); 1930 } 1931 }); 1932 getInstrumentation().waitForIdleSync(); 1933 assertTrue(mockView.hasCalledOnWindowVisibilityChanged()); 1934 1935 mockView.reset(); 1936 runTestOnUiThread(new Runnable() { 1937 public void run() { 1938 getActivity().setVisible(false); 1939 } 1940 }); 1941 getInstrumentation().waitForIdleSync(); 1942 assertTrue(mockView.hasCalledDispatchWindowVisibilityChanged()); 1943 assertTrue(mockView.hasCalledOnWindowVisibilityChanged()); 1944 1945 mockView.reset(); 1946 runTestOnUiThread(new Runnable() { 1947 public void run() { 1948 getActivity().setVisible(true); 1949 } 1950 }); 1951 getInstrumentation().waitForIdleSync(); 1952 assertTrue(mockView.hasCalledDispatchWindowVisibilityChanged()); 1953 assertTrue(mockView.hasCalledOnWindowVisibilityChanged()); 1954 1955 mockView.reset(); 1956 runTestOnUiThread(new Runnable() { 1957 public void run() { 1958 viewGroup.removeView(mockView); 1959 } 1960 }); 1961 getInstrumentation().waitForIdleSync(); 1962 assertTrue(mockView.hasCalledOnWindowVisibilityChanged()); 1963 } 1964 1965 public void testGetLocalVisibleRect() throws Throwable { 1966 final View view = mActivity.findViewById(R.id.mock_view); 1967 Rect rect = new Rect(); 1968 1969 assertTrue(view.getLocalVisibleRect(rect)); 1970 assertEquals(0, rect.left); 1971 assertEquals(0, rect.top); 1972 assertEquals(100, rect.right); 1973 assertEquals(200, rect.bottom); 1974 1975 final LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(0, 300); 1976 runTestOnUiThread(new Runnable() { 1977 public void run() { 1978 view.setLayoutParams(layoutParams1); 1979 } 1980 }); 1981 getInstrumentation().waitForIdleSync(); 1982 assertFalse(view.getLocalVisibleRect(rect)); 1983 1984 final LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(200, -10); 1985 runTestOnUiThread(new Runnable() { 1986 public void run() { 1987 view.setLayoutParams(layoutParams2); 1988 } 1989 }); 1990 getInstrumentation().waitForIdleSync(); 1991 assertFalse(view.getLocalVisibleRect(rect)); 1992 1993 Display display = getActivity().getWindowManager().getDefaultDisplay(); 1994 int halfWidth = display.getWidth() / 2; 1995 int halfHeight = display.getHeight() /2; 1996 1997 final LinearLayout.LayoutParams layoutParams3 = 1998 new LinearLayout.LayoutParams(halfWidth, halfHeight); 1999 runTestOnUiThread(new Runnable() { 2000 public void run() { 2001 view.setLayoutParams(layoutParams3); 2002 view.scrollTo(20, -30); 2003 } 2004 }); 2005 getInstrumentation().waitForIdleSync(); 2006 assertTrue(view.getLocalVisibleRect(rect)); 2007 assertEquals(20, rect.left); 2008 assertEquals(-30, rect.top); 2009 assertEquals(halfWidth + 20, rect.right); 2010 assertEquals(halfHeight - 30, rect.bottom); 2011 2012 try { 2013 view.getLocalVisibleRect(null); 2014 fail("should throw NullPointerException"); 2015 } catch (NullPointerException e) { 2016 } 2017 } 2018 2019 public void testMergeDrawableStates() { 2020 MockView view = new MockView(mActivity); 2021 2022 int[] states = view.mergeDrawableStatesWrapper(new int[] { 0, 1, 2, 0, 0 }, 2023 new int[] { 3 }); 2024 assertNotNull(states); 2025 assertEquals(5, states.length); 2026 assertEquals(0, states[0]); 2027 assertEquals(1, states[1]); 2028 assertEquals(2, states[2]); 2029 assertEquals(3, states[3]); 2030 assertEquals(0, states[4]); 2031 2032 try { 2033 view.mergeDrawableStatesWrapper(new int[] { 1, 2 }, new int[] { 3 }); 2034 fail("should throw IndexOutOfBoundsException"); 2035 } catch (IndexOutOfBoundsException e) { 2036 } 2037 2038 try { 2039 view.mergeDrawableStatesWrapper(null, new int[] { 0 }); 2040 fail("should throw NullPointerException"); 2041 } catch (NullPointerException e) { 2042 } 2043 2044 try { 2045 view.mergeDrawableStatesWrapper(new int [] { 0 }, null); 2046 fail("should throw NullPointerException"); 2047 } catch (NullPointerException e) { 2048 } 2049 } 2050 2051 public void testOnSaveAndRestoreInstanceState() { 2052 // it is hard to simulate operation to make callback be called. 2053 } 2054 2055 public void testSaveAndRestoreHierarchyState() { 2056 int viewId = R.id.mock_view; 2057 MockView view = (MockView) mActivity.findViewById(viewId); 2058 SparseArray<Parcelable> container = new SparseArray<Parcelable>(); 2059 view.saveHierarchyState(container); 2060 assertTrue(view.hasCalledDispatchSaveInstanceState()); 2061 assertTrue(view.hasCalledOnSaveInstanceState()); 2062 assertEquals(viewId, container.keyAt(0)); 2063 2064 view.reset(); 2065 container.put(R.id.mock_view, BaseSavedState.EMPTY_STATE); 2066 view.restoreHierarchyState(container); 2067 assertTrue(view.hasCalledDispatchRestoreInstanceState()); 2068 assertTrue(view.hasCalledOnRestoreInstanceState()); 2069 container.clear(); 2070 view.saveHierarchyState(container); 2071 assertTrue(view.hasCalledDispatchSaveInstanceState()); 2072 assertTrue(view.hasCalledOnSaveInstanceState()); 2073 assertEquals(viewId, container.keyAt(0)); 2074 2075 container.clear(); 2076 container.put(viewId, new BaseSavedState(BaseSavedState.EMPTY_STATE)); 2077 try { 2078 view.restoreHierarchyState(container); 2079 fail("should throw IllegalArgumentException"); 2080 } catch (IllegalArgumentException e) { 2081 // expected 2082 } 2083 2084 try { 2085 view.restoreHierarchyState(null); 2086 fail("should throw NullPointerException"); 2087 } catch (NullPointerException e) { 2088 // expected 2089 } 2090 2091 try { 2092 view.saveHierarchyState(null); 2093 fail("should throw NullPointerException"); 2094 } catch (NullPointerException e) { 2095 // expected 2096 } 2097 } 2098 2099 public void testOnKeyDownOrUp() throws Throwable { 2100 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 2101 runTestOnUiThread(new Runnable() { 2102 public void run() { 2103 view.setFocusable(true); 2104 view.requestFocus(); 2105 } 2106 }); 2107 getInstrumentation().waitForIdleSync(); 2108 assertTrue(view.isFocused()); 2109 2110 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0); 2111 getInstrumentation().sendKeySync(event); 2112 assertTrue(view.hasCalledOnKeyDown()); 2113 2114 event = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_0); 2115 getInstrumentation().sendKeySync(event); 2116 assertTrue(view.hasCalledOnKeyUp()); 2117 2118 view.reset(); 2119 assertTrue(view.isEnabled()); 2120 assertFalse(view.isClickable()); 2121 assertFalse(view.isPressed()); 2122 event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER); 2123 getInstrumentation().sendKeySync(event); 2124 assertFalse(view.isPressed()); 2125 assertTrue(view.hasCalledOnKeyDown()); 2126 2127 runTestOnUiThread(new Runnable() { 2128 public void run() { 2129 view.setEnabled(true); 2130 view.setClickable(true); 2131 } 2132 }); 2133 view.reset(); 2134 OnClickListenerImpl listener = new OnClickListenerImpl(); 2135 view.setOnClickListener(listener); 2136 2137 assertFalse(view.isPressed()); 2138 event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER); 2139 getInstrumentation().sendKeySync(event); 2140 assertTrue(view.isPressed()); 2141 assertTrue(view.hasCalledOnKeyDown()); 2142 event = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_ENTER); 2143 getInstrumentation().sendKeySync(event); 2144 assertFalse(view.isPressed()); 2145 assertTrue(view.hasCalledOnKeyUp()); 2146 assertTrue(listener.hasOnClick()); 2147 2148 view.setPressed(false); 2149 listener.reset(); 2150 view.reset(); 2151 2152 assertFalse(view.isPressed()); 2153 event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER); 2154 getInstrumentation().sendKeySync(event); 2155 assertTrue(view.isPressed()); 2156 assertTrue(view.hasCalledOnKeyDown()); 2157 event = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER); 2158 getInstrumentation().sendKeySync(event); 2159 assertFalse(view.isPressed()); 2160 assertTrue(view.hasCalledOnKeyUp()); 2161 assertTrue(listener.hasOnClick()); 2162 } 2163 2164 @UiThreadTest 2165 public void testDispatchKeyEvent() { 2166 MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 2167 MockView mockView1 = new MockView(mActivity); 2168 MockView mockView2 = new MockView(mActivity); 2169 ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root); 2170 viewGroup.addView(mockView1); 2171 viewGroup.addView(mockView2); 2172 view.setFocusable(true); 2173 mockView1.setFocusable(true); 2174 mockView2.setFocusable(true); 2175 2176 assertFalse(view.hasCalledOnKeyDown()); 2177 assertFalse(mockView1.hasCalledOnKeyDown()); 2178 assertFalse(mockView2.hasCalledOnKeyDown()); 2179 KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0); 2180 assertFalse(view.dispatchKeyEvent(event)); 2181 assertTrue(view.hasCalledOnKeyDown()); 2182 assertFalse(mockView1.hasCalledOnKeyDown()); 2183 assertFalse(mockView2.hasCalledOnKeyDown()); 2184 2185 view.reset(); 2186 mockView1.reset(); 2187 mockView2.reset(); 2188 assertFalse(view.hasCalledOnKeyDown()); 2189 assertFalse(mockView1.hasCalledOnKeyDown()); 2190 assertFalse(mockView2.hasCalledOnKeyDown()); 2191 event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0); 2192 assertFalse(mockView1.dispatchKeyEvent(event)); 2193 assertFalse(view.hasCalledOnKeyDown()); 2194 // issue 1695243 2195 // When the view has NOT focus, it dispatches to itself, which disobey the javadoc. 2196 assertTrue(mockView1.hasCalledOnKeyDown()); 2197 assertFalse(mockView2.hasCalledOnKeyDown()); 2198 2199 assertFalse(view.hasCalledOnKeyUp()); 2200 event = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_0); 2201 assertFalse(view.dispatchKeyEvent(event)); 2202 assertTrue(view.hasCalledOnKeyUp()); 2203 2204 assertFalse(view.hasCalledOnKeyMultiple()); 2205 event = new KeyEvent(1, 2, KeyEvent.ACTION_MULTIPLE, KeyEvent.KEYCODE_0, 2); 2206 assertFalse(view.dispatchKeyEvent(event)); 2207 assertTrue(view.hasCalledOnKeyMultiple()); 2208 2209 try { 2210 view.dispatchKeyEvent(null); 2211 fail("should throw NullPointerException"); 2212 } catch (NullPointerException e) { 2213 // expected 2214 } 2215 2216 view.reset(); 2217 event = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_0); 2218 OnKeyListenerImpl listener = new OnKeyListenerImpl(); 2219 view.setOnKeyListener(listener); 2220 assertFalse(listener.hasOnKey()); 2221 assertTrue(view.dispatchKeyEvent(event)); 2222 assertTrue(listener.hasOnKey()); 2223 assertFalse(view.hasCalledOnKeyUp()); 2224 } 2225 2226 @UiThreadTest 2227 public void testDispatchTouchEvent() { 2228 ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root); 2229 MockView mockView1 = new MockView(mActivity); 2230 MockView mockView2 = new MockView(mActivity); 2231 viewGroup.addView(mockView1); 2232 viewGroup.addView(mockView2); 2233 2234 int[] xy = new int[2]; 2235 mockView1.getLocationOnScreen(xy); 2236 2237 final int viewWidth = mockView1.getWidth(); 2238 final int viewHeight = mockView1.getHeight(); 2239 final float x = xy[0] + viewWidth / 2.0f; 2240 final float y = xy[1] + viewHeight / 2.0f; 2241 2242 long downTime = SystemClock.uptimeMillis(); 2243 long eventTime = SystemClock.uptimeMillis(); 2244 MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, 2245 x, y, 0); 2246 2247 assertFalse(mockView1.hasCalledOnTouchEvent()); 2248 assertFalse(mockView1.dispatchTouchEvent(event)); 2249 assertTrue(mockView1.hasCalledOnTouchEvent()); 2250 2251 assertFalse(mockView2.hasCalledOnTouchEvent()); 2252 assertFalse(mockView2.dispatchTouchEvent(event)); 2253 // issue 1695243 2254 // it passes the touch screen motion event down to itself even if it is not the target view. 2255 assertTrue(mockView2.hasCalledOnTouchEvent()); 2256 2257 mockView1.reset(); 2258 OnTouchListenerImpl listener = new OnTouchListenerImpl(); 2259 mockView1.setOnTouchListener(listener); 2260 assertFalse(listener.hasOnTouch()); 2261 assertTrue(mockView1.dispatchTouchEvent(event)); 2262 assertTrue(listener.hasOnTouch()); 2263 assertFalse(mockView1.hasCalledOnTouchEvent()); 2264 } 2265 2266 public void testInvalidate1() throws Throwable { 2267 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 2268 assertTrue(view.hasCalledOnDraw()); 2269 2270 view.reset(); 2271 runTestOnUiThread(new Runnable() { 2272 public void run() { 2273 view.invalidate(); 2274 } 2275 }); 2276 getInstrumentation().waitForIdleSync(); 2277 new PollingCheck() { 2278 @Override 2279 protected boolean check() { 2280 return view.hasCalledOnDraw(); 2281 } 2282 }.run(); 2283 2284 view.reset(); 2285 runTestOnUiThread(new Runnable() { 2286 public void run() { 2287 view.setVisibility(View.INVISIBLE); 2288 view.invalidate(); 2289 } 2290 }); 2291 getInstrumentation().waitForIdleSync(); 2292 assertFalse(view.hasCalledOnDraw()); 2293 } 2294 2295 public void testInvalidate2() throws Throwable { 2296 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 2297 assertTrue(view.hasCalledOnDraw()); 2298 2299 try { 2300 view.invalidate(null); 2301 fail("should throw NullPointerException"); 2302 } catch (NullPointerException e) { 2303 } 2304 2305 view.reset(); 2306 final Rect dirty = new Rect(view.getLeft() + 1, view.getTop() + 1, 2307 view.getLeft() + view.getWidth() / 2, view.getTop() + view.getHeight() / 2); 2308 runTestOnUiThread(new Runnable() { 2309 public void run() { 2310 view.invalidate(dirty); 2311 } 2312 }); 2313 getInstrumentation().waitForIdleSync(); 2314 new PollingCheck() { 2315 @Override 2316 protected boolean check() { 2317 return view.hasCalledOnDraw(); 2318 } 2319 }.run(); 2320 2321 view.reset(); 2322 runTestOnUiThread(new Runnable() { 2323 public void run() { 2324 view.setVisibility(View.INVISIBLE); 2325 view.invalidate(dirty); 2326 } 2327 }); 2328 getInstrumentation().waitForIdleSync(); 2329 assertFalse(view.hasCalledOnDraw()); 2330 } 2331 2332 public void testInvalidate3() throws Throwable { 2333 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 2334 assertTrue(view.hasCalledOnDraw()); 2335 2336 view.reset(); 2337 final Rect dirty = new Rect(view.getLeft() + 1, view.getTop() + 1, 2338 view.getLeft() + view.getWidth() / 2, view.getTop() + view.getHeight() / 2); 2339 runTestOnUiThread(new Runnable() { 2340 public void run() { 2341 view.invalidate(dirty.left, dirty.top, dirty.right, dirty.bottom); 2342 } 2343 }); 2344 getInstrumentation().waitForIdleSync(); 2345 new PollingCheck() { 2346 @Override 2347 protected boolean check() { 2348 return view.hasCalledOnDraw(); 2349 } 2350 }.run(); 2351 2352 view.reset(); 2353 runTestOnUiThread(new Runnable() { 2354 public void run() { 2355 view.setVisibility(View.INVISIBLE); 2356 view.invalidate(dirty.left, dirty.top, dirty.right, dirty.bottom); 2357 } 2358 }); 2359 getInstrumentation().waitForIdleSync(); 2360 assertFalse(view.hasCalledOnDraw()); 2361 } 2362 2363 public void testInvalidateDrawable() throws Throwable { 2364 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 2365 final Drawable d1 = mResources.getDrawable(R.drawable.scenery); 2366 final Drawable d2 = mResources.getDrawable(R.drawable.pass); 2367 2368 view.reset(); 2369 runTestOnUiThread(new Runnable() { 2370 public void run() { 2371 view.setBackgroundDrawable(d1); 2372 view.invalidateDrawable(d1); 2373 } 2374 }); 2375 getInstrumentation().waitForIdleSync(); 2376 new PollingCheck() { 2377 @Override 2378 protected boolean check() { 2379 return view.hasCalledOnDraw(); 2380 } 2381 }.run(); 2382 2383 view.reset(); 2384 runTestOnUiThread(new Runnable() { 2385 public void run() { 2386 view.invalidateDrawable(d2); 2387 } 2388 }); 2389 getInstrumentation().waitForIdleSync(); 2390 assertFalse(view.hasCalledOnDraw()); 2391 2392 MockView viewTestNull = new MockView(mActivity); 2393 try { 2394 viewTestNull.invalidateDrawable(null); 2395 fail("should throw NullPointerException"); 2396 } catch (NullPointerException e) { 2397 } 2398 } 2399 2400 @UiThreadTest 2401 public void testOnFocusChanged() { 2402 MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 2403 2404 mActivity.findViewById(R.id.fit_windows).setFocusable(true); 2405 view.setFocusable(true); 2406 assertFalse(view.hasCalledOnFocusChanged()); 2407 2408 view.requestFocus(); 2409 assertTrue(view.hasCalledOnFocusChanged()); 2410 2411 view.reset(); 2412 view.clearFocus(); 2413 assertTrue(view.hasCalledOnFocusChanged()); 2414 } 2415 2416 public void testDrawableState() { 2417 MockView view = new MockView(mActivity); 2418 view.setParent(mMockParent); 2419 2420 assertFalse(view.hasCalledOnCreateDrawableState()); 2421 assertTrue(Arrays.equals(MockView.getEnabledStateSet(), view.getDrawableState())); 2422 assertTrue(view.hasCalledOnCreateDrawableState()); 2423 2424 view.reset(); 2425 assertFalse(view.hasCalledOnCreateDrawableState()); 2426 assertTrue(Arrays.equals(MockView.getEnabledStateSet(), view.getDrawableState())); 2427 assertFalse(view.hasCalledOnCreateDrawableState()); 2428 2429 view.reset(); 2430 assertFalse(view.hasCalledDrawableStateChanged()); 2431 view.setPressed(true); 2432 assertTrue(view.hasCalledDrawableStateChanged()); 2433 assertFalse(view.hasCalledOnCreateDrawableState()); 2434 assertTrue(Arrays.equals(MockView.getPressedEnabledStateSet(), view.getDrawableState())); 2435 assertTrue(view.hasCalledOnCreateDrawableState()); 2436 2437 view.reset(); 2438 mMockParent.reset(); 2439 assertFalse(view.hasCalledDrawableStateChanged()); 2440 assertFalse(mMockParent.hasChildDrawableStateChanged()); 2441 view.refreshDrawableState(); 2442 assertTrue(view.hasCalledDrawableStateChanged()); 2443 assertTrue(mMockParent.hasChildDrawableStateChanged()); 2444 assertFalse(view.hasCalledOnCreateDrawableState()); 2445 assertTrue(Arrays.equals(MockView.getPressedEnabledStateSet(), view.getDrawableState())); 2446 assertTrue(view.hasCalledOnCreateDrawableState()); 2447 } 2448 2449 public void testWindowFocusChanged() { 2450 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 2451 2452 // Wait until the window has been focused. 2453 new PollingCheck(TIMEOUT_DELTA) { 2454 @Override 2455 protected boolean check() { 2456 return view.hasWindowFocus(); 2457 } 2458 }.run(); 2459 2460 new PollingCheck() { 2461 protected boolean check() { 2462 return view.hasCalledOnWindowFocusChanged(); 2463 } 2464 }.run(); 2465 2466 assertTrue(view.hasCalledOnWindowFocusChanged()); 2467 assertTrue(view.hasCalledDispatchWindowFocusChanged()); 2468 2469 view.reset(); 2470 assertFalse(view.hasCalledOnWindowFocusChanged()); 2471 assertFalse(view.hasCalledDispatchWindowFocusChanged()); 2472 2473 StubActivity activity = launchActivity("com.android.cts.stub", StubActivity.class, null); 2474 2475 // Wait until the window lost focus. 2476 new PollingCheck(TIMEOUT_DELTA) { 2477 @Override 2478 protected boolean check() { 2479 return !view.hasWindowFocus(); 2480 } 2481 }.run(); 2482 2483 assertTrue(view.hasCalledOnWindowFocusChanged()); 2484 assertTrue(view.hasCalledDispatchWindowFocusChanged()); 2485 2486 activity.finish(); 2487 } 2488 2489 public void testDraw() throws Throwable { 2490 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 2491 runTestOnUiThread(new Runnable() { 2492 public void run() { 2493 view.requestLayout(); 2494 } 2495 }); 2496 getInstrumentation().waitForIdleSync(); 2497 2498 assertTrue(view.hasCalledOnDraw()); 2499 assertTrue(view.hasCalledDispatchDraw()); 2500 } 2501 2502 public void testRequestFocusFromTouch() { 2503 View view = new View(mActivity); 2504 view.setFocusable(true); 2505 assertFalse(view.isFocused()); 2506 2507 view.requestFocusFromTouch(); 2508 assertTrue(view.isFocused()); 2509 2510 view.requestFocusFromTouch(); 2511 assertTrue(view.isFocused()); 2512 } 2513 2514 public void testRequestRectangleOnScreen1() { 2515 MockView view = new MockView(mActivity); 2516 Rect rectangle = new Rect(10, 10, 20, 30); 2517 MockViewGroupParent parent = new MockViewGroupParent(mActivity); 2518 2519 // parent is null 2520 assertFalse(view.requestRectangleOnScreen(rectangle, true)); 2521 assertFalse(view.requestRectangleOnScreen(rectangle, false)); 2522 assertFalse(view.requestRectangleOnScreen(null, true)); 2523 2524 view.setParent(parent); 2525 view.scrollTo(1, 2); 2526 assertFalse(parent.hasRequestChildRectangleOnScreen()); 2527 2528 assertFalse(view.requestRectangleOnScreen(rectangle, true)); 2529 assertTrue(parent.hasRequestChildRectangleOnScreen()); 2530 2531 parent.reset(); 2532 view.scrollTo(11, 22); 2533 assertFalse(parent.hasRequestChildRectangleOnScreen()); 2534 2535 assertFalse(view.requestRectangleOnScreen(rectangle, true)); 2536 assertTrue(parent.hasRequestChildRectangleOnScreen()); 2537 2538 try { 2539 view.requestRectangleOnScreen(null, true); 2540 fail("should throw NullPointerException"); 2541 } catch (NullPointerException e) { 2542 } 2543 } 2544 2545 public void testRequestRectangleOnScreen2() { 2546 MockView view = new MockView(mActivity); 2547 Rect rectangle = new Rect(); 2548 MockViewGroupParent parent = new MockViewGroupParent(mActivity); 2549 2550 final Rect requestedRect = new Rect(); 2551 MockViewGroupParent grandparent = new MockViewGroupParent(mActivity) { 2552 @Override 2553 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, 2554 boolean immediate) { 2555 requestedRect.set(rectangle); 2556 return super.requestChildRectangleOnScreen(child, rectangle, immediate); 2557 } 2558 }; 2559 2560 // parent is null 2561 assertFalse(view.requestRectangleOnScreen(rectangle)); 2562 assertFalse(view.requestRectangleOnScreen(null)); 2563 assertEquals(0, rectangle.left); 2564 assertEquals(0, rectangle.top); 2565 assertEquals(0, rectangle.right); 2566 assertEquals(0, rectangle.bottom); 2567 2568 parent.addView(view); 2569 parent.scrollTo(1, 2); 2570 grandparent.addView(parent); 2571 2572 assertFalse(parent.hasRequestChildRectangleOnScreen()); 2573 assertFalse(grandparent.hasRequestChildRectangleOnScreen()); 2574 2575 assertFalse(view.requestRectangleOnScreen(rectangle)); 2576 2577 assertTrue(parent.hasRequestChildRectangleOnScreen()); 2578 assertTrue(grandparent.hasRequestChildRectangleOnScreen()); 2579 2580 assertEquals(-1, requestedRect.left); 2581 assertEquals(-2, requestedRect.top); 2582 assertEquals(-1, requestedRect.right); 2583 assertEquals(-2, requestedRect.bottom); 2584 2585 try { 2586 view.requestRectangleOnScreen(null); 2587 fail("should throw NullPointerException"); 2588 } catch (NullPointerException e) { 2589 } 2590 } 2591 2592 /** 2593 * For the duration of the tap timeout we are in a 'prepressed' state 2594 * to differentiate between taps and touch scrolls. 2595 * Wait at least this long before testing if the view is pressed 2596 * by calling this function. 2597 */ 2598 private void waitPrepressedTimeout() { 2599 try { 2600 Thread.sleep(ViewConfiguration.getTapTimeout() + 10); 2601 } catch (InterruptedException e) { 2602 Log.e(LOG_TAG, "waitPrepressedTimeout() interrupted! Test may fail!", e); 2603 } 2604 getInstrumentation().waitForIdleSync(); 2605 } 2606 2607 public void testOnTouchEvent() throws Throwable { 2608 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 2609 2610 assertTrue(view.isEnabled()); 2611 assertFalse(view.isClickable()); 2612 assertFalse(view.isLongClickable()); 2613 2614 TouchUtils.clickView(this, view); 2615 assertTrue(view.hasCalledOnTouchEvent()); 2616 2617 runTestOnUiThread(new Runnable() { 2618 public void run() { 2619 view.setEnabled(true); 2620 view.setClickable(true); 2621 view.setLongClickable(true); 2622 } 2623 }); 2624 getInstrumentation().waitForIdleSync(); 2625 assertTrue(view.isEnabled()); 2626 assertTrue(view.isClickable()); 2627 assertTrue(view.isLongClickable()); 2628 2629 // MotionEvent.ACTION_DOWN 2630 int[] xy = new int[2]; 2631 view.getLocationOnScreen(xy); 2632 2633 final int viewWidth = view.getWidth(); 2634 final int viewHeight = view.getHeight(); 2635 float x = xy[0] + viewWidth / 2.0f; 2636 float y = xy[1] + viewHeight / 2.0f; 2637 2638 long downTime = SystemClock.uptimeMillis(); 2639 long eventTime = SystemClock.uptimeMillis(); 2640 MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, 2641 x, y, 0); 2642 assertFalse(view.isPressed()); 2643 getInstrumentation().sendPointerSync(event); 2644 waitPrepressedTimeout(); 2645 assertTrue(view.hasCalledOnTouchEvent()); 2646 assertTrue(view.isPressed()); 2647 2648 // MotionEvent.ACTION_MOVE 2649 // move out of the bound. 2650 view.reset(); 2651 downTime = SystemClock.uptimeMillis(); 2652 eventTime = SystemClock.uptimeMillis(); 2653 int slop = ViewConfiguration.get(mActivity).getScaledTouchSlop(); 2654 x = xy[0] + viewWidth + slop; 2655 y = xy[1] + viewHeight + slop; 2656 event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, x, y, 0); 2657 getInstrumentation().sendPointerSync(event); 2658 assertTrue(view.hasCalledOnTouchEvent()); 2659 assertFalse(view.isPressed()); 2660 2661 // move into view 2662 view.reset(); 2663 downTime = SystemClock.uptimeMillis(); 2664 eventTime = SystemClock.uptimeMillis(); 2665 x = xy[0] + viewWidth - 1; 2666 y = xy[1] + viewHeight - 1; 2667 event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, x, y, 0); 2668 getInstrumentation().sendPointerSync(event); 2669 waitPrepressedTimeout(); 2670 assertTrue(view.hasCalledOnTouchEvent()); 2671 assertFalse(view.isPressed()); 2672 2673 // MotionEvent.ACTION_UP 2674 OnClickListenerImpl listener = new OnClickListenerImpl(); 2675 view.setOnClickListener(listener); 2676 view.reset(); 2677 downTime = SystemClock.uptimeMillis(); 2678 eventTime = SystemClock.uptimeMillis(); 2679 event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x, y, 0); 2680 getInstrumentation().sendPointerSync(event); 2681 assertTrue(view.hasCalledOnTouchEvent()); 2682 assertFalse(listener.hasOnClick()); 2683 2684 view.reset(); 2685 x = xy[0] + viewWidth / 2.0f; 2686 y = xy[1] + viewHeight / 2.0f; 2687 downTime = SystemClock.uptimeMillis(); 2688 eventTime = SystemClock.uptimeMillis(); 2689 event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, x, y, 0); 2690 getInstrumentation().sendPointerSync(event); 2691 assertTrue(view.hasCalledOnTouchEvent()); 2692 2693 // MotionEvent.ACTION_CANCEL 2694 view.reset(); 2695 listener.reset(); 2696 downTime = SystemClock.uptimeMillis(); 2697 eventTime = SystemClock.uptimeMillis(); 2698 event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_CANCEL, x, y, 0); 2699 getInstrumentation().sendPointerSync(event); 2700 assertTrue(view.hasCalledOnTouchEvent()); 2701 assertFalse(view.isPressed()); 2702 assertFalse(listener.hasOnClick()); 2703 } 2704 2705 public void testBringToFront() { 2706 MockView view = new MockView(mActivity); 2707 view.setParent(mMockParent); 2708 2709 assertFalse(mMockParent.hasBroughtChildToFront()); 2710 view.bringToFront(); 2711 assertTrue(mMockParent.hasBroughtChildToFront()); 2712 } 2713 2714 public void testGetApplicationWindowToken() { 2715 View view = new View(mActivity); 2716 // mAttachInfo is null 2717 assertNull(view.getApplicationWindowToken()); 2718 2719 // mAttachInfo is not null 2720 view = mActivity.findViewById(R.id.fit_windows); 2721 assertNotNull(view.getApplicationWindowToken()); 2722 } 2723 2724 public void testGetBottomPaddingOffset() { 2725 MockView view = new MockView(mActivity); 2726 assertEquals(0, view.getBottomPaddingOffset()); 2727 } 2728 2729 public void testGetLeftPaddingOffset() { 2730 MockView view = new MockView(mActivity); 2731 assertEquals(0, view.getLeftPaddingOffset()); 2732 } 2733 2734 public void testGetRightPaddingOffset() { 2735 MockView view = new MockView(mActivity); 2736 assertEquals(0, view.getRightPaddingOffset()); 2737 } 2738 2739 public void testGetTopPaddingOffset() { 2740 MockView view = new MockView(mActivity); 2741 assertEquals(0, view.getTopPaddingOffset()); 2742 } 2743 2744 public void testIsPaddingOffsetRequired() { 2745 MockView view = new MockView(mActivity); 2746 assertFalse(view.isPaddingOffsetRequired()); 2747 } 2748 2749 @UiThreadTest 2750 public void testPadding() { 2751 MockView view = (MockView) mActivity.findViewById(R.id.mock_view_padding_full); 2752 Drawable background = view.getBackground(); 2753 Rect backgroundPadding = new Rect(); 2754 background.getPadding(backgroundPadding); 2755 2756 // There is some background with a non null padding 2757 assertNotNull(background); 2758 assertTrue(backgroundPadding.left != 0); 2759 assertTrue(backgroundPadding.right != 0); 2760 assertTrue(backgroundPadding.top != 0); 2761 assertTrue(backgroundPadding.bottom != 0); 2762 2763 // The XML defines android:padding="0dp" and that should be the resulting padding 2764 assertEquals(0, view.getPaddingLeft()); 2765 assertEquals(0, view.getPaddingTop()); 2766 assertEquals(0, view.getPaddingRight()); 2767 assertEquals(0, view.getPaddingBottom()); 2768 2769 // LEFT case 2770 view = (MockView) mActivity.findViewById(R.id.mock_view_padding_left); 2771 background = view.getBackground(); 2772 backgroundPadding = new Rect(); 2773 background.getPadding(backgroundPadding); 2774 2775 // There is some background with a non null padding 2776 assertNotNull(background); 2777 assertTrue(backgroundPadding.left != 0); 2778 assertTrue(backgroundPadding.right != 0); 2779 assertTrue(backgroundPadding.top != 0); 2780 assertTrue(backgroundPadding.bottom != 0); 2781 2782 // The XML defines android:paddingLeft="0dp" and that should be the resulting padding 2783 assertEquals(0, view.getPaddingLeft()); 2784 assertEquals(backgroundPadding.top, view.getPaddingTop()); 2785 assertEquals(backgroundPadding.right, view.getPaddingRight()); 2786 assertEquals(backgroundPadding.bottom, view.getPaddingBottom()); 2787 2788 // RIGHT case 2789 view = (MockView) mActivity.findViewById(R.id.mock_view_padding_right); 2790 background = view.getBackground(); 2791 backgroundPadding = new Rect(); 2792 background.getPadding(backgroundPadding); 2793 2794 // There is some background with a non null padding 2795 assertNotNull(background); 2796 assertTrue(backgroundPadding.left != 0); 2797 assertTrue(backgroundPadding.right != 0); 2798 assertTrue(backgroundPadding.top != 0); 2799 assertTrue(backgroundPadding.bottom != 0); 2800 2801 // The XML defines android:paddingRight="0dp" and that should be the resulting padding 2802 assertEquals(backgroundPadding.left, view.getPaddingLeft()); 2803 assertEquals(backgroundPadding.top, view.getPaddingTop()); 2804 assertEquals(0, view.getPaddingRight()); 2805 assertEquals(backgroundPadding.bottom, view.getPaddingBottom()); 2806 2807 // TOP case 2808 view = (MockView) mActivity.findViewById(R.id.mock_view_padding_top); 2809 background = view.getBackground(); 2810 backgroundPadding = new Rect(); 2811 background.getPadding(backgroundPadding); 2812 2813 // There is some background with a non null padding 2814 assertNotNull(background); 2815 assertTrue(backgroundPadding.left != 0); 2816 assertTrue(backgroundPadding.right != 0); 2817 assertTrue(backgroundPadding.top != 0); 2818 assertTrue(backgroundPadding.bottom != 0); 2819 2820 // The XML defines android:paddingTop="0dp" and that should be the resulting padding 2821 assertEquals(backgroundPadding.left, view.getPaddingLeft()); 2822 assertEquals(0, view.getPaddingTop()); 2823 assertEquals(backgroundPadding.right, view.getPaddingRight()); 2824 assertEquals(backgroundPadding.bottom, view.getPaddingBottom()); 2825 2826 // BOTTOM case 2827 view = (MockView) mActivity.findViewById(R.id.mock_view_padding_bottom); 2828 background = view.getBackground(); 2829 backgroundPadding = new Rect(); 2830 background.getPadding(backgroundPadding); 2831 2832 // There is some background with a non null padding 2833 assertNotNull(background); 2834 assertTrue(backgroundPadding.left != 0); 2835 assertTrue(backgroundPadding.right != 0); 2836 assertTrue(backgroundPadding.top != 0); 2837 assertTrue(backgroundPadding.bottom != 0); 2838 2839 // The XML defines android:paddingBottom="0dp" and that should be the resulting padding 2840 assertEquals(backgroundPadding.left, view.getPaddingLeft()); 2841 assertEquals(backgroundPadding.top, view.getPaddingTop()); 2842 assertEquals(backgroundPadding.right, view.getPaddingRight()); 2843 assertEquals(0, view.getPaddingBottom()); 2844 2845 // Case for interleaved background/padding changes 2846 view = (MockView) mActivity.findViewById(R.id.mock_view_padding_runtime_updated); 2847 background = view.getBackground(); 2848 backgroundPadding = new Rect(); 2849 background.getPadding(backgroundPadding); 2850 2851 // There is some background with a null padding 2852 assertNotNull(background); 2853 assertTrue(backgroundPadding.left == 0); 2854 assertTrue(backgroundPadding.right == 0); 2855 assertTrue(backgroundPadding.top == 0); 2856 assertTrue(backgroundPadding.bottom == 0); 2857 2858 final int paddingLeft = view.getPaddingLeft(); 2859 final int paddingRight = view.getPaddingRight(); 2860 final int paddingTop = view.getPaddingTop(); 2861 final int paddingBottom = view.getPaddingBottom(); 2862 assertEquals(8, paddingLeft); 2863 assertEquals(0, paddingTop); 2864 assertEquals(8, paddingRight); 2865 assertEquals(0, paddingBottom); 2866 2867 // Manipulate background and padding 2868 background.setState(view.getDrawableState()); 2869 background.jumpToCurrentState(); 2870 view.setBackground(background); 2871 view.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom); 2872 2873 assertEquals(8, view.getPaddingLeft()); 2874 assertEquals(0, view.getPaddingTop()); 2875 assertEquals(8, view.getPaddingRight()); 2876 assertEquals(0, view.getPaddingBottom()); 2877 } 2878 2879 public void testGetWindowVisibleDisplayFrame() { 2880 Rect outRect = new Rect(); 2881 View view = new View(mActivity); 2882 // mAttachInfo is null 2883 WindowManager wm = (WindowManager)mActivity.getSystemService(Context.WINDOW_SERVICE); 2884 Display d = wm.getDefaultDisplay(); 2885 view.getWindowVisibleDisplayFrame(outRect); 2886 assertEquals(0, outRect.left); 2887 assertEquals(0, outRect.top); 2888 assertEquals(d.getWidth(), outRect.right); 2889 assertEquals(d.getHeight(), outRect.bottom); 2890 2891 // mAttachInfo is not null 2892 outRect = new Rect(); 2893 view = mActivity.findViewById(R.id.fit_windows); 2894 // it's implementation detail 2895 view.getWindowVisibleDisplayFrame(outRect); 2896 } 2897 2898 public void testSetScrollContainer() throws Throwable { 2899 final MockView mockView = (MockView) mActivity.findViewById(R.id.mock_view); 2900 final MockView scrollView = (MockView) mActivity.findViewById(R.id.scroll_view); 2901 Bitmap bitmap = Bitmap.createBitmap(200, 300, Bitmap.Config.RGB_565); 2902 final BitmapDrawable d = new BitmapDrawable(bitmap); 2903 final InputMethodManager imm = (InputMethodManager)getActivity().getSystemService( 2904 Context.INPUT_METHOD_SERVICE); 2905 final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(300, 500); 2906 runTestOnUiThread(new Runnable() { 2907 public void run() { 2908 mockView.setBackgroundDrawable(d); 2909 mockView.setHorizontalFadingEdgeEnabled(true); 2910 mockView.setVerticalFadingEdgeEnabled(true); 2911 mockView.setLayoutParams(layoutParams); 2912 scrollView.setLayoutParams(layoutParams); 2913 2914 mockView.setFocusable(true); 2915 mockView.requestFocus(); 2916 mockView.setScrollContainer(true); 2917 scrollView.setScrollContainer(false); 2918 imm.showSoftInput(mockView, 0); 2919 } 2920 }); 2921 getInstrumentation().waitForIdleSync(); 2922 2923 // FIXME: why the size of view doesn't change? 2924 2925 runTestOnUiThread(new Runnable() { 2926 public void run() { 2927 imm.hideSoftInputFromInputMethod(mockView.getWindowToken(), 0); 2928 } 2929 }); 2930 getInstrumentation().waitForIdleSync(); 2931 } 2932 2933 public void testTouchMode() throws Throwable { 2934 final MockView mockView = (MockView) mActivity.findViewById(R.id.mock_view); 2935 final View fitWindowsView = mActivity.findViewById(R.id.fit_windows); 2936 runTestOnUiThread(new Runnable() { 2937 public void run() { 2938 mockView.setFocusableInTouchMode(true); 2939 fitWindowsView.setFocusable(true); 2940 fitWindowsView.requestFocus(); 2941 } 2942 }); 2943 getInstrumentation().waitForIdleSync(); 2944 assertTrue(mockView.isFocusableInTouchMode()); 2945 assertFalse(fitWindowsView.isFocusableInTouchMode()); 2946 assertTrue(mockView.isFocusable()); 2947 assertTrue(fitWindowsView.isFocusable()); 2948 assertFalse(mockView.isFocused()); 2949 assertTrue(fitWindowsView.isFocused()); 2950 assertFalse(mockView.isInTouchMode()); 2951 assertFalse(fitWindowsView.isInTouchMode()); 2952 2953 TouchUtils.tapView(this, mockView); 2954 assertFalse(fitWindowsView.isFocused()); 2955 assertFalse(mockView.isFocused()); 2956 runTestOnUiThread(new Runnable() { 2957 public void run() { 2958 mockView.requestFocus(); 2959 } 2960 }); 2961 getInstrumentation().waitForIdleSync(); 2962 assertTrue(mockView.isFocused()); 2963 runTestOnUiThread(new Runnable() { 2964 public void run() { 2965 fitWindowsView.requestFocus(); 2966 } 2967 }); 2968 getInstrumentation().waitForIdleSync(); 2969 assertFalse(fitWindowsView.isFocused()); 2970 assertTrue(mockView.isInTouchMode()); 2971 assertTrue(fitWindowsView.isInTouchMode()); 2972 2973 KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0); 2974 getInstrumentation().sendKeySync(keyEvent); 2975 assertTrue(mockView.isFocused()); 2976 assertFalse(fitWindowsView.isFocused()); 2977 runTestOnUiThread(new Runnable() { 2978 public void run() { 2979 fitWindowsView.requestFocus(); 2980 } 2981 }); 2982 getInstrumentation().waitForIdleSync(); 2983 assertFalse(mockView.isFocused()); 2984 assertTrue(fitWindowsView.isFocused()); 2985 assertFalse(mockView.isInTouchMode()); 2986 assertFalse(fitWindowsView.isInTouchMode()); 2987 } 2988 2989 @UiThreadTest 2990 public void testScrollbarStyle() { 2991 MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 2992 Bitmap bitmap = Bitmap.createBitmap(200, 300, Bitmap.Config.RGB_565); 2993 BitmapDrawable d = new BitmapDrawable(bitmap); 2994 view.setBackgroundDrawable(d); 2995 view.setHorizontalFadingEdgeEnabled(true); 2996 view.setVerticalFadingEdgeEnabled(true); 2997 2998 view.setHorizontalScrollBarEnabled(true); 2999 view.setVerticalScrollBarEnabled(true); 3000 view.initializeScrollbars(mActivity.obtainStyledAttributes(android.R.styleable.View)); 3001 assertTrue(view.isHorizontalScrollBarEnabled()); 3002 assertTrue(view.isVerticalScrollBarEnabled()); 3003 int verticalScrollBarWidth = view.getVerticalScrollbarWidth(); 3004 int horizontalScrollBarHeight = view.getHorizontalScrollbarHeight(); 3005 assertTrue(verticalScrollBarWidth > 0); 3006 assertTrue(horizontalScrollBarHeight > 0); 3007 assertEquals(0, view.getPaddingRight()); 3008 assertEquals(0, view.getPaddingBottom()); 3009 3010 view.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET); 3011 assertEquals(View.SCROLLBARS_INSIDE_INSET, view.getScrollBarStyle()); 3012 assertEquals(verticalScrollBarWidth, view.getPaddingRight()); 3013 assertEquals(horizontalScrollBarHeight, view.getPaddingBottom()); 3014 3015 view.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY); 3016 assertEquals(View.SCROLLBARS_OUTSIDE_OVERLAY, view.getScrollBarStyle()); 3017 assertEquals(0, view.getPaddingRight()); 3018 assertEquals(0, view.getPaddingBottom()); 3019 3020 view.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_INSET); 3021 assertEquals(View.SCROLLBARS_OUTSIDE_INSET, view.getScrollBarStyle()); 3022 assertEquals(verticalScrollBarWidth, view.getPaddingRight()); 3023 assertEquals(horizontalScrollBarHeight, view.getPaddingBottom()); 3024 3025 // TODO: how to get the position of the Scrollbar to assert it is inside or outside. 3026 } 3027 3028 @UiThreadTest 3029 public void testScrollFading() { 3030 MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 3031 Bitmap bitmap = Bitmap.createBitmap(200, 300, Bitmap.Config.RGB_565); 3032 BitmapDrawable d = new BitmapDrawable(bitmap); 3033 view.setBackgroundDrawable(d); 3034 3035 assertFalse(view.isHorizontalFadingEdgeEnabled()); 3036 assertFalse(view.isVerticalFadingEdgeEnabled()); 3037 assertEquals(0, view.getHorizontalFadingEdgeLength()); 3038 assertEquals(0, view.getVerticalFadingEdgeLength()); 3039 3040 view.setHorizontalFadingEdgeEnabled(true); 3041 view.setVerticalFadingEdgeEnabled(true); 3042 assertTrue(view.isHorizontalFadingEdgeEnabled()); 3043 assertTrue(view.isVerticalFadingEdgeEnabled()); 3044 assertTrue(view.getHorizontalFadingEdgeLength() > 0); 3045 assertTrue(view.getVerticalFadingEdgeLength() > 0); 3046 3047 final int fadingLength = 20; 3048 view.setFadingEdgeLength(fadingLength); 3049 assertEquals(fadingLength, view.getHorizontalFadingEdgeLength()); 3050 assertEquals(fadingLength, view.getVerticalFadingEdgeLength()); 3051 } 3052 3053 @UiThreadTest 3054 public void testScrolling() { 3055 MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 3056 view.reset(); 3057 assertEquals(0, view.getScrollX()); 3058 assertEquals(0, view.getScrollY()); 3059 assertFalse(view.hasCalledOnScrollChanged()); 3060 3061 view.scrollTo(0, 0); 3062 assertEquals(0, view.getScrollX()); 3063 assertEquals(0, view.getScrollY()); 3064 assertFalse(view.hasCalledOnScrollChanged()); 3065 3066 view.scrollBy(0, 0); 3067 assertEquals(0, view.getScrollX()); 3068 assertEquals(0, view.getScrollY()); 3069 assertFalse(view.hasCalledOnScrollChanged()); 3070 3071 view.scrollTo(10, 100); 3072 assertEquals(10, view.getScrollX()); 3073 assertEquals(100, view.getScrollY()); 3074 assertTrue(view.hasCalledOnScrollChanged()); 3075 3076 view.reset(); 3077 assertFalse(view.hasCalledOnScrollChanged()); 3078 view.scrollBy(-10, -100); 3079 assertEquals(0, view.getScrollX()); 3080 assertEquals(0, view.getScrollY()); 3081 assertTrue(view.hasCalledOnScrollChanged()); 3082 3083 view.reset(); 3084 assertFalse(view.hasCalledOnScrollChanged()); 3085 view.scrollTo(-1, -2); 3086 assertEquals(-1, view.getScrollX()); 3087 assertEquals(-2, view.getScrollY()); 3088 assertTrue(view.hasCalledOnScrollChanged()); 3089 } 3090 3091 public void testInitializeScrollbarsAndFadingEdge() { 3092 MockView view = (MockView) mActivity.findViewById(R.id.scroll_view); 3093 3094 assertTrue(view.isHorizontalScrollBarEnabled()); 3095 assertTrue(view.isVerticalScrollBarEnabled()); 3096 assertFalse(view.isHorizontalFadingEdgeEnabled()); 3097 assertFalse(view.isVerticalFadingEdgeEnabled()); 3098 3099 view = (MockView) mActivity.findViewById(R.id.scroll_view_2); 3100 final int fadingEdgeLength = 20; 3101 3102 assertTrue(view.isHorizontalScrollBarEnabled()); 3103 assertTrue(view.isVerticalScrollBarEnabled()); 3104 assertTrue(view.isHorizontalFadingEdgeEnabled()); 3105 assertTrue(view.isVerticalFadingEdgeEnabled()); 3106 assertEquals(fadingEdgeLength, view.getHorizontalFadingEdgeLength()); 3107 assertEquals(fadingEdgeLength, view.getVerticalFadingEdgeLength()); 3108 } 3109 3110 public void testOnStartAndFinishTemporaryDetach() throws Throwable { 3111 final MockListView listView = new MockListView(mActivity); 3112 List<String> items = Lists.newArrayList("1", "2", "3"); 3113 final Adapter<String> adapter = new Adapter<String>(mActivity, 0, items); 3114 3115 runTestOnUiThread(new Runnable() { 3116 public void run() { 3117 mActivity.setContentView(listView); 3118 listView.setAdapter(adapter); 3119 } 3120 }); 3121 getInstrumentation().waitForIdleSync(); 3122 final MockView focusChild = (MockView) listView.getChildAt(0); 3123 3124 runTestOnUiThread(new Runnable() { 3125 public void run() { 3126 focusChild.requestFocus(); 3127 } 3128 }); 3129 getInstrumentation().waitForIdleSync(); 3130 assertTrue(listView.getChildAt(0).isFocused()); 3131 3132 runTestOnUiThread(new Runnable() { 3133 public void run() { 3134 listView.detachViewFromParent(focusChild); 3135 } 3136 }); 3137 getInstrumentation().waitForIdleSync(); 3138 assertFalse(listView.hasCalledOnStartTemporaryDetach()); 3139 assertFalse(listView.hasCalledOnFinishTemporaryDetach()); 3140 } 3141 3142 private static class MockListView extends ListView { 3143 private boolean mCalledOnStartTemporaryDetach = false; 3144 private boolean mCalledOnFinishTemporaryDetach = false; 3145 3146 public MockListView(Context context) { 3147 super(context); 3148 } 3149 3150 @Override 3151 protected void detachViewFromParent(View child) { 3152 super.detachViewFromParent(child); 3153 } 3154 3155 @Override 3156 public void onFinishTemporaryDetach() { 3157 super.onFinishTemporaryDetach(); 3158 mCalledOnFinishTemporaryDetach = true; 3159 } 3160 3161 public boolean hasCalledOnFinishTemporaryDetach() { 3162 return mCalledOnFinishTemporaryDetach; 3163 } 3164 3165 @Override 3166 public void onStartTemporaryDetach() { 3167 super.onStartTemporaryDetach(); 3168 mCalledOnStartTemporaryDetach = true; 3169 } 3170 3171 public boolean hasCalledOnStartTemporaryDetach() { 3172 return mCalledOnStartTemporaryDetach; 3173 } 3174 3175 public void reset() { 3176 mCalledOnStartTemporaryDetach = false; 3177 mCalledOnFinishTemporaryDetach = false; 3178 } 3179 } 3180 3181 private static class Adapter<T> extends ArrayAdapter<T> { 3182 ArrayList<MockView> views = new ArrayList<MockView>(); 3183 3184 public Adapter(Context context, int textViewResourceId, List<T> objects) { 3185 super(context, textViewResourceId, objects); 3186 for (int i = 0; i < objects.size(); i++) { 3187 views.add(new MockView(context)); 3188 views.get(i).setFocusable(true); 3189 } 3190 } 3191 3192 @Override 3193 public int getCount() { 3194 return views.size(); 3195 } 3196 3197 @Override 3198 public long getItemId(int position) { 3199 return position; 3200 } 3201 3202 @Override 3203 public View getView(int position, View convertView, ViewGroup parent) { 3204 return views.get(position); 3205 } 3206 } 3207 3208 public void testKeyPreIme() throws Throwable { 3209 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 3210 3211 runTestOnUiThread(new Runnable() { 3212 public void run() { 3213 view.setFocusable(true); 3214 view.requestFocus(); 3215 } 3216 }); 3217 getInstrumentation().waitForIdleSync(); 3218 3219 getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK)); 3220 assertTrue(view.hasCalledDispatchKeyEventPreIme()); 3221 assertTrue(view.hasCalledOnKeyPreIme()); 3222 } 3223 3224 public void testHapticFeedback() { 3225 Vibrator vib = (Vibrator) mActivity.getSystemService(Context.VIBRATOR_SERVICE); 3226 boolean hasVibrator = vib.hasVibrator(); 3227 3228 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 3229 final int LONG_PRESS = HapticFeedbackConstants.LONG_PRESS; 3230 final int FLAG_IGNORE_VIEW_SETTING = HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING; 3231 final int FLAG_IGNORE_GLOBAL_SETTING = HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING; 3232 final int ALWAYS = FLAG_IGNORE_VIEW_SETTING | FLAG_IGNORE_GLOBAL_SETTING; 3233 3234 view.setHapticFeedbackEnabled(false); 3235 assertFalse(view.isHapticFeedbackEnabled()); 3236 assertFalse(view.performHapticFeedback(LONG_PRESS)); 3237 assertFalse(view.performHapticFeedback(LONG_PRESS, FLAG_IGNORE_GLOBAL_SETTING)); 3238 assertEquals(hasVibrator, view.performHapticFeedback(LONG_PRESS, ALWAYS)); 3239 3240 view.setHapticFeedbackEnabled(true); 3241 assertTrue(view.isHapticFeedbackEnabled()); 3242 assertEquals(hasVibrator, view.performHapticFeedback(LONG_PRESS, FLAG_IGNORE_GLOBAL_SETTING)); 3243 } 3244 3245 public void testInputConnection() throws Throwable { 3246 final InputMethodManager imm = (InputMethodManager)getActivity().getSystemService( 3247 Context.INPUT_METHOD_SERVICE); 3248 final MockView view = (MockView) mActivity.findViewById(R.id.mock_view); 3249 final ViewGroup viewGroup = (ViewGroup) mActivity.findViewById(R.id.viewlayout_root); 3250 final MockEditText editText = new MockEditText(mActivity); 3251 3252 runTestOnUiThread(new Runnable() { 3253 @Override 3254 public void run() { 3255 viewGroup.addView(editText); 3256 editText.requestFocus(); 3257 } 3258 }); 3259 getInstrumentation().waitForIdleSync(); 3260 assertTrue(editText.isFocused()); 3261 3262 runTestOnUiThread(new Runnable() { 3263 @Override 3264 public void run() { 3265 imm.showSoftInput(editText, 0); 3266 } 3267 }); 3268 getInstrumentation().waitForIdleSync(); 3269 3270 new PollingCheck(TIMEOUT_DELTA) { 3271 @Override 3272 protected boolean check() { 3273 return editText.hasCalledOnCreateInputConnection(); 3274 } 3275 }.run(); 3276 3277 assertTrue(editText.hasCalledOnCheckIsTextEditor()); 3278 3279 runTestOnUiThread(new Runnable() { 3280 @Override 3281 public void run() { 3282 assertTrue(imm.isActive(editText)); 3283 assertFalse(editText.hasCalledCheckInputConnectionProxy()); 3284 imm.isActive(view); 3285 assertTrue(editText.hasCalledCheckInputConnectionProxy()); 3286 } 3287 }); 3288 } 3289 3290 public void testFilterTouchesWhenObscured() throws Throwable { 3291 OnTouchListenerImpl touchListener = new OnTouchListenerImpl(); 3292 View view = new View(mActivity); 3293 view.setOnTouchListener(touchListener); 3294 3295 MotionEvent.PointerProperties[] props = new MotionEvent.PointerProperties[] { 3296 new MotionEvent.PointerProperties() 3297 }; 3298 MotionEvent.PointerCoords[] coords = new MotionEvent.PointerCoords[] { 3299 new MotionEvent.PointerCoords() 3300 }; 3301 MotionEvent obscuredTouch = MotionEvent.obtain(0, 0, MotionEvent.ACTION_DOWN, 3302 1, props, coords, 0, 0, 0, 0, -1, 0, InputDevice.SOURCE_TOUCHSCREEN, 3303 MotionEvent.FLAG_WINDOW_IS_OBSCURED); 3304 MotionEvent unobscuredTouch = MotionEvent.obtain(0, 0, MotionEvent.ACTION_DOWN, 3305 1, props, coords, 0, 0, 0, 0, -1, 0, InputDevice.SOURCE_TOUCHSCREEN, 3306 0); 3307 3308 // Initially filter touches is false so all touches are dispatched. 3309 assertFalse(view.getFilterTouchesWhenObscured()); 3310 3311 view.dispatchTouchEvent(unobscuredTouch); 3312 assertTrue(touchListener.hasOnTouch()); 3313 touchListener.reset(); 3314 view.dispatchTouchEvent(obscuredTouch); 3315 assertTrue(touchListener.hasOnTouch()); 3316 touchListener.reset(); 3317 3318 // Set filter touches to true so only unobscured touches are dispatched. 3319 view.setFilterTouchesWhenObscured(true); 3320 assertTrue(view.getFilterTouchesWhenObscured()); 3321 3322 view.dispatchTouchEvent(unobscuredTouch); 3323 assertTrue(touchListener.hasOnTouch()); 3324 touchListener.reset(); 3325 view.dispatchTouchEvent(obscuredTouch); 3326 assertFalse(touchListener.hasOnTouch()); 3327 touchListener.reset(); 3328 3329 // Set filter touches to false so all touches are dispatched. 3330 view.setFilterTouchesWhenObscured(false); 3331 assertFalse(view.getFilterTouchesWhenObscured()); 3332 3333 view.dispatchTouchEvent(unobscuredTouch); 3334 assertTrue(touchListener.hasOnTouch()); 3335 touchListener.reset(); 3336 view.dispatchTouchEvent(obscuredTouch); 3337 assertTrue(touchListener.hasOnTouch()); 3338 touchListener.reset(); 3339 } 3340 3341 private static class MockEditText extends EditText { 3342 private boolean mCalledCheckInputConnectionProxy = false; 3343 private boolean mCalledOnCreateInputConnection = false; 3344 private boolean mCalledOnCheckIsTextEditor = false; 3345 3346 public MockEditText(Context context) { 3347 super(context); 3348 } 3349 3350 @Override 3351 public boolean checkInputConnectionProxy(View view) { 3352 mCalledCheckInputConnectionProxy = true; 3353 return super.checkInputConnectionProxy(view); 3354 } 3355 3356 public boolean hasCalledCheckInputConnectionProxy() { 3357 return mCalledCheckInputConnectionProxy; 3358 } 3359 3360 @Override 3361 public InputConnection onCreateInputConnection(EditorInfo outAttrs) { 3362 mCalledOnCreateInputConnection = true; 3363 return super.onCreateInputConnection(outAttrs); 3364 } 3365 3366 public boolean hasCalledOnCreateInputConnection() { 3367 return mCalledOnCreateInputConnection; 3368 } 3369 3370 @Override 3371 public boolean onCheckIsTextEditor() { 3372 mCalledOnCheckIsTextEditor = true; 3373 return super.onCheckIsTextEditor(); 3374 } 3375 3376 public boolean hasCalledOnCheckIsTextEditor() { 3377 return mCalledOnCheckIsTextEditor; 3378 } 3379 3380 public void reset() { 3381 mCalledCheckInputConnectionProxy = false; 3382 mCalledOnCreateInputConnection = false; 3383 mCalledOnCheckIsTextEditor = false; 3384 } 3385 } 3386 3387 private final static class MockViewParent extends View implements ViewParent { 3388 private boolean mHasClearChildFocus = false; 3389 private boolean mHasRequestLayout = false; 3390 private boolean mHasCreateContextMenu = false; 3391 private boolean mHasShowContextMenuForChild = false; 3392 private boolean mHasGetChildVisibleRect = false; 3393 private boolean mHasInvalidateChild = false; 3394 private boolean mHasOnCreateDrawableState = false; 3395 private boolean mHasChildDrawableStateChanged = false; 3396 private boolean mHasBroughtChildToFront = false; 3397 private Rect mTempRect; 3398 3399 private final static int[] DEFAULT_PARENT_STATE_SET = new int[] { 789 }; 3400 3401 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, 3402 boolean immediate) { 3403 return false; 3404 } 3405 3406 public MockViewParent(Context context) { 3407 super(context); 3408 } 3409 3410 public void bringChildToFront(View child) { 3411 mHasBroughtChildToFront = true; 3412 } 3413 3414 public boolean hasBroughtChildToFront() { 3415 return mHasBroughtChildToFront; 3416 } 3417 3418 public void childDrawableStateChanged(View child) { 3419 mHasChildDrawableStateChanged = true; 3420 } 3421 3422 public boolean hasChildDrawableStateChanged() { 3423 return mHasChildDrawableStateChanged; 3424 } 3425 3426 @Override 3427 protected void dispatchSetPressed(boolean pressed) { 3428 super.dispatchSetPressed(pressed); 3429 } 3430 3431 @Override 3432 protected void dispatchSetSelected(boolean selected) { 3433 super.dispatchSetSelected(selected); 3434 } 3435 3436 public void clearChildFocus(View child) { 3437 mHasClearChildFocus = true; 3438 } 3439 3440 public boolean hasClearChildFocus() { 3441 return mHasClearChildFocus; 3442 } 3443 3444 public void createContextMenu(ContextMenu menu) { 3445 mHasCreateContextMenu = true; 3446 } 3447 3448 public boolean hasCreateContextMenu() { 3449 return mHasCreateContextMenu; 3450 } 3451 3452 public View focusSearch(View v, int direction) { 3453 return v; 3454 } 3455 3456 public void focusableViewAvailable(View v) { 3457 3458 } 3459 3460 public boolean getChildVisibleRect(View child, Rect r, Point offset) { 3461 mHasGetChildVisibleRect = true; 3462 return false; 3463 } 3464 3465 public boolean hasGetChildVisibleRect() { 3466 return mHasGetChildVisibleRect; 3467 } 3468 3469 public void invalidateChild(View child, Rect r) { 3470 mTempRect = new Rect(r); 3471 mHasInvalidateChild = true; 3472 } 3473 3474 public Rect getTempRect() { 3475 return mTempRect; 3476 } 3477 3478 public boolean hasInvalidateChild() { 3479 return mHasInvalidateChild; 3480 } 3481 3482 public ViewParent invalidateChildInParent(int[] location, Rect r) { 3483 return null; 3484 } 3485 3486 public boolean isLayoutRequested() { 3487 return false; 3488 } 3489 3490 public void recomputeViewAttributes(View child) { 3491 3492 } 3493 3494 public void requestChildFocus(View child, View focused) { 3495 3496 } 3497 3498 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { 3499 3500 } 3501 3502 public void requestLayout() { 3503 mHasRequestLayout = true; 3504 } 3505 3506 public boolean hasRequestLayout() { 3507 return mHasRequestLayout; 3508 } 3509 3510 public void requestTransparentRegion(View child) { 3511 3512 } 3513 3514 public boolean showContextMenuForChild(View originalView) { 3515 mHasShowContextMenuForChild = true; 3516 return false; 3517 } 3518 3519 public ActionMode startActionModeForChild(View originalView, 3520 ActionMode.Callback callback) { 3521 return null; 3522 } 3523 3524 public boolean hasShowContextMenuForChild() { 3525 return mHasShowContextMenuForChild; 3526 } 3527 3528 @Override 3529 protected int[] onCreateDrawableState(int extraSpace) { 3530 mHasOnCreateDrawableState = true; 3531 return DEFAULT_PARENT_STATE_SET; 3532 } 3533 3534 public boolean requestSendAccessibilityEvent(View child, AccessibilityEvent event) { 3535 return false; 3536 } 3537 3538 public static int[] getDefaultParentStateSet() { 3539 return DEFAULT_PARENT_STATE_SET; 3540 } 3541 3542 public boolean hasOnCreateDrawableState() { 3543 return mHasOnCreateDrawableState; 3544 } 3545 3546 public void reset() { 3547 mHasClearChildFocus = false; 3548 mHasRequestLayout = false; 3549 mHasCreateContextMenu = false; 3550 mHasShowContextMenuForChild = false; 3551 mHasGetChildVisibleRect = false; 3552 mHasInvalidateChild = false; 3553 mHasOnCreateDrawableState = false; 3554 mHasChildDrawableStateChanged = false; 3555 mHasBroughtChildToFront = false; 3556 } 3557 3558 public void childOverlayStateChanged(View child) { 3559 3560 } 3561 3562 @Override 3563 public void childHasTransientStateChanged(View child, boolean hasTransientState) { 3564 3565 } 3566 3567 @Override 3568 public ViewParent getParentForAccessibility() { 3569 return null; 3570 } 3571 3572 @Override 3573 public void notifySubtreeAccessibilityStateChanged(View child, 3574 View source, int changeType) { 3575 3576 } 3577 } 3578 3579 private final class OnCreateContextMenuListenerImpl implements OnCreateContextMenuListener { 3580 private boolean mHasOnCreateContextMenu = false; 3581 3582 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 3583 mHasOnCreateContextMenu = true; 3584 } 3585 3586 public boolean hasOnCreateContextMenu() { 3587 return mHasOnCreateContextMenu; 3588 } 3589 3590 public void reset() { 3591 mHasOnCreateContextMenu = false; 3592 } 3593 } 3594 3595 private static class MockViewGroupParent extends ViewGroup implements ViewParent { 3596 private boolean mHasRequestChildRectangleOnScreen = false; 3597 3598 public MockViewGroupParent(Context context) { 3599 super(context); 3600 } 3601 3602 @Override 3603 protected void onLayout(boolean changed, int l, int t, int r, int b) { 3604 3605 } 3606 3607 @Override 3608 public boolean requestChildRectangleOnScreen(View child, 3609 Rect rectangle, boolean immediate) { 3610 mHasRequestChildRectangleOnScreen = true; 3611 return super.requestChildRectangleOnScreen(child, rectangle, immediate); 3612 } 3613 3614 public boolean hasRequestChildRectangleOnScreen() { 3615 return mHasRequestChildRectangleOnScreen; 3616 } 3617 3618 @Override 3619 protected void detachViewFromParent(View child) { 3620 super.detachViewFromParent(child); 3621 } 3622 3623 public void reset() { 3624 mHasRequestChildRectangleOnScreen = false; 3625 } 3626 } 3627 3628 private static final class OnClickListenerImpl implements OnClickListener { 3629 private boolean mHasOnClick = false; 3630 3631 public void onClick(View v) { 3632 mHasOnClick = true; 3633 } 3634 3635 public boolean hasOnClick() { 3636 return mHasOnClick; 3637 } 3638 3639 public void reset() { 3640 mHasOnClick = false; 3641 } 3642 } 3643 3644 private static final class OnLongClickListenerImpl implements OnLongClickListener { 3645 private boolean mHasOnLongClick = false; 3646 3647 public boolean hasOnLongClick() { 3648 return mHasOnLongClick; 3649 } 3650 3651 public void reset() { 3652 mHasOnLongClick = false; 3653 } 3654 3655 public boolean onLongClick(View v) { 3656 mHasOnLongClick = true; 3657 return true; 3658 } 3659 } 3660 3661 private static final class OnFocusChangeListenerImpl implements OnFocusChangeListener { 3662 private boolean mHasOnFocusChange = false; 3663 3664 public void onFocusChange(View v, boolean hasFocus) { 3665 mHasOnFocusChange = true; 3666 } 3667 3668 public boolean hasOnFocusChange() { 3669 return mHasOnFocusChange; 3670 } 3671 3672 public void reset() { 3673 mHasOnFocusChange = false; 3674 } 3675 } 3676 3677 private static final class OnKeyListenerImpl implements OnKeyListener { 3678 private boolean mHasOnKey = false; 3679 3680 public boolean onKey(View v, int keyCode, KeyEvent event) { 3681 mHasOnKey = true; 3682 return true; 3683 } 3684 3685 public void reset() { 3686 mHasOnKey = false; 3687 } 3688 3689 public boolean hasOnKey() { 3690 return mHasOnKey; 3691 } 3692 } 3693 3694 private static final class OnTouchListenerImpl implements OnTouchListener { 3695 private boolean mHasOnTouch = false; 3696 3697 public boolean onTouch(View v, MotionEvent event) { 3698 mHasOnTouch = true; 3699 return true; 3700 } 3701 3702 public void reset() { 3703 mHasOnTouch = false; 3704 } 3705 3706 public boolean hasOnTouch() { 3707 return mHasOnTouch; 3708 } 3709 } 3710 3711 private static final class MockTouchDelegate extends TouchDelegate { 3712 public MockTouchDelegate(Rect bounds, View delegateView) { 3713 super(bounds, delegateView); 3714 } 3715 3716 private boolean mCalledOnTouchEvent = false; 3717 3718 @Override 3719 public boolean onTouchEvent(MotionEvent event) { 3720 mCalledOnTouchEvent = true; 3721 return super.onTouchEvent(event); 3722 } 3723 3724 public boolean hasCalledOnTouchEvent() { 3725 return mCalledOnTouchEvent; 3726 } 3727 3728 public void reset() { 3729 mCalledOnTouchEvent = false; 3730 } 3731 }; 3732 3733 private static final class ViewData { 3734 public int childCount; 3735 public String tag; 3736 public View firstChild; 3737 } 3738 3739 private static final class MockRunnable implements Runnable { 3740 public boolean hasRun = false; 3741 3742 public void run() { 3743 hasRun = true; 3744 } 3745 } 3746 } 3747