瀏覽代碼

android: tests for utils

Daniel Zlotin 7 年之前
父節點
當前提交
f500505b87

+ 5
- 5
android/app/src/main/java/com/reactnativenavigation/layout/LayoutFactory.java 查看文件

@@ -8,7 +8,7 @@ import android.view.ViewGroup.LayoutParams;
8 8
 
9 9
 import com.reactnativenavigation.layout.bottomtabs.BottomTabsCreator;
10 10
 import com.reactnativenavigation.layout.bottomtabs.BottomTabsLayout;
11
-import com.reactnativenavigation.utils.ViewIdGenerator;
11
+import com.reactnativenavigation.utils.CompatUtils;
12 12
 
13 13
 import java.util.List;
14 14
 
@@ -63,7 +63,7 @@ public class LayoutFactory {
63 63
 
64 64
 	private View createSideMenuLeft(LayoutNode node) {
65 65
 		View view = create(node.children.get(0));
66
-		view.setId(ViewIdGenerator.generate());
66
+		view.setId(CompatUtils.generateViewId());
67 67
 		DrawerLayout.LayoutParams lp = new DrawerLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
68 68
 		lp.gravity = Gravity.LEFT;
69 69
 		view.setLayoutParams(lp);
@@ -72,7 +72,7 @@ public class LayoutFactory {
72 72
 
73 73
 	private View createSideMenuRight(LayoutNode node) {
74 74
 		View view = create(node.children.get(0));
75
-		view.setId(ViewIdGenerator.generate());
75
+		view.setId(CompatUtils.generateViewId());
76 76
 		DrawerLayout.LayoutParams lp = new DrawerLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
77 77
 		lp.gravity = Gravity.RIGHT;
78 78
 		view.setLayoutParams(lp);
@@ -82,14 +82,14 @@ public class LayoutFactory {
82 82
 	private View createContainerView(LayoutNode node) {
83 83
 		final String name = (String) node.data.get("name");
84 84
 		Container container = new Container(activity, reactRootViewCreator, node.id, name);
85
-		container.setId(ViewIdGenerator.generate());
85
+		container.setId(CompatUtils.generateViewId());
86 86
 		return container;
87 87
 
88 88
 	}
89 89
 
90 90
 	private View createContainerStackView(LayoutNode node) {
91 91
 		final ContainerStackLayout containerStack = new ContainerStackLayout(activity);
92
-		containerStack.setId(ViewIdGenerator.generate());
92
+		containerStack.setId(CompatUtils.generateViewId());
93 93
 		addChildrenNodes(containerStack, node.children);
94 94
 		return containerStack;
95 95
 	}

+ 26
- 0
android/app/src/main/java/com/reactnativenavigation/utils/CompatUtils.java 查看文件

@@ -0,0 +1,26 @@
1
+package com.reactnativenavigation.utils;
2
+
3
+import android.os.Build;
4
+import android.view.View;
5
+
6
+import java.util.concurrent.atomic.AtomicInteger;
7
+
8
+public class CompatUtils {
9
+	private static final AtomicInteger viewId = new AtomicInteger(1);
10
+
11
+	public static int generateViewId() {
12
+		if (Build.VERSION.SDK_INT >= 17) {
13
+			return View.generateViewId();
14
+		} else {
15
+			while (true) {
16
+				final int result = viewId.get();
17
+				// aapt-generated IDs have the high byte nonzero; clamp to the range under that.
18
+				int newValue = result + 1;
19
+				if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
20
+				if (viewId.compareAndSet(result, newValue)) {
21
+					return result;
22
+				}
23
+			}
24
+		}
25
+	}
26
+}

+ 0
- 30
android/app/src/main/java/com/reactnativenavigation/utils/ViewIdGenerator.java 查看文件

@@ -1,30 +0,0 @@
1
-package com.reactnativenavigation.utils;
2
-
3
-import android.os.Build;
4
-import android.view.View;
5
-
6
-import java.util.concurrent.atomic.AtomicInteger;
7
-
8
-public class ViewIdGenerator {
9
-	private static final AtomicInteger viewId = new AtomicInteger(1);
10
-
11
-	public static int generate() {
12
-		if (Build.VERSION.SDK_INT >= 17) {
13
-			return View.generateViewId();
14
-		} else {
15
-			return compatGenerateViewId();
16
-		}
17
-	}
18
-
19
-	private static int compatGenerateViewId() {
20
-		while (true) {
21
-			final int result = viewId.get();
22
-			// aapt-generated IDs have the high byte nonzero; clamp to the range under that.
23
-			int newValue = result + 1;
24
-			if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
25
-			if (viewId.compareAndSet(result, newValue)) {
26
-				return result;
27
-			}
28
-		}
29
-	}
30
-}

+ 17
- 0
android/app/src/test/java/com/reactnativenavigation/utils/CompatUtilsTest.java 查看文件

@@ -0,0 +1,17 @@
1
+package com.reactnativenavigation.utils;
2
+
3
+import com.reactnativenavigation.BaseTest;
4
+
5
+import org.junit.Test;
6
+
7
+import static org.assertj.core.api.Java6Assertions.assertThat;
8
+
9
+public class CompatUtilsTest extends BaseTest {
10
+
11
+	@Test
12
+	public void generateViewId() throws Exception {
13
+		assertThat(CompatUtils.generateViewId())
14
+				.isPositive()
15
+				.isNotEqualTo(CompatUtils.generateViewId());
16
+	}
17
+}

+ 32
- 0
android/app/src/test/java/com/reactnativenavigation/utils/UiThreadTest.java 查看文件

@@ -0,0 +1,32 @@
1
+package com.reactnativenavigation.utils;
2
+
3
+import com.reactnativenavigation.BaseTest;
4
+
5
+import org.junit.Test;
6
+import org.robolectric.shadows.ShadowLooper;
7
+
8
+import static org.mockito.Mockito.mock;
9
+import static org.mockito.Mockito.times;
10
+import static org.mockito.Mockito.verify;
11
+import static org.mockito.Mockito.verifyZeroInteractions;
12
+
13
+public class UiThreadTest extends BaseTest {
14
+	@Test
15
+	public void postOnUiThread() throws Exception {
16
+		Runnable task = mock(Runnable.class);
17
+		ShadowLooper.pauseMainLooper();
18
+		UiThread.post(task);
19
+		verifyZeroInteractions(task);
20
+		ShadowLooper.runUiThreadTasks();
21
+		verify(task, times(1)).run();
22
+	}
23
+
24
+	@Test
25
+	public void postDelayedOnUiThread() throws Exception {
26
+		Runnable task = mock(Runnable.class);
27
+		UiThread.postDelayed(task, 1000);
28
+		verifyZeroInteractions(task);
29
+		ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
30
+		verify(task, times(1)).run();
31
+	}
32
+}

+ 0
- 1
android/prepare-robolectric.gradle 查看文件

@@ -5,7 +5,6 @@ configurations.create('robolectricRuntime')
5 5
 dependencies {
6 6
     testCompile "org.khronos:opengl-api:gl1.1-android-2.1_r1"
7 7
 
8
-    // robolectricRuntime "org.robolectric:android-all:6.0.1_r3-robolectric-0"
9 8
     robolectricRuntime "org.robolectric:android-all:7.1.0_r7-robolectric-0"
10 9
     robolectricRuntime "org.robolectric:shadows-core:3.3"
11 10
     robolectricRuntime "org.robolectric:robolectric-utils:3.3"

+ 1
- 1
playground/e2e/app.test.js 查看文件

@@ -1,4 +1,4 @@
1
-describe('app', () => {
1
+describe('top level api', () => {
2 2
   beforeEach(async () => {
3 3
     await device.relaunchApp();
4 4
   });