diff --git a/EventBus/build.gradle b/EventBus/build.gradle index 50637de7..60046b37 100644 --- a/EventBus/build.gradle +++ b/EventBus/build.gradle @@ -25,6 +25,8 @@ repositories { configurations { provided deployerJars + // include test classes and their dependencies + tests.extendsFrom testRuntime } dependencies { @@ -32,6 +34,10 @@ dependencies { provided 'com.google.android:android-test:4.1.1.4' provided 'com.google.android:annotations:4.1.1.4' provided 'com.google.android:support-v4:r7' + + testCompile 'junit:junit:4.12' + testCompile fileTree(dir: '../EventBusTest/libs', include: '*.jar') + // deployerJars 'org.apache.maven.wagon:wagon-webdav-jackrabbit:2.4' deployerJars 'org.apache.maven.wagon:wagon-webdav:1.0-beta-2' } @@ -44,6 +50,10 @@ sourceSets { // exclude 'de/greenrobot/event/util/**' } } + test { + compileClasspath += configurations.provided + java.srcDirs = ['test'] + } } idea { @@ -69,10 +79,20 @@ task sourcesJar(type: Jar) { classifier = 'sources' } +// build a jar with all test classes to allow usage as dependency in android test project +// https://discuss.gradle.org/t/how-do-i-declare-a-dependency-on-a-modules-test-code/7172/9 +task testJar(type: Jar) { + classifier "test" + from sourceSets.test.output + // exclude tests which can not run on Android + exclude '**/*Jvm*' +} + artifacts { archives jar archives javadocJar archives sourcesJar + tests testJar } signing { diff --git a/EventBus/src/org/greenrobot/eventbus/AsyncPoster.java b/EventBus/src/org/greenrobot/eventbus/AsyncPoster.java index 90a30d1e..9cb0caa5 100644 --- a/EventBus/src/org/greenrobot/eventbus/AsyncPoster.java +++ b/EventBus/src/org/greenrobot/eventbus/AsyncPoster.java @@ -16,6 +16,8 @@ package org.greenrobot.eventbus; +import java.util.concurrent.Executor; + /** * Posts events in background. * @@ -25,16 +27,22 @@ class AsyncPoster implements Runnable, Poster { private final PendingPostQueue queue; private final EventBus eventBus; + private final Executor executor; AsyncPoster(EventBus eventBus) { + this(eventBus, eventBus.getExecutorService()); + } + + AsyncPoster(EventBus eventBus, Executor executor) { this.eventBus = eventBus; + this.executor = executor; queue = new PendingPostQueue(); } public void enqueue(Subscription subscription, Object event) { PendingPost pendingPost = PendingPost.obtainPendingPost(subscription, event); queue.enqueue(pendingPost); - eventBus.getExecutorService().execute(this); + executor.execute(this); } @Override diff --git a/EventBus/src/org/greenrobot/eventbus/EventBus.java b/EventBus/src/org/greenrobot/eventbus/EventBus.java index 2518946b..ffab34ce 100644 --- a/EventBus/src/org/greenrobot/eventbus/EventBus.java +++ b/EventBus/src/org/greenrobot/eventbus/EventBus.java @@ -113,12 +113,9 @@ public EventBus() { subscriptionsByEventType = new HashMap<>(); typesBySubscriber = new HashMap<>(); stickyEvents = new ConcurrentHashMap<>(); - mainThreadSupport = builder.mainThreadSupport != null ? builder.mainThreadSupport : - Logger.AndroidLogger.isAndroidLogAvailable() ? - new MainThreadSupport.AndroidHandlerMainThreadSupport(Looper.getMainLooper()) : null; + mainThreadSupport = builder.mainThreadSupport; mainThreadPoster = mainThreadSupport != null ? mainThreadSupport.createPoster(this) : null; backgroundPoster = new BackgroundPoster(this); - asyncPoster = new AsyncPoster(this); indexCount = builder.subscriberInfoIndexes != null ? builder.subscriberInfoIndexes.size() : 0; subscriberMethodFinder = new SubscriberMethodFinder(builder.subscriberInfoIndexes, builder.strictMethodVerification, builder.ignoreGeneratedIndex); @@ -129,6 +126,7 @@ public EventBus() { throwSubscriberException = builder.throwSubscriberException; eventInheritance = builder.eventInheritance; executorService = builder.executorService; + asyncPoster = new AsyncPoster(this); } /** diff --git a/EventBus/src/org/greenrobot/eventbus/EventBusBuilder.java b/EventBus/src/org/greenrobot/eventbus/EventBusBuilder.java index 90e53eb0..aa88154a 100644 --- a/EventBus/src/org/greenrobot/eventbus/EventBusBuilder.java +++ b/EventBus/src/org/greenrobot/eventbus/EventBusBuilder.java @@ -15,6 +15,8 @@ */ package org.greenrobot.eventbus; +import android.os.Looper; + import org.greenrobot.eventbus.meta.SubscriberInfoIndex; import java.util.ArrayList; @@ -44,6 +46,9 @@ public class EventBusBuilder { MainThreadSupport mainThreadSupport; EventBusBuilder() { + if (Logger.AndroidLogger.isAndroidLogAvailable()) { + mainThreadSupport = new MainThreadSupport.AndroidHandlerMainThreadSupport(Looper.getMainLooper()); + } } /** Default: true */ @@ -70,6 +75,15 @@ public EventBusBuilder sendNoSubscriberEvent(boolean sendNoSubscriberEvent) { return this; } + /** + * Interface to the "main" thread, which can be whatever you like + * Default: android main thread if running on Android, or null otherwise + */ + public EventBusBuilder mainThreadSupport(MainThreadSupport mainThreadSupport) { + this.mainThreadSupport = mainThreadSupport; + return this; + } + /** * Fails if an subscriber throws an exception (default: false). *

diff --git a/EventBus/src/org/greenrobot/eventbus/Logger.java b/EventBus/src/org/greenrobot/eventbus/Logger.java index 6609b61b..7b509f79 100644 --- a/EventBus/src/org/greenrobot/eventbus/Logger.java +++ b/EventBus/src/org/greenrobot/eventbus/Logger.java @@ -15,6 +15,7 @@ */ package org.greenrobot.eventbus; +import android.os.Looper; import android.util.Log; import java.util.logging.Level; @@ -31,8 +32,9 @@ public static class AndroidLogger implements Logger { static { boolean android = false; try { - android = Class.forName("android.util.Log") != null; - } catch (ClassNotFoundException e) { + // getMainLooper will throw RuntimeException if running from Android Studio on JVM + android = Class.forName("android.util.Log") != null && Looper.getMainLooper() != null; + } catch (ClassNotFoundException | RuntimeException e) { // OK } ANDROID_LOG_AVAILABLE = android; diff --git a/EventBusTest/src/org/greenrobot/eventbus/AbstractEventBusTest.java b/EventBus/test/org/greenrobot/eventbus/AbstractEventBusTest.java similarity index 69% rename from EventBusTest/src/org/greenrobot/eventbus/AbstractEventBusTest.java rename to EventBus/test/org/greenrobot/eventbus/AbstractEventBusTest.java index 867cada1..c23f8499 100644 --- a/EventBusTest/src/org/greenrobot/eventbus/AbstractEventBusTest.java +++ b/EventBus/test/org/greenrobot/eventbus/AbstractEventBusTest.java @@ -15,28 +15,28 @@ */ package org.greenrobot.eventbus; -import android.annotation.SuppressLint; -import android.os.Handler; import android.os.Looper; -import android.os.Message; -import android.support.test.runner.AndroidJUnit4; -import org.greenrobot.eventbus.EventBus; import org.junit.Before; -import org.junit.runner.RunWith; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; +import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; +import java.util.logging.Level; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** * @author Markus Junginger, greenrobot */ -@RunWith(AndroidJUnit4.class) public abstract class AbstractEventBusTest { /** Activates long(er) running tests e.g. testing multi-threading more thoroughly. */ protected static final boolean LONG_TESTS = false; @@ -49,7 +49,7 @@ public abstract class AbstractEventBusTest { protected volatile Object lastEvent; protected volatile Thread lastThread; - private EventPostHandler mainPoster; + protected Thread mainThread; public AbstractEventBusTest() { this(false); @@ -66,13 +66,33 @@ public AbstractEventBusTest(boolean collectEventsReceived) { @Before public void setUpBase() throws Exception { EventBus.clearCaches(); - eventBus = new EventBus(); - mainPoster = new EventPostHandler(Looper.getMainLooper()); - assertFalse(Looper.getMainLooper().getThread().equals(Thread.currentThread())); - } - protected void postInMainThread(Object event) { - mainPoster.post(event); + final EventBusBuilder builder = new EventBusBuilder(); + if (builder.mainThreadSupport == null) { + final Executor mainExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() { + @Override + public Thread newThread(Runnable r) { + mainThread = new Thread(r); + return mainThread; + } + }); + // running on JVM + builder.mainThreadSupport = new MainThreadSupport() { + @Override + public boolean isMainThread() { + return Thread.currentThread() == mainThread; + } + + @Override + public Poster createPoster(EventBus eventBus) { + return new AsyncPoster(eventBus, mainExecutor); + } + }; + } else { + mainThread = Looper.getMainLooper().getThread(); + assertFalse(Looper.getMainLooper().getThread().equals(Thread.currentThread())); + } + eventBus = builder.build(); } protected void waitForEventCount(int expectedCount, int maxMillis) { @@ -104,23 +124,6 @@ protected void trackEvent(Object event) { eventCount.incrementAndGet(); } - @SuppressLint("HandlerLeak") - class EventPostHandler extends Handler { - public EventPostHandler(Looper looper) { - super(looper); - } - - @Override - public void handleMessage(Message msg) { - eventBus.post(msg.obj); - } - - void post(Object event) { - sendMessage(obtainMessage(0, event)); - } - - } - protected void assertEventCount(int expectedEventCount) { assertEquals(expectedEventCount, eventCount.intValue()); } @@ -138,4 +141,7 @@ protected void awaitLatch(CountDownLatch latch, long seconds) { } } + public void log(String message) { + eventBus.getLogger().log(Level.FINE, message); + } } diff --git a/EventBusTest/src/org/greenrobot/eventbus/EventBusBasicTest.java b/EventBus/test/org/greenrobot/eventbus/EventBusBasicTest.java similarity index 83% rename from EventBusTest/src/org/greenrobot/eventbus/EventBusBasicTest.java rename to EventBus/test/org/greenrobot/eventbus/EventBusBasicTest.java index d6af1eb4..d880caa3 100644 --- a/EventBusTest/src/org/greenrobot/eventbus/EventBusBasicTest.java +++ b/EventBus/test/org/greenrobot/eventbus/EventBusBasicTest.java @@ -15,24 +15,18 @@ */ package org.greenrobot.eventbus; -import android.app.Activity; -import android.support.test.annotation.UiThreadTest; -import android.support.test.rule.UiThreadTestRule; -import android.support.test.runner.AndroidJUnit4; -import android.util.Log; - -import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.runner.RunWith; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** * @author Markus Junginger, greenrobot */ -@RunWith(AndroidJUnit4.class) -public class EventBusBasicTest { +public class EventBusBasicTest extends AbstractEventBusTest { public static class WithIndex extends EventBusBasicTest { @Test @@ -40,10 +34,6 @@ public void dummy() {} } - @Rule - public final UiThreadTestRule uiThreadTestRule = new UiThreadTestRule(); - - protected EventBus eventBus; private String lastStringEvent; private int countStringEvent; private int countIntEvent; @@ -52,28 +42,6 @@ public void dummy() {} private int countMyEvent; private int countMyEvent2; - @Before - public void setUp() throws Exception { - eventBus = new EventBus(); - } - - @Test - @UiThreadTest - public void testRegisterAndPost() { - // Use an activity to test real life performance - TestActivity testActivity = new TestActivity(); - String event = "Hello"; - - long start = System.currentTimeMillis(); - eventBus.register(testActivity); - long time = System.currentTimeMillis() - start; - Log.d(EventBus.TAG, "Registered in " + time + "ms"); - - eventBus.post(event); - - assertEquals(event, testActivity.lastStringEvent); - } - @Test public void testPostWithoutSubscriber() { eventBus.post("Hello"); @@ -95,7 +63,7 @@ public void testUnregisterNotLeaking() { }; eventBus.register(subscriber); eventBus.unregister(subscriber); - Log.d("Test", "Iteration " + i + " / max heap: " + heapMBytes); + log("Iteration " + i + " / max heap: " + heapMBytes); } } @@ -142,7 +110,7 @@ public void testPostMultipleTimes() { } // Debug.stopMethodTracing(); long time = System.currentTimeMillis() - start; - Log.d(EventBus.TAG, "Posted " + count + " events in " + time + "ms"); + log("Posted " + count + " events in " + time + "ms"); assertEquals(count, countMyEvent); } @@ -268,15 +236,6 @@ public void onEvent(MyEventExtended event) { countMyEventExtended++; } - public static class TestActivity extends Activity { - public String lastStringEvent; - - @Subscribe - public void onEvent(String event) { - lastStringEvent = event; - } - } - public static class CharSequenceSubscriber { @Subscribe public void onEvent(CharSequence event) { diff --git a/EventBusTest/src/org/greenrobot/eventbus/EventBusBuilderTest.java b/EventBus/test/org/greenrobot/eventbus/EventBusBuilderTest.java similarity index 100% rename from EventBusTest/src/org/greenrobot/eventbus/EventBusBuilderTest.java rename to EventBus/test/org/greenrobot/eventbus/EventBusBuilderTest.java diff --git a/EventBusTest/src/org/greenrobot/eventbus/EventBusCancelEventDeliveryTest.java b/EventBus/test/org/greenrobot/eventbus/EventBusCancelEventDeliveryTest.java similarity index 96% rename from EventBusTest/src/org/greenrobot/eventbus/EventBusCancelEventDeliveryTest.java rename to EventBus/test/org/greenrobot/eventbus/EventBusCancelEventDeliveryTest.java index 869da70e..0c02832b 100644 --- a/EventBusTest/src/org/greenrobot/eventbus/EventBusCancelEventDeliveryTest.java +++ b/EventBus/test/org/greenrobot/eventbus/EventBusCancelEventDeliveryTest.java @@ -21,14 +21,16 @@ import java.util.concurrent.CountDownLatch; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; /** * @author Markus Junginger, greenrobot */ public class EventBusCancelEventDeliveryTest extends AbstractEventBusTest { - private Throwable failed; + protected Throwable failed; @Test public void testCancel() { diff --git a/EventBusTest/src/org/greenrobot/eventbus/EventBusFallbackToReflectionTest.java b/EventBus/test/org/greenrobot/eventbus/EventBusFallbackToReflectionTest.java similarity index 100% rename from EventBusTest/src/org/greenrobot/eventbus/EventBusFallbackToReflectionTest.java rename to EventBus/test/org/greenrobot/eventbus/EventBusFallbackToReflectionTest.java diff --git a/EventBusTest/src/org/greenrobot/eventbus/EventBusGenericsTest.java b/EventBus/test/org/greenrobot/eventbus/EventBusGenericsTest.java similarity index 100% rename from EventBusTest/src/org/greenrobot/eventbus/EventBusGenericsTest.java rename to EventBus/test/org/greenrobot/eventbus/EventBusGenericsTest.java diff --git a/EventBusTest/src/org/greenrobot/eventbus/EventBusIndexTest.java b/EventBus/test/org/greenrobot/eventbus/EventBusIndexTest.java similarity index 100% rename from EventBusTest/src/org/greenrobot/eventbus/EventBusIndexTest.java rename to EventBus/test/org/greenrobot/eventbus/EventBusIndexTest.java diff --git a/EventBusTest/src/org/greenrobot/eventbus/EventBusInheritanceDisabledTest.java b/EventBus/test/org/greenrobot/eventbus/EventBusInheritanceDisabledTest.java similarity index 70% rename from EventBusTest/src/org/greenrobot/eventbus/EventBusInheritanceDisabledTest.java rename to EventBus/test/org/greenrobot/eventbus/EventBusInheritanceDisabledTest.java index 80638e8b..4ceb1420 100644 --- a/EventBusTest/src/org/greenrobot/eventbus/EventBusInheritanceDisabledTest.java +++ b/EventBus/test/org/greenrobot/eventbus/EventBusInheritanceDisabledTest.java @@ -15,27 +15,19 @@ */ package org.greenrobot.eventbus; -import android.support.test.runner.AndroidJUnit4; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; import static junit.framework.Assert.assertEquals; /** * @author Markus Junginger, greenrobot */ -@RunWith(AndroidJUnit4.class) public class EventBusInheritanceDisabledTest { protected EventBus eventBus; - - protected int countMyEventExtended; - protected int countMyEvent; - protected int countObjectEvent; - private int countMyEventInterface; - private int countMyEventInterfaceExtended; + protected final Subscriber subscriber = new Subscriber(); @Before public void setUp() throws Exception { @@ -44,19 +36,19 @@ public void setUp() throws Exception { @Test public void testEventClassHierarchy() { - eventBus.register(this); + eventBus.register(subscriber); eventBus.post("Hello"); - assertEquals(0, countObjectEvent); + assertEquals(0, subscriber.countObjectEvent); eventBus.post(new MyEvent()); - assertEquals(0, countObjectEvent); - assertEquals(1, countMyEvent); + assertEquals(0, subscriber.countObjectEvent); + assertEquals(1, subscriber.countMyEvent); eventBus.post(new MyEventExtended()); - assertEquals(0, countObjectEvent); - assertEquals(1, countMyEvent); - assertEquals(1, countMyEventExtended); + assertEquals(0, subscriber.countObjectEvent); + assertEquals(1, subscriber.countMyEvent); + assertEquals(1, subscriber.countMyEventExtended); } @Test @@ -65,31 +57,31 @@ public void testEventClassHierarchySticky() { eventBus.postSticky(new MyEvent()); eventBus.postSticky(new MyEventExtended()); eventBus.register(new StickySubscriber()); - assertEquals(1, countMyEventExtended); - assertEquals(1, countMyEvent); - assertEquals(0, countObjectEvent); + assertEquals(1, subscriber.countMyEventExtended); + assertEquals(1, subscriber.countMyEvent); + assertEquals(0, subscriber.countObjectEvent); } @Test public void testEventInterfaceHierarchy() { - eventBus.register(this); + eventBus.register(subscriber); eventBus.post(new MyEvent()); - assertEquals(0, countMyEventInterface); + assertEquals(0, subscriber.countMyEventInterface); eventBus.post(new MyEventExtended()); - assertEquals(0, countMyEventInterface); - assertEquals(0, countMyEventInterfaceExtended); + assertEquals(0, subscriber.countMyEventInterface); + assertEquals(0, subscriber.countMyEventInterfaceExtended); } @Test public void testEventSuperInterfaceHierarchy() { - eventBus.register(this); + eventBus.register(subscriber); eventBus.post(new MyEventInterfaceExtended() { }); - assertEquals(0, countMyEventInterface); - assertEquals(0, countMyEventInterfaceExtended); + assertEquals(0, subscriber.countMyEventInterface); + assertEquals(0, subscriber.countMyEventInterfaceExtended); } @Test @@ -130,44 +122,52 @@ public void testSubscriberClassHierarchyWithoutNewSubscriberMethod() { assertEquals(1, subscriber.countMyEventExtended); } - @Subscribe - public void onEvent(Object event) { - countObjectEvent++; + public static interface MyEventInterface { } - @Subscribe - public void onEvent(MyEvent event) { - countMyEvent++; + public static class MyEvent implements MyEventInterface { } - @Subscribe - public void onEvent(MyEventExtended event) { - countMyEventExtended++; + public static interface MyEventInterfaceExtended extends MyEventInterface { } - @Subscribe - public void onEvent(MyEventInterface event) { - countMyEventInterface++; + public static class MyEventExtended extends MyEvent implements MyEventInterfaceExtended { } - @Subscribe - public void onEvent(MyEventInterfaceExtended event) { - countMyEventInterfaceExtended++; - } + public static class Subscriber { + int countMyEventExtended; + int countMyEvent; + int countObjectEvent; + int countMyEventInterface; + int countMyEventInterfaceExtended; - public static interface MyEventInterface { - } + @Subscribe + public void onEvent(Object event) { + countObjectEvent++; + } - public static class MyEvent implements MyEventInterface { - } + @Subscribe + public void onEvent(MyEvent event) { + countMyEvent++; + } - public static interface MyEventInterfaceExtended extends MyEventInterface { - } + @Subscribe + public void onEvent(MyEventExtended event) { + countMyEventExtended++; + } - public static class MyEventExtended extends MyEvent implements MyEventInterfaceExtended { + @Subscribe + public void onEvent(MyEventInterface event) { + countMyEventInterface++; + } + + @Subscribe + public void onEvent(MyEventInterfaceExtended event) { + countMyEventInterfaceExtended++; + } } - public static class SubscriberExtended extends EventBusInheritanceDisabledTest { + public static class SubscriberExtended extends Subscriber { private int countMyEventOverwritten; @Subscribe @@ -176,34 +176,35 @@ public void onEvent(MyEvent event) { } } - static class SubscriberExtendedWithoutNewSubscriberMethod extends EventBusInheritanceDisabledTest { + static class SubscriberExtendedWithoutNewSubscriberMethod extends Subscriber { } public class StickySubscriber { @Subscribe(sticky = true) public void onEvent(Object event) { - countObjectEvent++; + subscriber.countObjectEvent++; } @Subscribe(sticky = true) public void onEvent(MyEvent event) { - countMyEvent++; + subscriber.countMyEvent++; } @Subscribe(sticky = true) public void onEvent(MyEventExtended event) { - countMyEventExtended++; + subscriber.countMyEventExtended++; } @Subscribe(sticky = true) public void onEvent(MyEventInterface event) { - countMyEventInterface++; + subscriber.countMyEventInterface++; } @Subscribe(sticky = true) public void onEvent(MyEventInterfaceExtended event) { - countMyEventInterfaceExtended++; + subscriber.countMyEventInterfaceExtended++; } } + } diff --git a/EventBusTest/src/org/greenrobot/eventbus/EventBusInheritanceTest.java b/EventBus/test/org/greenrobot/eventbus/EventBusInheritanceTest.java similarity index 65% rename from EventBusTest/src/org/greenrobot/eventbus/EventBusInheritanceTest.java rename to EventBus/test/org/greenrobot/eventbus/EventBusInheritanceTest.java index f9619bed..9e08078a 100644 --- a/EventBusTest/src/org/greenrobot/eventbus/EventBusInheritanceTest.java +++ b/EventBus/test/org/greenrobot/eventbus/EventBusInheritanceTest.java @@ -15,43 +15,39 @@ */ package org.greenrobot.eventbus; -import junit.framework.TestCase; - +import org.junit.Before; import org.junit.Test; +import static org.junit.Assert.assertEquals; + /** * @author Markus Junginger, greenrobot */ -public class EventBusInheritanceTest extends TestCase { +public class EventBusInheritanceTest { protected EventBus eventBus; + protected final Subscriber subscriber = new Subscriber(); - protected int countMyEventExtended; - protected int countMyEvent; - protected int countObjectEvent; - private int countMyEventInterface; - private int countMyEventInterfaceExtended; - - protected void setUp() throws Exception { - super.setUp(); + @Before + public void setUp() throws Exception { eventBus = new EventBus(); } @Test public void testEventClassHierarchy() { - eventBus.register(this); + eventBus.register(subscriber); eventBus.post("Hello"); - assertEquals(1, countObjectEvent); + assertEquals(1, subscriber.countObjectEvent); eventBus.post(new MyEvent()); - assertEquals(2, countObjectEvent); - assertEquals(1, countMyEvent); + assertEquals(2, subscriber.countObjectEvent); + assertEquals(1, subscriber.countMyEvent); eventBus.post(new MyEventExtended()); - assertEquals(3, countObjectEvent); - assertEquals(2, countMyEvent); - assertEquals(1, countMyEventExtended); + assertEquals(3, subscriber.countObjectEvent); + assertEquals(2, subscriber.countMyEvent); + assertEquals(1, subscriber.countMyEventExtended); } @Test @@ -60,31 +56,31 @@ public void testEventClassHierarchySticky() { eventBus.postSticky(new MyEvent()); eventBus.postSticky(new MyEventExtended()); eventBus.register(new StickySubscriber()); - assertEquals(1, countMyEventExtended); - assertEquals(2, countMyEvent); - assertEquals(3, countObjectEvent); + assertEquals(1, subscriber.countMyEventExtended); + assertEquals(2, subscriber.countMyEvent); + assertEquals(3, subscriber.countObjectEvent); } @Test public void testEventInterfaceHierarchy() { - eventBus.register(this); + eventBus.register(subscriber); eventBus.post(new MyEvent()); - assertEquals(1, countMyEventInterface); + assertEquals(1, subscriber.countMyEventInterface); eventBus.post(new MyEventExtended()); - assertEquals(2, countMyEventInterface); - assertEquals(1, countMyEventInterfaceExtended); + assertEquals(2, subscriber.countMyEventInterface); + assertEquals(1, subscriber.countMyEventInterfaceExtended); } @Test public void testEventSuperInterfaceHierarchy() { - eventBus.register(this); + eventBus.register(subscriber); eventBus.post(new MyEventInterfaceExtended() { }); - assertEquals(1, countMyEventInterface); - assertEquals(1, countMyEventInterfaceExtended); + assertEquals(1, subscriber.countMyEventInterface); + assertEquals(1, subscriber.countMyEventInterfaceExtended); } @Test @@ -125,44 +121,52 @@ public void testSubscriberClassHierarchyWithoutNewSubscriberMethod() { assertEquals(1, subscriber.countMyEventExtended); } - @Subscribe - public void onEvent(Object event) { - countObjectEvent++; - } - - @Subscribe - public void onEvent(MyEvent event) { - countMyEvent++; + public interface MyEventInterface { } - @Subscribe - public void onEvent(MyEventExtended event) { - countMyEventExtended++; + public static class MyEvent implements MyEventInterface { } - @Subscribe - public void onEvent(MyEventInterface event) { - countMyEventInterface++; + public interface MyEventInterfaceExtended extends MyEventInterface { } - @Subscribe - public void onEvent(MyEventInterfaceExtended event) { - countMyEventInterfaceExtended++; + public static class MyEventExtended extends MyEvent implements MyEventInterfaceExtended { } + + public static class Subscriber { + int countMyEventExtended; + int countMyEvent; + int countObjectEvent; + int countMyEventInterface; + int countMyEventInterfaceExtended; + + @Subscribe + public void onEvent(Object event) { + countObjectEvent++; + } - public static interface MyEventInterface { - } + @Subscribe + public void onEvent(MyEvent event) { + countMyEvent++; + } - public static class MyEvent implements MyEventInterface { - } + @Subscribe + public void onEvent(MyEventExtended event) { + countMyEventExtended++; + } - public static interface MyEventInterfaceExtended extends MyEventInterface { - } + @Subscribe + public void onEvent(MyEventInterface event) { + countMyEventInterface++; + } - public static class MyEventExtended extends MyEvent implements MyEventInterfaceExtended { + @Subscribe + public void onEvent(MyEventInterfaceExtended event) { + countMyEventInterfaceExtended++; + } } - public static class SubscriberExtended extends EventBusInheritanceTest { + public static class SubscriberExtended extends Subscriber { private int countMyEventOverwritten; @Subscribe @@ -171,33 +175,33 @@ public void onEvent(MyEvent event) { } } - static class SubscriberExtendedWithoutNewSubscriberMethod extends EventBusInheritanceTest { + static class SubscriberExtendedWithoutNewSubscriberMethod extends Subscriber { } public class StickySubscriber { @Subscribe(sticky = true) public void onEvent(Object event) { - countObjectEvent++; + subscriber.countObjectEvent++; } @Subscribe(sticky = true) public void onEvent(MyEvent event) { - countMyEvent++; + subscriber.countMyEvent++; } @Subscribe(sticky = true) public void onEvent(MyEventExtended event) { - countMyEventExtended++; + subscriber.countMyEventExtended++; } @Subscribe(sticky = true) public void onEvent(MyEventInterface event) { - countMyEventInterface++; + subscriber.countMyEventInterface++; } @Subscribe(sticky = true) public void onEvent(MyEventInterfaceExtended event) { - countMyEventInterfaceExtended++; + subscriber.countMyEventInterfaceExtended++; } } diff --git a/EventBusTest/src/org/greenrobot/eventbus/EventBusMainThreadTest.java b/EventBus/test/org/greenrobot/eventbus/EventBusMainThreadTest.java similarity index 95% rename from EventBusTest/src/org/greenrobot/eventbus/EventBusMainThreadTest.java rename to EventBus/test/org/greenrobot/eventbus/EventBusMainThreadTest.java index 2195d10f..03a1b542 100644 --- a/EventBusTest/src/org/greenrobot/eventbus/EventBusMainThreadTest.java +++ b/EventBus/test/org/greenrobot/eventbus/EventBusMainThreadTest.java @@ -15,8 +15,6 @@ */ package org.greenrobot.eventbus; -import android.os.Looper; - import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -52,7 +50,7 @@ public void testPost() throws InterruptedException { waitForEventCount(1, 1000); assertEquals("Hello", lastEvent); - assertEquals(Looper.getMainLooper().getThread(), lastThread); + assertEquals(mainThread, lastThread); } @Test @@ -61,7 +59,7 @@ public void testPostInBackgroundThread() throws InterruptedException { backgroundPoster.post("Hello"); waitForEventCount(1, 1000); assertEquals("Hello", lastEvent); - assertEquals(Looper.getMainLooper().getThread(), lastThread); + assertEquals(mainThread, lastThread); } @Subscribe(threadMode = ThreadMode.MAIN) diff --git a/EventBusTest/src/org/greenrobot/eventbus/EventBusMethodModifiersTest.java b/EventBus/test/org/greenrobot/eventbus/EventBusMethodModifiersTest.java similarity index 84% rename from EventBusTest/src/org/greenrobot/eventbus/EventBusMethodModifiersTest.java rename to EventBus/test/org/greenrobot/eventbus/EventBusMethodModifiersTest.java index e974e73a..d7f4205e 100644 --- a/EventBusTest/src/org/greenrobot/eventbus/EventBusMethodModifiersTest.java +++ b/EventBus/test/org/greenrobot/eventbus/EventBusMethodModifiersTest.java @@ -15,8 +15,6 @@ */ package org.greenrobot.eventbus; -import android.os.Looper; - import org.junit.Test; import static org.junit.Assert.assertNotSame; @@ -37,26 +35,26 @@ public void testRegisterForEventTypeAndPost() throws InterruptedException { @Subscribe public void onEvent(String event) { + assertNotSame(mainThread, Thread.currentThread()); trackEvent(event); - assertNotSame(Looper.getMainLooper(), Looper.myLooper()); } @Subscribe(threadMode = ThreadMode.MAIN) public void onEventMainThread(String event) { + assertSame(mainThread, Thread.currentThread()); trackEvent(event); - assertSame(Looper.getMainLooper(), Looper.myLooper()); } @Subscribe(threadMode = ThreadMode.BACKGROUND) public void onEventBackgroundThread(String event) { + assertNotSame(mainThread, Thread.currentThread()); trackEvent(event); - assertNotSame(Looper.getMainLooper(), Looper.myLooper()); } @Subscribe(threadMode = ThreadMode.ASYNC) public void onEventAsync(String event) { + assertNotSame(mainThread, Thread.currentThread()); trackEvent(event); - assertNotSame(Looper.getMainLooper(), Looper.myLooper()); } } diff --git a/EventBusTest/src/org/greenrobot/eventbus/EventBusMultithreadedTest.java b/EventBus/test/org/greenrobot/eventbus/EventBusMultithreadedTest.java similarity index 94% rename from EventBusTest/src/org/greenrobot/eventbus/EventBusMultithreadedTest.java rename to EventBus/test/org/greenrobot/eventbus/EventBusMultithreadedTest.java index f3e7bd50..0e4ee817 100644 --- a/EventBusTest/src/org/greenrobot/eventbus/EventBusMultithreadedTest.java +++ b/EventBus/test/org/greenrobot/eventbus/EventBusMultithreadedTest.java @@ -15,7 +15,6 @@ */ package org.greenrobot.eventbus; -import android.os.Looper; import android.util.Log; import org.junit.Test; @@ -103,7 +102,7 @@ private void runThreadsSingleEventType(int threadCount) throws InterruptedExcept List threads = startThreads(latch, threadCount, iterations, "Hello"); long time = triggerAndWaitForThreads(threads, latch); - Log.d(EventBus.TAG, threadCount + " threads posted " + iterations + " events each in " + time + "ms"); + log(threadCount + " threads posted " + iterations + " events each in " + time + "ms"); waitForEventCount(COUNT * 2, 5000); @@ -133,8 +132,7 @@ private void runThreadsMixedEventType(int count, int threadCount) throws Interru threads.addAll(threadsIntTestEvent); long time = triggerAndWaitForThreads(threads, latch); - Log.d(EventBus.TAG, threadCount * eventTypeCount + " mixed threads posted " + iterations + " events each in " - + time + "ms"); + log(threadCount * eventTypeCount + " mixed threads posted " + iterations + " events each in " + time + "ms"); int expectedCountEach = threadCount * iterations; int expectedCountTotal = expectedCountEach * eventTypeCount * 2; @@ -255,22 +253,22 @@ public void run() { @Subscribe(threadMode = ThreadMode.MAIN) public void onEventMainThread(String event) { - assertSame(Looper.getMainLooper(), Looper.myLooper()); + assertSame(mainThread, Thread.currentThread()); } @Subscribe(threadMode = ThreadMode.BACKGROUND) public void onEventBackgroundThread(Integer event) { - assertNotSame(Looper.getMainLooper(), Looper.myLooper()); + assertNotSame(mainThread, Thread.currentThread()); } @Subscribe public void onEvent(Object event) { - assertNotSame(Looper.getMainLooper(), Looper.myLooper()); + assertNotSame(mainThread, Thread.currentThread()); } @Subscribe(threadMode = ThreadMode.ASYNC) public void onEventAsync(Object event) { - assertNotSame(Looper.getMainLooper(), Looper.myLooper()); + assertNotSame(mainThread, Thread.currentThread()); } } diff --git a/EventBusTest/src/org/greenrobot/eventbus/EventBusNoSubscriberEventTest.java b/EventBus/test/org/greenrobot/eventbus/EventBusNoSubscriberEventTest.java similarity index 100% rename from EventBusTest/src/org/greenrobot/eventbus/EventBusNoSubscriberEventTest.java rename to EventBus/test/org/greenrobot/eventbus/EventBusNoSubscriberEventTest.java diff --git a/EventBusTest/src/org/greenrobot/eventbus/EventBusOrderedSubscriptionsTest.java b/EventBus/test/org/greenrobot/eventbus/EventBusOrderedSubscriptionsTest.java similarity index 97% rename from EventBusTest/src/org/greenrobot/eventbus/EventBusOrderedSubscriptionsTest.java rename to EventBus/test/org/greenrobot/eventbus/EventBusOrderedSubscriptionsTest.java index f6f97201..86a527f5 100644 --- a/EventBusTest/src/org/greenrobot/eventbus/EventBusOrderedSubscriptionsTest.java +++ b/EventBus/test/org/greenrobot/eventbus/EventBusOrderedSubscriptionsTest.java @@ -15,12 +15,11 @@ */ package org.greenrobot.eventbus; -import android.util.Log; - import org.junit.Test; import java.util.ArrayList; import java.util.List; +import java.util.logging.Level; import static org.junit.Assert.assertEquals; @@ -137,7 +136,7 @@ protected void handleEvent(int prio, Object event) { } lastPrio = prio; - Log.d(EventBus.TAG, "Subscriber " + prio + " got: " + event); + log("Subscriber " + prio + " got: " + event); trackEvent(event); } @@ -206,7 +205,7 @@ protected void handleEvent(int prio, Object event) { } lastPrio = prio; - Log.d(EventBus.TAG, "Subscriber " + prio + " got: " + event); + log("Subscriber " + prio + " got: " + event); trackEvent(event); } diff --git a/EventBusTest/src/org/greenrobot/eventbus/EventBusRegistrationRacingTest.java b/EventBus/test/org/greenrobot/eventbus/EventBusRegistrationRacingTest.java similarity index 100% rename from EventBusTest/src/org/greenrobot/eventbus/EventBusRegistrationRacingTest.java rename to EventBus/test/org/greenrobot/eventbus/EventBusRegistrationRacingTest.java diff --git a/EventBusTest/src/org/greenrobot/eventbus/EventBusStickyEventTest.java b/EventBus/test/org/greenrobot/eventbus/EventBusStickyEventTest.java similarity index 100% rename from EventBusTest/src/org/greenrobot/eventbus/EventBusStickyEventTest.java rename to EventBus/test/org/greenrobot/eventbus/EventBusStickyEventTest.java diff --git a/EventBusTest/src/org/greenrobot/eventbus/EventBusSubscriberExceptionTest.java b/EventBus/test/org/greenrobot/eventbus/EventBusSubscriberExceptionTest.java similarity index 100% rename from EventBusTest/src/org/greenrobot/eventbus/EventBusSubscriberExceptionTest.java rename to EventBus/test/org/greenrobot/eventbus/EventBusSubscriberExceptionTest.java diff --git a/EventBusTest/src/org/greenrobot/eventbus/EventBusSubscriberInJarTest.java b/EventBus/test/org/greenrobot/eventbus/EventBusSubscriberInJarTest.java similarity index 95% rename from EventBusTest/src/org/greenrobot/eventbus/EventBusSubscriberInJarTest.java rename to EventBus/test/org/greenrobot/eventbus/EventBusSubscriberInJarTest.java index 5b013eb9..eb36a28c 100644 --- a/EventBusTest/src/org/greenrobot/eventbus/EventBusSubscriberInJarTest.java +++ b/EventBus/test/org/greenrobot/eventbus/EventBusSubscriberInJarTest.java @@ -24,7 +24,7 @@ import org.greenrobot.eventbus.SubscriberInJar; -public class EventBusSubscriberInJarTest extends TestCase { +public class EventBusSubscriberInJarTest { protected EventBus eventBus = EventBus.builder().build(); @Test diff --git a/EventBusTest/src/org/greenrobot/eventbus/EventBusSubscriberLegalTest.java b/EventBus/test/org/greenrobot/eventbus/EventBusSubscriberLegalTest.java similarity index 100% rename from EventBusTest/src/org/greenrobot/eventbus/EventBusSubscriberLegalTest.java rename to EventBus/test/org/greenrobot/eventbus/EventBusSubscriberLegalTest.java diff --git a/EventBusTest/src/org/greenrobot/eventbus/IntTestEvent.java b/EventBus/test/org/greenrobot/eventbus/IntTestEvent.java similarity index 100% rename from EventBusTest/src/org/greenrobot/eventbus/IntTestEvent.java rename to EventBus/test/org/greenrobot/eventbus/IntTestEvent.java diff --git a/EventBus/test/org/greenrobot/eventbus/README.md b/EventBus/test/org/greenrobot/eventbus/README.md new file mode 100644 index 00000000..1467ad0b --- /dev/null +++ b/EventBus/test/org/greenrobot/eventbus/README.md @@ -0,0 +1,2 @@ +All the tests here are running also on Android within EventBusTest project. +If there is a need to exclude test from running on Android, you can put **"Jvm"** somewhere in the class name of the test. \ No newline at end of file diff --git a/EventBusTest/build.gradle b/EventBusTest/build.gradle index 990fb0c4..81c06fb5 100644 --- a/EventBusTest/build.gradle +++ b/EventBusTest/build.gradle @@ -19,9 +19,11 @@ repositories { } dependencies { + compile fileTree(dir: 'libs', include: '*.jar') + androidTestApt project(':EventBusAnnotationProcessor') androidTestCompile project(':EventBus') - compile fileTree(dir: 'libs', include: '*.jar') + androidTestCompile project(path: ':EventBus', configuration: 'tests') androidTestCompile 'com.android.support.test:runner:0.4.1' androidTestCompile 'com.android.support.test:rules:0.4.1' } @@ -56,4 +58,3 @@ apt { eventBusIndex "org.greenrobot.eventbus.EventBusTestsIndex" } } - diff --git a/EventBusTest/src/org/greenrobot/eventbus/EventBusActivityPerformanceTest.java b/EventBusTest/src/org/greenrobot/eventbus/EventBusActivityPerformanceTest.java new file mode 100644 index 00000000..bf3e53a3 --- /dev/null +++ b/EventBusTest/src/org/greenrobot/eventbus/EventBusActivityPerformanceTest.java @@ -0,0 +1,53 @@ +package org.greenrobot.eventbus; + +import android.app.Activity; +import android.support.test.annotation.UiThreadTest; +import android.support.test.rule.UiThreadTestRule; +import android.support.test.runner.AndroidJUnit4; +import android.util.Log; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.assertEquals; + +@RunWith(AndroidJUnit4.class) +public class EventBusActivityPerformanceTest { + protected EventBus eventBus; + + @Rule + public final UiThreadTestRule uiThreadTestRule = new UiThreadTestRule(); + + @Before + public void setUp() throws Exception { + eventBus = new EventBus(); + } + + @Test + @UiThreadTest + public void testRegisterAndPost() { + // Use an activity to test real life performance + TestActivity testActivity = new TestActivity(); + String event = "Hello"; + + long start = System.currentTimeMillis(); + eventBus.register(testActivity); + long time = System.currentTimeMillis() - start; + Log.d(EventBus.TAG, "Registered in " + time + "ms"); + + eventBus.post(event); + + assertEquals(event, testActivity.lastStringEvent); + } + + public static class TestActivity extends Activity { + public String lastStringEvent; + + @Subscribe + public void onEvent(String event) { + lastStringEvent = event; + } + } +} diff --git a/EventBusTest/src/org/greenrobot/eventbus/EventBusBackgroundThreadTest.java b/EventBusTest/src/org/greenrobot/eventbus/EventBusBackgroundThreadTest.java index 5a57f744..331c8a9a 100644 --- a/EventBusTest/src/org/greenrobot/eventbus/EventBusBackgroundThreadTest.java +++ b/EventBusTest/src/org/greenrobot/eventbus/EventBusBackgroundThreadTest.java @@ -15,8 +15,12 @@ */ package org.greenrobot.eventbus; +import android.annotation.SuppressLint; +import android.os.Handler; import android.os.Looper; +import android.os.Message; +import org.junit.Before; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -25,7 +29,18 @@ /** * @author Markus Junginger, greenrobot */ +//TODO port to JVM public class EventBusBackgroundThreadTest extends AbstractEventBusTest { + private EventPostHandler mainPoster; + + @Before + public void setUp() throws Exception { + mainPoster = new EventPostHandler(Looper.getMainLooper()); + } + + private void postInMainThread(Object event) { + mainPoster.post(event); + } @Test public void testPostInCurrentThread() throws InterruptedException { @@ -52,4 +67,20 @@ public void onEventBackgroundThread(String event) { trackEvent(event); } + @SuppressLint("HandlerLeak") + class EventPostHandler extends Handler { + public EventPostHandler(Looper looper) { + super(looper); + } + + @Override + public void handleMessage(Message msg) { + eventBus.post(msg.obj); + } + + void post(Object event) { + sendMessage(obtainMessage(0, event)); + } + + } } diff --git a/EventBusTest/src/org/greenrobot/eventbus/EventBusMainThreadRacingTest.java b/EventBusTest/src/org/greenrobot/eventbus/EventBusMainThreadRacingTest.java index a598220e..2dac9d86 100644 --- a/EventBusTest/src/org/greenrobot/eventbus/EventBusMainThreadRacingTest.java +++ b/EventBusTest/src/org/greenrobot/eventbus/EventBusMainThreadRacingTest.java @@ -26,6 +26,7 @@ /** * @author Markus Junginger, greenrobot */ +//TODO port to JVM public class EventBusMainThreadRacingTest extends AbstractEventBusTest { private static final int ITERATIONS = LONG_TESTS ? 100000 : 1000; diff --git a/EventBusTest/src/org/greenrobot/eventbus/indexed/EventBusInheritanceDisabledTestWithIndex.java b/EventBusTest/src/org/greenrobot/eventbus/indexed/EventBusInheritanceDisabledTestWithIndex.java index 2443e9e1..b9381462 100644 --- a/EventBusTest/src/org/greenrobot/eventbus/indexed/EventBusInheritanceDisabledTestWithIndex.java +++ b/EventBusTest/src/org/greenrobot/eventbus/indexed/EventBusInheritanceDisabledTestWithIndex.java @@ -17,11 +17,10 @@ package org.greenrobot.eventbus.indexed; import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.EventBusInheritanceDisabledTest; import org.greenrobot.eventbus.EventBusTestsIndex; import org.junit.Before; -import org.greenrobot.eventbus.EventBusInheritanceDisabledTest; - public class EventBusInheritanceDisabledTestWithIndex extends EventBusInheritanceDisabledTest { @Before public void setUp() throws Exception {