|
@@ -1,238 +0,0 @@
|
1
|
|
-package com.reactnativenavigation.layout;
|
2
|
|
-
|
3
|
|
-import android.app.Activity;
|
4
|
|
-import android.support.v7.app.AppCompatActivity;
|
5
|
|
-import android.view.View;
|
6
|
|
-import android.view.ViewGroup;
|
7
|
|
-
|
8
|
|
-import com.reactnativenavigation.BaseTest;
|
9
|
|
-import com.reactnativenavigation.layout.bottomtabs.BottomTabsLayout;
|
10
|
|
-import com.reactnativenavigation.react.ReactRootViewCreator;
|
11
|
|
-
|
12
|
|
-import org.json.JSONException;
|
13
|
|
-import org.json.JSONObject;
|
14
|
|
-import org.junit.Before;
|
15
|
|
-import org.junit.Test;
|
16
|
|
-import org.robolectric.Robolectric;
|
17
|
|
-
|
18
|
|
-import java.util.Arrays;
|
19
|
|
-import java.util.List;
|
20
|
|
-
|
21
|
|
-import static org.assertj.core.api.Java6Assertions.assertThat;
|
22
|
|
-import static org.mockito.ArgumentMatchers.any;
|
23
|
|
-import static org.mockito.ArgumentMatchers.eq;
|
24
|
|
-import static org.mockito.Mockito.mock;
|
25
|
|
-import static org.mockito.Mockito.when;
|
26
|
|
-
|
27
|
|
-public class LayoutFactoryTest extends BaseTest {
|
28
|
|
-
|
29
|
|
- private final static String NODE_ID = "myUniqueId";
|
30
|
|
- private final static String REACT_ROOT_VIEW_KEY = "myName";
|
31
|
|
-
|
32
|
|
- private final static String OTHER_NODE_ID = "anotherUniqueId";
|
33
|
|
- private final static String OTHER_REACT_ROOT_VIEW_KEY = "anotherName";
|
34
|
|
-
|
35
|
|
- private Activity activity;
|
36
|
|
- private View mockView;
|
37
|
|
- private View otherMockView;
|
38
|
|
- private ReactRootViewCreator reactRootViewCreator;
|
39
|
|
-
|
40
|
|
- @Before
|
41
|
|
- public void setUp() {
|
42
|
|
- activity = Robolectric.buildActivity(AppCompatActivity.class).get();
|
43
|
|
- mockView = new View(activity);
|
44
|
|
- otherMockView = new View(activity);
|
45
|
|
- reactRootViewCreator = mock(ReactRootViewCreator.class);
|
46
|
|
- }
|
47
|
|
-
|
48
|
|
- @Test
|
49
|
|
- public void returnsContainerThatHoldsTheRootView() throws Exception {
|
50
|
|
- when(reactRootViewCreator.create(any(Activity.class), eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);
|
51
|
|
- final LayoutNode node = createContainerNode();
|
52
|
|
-
|
53
|
|
- final ViewGroup result = (ViewGroup) createLayoutFactory().create(node);
|
54
|
|
-
|
55
|
|
- assertThat(result).isInstanceOf(Container.class);
|
56
|
|
- TestUtils.assertViewChildren(result, mockView);
|
57
|
|
- }
|
58
|
|
-
|
59
|
|
- @Test
|
60
|
|
- public void returnsContainerStack() throws Exception {
|
61
|
|
- when(reactRootViewCreator.create(any(Activity.class), eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);
|
62
|
|
- final LayoutNode containerNode = createContainerNode();
|
63
|
|
- final LayoutNode stackNode = createContainerStackNode(containerNode);
|
64
|
|
-
|
65
|
|
- final ViewGroup result = (ViewGroup) createLayoutFactory().create(stackNode);
|
66
|
|
-
|
67
|
|
- assertThat(result).isInstanceOf(ContainerStackLayout.class);
|
68
|
|
- ViewGroup container = (ViewGroup) TestUtils.assertViewChildrenCount(result, 1).get(0);
|
69
|
|
- TestUtils.assertViewChildren(container, mockView);
|
70
|
|
- }
|
71
|
|
-
|
72
|
|
- @Test
|
73
|
|
- public void returnsContainerStackWithMultipleViews() throws Exception {
|
74
|
|
- final View mockView1 = mock(View.class);
|
75
|
|
- final View mockView2 = mock(View.class);
|
76
|
|
- when(reactRootViewCreator.create(any(Activity.class), eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView1);
|
77
|
|
- when(reactRootViewCreator.create(any(Activity.class), eq(OTHER_NODE_ID), eq(OTHER_REACT_ROOT_VIEW_KEY))).thenReturn(mockView2);
|
78
|
|
-
|
79
|
|
- final LayoutNode containerNode1 = createContainerNode(NODE_ID, REACT_ROOT_VIEW_KEY);
|
80
|
|
- final LayoutNode containerNode2 = createContainerNode(OTHER_NODE_ID, OTHER_REACT_ROOT_VIEW_KEY);
|
81
|
|
- final LayoutNode stackNode = createContainerStackNode(containerNode1, containerNode2);
|
82
|
|
-
|
83
|
|
- final ViewGroup result = (ViewGroup) createLayoutFactory().create(stackNode);
|
84
|
|
-
|
85
|
|
- assertThat(result).isInstanceOf(ContainerStackLayout.class);
|
86
|
|
- List<View> containers = TestUtils.assertViewChildrenCount(result, 2);
|
87
|
|
- ViewGroup container1 = (ViewGroup) containers.get(0);
|
88
|
|
- ViewGroup container2 = (ViewGroup) containers.get(1);
|
89
|
|
- TestUtils.assertViewChildren(container1, mockView1);
|
90
|
|
- TestUtils.assertViewChildren(container2, mockView2);
|
91
|
|
- }
|
92
|
|
-
|
93
|
|
- @Test
|
94
|
|
- public void returnsSideMenuRoot() throws Exception {
|
95
|
|
- when(reactRootViewCreator.create(any(Activity.class), eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);
|
96
|
|
- final LayoutNode containerNode = createSideMenuContainerNode(Arrays.asList(createContainerNode()));
|
97
|
|
- final ViewGroup result = (ViewGroup) createLayoutFactory().create(containerNode);
|
98
|
|
- assertThat(result).isInstanceOf(SideMenuLayout.class);
|
99
|
|
- }
|
100
|
|
-
|
101
|
|
- @Test
|
102
|
|
- public void hasContentContainer() throws Exception {
|
103
|
|
- when(reactRootViewCreator.create(any(Activity.class), eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);
|
104
|
|
- LayoutNode contentContainer = createContainerNode();
|
105
|
|
- final LayoutNode sideMenu = createSideMenuContainerNode(Arrays.asList(contentContainer));
|
106
|
|
- final ViewGroup result = (ViewGroup) createLayoutFactory().create(sideMenu);
|
107
|
|
- assertThat(result.getChildAt(0)).isInstanceOf(Container.class);
|
108
|
|
- }
|
109
|
|
-
|
110
|
|
- @Test
|
111
|
|
- public void hasLeftMenu() throws Exception {
|
112
|
|
- when(reactRootViewCreator.create(any(Activity.class), eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);
|
113
|
|
- LayoutNode sideMenuLeft = createSideMenuLeftNode();
|
114
|
|
- final LayoutNode sideMenu = createSideMenuContainerNode(Arrays.asList(sideMenuLeft));
|
115
|
|
- final ViewGroup result = (ViewGroup) createLayoutFactory().create(sideMenu);
|
116
|
|
- assertThat(result.getChildAt(0)).isInstanceOf(Container.class);
|
117
|
|
- }
|
118
|
|
-
|
119
|
|
- @Test
|
120
|
|
- public void hasRightMenu() throws Exception {
|
121
|
|
- when(reactRootViewCreator.create(any(Activity.class), eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);
|
122
|
|
- LayoutNode sideMenuRight = createSideMenuRightNode();
|
123
|
|
- final LayoutNode sideMenu = createSideMenuContainerNode(Arrays.asList(sideMenuRight));
|
124
|
|
- final ViewGroup result = (ViewGroup) createLayoutFactory().create(sideMenu);
|
125
|
|
- assertThat(result.getChildAt(0)).isInstanceOf(Container.class);
|
126
|
|
- }
|
127
|
|
-
|
128
|
|
- @Test
|
129
|
|
- public void pushScreenToScreenStackLayout() throws Exception {
|
130
|
|
- when(reactRootViewCreator.create(any(Activity.class), eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);
|
131
|
|
- final LayoutNode container = createContainerNode();
|
132
|
|
- final LayoutNode stackNode = createContainerStackNode(container);
|
133
|
|
- final ContainerStackLayout containerStackLayout = (ContainerStackLayout) createLayoutFactory().create(stackNode);
|
134
|
|
-
|
135
|
|
- when(reactRootViewCreator.create(any(Activity.class), eq(OTHER_NODE_ID), eq(OTHER_REACT_ROOT_VIEW_KEY))).thenReturn(otherMockView);
|
136
|
|
- final LayoutNode pushedContainer = createContainerNode(OTHER_NODE_ID, OTHER_REACT_ROOT_VIEW_KEY);
|
137
|
|
- containerStackLayout.push(createLayoutFactory().create(pushedContainer));
|
138
|
|
-
|
139
|
|
- ViewGroup result = (ViewGroup) TestUtils.assertViewChildrenCount(containerStackLayout, 1).get(0);
|
140
|
|
- assertThat(result.getChildAt(0)).isEqualTo(otherMockView);
|
141
|
|
- }
|
142
|
|
-
|
143
|
|
- @Test
|
144
|
|
- public void pushTwoScreensToStackLayout() throws Exception {
|
145
|
|
- when(reactRootViewCreator.create(any(Activity.class), eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);
|
146
|
|
- final LayoutNode container = createContainerNode();
|
147
|
|
- final LayoutNode stackNode = createContainerStackNode(container);
|
148
|
|
- final ContainerStackLayout containerStackLayout = (ContainerStackLayout) createLayoutFactory().create(stackNode);
|
149
|
|
-
|
150
|
|
- View first = new View(activity);
|
151
|
|
- pushContainer(containerStackLayout, OTHER_NODE_ID, OTHER_REACT_ROOT_VIEW_KEY, first);
|
152
|
|
-
|
153
|
|
- View second = new View(activity);
|
154
|
|
- pushContainer(containerStackLayout, "secondPushedScreenId", "secondPushedScreenKey", second);
|
155
|
|
-
|
156
|
|
- ViewGroup result = (ViewGroup) TestUtils.assertViewChildrenCount(containerStackLayout, 1).get(0);
|
157
|
|
- assertThat(result.getChildAt(0)).isEqualTo(second);
|
158
|
|
- }
|
159
|
|
-
|
160
|
|
- @Test
|
161
|
|
- public void popTwoScreensFromStackLayout() throws Exception {
|
162
|
|
- when(reactRootViewCreator.create(any(Activity.class), eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);
|
163
|
|
- final LayoutNode container = createContainerNode();
|
164
|
|
- final LayoutNode stackNode = createContainerStackNode(container);
|
165
|
|
- final ContainerStackLayout containerStackLayout = (ContainerStackLayout) createLayoutFactory().create(stackNode);
|
166
|
|
-
|
167
|
|
- pushContainer(containerStackLayout, OTHER_NODE_ID, OTHER_REACT_ROOT_VIEW_KEY, new View(activity));
|
168
|
|
- pushContainer(containerStackLayout, "secondPushedScreenId", "secondPushedScreenKey", new View(activity));
|
169
|
|
-
|
170
|
|
- containerStackLayout.pop();
|
171
|
|
- containerStackLayout.pop();
|
172
|
|
-
|
173
|
|
- ViewGroup result = (ViewGroup) TestUtils.assertViewChildrenCount(containerStackLayout, 1).get(0);
|
174
|
|
- assertThat(result.getChildAt(0)).isEqualTo(mockView);
|
175
|
|
- }
|
176
|
|
-
|
177
|
|
- private void pushContainer(ContainerStackLayout containerStackLayout, String screenId, String reactRootViewKey, View rootView) throws Exception {
|
178
|
|
- when(reactRootViewCreator.create(any(Activity.class), eq(screenId), eq(reactRootViewKey))).thenReturn(rootView);
|
179
|
|
- View pushedContainer = createLayoutFactory().create(createContainerNode(screenId, reactRootViewKey));
|
180
|
|
- containerStackLayout.push(pushedContainer);
|
181
|
|
- }
|
182
|
|
-
|
183
|
|
- private void pushContainer(BottomTabsLayout containerStackLayout, String screenId, String reactRootViewKey, View rootView) throws Exception {
|
184
|
|
- when(reactRootViewCreator.create(any(Activity.class), eq(screenId), eq(reactRootViewKey))).thenReturn(rootView);
|
185
|
|
- View pushedContainer = createLayoutFactory().create(createContainerNode(screenId, reactRootViewKey));
|
186
|
|
- containerStackLayout.push(pushedContainer);
|
187
|
|
- }
|
188
|
|
-
|
189
|
|
- @Test
|
190
|
|
- public void popScreenFromScreenStackLayout() throws Exception {
|
191
|
|
- when(reactRootViewCreator.create(any(Activity.class), eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);
|
192
|
|
- final LayoutNode container = createContainerNode();
|
193
|
|
- final LayoutNode stackNode = createContainerStackNode(container);
|
194
|
|
- final ContainerStackLayout containerStackLayout = (ContainerStackLayout) createLayoutFactory().create(stackNode);
|
195
|
|
-
|
196
|
|
- when(reactRootViewCreator.create(any(Activity.class), eq(OTHER_NODE_ID), eq(OTHER_REACT_ROOT_VIEW_KEY))).thenReturn(otherMockView);
|
197
|
|
- final LayoutNode pushedContainer = createContainerNode(OTHER_NODE_ID, OTHER_REACT_ROOT_VIEW_KEY);
|
198
|
|
- containerStackLayout.push(createLayoutFactory().create(pushedContainer));
|
199
|
|
-
|
200
|
|
- containerStackLayout.pop();
|
201
|
|
- ViewGroup result = (ViewGroup) TestUtils.assertViewChildrenCount(containerStackLayout, 1).get(0);
|
202
|
|
- assertThat(result.getChildAt(0)).isEqualTo(mockView);
|
203
|
|
- }
|
204
|
|
-
|
205
|
|
- private LayoutFactory createLayoutFactory() {
|
206
|
|
- return new LayoutFactory(activity, reactRootViewCreator);
|
207
|
|
- }
|
208
|
|
-
|
209
|
|
- private LayoutNode createContainerNode() throws Exception {
|
210
|
|
- return createContainerNode(NODE_ID, REACT_ROOT_VIEW_KEY);
|
211
|
|
- }
|
212
|
|
-
|
213
|
|
- private LayoutNode createSideMenuLeftNode() throws Exception {
|
214
|
|
- List<LayoutNode> children = Arrays.asList(createContainerNode());
|
215
|
|
- return new LayoutNode("SideMenuLeft", LayoutNode.Type.SideMenuLeft, null, children);
|
216
|
|
- }
|
217
|
|
-
|
218
|
|
- private LayoutNode createSideMenuRightNode() throws Exception {
|
219
|
|
- List<LayoutNode> children = Arrays.asList(createContainerNode());
|
220
|
|
- return new LayoutNode("SideMenuRight", LayoutNode.Type.SideMenuRight, null, children);
|
221
|
|
- }
|
222
|
|
-
|
223
|
|
- private LayoutNode createContainerNode(final String id, final String name) throws JSONException {
|
224
|
|
- return new LayoutNode(id, LayoutNode.Type.Container, new JSONObject().put("name", name), null);
|
225
|
|
- }
|
226
|
|
-
|
227
|
|
- private LayoutNode createSideMenuContainerNode(List<LayoutNode> children) {
|
228
|
|
- return new LayoutNode("SideMenuRoot", LayoutNode.Type.SideMenuRoot, null, children);
|
229
|
|
- }
|
230
|
|
-
|
231
|
|
- private LayoutNode createContainerStackNode(LayoutNode... children) {
|
232
|
|
- return new LayoutNode("ContainerStack", LayoutNode.Type.ContainerStack, null, Arrays.asList(children));
|
233
|
|
- }
|
234
|
|
-
|
235
|
|
- private LayoutNode createBottomTabNode(LayoutNode... children) {
|
236
|
|
- return new LayoutNode("BottomTabs", LayoutNode.Type.BottomTabs, null, Arrays.asList(children));
|
237
|
|
- }
|
238
|
|
-}
|