1package com.xtremelabs.robolectric.shadows; 2 3import android.os.Bundle; 4import android.support.v4.app.Fragment; 5import android.support.v4.app.FragmentActivity; 6import android.view.View; 7import android.view.ViewGroup; 8import android.widget.Button; 9 10import com.xtremelabs.robolectric.R; 11import com.xtremelabs.robolectric.WithTestDefaultsRunner; 12import com.xtremelabs.robolectric.tester.android.util.TestFragmentManager; 13import org.junit.Before; 14import org.junit.Ignore; 15import org.junit.Test; 16import org.junit.runner.RunWith; 17 18import static com.xtremelabs.robolectric.Robolectric.shadowOf; 19import static junit.framework.Assert.assertNull; 20import static junit.framework.Assert.assertTrue; 21import static org.junit.Assert.*; 22 23@RunWith(WithTestDefaultsRunner.class) 24public class FragmentActivityTest { 25 26 private TestFragmentActivity activity; 27 private TestFragment fragment; 28 29 @Before 30 public void setUp() throws Exception { 31 activity = new TestFragmentActivity(); 32 activity.onCreate(null); 33 fragment = (TestFragment) activity.getSupportFragmentManager().findFragmentByTag("fragment_tag"); 34 } 35 36 @Test 37 public void shouldHaveAFragmentManager() throws Exception { 38 assertNotNull(activity.getSupportFragmentManager()); 39 } 40 41 @Test 42 public void viewLoader_shouldInflateFragment() throws Exception { 43 assertEquals(TestFragment.class, fragment.getClass()); 44 } 45 46 @Test 47 public void viewLoader_shouldSetFragmentId() throws Exception { 48 Fragment fragmentById = activity.getSupportFragmentManager().findFragmentById(R.id.fragment); 49 assertSame(fragment, fragmentById); 50 } 51 52 @Test 53 public void viewLoader_shouldInsertFragmentViewIntoLayout() throws Exception { 54 assertSame(fragment.onCreateViewReturnValue, activity.findViewById(TestFragment.FRAGMENT_VIEW_ID)); 55 } 56 57 @Test 58 public void viewLoader_shouldSetFragmentsActivity() throws Exception { 59 assertSame(activity, fragment.getActivity()); 60 } 61 62 @Test 63 public void viewLoader_shouldCreateContainerView() throws Exception { 64 ViewGroup container = (ViewGroup) activity.findViewById(R.id.fragment); 65 assertNotNull(container); 66 } 67 68 @Test 69 public void viewLoader_shouldInsertFragmentViewIntoContainer() throws Exception { 70 ViewGroup container = (ViewGroup) activity.findViewById(R.id.fragment); 71 View fragmentView = container.findViewById(TestFragment.FRAGMENT_VIEW_ID); 72 assertSame(fragment.onCreateViewReturnValue, fragmentView); 73 } 74 75 @Test 76 @Ignore("Seems to be broken by 'Android Support' rev 8") 77 public void onSaveInstanceState_shouldStoreListOfFragments() throws Exception { 78 Fragment fragment = new TestFragment(); 79 int fragment_container = R.id.dynamic_fragment_container; 80 activity.getSupportFragmentManager().beginTransaction().add(fragment_container, fragment).commit(); 81 Bundle outState = new Bundle(); 82 activity.onSaveInstanceState(outState); 83 84 assertTrue(outState.containsKey(ShadowFragmentActivity.FRAGMENTS_TAG)); 85 86 Object[] states = (Object[]) outState.getSerializable(ShadowFragmentActivity.FRAGMENTS_TAG); 87 SerializedFragmentState fragmentState = (SerializedFragmentState) states[1]; 88 89 assertEquals(fragmentState.id, fragment.getId()); 90 assertEquals(fragmentState.tag, fragment.getTag()); 91 assertEquals(fragmentState.fragmentClass, fragment.getClass()); 92 assertEquals(fragmentState.containerId, fragment_container); 93 } 94 95 @Test 96 public void onSaveInstanceState_shouldCallOnSaveInstanceStateOnFragments() throws Exception { 97 TestFragment fragment = new TestFragment(); 98 int fragment_container = R.id.dynamic_fragment_container; 99 activity.getSupportFragmentManager().beginTransaction().add(fragment_container, fragment).commit(); 100 Bundle outState = new Bundle(); 101 activity.onSaveInstanceState(outState); 102 103 assertTrue(fragment.onSaveInstanceStateWasCalled); 104 } 105 106 @Test 107 public void onCreate_shouldRecreateFragments() throws Exception { 108 Bundle bundle = new Bundle(); 109 TestFragment dynamicFrag = new TestFragment(); 110 int containerId = 123; 111 SerializedFragmentState fragmentState = new SerializedFragmentState(containerId, dynamicFrag); 112 bundle.putSerializable(ShadowFragmentActivity.FRAGMENTS_TAG, new Object[]{fragmentState}); 113 114 activity = new TestFragmentActivity(); 115 activity.onCreate(bundle); 116 TestFragmentManager fragmentManager = (TestFragmentManager) activity.getSupportFragmentManager(); 117 assertEquals(2, fragmentManager.getFragments().size()); 118 119 TestFragment restoredFrag = (TestFragment) fragmentManager.getFragments().get(containerId); 120 assertEquals(restoredFrag.getId(), dynamicFrag.getId()); 121 assertEquals(restoredFrag.getTag(), dynamicFrag.getTag()); 122 assertEquals(bundle, shadowOf(restoredFrag).getSavedInstanceState()); 123 assertSame(activity, restoredFrag.onAttachActivity); 124 assertSame(activity, restoredFrag.getActivity()); 125 assertNull(restoredFrag.getView()); 126 } 127 128 @Test 129 public void onStart_shouldStartFragments() throws Exception { 130 Bundle bundle = new Bundle(); 131 TestFragment dynamicFrag = new TestFragment(); 132 int containerId = 123; 133 SerializedFragmentState fragmentState = new SerializedFragmentState(containerId, dynamicFrag); 134 bundle.putSerializable(ShadowFragmentActivity.FRAGMENTS_TAG, new Object[]{fragmentState}); 135 136 activity = new TestFragmentActivity(); 137 activity.onCreate(bundle); 138 shadowOf(activity).onStart(); 139 TestFragmentManager fragmentManager = (TestFragmentManager) activity.getSupportFragmentManager(); 140 assertEquals(2, fragmentManager.getFragments().size()); 141 TestFragment restoredFrag = (TestFragment) fragmentManager.getFragments().get(containerId); 142 143 assertEquals(restoredFrag.onCreateViewInflater, activity.getLayoutInflater()); 144 assertNotNull(restoredFrag.getView()); 145 } 146 147 @Test 148 public void onPause_shouldPauseTheFragment() throws Exception { 149 activity.onPause(); 150 assertTrue(fragment.onPauseWasCalled); 151 } 152 153 @Test 154 public void getCurrentFocus_shouldGetFocusFromFragment() { 155 activity = new TestFragmentActivity(); 156 activity.onCreate(null); 157 shadowOf(activity).onStart(); 158 159 Fragment fragment = activity.getSupportFragmentManager().findFragmentById( 160 TestFragment.FRAGMENT_VIEW_ID); 161 View button = activity.findViewById(R.id.button); 162 button.requestFocus(); 163 164 View focusedView = activity.getCurrentFocus(); 165 assertSame(button, focusedView); 166 } 167 168 private static class TestFragmentActivity extends FragmentActivity { 169 @Override 170 public void onCreate(Bundle savedInstanceState) { 171 super.onCreate(savedInstanceState); 172 setContentView(R.layout.fragment_activity); 173 } 174 175 @Override 176 public void onSaveInstanceState(Bundle outState) { 177 super.onSaveInstanceState(outState); 178 } 179 180 @Override 181 public void onPause() { 182 super.onPause(); 183 } 184 } 185} 186