FragmentController { @SuppressWarnings("NewApi") @NonNull public FragmentTransaction createTransaction(@NonNull final FragmentRequest request) { this.assertNotDestroyed("CREATE TRANSACTION"); final FragmentTransaction transaction = manager.beginTransaction(); final Fragment fragment = request.fragment; if (request.arguments != null) { fragment.setArguments(request.arguments); } if (request.transition != null) { if (context == null || FragmentUtils.willBeCustomAnimationsPlayed(context)) { transaction.setCustomAnimations( request.transition.getIncomingAnimation(), request.transition.getOutgoingAnimation(), request.transition.getIncomingBackStackAnimation(), request.transition.getOutgoingBackStackAnimation() ); } } else if (request.transitionStyle != FragmentRequest.NO_STYLE) { transaction.setTransitionStyle(request.transitionStyle); } switch (request.transaction) { case FragmentRequest.REPLACE: if (request.viewContainerId == NO_CONTAINER_ID) { throw new IllegalArgumentException("Cannot create REPLACE transaction. No view container id specified!"); } transaction.replace(request.viewContainerId, fragment, request.tag); break; case FragmentRequest.ADD: if (request.viewContainerId == NO_CONTAINER_ID) { throw new IllegalArgumentException("Cannot create ADD transaction. No view container id specified!"); } transaction.add(request.viewContainerId, fragment, request.tag); break; case FragmentRequest.REMOVE: transaction.remove(fragment); break; case FragmentRequest.SHOW: transaction.show(fragment); break; case FragmentRequest.HIDE: transaction.hide(fragment); break; case FragmentRequest.ATTACH: transaction.attach(fragment); break; case FragmentRequest.DETACH: transaction.detach(fragment); break; default: throw new IllegalArgumentException("Unsupported transaction type(" + request.transaction + ") specified for the fragment request!"); } attachTransitionsToFragment(request, fragment); if (request.sharedElements != null && !request.sharedElements.isEmpty()) { final List<Pair<View, String>> elements = request.sharedElements; for (final Pair<View, String> pair : elements) { if (pair.first == null || pair.second == null) { FragmentsLogging.i(TAG, "Skipping invalid shared element pair(view: " + pair.first + ", name: " + pair.second + ")."); continue; } if (ViewCompat.getTransitionName(pair.first) == null) { ViewCompat.setTransitionName(pair.first, pair.second); } transaction.addSharedElement(pair.first, pair.second); } } if (request.hasFlag(FragmentRequest.ADD_TO_BACK_STACK)) { transaction.addToBackStack(fragment.getTag()); } return transaction; } 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 testCreateTransaction() { final FragmentManager mockManager = mock(FragmentManager.class); final FragmentTransaction mockTransaction = mock(FragmentTransaction.class); final Fragment mockFragment = mock(TestFragment.class); final FragmentController controller = new FragmentController(mockManager); controller.setViewContainerId(TestActivity.CONTENT_VIEW_ID); final FragmentRequest request = controller.newRequest(mockFragment); when(mockManager.beginTransaction()).thenReturn(mockTransaction); final FragmentTransaction transaction = controller.createTransaction(request); assertThat(transaction, is(notNullValue())); verify(mockFragment, times(0)).setArguments(any(Bundle.class)); verify(mockTransaction, times(0)).setCustomAnimations(anyInt(), anyInt(), anyInt(), anyInt()); verify(mockTransaction, times(0)).setCustomAnimations(anyInt(), anyInt()); verify(mockTransaction, times(0)).setTransitionStyle(anyInt()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { verify(mockTransaction, times(0)).addSharedElement(any(View.class), anyString()); } verify(mockTransaction, times(0)).addToBackStack(anyString()); }