Browse Source

android: tests for utils

Daniel Zlotin 7 years ago
parent
commit
f500505b87

+ 5
- 5
android/app/src/main/java/com/reactnativenavigation/layout/LayoutFactory.java View File

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

+ 26
- 0
android/app/src/main/java/com/reactnativenavigation/utils/CompatUtils.java View File

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 View File

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 View File

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 View File

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 View File

5
 dependencies {
5
 dependencies {
6
     testCompile "org.khronos:opengl-api:gl1.1-android-2.1_r1"
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
     robolectricRuntime "org.robolectric:android-all:7.1.0_r7-robolectric-0"
8
     robolectricRuntime "org.robolectric:android-all:7.1.0_r7-robolectric-0"
10
     robolectricRuntime "org.robolectric:shadows-core:3.3"
9
     robolectricRuntime "org.robolectric:shadows-core:3.3"
11
     robolectricRuntime "org.robolectric:robolectric-utils:3.3"
10
     robolectricRuntime "org.robolectric:robolectric-utils:3.3"

+ 1
- 1
playground/e2e/app.test.js View File

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