Home | History | Annotate | Download | only in v4
      1 package org.robolectric.shadows.support.v4;
      2 
      3 import android.os.Bundle;
      4 import android.support.v4.app.Fragment;
      5 import android.support.v4.app.FragmentActivity;
      6 import android.support.v4.app.FragmentManager;
      7 import android.widget.LinearLayout;
      8 import org.robolectric.Robolectric;
      9 
     10 /**
     11  * Utilities for creating Fragments for testing.
     12  *
     13  * @deprecated Please use {@link SupportFragmentController} instead.
     14  */
     15 @Deprecated
     16 public class SupportFragmentTestUtil {
     17 
     18   public static void startFragment(Fragment fragment) {
     19     buildSupportFragmentManager(FragmentUtilActivity.class)
     20         .beginTransaction().add(fragment, null).commitNow();
     21   }
     22 
     23   public static void startFragment(Fragment fragment, Class<? extends FragmentActivity> fragmentActivityClass) {
     24     buildSupportFragmentManager(fragmentActivityClass)
     25         .beginTransaction().add(fragment, null).commitNow();
     26   }
     27 
     28   public static void startVisibleFragment(Fragment fragment) {
     29     buildSupportFragmentManager(FragmentUtilActivity.class)
     30         .beginTransaction().add(1, fragment, null).commitNow();
     31   }
     32 
     33   public static void startVisibleFragment(Fragment fragment, Class<? extends FragmentActivity> fragmentActivityClass, int containerViewId) {
     34     buildSupportFragmentManager(fragmentActivityClass)
     35         .beginTransaction().add(containerViewId, fragment, null).commitNow();
     36   }
     37 
     38   private static FragmentManager buildSupportFragmentManager(Class<? extends FragmentActivity> fragmentActivityClass) {
     39     FragmentActivity activity = Robolectric.setupActivity(fragmentActivityClass);
     40     return activity.getSupportFragmentManager();
     41   }
     42 
     43   private static class FragmentUtilActivity extends FragmentActivity {
     44     @Override
     45     protected void onCreate(Bundle savedInstanceState) {
     46       super.onCreate(savedInstanceState);
     47       LinearLayout view = new LinearLayout(this);
     48       view.setId(1);
     49 
     50       setContentView(view);
     51     }
     52   }
     53 }
     54