fragment/src/androidTest/java/androidx/fragment/app/FragmentTransitionTest.java
@Test
    public void onMapSharedElementIn() throws Throwable {
        TransitionFragment fragment1 = setupInitialFragment();

        // Now do a transition to scene2
        final 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(mReorderingAllowed)
                .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);
    }
// Make sure that onMapSharedElement works to change the shared element target
Make sure that onMapSharedElement works to change the shared element target
Make sure that onMapSharedElement works to change the shared element target
@Test
    public void onMapSharedElementIn() throws Throwable {
        TransitionFragment fragment1 = setupInitialFragment();
        final 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(mReorderingAllowed)
                .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);
    }
@ Test public void onMapSharedElementIn ( ) throws Throwable { TransitionFragment fragment1 = setupInitialFragment ( ) ; final 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 ( mReorderingAllowed ) . 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 ) ; }
Make sure that onMapSharedElement works to change the shared element target
Make sure that onMapSharedElement works to change the shared element target
// Now do a transition to scene2