Java Code Examples for de.greenrobot.event.EventBus#removeStickyEvent()

The following examples show how to use de.greenrobot.event.EventBus#removeStickyEvent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NewVersionAvailableEventTest.java    From edx-app-android with Apache License 2.0 6 votes vote down vote up
/**
 * Verify that the {@link NewVersionBroadcastInterceptor} posts an instance of
 * {@link NewVersionAvailableEvent} on the event bus as a sticky event correctly, when
 * provided data (headers and status code) in it's request chain that resolve into a higher
 * priority event than one that has already been posted.
 */
@Test
public void testPostFromInterceptor_higherPriorityEvent_postsEventOnBus()
        throws IOException {
    assertNotEquals(higherPriorityEvent, lowerPriorityEvent);
    // This will throw an AssumptionViolatedException if the Android runtime
    // isn't loaded (i.e. through using the Robolectric test runner).
    final EventBus eventBus = getEventBus();
    eventBus.removeStickyEvent(NewVersionAvailableEvent.class);
    final NewVersionBroadcastInterceptor interceptor = new NewVersionBroadcastInterceptor();
    assertEquals(lowerPriorityEvent, postEventFromInterceptor(
            interceptor, lowerPriorityEvent));
    assertEquals(higherPriorityEvent, postEventFromInterceptor(
            interceptor, higherPriorityEvent));
    eventBus.removeStickyEvent(NewVersionAvailableEvent.class);
}
 
Example 2
Source File: NewVersionAvailableEventTest.java    From edx-app-android with Apache License 2.0 6 votes vote down vote up
/**
 * Post the provided data via the {@link NewVersionAvailableEvent#post(Version, Date, boolean)}
 * method, then remove the sticky event from the event bus.
 *
 * @param newVersion The new version.
 * @param lastSupportedDate The last supported date.
 * @param isUnsupported Whether the current version is unsupported.
 * @return The event that was posted. This can be null if the data does not constitute a valid
 * event.
 */
@Nullable
private static NewVersionAvailableEvent postAndRemoveEvent(
        @Nullable final Version newVersion,
        @Nullable final Date lastSupportedDate,
        final boolean isUnsupported) {
    // This will throw an AssumptionViolatedException if the Android runtime
    // isn't loaded (i.e. through using the Robolectric test runner).
    final EventBus eventBus = getEventBus();
    eventBus.removeStickyEvent(NewVersionAvailableEvent.class);
    final NewVersionAvailableEvent event =
            postEvent(newVersion, lastSupportedDate, isUnsupported);
    if (event != null) {
        assertTrue(eventBus.removeStickyEvent(event));
    }
    return event;
}
 
Example 3
Source File: NewVersionAvailableEventTest.java    From edx-app-android with Apache License 2.0 6 votes vote down vote up
/**
 * Post the provided data by passing the appropriate headers and status code in a request chain
 * through an {@link NewVersionBroadcastInterceptor}, then remove the sticky event from the
 * event bus.
 *
 * @param newVersion The new version.
 * @param lastSupportedDate The last supported date.
 * @param isUnsupported Whether the current version is unsupported.
 * @return The event that was posted. This can be null if the data does not constitute a valid
 * event.
 */
@Nullable
private static NewVersionAvailableEvent postAndRemoveEventFromInterceptor(
        @Nullable final Version newVersion,
        @Nullable final Date lastSupportedDate,
        final boolean isUnsupported) throws IOException {
    // This will throw an AssumptionViolatedException if the Android runtime
    // isn't loaded (i.e. through using the Robolectric test runner).
    final EventBus eventBus = getEventBus();
    eventBus.removeStickyEvent(NewVersionAvailableEvent.class);
    final NewVersionAvailableEvent event = postEventFromInterceptor(
            new NewVersionBroadcastInterceptor(), newVersion, lastSupportedDate, isUnsupported);
    if (event != null) {
        assertTrue(eventBus.removeStickyEvent(event));
    }
    return event;
}
 
Example 4
Source File: NewVersionAvailableEventTest.java    From edx-app-android with Apache License 2.0 5 votes vote down vote up
/**
 * Verify that the {@link NewVersionAvailableEvent#post(Version, Date, boolean)} method
 * doesn't do anything when provided parameters that resolve into a lower priority event
 * than one that has already been posted.
 */
@Test
public void testPost_lowerPriorityEvent_doesNothing() {
    assertNotEquals(higherPriorityEvent, lowerPriorityEvent);
    // This will throw an AssumptionViolatedException if the Android runtime
    // isn't loaded (i.e. through using the Robolectric test runner).
    final EventBus eventBus = getEventBus();
    eventBus.removeStickyEvent(NewVersionAvailableEvent.class);
    assertEquals(higherPriorityEvent, postEvent(higherPriorityEvent));
    assertEquals(higherPriorityEvent, postEvent(lowerPriorityEvent));
    eventBus.removeStickyEvent(NewVersionAvailableEvent.class);
}
 
Example 5
Source File: NewVersionAvailableEventTest.java    From edx-app-android with Apache License 2.0 5 votes vote down vote up
/**
 * Verify that the {@link NewVersionAvailableEvent#post(Version, Date, boolean)} method
 * posts an instance of {@link NewVersionAvailableEvent} on the event bus as a sticky event
 * correctly, when provided parameters that resolve into a higher priority event than one
 * that has already been posted.
 */
@Test
public void testPost_higherPriorityEvent_postsEventOnBus() {
    assertNotEquals(higherPriorityEvent, lowerPriorityEvent);
    // This will throw an AssumptionViolatedException if the Android runtime
    // isn't loaded (i.e. through using the Robolectric test runner).
    final EventBus eventBus = getEventBus();
    eventBus.removeStickyEvent(NewVersionAvailableEvent.class);
    assertEquals(lowerPriorityEvent, postEvent(lowerPriorityEvent));
    assertEquals(higherPriorityEvent, postEvent(higherPriorityEvent));
    eventBus.removeStickyEvent(NewVersionAvailableEvent.class);
}
 
Example 6
Source File: NewVersionAvailableEventTest.java    From edx-app-android with Apache License 2.0 5 votes vote down vote up
/**
 * Verify that the {@link NewVersionBroadcastInterceptor} doesn't do anything when provided
 * data (headers and status code) in it's request chain that resolve into a lower priority
 * event than one that has already been posted.
 */
@Test
public void testPostFromInterceptor_lowerPriorityEvent_doesNothing() throws IOException {
    assertNotEquals(higherPriorityEvent, lowerPriorityEvent);
    // This will throw an AssumptionViolatedException if the Android runtime
    // isn't loaded (i.e. through using the Robolectric test runner).
    final EventBus eventBus = getEventBus();
    eventBus.removeStickyEvent(NewVersionAvailableEvent.class);
    final NewVersionBroadcastInterceptor interceptor = new NewVersionBroadcastInterceptor();
    assertEquals(higherPriorityEvent, postEventFromInterceptor(
            interceptor, higherPriorityEvent));
    assertEquals(higherPriorityEvent, postEventFromInterceptor(
            interceptor, lowerPriorityEvent));
    eventBus.removeStickyEvent(NewVersionAvailableEvent.class);
}
 
Example 7
Source File: NewVersionAvailableEventTest.java    From edx-app-android with Apache License 2.0 5 votes vote down vote up
/**
 * Post the provided data by passing the appropriate headers and status code in a request chain
 * through an {@link NewVersionBroadcastInterceptor}.
 *
 * @param interceptor The interceptor through which the data is to be posted.
 * @param newVersion The new version.
 * @param lastSupportedDate The last supported date.
 * @param isUnsupported Whether the current version is unsupported.
 * @return The posted event. This can be null if the data does not constitute a valid event.
 */
@Nullable
private static NewVersionAvailableEvent postEventFromInterceptor(
        @NonNull final NewVersionBroadcastInterceptor interceptor,
        @Nullable final Version newVersion,
        @Nullable final Date lastSupportedDate,
        final boolean isUnsupported) throws IOException {
    // This will throw an AssumptionViolatedException if the Android runtime
    // isn't loaded (i.e. through using the Robolectric test runner).
    final EventBus eventBus = getEventBus();
    eventBus.removeStickyEvent(NewVersionAvailableEvent.class);
    final Interceptor.Chain chain = mock(Interceptor.Chain.class);
    final Request request = new Request.Builder()
            .url("https://localhost:1/")
            .build();
    final Response response; {
        final Response.Builder responseBuilder = new Response.Builder();
        responseBuilder.request(request);
        responseBuilder.protocol(Protocol.HTTP_1_1);
        responseBuilder.code(isUnsupported ? UPGRADE_REQUIRED : ACCEPTED);
        responseBuilder.message("");
        if (newVersion != null) {
            responseBuilder.header(HEADER_APP_LATEST_VERSION, newVersion.toString());
        }
        if (lastSupportedDate != null) {
            responseBuilder.header(HEADER_APP_VERSION_LAST_SUPPORTED_DATE,
                    ISO8601Utils.format(lastSupportedDate, true));
        }
        response = responseBuilder.build();
    }
    when(chain.request()).thenReturn(request);
    when(chain.proceed(request)).thenReturn(response);
    interceptor.intercept(chain);
    final InOrder inOrder = inOrder(chain);
    inOrder.verify(chain).request();
    inOrder.verify(chain).proceed(request);
    verifyNoMoreInteractions(chain);
    return eventBus.getStickyEvent(NewVersionAvailableEvent.class);
}