fragment/src/androidTest/java/androidx/fragment/app/PostponedTransitionTest.java
@Test
    public void outOfOrderContainers() throws Throwable {
        final FragmentManager fm = mActivityRule.getActivity().getSupportFragmentManager();
        fm.beginTransaction()
                .remove(mBeginningFragment)
                .setReorderingAllowed(true)
                .commit();
        FragmentTestUtil.waitForExecution(mActivityRule);
        FragmentTestUtil.setContentView(mActivityRule, R.layout.double_container);

        TransitionFragment fragment1 = new PostponedFragment1();
        TransitionFragment fragment2 = new PostponedFragment1();

        fm.beginTransaction()
                .add(R.id.fragmentContainer1, fragment1)
                .add(R.id.fragmentContainer2, fragment2)
                .setReorderingAllowed(true)
                .commit();
        FragmentTestUtil.waitForExecution(mActivityRule);
        fragment1.startPostponedEnterTransition();
        fragment2.startPostponedEnterTransition();
        fragment1.waitForTransition();
        fragment2.waitForTransition();
        clearTargets(fragment1);
        clearTargets(fragment2);

        final View startBlue1 = fragment1.getView().findViewById(R.id.blueSquare);
        final View startBlue2 = fragment2.getView().findViewById(R.id.blueSquare);

        final TransitionFragment fragment3 = new PostponedFragment2();

        fm.beginTransaction()
                .addSharedElement(startBlue1, "blueSquare")
                .replace(R.id.fragmentContainer1, fragment3)
                .addToBackStack(null)
                .setReorderingAllowed(true)
                .commit();

        FragmentTestUtil.waitForExecution(mActivityRule);

        assertPostponedTransition(fragment1, fragment3, null);

        final TransitionFragment fragment4 = new PostponedFragment2();

        fm.beginTransaction()
                .addSharedElement(startBlue2, "blueSquare")
                .replace(R.id.fragmentContainer2, fragment4)
                .addToBackStack(null)
                .setReorderingAllowed(true)
                .commit();

        FragmentTestUtil.waitForExecution(mActivityRule);

        assertPostponedTransition(fragment1, fragment3, null);
        assertPostponedTransition(fragment2, fragment4, null);

        // start the postponed transition
        fragment4.startPostponedEnterTransition();

        // make sure only one ran
        assertForwardTransition(fragment2, fragment4);
        assertPostponedTransition(fragment1, fragment3, null);

        // start the postponed transition
        fragment3.startPostponedEnterTransition();

        // make sure it ran
        assertForwardTransition(fragment1, fragment3);

        // Pop back to fragment2 -- should be postponed
        FragmentTestUtil.popBackStackImmediate(mActivityRule);

        assertPostponedTransition(fragment4, fragment2, null);

        // Pop back to fragment1 -- also should be postponed
        FragmentTestUtil.popBackStackImmediate(mActivityRule);

        assertPostponedTransition(fragment4, fragment2, null);
        assertPostponedTransition(fragment3, fragment1, null);

        // start the postponed transition
        fragment1.startPostponedEnterTransition();

        // make sure it ran
        assertBackTransition(fragment3, fragment1);

        // but not the postponed one
        assertPostponedTransition(fragment4, fragment2, null);

        // start the postponed transition
        fragment2.startPostponedEnterTransition();

        // make sure it ran
        assertBackTransition(fragment4, fragment2);
    }
// The postponement can be started out-of-order
The postponement can be started out-of-order
The postponement can be started out - of - order
@Test
    public void outOfOrderContainers() throws Throwable {
        final FragmentManager fm = mActivityRule.getActivity().getSupportFragmentManager();
        fm.beginTransaction()
                .remove(mBeginningFragment)
                .setReorderingAllowed(true)
                .commit();
        FragmentTestUtil.waitForExecution(mActivityRule);
        FragmentTestUtil.setContentView(mActivityRule, R.layout.double_container);
        TransitionFragment fragment1 = new PostponedFragment1();
        TransitionFragment fragment2 = new PostponedFragment1();
        fm.beginTransaction()
                .add(R.id.fragmentContainer1, fragment1)
                .add(R.id.fragmentContainer2, fragment2)
                .setReorderingAllowed(true)
                .commit();
        FragmentTestUtil.waitForExecution(mActivityRule);
        fragment1.startPostponedEnterTransition();
        fragment2.startPostponedEnterTransition();
        fragment1.waitForTransition();
        fragment2.waitForTransition();
        clearTargets(fragment1);
        clearTargets(fragment2);
        final View startBlue1 = fragment1.getView().findViewById(R.id.blueSquare);
        final View startBlue2 = fragment2.getView().findViewById(R.id.blueSquare);
        final TransitionFragment fragment3 = new PostponedFragment2();
        fm.beginTransaction()
                .addSharedElement(startBlue1, "blueSquare")
                .replace(R.id.fragmentContainer1, fragment3)
                .addToBackStack(null)
                .setReorderingAllowed(true)
                .commit();
        FragmentTestUtil.waitForExecution(mActivityRule);
        assertPostponedTransition(fragment1, fragment3, null);
        final TransitionFragment fragment4 = new PostponedFragment2();
        fm.beginTransaction()
                .addSharedElement(startBlue2, "blueSquare")
                .replace(R.id.fragmentContainer2, fragment4)
                .addToBackStack(null)
                .setReorderingAllowed(true)
                .commit();
        FragmentTestUtil.waitForExecution(mActivityRule);
        assertPostponedTransition(fragment1, fragment3, null);
        assertPostponedTransition(fragment2, fragment4, null);
        fragment4.startPostponedEnterTransition();
        assertForwardTransition(fragment2, fragment4);
        assertPostponedTransition(fragment1, fragment3, null);
        fragment3.startPostponedEnterTransition();
        assertForwardTransition(fragment1, fragment3);
        FragmentTestUtil.popBackStackImmediate(mActivityRule);
        assertPostponedTransition(fragment4, fragment2, null);
        FragmentTestUtil.popBackStackImmediate(mActivityRule);
        assertPostponedTransition(fragment4, fragment2, null);
        assertPostponedTransition(fragment3, fragment1, null);
        fragment1.startPostponedEnterTransition();
        assertBackTransition(fragment3, fragment1);
        assertPostponedTransition(fragment4, fragment2, null);
        fragment2.startPostponedEnterTransition();
        assertBackTransition(fragment4, fragment2);
    }
@ Test public void outOfOrderContainers ( ) throws Throwable { final FragmentManager fm = mActivityRule . getActivity ( ) . getSupportFragmentManager ( ) ; fm . beginTransaction ( ) . remove ( mBeginningFragment ) . setReorderingAllowed ( true ) . commit ( ) ; FragmentTestUtil . waitForExecution ( mActivityRule ) ; FragmentTestUtil . setContentView ( mActivityRule , R . layout . double_container ) ; TransitionFragment fragment1 = new PostponedFragment1 ( ) ; TransitionFragment fragment2 = new PostponedFragment1 ( ) ; fm . beginTransaction ( ) . add ( R . id . fragmentContainer1 , fragment1 ) . add ( R . id . fragmentContainer2 , fragment2 ) . setReorderingAllowed ( true ) . commit ( ) ; FragmentTestUtil . waitForExecution ( mActivityRule ) ; fragment1 . startPostponedEnterTransition ( ) ; fragment2 . startPostponedEnterTransition ( ) ; fragment1 . waitForTransition ( ) ; fragment2 . waitForTransition ( ) ; clearTargets ( fragment1 ) ; clearTargets ( fragment2 ) ; final View startBlue1 = fragment1 . getView ( ) . findViewById ( R . id . blueSquare ) ; final View startBlue2 = fragment2 . getView ( ) . findViewById ( R . id . blueSquare ) ; final TransitionFragment fragment3 = new PostponedFragment2 ( ) ; fm . beginTransaction ( ) . addSharedElement ( startBlue1 , "blueSquare" ) . replace ( R . id . fragmentContainer1 , fragment3 ) . addToBackStack ( null ) . setReorderingAllowed ( true ) . commit ( ) ; FragmentTestUtil . waitForExecution ( mActivityRule ) ; assertPostponedTransition ( fragment1 , fragment3 , null ) ; final TransitionFragment fragment4 = new PostponedFragment2 ( ) ; fm . beginTransaction ( ) . addSharedElement ( startBlue2 , "blueSquare" ) . replace ( R . id . fragmentContainer2 , fragment4 ) . addToBackStack ( null ) . setReorderingAllowed ( true ) . commit ( ) ; FragmentTestUtil . waitForExecution ( mActivityRule ) ; assertPostponedTransition ( fragment1 , fragment3 , null ) ; assertPostponedTransition ( fragment2 , fragment4 , null ) ; fragment4 . startPostponedEnterTransition ( ) ; assertForwardTransition ( fragment2 , fragment4 ) ; assertPostponedTransition ( fragment1 , fragment3 , null ) ; fragment3 . startPostponedEnterTransition ( ) ; assertForwardTransition ( fragment1 , fragment3 ) ; FragmentTestUtil . popBackStackImmediate ( mActivityRule ) ; assertPostponedTransition ( fragment4 , fragment2 , null ) ; FragmentTestUtil . popBackStackImmediate ( mActivityRule ) ; assertPostponedTransition ( fragment4 , fragment2 , null ) ; assertPostponedTransition ( fragment3 , fragment1 , null ) ; fragment1 . startPostponedEnterTransition ( ) ; assertBackTransition ( fragment3 , fragment1 ) ; assertPostponedTransition ( fragment4 , fragment2 , null ) ; fragment2 . startPostponedEnterTransition ( ) ; assertBackTransition ( fragment4 , fragment2 ) ; }
The postponement can be started out-of-order
The postponement can be started out - of - order
// start the postponed transition // make sure only one ran // start the postponed transition // make sure it ran // Pop back to fragment2 -- should be postponed // Pop back to fragment1 -- also should be postponed // start the postponed transition // make sure it ran // but not the postponed one // start the postponed transition // make sure it ran