Search in sources :

Example 1 with NewVersionBroadcastInterceptor

use of org.edx.mobile.http.interceptor.NewVersionBroadcastInterceptor in project edx-app-android by edx.

the class NewVersionAvailableEventTest method postAndRemoveEventFromInterceptor.

/**
 * 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;
}
Also used : EventBus(de.greenrobot.event.EventBus) NewVersionBroadcastInterceptor(org.edx.mobile.http.interceptor.NewVersionBroadcastInterceptor) Nullable(android.support.annotation.Nullable)

Example 2 with NewVersionBroadcastInterceptor

use of org.edx.mobile.http.interceptor.NewVersionBroadcastInterceptor in project edx-app-android by edx.

the class NewVersionAvailableEventTest method postEventFromInterceptor.

/**
 * 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);
        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);
}
Also used : Response(okhttp3.Response) InOrder(org.mockito.InOrder) Request(okhttp3.Request) EventBus(de.greenrobot.event.EventBus) Interceptor(okhttp3.Interceptor) NewVersionBroadcastInterceptor(org.edx.mobile.http.interceptor.NewVersionBroadcastInterceptor) Nullable(android.support.annotation.Nullable)

Aggregations

Nullable (android.support.annotation.Nullable)2 EventBus (de.greenrobot.event.EventBus)2 NewVersionBroadcastInterceptor (org.edx.mobile.http.interceptor.NewVersionBroadcastInterceptor)2 Interceptor (okhttp3.Interceptor)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 InOrder (org.mockito.InOrder)1