fragment/src/androidTest/java/androidx/fragment/app/FragmentViewTests.java
@Test
    public void testReplaceFragment() throws Throwable {
        FragmentTestUtil.setContentView(mActivityRule, R.layout.simple_container);
        final FragmentManager fm = mActivityRule.getActivity().getSupportFragmentManager();
        StrictViewFragment fragmentA = new StrictViewFragment();
        fragmentA.setLayoutId(R.layout.text_a);

        fm.beginTransaction()
                .add(R.id.fragmentContainer, fragmentA)
                .addToBackStack(null)
                .commit();
        FragmentTestUtil.executePendingTransactions(mActivityRule);

        assertNotNull(findViewById(R.id.textA));
        assertNull(findViewById(R.id.textB));
        assertNull(findViewById(R.id.textC));

        StrictViewFragment fragmentB = new StrictViewFragment();
        fragmentB.setLayoutId(R.layout.text_b);
        fm.beginTransaction()
                .add(R.id.fragmentContainer, fragmentB)
                .addToBackStack(null)
                .commit();
        FragmentTestUtil.executePendingTransactions(mActivityRule);
        assertNotNull(findViewById(R.id.textA));
        assertNotNull(findViewById(R.id.textB));
        assertNull(findViewById(R.id.textC));

        StrictViewFragment fragmentC = new StrictViewFragment();
        fragmentC.setLayoutId(R.layout.text_c);
        fm.beginTransaction()
                .replace(R.id.fragmentContainer, fragmentC)
                .addToBackStack(null)
                .commit();
        FragmentTestUtil.executePendingTransactions(mActivityRule);
        assertNull(findViewById(R.id.textA));
        assertNull(findViewById(R.id.textB));
        assertNotNull(findViewById(R.id.textC));
    }
// Test to prevent regressions in FragmentManager fragment replace method. See b/24693644
Test to prevent regressions in FragmentManager fragment replace method.
Test to prevent regressions in FragmentManager fragment replace method .
@Test
    public void testReplaceFragment() throws Throwable {
        FragmentTestUtil.setContentView(mActivityRule, R.layout.simple_container);
        final FragmentManager fm = mActivityRule.getActivity().getSupportFragmentManager();
        StrictViewFragment fragmentA = new StrictViewFragment();
        fragmentA.setLayoutId(R.layout.text_a);
        fm.beginTransaction()
                .add(R.id.fragmentContainer, fragmentA)
                .addToBackStack(null)
                .commit();
        FragmentTestUtil.executePendingTransactions(mActivityRule);
        assertNotNull(findViewById(R.id.textA));
        assertNull(findViewById(R.id.textB));
        assertNull(findViewById(R.id.textC));
        StrictViewFragment fragmentB = new StrictViewFragment();
        fragmentB.setLayoutId(R.layout.text_b);
        fm.beginTransaction()
                .add(R.id.fragmentContainer, fragmentB)
                .addToBackStack(null)
                .commit();
        FragmentTestUtil.executePendingTransactions(mActivityRule);
        assertNotNull(findViewById(R.id.textA));
        assertNotNull(findViewById(R.id.textB));
        assertNull(findViewById(R.id.textC));
        StrictViewFragment fragmentC = new StrictViewFragment();
        fragmentC.setLayoutId(R.layout.text_c);
        fm.beginTransaction()
                .replace(R.id.fragmentContainer, fragmentC)
                .addToBackStack(null)
                .commit();
        FragmentTestUtil.executePendingTransactions(mActivityRule);
        assertNull(findViewById(R.id.textA));
        assertNull(findViewById(R.id.textB));
        assertNotNull(findViewById(R.id.textC));
    }
@ Test public void testReplaceFragment ( ) throws Throwable { FragmentTestUtil . setContentView ( mActivityRule , R . layout . simple_container ) ; final FragmentManager fm = mActivityRule . getActivity ( ) . getSupportFragmentManager ( ) ; StrictViewFragment fragmentA = new StrictViewFragment ( ) ; fragmentA . setLayoutId ( R . layout . text_a ) ; fm . beginTransaction ( ) . add ( R . id . fragmentContainer , fragmentA ) . addToBackStack ( null ) . commit ( ) ; FragmentTestUtil . executePendingTransactions ( mActivityRule ) ; assertNotNull ( findViewById ( R . id . textA ) ) ; assertNull ( findViewById ( R . id . textB ) ) ; assertNull ( findViewById ( R . id . textC ) ) ; StrictViewFragment fragmentB = new StrictViewFragment ( ) ; fragmentB . setLayoutId ( R . layout . text_b ) ; fm . beginTransaction ( ) . add ( R . id . fragmentContainer , fragmentB ) . addToBackStack ( null ) . commit ( ) ; FragmentTestUtil . executePendingTransactions ( mActivityRule ) ; assertNotNull ( findViewById ( R . id . textA ) ) ; assertNotNull ( findViewById ( R . id . textB ) ) ; assertNull ( findViewById ( R . id . textC ) ) ; StrictViewFragment fragmentC = new StrictViewFragment ( ) ; fragmentC . setLayoutId ( R . layout . text_c ) ; fm . beginTransaction ( ) . replace ( R . id . fragmentContainer , fragmentC ) . addToBackStack ( null ) . commit ( ) ; FragmentTestUtil . executePendingTransactions ( mActivityRule ) ; assertNull ( findViewById ( R . id . textA ) ) ; assertNull ( findViewById ( R . id . textB ) ) ; assertNotNull ( findViewById ( R . id . textC ) ) ; }
Test to prevent regressions in FragmentManager fragment replace method.
Test to prevent regressions in FragmentManager fragment replace method .