FragmentController { public void unregisterOnBackStackChangeListener(@NonNull final OnBackStackChangeListener listener) { if (backStackChangeListeners != null) backStackChangeListeners.remove(listener); } 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 testUnregisterOnBackStackChangeListener() { final FragmentActivity activity = Robolectric.buildActivity(TestCompatActivity.class).create().start().resume().get(); final FragmentController controller = new FragmentController(activity.getSupportFragmentManager()); final FragmentController.OnBackStackChangeListener firstMockListener = mock(FragmentController.OnBackStackChangeListener.class); final FragmentController.OnBackStackChangeListener secondMockListener = mock(FragmentController.OnBackStackChangeListener.class); controller.registerOnBackStackChangeListener(firstMockListener); controller.registerOnBackStackChangeListener(secondMockListener); controller.unregisterOnBackStackChangeListener(firstMockListener); final FragmentManager.BackStackEntry mockBackStackEntry = mock(FragmentManager.BackStackEntry.class); controller.notifyBackStackEntryChange(mockBackStackEntry, true); verifyZeroInteractions(firstMockListener); verify(secondMockListener).onFragmentsBackStackChanged(mockBackStackEntry, true); controller.unregisterOnBackStackChangeListener(secondMockListener); controller.notifyBackStackEntryChange(mockBackStackEntry, false); verifyNoMoreInteractions(secondMockListener); }
@Test public void testUnregisterOnBackStackChangeListenerNotRegistered() { final FragmentActivity activity = Robolectric.buildActivity(TestCompatActivity.class).create().start().resume().get(); final FragmentController controller = new FragmentController(activity.getSupportFragmentManager()); final FragmentController.OnBackStackChangeListener mockListener = mock(FragmentController.OnBackStackChangeListener.class); controller.unregisterOnBackStackChangeListener(mockListener); }