@Test public void startFragment_shouldPassSavedInstanceStateToOnCreateMethodOfFragment() throws Exception { shadowOf(fragment).setSavedInstanceState(new Bundle()); manager.addFragment(CONTAINER_VIEW_ID, null, fragment, true); manager.startFragment(fragment); assertTrue(fragment.onActivityCreated_savedInstanceState != null); }
@Test public void getFragment_whenBundleSavedByShadowFragmentActivity_shouldGetFragmentByTagFromBundle() throws Exception { manager.addFragment(CONTAINER_VIEW_ID, "fragment tag", fragment, true); Bundle outState = new Bundle(); shadowOf(activity).onSaveInstanceState(outState); Fragment retrievedFragment = manager.getFragment(outState, "fragment tag"); assertEquals(TestFragment.class, retrievedFragment.getClass()); }
@Test public void startFragment_shouldInsertTheFragmentViewIntoTheContainerView() throws Exception { manager.addFragment(CONTAINER_VIEW_ID, null, fragment, false); manager.startFragment(fragment); View fragmentViewParent = (View) activity.findViewById(TestFragment.FRAGMENT_VIEW_ID).getParent(); assertThat( activity.findViewById(TestFragment.FRAGMENT_VIEW_ID), sameInstance(fragment.onCreateViewReturnValue)); assertThat(fragmentViewParent, sameInstance((View) containerView)); }
@Test public void startFragment_shouldCallLifecycleMethods() throws Exception { manager.addFragment(View.NO_ID, null, fragment, false); fragment.transcript.clear(); manager.startFragment(fragment); fragment.transcript.assertEventsSoFar( "onCreateView", "onViewCreated", "onActivityCreated", "onStart"); assertEquals(fragment.onCreateViewInflater, activity.getLayoutInflater()); assertNotNull(fragment.getView()); }
@Test public void addFragment_shouldPassTheSavedInstanceStateToOnCreate() throws Exception { Bundle bundle = new Bundle(); shadowOf(fragment).setSavedInstanceState(bundle); manager.addFragment(0, null, fragment, false); assertSame(bundle, fragment.onCreateSavedInstanceState); }
@Test public void addFragment_shouldCallLifecycleMethods() throws Exception { manager.addFragment(View.NO_ID, null, fragment, false); fragment.transcript.assertEventsSoFar("onAttach", "onCreate"); assertSame(activity, fragment.onAttachActivity); assertSame(activity, fragment.getActivity()); }
@Test public void addFragmentWithReplace_shouldEmptyTheContainer() throws Exception { containerView.addView(new Button(activity)); assertEquals(1, containerView.getChildCount()); manager.addFragment(CONTAINER_VIEW_ID, null, fragment, true); assertEquals(1, containerView.getChildCount()); }
@Test public void shouldUseFragmentTransactionToAddFragmentToDummyActivityIfSubjectIsAnFragment() { when(testCase.getTestSubjectClassType()).thenReturn(RoboFragment.class); when(reflectionUtils.classIsOfAssignableForm(RoboFragment.class, RoboFragment.class)) .thenReturn(true); when(reflectionUtils.createObjectProxy(RoboFragment.class, testFieldInjector)) .thenReturn(fragment); when(reflectionUtils.objectIsOfAnyType(fragment, RoboFragment.class)).thenReturn(true); testFieldInjector.setupTestCase(); TestFragmentManager fragmentManager = (TestFragmentManager) shadowOf((FragmentActivity) testFieldInjector.getCurrentContextForTest()) .getSupportFragmentManager(); assertThat(fragmentManager.getCommittedTransactions().size(), is(1)); assertThat( (RoboFragment) fragmentManager.getCommittedTransactions().get(0).getFragment(), is(fragment)); }
@Test public void getCommittedTransactions_shouldReturnListOfOnlyCommittedTransactions() throws Exception { assertTrue(manager.getCommittedTransactions().isEmpty()); FragmentTransaction transaction = manager.beginTransaction(); assertTrue(manager.getCommittedTransactions().isEmpty()); transaction.add(new Fragment(), "tag"); transaction.commit(); assertEquals(1, manager.getCommittedTransactions().size()); assertSame(transaction, manager.getCommittedTransactions().get(0)); FragmentTransaction anotherTransaction = manager.beginTransaction(); anotherTransaction.add(new Fragment(), "tag"); anotherTransaction.commit(); assertEquals(2, manager.getCommittedTransactions().size()); assertSame(anotherTransaction, manager.getCommittedTransactions().get(1)); }
@Test public void addFragment_shouldSetTheFragmentsTag() throws Exception { manager.addFragment(View.NO_ID, "expected tag", fragment, false); assertThat(fragment.getTag(), equalTo("expected tag")); }
@Test public void addFragment_shouldSetTheFragmentsView() throws Exception { manager.addFragment(View.NO_ID, null, fragment, false); assertThat(fragment.getView(), sameInstance(fragment.onCreateViewReturnValue)); }
@Test public void shouldFindFragmentByTag() throws Exception { manager.addFragment(CONTAINER_VIEW_ID, "tag1", fragment, false); assertThat(manager.findFragmentByTag("tag1"), sameInstance((Fragment) fragment)); }
@Test public void addFragment_shouldSetFragmentsActivity() throws Exception { manager.addFragment(0, null, fragment, false); assertSame(activity, fragment.getActivity()); }
@Test public void addFragmentWithReplace_withNoContainer_shouldNotThrow() throws Exception { manager.addFragment(0, null, fragment, true); // pass }