@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 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 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 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_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 }