fragment/src/androidTest/java/androidx/fragment/app/FragmentReorderingTest.java
@Test
    public void removeRedundantAdd() throws Throwable {
        final CountCallsFragment fragment1 = new CountCallsFragment();
        int id = mFM.beginTransaction()
                .add(R.id.fragmentContainer, fragment1)
                .addToBackStack(null)
                .setReorderingAllowed(true)
                .commit();
        FragmentTestUtil.executePendingTransactions(mActivityRule);
        assertEquals(1, fragment1.onCreateViewCount);

        mInstrumentation.runOnMainSync(new Runnable() {
            @Override
            public void run() {
                mFM.beginTransaction()
                        .remove(fragment1)
                        .addToBackStack(null)
                        .setReorderingAllowed(true)
                        .commit();
                mFM.beginTransaction()
                        .add(R.id.fragmentContainer, fragment1)
                        .addToBackStack(null)
                        .setReorderingAllowed(true)
                        .commit();
                mFM.executePendingTransactions();
            }
        });

        FragmentTestUtil.assertChildren(mContainer, fragment1);
        // should be optimized out
        assertEquals(1, fragment1.onCreateViewCount);

        mFM.popBackStack(id, 0);
        FragmentTestUtil.executePendingTransactions(mActivityRule);
        FragmentTestUtil.assertChildren(mContainer, fragment1);
        // optimize out going back, too
        assertEquals(1, fragment1.onCreateViewCount);
    }
// Ensure that removing and adding the same view results in no operation
Ensure that removing and adding the same view results in no operation
Ensure that removing and adding the same view results in no operation
@Test
    public void removeRedundantAdd() throws Throwable {
        final CountCallsFragment fragment1 = new CountCallsFragment();
        int id = mFM.beginTransaction()
                .add(R.id.fragmentContainer, fragment1)
                .addToBackStack(null)
                .setReorderingAllowed(true)
                .commit();
        FragmentTestUtil.executePendingTransactions(mActivityRule);
        assertEquals(1, fragment1.onCreateViewCount);
        mInstrumentation.runOnMainSync(new Runnable() {
            @Override
            public void run() {
                mFM.beginTransaction()
                        .remove(fragment1)
                        .addToBackStack(null)
                        .setReorderingAllowed(true)
                        .commit();
                mFM.beginTransaction()
                        .add(R.id.fragmentContainer, fragment1)
                        .addToBackStack(null)
                        .setReorderingAllowed(true)
                        .commit();
                mFM.executePendingTransactions();
            }
        });
        FragmentTestUtil.assertChildren(mContainer, fragment1);
        assertEquals(1, fragment1.onCreateViewCount);
        mFM.popBackStack(id, 0);
        FragmentTestUtil.executePendingTransactions(mActivityRule);
        FragmentTestUtil.assertChildren(mContainer, fragment1);
        assertEquals(1, fragment1.onCreateViewCount);
    }
@ Test public void removeRedundantAdd ( ) throws Throwable { final CountCallsFragment fragment1 = new CountCallsFragment ( ) ; int id = mFM . beginTransaction ( ) . add ( R . id . fragmentContainer , fragment1 ) . addToBackStack ( null ) . setReorderingAllowed ( true ) . commit ( ) ; FragmentTestUtil . executePendingTransactions ( mActivityRule ) ; assertEquals ( 1 , fragment1 . onCreateViewCount ) ; mInstrumentation . runOnMainSync ( new Runnable ( ) { @ Override public void run ( ) { mFM . beginTransaction ( ) . remove ( fragment1 ) . addToBackStack ( null ) . setReorderingAllowed ( true ) . commit ( ) ; mFM . beginTransaction ( ) . add ( R . id . fragmentContainer , fragment1 ) . addToBackStack ( null ) . setReorderingAllowed ( true ) . commit ( ) ; mFM . executePendingTransactions ( ) ; } } ) ; FragmentTestUtil . assertChildren ( mContainer , fragment1 ) ; assertEquals ( 1 , fragment1 . onCreateViewCount ) ; mFM . popBackStack ( id , 0 ) ; FragmentTestUtil . executePendingTransactions ( mActivityRule ) ; FragmentTestUtil . assertChildren ( mContainer , fragment1 ) ; assertEquals ( 1 , fragment1 . onCreateViewCount ) ; }
Ensure that removing and adding the same view results in no operation
Ensure that removing and adding the same view results in no operation
// should be optimized out // optimize out going back, too