@Override public void onDestroyView() { super.onDestroyView(); EventBus.getDefault().unregister(this); }
@Test public void testPostAfterUnregister() { eventBus.register(this); eventBus.unregister(this); eventBus.post("Hello"); assertNull(lastStringEvent); }
@Test public void testIsRegistered() { assertFalse(eventBus.isRegistered(this)); eventBus.register(this); assertTrue(eventBus.isRegistered(this)); eventBus.unregister(this); assertFalse(eventBus.isRegistered(this)); }
@Override public void onReceive(Context context, Intent intent) { EventBus.getDefault().post(intent); } }
private void registerUnregisterOneSubscribers() { if (!subscribers.isEmpty()) { Object subscriber = subscribers.get(0); eventBus.register(subscriber); eventBus.unregister(subscriber); } }
@Override protected void onStart() { super.onStart(); EventBus.getDefault().register(this); }
@Test public void testHasSubscriberForEvent() { assertFalse(eventBus.hasSubscriberForEvent(String.class)); eventBus.register(this); assertTrue(eventBus.hasSubscriberForEvent(String.class)); eventBus.unregister(this); assertFalse(eventBus.hasSubscriberForEvent(String.class)); }
@Test public void testPostStickyTwice() throws InterruptedException { eventBus.postSticky("Sticky"); eventBus.postSticky("NewSticky"); eventBus.register(this); assertEquals("NewSticky", lastEvent); }
@Test public void testThrowSubscriberException() { eventBus = EventBus.builder().throwSubscriberException(true).build(); eventBus.register(new SubscriberExceptionEventTracker()); eventBus.register(new ThrowingSubscriber()); try { eventBus.post("Foo"); fail("Should have thrown"); } catch (EventBusException e) { // Expected } }
@Test public void testPostStickyThenPostNormal() throws InterruptedException { eventBus.postSticky("Sticky"); eventBus.post("NonSticky"); eventBus.register(this); assertEquals("Sticky", lastEvent); }
@Test public void testPostStickyWithRegisterAndUnregister() throws InterruptedException { eventBus.register(this); eventBus.postSticky("Sticky"); assertEquals("Sticky", lastEvent); eventBus.unregister(this); eventBus.register(this); assertEquals("Sticky", lastEvent); assertEquals(2, eventCount.intValue()); eventBus.postSticky("NewSticky"); assertEquals(3, eventCount.intValue()); assertEquals("NewSticky", lastEvent); eventBus.unregister(this); eventBus.register(this); assertEquals(4, eventCount.intValue()); assertEquals("NewSticky", lastEvent); }
@Test public void testPostStickyRemoveClass() throws InterruptedException { eventBus.postSticky("Sticky"); eventBus.removeStickyEvent(String.class); assertNull(eventBus.getStickyEvent(String.class)); eventBus.register(this); assertNull(lastEvent); assertEquals(0, eventCount.intValue()); }
/** * 发送黏性事件, 如果您在项目中同时依赖了两个 EventBus, 请自己使用想使用的 EventBus 的 Api 发送黏性事件 * * @param event 事件 */ public void postSticky(Object event) { if (DEPENDENCY_ANDROID_EVENTBUS) { org.simple.eventbus.EventBus.getDefault().postSticky(event); } else if (DEPENDENCY_EVENTBUS) { org.greenrobot.eventbus.EventBus.getDefault().postSticky(event); } }
@Test public void testPostStickyRemoveAll() throws InterruptedException { eventBus.postSticky("Sticky"); eventBus.postSticky(new IntTestEvent(77)); eventBus.removeAllStickyEvents(); assertNull(eventBus.getStickyEvent(String.class)); assertNull(eventBus.getStickyEvent(IntTestEvent.class)); eventBus.register(this); assertNull(lastEvent); assertEquals(0, eventCount.intValue()); }
@Test public void testRemoveStickyEventInSubscriber() throws InterruptedException { eventBus.register(new RemoveStickySubscriber()); eventBus.postSticky("Sticky"); eventBus.register(this); assertNull(lastEvent); assertEquals(0, eventCount.intValue()); assertNull(eventBus.getStickyEvent(String.class)); }
@Test public void testUnregisterWithoutRegister() { // Results in a warning without throwing eventBus.unregister(this); }
@Test public void testInstallDefaultEventBus() { EventBusBuilder builder = EventBus.builder(); try { // Either this should throw when another unit test got the default event bus... eventBus = builder.installDefaultEventBus(); Assert.assertEquals(eventBus, EventBus.getDefault()); // ...or this should throw eventBus = builder.installDefaultEventBus(); fail("Should have thrown"); } catch (EventBusException e) { // Expected } }
public AsyncExecutor buildForScope(Object executionContext) { if (eventBus == null) { eventBus = EventBus.getDefault(); } if (threadPool == null) { threadPool = Executors.newCachedThreadPool(); } if (failureEventType == null) { failureEventType = ThrowableFailureEvent.class; } return new AsyncExecutor(threadPool, eventBus, failureEventType, executionContext); } }