@Before public void setUp() throws Exception { activity = new TestFragmentActivity(); activity.onCreate(null); manager = (TestFragmentManager) activity.getSupportFragmentManager(); fragment = new TestFragment(); containerView = (ViewGroup) activity.findViewById(CONTAINER_VIEW_ID); }
private F startFragment(F fragment) { FragmentTransaction transaction = mActivity.getFragmentManager().beginTransaction(); transaction.add(R.id.activity_test_fragment_linearlayout, fragment, "tag"); transaction.commit(); getInstrumentation().waitForIdleSync(); F frag = (F) mActivity.getFragmentManager().findFragmentByTag("tag"); return frag; }
@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()); }