fragment/src/androidTest/java/androidx/fragment/app/FragmentTransitionTest.java
@Test
    public void onMapSharedElementOut() 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 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(mReorderingAllowed)
                .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 going out
Make sure that onMapSharedElement works to change the shared element going out
Make sure that onMapSharedElement works to change the shared element going out
@Test
    public void onMapSharedElementOut() throws Throwable {
        final TransitionFragment fragment1 = setupInitialFragment();
        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(mReorderingAllowed)
                .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);
    }
@ Test public void onMapSharedElementOut ( ) throws Throwable { final TransitionFragment fragment1 = setupInitialFragment ( ) ; 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 ( mReorderingAllowed ) . 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 going out
Make sure that onMapSharedElement works to change the shared element going out
// Now do a transition to scene2