FragmentController { public boolean clearBackStackImmediate() { this.assertNotDestroyed("CLEAR BACK STACK IMMEDIATE"); boolean popped = false; final int n = manager.getBackStackEntryCount(); for (int i = 0; i < n; i++) { if (manager.popBackStackImmediate() && !popped) { popped = true; } } return popped; } FragmentController(@NonNull final FragmentActivity parentActivity);  FragmentController(@NonNull final Fragment parentFragment);  FragmentController(@NonNull final FragmentManager fragmentManager);  FragmentController(@Nullable final Context context, @NonNull final FragmentManager fragmentManager); @NonNull FragmentManager getFragmentManager(); void setViewContainerId(@IdRes final int containerId); @IdRes int getViewContainerId(); void setFactory(@Nullable final FragmentFactory factory); boolean hasFactory(); @Nullable FragmentFactory getFactory(); void setRequestInterceptor(@Nullable final FragmentRequestInterceptor interceptor); void registerOnRequestListener(@NonNull final OnRequestListener listener); void unregisterOnRequestListener(@NonNull final OnRequestListener listener); void registerOnBackStackChangeListener(@NonNull final OnBackStackChangeListener listener); void unregisterOnBackStackChangeListener(@NonNull final OnBackStackChangeListener listener); @NonNull final FragmentRequest newRequest(); @NonNull final FragmentRequest newRequest(@NonNull final Fragment fragment); @NonNull final FragmentRequest newRequest(final int fragmentId); @SuppressWarnings("NewApi") @NonNull FragmentTransaction createTransaction(@NonNull final FragmentRequest request); @Nullable Fragment findCurrentFragment(); @Nullable Fragment findFragmentByFactoryId(final int factoryFragmentId); boolean hasBackStackEntries(); @Nullable FragmentManager.BackStackEntry getTopBackStackEntry(); void clearBackStack(); boolean clearBackStackImmediate(); final void destroy(); static final String FRAGMENT_TAG; static final int NO_CONTAINER_ID; }
@Test public void testClearBackStackImmediate() { final FragmentManager mockManager = mock(FragmentManager.class); final FragmentController controller = new FragmentController(mockManager); when(mockManager.getBackStackEntryCount()).thenReturn(5); when(mockManager.popBackStackImmediate()).thenReturn(true); assertThat(controller.clearBackStackImmediate(), is(true)); verify(mockManager, times(2)).getBackStackEntryCount(); verify(mockManager, times(5)).popBackStackImmediate(); }
@Test public void testClearBackStackImmediateWhenManagerDoesNotPops() { final FragmentManager mockManager = mock(FragmentManager.class); final FragmentController controller = new FragmentController(mockManager); when(mockManager.getBackStackEntryCount()).thenReturn(5); when(mockManager.popBackStackImmediate()).thenReturn(false); assertThat(controller.clearBackStackImmediate(), is(false)); verify(mockManager, times(2)).getBackStackEntryCount(); verify(mockManager, times(5)).popBackStackImmediate(); }
@Test public void testClearBackStackImmediateOnEmptyStack() { final FragmentManager mockManager = mock(FragmentManager.class); final FragmentController controller = new FragmentController(mockManager); when(mockManager.getBackStackEntryCount()).thenReturn(0); assertThat(controller.clearBackStackImmediate(), is(false)); verify(mockManager, times(2)).getBackStackEntryCount(); }
@Test(expected = IllegalStateException.class) public void testClearBackStackImmediateWhenDestroyed() { createDestroyedController().clearBackStackImmediate(); }