fragment/src/androidTest/java/androidx/fragment/app/FragmentTransitionTest.java
@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(mReorderingAllowed)
                .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() {
                FragmentManager fm = mActivityRule.getActivity().getSupportFragmentManager();
                fm.popBackStack();
                fm.beginTransaction()
                        .setReorderingAllowed(mReorderingAllowed)
                        .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 (mReorderingAllowed) {
            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. Ordered transactions can't handle
            // multiple transitions acting together except for popping multiple together.
        }
    }
// No crash when transitioning a shared element and there is no shared element transition.
No crash when transitioning a shared element and there is no shared element transition.
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(mReorderingAllowed)
                .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() {
                FragmentManager fm = mActivityRule.getActivity().getSupportFragmentManager();
                fm.popBackStack();
                fm.beginTransaction()
                        .setReorderingAllowed(mReorderingAllowed)
                        .replace(R.id.fragmentContainer, fragment3)
                        .addToBackStack(null)
                        .commit();
            }
        });
        FragmentTestUtil.executePendingTransactions(mActivityRule);
        fragment2.waitForTransition();
        if (mReorderingAllowed) {
            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 {
            verifyNoOtherTransitions(fragment3);
        }
    }
@ 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 ( mReorderingAllowed ) . 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 ( ) { FragmentManager fm = mActivityRule . getActivity ( ) . getSupportFragmentManager ( ) ; fm . popBackStack ( ) ; fm . beginTransaction ( ) . setReorderingAllowed ( mReorderingAllowed ) . replace ( R . id . fragmentContainer , fragment3 ) . addToBackStack ( null ) . commit ( ) ; } } ) ; FragmentTestUtil . executePendingTransactions ( mActivityRule ) ; fragment2 . waitForTransition ( ) ; if ( mReorderingAllowed ) { 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 { verifyNoOtherTransitions ( fragment3 ) ; } }
No crash when transitioning a shared element and there is no shared element transition.
No crash when transitioning a shared element and there is no shared element transition .
// This shouldn't give an error. // It does not transition properly for ordered transactions, though. // fragment3 doesn't get a transition since it conflicts with the pop transition // Everything else is just doing its best. Ordered transactions can't handle // multiple transitions acting together except for popping multiple together.