|
@@ -7,6 +7,7 @@ import android.view.ViewGroup;
|
7
|
7
|
import com.reactnativenavigation.layout.Container;
|
8
|
8
|
import com.reactnativenavigation.layout.ContainerStack;
|
9
|
9
|
import com.reactnativenavigation.layout.LayoutFactory;
|
|
10
|
+import com.reactnativenavigation.layout.LayoutFactory.LayoutNode;
|
10
|
11
|
|
11
|
12
|
import org.junit.Before;
|
12
|
13
|
import org.junit.Test;
|
|
@@ -15,89 +16,115 @@ import org.robolectric.Robolectric;
|
15
|
16
|
import org.robolectric.RobolectricTestRunner;
|
16
|
17
|
|
17
|
18
|
import java.util.ArrayList;
|
|
19
|
+import java.util.Arrays;
|
18
|
20
|
import java.util.HashMap;
|
19
|
21
|
import java.util.List;
|
20
|
22
|
import java.util.Map;
|
21
|
|
-import java.util.concurrent.atomic.AtomicReference;
|
22
|
23
|
|
23
|
24
|
import static org.assertj.core.api.Java6Assertions.assertThat;
|
|
25
|
+import static org.mockito.ArgumentMatchers.eq;
|
|
26
|
+import static org.mockito.Mockito.mock;
|
|
27
|
+import static org.mockito.Mockito.when;
|
24
|
28
|
|
25
|
29
|
|
26
|
30
|
@RunWith(RobolectricTestRunner.class)
|
27
|
31
|
public class LayoutFactoryTest {
|
|
32
|
+
|
|
33
|
+ private final static String VIEW_ID = "myUniqueId";
|
|
34
|
+ private final static String VIEW_NAME = "myName";
|
|
35
|
+
|
|
36
|
+ private final static String OTHER_VIEW_ID = "anotherUniqueId";
|
|
37
|
+ private final static String OTHER_VIEW_NAME = "anotherName";
|
|
38
|
+
|
|
39
|
+ private View mockView;
|
|
40
|
+ private LayoutFactory.RootViewCreator rootViewCreator;
|
|
41
|
+
|
28
|
42
|
@Before
|
29
|
43
|
public void setUp() {
|
30
|
|
-
|
|
44
|
+ mockView = new View(Robolectric.setupActivity(Activity.class));
|
|
45
|
+ rootViewCreator = mock(LayoutFactory.RootViewCreator.class);
|
31
|
46
|
}
|
32
|
47
|
|
33
|
48
|
@Test
|
34
|
49
|
public void returnsContainerThatHoldsTheRootView() {
|
35
|
|
- final AtomicReference<String> idRef = new AtomicReference<>();
|
36
|
|
- final AtomicReference<String> nameRef = new AtomicReference<>();
|
37
|
|
- final AtomicReference<View> viewRef = new AtomicReference<>();
|
38
|
|
-
|
39
|
|
- LayoutFactory.RootViewCreator rootViewCreator = new LayoutFactory.RootViewCreator() {
|
40
|
|
- @Override
|
41
|
|
- public View createRootView(String id, String name) {
|
42
|
|
- idRef.set(id);
|
43
|
|
- nameRef.set(name);
|
44
|
|
- viewRef.set(new View(Robolectric.setupActivity(Activity.class)));
|
45
|
|
- return viewRef.get();
|
46
|
|
- }
|
47
|
|
- };
|
48
|
|
-
|
49
|
|
- Map<String, Object> node = new HashMap() {{
|
50
|
|
- Map<String, Object> data = new HashMap<>();
|
51
|
|
- data.put("name", "MyName");
|
52
|
|
- put("id", "myUniqueId");
|
53
|
|
- put("data", data);
|
54
|
|
- put("type", "Container");
|
55
|
|
- }};
|
56
|
|
-
|
57
|
|
- ViewGroup result =
|
58
|
|
- (ViewGroup) new LayoutFactory(Robolectric.buildActivity(Activity.class).get(), rootViewCreator).create(node);
|
|
50
|
+ when(rootViewCreator.createRootView(eq(VIEW_ID), eq(VIEW_NAME))).thenReturn(mockView);
|
|
51
|
+ final LayoutNode node = createContainerNode();
|
|
52
|
+
|
|
53
|
+ final ViewGroup result = (ViewGroup) createLayoutFactory().create(node);
|
|
54
|
+
|
59
|
55
|
assertThat(result).isInstanceOf(Container.class);
|
60
|
|
- assertThat(result.getChildCount()).isEqualTo(1);
|
61
|
|
- assertThat(result.getChildAt(0)).isEqualTo(viewRef.get());
|
|
56
|
+ assertViewChildren(result, mockView);
|
62
|
57
|
}
|
63
|
58
|
|
64
|
|
-
|
65
|
59
|
@Test
|
66
|
60
|
public void returnsContainerStack() {
|
67
|
|
- final AtomicReference<String> idRef = new AtomicReference<>();
|
68
|
|
- final AtomicReference<String> nameRef = new AtomicReference<>();
|
69
|
|
- final AtomicReference<View> viewRef = new AtomicReference<>();
|
70
|
|
-
|
71
|
|
- LayoutFactory.RootViewCreator rootViewCreator = new LayoutFactory.RootViewCreator() {
|
72
|
|
- @Override
|
73
|
|
- public View createRootView(String id, String name) {
|
74
|
|
- idRef.set(id);
|
75
|
|
- nameRef.set(name);
|
76
|
|
- viewRef.set(new View(Robolectric.setupActivity(Activity.class)));
|
77
|
|
- return viewRef.get();
|
78
|
|
- }
|
79
|
|
- };
|
80
|
|
-
|
81
|
|
- Map<String, Object> node = new HashMap() {{
|
82
|
|
- Map<String, Object> data = new HashMap<>();
|
83
|
|
- data.put("name", "MyName");
|
84
|
|
- put("id", "myUniqueId");
|
85
|
|
- put("data", data);
|
86
|
|
- put("type", "Container");
|
87
|
|
- }};
|
88
|
|
-
|
89
|
|
- HashMap<String, Object> outerNode = new HashMap<>();
|
90
|
|
- outerNode.put("type", "ContainerStack");
|
91
|
|
- List<Map<String, Object>> children = new ArrayList<>();
|
92
|
|
- children.add(node);
|
93
|
|
- outerNode.put("children", children);
|
94
|
|
-
|
95
|
|
- ViewGroup result =
|
96
|
|
- (ViewGroup) new LayoutFactory(Robolectric.buildActivity(Activity.class).get(), rootViewCreator).create(outerNode);
|
|
61
|
+ when(rootViewCreator.createRootView(eq(VIEW_ID), eq(VIEW_NAME))).thenReturn(mockView);
|
|
62
|
+ final LayoutNode node = createContainerNode();
|
|
63
|
+ final LayoutNode outerNode = getContainerStackNode(node);
|
|
64
|
+
|
|
65
|
+ final ViewGroup result = (ViewGroup) createLayoutFactory().create(outerNode);
|
|
66
|
+
|
|
67
|
+ assertThat(result).isInstanceOf(ContainerStack.class);
|
|
68
|
+ ViewGroup container = (ViewGroup) assertViewChildrenCount(result, 1).get(0);
|
|
69
|
+ assertViewChildren(container, mockView);
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ @Test
|
|
73
|
+ public void returnsContainerStackWithMultipleViews() {
|
|
74
|
+ final View mockView1 = mock(View.class);
|
|
75
|
+ final View mockView2 = mock(View.class);
|
|
76
|
+ when(rootViewCreator.createRootView(eq(VIEW_ID), eq(VIEW_NAME))).thenReturn(mockView1);
|
|
77
|
+ when(rootViewCreator.createRootView(eq(OTHER_VIEW_ID), eq(OTHER_VIEW_NAME))).thenReturn(mockView2);
|
|
78
|
+
|
|
79
|
+ final LayoutNode node1 = createContainerNode(VIEW_ID, VIEW_NAME);
|
|
80
|
+ final LayoutNode node2 = createContainerNode(OTHER_VIEW_ID, OTHER_VIEW_NAME);
|
|
81
|
+ final LayoutNode outerNode = getContainerStackNode(Arrays.asList(node1, node2));
|
|
82
|
+
|
|
83
|
+ final ViewGroup result = (ViewGroup) createLayoutFactory().create(outerNode);
|
|
84
|
+
|
97
|
85
|
assertThat(result).isInstanceOf(ContainerStack.class);
|
98
|
|
- assertThat(result.getChildCount()).isEqualTo(1);
|
99
|
|
- ViewGroup container = (ViewGroup) result.getChildAt(0);
|
100
|
|
- assertThat(container.getChildCount()).isEqualTo(1);
|
101
|
|
- assertThat(container.getChildAt(0)).isEqualTo(viewRef.get());
|
|
86
|
+ List<View> containers = assertViewChildrenCount(result, 2);
|
|
87
|
+ ViewGroup container1 = (ViewGroup) containers.get(0);
|
|
88
|
+ ViewGroup container2 = (ViewGroup) containers.get(1);
|
|
89
|
+ assertViewChildren(container1, mockView1);
|
|
90
|
+ assertViewChildren(container2, mockView2);
|
|
91
|
+ }
|
|
92
|
+
|
|
93
|
+ private LayoutFactory createLayoutFactory() {
|
|
94
|
+ return new LayoutFactory(Robolectric.buildActivity(Activity.class).get(), rootViewCreator);
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+ private LayoutNode getContainerStackNode(LayoutNode innerNode) {
|
|
98
|
+ return getContainerStackNode(Arrays.asList(innerNode));
|
|
99
|
+ }
|
|
100
|
+
|
|
101
|
+ private LayoutNode getContainerStackNode(List<LayoutNode> children) {
|
|
102
|
+ LayoutNode outerNode = new LayoutNode();
|
|
103
|
+ outerNode.type = "ContainerStack";
|
|
104
|
+ outerNode.children = children;
|
|
105
|
+ return outerNode;
|
|
106
|
+ }
|
|
107
|
+
|
|
108
|
+ private LayoutNode createContainerNode() {
|
|
109
|
+ return createContainerNode(VIEW_ID, VIEW_NAME);
|
|
110
|
+ }
|
|
111
|
+
|
|
112
|
+ private LayoutNode createContainerNode(final String id, final String name) {
|
|
113
|
+ return new LayoutNode(id, "Container", new HashMap<String, Object>() {{ put("name", name); }});
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+ private List<View> assertViewChildrenCount(ViewGroup view, int count) {
|
|
117
|
+ assertThat(view.getChildCount()).isEqualTo(count);
|
|
118
|
+
|
|
119
|
+ final List<View> children = new ArrayList<>(count);
|
|
120
|
+ for (int i = 0; i < count; i++) {
|
|
121
|
+ children.add(view.getChildAt(i));
|
|
122
|
+ }
|
|
123
|
+ return children;
|
|
124
|
+ }
|
|
125
|
+
|
|
126
|
+ private void assertViewChildren(ViewGroup view, View... children) {
|
|
127
|
+ final List<View> childViews = assertViewChildrenCount(view, children.length);
|
|
128
|
+ assertThat(childViews).isEqualTo(Arrays.asList(children));
|
102
|
129
|
}
|
103
|
130
|
}
|