Daniel Zlotin 7 lat temu
rodzic
commit
b0871a8a10

+ 3
- 3
lib/android/app/src/main/java/com/reactnativenavigation/controllers/CommandsHandler.java Wyświetl plik

@@ -12,11 +12,11 @@ import com.reactnativenavigation.layout.bottomtabs.BottomTabsCreator;
12 12
 import com.reactnativenavigation.layout.parse.LayoutNode;
13 13
 import com.reactnativenavigation.utils.UiThread;
14 14
 
15
-import java.util.Map;
15
+import org.json.JSONObject;
16 16
 
17 17
 public class CommandsHandler {
18 18
 
19
-	public void setRoot(final NavigationActivity activity, final Map<String, Object> layoutTree) {
19
+	public void setRoot(final NavigationActivity activity, final JSONObject layoutTree) {
20 20
 		LayoutFactory factory =
21 21
 				new LayoutFactory(activity, new LayoutFactory.ReactRootViewCreator() {
22 22
 					@Override
@@ -34,7 +34,7 @@ public class CommandsHandler {
34 34
 		activity.setContentView(rootView);
35 35
 	}
36 36
 
37
-	public void push(final NavigationActivity activity, String onContainerId, final Map<String, Object> layoutTree) {
37
+	public void push(final NavigationActivity activity, String onContainerId, JSONObject layoutTree) {
38 38
 		LayoutFactory factory =
39 39
 				new LayoutFactory(activity, new LayoutFactory.ReactRootViewCreator() {
40 40
 					@Override

+ 1
- 1
lib/android/app/src/main/java/com/reactnativenavigation/layout/LayoutFactory.java Wyświetl plik

@@ -81,7 +81,7 @@ public class LayoutFactory {
81 81
 	}
82 82
 
83 83
 	private View createContainerView(LayoutNode node) {
84
-		final String name = (String) node.data.get("name");
84
+		final String name = node.data.optString("name");
85 85
 		Container container = new Container(activity, reactRootViewCreator, node.id, name);
86 86
 		container.setId(CompatUtils.generateViewId());
87 87
 		return container;

+ 17
- 17
lib/android/app/src/main/java/com/reactnativenavigation/layout/parse/LayoutNode.java Wyświetl plik

@@ -1,9 +1,10 @@
1 1
 package com.reactnativenavigation.layout.parse;
2 2
 
3
+import org.json.JSONArray;
4
+import org.json.JSONObject;
5
+
3 6
 import java.util.ArrayList;
4
-import java.util.HashMap;
5 7
 import java.util.List;
6
-import java.util.Map;
7 8
 
8 9
 public class LayoutNode {
9 10
 	public enum Type {
@@ -22,15 +23,14 @@ public class LayoutNode {
22 23
 
23 24
 	public final String id;
24 25
 	public final Type type;
25
-	public final Map<String, Object> data;
26
-
26
+	public final JSONObject data;
27 27
 	public final List<LayoutNode> children;
28 28
 
29 29
 	public LayoutNode(String id, Type type) {
30
-		this(id, type, new HashMap<String, Object>(), new ArrayList<LayoutNode>());
30
+		this(id, type, new JSONObject(), new ArrayList<LayoutNode>());
31 31
 	}
32 32
 
33
-	public LayoutNode(String id, Type type, Map<String, Object> data, List<LayoutNode> children) {
33
+	public LayoutNode(String id, Type type, JSONObject data, List<LayoutNode> children) {
34 34
 		this.id = id;
35 35
 		this.type = type;
36 36
 		this.data = data;
@@ -38,22 +38,22 @@ public class LayoutNode {
38 38
 	}
39 39
 
40 40
 	@SuppressWarnings("unchecked")
41
-	public static LayoutNode fromTree(Map<String, Object> layoutTree) {
42
-		String id = (String) layoutTree.get("id");
43
-		LayoutNode.Type type = LayoutNode.Type.fromString((String) layoutTree.get("type"));
41
+	public static LayoutNode fromTree(JSONObject layoutTree) {
42
+		String id = layoutTree.optString("id");
43
+		LayoutNode.Type type = LayoutNode.Type.fromString(layoutTree.optString("type"));
44 44
 
45
-		Map<String, Object> data;
46
-		if (layoutTree.containsKey("data")) {
47
-			data = (Map<String, Object>) layoutTree.get("data");
45
+		JSONObject data;
46
+		if (layoutTree.has("data")) {
47
+			data = layoutTree.optJSONObject("data");
48 48
 		} else {
49
-			data = new HashMap<>();
49
+			data = new JSONObject();
50 50
 		}
51 51
 
52 52
 		List<LayoutNode> children = new ArrayList<>();
53
-		if (layoutTree.containsKey("children")) {
54
-			List<Object> rawChildren = (List<Object>) layoutTree.get("children");
55
-			for (Object rawChild : rawChildren) {
56
-				children.add(LayoutNode.fromTree((Map<String, Object>) rawChild));
53
+		if (layoutTree.has("children")) {
54
+			JSONArray rawChildren = layoutTree.optJSONArray("children");
55
+			for (int i = 0; i < rawChildren.length(); i++) {
56
+				children.add(LayoutNode.fromTree(rawChildren.optJSONObject(i)));
57 57
 			}
58 58
 		}
59 59
 

+ 38
- 35
lib/android/app/src/main/java/com/reactnativenavigation/react/ArgsParser.java Wyświetl plik

@@ -4,58 +4,61 @@ import com.facebook.react.bridge.ReadableArray;
4 4
 import com.facebook.react.bridge.ReadableMap;
5 5
 import com.facebook.react.bridge.ReadableMapKeySetIterator;
6 6
 
7
-import java.util.ArrayList;
8
-import java.util.HashMap;
9
-import java.util.List;
10
-import java.util.Map;
7
+import org.json.JSONArray;
8
+import org.json.JSONException;
9
+import org.json.JSONObject;
11 10
 
12 11
 public class ArgsParser {
13
-	public static Map<String, Object> parse(ReadableMap map) {
14
-		Map<String, Object> result = new HashMap<>();
15
-		ReadableMapKeySetIterator it = map.keySetIterator();
16
-		while (it.hasNextKey()) {
17
-			String key = it.nextKey();
18
-			switch (map.getType(key)) {
19
-				case String:
20
-					result.put(key, map.getString(key));
21
-					break;
22
-				case Number:
23
-					result.put(key, parseNumber(map, key));
24
-					break;
25
-				case Boolean:
26
-					result.put(key, map.getBoolean(key));
27
-					break;
28
-				case Array:
29
-					result.put(key, parseArray(map.getArray(key)));
30
-					break;
31
-				case Map:
32
-					result.put(key, parse(map.getMap(key)));
33
-					break;
34
-				default:
35
-					break;
12
+	public static JSONObject parse(ReadableMap map) {
13
+		try {
14
+			ReadableMapKeySetIterator it = map.keySetIterator();
15
+			JSONObject result = new JSONObject();
16
+			while (it.hasNextKey()) {
17
+				String key = it.nextKey();
18
+				switch (map.getType(key)) {
19
+					case String:
20
+						result.put(key, map.getString(key));
21
+						break;
22
+					case Number:
23
+						result.put(key, parseNumber(map, key));
24
+						break;
25
+					case Boolean:
26
+						result.put(key, map.getBoolean(key));
27
+						break;
28
+					case Array:
29
+						result.put(key, parse(map.getArray(key)));
30
+						break;
31
+					case Map:
32
+						result.put(key, parse(map.getMap(key)));
33
+						break;
34
+					default:
35
+						break;
36
+				}
36 37
 			}
38
+			return result;
39
+		} catch (JSONException e) {
40
+			throw new RuntimeException(e);
37 41
 		}
38
-		return result;
39 42
 	}
40 43
 
41
-	public static List<Object> parseArray(ReadableArray arr) {
42
-		List<Object> result = new ArrayList<>();
44
+	public static JSONArray parse(ReadableArray arr) {
45
+		JSONArray result = new JSONArray();
43 46
 		for (int i = 0; i < arr.size(); i++) {
44 47
 			switch (arr.getType(i)) {
45 48
 				case String:
46
-					result.add(arr.getString(i));
49
+					result.put(arr.getString(i));
47 50
 					break;
48 51
 				case Number:
49
-					result.add(parseNumber(arr, i));
52
+					result.put(parseNumber(arr, i));
50 53
 					break;
51 54
 				case Boolean:
52
-					result.add(arr.getBoolean(i));
55
+					result.put(arr.getBoolean(i));
53 56
 					break;
54 57
 				case Array:
55
-					result.add(parseArray(arr.getArray(i)));
58
+					result.put(parse(arr.getArray(i)));
56 59
 					break;
57 60
 				case Map:
58
-					result.add(parse(arr.getMap(i)));
61
+					result.put(parse(arr.getMap(i)));
59 62
 					break;
60 63
 				default:
61 64
 					break;

+ 13
- 14
lib/android/app/src/test/java/com/reactnativenavigation/layout/LayoutFactoryTest.java Wyświetl plik

@@ -11,6 +11,8 @@ import com.reactnativenavigation.layout.bottomtabs.BottomTabsCreator;
11 11
 import com.reactnativenavigation.layout.bottomtabs.BottomTabsLayout;
12 12
 import com.reactnativenavigation.layout.parse.LayoutNode;
13 13
 
14
+import org.json.JSONException;
15
+import org.json.JSONObject;
14 16
 import org.junit.Before;
15 17
 import org.junit.Ignore;
16 18
 import org.junit.Test;
@@ -18,7 +20,6 @@ import org.robolectric.Robolectric;
18 20
 
19 21
 import java.util.Arrays;
20 22
 import java.util.Collections;
21
-import java.util.HashMap;
22 23
 import java.util.List;
23 24
 
24 25
 import static org.assertj.core.api.Java6Assertions.assertThat;
@@ -170,7 +171,7 @@ public class LayoutFactoryTest extends BaseTest {
170 171
 	}
171 172
 
172 173
 	@Test
173
-	public void pushScreenToFirstBottomTab() {
174
+	public void pushScreenToFirstBottomTab() throws Exception {
174 175
 		BottomTabs bottomTabsMock = mock(BottomTabs.class);
175 176
 		when(bottomTabsMock.size()).thenReturn(0, 1);
176 177
 
@@ -193,7 +194,7 @@ public class LayoutFactoryTest extends BaseTest {
193 194
 	}
194 195
 
195 196
 	@Test
196
-	public void popScreenFromFirstBottomTab() {
197
+	public void popScreenFromFirstBottomTab() throws Exception {
197 198
 		BottomTabs bottomTabsMock = mock(BottomTabs.class);
198 199
 		when(bottomTabsMock.size()).thenReturn(0, 1);
199 200
 
@@ -263,20 +264,20 @@ public class LayoutFactoryTest extends BaseTest {
263 264
 		assertThat(result.getChildAt(0)).isEqualTo(mockView);
264 265
 	}
265 266
 
266
-	private void pushContainer(ContainerStackLayout containerStackLayout, String screenId, String reactRootViewKey, View rootView) {
267
+	private void pushContainer(ContainerStackLayout containerStackLayout, String screenId, String reactRootViewKey, View rootView) throws Exception {
267 268
 		when(reactRootViewCreator.create(eq(screenId), eq(reactRootViewKey))).thenReturn(rootView);
268 269
 		View pushedContainer = createLayoutFactory().create(createContainerNode(screenId, reactRootViewKey));
269 270
 		containerStackLayout.push(pushedContainer);
270 271
 	}
271 272
 
272
-	private void pushContainer(BottomTabsLayout containerStackLayout, String screenId, String reactRootViewKey, View rootView) {
273
+	private void pushContainer(BottomTabsLayout containerStackLayout, String screenId, String reactRootViewKey, View rootView) throws Exception {
273 274
 		when(reactRootViewCreator.create(eq(screenId), eq(reactRootViewKey))).thenReturn(rootView);
274 275
 		View pushedContainer = createLayoutFactory().create(createContainerNode(screenId, reactRootViewKey));
275 276
 		containerStackLayout.push(pushedContainer);
276 277
 	}
277 278
 
278 279
 	@Test
279
-	public void popScreenFromScreenStackLayout() {
280
+	public void popScreenFromScreenStackLayout() throws Exception {
280 281
 		when(reactRootViewCreator.create(eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);
281 282
 		final LayoutNode container = createContainerNode();
282 283
 		final LayoutNode stackNode = createContainerStackNode(container);
@@ -295,7 +296,7 @@ public class LayoutFactoryTest extends BaseTest {
295 296
 	@Ignore
296 297
 	public void throwsExceptionForUnknownType() throws Exception {
297 298
 		when(reactRootViewCreator.create(eq(NODE_ID), eq(REACT_ROOT_VIEW_KEY))).thenReturn(mockView);
298
-		final LayoutNode node = new LayoutNode(NODE_ID, null, Collections.<String, Object>emptyMap(), Collections.<LayoutNode>emptyList());
299
+		final LayoutNode node = new LayoutNode(NODE_ID, null, new JSONObject(), Collections.<LayoutNode>emptyList());
299 300
 
300 301
 		createLayoutFactory().create(node);
301 302
 	}
@@ -314,24 +315,22 @@ public class LayoutFactoryTest extends BaseTest {
314 315
 		return new LayoutFactory(activity, reactRootViewCreator, bottomTabsCreator);
315 316
 	}
316 317
 
317
-	private LayoutNode createContainerNode() {
318
+	private LayoutNode createContainerNode() throws Exception {
318 319
 		return createContainerNode(NODE_ID, REACT_ROOT_VIEW_KEY);
319 320
 	}
320 321
 
321
-	private LayoutNode createSideMenuLeftNode() {
322
+	private LayoutNode createSideMenuLeftNode() throws Exception {
322 323
 		List<LayoutNode> children = Arrays.asList(createContainerNode());
323 324
 		return new LayoutNode("SideMenuLeft", LayoutNode.Type.SideMenuLeft, null, children);
324 325
 	}
325 326
 
326
-	private LayoutNode createSideMenuRightNode() {
327
+	private LayoutNode createSideMenuRightNode() throws Exception {
327 328
 		List<LayoutNode> children = Arrays.asList(createContainerNode());
328 329
 		return new LayoutNode("SideMenuRight", LayoutNode.Type.SideMenuRight, null, children);
329 330
 	}
330 331
 
331
-	private LayoutNode createContainerNode(final String id, final String name) {
332
-		return new LayoutNode(id, LayoutNode.Type.Container, new HashMap<String, Object>() {{
333
-			put("name", name);
334
-		}}, null);
332
+	private LayoutNode createContainerNode(final String id, final String name) throws JSONException {
333
+		return new LayoutNode(id, LayoutNode.Type.Container, new JSONObject().put("name", name), null);
335 334
 	}
336 335
 
337 336
 	private LayoutNode createSideMenuContainerNode(List<LayoutNode> children) {

+ 9
- 21
lib/android/app/src/test/java/com/reactnativenavigation/layout/parse/LayoutNodeTest.java Wyświetl plik

@@ -2,15 +2,10 @@ package com.reactnativenavigation.layout.parse;
2 2
 
3 3
 import com.reactnativenavigation.BaseTest;
4 4
 
5
+import org.json.JSONObject;
5 6
 import org.junit.Test;
6 7
 
7
-import java.util.ArrayList;
8
-import java.util.HashMap;
9
-import java.util.List;
10
-import java.util.Map;
11
-
12 8
 import static org.assertj.core.api.Java6Assertions.assertThat;
13
-import static org.assertj.core.api.Java6Assertions.entry;
14 9
 
15 10
 public class LayoutNodeTest extends BaseTest {
16 11
 	@Test
@@ -18,7 +13,7 @@ public class LayoutNodeTest extends BaseTest {
18 13
 		LayoutNode node = new LayoutNode("the id", LayoutNode.Type.Container);
19 14
 		assertThat(node.id).isEqualTo("the id");
20 15
 		assertThat(node.type).isEqualTo(LayoutNode.Type.Container);
21
-		assertThat(node.data).isEmpty();
16
+		assertThat(node.data.keys()).isEmpty();
22 17
 		assertThat(node.children).isEmpty();
23 18
 	}
24 19
 
@@ -34,29 +29,22 @@ public class LayoutNodeTest extends BaseTest {
34 29
 
35 30
 	@Test
36 31
 	public void parseFromTree() throws Exception {
37
-		Map<String, Object> tree = new HashMap<>();
38
-		tree.put("id", "node1");
39
-		tree.put("type", "ContainerStack");
40
-		Map<String, Object> rawData = new HashMap<>();
41
-		rawData.put("dataKey", "dataValue");
42
-		tree.put("data", rawData);
43
-		List<Object> rawChildren = new ArrayList<>();
44
-		Map<String, Object> rawChild = new HashMap<>();
45
-		rawChild.put("id", "childId1");
46
-		rawChild.put("type", "Container");
47
-		rawChildren.add(rawChild);
48
-		tree.put("children", rawChildren);
32
+		JSONObject tree = new JSONObject("{id: node1, " +
33
+				"type: ContainerStack, " +
34
+				"data: {dataKey: dataValue}, " +
35
+				"children: [{id: childId1, type: Container}]}");
49 36
 
50 37
 		LayoutNode result = LayoutNode.fromTree(tree);
51 38
 
52 39
 		assertThat(result).isNotNull();
53 40
 		assertThat(result.id).isEqualTo("node1");
54 41
 		assertThat(result.type).isEqualTo(LayoutNode.Type.ContainerStack);
55
-		assertThat(result.data).containsOnly(entry("dataKey", "dataValue"));
42
+		assertThat(result.data.length()).isEqualTo(1);
43
+		assertThat(result.data.getString("dataKey")).isEqualTo("dataValue");
56 44
 		assertThat(result.children).hasSize(1);
57 45
 		assertThat(result.children.get(0).id).isEqualTo("childId1");
58 46
 		assertThat(result.children.get(0).type).isEqualTo(LayoutNode.Type.Container);
59
-		assertThat(result.children.get(0).data).isEmpty();
47
+		assertThat(result.children.get(0).data.keys()).isEmpty();
60 48
 		assertThat(result.children.get(0).children).isEmpty();
61 49
 	}
62 50
 }

+ 28
- 17
lib/android/app/src/test/java/com/reactnativenavigation/react/ArgsParserTest.java Wyświetl plik

@@ -4,36 +4,41 @@ import com.facebook.react.bridge.JavaOnlyArray;
4 4
 import com.facebook.react.bridge.JavaOnlyMap;
5 5
 import com.reactnativenavigation.BaseTest;
6 6
 
7
+import org.json.JSONArray;
8
+import org.json.JSONObject;
7 9
 import org.junit.Test;
8 10
 
9
-import java.util.ArrayList;
10
-import java.util.HashMap;
11
-import java.util.List;
12
-import java.util.Map;
13
-
14 11
 import static org.assertj.core.api.Java6Assertions.assertThat;
15
-import static org.assertj.core.api.Java6Assertions.entry;
16 12
 
17 13
 public class ArgsParserTest extends BaseTest {
18 14
 	@Test
19
-	public void parsesToMap() throws Exception {
15
+	public void parsesMap() throws Exception {
20 16
 		JavaOnlyMap input = new JavaOnlyMap();
21 17
 		input.putString("keyString", "stringValue");
22 18
 		input.putInt("keyInt", 123);
23 19
 		input.putDouble("keyDouble", 123.456);
24 20
 		input.putBoolean("keyBoolean", true);
25 21
 		input.putArray("keyArray", new JavaOnlyArray());
22
+		input.putMap("keyMap", new JavaOnlyMap());
26 23
 		input.putNull("bla");
27 24
 
28
-		Map<String, Object> result = ArgsParser.parse(input);
25
+		JSONObject result = ArgsParser.parse(input);
26
+
27
+
28
+		assertThat(result.keys()).containsOnly(
29
+				"keyString",
30
+				"keyInt",
31
+				"keyDouble",
32
+				"keyBoolean",
33
+				"keyMap",
34
+				"keyArray");
29 35
 
30
-		assertThat(result).containsOnly(
31
-				entry("keyString", "stringValue"),
32
-				entry("keyInt", 123),
33
-				entry("keyDouble", 123.456),
34
-				entry("keyBoolean", true),
35
-				entry("keyArray", new ArrayList<>())
36
-		);
36
+		assertThat(result.get("keyString")).isEqualTo("stringValue");
37
+		assertThat(result.get("keyInt")).isEqualTo(123);
38
+		assertThat(result.get("keyDouble")).isEqualTo(123.456);
39
+		assertThat(result.get("keyBoolean")).isEqualTo(true);
40
+		assertThat(result.getJSONObject("keyMap").keys()).isEmpty();
41
+		assertThat(result.getJSONArray("keyArray").length()).isZero();
37 42
 	}
38 43
 
39 44
 	@Test
@@ -47,7 +52,13 @@ public class ArgsParserTest extends BaseTest {
47 52
 		input.pushMap(new JavaOnlyMap());
48 53
 		input.pushNull();
49 54
 
50
-		List<Object> result = ArgsParser.parseArray(input);
51
-		assertThat(result).containsExactly("Hello", 123, 123.456, true, new ArrayList<>(), new HashMap<>());
55
+		JSONArray result = ArgsParser.parse(input);
56
+		assertThat(result.length()).isEqualTo(6);
57
+		assertThat(result.get(0)).isEqualTo("Hello");
58
+		assertThat(result.get(1)).isEqualTo(123);
59
+		assertThat(result.get(2)).isEqualTo(123.456);
60
+		assertThat(result.get(3)).isEqualTo(true);
61
+		assertThat(result.getJSONArray(4).length()).isZero();
62
+		assertThat(result.getJSONObject(5).keys()).isEmpty();
52 63
 	}
53 64
 }

+ 3
- 3
lib/android/app/src/test/java/com/reactnativenavigation/react/NavigationModuleTest.java Wyświetl plik

@@ -8,13 +8,13 @@ import com.reactnativenavigation.NavigationActivity;
8 8
 import com.reactnativenavigation.NavigationApplication;
9 9
 import com.reactnativenavigation.controllers.CommandsHandler;
10 10
 
11
+import org.json.JSONObject;
11 12
 import org.junit.Before;
12 13
 import org.junit.Test;
13 14
 import org.mockito.ArgumentCaptor;
14 15
 import org.robolectric.Robolectric;
15 16
 
16 17
 import java.lang.reflect.Method;
17
-import java.util.HashMap;
18 18
 
19 19
 import static org.assertj.core.api.Java6Assertions.assertThat;
20 20
 import static org.mockito.ArgumentMatchers.any;
@@ -63,8 +63,8 @@ public class NavigationModuleTest extends BaseTest {
63 63
 		JavaOnlyMap input = new JavaOnlyMap();
64 64
 		input.putString("key", "value");
65 65
 		uut.setRoot(input);
66
-		ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(HashMap.class);
67
-		verify(NavigationApplication.instance.getConfig().commandsHandler, times(1)).setRoot((NavigationActivity) any(), (HashMap<String, Object>) captor.capture());
66
+		ArgumentCaptor<JSONObject> captor = ArgumentCaptor.forClass(JSONObject.class);
67
+		verify(NavigationApplication.instance.getConfig().commandsHandler, times(1)).setRoot((NavigationActivity) any(), captor.capture());
68 68
 		assertThat(captor.getAllValues()).hasSize(1);
69 69
 		assertThat(captor.getValue()).isNotNull();
70 70
 	}

+ 1
- 1
scripts/test.unit.android.js Wyświetl plik

@@ -2,5 +2,5 @@ const exec = require('shell-utils').exec;
2 2
 run();
3 3
 
4 4
 function run() {
5
-  exec.execSync(`cd lib/android && ./gradlew clean unitTest`);
5
+  exec.execSync(`cd lib/android && ./gradlew ${process.env.CI ? 'clean' : ''} unitTest`);
6 6
 }