package org.robolectric.shadows.support.v4;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.widget.LinearLayout;
import org.robolectric.Robolectric;

/**
 * Utilities for creating Fragments for testing.
 */
public class SupportFragmentTestUtil {

  public static void startFragment(Fragment fragment) {
    buildSupportFragmentManager(FragmentUtilActivity.class)
        .beginTransaction().add(fragment, null).commit();
  }

  public static void startFragment(Fragment fragment, Class<? extends FragmentActivity> fragmentActivityClass) {
    buildSupportFragmentManager(fragmentActivityClass)
        .beginTransaction().add(fragment, null).commit();
  }

  public static void startVisibleFragment(Fragment fragment) {
    buildSupportFragmentManager(FragmentUtilActivity.class)
        .beginTransaction().add(1, fragment, null).commit();
  }

  public static void startVisibleFragment(Fragment fragment, Class<? extends FragmentActivity> fragmentActivityClass, int containerViewId) {
    buildSupportFragmentManager(fragmentActivityClass)
        .beginTransaction().add(containerViewId, fragment, null).commit();
  }

  private static FragmentManager buildSupportFragmentManager(Class<? extends FragmentActivity> fragmentActivityClass) {
    FragmentActivity activity = Robolectric.setupActivity(fragmentActivityClass);
    return activity.getSupportFragmentManager();
  }

  private static class FragmentUtilActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      LinearLayout view = new LinearLayout(this);
      view.setId(1);

      setContentView(view);
    }
  }
}

@@ -531,36 +531,39 @@
 nt, null).commit
+Now
 ();%0A
-
   %7D%0A%0A  public st
@@ -765,32 +765,35 @@
 nt, null).commit
+Now
 ();%0A  %7D%0A%0A  publi
@@ -957,32 +957,35 @@
 nt, null).commit
+Now
 ();%0A  %7D%0A%0A  publi
@@ -1165,32 +1165,32 @@
 tActivityClass)%0A
-
         .beginTr
@@ -1244,16 +1244,19 @@
 ).commit
+Now
 ();%0A  %7D%0A

Change SupportFragmentTestUtil to use commitNow() instead of commit()