FragmentController { @SuppressWarnings("ConstantConditions") @NonNull protected Fragment onExecuteRequest(@NonNull final FragmentRequest request) { if (manager.isDestroyed()) { throw new IllegalStateException("Cannot execute fragment request in context of activity that has been already destroyed!"); } if (request.transaction == FragmentRequest.REPLACE && !request.hasFlag(FragmentRequest.REPLACE_SAME)) { final Fragment existingFragment = manager.findFragmentByTag(request.tag); if (existingFragment != null) { FragmentsLogging.d(TAG, "Fragment with tag(" + request.tag + ") is already displayed or it is in the back-stack."); return existingFragment; } } final Fragment fragment = request.fragment; final FragmentTransaction transaction = createTransaction(request); if (request.hasFlag(FragmentRequest.ADD_TO_BACK_STACK)) { FragmentsLogging.d(TAG, "Fragment(" + fragment + ") will be added into back-stack under the tag(" + fragment.getTag() + ")."); } if (request.hasFlag(FragmentRequest.ALLOW_STATE_LOSS)) { transaction.commitAllowingStateLoss(); } else { transaction.commit(); } if (request.hasFlag(FragmentRequest.IMMEDIATE)) { manager.executePendingTransactions(); } return fragment; } 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; }
@SuppressLint("CommitTransaction") @Test public void testOnExecuteRequest() { final FragmentManager mockManager = mock(FragmentManager.class); final FragmentTransaction mockTransaction = mock(FragmentTransaction.class); final FragmentController controller = new FragmentController(mockManager); controller.setViewContainerId(TestActivity.CONTENT_VIEW_ID); when(mockManager.beginTransaction()).thenReturn(mockTransaction); final Fragment fragment = new TestFragment(); final FragmentRequest request = controller.newRequest(fragment).transaction(FragmentRequest.ADD); assertThat(controller.onExecuteRequest(request), is(fragment)); verify(mockManager).beginTransaction(); verify(mockTransaction).commit(); }