Home | History | Annotate | Download | only in shadows
      1 package com.xtremelabs.robolectric.shadows;
      2 
      3 import android.app.Dialog;
      4 import android.support.v4.app.DialogFragment;
      5 import android.support.v4.app.FragmentActivity;
      6 import android.support.v4.app.FragmentManager;
      7 import android.support.v4.app.FragmentTransaction;
      8 import com.xtremelabs.robolectric.internal.Implementation;
      9 import com.xtremelabs.robolectric.internal.Implements;
     10 import com.xtremelabs.robolectric.internal.RealObject;
     11 import com.xtremelabs.robolectric.tester.android.util.TestFragmentManager;
     12 import com.xtremelabs.robolectric.tester.android.util.TestFragmentTransaction;
     13 
     14 import static com.xtremelabs.robolectric.Robolectric.shadowOf;
     15 
     16 /**
     17  * Shadow implementation of the {@link DialogFragment}.
     18  */
     19 @Implements(DialogFragment.class)
     20 public class ShadowDialogFragment extends ShadowFragment {
     21     private static DialogFragment latestDialogFragment;
     22 
     23     private Dialog dialog;
     24     private boolean cancelable = true;  // defaults to true
     25 
     26     @RealObject
     27     private DialogFragment realDialogFragment;
     28 
     29     @Implementation
     30     public int show(FragmentTransaction transaction, String tag) {
     31         show(((TestFragmentTransaction)transaction).getManager(), tag);
     32         return 0;
     33     }
     34 
     35     @Implementation
     36     public void show(FragmentManager manager, String tag) {
     37         TestFragmentManager testFragmentManager = (TestFragmentManager) manager;
     38         FragmentActivity activityFromManager = testFragmentManager.getActivity();
     39 
     40         shadowOf(realDialogFragment).setActivity(activityFromManager);
     41 
     42         realDialogFragment.onAttach(activity);
     43         realDialogFragment.onCreate(null);
     44         dialog = realDialogFragment.onCreateDialog(null);
     45         view = realDialogFragment.onCreateView(ShadowLayoutInflater.from(activity), null, null);
     46         if (dialog == null) {
     47             dialog = new Dialog(activityFromManager);
     48             dialog.setContentView(view);
     49         }
     50         dialog.setCancelable(cancelable);
     51         testFragmentManager.addDialogFragment(tag, realDialogFragment);
     52         realDialogFragment.onViewCreated(view, null);
     53         realDialogFragment.onActivityCreated(null);
     54         realDialogFragment.onStart();
     55         realDialogFragment.onResume();
     56 
     57         latestDialogFragment = realDialogFragment;
     58     }
     59 
     60     @Implementation
     61     public void onStart() {
     62         if (dialog != null) {
     63             dialog.show();
     64         }
     65     }
     66 
     67     @Implementation
     68     public void dismiss() {
     69         if (dialog != null) {
     70             dialog.dismiss();
     71         }
     72     }
     73 
     74     @Implementation
     75     public Dialog getDialog() {
     76         return dialog;
     77     }
     78 
     79     @Implementation
     80     public boolean isCancelable() {
     81         return cancelable;
     82     }
     83 
     84     @Implementation
     85     public void setCancelable(boolean cancelable) {
     86         this.cancelable = cancelable;
     87         if (dialog != null) {
     88             dialog.setCancelable(cancelable);
     89         }
     90     }
     91 
     92     public static DialogFragment getLatestDialogFragment() {
     93         return latestDialogFragment;
     94     }
     95 }
     96