FragmentController { @Nullable Fragment executeRequest(final FragmentRequest request) { this.assertNotDestroyed("EXECUTE REQUEST"); Fragment fragment = request.fragment; if (fragment == null) { String fragmentTag = request.tag; final int fragmentId = request.fragmentId; if (fragmentId == FragmentRequest.NO_ID) { switch (request.transaction) { case FragmentRequest.REMOVE: case FragmentRequest.SHOW: case FragmentRequest.HIDE: case FragmentRequest.ATTACH: case FragmentRequest.DETACH: fragment = manager.findFragmentByTag(fragmentTag); break; case FragmentRequest.ADD: case FragmentRequest.REPLACE: default: break; } } else { this.assertHasFactory(); if (!factory.isFragmentProvided(fragmentId)) { throw new IllegalArgumentException( "Cannot execute request for factory fragment. Current factory(" + factory.getClass() + ") " + "does not provide fragment for the requested id(" + fragmentId + ")!"); } if (fragmentTag == null) { fragmentTag = factory.createFragmentTag(fragmentId); } switch (request.transaction) { case FragmentRequest.REMOVE: case FragmentRequest.SHOW: case FragmentRequest.HIDE: case FragmentRequest.ATTACH: case FragmentRequest.DETACH: fragment = manager.findFragmentByTag(fragmentTag); break; case FragmentRequest.REPLACE: case FragmentRequest.ADD: default: fragment = factory.createFragment(fragmentId); if (fragment == null) { throw new IllegalArgumentException( "Cannot execute request for factory fragment. Current factory(" + factory.getClass() + ") is cheating. " + "FragmentFactory.isFragmentProvided(...) returned true, but FragmentFactory.createFragment(...) returned null!" ); } break; } request.tag = fragmentTag; } } if ((request.fragment = fragment) == null) { return null; } fragment = requestInterceptor == null ? null : requestInterceptor.interceptFragmentRequest(request); if (fragment == null) { fragment = onExecuteRequest(request); } notifyRequestExecuted(request); 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 testExecuteRequest() { final FragmentManager mockManager = mock(FragmentManager.class); final FragmentTransaction mockTransaction = mock(FragmentTransaction.class); final FragmentController.OnRequestListener mockListener = mock(FragmentController.OnRequestListener.class); final FragmentController controller = new FragmentController(mockManager); controller.setViewContainerId(TestActivity.CONTENT_VIEW_ID); controller.registerOnRequestListener(mockListener); when(mockManager.beginTransaction()).thenReturn(mockTransaction); final Fragment fragment = new TestFragment(); final FragmentRequest request = controller.newRequest(fragment); assertThat(controller.executeRequest(request), is(fragment)); verify(mockManager).beginTransaction(); verify(mockListener).onRequestExecuted(request); verifyNoMoreInteractions(mockListener); }
@Test(expected = IllegalStateException.class) public void testExecuteRequestWhenDestroyed() { createDestroyedController().executeRequest(new FragmentRequest(null, FragmentRequest.NO_ID)); }