aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/xtremelabs/robolectric/shadows/FragmentActivityTest.java
blob: d03deed348cbb5e097664c1c5751527239303a6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.xtremelabs.robolectric.shadows;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.xtremelabs.robolectric.R;
import com.xtremelabs.robolectric.WithTestDefaultsRunner;
import com.xtremelabs.robolectric.tester.android.util.TestFragmentManager;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;

import static com.xtremelabs.robolectric.Robolectric.shadowOf;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.*;

@RunWith(WithTestDefaultsRunner.class)
public class FragmentActivityTest {

    private TestFragmentActivity activity;
    private TestFragment fragment;

    @Before
    public void setUp() throws Exception {
        activity = new TestFragmentActivity();
        activity.onCreate(null);
        fragment = (TestFragment) activity.getSupportFragmentManager().findFragmentByTag("fragment_tag");
    }

    @Test
    public void shouldHaveAFragmentManager() throws Exception {
        assertNotNull(activity.getSupportFragmentManager());
    }

    @Test
    public void viewLoader_shouldInflateFragment() throws Exception {
        assertEquals(TestFragment.class, fragment.getClass());
    }

    @Test
    public void viewLoader_shouldSetFragmentId() throws Exception {
        Fragment fragmentById = activity.getSupportFragmentManager().findFragmentById(R.id.fragment);
        assertSame(fragment, fragmentById);
    }

    @Test
    public void viewLoader_shouldInsertFragmentViewIntoLayout() throws Exception {
        assertSame(fragment.onCreateViewReturnValue, activity.findViewById(TestFragment.FRAGMENT_VIEW_ID));
    }

    @Test
    public void viewLoader_shouldSetFragmentsActivity() throws Exception {
        assertSame(activity, fragment.getActivity());
    }

    @Test
    public void viewLoader_shouldCreateContainerView() throws Exception {
        ViewGroup container = (ViewGroup) activity.findViewById(R.id.fragment);
        assertNotNull(container);
    }

    @Test
    public void viewLoader_shouldInsertFragmentViewIntoContainer() throws Exception {
        ViewGroup container = (ViewGroup) activity.findViewById(R.id.fragment);
        View fragmentView = container.findViewById(TestFragment.FRAGMENT_VIEW_ID);
        assertSame(fragment.onCreateViewReturnValue, fragmentView);
    }

    @Test
    @Ignore("Seems to be broken by 'Android Support' rev 8")
    public void onSaveInstanceState_shouldStoreListOfFragments() throws Exception {
        Fragment fragment = new TestFragment();
        int fragment_container = R.id.dynamic_fragment_container;
        activity.getSupportFragmentManager().beginTransaction().add(fragment_container, fragment).commit();
        Bundle outState = new Bundle();
        activity.onSaveInstanceState(outState);

        assertTrue(outState.containsKey(ShadowFragmentActivity.FRAGMENTS_TAG));

        Object[] states = (Object[]) outState.getSerializable(ShadowFragmentActivity.FRAGMENTS_TAG);
        SerializedFragmentState fragmentState = (SerializedFragmentState) states[1];

        assertEquals(fragmentState.id, fragment.getId());
        assertEquals(fragmentState.tag, fragment.getTag());
        assertEquals(fragmentState.fragmentClass, fragment.getClass());
        assertEquals(fragmentState.containerId, fragment_container);
    }

    @Test
    public void onSaveInstanceState_shouldCallOnSaveInstanceStateOnFragments() throws Exception {
        TestFragment fragment = new TestFragment();
        int fragment_container = R.id.dynamic_fragment_container;
        activity.getSupportFragmentManager().beginTransaction().add(fragment_container, fragment).commit();
        Bundle outState = new Bundle();
        activity.onSaveInstanceState(outState);

        assertTrue(fragment.onSaveInstanceStateWasCalled);
    }

    @Test
    public void onCreate_shouldRecreateFragments() throws Exception {
        Bundle bundle = new Bundle();
        TestFragment dynamicFrag = new TestFragment();
        int containerId = 123;
        SerializedFragmentState fragmentState = new SerializedFragmentState(containerId, dynamicFrag);
        bundle.putSerializable(ShadowFragmentActivity.FRAGMENTS_TAG, new Object[]{fragmentState});

        activity = new TestFragmentActivity();
        activity.onCreate(bundle);
        TestFragmentManager fragmentManager = (TestFragmentManager) activity.getSupportFragmentManager();
        assertEquals(2, fragmentManager.getFragments().size());

        TestFragment restoredFrag = (TestFragment) fragmentManager.getFragments().get(containerId);
        assertEquals(restoredFrag.getId(), dynamicFrag.getId());
        assertEquals(restoredFrag.getTag(), dynamicFrag.getTag());
        assertEquals(bundle, shadowOf(restoredFrag).getSavedInstanceState());
        assertSame(activity, restoredFrag.onAttachActivity);
        assertSame(activity, restoredFrag.getActivity());
        assertNull(restoredFrag.getView());
    }

    @Test
    public void onStart_shouldStartFragments() throws Exception {
        Bundle bundle = new Bundle();
        TestFragment dynamicFrag = new TestFragment();
        int containerId = 123;
        SerializedFragmentState fragmentState = new SerializedFragmentState(containerId, dynamicFrag);
        bundle.putSerializable(ShadowFragmentActivity.FRAGMENTS_TAG, new Object[]{fragmentState});

        activity = new TestFragmentActivity();
        activity.onCreate(bundle);
        shadowOf(activity).onStart();
        TestFragmentManager fragmentManager = (TestFragmentManager) activity.getSupportFragmentManager();
        assertEquals(2, fragmentManager.getFragments().size());
        TestFragment restoredFrag = (TestFragment) fragmentManager.getFragments().get(containerId);

        assertEquals(restoredFrag.onCreateViewInflater, activity.getLayoutInflater());
        assertNotNull(restoredFrag.getView());
    }

    @Test
    public void onPause_shouldPauseTheFragment() throws Exception {
        activity.onPause();
        assertTrue(fragment.onPauseWasCalled);
    }

    @Test
    public void getCurrentFocus_shouldGetFocusFromFragment() {
        activity = new TestFragmentActivity();
        activity.onCreate(null);
        shadowOf(activity).onStart();

        Fragment fragment = activity.getSupportFragmentManager().findFragmentById(
                TestFragment.FRAGMENT_VIEW_ID);
        View button = activity.findViewById(R.id.button);
        button.requestFocus();

        View focusedView = activity.getCurrentFocus();
        assertSame(button, focusedView);
    }

    private static class TestFragmentActivity extends FragmentActivity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.fragment_activity);
        }

        @Override
        public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
        }

        @Override
        public void onPause() {
            super.onPause();
        }
    }
}