Browse Source

test env and eventbus fixes

Daniel Zlotin 7 years ago
parent
commit
51624f7998

+ 18
- 1
android/app/build.gradle View File

@@ -1,3 +1,12 @@
1
+buildscript {
2
+    repositories {
3
+        jcenter()
4
+    }
5
+    dependencies {
6
+        classpath 'com.android.tools.build:gradle:2.1.3'
7
+    }
8
+}
9
+
1 10
 apply plugin: 'com.android.library'
2 11
 
3 12
 android {
@@ -30,9 +39,11 @@ android {
30 39
 }
31 40
 
32 41
 repositories {
42
+    mavenLocal()
43
+    jcenter()
33 44
     maven {
34 45
         // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
35
-        url '../../../react-native/android'
46
+        url "$rootDir/../../example-redux/node_modules/react-native/android"
36 47
     }
37 48
 }
38 49
 
@@ -54,4 +65,10 @@ dependencies {
54 65
 
55 66
     testCompile "junit:junit:4.12"
56 67
     testCompile "org.robolectric:robolectric:3.1.1"
68
+    testCompile 'org.assertj:assertj-core:3.5.2'
69
+}
70
+
71
+task unit(dependsOn: 'testLibraryDebugUnitTest') << {
72
+    println 'Finished running unit all tests'
73
+    println 'report at file://' + tasks.testLibraryDebugUnitTest.outputs.files.last().absolutePath + '/index.html'
57 74
 }

+ 25
- 12
android/app/src/main/java/com/reactnativenavigation/events/EventBus.java View File

@@ -1,31 +1,44 @@
1 1
 package com.reactnativenavigation.events;
2 2
 
3
+import java.lang.ref.WeakReference;
3 4
 import java.util.ArrayList;
4 5
 import java.util.List;
5 6
 
6 7
 public enum EventBus {
7 8
     instance;
8 9
 
9
-    List<Subscriber> subscribers;
10
-
11
-    EventBus() {
12
-        subscribers = new ArrayList<>();
13
-    }
10
+    private final List<WeakReference<Subscriber>> subscribers = new ArrayList<>();
14 11
 
15 12
     public void register(Subscriber subscriber) {
16
-        if (subscribers.contains(subscriber)) {
17
-            throw new RuntimeException("Subscriber already registered");
18
-        }
19
-        subscribers.add(subscriber);
13
+        if (isSubscribed(subscriber)) return;
14
+        subscribers.add(new WeakReference<>(subscriber));
20 15
     }
21 16
 
22 17
     public void unregister(Subscriber subscriber) {
23
-        subscribers.remove(subscriber);
18
+        for (WeakReference<Subscriber> ref : subscribers) {
19
+            Subscriber registered = ref.get();
20
+            if (registered != null && registered == subscriber) {
21
+                subscribers.remove(ref);
22
+            }
23
+        }
24 24
     }
25 25
 
26 26
     public void post(Event event) {
27
-        for (Subscriber subscriber : subscribers) {
28
-            subscriber.onEvent(event);
27
+        for (WeakReference<Subscriber> ref : subscribers) {
28
+            Subscriber registered = ref.get();
29
+            if (registered != null) {
30
+                registered.onEvent(event);
31
+            }
32
+        }
33
+    }
34
+
35
+    public boolean isSubscribed(Subscriber subscriber) {
36
+        for (WeakReference<Subscriber> ref : subscribers) {
37
+            Subscriber registered = ref.get();
38
+            if (registered != null && registered.equals(subscriber)) {
39
+                return true;
40
+            }
29 41
         }
42
+        return false;
30 43
     }
31 44
 }

+ 15
- 0
android/app/src/test/java/com/reactnativenavigation/EnvironmentTest.java View File

@@ -0,0 +1,15 @@
1
+package com.reactnativenavigation;
2
+
3
+import org.junit.Test;
4
+import org.junit.runner.RunWith;
5
+import org.robolectric.RobolectricTestRunner;
6
+
7
+import static org.assertj.core.api.Java6Assertions.assertThat;
8
+
9
+@RunWith(RobolectricTestRunner.class)
10
+public class EnvironmentTest {
11
+    @Test
12
+    public void assertJ() {
13
+        assertThat(1 + 2).isEqualTo(3);
14
+    }
15
+}

+ 0
- 17
android/app/src/test/java/com/reactnativenavigation/FirstTest.java View File

@@ -1,17 +0,0 @@
1
-package com.reactnativenavigation;
2
-
3
-import org.junit.Test;
4
-import org.junit.runner.RunWith;
5
-import org.robolectric.RobolectricTestRunner;
6
-
7
-import static org.hamcrest.Matchers.is;
8
-import static org.junit.Assert.assertThat;
9
-
10
-//TODO test the shit out of this project
11
-@RunWith(RobolectricTestRunner.class)
12
-public class FirstTest {
13
-    @Test
14
-    public void woohoo() {
15
-        assertThat(1 + 2, is(3));
16
-    }
17
-}

+ 0
- 0
android/gradlew View File


+ 3
- 3
example-redux/android/app/build.gradle View File

@@ -115,7 +115,7 @@ android {
115 115
         variant.outputs.each { output ->
116 116
             // For each separate APK per architecture, set a unique version code as described here:
117 117
             // http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
118
-            def versionCodes = ["armeabi-v7a":1, "x86":2]
118
+            def versionCodes = ["armeabi-v7a": 1, "x86": 2]
119 119
             def abi = output.getFilter(OutputFile.ABI)
120 120
             if (abi != null) {  // null for the universal-debug, universal-release variants
121 121
                 output.versionCodeOverride =
@@ -136,6 +136,6 @@ dependencies {
136 136
 // Run this once to be able to run the application with BUCK
137 137
 // puts all compile dependencies into folder libs for BUCK to use
138 138
 task copyDownloadableDepsToLibs(type: Copy) {
139
-  from configurations.compile
140
-  into 'libs'
139
+    from configurations.compile
140
+    into 'libs'
141 141
 }