aboutsummaryrefslogtreecommitdiff
path: root/robolectric/src/test/java/org/robolectric/shadows/ShadowViewTest.java
blob: 432eec66ca0683af3e686be949b7216c65ddce28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
package org.robolectric.shadows;

import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
import static android.os.Build.VERSION_CODES.LOLLIPOP;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.robolectric.Robolectric.buildActivity;
import static org.robolectric.Robolectric.setupActivity;
import static org.robolectric.Shadows.shadowOf;
import static org.robolectric.shadows.ShadowLooper.shadowMainLooper;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Looper;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.ContextMenu;
import android.view.HapticFeedbackConstants;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowId;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.R;
import org.robolectric.Robolectric;
import org.robolectric.android.DeviceConfig;
import org.robolectric.android.controller.ActivityController;
import org.robolectric.annotation.Config;
import org.robolectric.util.TestRunnable;

@RunWith(AndroidJUnit4.class)
public class ShadowViewTest {
  private View view;
  private List<String> transcript;
  private Application context;

  @Before
  public void setUp() throws Exception {
    InstrumentationRegistry.getInstrumentation().setInTouchMode(false);
    transcript = new ArrayList<>();
    context = ApplicationProvider.getApplicationContext();
    view = Robolectric.setupActivity(ContainerActivity.class).getView();
  }

  public static class ContainerActivity extends Activity {

    private TextView view;

    @Override
    protected void onResume() {
      super.onResume();
      LinearLayout parent = new LinearLayout(this);

      // decoy to receive default focus
      TextView otherTextView = new TextView(this);
      otherTextView.setFocusable(true);
      parent.addView(otherTextView);

      view = new TextView(this);
      view.setId(R.id.action_search);
      parent.addView(view);
      setContentView(parent);
    }

    public View getView() {
      return view;
    }
  }

  @Test
  public void layout_shouldAffectWidthAndHeight() throws Exception {
    view.layout(100, 200, 303, 404);
    assertThat(view.getWidth()).isEqualTo(303 - 100);
    assertThat(view.getHeight()).isEqualTo(404 - 200);
  }

  @Test
  public void measuredDimensions() throws Exception {
    View view1 =
        new View(context) {
          {
            setMeasuredDimension(123, 456);
          }
        };
    assertThat(view1.getMeasuredWidth()).isEqualTo(123);
    assertThat(view1.getMeasuredHeight()).isEqualTo(456);
  }

  @Test
  public void layout_shouldCallOnLayoutOnlyIfChanged() throws Exception {
    View view1 =
        new View(context) {
          @Override
          protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
            transcript.add(
                "onLayout " + changed + " " + left + " " + top + " " + right + " " + bottom);
          }
        };
    view1.layout(0, 0, 0, 0);
    assertThat(transcript).isEmpty();
    view1.layout(1, 2, 3, 4);
    assertThat(transcript).containsExactly("onLayout true 1 2 3 4");
    transcript.clear();
    view1.layout(1, 2, 3, 4);
    assertThat(transcript).isEmpty();
  }

  @Test
  public void shouldFocus() throws Exception {
    final List<String> transcript = new ArrayList<>();

    view.setOnFocusChangeListener(new View.OnFocusChangeListener() {
      @Override
      public void onFocusChange(View v, boolean hasFocus) {
        transcript.add(hasFocus ? "Gained focus" : "Lost focus");
      }
    });

    assertFalse(view.isFocused());
    assertFalse(view.hasFocus());
    assertThat(transcript).isEmpty();

    view.requestFocus();
    assertFalse(view.isFocused());
    assertFalse(view.hasFocus());
    assertThat(transcript).isEmpty();

    view.setFocusable(true);
    shadowMainLooper().idle();
    view.requestFocus();
    assertTrue(view.isFocused());
    assertTrue(view.hasFocus());
    assertThat(transcript).containsExactly("Gained focus");
    transcript.clear();

    // take it
    view.clearFocus();
    shadowMainLooper().idle();
    assertFalse(view.isFocused());
    assertFalse(view.hasFocus());
    assertThat(transcript).containsExactly("Lost focus");
  }

  @Test
  public void shouldNotBeFocusableByDefault() throws Exception {
    assertFalse(view.isFocusable());

    view.setFocusable(true);
    assertTrue(view.isFocusable());
  }

  @Test
  public void shouldKnowIfThisOrAncestorsAreVisible() throws Exception {
    assertThat(view.isShown()).isTrue();
    shadowOf(view).setMyParent(null);

    ViewGroup parent = new LinearLayout(context);
    parent.addView(view);

    ViewGroup grandParent = new LinearLayout(context);
    grandParent.addView(parent);

    grandParent.setVisibility(View.GONE);

    assertFalse(view.isShown());
  }

  @Test
  public void shouldInflateMergeRootedLayoutAndNotCreateReferentialLoops() throws Exception {
    LinearLayout root = new LinearLayout(context);
    LinearLayout.inflate(context, R.layout.inner_merge, root);
    for (int i = 0; i < root.getChildCount(); i++) {
      View child = root.getChildAt(i);
      assertNotSame(root, child);
    }
  }

  @Test
  public void performLongClick_shouldClickOnView() throws Exception {
    OnLongClickListener clickListener = mock(OnLongClickListener.class);
    view.setOnLongClickListener(clickListener);
    view.performLongClick();

    verify(clickListener).onLongClick(view);
  }

  @Test
  public void checkedClick_shouldClickOnView() throws Exception {
    OnClickListener clickListener = mock(OnClickListener.class);
    view.setOnClickListener(clickListener);
    shadowOf(view).checkedPerformClick();

    verify(clickListener).onClick(view);
  }

  @Test(expected = RuntimeException.class)
  public void checkedClick_shouldThrowIfViewIsNotVisible() throws Exception {
    ViewGroup grandParent = new LinearLayout(context);
    ViewGroup parent = new LinearLayout(context);
    grandParent.addView(parent);
    parent.addView(view);
    grandParent.setVisibility(View.GONE);

    shadowOf(view).checkedPerformClick();
  }

  @Test(expected = RuntimeException.class)
  public void checkedClick_shouldThrowIfViewIsDisabled() throws Exception {
    view.setEnabled(false);
    shadowOf(view).checkedPerformClick();
  }

  @Test
  public void getBackground_shouldReturnNullIfNoBackgroundHasBeenSet() throws Exception {
    assertThat(view.getBackground()).isNull();
  }

  @Test
  public void shouldSetBackgroundColor() {
    int red = 0xffff0000;
    view.setBackgroundColor(red);
    ColorDrawable background = (ColorDrawable) view.getBackground();
    assertThat(background.getColor()).isEqualTo(red);
  }

  @Test
  public void shouldSetBackgroundResource() throws Exception {
    view.setBackgroundResource(R.drawable.an_image);
    assertThat(shadowOf((BitmapDrawable) view.getBackground()).getCreatedFromResId())
        .isEqualTo(R.drawable.an_image);
  }

  @Test
  public void shouldClearBackgroundResource() throws Exception {
    view.setBackgroundResource(R.drawable.an_image);
    view.setBackgroundResource(0);
    assertThat(view.getBackground()).isEqualTo(null);
  }

  @Test
  public void shouldRecordBackgroundColor() {
    int[] colors = {R.color.black, R.color.clear, R.color.white};

    for (int color : colors) {
      view.setBackgroundColor(color);
      ColorDrawable drawable = (ColorDrawable) view.getBackground();
      assertThat(drawable.getColor()).isEqualTo(color);
    }
  }

  @Test
  public void shouldRecordBackgroundDrawable() {
    Drawable drawable = new BitmapDrawable(BitmapFactory.decodeFile("some/fake/file"));
    view.setBackgroundDrawable(drawable);
    assertThat(view.getBackground()).isSameInstanceAs(drawable);
    assertThat(ShadowView.visualize(view)).isEqualTo("background:\nBitmap for file:some/fake/file");
  }

  @Test
  public void shouldPostActionsToTheMessageQueue() throws Exception {
    shadowMainLooper().pause();

    TestRunnable runnable = new TestRunnable();
    assertThat(view.post(runnable)).isTrue();
    assertFalse(runnable.wasRun);

    shadowMainLooper().idle();
    assertTrue(runnable.wasRun);
  }

  @Test
  public void shouldPostInvalidateDelayed() throws Exception {
    shadowMainLooper().pause();
    ShadowView shadowView = shadowOf(view);
    shadowView.clearWasInvalidated();
    assertFalse(shadowView.wasInvalidated());

    view.postInvalidateDelayed(1);
    assertFalse(shadowView.wasInvalidated());

    shadowMainLooper().idleFor(Duration.ofMillis(1));
    assertTrue(shadowView.wasInvalidated());
  }

  @Test
  public void shouldPostActionsToTheMessageQueueWithDelay() throws Exception {
    shadowMainLooper().pause();

    TestRunnable runnable = new TestRunnable();
    view.postDelayed(runnable, 1);
    assertFalse(runnable.wasRun);

    shadowMainLooper().idleFor(Duration.ofMillis(1));
    assertTrue(runnable.wasRun);
  }

  @Test
  public void shouldRemovePostedCallbacksFromMessageQueue() throws Exception {
    TestRunnable runnable = new TestRunnable();
    assertThat(view.postDelayed(runnable, 1)).isTrue();

    assertThat(view.removeCallbacks(runnable)).isTrue();

    shadowMainLooper().idleFor(Duration.ofMillis(1));
    assertThat(runnable.wasRun).isFalse();
  }

  @Test
  public void shouldSupportAllConstructors() throws Exception {
    new View(context);
    new View(context, null);
    new View(context, null, 0);
  }

  @Test
  public void shouldRememberIsPressed() {
    view.setPressed(true);
    assertTrue(view.isPressed());
    view.setPressed(false);
    assertFalse(view.isPressed());
  }

  @Test
  public void shouldAddOnClickListenerFromAttribute() throws Exception {
    AttributeSet attrs = Robolectric.buildAttributeSet()
        .addAttribute(android.R.attr.onClick, "clickMe")
        .build()
        ;

    view = new View(context, attrs);
    assertNotNull(shadowOf(view).getOnClickListener());
  }

  @Test
  public void shouldCallOnClickWithAttribute() throws Exception {
    MyActivity myActivity = buildActivity(MyActivity.class).create().get();

    AttributeSet attrs = Robolectric.buildAttributeSet()
        .addAttribute(android.R.attr.onClick, "clickMe")
        .build();

    view = new View(myActivity, attrs);
    view.performClick();
    assertTrue("Should have been called", myActivity.called);
  }

  @Test(expected = RuntimeException.class)
  public void shouldThrowExceptionWithBadMethodName() throws Exception {
    MyActivity myActivity = buildActivity(MyActivity.class).create().get();

    AttributeSet attrs = Robolectric.buildAttributeSet()
        .addAttribute(android.R.attr.onClick, "clickYou")
        .build();

    view = new View(myActivity, attrs);
    view.performClick();
  }

  @Test
  public void shouldSetAnimation() throws Exception {
    Animation anim = new TestAnimation();
    view.setAnimation(anim);
    assertThat(view.getAnimation()).isSameInstanceAs(anim);
  }

  @Test
  public void clearAnimation_cancelsAnimation() throws Exception {
    AtomicInteger numTicks = new AtomicInteger();
    final Animation anim =
        new Animation() {
          @Override
          protected void applyTransformation(float interpolatedTime, Transformation t) {
            super.applyTransformation(interpolatedTime, t);
            numTicks.incrementAndGet();
          }
        };
    anim.setDuration(Duration.ofSeconds(1).toMillis());
    view.setAnimation(anim);
    view.clearAnimation();
    shadowOf(Looper.getMainLooper()).runToEndOfTasks();
    assertThat(numTicks.get()).isEqualTo(0);
  }

  @Test
  public void shouldFindViewWithTag() {
    view.setTag("tagged");
    assertThat((View) view.findViewWithTag("tagged")).isSameInstanceAs(view);
  }

  @Test
  public void scrollTo_shouldStoreTheScrolledCoordinates() throws Exception {
    view.scrollTo(1, 2);
    assertThat(shadowOf(view).scrollToCoordinates).isEqualTo(new Point(1, 2));
  }

  @Test
  public void shouldScrollTo() throws Exception {
    view.scrollTo(7, 6);

    assertEquals(7, view.getScrollX());
    assertEquals(6, view.getScrollY());
  }

  @Test
  public void scrollBy_shouldStoreTheScrolledCoordinates() throws Exception {
    view.scrollTo(4, 5);
    view.scrollBy(10, 20);
    assertThat(shadowOf(view).scrollToCoordinates).isEqualTo(new Point(14, 25));

    assertThat(view.getScrollX()).isEqualTo(14);
    assertThat(view.getScrollY()).isEqualTo(25);
  }

  @Test
  public void shouldGetScrollXAndY() {
    assertEquals(0, view.getScrollX());
    assertEquals(0, view.getScrollY());
  }

  @Test
  public void getViewTreeObserver_shouldReturnTheSameObserverFromMultipleCalls() throws Exception {
    ViewTreeObserver observer = view.getViewTreeObserver();
    assertThat(observer).isInstanceOf(ViewTreeObserver.class);
    assertThat(view.getViewTreeObserver()).isSameInstanceAs(observer);
  }

  @Test
  public void dispatchTouchEvent_sendsMotionEventToOnTouchEvent() throws Exception {
    TouchableView touchableView = new TouchableView(context);
    MotionEvent event = MotionEvent.obtain(0L, 0L, MotionEvent.ACTION_DOWN, 12f, 34f, 0);
    touchableView.dispatchTouchEvent(event);
    assertThat(touchableView.event).isSameInstanceAs(event);
    view.dispatchTouchEvent(event);
    assertThat(shadowOf(view).getLastTouchEvent()).isSameInstanceAs(event);
  }

  @Test
  public void dispatchTouchEvent_listensToFalseFromListener() throws Exception {
    final AtomicBoolean called = new AtomicBoolean(false);
    view.setOnTouchListener(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View view, MotionEvent motionEvent) {
        called.set(true); return false;
      }
    });
    MotionEvent event = MotionEvent.obtain(0L, 0L, MotionEvent.ACTION_DOWN, 12f, 34f, 0);
    view.dispatchTouchEvent(event);
    assertThat(shadowOf(view).getLastTouchEvent()).isSameInstanceAs(event);
    assertThat(called.get()).isTrue();
  }

  @Test
  public void test_nextFocusDownId() throws Exception {
    assertEquals(View.NO_ID, view.getNextFocusDownId());

    view.setNextFocusDownId(R.id.icon);
    assertEquals(R.id.icon, view.getNextFocusDownId());
  }

  @Test
  public void startAnimation() {
    AlphaAnimation animation = new AlphaAnimation(0, 1);
    Animation.AnimationListener listener = mock(Animation.AnimationListener.class);
    animation.setAnimationListener(listener);

    view.startAnimation(animation);
    shadowMainLooper().idle();

    verify(listener).onAnimationStart(animation);
    verify(listener).onAnimationEnd(animation);
    assertThat(animation.isInitialized()).isTrue();
    assertThat(view.getAnimation()).isNull();
    assertThat(shadowOf(view).getAnimations()).contains(animation);
  }

  @Test
  public void setAnimation() {
    AlphaAnimation animation = new AlphaAnimation(0, 1);

    Animation.AnimationListener listener = mock(Animation.AnimationListener.class);
    animation.setAnimationListener(listener);
    animation.setStartTime(1000);
    view.setAnimation(animation);

    verifyNoMoreInteractions(listener);

    SystemClock.setCurrentTimeMillis(1000);
    shadowMainLooper().idle();

    verify(listener).onAnimationStart(animation);
    verify(listener).onAnimationEnd(animation);
  }

  @Test
  public void setNullAnimation() {
    TestView view = new TestView(buildActivity(Activity.class).create().get());
    view.setAnimation(null);
    assertThat(view.getAnimation()).isNull();
  }

  @Test
  public void test_measuredDimension() {
    // View does not provide its own onMeasure implementation
    TestView view1 = new TestView(buildActivity(Activity.class).create().get());

    assertThat(view1.getHeight()).isEqualTo(0);
    assertThat(view1.getWidth()).isEqualTo(0);
    assertThat(view1.getMeasuredHeight()).isEqualTo(0);
    assertThat(view1.getMeasuredWidth()).isEqualTo(0);

    view1.measure(MeasureSpec.makeMeasureSpec(150, MeasureSpec.AT_MOST),
        MeasureSpec.makeMeasureSpec(300, MeasureSpec.AT_MOST));

    assertThat(view1.getHeight()).isEqualTo(0);
    assertThat(view1.getWidth()).isEqualTo(0);
    assertThat(view1.getMeasuredHeight()).isEqualTo(300);
    assertThat(view1.getMeasuredWidth()).isEqualTo(150);
  }

  @Test
  public void test_measuredDimensionCustomView() {
    // View provides its own onMeasure implementation
    TestView2 view2 = new TestView2(buildActivity(Activity.class).create().get(), 300, 100);

    assertThat(view2.getWidth()).isEqualTo(0);
    assertThat(view2.getHeight()).isEqualTo(0);
    assertThat(view2.getMeasuredWidth()).isEqualTo(0);
    assertThat(view2.getMeasuredHeight()).isEqualTo(0);

    view2.measure(MeasureSpec.makeMeasureSpec(200, MeasureSpec.AT_MOST),
    MeasureSpec.makeMeasureSpec(50, MeasureSpec.AT_MOST));

    assertThat(view2.getWidth()).isEqualTo(0);
    assertThat(view2.getHeight()).isEqualTo(0);
    assertThat(view2.getMeasuredWidth()).isEqualTo(300);
    assertThat(view2.getMeasuredHeight()).isEqualTo(100);
  }

  @Test
  public void shouldGetAndSetTranslations() throws Exception {
    view = new TestView(buildActivity(Activity.class).create().get());
    view.setTranslationX(8.9f);
    view.setTranslationY(4.6f);

    assertThat(view.getTranslationX()).isEqualTo(8.9f);
    assertThat(view.getTranslationY()).isEqualTo(4.6f);
  }

  @Test
  public void shouldGetAndSetAlpha() throws Exception {
    view = new TestView(buildActivity(Activity.class).create().get());
    view.setAlpha(9.1f);

    assertThat(view.getAlpha()).isEqualTo(9.1f);
  }

  @Test
  public void itKnowsIfTheViewIsShown() {
    view.setVisibility(View.VISIBLE);
    assertThat(view.isShown()).isTrue();
  }

  @Test
  public void itKnowsIfTheViewIsNotShown() {
    view.setVisibility(View.GONE);
    assertThat(view.isShown()).isFalse();

    view.setVisibility(View.INVISIBLE);
    assertThat(view.isShown()).isFalse();
  }

  @Test
  public void shouldTrackRequestLayoutCalls() throws Exception {
    shadowOf(view).setDidRequestLayout(false);
    assertThat(shadowOf(view).didRequestLayout()).isFalse();
    view.requestLayout();
    assertThat(shadowOf(view).didRequestLayout()).isTrue();
    shadowOf(view).setDidRequestLayout(false);
    assertThat(shadowOf(view).didRequestLayout()).isFalse();
  }

  @Test
  public void shouldClickAndNotClick() throws Exception {
    assertThat(view.isClickable()).isFalse();
    view.setClickable(true);
    assertThat(view.isClickable()).isTrue();
    view.setClickable(false);
    assertThat(view.isClickable()).isFalse();
    view.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        ;
      }
    });
    assertThat(view.isClickable()).isTrue();
  }

  @Test
  public void shouldLongClickAndNotLongClick() throws Exception {
    assertThat(view.isLongClickable()).isFalse();
    view.setLongClickable(true);
    assertThat(view.isLongClickable()).isTrue();
    view.setLongClickable(false);
    assertThat(view.isLongClickable()).isFalse();
    view.setOnLongClickListener(new OnLongClickListener() {
      @Override
      public boolean onLongClick(View v) {
        return false;
      }
    });
    assertThat(view.isLongClickable()).isTrue();
  }

  @Test
  public void rotationX() {
    view.setRotationX(10f);
    assertThat(view.getRotationX()).isEqualTo(10f);
  }

  @Test
  public void rotationY() {
    view.setRotationY(20f);
    assertThat(view.getRotationY()).isEqualTo(20f);
  }

  @Test
  public void rotation() {
    view.setRotation(30f);
    assertThat(view.getRotation()).isEqualTo(30f);
  }

  @Test
  @Config(minSdk = LOLLIPOP)
  public void cameraDistance() {
    view.setCameraDistance(100f);
    assertThat(view.getCameraDistance()).isEqualTo(100f);
  }

  @Test
  public void scaleX() {
    assertThat(view.getScaleX()).isEqualTo(1f);
    view.setScaleX(0.5f);
    assertThat(view.getScaleX()).isEqualTo(0.5f);
  }

  @Test
  public void scaleY() {
    assertThat(view.getScaleY()).isEqualTo(1f);
    view.setScaleY(0.5f);
    assertThat(view.getScaleY()).isEqualTo(0.5f);
  }

  @Test
  public void pivotX() {
    view.setPivotX(10f);
    assertThat(view.getPivotX()).isEqualTo(10f);
  }

  @Test
  public void pivotY() {
    view.setPivotY(10f);
    assertThat(view.getPivotY()).isEqualTo(10f);
  }

  @Test
  @Config(minSdk = LOLLIPOP)
  public void elevation() {
    view.setElevation(10f);
    assertThat(view.getElevation()).isEqualTo(10f);
  }

  @Test
  public void translationX() {
    view.setTranslationX(10f);
    assertThat(view.getTranslationX()).isEqualTo(10f);
  }

  @Test
  public void translationY() {
    view.setTranslationY(10f);
    assertThat(view.getTranslationY()).isEqualTo(10f);
  }

  @Test
  @Config(minSdk = LOLLIPOP)
  public void translationZ() {
    view.setTranslationZ(10f);
    assertThat(view.getTranslationZ()).isEqualTo(10f);
  }

  @Test
  @Config(minSdk = LOLLIPOP)
  public void clipToOutline() {
    view.setClipToOutline(true);
    assertThat(view.getClipToOutline()).isTrue();
  }

  @Test
  public void performHapticFeedback_shouldSetLastPerformedHapticFeedback() throws Exception {
    assertThat(shadowOf(view).lastHapticFeedbackPerformed()).isEqualTo(-1);
    view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
    assertThat(shadowOf(view).lastHapticFeedbackPerformed()).isEqualTo(HapticFeedbackConstants.LONG_PRESS);
  }

  @Test
  public void canAssertThatSuperDotOnLayoutWasCalledFromViewSubclasses() throws Exception {
    TestView2 view = new TestView2(setupActivity(Activity.class), 1111, 1112);
    assertThat(shadowOf(view).onLayoutWasCalled()).isFalse();
    view.onLayout(true, 1, 2, 3, 4);
    assertThat(shadowOf(view).onLayoutWasCalled()).isTrue();
  }

  @Test
  public void setScrolls_canBeAskedFor() throws Exception {
    view.setScrollX(234);
    view.setScrollY(544);
    assertThat(view.getScrollX()).isEqualTo(234);
    assertThat(view.getScrollY()).isEqualTo(544);
  }

  @Test
  public void setScrolls_firesOnScrollChanged() throws Exception {
    TestView testView = new TestView(buildActivity(Activity.class).create().get());
    testView.setScrollX(122);
    testView.setScrollY(150);
    testView.setScrollX(453);
    assertThat(testView.oldl).isEqualTo(122);
    testView.setScrollY(54);
    assertThat(testView.l).isEqualTo(453);
    assertThat(testView.t).isEqualTo(54);
    assertThat(testView.oldt).isEqualTo(150);
  }

  @Test
  public void layerType() throws Exception {
    assertThat(view.getLayerType()).isEqualTo(View.LAYER_TYPE_NONE);
    view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    assertThat(view.getLayerType()).isEqualTo(View.LAYER_TYPE_SOFTWARE);
  }

  private static class TestAnimation extends Animation {
  }

  private static class TouchableView extends View {
    MotionEvent event;

    public TouchableView(Context context) {
      super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
      this.event = event;
      return false;
    }
  }

  public static class TestView extends View {
    boolean onAnimationEndWasCalled;
    private int l;
    private int t;
    private int oldl;
    private int oldt;

    public TestView(Context context) {
      super(context);
    }

    @Override
    protected void onAnimationEnd() {
      super.onAnimationEnd();
      onAnimationEndWasCalled = true;
    }

    @Override
    public void onScrollChanged(int l, int t, int oldl, int oldt) {
      this.l = l;
      this.t = t;
      this.oldl = oldl;
      this.oldt = oldt;
    }
  }

  private static class TestView2 extends View {

    private int minWidth;
    private int minHeight;

    public TestView2(Context context, int minWidth, int minHeight) {
      super(context);
      this.minWidth = minWidth;
      this.minHeight = minHeight;
    }

    @Override
    public void onLayout(boolean changed, int l, int t, int r, int b) {
      super.onLayout(changed, l, t, r, b);
    }

    @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      setMeasuredDimension(minWidth, minHeight);
    }
  }

  @Test
  public void shouldCallOnAttachedToAndDetachedFromWindow() throws Exception {
    MyView parent = new MyView("parent", transcript);
    parent.addView(new MyView("child", transcript));
    assertThat(transcript).isEmpty();

    Activity activity = Robolectric.buildActivity(ContentViewActivity.class).setup().get();
    activity.getWindowManager().addView(parent, new WindowManager.LayoutParams(100, 100));
    shadowMainLooper().idle();
    assertThat(transcript).containsExactly("parent attached", "child attached");
    transcript.clear();

    parent.addView(new MyView("another child", transcript));
    assertThat(transcript).containsExactly("another child attached");
    transcript.clear();

    MyView temporaryChild = new MyView("temporary child", transcript);
    parent.addView(temporaryChild);
    assertThat(transcript).containsExactly("temporary child attached");
    transcript.clear();
    assertTrue(shadowOf(temporaryChild).isAttachedToWindow());

    parent.removeView(temporaryChild);
    assertThat(transcript).containsExactly("temporary child detached");
    assertFalse(shadowOf(temporaryChild).isAttachedToWindow());
  }

  @Test @Config(minSdk = JELLY_BEAN_MR2)
  public void getWindowId_shouldReturnValidObjectWhenAttached() throws Exception {
    MyView parent = new MyView("parent", transcript);
    MyView child = new MyView("child", transcript);
    parent.addView(child);

    assertThat(parent.getWindowId()).isNull();
    assertThat(child.getWindowId()).isNull();

    Activity activity = Robolectric.buildActivity(ContentViewActivity.class).create().get();
    activity.getWindowManager().addView(parent, new WindowManager.LayoutParams(100, 100));
    shadowMainLooper().idle();

    WindowId windowId = parent.getWindowId();
    assertThat(windowId).isNotNull();
    assertThat(child.getWindowId()).isSameInstanceAs(windowId);
    assertThat(child.getWindowId()).isEqualTo(windowId); // equals must work!

    MyView anotherChild = new MyView("another child", transcript);
    parent.addView(anotherChild);
    assertThat(anotherChild.getWindowId()).isEqualTo(windowId);

    parent.removeView(anotherChild);
    assertThat(anotherChild.getWindowId()).isNull();
  }

  // todo looks like this is flaky...
  @Test
  public void removeAllViews_shouldCallOnAttachedToAndDetachedFromWindow() throws Exception {
    MyView parent = new MyView("parent", transcript);
    Activity activity = Robolectric.buildActivity(ContentViewActivity.class).create().get();
    activity.getWindowManager().addView(parent, new WindowManager.LayoutParams(100, 100));

    parent.addView(new MyView("child", transcript));
    parent.addView(new MyView("another child", transcript));
    shadowMainLooper().idle();
    transcript.clear();
    parent.removeAllViews();
    shadowMainLooper().idle();
    assertThat(transcript).containsExactly("another child detached", "child detached");
  }

  @Test
  public void capturesOnSystemUiVisibilityChangeListener() throws Exception {
    TestView testView = new TestView(buildActivity(Activity.class).create().get());
    View.OnSystemUiVisibilityChangeListener changeListener = new View.OnSystemUiVisibilityChangeListener() {
      @Override
      public void onSystemUiVisibilityChange(int i) { }
    };
    testView.setOnSystemUiVisibilityChangeListener(changeListener);

    assertThat(changeListener).isEqualTo(shadowOf(testView).getOnSystemUiVisibilityChangeListener());
  }

  @Test
  public void capturesOnCreateContextMenuListener() throws Exception {
    TestView testView = new TestView(buildActivity(Activity.class).create().get());
    assertThat(shadowOf(testView).getOnCreateContextMenuListener()).isNull();

    View.OnCreateContextMenuListener createListener = new View.OnCreateContextMenuListener() {
      @Override
      public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {}
    };

    testView.setOnCreateContextMenuListener(createListener);
    assertThat(shadowOf(testView).getOnCreateContextMenuListener()).isEqualTo(createListener);

    testView.setOnCreateContextMenuListener(null);
    assertThat(shadowOf(testView).getOnCreateContextMenuListener()).isNull();
  }

  @Test
  public void capturesOnAttachStateChangeListeners() throws Exception {
    TestView testView = new TestView(buildActivity(Activity.class).create().get());
    assertThat(shadowOf(testView).getOnAttachStateChangeListeners()).isEmpty();

    View.OnAttachStateChangeListener attachListener1 =
        new View.OnAttachStateChangeListener() {
          @Override
          public void onViewAttachedToWindow(View v) {}

          @Override
          public void onViewDetachedFromWindow(View v) {}
        };

    View.OnAttachStateChangeListener attachListener2 =
        new View.OnAttachStateChangeListener() {
          @Override
          public void onViewAttachedToWindow(View v) {}

          @Override
          public void onViewDetachedFromWindow(View v) {}
        };

    testView.addOnAttachStateChangeListener(attachListener1);
    assertThat(shadowOf(testView).getOnAttachStateChangeListeners())
        .containsExactly(attachListener1);

    testView.addOnAttachStateChangeListener(attachListener2);
    assertThat(shadowOf(testView).getOnAttachStateChangeListeners())
        .containsExactly(attachListener1, attachListener2);

    testView.removeOnAttachStateChangeListener(attachListener2);
    assertThat(shadowOf(testView).getOnAttachStateChangeListeners())
        .containsExactly(attachListener1);

    testView.removeOnAttachStateChangeListener(attachListener1);
    assertThat(shadowOf(testView).getOnAttachStateChangeListeners()).isEmpty();
  }

  @Test
  public void setsGlobalVisibleRect() {
    Rect globalVisibleRect = new Rect();
    shadowOf(view).setGlobalVisibleRect(new Rect());
    assertThat(view.getGlobalVisibleRect(globalVisibleRect))
        .isFalse();
    assertThat(globalVisibleRect.isEmpty())
        .isTrue();
    assertThat(view.getGlobalVisibleRect(globalVisibleRect, new Point(1, 1)))
        .isFalse();
    assertThat(globalVisibleRect.isEmpty())
        .isTrue();

    shadowOf(view).setGlobalVisibleRect(new Rect(1, 2, 3, 4));
    assertThat(view.getGlobalVisibleRect(globalVisibleRect))
        .isTrue();
    assertThat(globalVisibleRect)
        .isEqualTo(new Rect(1, 2, 3, 4));
    assertThat(view.getGlobalVisibleRect(globalVisibleRect, new Point(1, 1)))
        .isTrue();
    assertThat(globalVisibleRect)
        .isEqualTo(new Rect(0, 1, 2, 3));
  }

  @Test
  public void usesDefaultGlobalVisibleRect() {

    final ActivityController<Activity> activityController = Robolectric.buildActivity(Activity.class);
    final Activity activity = activityController.get();
    TextView fooView = new TextView(activity);
    activity.setContentView(
        fooView,
        new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    activityController.setup();

    Rect globalVisibleRect = new Rect();
    assertThat(fooView.getGlobalVisibleRect(globalVisibleRect)).isTrue();
    assertThat(globalVisibleRect)
        .isEqualTo(new Rect(0, 25,
            DeviceConfig.DEFAULT_SCREEN_SIZE.width, DeviceConfig.DEFAULT_SCREEN_SIZE.height));
  }

  @Test
  public void performClick_addListener_sendsGlobalViewAction() {
    class GlobalListener implements View.OnClickListener {
      boolean receivedEvent = false;

      @Override
      public void onClick(View view) {
        receivedEvent = true;
      }
    }
    GlobalListener listener = new GlobalListener();

    ShadowView.addGlobalPerformClickListener(listener);
    view.performClick();

    assertThat(listener.receivedEvent).isTrue();
  }

  @Test
  public void performClick_removeListener_sendsGlobalViewAction() {
    class GlobalListener implements View.OnClickListener {
      boolean receivedEvent = false;

      @Override
      public void onClick(View view) {
        receivedEvent = true;
      }
    }
    GlobalListener listener = new GlobalListener();

    ShadowView.addGlobalPerformClickListener(listener);
    ShadowView.removeGlobalPerformClickListener(listener);
    view.performClick();

    assertThat(listener.receivedEvent).isFalse();
  }

  @Test
  public void performLongClick_addListener_sendsGlobalViewAction() {
    class GlobalListener implements View.OnLongClickListener {
      boolean receivedEvent = false;

      @Override
      public boolean onLongClick(View view) {
        receivedEvent = true;
        return true;
      }
    }
    GlobalListener listener = new GlobalListener();

    ShadowView.addGlobalPerformLongClickListener(listener);
    view.performLongClick();

    assertThat(listener.receivedEvent).isTrue();
  }

  @Test
  public void performLongClick_removeListener_sendsGlobalViewAction() {
    class GlobalListener implements View.OnLongClickListener {
      boolean receivedEvent = false;

      @Override
      public boolean onLongClick(View view) {
        receivedEvent = true;
        return true;
      }
    }
    GlobalListener listener = new GlobalListener();

    ShadowView.addGlobalPerformLongClickListener(listener);
    ShadowView.removeGlobalPerformLongClickListener(listener);
    view.performLongClick();

    assertThat(listener.receivedEvent).isFalse();
  }

  @Test
  public void reset_removesAllGlobalViewActionListeners() {
    class GlobalClickListener implements View.OnClickListener {
      boolean receivedEvent = false;

      @Override
      public void onClick(View view) {
        receivedEvent = true;
      }
    }
    class GlobalLongClickListener implements View.OnLongClickListener {
      boolean receivedEvent = false;

      @Override
      public boolean onLongClick(View view) {
        receivedEvent = true;
        return true;
      }
    }
    GlobalClickListener globalClickListener = new GlobalClickListener();
    GlobalLongClickListener globalLongClickListener = new GlobalLongClickListener();

    ShadowView.addGlobalPerformClickListener(globalClickListener);
    ShadowView.addGlobalPerformLongClickListener(globalLongClickListener);

    ShadowView.reset();

    // Should call both listeners since this calls sendAccessibilityEvent.
    view.performClick();

    assertThat(globalClickListener.receivedEvent).isFalse();
    assertThat(globalLongClickListener.receivedEvent).isFalse();
  }

  @Test
  public void getSourceLayoutResId() {
    View view = LayoutInflater.from(context).inflate(R.layout.edit_text, null, false);

    assertThat(shadowOf(view).getSourceLayoutResId()).isEqualTo(R.layout.edit_text);
  }

  public static class MyActivity extends Activity {
    public boolean called;

    @SuppressWarnings("UnusedDeclaration")
    public void clickMe(View view) {
      called = true;
    }
  }

  public static class MyView extends LinearLayout {
    private String name;
    private List<String> transcript;

    public MyView(String name, List<String> transcript) {
      super(ApplicationProvider.getApplicationContext());
      this.name = name;
      this.transcript = transcript;
    }

    @Override protected void onAttachedToWindow() {
      transcript.add(name + " attached");
      super.onAttachedToWindow();
    }

    @Override protected void onDetachedFromWindow() {
      transcript.add(name + " detached");
      super.onDetachedFromWindow();
    }
  }

  private static class ContentViewActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(new FrameLayout(this));
    }
  }
}