summaryrefslogtreecommitdiffstats
path: root/tests/fragment/src/android/fragment/cts/FragmentTransitionTest.java
blob: 6f63e981ea45cfd1793eda8e615c666200d865ba (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
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.fragment.cts;

import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.fail;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.SharedElementCallback;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.test.filters.MediumTest;
import android.support.test.rule.ActivityTestRule;
import android.transition.TransitionSet;
import android.view.View;

import com.android.compatibility.common.util.transition.TargetTracking;
import com.android.compatibility.common.util.transition.TrackingTransition;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.mockito.ArgumentCaptor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@MediumTest
@RunWith(Parameterized.class)
public class FragmentTransitionTest {
    private final boolean mReordered;
    private int mOnBackStackChangedTimes;
    private FragmentManager.OnBackStackChangedListener mOnBackStackChangedListener;

    @Parameterized.Parameters
    public static Object[] data() {
        return new Boolean[] {
                false, true
        };
    }

    @Rule
    public ActivityTestRule<FragmentTestActivity> mActivityRule =
            new ActivityTestRule<FragmentTestActivity>(FragmentTestActivity.class);

    private FragmentManager mFragmentManager;

    public FragmentTransitionTest(final boolean reordered) {
        mReordered = reordered;
    }

    @Before
    public void setup() throws Throwable {
        mFragmentManager = mActivityRule.getActivity().getFragmentManager();
        FragmentTestUtil.setContentView(mActivityRule, R.layout.simple_container);
        mOnBackStackChangedTimes = 0;
        mOnBackStackChangedListener = new FragmentManager.OnBackStackChangedListener() {
            @Override
            public void onBackStackChanged() {
                mOnBackStackChangedTimes++;
            }
        };
        mFragmentManager.addOnBackStackChangedListener(mOnBackStackChangedListener);
    }

    @After
    public void teardown() throws Throwable {
        mFragmentManager.removeOnBackStackChangedListener(mOnBackStackChangedListener);
        mOnBackStackChangedListener = null;
    }

    // Test that normal view transitions (enter, exit, reenter, return) run with
    // a single fragment.
    @Test
    public void enterExitTransitions() throws Throwable {
        // enter transition
        TransitionFragment fragment = setupInitialFragment();
        final View blue = findBlue();
        final View green = findBlue();

        // exit transition
        mFragmentManager.beginTransaction()
                .setReorderingAllowed(mReordered)
                .remove(fragment)
                .addToBackStack(null)
                .commit();

        fragment.waitForTransition();
        verifyAndClearTransition(fragment.exitTransition, null, green, blue);
        verifyNoOtherTransitions(fragment);
        assertEquals(2, mOnBackStackChangedTimes);

        // reenter transition
        FragmentTestUtil.popBackStackImmediate(mActivityRule);
        fragment.waitForTransition();
        final View green2 = findGreen();
        final View blue2 = findBlue();
        verifyAndClearTransition(fragment.reenterTransition, null, green2, blue2);
        verifyNoOtherTransitions(fragment);
        assertEquals(3, mOnBackStackChangedTimes);

        // return transition
        FragmentTestUtil.popBackStackImmediate(mActivityRule);
        fragment.waitForTransition();
        verifyAndClearTransition(fragment.returnTransition, null, green2, blue2);
        verifyNoOtherTransitions(fragment);
        assertEquals(4, mOnBackStackChangedTimes);
    }

    // Test that shared elements transition from one fragment to the next
    // and back during pop.
    @Test
    public void sharedElement() throws Throwable {
        TransitionFragment fragment1 = setupInitialFragment();

        // Now do a transition to scene2
        TransitionFragment fragment2 = new TransitionFragment();
        fragment2.setLayoutId(R.layout.scene2);

        verifyTransition(fragment1, fragment2, "blueSquare");
        assertEquals(2, mOnBackStackChangedTimes);

        // Now pop the back stack
        verifyPopTransition(1, fragment2, fragment1);
        assertEquals(3, mOnBackStackChangedTimes);
    }

    // Test that shared element transitions through multiple fragments work together
    @Test
    public void intermediateFragment() throws Throwable {
        TransitionFragment fragment1 = setupInitialFragment();

        final TransitionFragment fragment2 = new TransitionFragment();
        fragment2.setLayoutId(R.layout.scene3);

        verifyTransition(fragment1, fragment2, "shared");

        final TransitionFragment fragment3 = new TransitionFragment();
        fragment3.setLayoutId(R.layout.scene2);

        verifyTransition(fragment2, fragment3, "blueSquare");

        // Should transfer backwards when popping multiple:
        verifyPopTransition(2, fragment3, fragment1, fragment2);
    }

    // Adding/removing the same fragment multiple times shouldn't mess anything up
    @Test
    public void removeAdded() throws Throwable {
        final TransitionFragment fragment1 = setupInitialFragment();

        final View startBlue = findBlue();
        final View startGreen = findGreen();

        final TransitionFragment fragment2 = new TransitionFragment();
        fragment2.setLayoutId(R.layout.scene2);

        mActivityRule.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mFragmentManager.beginTransaction()
                        .setReorderingAllowed(mReordered)
                        .replace(R.id.fragmentContainer, fragment2)
                        .replace(R.id.fragmentContainer, fragment1)
                        .replace(R.id.fragmentContainer, fragment2)
                        .addToBackStack(null)
                        .commit();
            }
        });
        FragmentTestUtil.waitForExecution(mActivityRule);
        assertEquals(2, mOnBackStackChangedTimes);

        // should be a normal transition from fragment1 to fragment2
        fragment1.waitForTransition();
        fragment2.waitForTransition();
        FragmentTestUtil.waitForExecution(mActivityRule);

        final View endBlue = findBlue();
        final View endGreen = findGreen();
        verifyAndClearTransition(fragment1.exitTransition, null, startBlue, startGreen);
        verifyAndClearTransition(fragment2.enterTransition, null, endBlue, endGreen);
        verifyNoOtherTransitions(fragment1);
        verifyNoOtherTransitions(fragment2);

        // Pop should also do the same thing
        FragmentTestUtil.popBackStackImmediate(mActivityRule);
        assertEquals(3, mOnBackStackChangedTimes);

        fragment1.waitForTransition();
        fragment2.waitForTransition();
        FragmentTestUtil.waitForExecution(mActivityRule);

        final View popBlue = findBlue();
        final View popGreen = findGreen();
        verifyAndClearTransition(fragment1.reenterTransition, null, popBlue, popGreen);
        verifyAndClearTransition(fragment2.returnTransition, null, endBlue, endGreen);
        verifyNoOtherTransitions(fragment1);
        verifyNoOtherTransitions(fragment2);
    }

    // Make sure that shared elements on two different fragment containers don't interact
    @Test
    public void crossContainer() throws Throwable {
        FragmentTestUtil.setContentView(mActivityRule, R.layout.double_container);
        TransitionFragment fragment1 = new TransitionFragment();
        fragment1.setLayoutId(R.layout.scene1);
        TransitionFragment fragment2 = new TransitionFragment();
        fragment2.setLayoutId(R.layout.scene1);
        mFragmentManager.beginTransaction()
                .setReorderingAllowed(mReordered)
                .add(R.id.fragmentContainer1, fragment1)
                .add(R.id.fragmentContainer2, fragment2)
                .addToBackStack(null)
                .commit();
        FragmentTestUtil.waitForExecution(mActivityRule);
        assertEquals(1, mOnBackStackChangedTimes);

        fragment1.waitForTransition();
        final View greenSquare1 = findViewById(fragment1, R.id.greenSquare);
        final View blueSquare1 = findViewById(fragment1, R.id.blueSquare);
        verifyAndClearTransition(fragment1.enterTransition, null, greenSquare1, blueSquare1);
        verifyNoOtherTransitions(fragment1);
        fragment2.waitForTransition();
        final View greenSquare2 = findViewById(fragment2, R.id.greenSquare);
        final View blueSquare2 = findViewById(fragment2, R.id.blueSquare);
        verifyAndClearTransition(fragment2.enterTransition, null, greenSquare2, blueSquare2);
        verifyNoOtherTransitions(fragment2);

        // Make sure the correct transitions are run when the target names
        // are different in both shared elements. We may fool the system.
        verifyCrossTransition(false, fragment1, fragment2);

        // Make sure the correct transitions are run when the source names
        // are different in both shared elements. We may fool the system.
        verifyCrossTransition(true, fragment1, fragment2);
    }

    // Make sure that onSharedElementStart and onSharedElementEnd are called
    @Test
    public void callStartEndWithSharedElements() throws Throwable {
        TransitionFragment fragment1 = setupInitialFragment();

        // Now do a transition to scene2
        TransitionFragment fragment2 = new TransitionFragment();
        fragment2.setLayoutId(R.layout.scene2);

        SharedElementCallback enterCallback = mock(SharedElementCallback.class);
        fragment2.setEnterSharedElementCallback(enterCallback);

        final View startBlue = findBlue();

        verifyTransition(fragment1, fragment2, "blueSquare");

        ArgumentCaptor<List> names = ArgumentCaptor.forClass(List.class);
        ArgumentCaptor<List> views = ArgumentCaptor.forClass(List.class);
        ArgumentCaptor<List> snapshots = ArgumentCaptor.forClass(List.class);
        verify(enterCallback).onSharedElementStart(names.capture(), views.capture(),
                snapshots.capture());
        assertEquals(1, names.getValue().size());
        assertEquals(1, views.getValue().size());
        assertNull(snapshots.getValue());
        assertEquals("blueSquare", names.getValue().get(0));
        assertEquals(startBlue, views.getValue().get(0));

        final View endBlue = findBlue();

        verify(enterCallback).onSharedElementEnd(names.capture(), views.capture(),
                snapshots.capture());
        assertEquals(1, names.getValue().size());
        assertEquals(1, views.getValue().size());
        assertNull(snapshots.getValue());
        assertEquals("blueSquare", names.getValue().get(0));
        assertEquals(endBlue, views.getValue().get(0));

        // Now pop the back stack
        reset(enterCallback);
        verifyPopTransition(1, fragment2, fragment1);

        verify(enterCallback).onSharedElementStart(names.capture(), views.capture(),
                snapshots.capture());
        assertEquals(1, names.getValue().size());
        assertEquals(1, views.getValue().size());
        assertNull(snapshots.getValue());
        assertEquals("blueSquare", names.getValue().get(0));
        assertEquals(endBlue, views.getValue().get(0));

        final View reenterBlue = findBlue();

        verify(enterCallback).onSharedElementEnd(names.capture(), views.capture(),
                snapshots.capture());
        assertEquals(1, names.getValue().size());
        assertEquals(1, views.getValue().size());
        assertNull(snapshots.getValue());
        assertEquals("blueSquare", names.getValue().get(0));
        assertEquals(reenterBlue, views.getValue().get(0));
    }

    // Make sure that onMapSharedElement works to change the shared element going out
    @Test
    public void onMapSharedElementOut() throws Throwable {
        TransitionFragment fragment1 = setupInitialFragment();

        // Now do a transition to scene2
        TransitionFragment fragment2 = new TransitionFragment();
        fragment2.setLayoutId(R.layout.scene2);

        final View startBlue = findBlue();
        final View startGreen = findGreen();

        final Rect startGreenBounds = getBoundsOnScreen(startGreen);

        SharedElementCallback mapOut = new SharedElementCallback() {
            @Override
            public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
                assertEquals(1, names.size());
                assertEquals("blueSquare", names.get(0));
                assertEquals(1, sharedElements.size());
                assertEquals(startBlue, sharedElements.get("blueSquare"));
                sharedElements.put("blueSquare", startGreen);
            }
        };
        fragment1.setExitSharedElementCallback(mapOut);

        mFragmentManager.beginTransaction()
                .addSharedElement(startBlue, "blueSquare")
                .replace(R.id.fragmentContainer, fragment2)
                .setReorderingAllowed(mReordered)
                .addToBackStack(null)
                .commit();
        FragmentTestUtil.waitForExecution(mActivityRule);

        fragment1.waitForTransition();
        fragment2.waitForTransition();

        final View endBlue = findBlue();
        final Rect endBlueBounds = getBoundsOnScreen(endBlue);

        verifyAndClearTransition(fragment2.sharedElementEnter, startGreenBounds, startGreen,
                endBlue);

        SharedElementCallback mapBack = new SharedElementCallback() {
            @Override
            public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
                assertEquals(1, names.size());
                assertEquals("blueSquare", names.get(0));
                assertEquals(1, sharedElements.size());
                final View expectedBlue = findViewById(fragment1, R.id.blueSquare);
                assertEquals(expectedBlue, sharedElements.get("blueSquare"));
                final View greenSquare = findViewById(fragment1, R.id.greenSquare);
                sharedElements.put("blueSquare", greenSquare);
            }
        };
        fragment1.setExitSharedElementCallback(mapBack);

        FragmentTestUtil.popBackStackImmediate(mActivityRule);

        fragment1.waitForTransition();
        fragment2.waitForTransition();

        final View reenterGreen = findGreen();
        verifyAndClearTransition(fragment2.sharedElementReturn, endBlueBounds, endBlue,
                reenterGreen);
    }

    // Make sure that onMapSharedElement works to change the shared element target
    @Test
    public void onMapSharedElementIn() throws Throwable {
        TransitionFragment fragment1 = setupInitialFragment();

        // Now do a transition to scene2
        TransitionFragment fragment2 = new TransitionFragment();
        fragment2.setLayoutId(R.layout.scene2);

        final View startBlue = findBlue();
        final Rect startBlueBounds = getBoundsOnScreen(startBlue);

        SharedElementCallback mapIn = new SharedElementCallback() {
            @Override
            public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
                assertEquals(1, names.size());
                assertEquals("blueSquare", names.get(0));
                assertEquals(1, sharedElements.size());
                final View blueSquare = findViewById(fragment2, R.id.blueSquare);
                assertEquals(blueSquare, sharedElements.get("blueSquare"));
                final View greenSquare = findViewById(fragment2, R.id.greenSquare);
                sharedElements.put("blueSquare", greenSquare);
            }
        };
        fragment2.setEnterSharedElementCallback(mapIn);

        mFragmentManager.beginTransaction()
                .addSharedElement(startBlue, "blueSquare")
                .replace(R.id.fragmentContainer, fragment2)
                .setReorderingAllowed(mReordered)
                .addToBackStack(null)
                .commit();
        FragmentTestUtil.waitForExecution(mActivityRule);

        fragment1.waitForTransition();
        fragment2.waitForTransition();

        final View endGreen = findGreen();
        final View endBlue = findBlue();
        final Rect endGreenBounds = getBoundsOnScreen(endGreen);

        verifyAndClearTransition(fragment2.sharedElementEnter, startBlueBounds, startBlue,
                endGreen);

        SharedElementCallback mapBack = new SharedElementCallback() {
            @Override
            public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
                assertEquals(1, names.size());
                assertEquals("blueSquare", names.get(0));
                assertEquals(1, sharedElements.size());
                assertEquals(endBlue, sharedElements.get("blueSquare"));
                sharedElements.put("blueSquare", endGreen);
            }
        };
        fragment2.setEnterSharedElementCallback(mapBack);

        FragmentTestUtil.popBackStackImmediate(mActivityRule);

        fragment1.waitForTransition();
        fragment2.waitForTransition();

        final View reenterBlue = findBlue();
        verifyAndClearTransition(fragment2.sharedElementReturn, endGreenBounds, endGreen,
                reenterBlue);
    }

    // Ensure that shared element transitions that have targets properly target the views
    @Test
    public void complexSharedElementTransition() throws Throwable {
        TransitionFragment fragment1 = setupInitialFragment();

        // Now do a transition to scene2
        ComplexTransitionFragment fragment2 = new ComplexTransitionFragment();
        fragment2.setLayoutId(R.layout.scene2);

        final View startBlue = findBlue();
        final View startGreen = findGreen();
        final Rect startBlueBounds = getBoundsOnScreen(startBlue);

        mFragmentManager.beginTransaction()
                .addSharedElement(startBlue, "blueSquare")
                .addSharedElement(startGreen, "greenSquare")
                .replace(R.id.fragmentContainer, fragment2)
                .addToBackStack(null)
                .commit();
        FragmentTestUtil.waitForExecution(mActivityRule);
        assertEquals(2, mOnBackStackChangedTimes);

        fragment1.waitForTransition();
        fragment2.waitForTransition();

        final View endBlue = findBlue();
        final View endGreen = findGreen();
        final Rect endBlueBounds = getBoundsOnScreen(endBlue);

        verifyAndClearTransition(fragment2.sharedElementEnterTransition1, startBlueBounds,
                startBlue, endBlue);
        verifyAndClearTransition(fragment2.sharedElementEnterTransition2, startBlueBounds,
                startGreen, endGreen);

        // Now see if it works when popped
        FragmentTestUtil.popBackStackImmediate(mActivityRule);
        assertEquals(3, mOnBackStackChangedTimes);

        fragment1.waitForTransition();
        fragment2.waitForTransition();

        final View reenterBlue = findBlue();
        final View reenterGreen = findGreen();

        verifyAndClearTransition(fragment2.sharedElementReturnTransition1, endBlueBounds,
                endBlue, reenterBlue);
        verifyAndClearTransition(fragment2.sharedElementReturnTransition2, endBlueBounds,
                endGreen, reenterGreen);
    }

    // Ensure that after transitions have executed that they don't have any targets or other
    // unfortunate modifications.
    @Test
    public void transitionsEndUnchanged() throws Throwable {
        TransitionFragment fragment1 = setupInitialFragment();

        // Now do a transition to scene2
        TransitionFragment fragment2 = new TransitionFragment();
        fragment2.setLayoutId(R.layout.scene2);

        verifyTransition(fragment1, fragment2, "blueSquare");
        assertEquals(0, fragment1.exitTransition.getTargets().size());
        assertEquals(0, fragment2.sharedElementEnter.getTargets().size());
        assertEquals(0, fragment2.enterTransition.getTargets().size());
        assertNull(fragment1.exitTransition.getEpicenterCallback());
        assertNull(fragment2.enterTransition.getEpicenterCallback());
        assertNull(fragment2.sharedElementEnter.getEpicenterCallback());

        // Now pop the back stack
        verifyPopTransition(1, fragment2, fragment1);

        assertEquals(0, fragment2.returnTransition.getTargets().size());
        assertEquals(0, fragment2.sharedElementReturn.getTargets().size());
        assertEquals(0, fragment1.reenterTransition.getTargets().size());
        assertNull(fragment2.returnTransition.getEpicenterCallback());
        assertNull(fragment2.sharedElementReturn.getEpicenterCallback());
        assertNull(fragment2.reenterTransition.getEpicenterCallback());
    }

    // Ensure that transitions are done when a fragment is shown and hidden
    @Test
    public void showHideTransition() throws Throwable {
        TransitionFragment fragment1 = setupInitialFragment();
        TransitionFragment fragment2 = new TransitionFragment();
        fragment2.setLayoutId(R.layout.scene2);

        final View startBlue = findBlue();
        final View startGreen = findGreen();

        mFragmentManager.beginTransaction()
                .setReorderingAllowed(mReordered)
                .add(R.id.fragmentContainer, fragment2)
                .hide(fragment1)
                .addToBackStack(null)
                .commit();

        FragmentTestUtil.waitForExecution(mActivityRule);
        fragment1.waitForTransition();
        fragment2.waitForTransition();

        final View endGreen = findViewById(fragment2, R.id.greenSquare);
        final View endBlue = findViewById(fragment2, R.id.blueSquare);

        assertEquals(View.GONE, fragment1.getView().getVisibility());
        assertEquals(View.VISIBLE, startGreen.getVisibility());
        assertEquals(View.VISIBLE, startBlue.getVisibility());

        verifyAndClearTransition(fragment1.exitTransition, null, startGreen, startBlue);
        verifyNoOtherTransitions(fragment1);

        verifyAndClearTransition(fragment2.enterTransition, null, endGreen, endBlue);
        verifyNoOtherTransitions(fragment2);

        FragmentTestUtil.popBackStackImmediate(mActivityRule);

        FragmentTestUtil.waitForExecution(mActivityRule);
        fragment1.waitForTransition();
        fragment2.waitForTransition();

        verifyAndClearTransition(fragment1.reenterTransition, null, startGreen, startBlue);
        verifyNoOtherTransitions(fragment1);

        assertEquals(View.VISIBLE, fragment1.getView().getVisibility());
        assertEquals(View.VISIBLE, startGreen.getVisibility());
        assertEquals(View.VISIBLE, startBlue.getVisibility());

        verifyAndClearTransition(fragment2.returnTransition, null, endGreen, endBlue);
        verifyNoOtherTransitions(fragment2);
    }

    // Ensure that transitions are done when a fragment is attached and detached
    @Test
    public void attachDetachTransition() throws Throwable {
        TransitionFragment fragment1 = setupInitialFragment();
        TransitionFragment fragment2 = new TransitionFragment();
        fragment2.setLayoutId(R.layout.scene2);

        final View startBlue = findBlue();
        final View startGreen = findGreen();

        mFragmentManager.beginTransaction()
                .setReorderingAllowed(mReordered)
                .add(R.id.fragmentContainer, fragment2)
                .detach(fragment1)
                .addToBackStack(null)
                .commit();

        FragmentTestUtil.waitForExecution(mActivityRule);

        final View endGreen = findViewById(fragment2, R.id.greenSquare);
        final View endBlue = findViewById(fragment2, R.id.blueSquare);

        verifyAndClearTransition(fragment1.exitTransition, null, startGreen, startBlue);
        verifyNoOtherTransitions(fragment1);

        verifyAndClearTransition(fragment2.enterTransition, null, endGreen, endBlue);
        verifyNoOtherTransitions(fragment2);

        FragmentTestUtil.popBackStackImmediate(mActivityRule);

        FragmentTestUtil.waitForExecution(mActivityRule);

        final View reenterBlue = findBlue();
        final View reenterGreen = findGreen();

        verifyAndClearTransition(fragment1.reenterTransition, null, reenterGreen, reenterBlue);
        verifyNoOtherTransitions(fragment1);

        verifyAndClearTransition(fragment2.returnTransition, null, endGreen, endBlue);
        verifyNoOtherTransitions(fragment2);
    }

    // Ensure that shared element without matching transition name doesn't error out
    @Test
    public void sharedElementMismatch() throws Throwable {
        final TransitionFragment fragment1 = setupInitialFragment();

        // Now do a transition to scene2
        TransitionFragment fragment2 = new TransitionFragment();
        fragment2.setLayoutId(R.layout.scene2);

        final View startBlue = findBlue();
        final View startGreen = findGreen();
        final Rect startBlueBounds = getBoundsOnScreen(startBlue);

        mFragmentManager.beginTransaction()
                .addSharedElement(startBlue, "fooSquare")
                .replace(R.id.fragmentContainer, fragment2)
                .setReorderingAllowed(mReordered)
                .addToBackStack(null)
                .commit();
        FragmentTestUtil.waitForExecution(mActivityRule);

        fragment1.waitForTransition();
        fragment2.waitForTransition();

        final View endBlue = findBlue();
        final View endGreen = findGreen();

        if (mReordered) {
            verifyAndClearTransition(fragment1.exitTransition, null, startGreen, startBlue);
        } else {
            verifyAndClearTransition(fragment1.exitTransition, startBlueBounds, startGreen);
            verifyAndClearTransition(fragment2.sharedElementEnter, startBlueBounds, startBlue);
        }
        verifyNoOtherTransitions(fragment1);

        verifyAndClearTransition(fragment2.enterTransition, null, endGreen, endBlue);
        verifyNoOtherTransitions(fragment2);
    }

    // Ensure that using the same source or target shared element results in an exception.
    @Test
    public void sharedDuplicateTargetNames() throws Throwable {
        setupInitialFragment();

        final View startBlue = findBlue();
        final View startGreen = findGreen();

        FragmentTransaction ft = mFragmentManager.beginTransaction();
        ft.addSharedElement(startBlue, "blueSquare");
        try {
            ft.addSharedElement(startGreen, "blueSquare");
            fail("Expected IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            // expected
        }

        try {
            ft.addSharedElement(startBlue, "greenSquare");
            fail("Expected IllegalArgumentException");
        } catch (IllegalArgumentException e) {
            // expected
        }
    }

    // Test that invisible fragment views don't participate in transitions
    @Test
    public void invisibleNoTransitions() throws Throwable {
        if (!mReordered) {
            return; // only reordered transitions can avoid interaction
        }
        // enter transition
        TransitionFragment fragment = new InvisibleFragment();
        fragment.setLayoutId(R.layout.scene1);
        mFragmentManager.beginTransaction()
                .setReorderingAllowed(mReordered)
                .add(R.id.fragmentContainer, fragment)
                .addToBackStack(null)
                .commit();
        FragmentTestUtil.waitForExecution(mActivityRule);
        fragment.waitForNoTransition();
        verifyNoOtherTransitions(fragment);

        // exit transition
        mFragmentManager.beginTransaction()
                .setReorderingAllowed(mReordered)
                .remove(fragment)
                .addToBackStack(null)
                .commit();

        fragment.waitForNoTransition();
        verifyNoOtherTransitions(fragment);

        // reenter transition
        FragmentTestUtil.popBackStackImmediate(mActivityRule);
        fragment.waitForNoTransition();
        verifyNoOtherTransitions(fragment);

        // return transition
        FragmentTestUtil.popBackStackImmediate(mActivityRule);
        fragment.waitForNoTransition();
        verifyNoOtherTransitions(fragment);
    }

    // No crash when transitioning a shared element and there is no shared element transition.
    @Test
    public void noSharedElementTransition() throws Throwable {
        TransitionFragment fragment1 = setupInitialFragment();

        final View startBlue = findBlue();
        final View startGreen = findGreen();
        final Rect startBlueBounds = getBoundsOnScreen(startBlue);

        TransitionFragment fragment2 = new TransitionFragment();
        fragment2.setLayoutId(R.layout.scene2);

        mFragmentManager.beginTransaction()
                .setReorderingAllowed(mReordered)
                .addSharedElement(startBlue, "blueSquare")
                .replace(R.id.fragmentContainer, fragment2)
                .addToBackStack(null)
                .commit();

        fragment1.waitForTransition();
        fragment2.waitForTransition();
        final View midGreen = findGreen();
        final View midBlue = findBlue();
        final Rect midBlueBounds = getBoundsOnScreen(midBlue);
        verifyAndClearTransition(fragment1.exitTransition, startBlueBounds, startGreen);
        verifyAndClearTransition(fragment2.sharedElementEnter, startBlueBounds, startBlue, midBlue);
        verifyAndClearTransition(fragment2.enterTransition, midBlueBounds, midGreen);
        verifyNoOtherTransitions(fragment1);
        verifyNoOtherTransitions(fragment2);

        final TransitionFragment fragment3 = new TransitionFragment();
        fragment3.setLayoutId(R.layout.scene3);

        mActivityRule.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mFragmentManager.popBackStack();
                mFragmentManager.beginTransaction()
                        .setReorderingAllowed(mReordered)
                        .replace(R.id.fragmentContainer, fragment3)
                        .addToBackStack(null)
                        .commit();
            }
        });

        // This shouldn't give an error.
        FragmentTestUtil.executePendingTransactions(mActivityRule);

        fragment2.waitForTransition();
        // It does not transition properly for ordered transactions, though.
        if (mReordered) {
            verifyAndClearTransition(fragment2.returnTransition, null, midGreen, midBlue);
            final View endGreen = findGreen();
            final View endBlue = findBlue();
            final View endRed = findRed();
            verifyAndClearTransition(fragment3.enterTransition, null, endGreen, endBlue, endRed);
            verifyNoOtherTransitions(fragment2);
            verifyNoOtherTransitions(fragment3);
        } else {
            // fragment3 doesn't get a transition since it conflicts with the pop transition
            verifyNoOtherTransitions(fragment3);
            // Everything else is just doing its best. Reordered transactions can't handle
            // multiple transitions acting together except for popping multiple together.
        }
    }

    private TransitionFragment setupInitialFragment() throws Throwable {
        TransitionFragment fragment1 = new TransitionFragment();
        fragment1.setLayoutId(R.layout.scene1);
        mFragmentManager.beginTransaction()
                .setReorderingAllowed(mReordered)
                .add(R.id.fragmentContainer, fragment1)
                .addToBackStack(null)
                .commit();
        FragmentTestUtil.waitForExecution(mActivityRule);
        assertEquals(1, mOnBackStackChangedTimes);
        fragment1.waitForTransition();
        final View blueSquare1 = findBlue();
        final View greenSquare1 = findGreen();
        verifyAndClearTransition(fragment1.enterTransition, null, blueSquare1, greenSquare1);
        verifyNoOtherTransitions(fragment1);
        return fragment1;
    }

    private View findViewById(Fragment fragment, int id) {
        return fragment.getView().findViewById(id);
    }

    private View findGreen() {
        return mActivityRule.getActivity().findViewById(R.id.greenSquare);
    }

    private View findBlue() {
        return mActivityRule.getActivity().findViewById(R.id.blueSquare);
    }

    private View findRed() {
        return mActivityRule.getActivity().findViewById(R.id.redSquare);
    }

    private void verifyAndClearTransition(TargetTracking transition, Rect epicenter,
            View... expected) {
        if (epicenter == null) {
            assertNull(transition.getCapturedEpicenter());
        } else {
            assertEquals(epicenter, transition.getCapturedEpicenter());
        }
        ArrayList<View> targets = transition.getTrackedTargets();
        String errorMessage = "Expected: [" + expected.length + "] {" +
                Arrays.stream(expected).map(v -> v.toString()).collect(Collectors.joining(", ")) +
                "}, but got: [" + targets.size() + "] {" +
                targets.stream().map(v -> v.toString()).collect(Collectors.joining(", ")) +
                "}";
        assertEquals(errorMessage, expected.length, targets.size());
        for (View view : expected) {
            assertTrue(errorMessage, targets.contains(view));
        }
        transition.clearTargets();
    }

    private void verifyNoOtherTransitions(TransitionFragment fragment) {
        assertEquals(0, fragment.enterTransition.targets.size());
        assertEquals(0, fragment.exitTransition.targets.size());
        assertEquals(0, fragment.reenterTransition.targets.size());
        assertEquals(0, fragment.returnTransition.targets.size());
        assertEquals(0, fragment.sharedElementEnter.targets.size());
        assertEquals(0, fragment.sharedElementReturn.targets.size());
    }

    private void verifyTransition(TransitionFragment from, TransitionFragment to,
            String sharedElementName) throws Throwable {
        final int startOnBackStackChanged = mOnBackStackChangedTimes;
        final View startBlue = findBlue();
        final View startGreen = findGreen();
        final View startRed = findRed();

        final Rect startBlueRect = getBoundsOnScreen(startBlue);

        mFragmentManager.beginTransaction()
                .setReorderingAllowed(mReordered)
                .addSharedElement(startBlue, sharedElementName)
                .replace(R.id.fragmentContainer, to)
                .addToBackStack(null)
                .commit();

        FragmentTestUtil.waitForExecution(mActivityRule);
        assertEquals(startOnBackStackChanged + 1, mOnBackStackChangedTimes);

        to.waitForTransition();
        final View endGreen = findGreen();
        final View endBlue = findBlue();
        final View endRed = findRed();
        final Rect endBlueRect = getBoundsOnScreen(endBlue);

        if (startRed != null) {
            verifyAndClearTransition(from.exitTransition, startBlueRect, startGreen, startRed);
        } else {
            verifyAndClearTransition(from.exitTransition, startBlueRect, startGreen);
        }
        verifyNoOtherTransitions(from);

        if (endRed != null) {
            verifyAndClearTransition(to.enterTransition, endBlueRect, endGreen, endRed);
        } else {
            verifyAndClearTransition(to.enterTransition, endBlueRect, endGreen);
        }
        verifyAndClearTransition(to.sharedElementEnter, startBlueRect, startBlue, endBlue);
        verifyNoOtherTransitions(to);
    }

    private void verifyCrossTransition(boolean swapSource,
            TransitionFragment from1, TransitionFragment from2) throws Throwable {
        final int startNumOnBackStackChanged = mOnBackStackChangedTimes;
        final int changesPerOperation = mReordered ? 1 : 2;
        final TransitionFragment to1 = new TransitionFragment();
        to1.setLayoutId(R.layout.scene2);
        final TransitionFragment to2 = new TransitionFragment();
        to2.setLayoutId(R.layout.scene2);

        final View fromExit1 = findViewById(from1, R.id.greenSquare);
        final View fromShared1 = findViewById(from1, R.id.blueSquare);
        final Rect fromSharedRect1 = getBoundsOnScreen(fromShared1);

        final int fromExitId2 = swapSource ? R.id.blueSquare : R.id.greenSquare;
        final int fromSharedId2 = swapSource ? R.id.greenSquare : R.id.blueSquare;
        final View fromExit2 = findViewById(from2, fromExitId2);
        final View fromShared2 = findViewById(from2, fromSharedId2);
        final Rect fromSharedRect2 = getBoundsOnScreen(fromShared2);

        final String sharedElementName = swapSource ? "blueSquare" : "greenSquare";

        mActivityRule.runOnUiThread(() -> {
            mFragmentManager.beginTransaction()
                    .setReorderingAllowed(mReordered)
                    .addSharedElement(fromShared1, "blueSquare")
                    .replace(R.id.fragmentContainer1, to1)
                    .addToBackStack(null)
                    .commit();
            mFragmentManager.beginTransaction()
                    .setReorderingAllowed(mReordered)
                    .addSharedElement(fromShared2, sharedElementName)
                    .replace(R.id.fragmentContainer2, to2)
                    .addToBackStack(null)
                    .commit();
        });
        FragmentTestUtil.waitForExecution(mActivityRule);

        assertEquals(startNumOnBackStackChanged + changesPerOperation, mOnBackStackChangedTimes);

        from1.waitForTransition();
        from2.waitForTransition();
        to1.waitForTransition();
        to2.waitForTransition();

        final View toEnter1 = findViewById(to1, R.id.greenSquare);
        final View toShared1 = findViewById(to1, R.id.blueSquare);
        final Rect toSharedRect1 = getBoundsOnScreen(toShared1);

        final View toEnter2 = findViewById(to2, fromSharedId2);
        final View toShared2 = findViewById(to2, fromExitId2);
        final Rect toSharedRect2 = getBoundsOnScreen(toShared2);

        verifyAndClearTransition(from1.exitTransition, fromSharedRect1, fromExit1);
        verifyAndClearTransition(from2.exitTransition, fromSharedRect2, fromExit2);
        verifyNoOtherTransitions(from1);
        verifyNoOtherTransitions(from2);

        verifyAndClearTransition(to1.enterTransition, toSharedRect1, toEnter1);
        verifyAndClearTransition(to2.enterTransition, toSharedRect2, toEnter2);
        verifyAndClearTransition(to1.sharedElementEnter, fromSharedRect1, fromShared1, toShared1);
        verifyAndClearTransition(to2.sharedElementEnter, fromSharedRect2, fromShared2, toShared2);
        verifyNoOtherTransitions(to1);
        verifyNoOtherTransitions(to2);

        // Now pop it back
        mActivityRule.runOnUiThread(() -> {
            mFragmentManager.popBackStack();
            mFragmentManager.popBackStack();
        });
        FragmentTestUtil.waitForExecution(mActivityRule);
        assertEquals(startNumOnBackStackChanged + changesPerOperation + 1,
                mOnBackStackChangedTimes);

        from1.waitForTransition();
        from2.waitForTransition();
        to1.waitForTransition();
        to2.waitForTransition();

        final View returnEnter1 = findViewById(from1, R.id.greenSquare);
        final View returnShared1 = findViewById(from1, R.id.blueSquare);

        final View returnEnter2 = findViewById(from2, fromExitId2);
        final View returnShared2 = findViewById(from2, fromSharedId2);

        verifyAndClearTransition(to1.returnTransition, toSharedRect1, toEnter1);
        verifyAndClearTransition(to2.returnTransition, toSharedRect2, toEnter2);
        verifyAndClearTransition(to1.sharedElementReturn, toSharedRect1, toShared1, returnShared1);
        verifyAndClearTransition(to2.sharedElementReturn, toSharedRect2, toShared2, returnShared2);
        verifyNoOtherTransitions(to1);
        verifyNoOtherTransitions(to2);

        verifyAndClearTransition(from1.reenterTransition, fromSharedRect1, returnEnter1);
        verifyAndClearTransition(from2.reenterTransition, fromSharedRect2, returnEnter2);
        verifyNoOtherTransitions(from1);
        verifyNoOtherTransitions(from2);
    }

    private void verifyPopTransition(final int numPops, TransitionFragment from,
            TransitionFragment to, TransitionFragment... others) throws Throwable {
        final int startOnBackStackChanged = mOnBackStackChangedTimes;
        final View startBlue = findBlue();
        final View startGreen = findGreen();
        final View startRed = findRed();
        final Rect startSharedRect = getBoundsOnScreen(startBlue);

        mActivityRule.runOnUiThread(() -> {
            for (int i = 0; i < numPops; i++) {
                mFragmentManager.popBackStack();
            }
        });
        FragmentTestUtil.waitForExecution(mActivityRule);
        assertEquals(startOnBackStackChanged + 1, mOnBackStackChangedTimes);

        to.waitForTransition();
        final View endGreen = findGreen();
        final View endBlue = findBlue();
        final View endRed = findRed();
        final Rect endSharedRect = getBoundsOnScreen(endBlue);

        if (startRed != null) {
            verifyAndClearTransition(from.returnTransition, startSharedRect, startGreen, startRed);
        } else {
            verifyAndClearTransition(from.returnTransition, startSharedRect, startGreen);
        }
        verifyAndClearTransition(from.sharedElementReturn, startSharedRect, startBlue, endBlue);
        verifyNoOtherTransitions(from);

        if (endRed != null) {
            verifyAndClearTransition(to.reenterTransition, endSharedRect, endGreen, endRed);
        } else {
            verifyAndClearTransition(to.reenterTransition, endSharedRect, endGreen);
        }
        verifyNoOtherTransitions(to);

        if (others != null) {
            for (TransitionFragment fragment : others) {
                verifyNoOtherTransitions(fragment);
            }
        }
    }

    private static Rect getBoundsOnScreen(View view) {
        final int[] loc = new int[2];
        view.getLocationOnScreen(loc);
        return new Rect(loc[0], loc[1], loc[0] + view.getWidth(), loc[1] + view.getHeight());
    }

    public static class ComplexTransitionFragment extends TransitionFragment {
        public final TrackingTransition sharedElementEnterTransition1 = new TrackingTransition();
        public final TrackingTransition sharedElementEnterTransition2 = new TrackingTransition();
        public final TrackingTransition sharedElementReturnTransition1 = new TrackingTransition();
        public final TrackingTransition sharedElementReturnTransition2 = new TrackingTransition();

        public final TransitionSet sharedElementEnterTransition = new TransitionSet()
                .addTransition(sharedElementEnterTransition1)
                .addTransition(sharedElementEnterTransition2);
        public final TransitionSet sharedElementReturnTransition = new TransitionSet()
                .addTransition(sharedElementReturnTransition1)
                .addTransition(sharedElementReturnTransition2);

        public ComplexTransitionFragment() {
            sharedElementEnterTransition1.addTarget(R.id.blueSquare);
            sharedElementEnterTransition2.addTarget(R.id.greenSquare);
            sharedElementReturnTransition1.addTarget(R.id.blueSquare);
            sharedElementReturnTransition2.addTarget(R.id.greenSquare);
            setSharedElementEnterTransition(sharedElementEnterTransition);
            setSharedElementReturnTransition(sharedElementReturnTransition);
        }
    }

    public static class InvisibleFragment extends TransitionFragment {
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            view.setVisibility(View.INVISIBLE);
            super.onViewCreated(view, savedInstanceState);
        }
    }
}