Search in sources :

Example 1 with ResourceCallback

use of com.bumptech.glide.request.ResourceCallback in project glide by bumptech.

the class EngineJob method handleResultOnMainThread.

@Synthetic
void handleResultOnMainThread() {
    stateVerifier.throwIfRecycled();
    if (isCancelled) {
        resource.recycle();
        release(false);
        return;
    } else if (cbs.isEmpty()) {
        throw new IllegalStateException("Received a resource without any callbacks to notify");
    } else if (hasResource) {
        throw new IllegalStateException("Already have resource");
    }
    engineResource = engineResourceFactory.build(resource, isCacheable);
    hasResource = true;
    // Hold on to resource for duration of request so we don't recycle it in the middle of
    // notifying if it synchronously released by one of the callbacks.
    engineResource.acquire();
    listener.onEngineJobComplete(key, engineResource);
    for (ResourceCallback cb : cbs) {
        if (!isInIgnoredCallbacks(cb)) {
            engineResource.acquire();
            cb.onResourceReady(engineResource, dataSource);
        }
    }
    // Our request is complete, so we can release the resource.
    engineResource.release();
    release(false);
}
Also used : ResourceCallback(com.bumptech.glide.request.ResourceCallback) Synthetic(com.bumptech.glide.util.Synthetic)

Example 2 with ResourceCallback

use of com.bumptech.glide.request.ResourceCallback in project glide by bumptech.

the class EngineTest method testCallbackIsAddedToExistingRunnerWithExistingLoad.

@Test
public void testCallbackIsAddedToExistingRunnerWithExistingLoad() {
    harness.doLoad();
    ResourceCallback newCallback = mock(ResourceCallback.class);
    harness.cb = newCallback;
    harness.doLoad();
    verify(harness.job).addCallback(eq(newCallback));
}
Also used : ResourceCallback(com.bumptech.glide.request.ResourceCallback) Test(org.junit.Test)

Example 3 with ResourceCallback

use of com.bumptech.glide.request.ResourceCallback in project glide by bumptech.

the class EngineJobTest method testRemovingCallbackDuringOnExceptionIsIgnoredIfCallbackHasAlreadyBeenCalled.

@Test
public void testRemovingCallbackDuringOnExceptionIsIgnoredIfCallbackHasAlreadyBeenCalled() {
    harness = new EngineJobHarness();
    final EngineJob<Object> job = harness.getJob();
    final ResourceCallback cb = mock(ResourceCallback.class);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            job.removeCallback(cb);
            return null;
        }
    }).when(cb).onLoadFailed(any(GlideException.class));
    GlideException exception = new GlideException("test");
    job.addCallback(cb);
    job.start(harness.decodeJob);
    job.onLoadFailed(exception);
    verify(cb, times(1)).onLoadFailed(eq(exception));
}
Also used : ResourceCallback(com.bumptech.glide.request.ResourceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 4 with ResourceCallback

use of com.bumptech.glide.request.ResourceCallback in project glide by bumptech.

the class EngineJobTest method testNotifiesNewCallbackOfResourceIfCallbackIsAddedDuringOnResourceReady.

@Test
public void testNotifiesNewCallbackOfResourceIfCallbackIsAddedDuringOnResourceReady() {
    final EngineJob<Object> job = harness.getJob();
    final ResourceCallback existingCallback = mock(ResourceCallback.class);
    final ResourceCallback newCallback = mock(ResourceCallback.class);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            job.addCallback(newCallback);
            return null;
        }
    }).when(existingCallback).onResourceReady(anyResource(), isADataSource());
    job.addCallback(existingCallback);
    job.start(harness.decodeJob);
    job.onResourceReady(harness.resource, harness.dataSource);
    verify(newCallback).onResourceReady(eq(harness.engineResource), eq(harness.dataSource));
}
Also used : ResourceCallback(com.bumptech.glide.request.ResourceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 5 with ResourceCallback

use of com.bumptech.glide.request.ResourceCallback in project glide by bumptech.

the class EngineJobTest method testRemovingCallbackDuringOnResourceReadyPreventsResourceFromBeingAcquiredForCallback.

@Test
public void testRemovingCallbackDuringOnResourceReadyPreventsResourceFromBeingAcquiredForCallback() {
    final EngineJob<Object> job = harness.getJob();
    final ResourceCallback notYetCalled = mock(ResourceCallback.class);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            job.removeCallback(notYetCalled);
            return null;
        }
    }).when(harness.cb).onResourceReady(anyResource(), isADataSource());
    job.addCallback(notYetCalled);
    job.start(harness.decodeJob);
    job.onResourceReady(harness.resource, harness.dataSource);
    // Once for notifying, once for called.
    verify(harness.engineResource, times(2)).acquire();
}
Also used : ResourceCallback(com.bumptech.glide.request.ResourceCallback) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Aggregations

ResourceCallback (com.bumptech.glide.request.ResourceCallback)12 Test (org.junit.Test)10 InvocationOnMock (org.mockito.invocation.InvocationOnMock)7 Synthetic (com.bumptech.glide.util.Synthetic)2