Home | History | Annotate | Download | only in v4
      1 package org.robolectric.shadows.support.v4;
      2 
      3 import static com.google.common.truth.Truth.assertThat;
      4 import static org.mockito.Mockito.spy;
      5 import static org.mockito.Mockito.verify;
      6 
      7 import android.content.Intent;
      8 import android.os.Bundle;
      9 import android.support.v4.app.Fragment;
     10 import android.support.v4.app.FragmentActivity;
     11 import android.view.LayoutInflater;
     12 import android.view.View;
     13 import android.view.ViewGroup;
     14 import android.widget.LinearLayout;
     15 import android.widget.TextView;
     16 import org.junit.Test;
     17 import org.junit.runner.RunWith;
     18 import org.robolectric.R;
     19 import org.robolectric.util.TestRunnerWithManifest;
     20 
     21 @RunWith(TestRunnerWithManifest.class)
     22 public class SupportFragmentControllerTest {
     23 
     24   private static final int VIEW_ID_CUSTOMIZED_LOGIN_ACTIVITY = 123;
     25 
     26   @Test
     27   public void initialNotAttached() {
     28     final LoginFragment fragment = new LoginFragment();
     29     SupportFragmentController.of(fragment);
     30 
     31     assertThat(fragment.getView()).isNull();
     32     assertThat(fragment.getActivity()).isNull();
     33     assertThat(fragment.isAdded()).isFalse();
     34   }
     35 
     36   @Test
     37   public void initialNotAttached_customActivity() {
     38     final LoginFragment fragment = new LoginFragment();
     39     SupportFragmentController.of(fragment, LoginActivity.class);
     40 
     41     assertThat(fragment.getView()).isNull();
     42     assertThat(fragment.getActivity()).isNull();
     43     assertThat(fragment.isAdded()).isFalse();
     44   }
     45 
     46   @Test
     47   public void attachedAfterCreate() {
     48     final LoginFragment fragment = new LoginFragment();
     49     SupportFragmentController.of(fragment).create();
     50 
     51     assertThat(fragment.getActivity()).isNotNull();
     52     assertThat(fragment.isAdded()).isTrue();
     53     assertThat(fragment.isResumed()).isFalse();
     54   }
     55 
     56   @Test
     57   public void attachedAfterCreate_customActivity() {
     58     final LoginFragment fragment = new LoginFragment();
     59     SupportFragmentController.of(fragment, LoginActivity.class).create();
     60 
     61     assertThat(fragment.getActivity()).isNotNull();
     62     assertThat(fragment.getActivity()).isInstanceOf(LoginActivity.class);
     63     assertThat(fragment.isAdded()).isTrue();
     64     assertThat(fragment.isResumed()).isFalse();
     65   }
     66 
     67   @Test
     68   public void attachedAfterCreate_customizedViewId() {
     69     final LoginFragment fragment = new LoginFragment();
     70     SupportFragmentController.of(fragment, CustomizedViewIdLoginActivity.class).create(VIEW_ID_CUSTOMIZED_LOGIN_ACTIVITY, null).start();
     71 
     72     assertThat(fragment.getView()).isNotNull();
     73     assertThat(fragment.getActivity()).isNotNull();
     74     assertThat(fragment.isAdded()).isTrue();
     75     assertThat(fragment.isResumed()).isFalse();
     76     assertThat((TextView) fragment.getView().findViewById(R.id.tacos)).isNotNull();
     77   }
     78 
     79   @Test
     80   public void hasViewAfterStart() {
     81     final LoginFragment fragment = new LoginFragment();
     82     SupportFragmentController.of(fragment).create().start();
     83 
     84     assertThat(fragment.getView()).isNotNull();
     85   }
     86 
     87   @Test
     88   public void isResumed() {
     89     final LoginFragment fragment = new LoginFragment();
     90     SupportFragmentController.of(fragment, LoginActivity.class).create().start().resume();
     91 
     92     assertThat(fragment.getView()).isNotNull();
     93     assertThat(fragment.getActivity()).isNotNull();
     94     assertThat(fragment.isAdded()).isTrue();
     95     assertThat(fragment.isResumed()).isTrue();
     96     assertThat((TextView) fragment.getView().findViewById(R.id.tacos)).isNotNull();
     97   }
     98 
     99   @Test
    100   public void isPaused() {
    101     final LoginFragment fragment = spy(new LoginFragment());
    102     SupportFragmentController.of(fragment, LoginActivity.class).create().start().resume().pause();
    103 
    104     assertThat(fragment.getView()).isNotNull();
    105     assertThat(fragment.getActivity()).isNotNull();
    106     assertThat(fragment.isAdded()).isTrue();
    107     assertThat(fragment.isResumed()).isFalse();
    108 
    109     verify(fragment).onResume();
    110     verify(fragment).onPause();
    111   }
    112 
    113   @Test
    114   public void isStopped() {
    115     final LoginFragment fragment = spy(new LoginFragment());
    116     SupportFragmentController.of(fragment, LoginActivity.class).create().start().resume().pause().stop();
    117 
    118     assertThat(fragment.getView()).isNotNull();
    119     assertThat(fragment.getActivity()).isNotNull();
    120     assertThat(fragment.isAdded()).isTrue();
    121     assertThat(fragment.isResumed()).isFalse();
    122 
    123     verify(fragment).onStart();
    124     verify(fragment).onResume();
    125     verify(fragment).onPause();
    126     verify(fragment).onStop();
    127   }
    128 
    129   @Test
    130   public void withIntent() {
    131     final LoginFragment fragment = new LoginFragment();
    132     Intent intent = new Intent("test_action");
    133     intent.putExtra("test_key", "test_value");
    134     SupportFragmentController<LoginFragment> controller =
    135         SupportFragmentController.of(fragment, LoginActivity.class, intent).create();
    136 
    137     Intent intentInFragment = controller.get().getActivity().getIntent();
    138     assertThat(intentInFragment.getAction()).isEqualTo("test_action");
    139     assertThat(intentInFragment.getExtras().getString("test_key")).isEqualTo("test_value");
    140   }
    141 
    142   @Test
    143   public void visible() {
    144     final LoginFragment fragment = new LoginFragment();
    145     final SupportFragmentController<LoginFragment> controller = SupportFragmentController.of(fragment, LoginActivity.class);
    146 
    147     controller.create().start().resume();
    148     assertThat(fragment.isVisible()).isFalse();
    149 
    150     controller.visible();
    151     assertThat(fragment.isVisible()).isTrue();
    152   }
    153 
    154   @Test
    155   public void savesInstanceState() {
    156     final LoginFragment fragment = new LoginFragment();
    157     final SupportFragmentController<LoginFragment> controller =
    158         SupportFragmentController.of(fragment, LoginActivity.class);
    159     controller.create().start().resume().visible();
    160     LoginActivity activity = (LoginActivity) controller.get().getActivity();
    161     Bundle expectedState = new Bundle();
    162     expectedState.putBoolean("isRestored", true);
    163     activity.setState(expectedState);
    164     final Bundle savedInstanceState = new Bundle();
    165 
    166     controller.saveInstanceState(savedInstanceState);
    167 
    168     assertThat(savedInstanceState.getBoolean("isRestored")).isTrue();
    169   }
    170 
    171   public static class LoginFragment extends Fragment {
    172     @Override
    173     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    174       return inflater.inflate(R.layout.fragment_contents, container, false);
    175     }
    176   }
    177 
    178   public static class LoginActivity extends FragmentActivity {
    179     private Bundle state = new Bundle();
    180 
    181     @Override
    182     protected void onCreate(Bundle savedInstanceState) {
    183       super.onCreate(savedInstanceState);
    184       LinearLayout view = new LinearLayout(this);
    185       view.setId(1);
    186 
    187       setContentView(view);
    188     }
    189 
    190     @Override
    191     protected void onSaveInstanceState(Bundle savedInstanceState) {
    192       super.onSaveInstanceState(savedInstanceState);
    193       savedInstanceState.putAll(state);
    194     }
    195 
    196     public void setState(Bundle state) {
    197       this.state = state;
    198     }
    199   }
    200 
    201   public static class CustomizedViewIdLoginActivity extends FragmentActivity {
    202     @Override
    203     protected void onCreate(Bundle savedInstanceState) {
    204       super.onCreate(savedInstanceState);
    205       LinearLayout view = new LinearLayout(this);
    206       view.setId(VIEW_ID_CUSTOMIZED_LOGIN_ACTIVITY);
    207 
    208       setContentView(view);
    209     }
    210   }
    211 }
    212