Search in sources :

Example 1 with FakeClock

use of com.facebook.imagepipeline.testing.FakeClock in project fresco by facebook.

the class AnimatedDrawableCachingBackendImplTest method setup.

@Before
public void setup() {
    mActivityManager = (ActivityManager) RuntimeEnvironment.application.getSystemService(Context.ACTIVITY_SERVICE);
    mFakeClock = new FakeClock();
    mExecutorService = new TestExecutorService(mFakeClock);
    mAnimatedDrawableUtil = new AnimatedDrawableUtil();
    mDrawableBackend = new TestAnimatedDrawableBackend(WIDTH, HEIGHT, FRAME_DURATIONS);
}
Also used : TestAnimatedDrawableBackend(com.facebook.imagepipeline.animated.testing.TestAnimatedDrawableBackend) FakeClock(com.facebook.imagepipeline.testing.FakeClock) TestExecutorService(com.facebook.imagepipeline.testing.TestExecutorService) AnimatedDrawableUtil(com.facebook.imagepipeline.animated.util.AnimatedDrawableUtil) Before(org.junit.Before)

Example 2 with FakeClock

use of com.facebook.imagepipeline.testing.FakeClock in project fresco by facebook.

the class AnimatedSingleUsePostprocessorProducerTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    mTestExecutorService = new TestExecutorService(new FakeClock());
    mPostprocessorProducer = new PostprocessorProducer(mInputProducer, mPlatformBitmapFactory, mTestExecutorService);
    when(mImageRequest.getPostprocessor()).thenReturn(mPostprocessor);
    when(mProducerContext.getId()).thenReturn(mRequestId);
    when(mProducerContext.getProducerListener()).thenReturn(mProducerListener);
    when(mProducerContext.getImageRequest()).thenReturn(mImageRequest);
    mResults = new ArrayList<>();
    when(mPostprocessor.getName()).thenReturn(POSTPROCESSOR_NAME);
    when(mProducerListener.requiresExtraMap(eq(mProducerContext), eq(POSTPROCESSOR_NAME))).thenReturn(true);
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            mResults.add(((CloseableReference<CloseableImage>) invocation.getArguments()[0]).clone());
            return null;
        }
    }).when(mConsumer).onNewResult(any(CloseableReference.class), anyInt());
    mInOrder = inOrder(mPostprocessor, mProducerListener, mConsumer);
    mSourceBitmap = mock(Bitmap.class);
    mSourceCloseableStaticBitmap = mock(CloseableStaticBitmap.class);
    when(mSourceCloseableStaticBitmap.getUnderlyingBitmap()).thenReturn(mSourceBitmap);
    mSourceCloseableImageRef = CloseableReference.<CloseableImage>of(mSourceCloseableStaticBitmap);
    mDestinationBitmap = mock(Bitmap.class);
    mDestinationCloseableBitmapRef = CloseableReference.of(mDestinationBitmap, mBitmapResourceReleaser);
}
Also used : CloseableStaticBitmap(com.facebook.imagepipeline.image.CloseableStaticBitmap) Bitmap(android.graphics.Bitmap) FakeClock(com.facebook.imagepipeline.testing.FakeClock) TestExecutorService(com.facebook.imagepipeline.testing.TestExecutorService) CloseableReference(com.facebook.common.references.CloseableReference) CloseableStaticBitmap(com.facebook.imagepipeline.image.CloseableStaticBitmap)

Example 3 with FakeClock

use of com.facebook.imagepipeline.testing.FakeClock in project fresco by facebook.

the class DiskStorageCacheTest method setUp.

@Before
public void setUp() {
    mClock = mock(SystemClock.class);
    PowerMockito.mockStatic(SystemClock.class);
    PowerMockito.when(SystemClock.get()).thenReturn(mClock);
    mBackgroundExecutor = new TestExecutorService(new FakeClock());
    mDiskTrimmableRegistry = mock(DiskTrimmableRegistry.class);
    mCacheEventListener = mock(CacheEventListener.class);
    mCacheEventListenerInOrder = inOrder(mCacheEventListener);
    // we know the directory will be this
    mCacheDirectory = new File(RuntimeEnvironment.application.getCacheDir(), CACHE_TYPE);
    mCacheDirectory.mkdirs();
    if (!mCacheDirectory.exists()) {
        throw new RuntimeException(String.format((Locale) null, "Cannot create cache dir: %s: directory %s", mCacheDirectory.getAbsolutePath(), mCacheDirectory.exists() ? "already exists" : "does not exist"));
    }
    mStorage = createDiskStorage(TESTCACHE_VERSION_START_OF_VERSIONING);
    mCache = createDiskCache(mStorage, false);
    mCache.clearAll();
    reset(mCacheEventListener);
    verify(mDiskTrimmableRegistry).registerDiskTrimmable(mCache);
}
Also used : Locale(java.util.Locale) CacheEventListener(com.facebook.cache.common.CacheEventListener) SystemClock(com.facebook.common.time.SystemClock) FakeClock(com.facebook.imagepipeline.testing.FakeClock) TestExecutorService(com.facebook.imagepipeline.testing.TestExecutorService) DiskTrimmableRegistry(com.facebook.common.disk.DiskTrimmableRegistry) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Before(org.junit.Before)

Example 4 with FakeClock

use of com.facebook.imagepipeline.testing.FakeClock in project fresco by facebook.

the class LocalVideoThumbnailProducerTest method setUp.

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    mExecutor = new TestExecutorService(new FakeClock());
    mLocalVideoThumbnailProducer = new LocalVideoThumbnailProducer(mExecutor, RuntimeEnvironment.application.getContentResolver());
    mFile = new File(RuntimeEnvironment.application.getExternalFilesDir(null), TEST_FILENAME);
    mockStatic(ThumbnailUtils.class);
    mProducerContext = new SettableProducerContext(mImageRequest, mRequestId, mProducerListener, mock(Object.class), ImageRequest.RequestLevel.FULL_FETCH, false, false, Priority.MEDIUM, mConfig);
    when(mImageRequest.getSourceFile()).thenReturn(mFile);
    mLocalVideoUri = Uri.parse("file:///dancing_hotdog.mp4");
}
Also used : FakeClock(com.facebook.imagepipeline.testing.FakeClock) TestExecutorService(com.facebook.imagepipeline.testing.TestExecutorService) File(java.io.File)

Example 5 with FakeClock

use of com.facebook.imagepipeline.testing.FakeClock in project fresco by facebook.

the class PriorityNetworkFetcherTest method testNotMovingDelayedRequeue_requeueOnFail.

/**
 * Scenario: an image fetch fails. We expect it to be re-queued up to maxNumberOfRequeue times.
 * The reuqest should be re-queued immediately up to immediateRequeueCount and then it will wait
 * in the delayedQueue to be re-queued again after delayTimeInMillis. In this test we make sure
 * the request is not moved back to the priority queue unless delayTimeInMillis has passed.
 */
@Test
public void testNotMovingDelayedRequeue_requeueOnFail() {
    RecordingNetworkFetcher recordingNetworkFetcher = new RecordingNetworkFetcher();
    final int maxNumberOfRequeue = 3;
    final int immediateRequeueCount = 1;
    final int delayTimeInMillis = 300;
    FakeClock clock = new FakeClock();
    // Max hi-pri: 1, max low-pri: 0
    PriorityNetworkFetcher<FetchState> fetcher = new PriorityNetworkFetcher<>(recordingNetworkFetcher, false, 1, 0, true, maxNumberOfRequeue, false, immediateRequeueCount, delayTimeInMillis, false, false, /* nonRecoverableExceptionPreventsRequeue */
    1, /* maxConnectAttemptCount */
    1, /* maxAttemptCount */
    false, /* retryLowPriAll */
    false, /* retryLowPriUnknownHostException */
    false, /* retryLowPriConnectionException */
    clock);
    PriorityFetchState<FetchState> hipri1 = fetch(fetcher, "hipri1", callback, true);
    assertThat(fetcher.getCurrentlyFetching()).containsExactly(hipri1);
    assertThat(fetcher.getHiPriQueue()).isEmpty();
    assertThat(fetcher.getLowPriQueue()).isEmpty();
    // Simulate 1st failure in hipri1, the request should be requeued immediately.
    getOnlyElement(recordingNetworkFetcher.callbacks.get(hipri1.delegatedState)).onFailure(new Exception());
    assertThat(fetcher.getCurrentlyFetching()).containsExactly(hipri1);
    assertThat(fetcher.getHiPriQueue()).isEmpty();
    assertThat(fetcher.getLowPriQueue()).isEmpty();
    assertThat(fetcher.getDelayedQeueue()).isEmpty();
    assertThat(hipri1.requeueCount).isEqualTo(1);
    assertThat(fetcher.getExtraMap(hipri1, 123)).containsEntry("delay_count", "0");
    // Simulate 2nd failure in hipri1, the request should wait in the delayedQueue for
    // delayTimeInMillis.
    getOnlyElement(recordingNetworkFetcher.callbacks.get(hipri1.delegatedState)).onFailure(new Exception());
    assertThat(fetcher.getCurrentlyFetching()).isEmpty();
    assertThat(fetcher.getHiPriQueue()).isEmpty();
    assertThat(fetcher.getLowPriQueue()).isEmpty();
    assertThat(fetcher.getDelayedQeueue()).containsExactly(hipri1);
    assertThat(fetcher.getExtraMap(hipri1, 123)).containsEntry("delay_count", "1");
    clock.incrementBy(200);
    // to trigger inflight requests
    PriorityFetchState<FetchState> hipri2 = fetch(fetcher, "hipri2", callback, true);
    // 200 ms is smaller than delayTimeInMillis, so hipri1 request should still wait in the
    // delayedQueue.
    assertThat(fetcher.getCurrentlyFetching()).containsExactly(hipri2);
    assertThat(fetcher.getHiPriQueue()).isEmpty();
    assertThat(fetcher.getLowPriQueue()).isEmpty();
    assertThat(fetcher.getDelayedQeueue()).containsExactly(hipri1);
    assertThat(hipri1.requeueCount).isEqualTo(2);
}
Also used : PriorityFetchState(com.facebook.imagepipeline.producers.PriorityNetworkFetcher.PriorityFetchState) FakeClock(com.facebook.imagepipeline.testing.FakeClock) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) Test(org.junit.Test)

Aggregations

FakeClock (com.facebook.imagepipeline.testing.FakeClock)27 TestExecutorService (com.facebook.imagepipeline.testing.TestExecutorService)19 Before (org.junit.Before)10 EncodedImage (com.facebook.imagepipeline.image.EncodedImage)6 PriorityFetchState (com.facebook.imagepipeline.producers.PriorityNetworkFetcher.PriorityFetchState)5 TestScheduledExecutorService (com.facebook.imagepipeline.testing.TestScheduledExecutorService)5 Test (org.junit.Test)5 CloseableReference (com.facebook.common.references.CloseableReference)4 File (java.io.File)3 IOException (java.io.IOException)3 ConnectException (java.net.ConnectException)3 SocketTimeoutException (java.net.SocketTimeoutException)3 UnknownHostException (java.net.UnknownHostException)3 Bitmap (android.graphics.Bitmap)2 TestAnimatedDrawableBackend (com.facebook.imagepipeline.animated.testing.TestAnimatedDrawableBackend)2 CloseableStaticBitmap (com.facebook.imagepipeline.image.CloseableStaticBitmap)2 InputStream (java.io.InputStream)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Canvas (android.graphics.Canvas)1 Drawable (android.graphics.drawable.Drawable)1