|
@@ -0,0 +1,103 @@
|
|
1
|
+package com.reactnativenavigation;
|
|
2
|
+
|
|
3
|
+import android.app.Activity;
|
|
4
|
+import android.view.View;
|
|
5
|
+import android.view.ViewGroup;
|
|
6
|
+
|
|
7
|
+import com.reactnativenavigation.layout.Container;
|
|
8
|
+import com.reactnativenavigation.layout.ContainerStack;
|
|
9
|
+import com.reactnativenavigation.layout.LayoutFactory;
|
|
10
|
+
|
|
11
|
+import org.junit.Before;
|
|
12
|
+import org.junit.Test;
|
|
13
|
+import org.junit.runner.RunWith;
|
|
14
|
+import org.robolectric.Robolectric;
|
|
15
|
+import org.robolectric.RobolectricTestRunner;
|
|
16
|
+
|
|
17
|
+import java.util.ArrayList;
|
|
18
|
+import java.util.HashMap;
|
|
19
|
+import java.util.List;
|
|
20
|
+import java.util.Map;
|
|
21
|
+import java.util.concurrent.atomic.AtomicReference;
|
|
22
|
+
|
|
23
|
+import static org.assertj.core.api.Java6Assertions.assertThat;
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+@RunWith(RobolectricTestRunner.class)
|
|
27
|
+public class LayoutFactoryTest {
|
|
28
|
+ @Before
|
|
29
|
+ public void setUp() {
|
|
30
|
+
|
|
31
|
+ }
|
|
32
|
+
|
|
33
|
+ @Test
|
|
34
|
+ 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);
|
|
59
|
+ assertThat(result).isInstanceOf(Container.class);
|
|
60
|
+ assertThat(result.getChildCount()).isEqualTo(1);
|
|
61
|
+ assertThat(result.getChildAt(0)).isEqualTo(viewRef.get());
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+ @Test
|
|
66
|
+ 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);
|
|
97
|
+ 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());
|
|
102
|
+ }
|
|
103
|
+}
|