@Test public void testRecyclesTransformedResourceAfterWritingIfTransformedResourceIsDifferent() { when(decoder.getFrameCount()).thenReturn(1); Bitmap expected = Bitmap.createBitmap(100, 200, Bitmap.Config.RGB_565); when(transformedResource.get()).thenReturn(expected); when(frameTransformation.transform(anyContext(), eq(frameResource), anyInt(), anyInt())) .thenReturn(transformedResource); when(gifEncoder.start(any(OutputStream.class))).thenReturn(true); encoder.encode(resource, file, options); InOrder order = inOrder(transformedResource, gifEncoder); order.verify(gifEncoder).addFrame(eq(expected)); order.verify(transformedResource).recycle(); }
/** * Detects which build system is in use and returns the appropriate ManifestFactory implementation. * * Custom TestRunner subclasses may wish to override this method to provide alternate configuration. * * @param config Specification of the SDK version, manifest file, package name, etc. */ private ManifestFactory getManifestFactory(Config config) { Properties buildSystemApiProperties = getBuildSystemApiProperties(); if (buildSystemApiProperties != null) { return new DefaultManifestFactory(buildSystemApiProperties); } Class<?> buildConstants = config.constants(); //noinspection ConstantConditions if (BuckManifestFactory.isBuck()) { return new BuckManifestFactory(); } else if (buildConstants != Void.class) { return new GradleManifestFactory(); } else { return new MavenManifestFactory(); } }
@Test public void startFromFirstFrame_withPendingFrame_clearsPendingFrame() { loader = createGifFrameLoader(/*handler=*/ null); DelayTarget loaded = mock(DelayTarget.class); when(loaded.getResource()).thenReturn(Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888)); loader.onFrameReady(loaded); loader.unsubscribe(callback); DelayTarget nextFrame = mock(DelayTarget.class); Bitmap expected = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888); when(nextFrame.getResource()) .thenReturn(expected); loader.onFrameReady(nextFrame); loader.setNextStartFromFirstFrame(); verify(requestManager).clear(nextFrame); loader.subscribe(callback); verify(callback, times(1)).onFrameReady(); }
@Test public void testCenterCropSetsOutBitmapToNotHaveAlphaIfInBitmapDoesNotHaveAlphaAndOutBitmapIsReused() { Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); Bitmap toReuse = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888); reset(bitmapPool); when(bitmapPool.get(eq(50), eq(50), eq(Bitmap.Config.ARGB_8888))).thenReturn(toReuse); toReuse.setHasAlpha(true); toTransform.setHasAlpha(false); Bitmap result = TransformationUtils.centerCrop(bitmapPool, toTransform, toReuse.getWidth(), toReuse.getHeight()); assertEquals(toReuse, result); assertFalse(result.hasAlpha()); }
@Test public void testFitCenterSetsOutBitmapToNotHaveAlphaIfInBitmapDoesNotHaveAlphaAndOutBitmapIsReused() { Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); Bitmap toReuse = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888); reset(bitmapPool); when(bitmapPool.get(eq(toReuse.getWidth()), eq(toReuse.getHeight()), eq(toReuse.getConfig()))) .thenReturn(toReuse); toReuse.setHasAlpha(true); toTransform.setHasAlpha(false); Bitmap result = TransformationUtils.fitCenter(bitmapPool, toTransform, toReuse.getWidth(), toReuse.getHeight()); assertEquals(toReuse, result); assertFalse(result.hasAlpha()); }
@Test public void testOnFrameReadyClearsPreviousFrame() { // Force the loader to create a real Handler. loader = createGifFrameLoader(null); DelayTarget previous = mock(DelayTarget.class); Request previousRequest = mock(Request.class); when(previous.getRequest()).thenReturn(previousRequest); when(previous.getResource()).thenReturn(Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888)); DelayTarget current = mock(DelayTarget.class); when(current.getResource()).thenReturn(Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565)); loader.onFrameReady(previous); loader.onFrameReady(current); verify(requestManager).clear(eq(previous)); }
@Test public void testReturnsBitmapWithExactlyGivenDimensionsIfBitmapIsLargerThanTarget() { int expectedWidth = 75; int expectedHeight = 74; for (int[] dimens : new int[][] { new int[] { 800, 200 }, new int[] { 450, 100 }, new int[] { 78, 78 } }) { Bitmap toTransform = Bitmap.createBitmap(dimens[0], dimens[1], Bitmap.Config.ARGB_4444); when(resource.get()).thenReturn(toTransform); Resource<Bitmap> result = centerCrop.transform(context, resource, expectedWidth, expectedHeight); Bitmap transformed = result.get(); assertEquals(expectedWidth, transformed.getWidth()); assertEquals(expectedHeight, transformed.getHeight()); } }
@Test public void testCenterCropSetsOutBitmapToHaveAlphaIfInBitmapHasAlphaAndOutBitmapIsReused() { Bitmap toTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); Bitmap toReuse = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888); reset(bitmapPool); when(bitmapPool.get(eq(50), eq(50), eq(Bitmap.Config.ARGB_8888))) .thenReturn(toReuse); toReuse.setHasAlpha(false); toTransform.setHasAlpha(true); Bitmap result = TransformationUtils.centerCrop(bitmapPool, toTransform, toReuse.getWidth(), toReuse.getHeight()); assertEquals(toReuse, result); assertTrue(result.hasAlpha()); }
@Test public void onFrameReady_whenNotRunning_clearsOldFrameOnStart() { loader = createGifFrameLoader(/*handler=*/ null); DelayTarget loaded = mock(DelayTarget.class); when(loaded.getResource()).thenReturn(Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888)); loader.onFrameReady(loaded); loader.unsubscribe(callback); DelayTarget nextFrame = mock(DelayTarget.class); when(nextFrame.getResource()) .thenReturn(Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888)); loader.onFrameReady(nextFrame); loader.subscribe(callback); verify(requestManager).clear(loaded); }
@Test public void testReturnsNewResourceWhenBitmapTransformed() { final Bitmap transformed = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_4444); BitmapTransformation transformation = new BitmapTransformation() { @Override public void updateDiskCacheKey(MessageDigest messageDigest) { } @Override protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap bitmap, int outWidth, int outHeight) { return transformed; } }; Resource<Bitmap> resource = mockResource(1, 2); assertNotSame(resource, transformation.transform(context, resource, 100, 100)); }
@Test public void testReturnsNewResourceIfTransformationDoesTransform() { int outWidth = 999; int outHeight = 555; Bitmap transformedBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.RGB_565); Resource<Bitmap> transformedBitmapResource = Util.mockResource(); when(transformedBitmapResource.get()).thenReturn(transformedBitmap); when(wrapped.transform(anyContext(), Util.<Bitmap>anyResource(), eq(outWidth), eq(outHeight))) .thenReturn(transformedBitmapResource); Resource<BitmapDrawable> transformed = transformation.transform(context, drawableResourceToTransform, outWidth, outHeight); assertThat(transformed.get().getBitmap()).isEqualTo(transformedBitmap); }
@Override protected AndroidManifest getAppManifest(Config config) { final String manifestPath = PathResolver.resolveAndroidManifestPath(); final String resourcesPath = PathResolver.resolveResPath(); final String assetsPath = PathResolver.resolveAssetsPath(); AndroidManifest manifest = new AndroidManifest( Fs.fileFromPath(manifestPath), Fs.fileFromPath(resourcesPath), Fs.fileFromPath(assetsPath)) { @Override public int getTargetSdkVersion() { return TARGET_SDK_VERSION; } @Override public int getMinSdkVersion() { return MIN_SDK_VERSION; } }; return manifest; }
@Test public void testAddsBitmapsToMemoryCacheIfMemoryCacheHasEnoughSpaceRemaining() { Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); when(cache.getMaxSize()).thenReturn(Util.getBitmapByteSize(bitmap)); PreFillType size = new PreFillType.Builder(bitmap.getWidth(), bitmap.getHeight()).setConfig(bitmap.getConfig()) .build(); Map<PreFillType, Integer> allocationOrder = new HashMap<>(); allocationOrder.put(size, 1); getHandler(allocationOrder).run(); verify(cache).put(any(Key.class), anyResource()); verify(pool, never()).put(any(Bitmap.class)); // TODO(b/20335397): This code was relying on Bitmap equality which Robolectric removed // assertThat(addedBitmaps).containsExactly(bitmap); }
@Test public void testWritesTransformedBitmaps() { final Bitmap frame = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); when(decoder.getFrameCount()).thenReturn(1); when(decoder.getNextFrame()).thenReturn(frame); when(gifEncoder.start(any(OutputStream.class))).thenReturn(true); int expectedWidth = 123; int expectedHeight = 456; when(gifDrawable.getIntrinsicWidth()).thenReturn(expectedWidth); when(gifDrawable.getIntrinsicHeight()).thenReturn(expectedHeight); Bitmap transformedFrame = Bitmap.createBitmap(200, 200, Bitmap.Config.RGB_565); when(transformedResource.get()).thenReturn(transformedFrame); when(frameTransformation.transform( anyContext(), eq(frameResource), eq(expectedWidth), eq(expectedHeight))) .thenReturn(transformedResource); when(gifDrawable.getFrameTransformation()).thenReturn(frameTransformation); encoder.encode(resource, file, options); verify(gifEncoder).addFrame(eq(transformedFrame)); }
@Test public void testAddsBitmapsToPoolIfMemoryCacheIsNotFullButCannotFitBitmap() { Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); when(cache.getMaxSize()).thenReturn(Util.getBitmapByteSize(bitmap) / 2); PreFillType size = new PreFillType.Builder(bitmap.getWidth(), bitmap.getHeight()).setConfig(bitmap.getConfig()) .build(); Map<PreFillType, Integer> allocationOrder = new HashMap<>(); allocationOrder.put(size, 1); getHandler(allocationOrder).run(); verify(cache, never()).put(any(Key.class), anyResource()); // TODO(b/20335397): This code was relying on Bitmap equality which Robolectric removed //verify(pool).put(eq(bitmap)); //assertThat(addedBitmaps).containsExactly(bitmap); }
@Test public void testSetsDelayOnEncoderAfterAddingFrame() { when(gifEncoder.start(any(OutputStream.class))).thenReturn(true); when(gifEncoder.addFrame(any(Bitmap.class))).thenReturn(true); when(decoder.getFrameCount()).thenReturn(1); when(decoder.getNextFrame()).thenReturn(Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565)); int expectedIndex = 34; when(decoder.getCurrentFrameIndex()).thenReturn(expectedIndex); int expectedDelay = 5000; when(decoder.getDelay(eq(expectedIndex))).thenReturn(expectedDelay); encoder.encode(resource, file, options); InOrder order = inOrder(gifEncoder, decoder); order.verify(decoder).advance(); order.verify(gifEncoder).addFrame(any(Bitmap.class)); order.verify(gifEncoder).setDelay(eq(expectedDelay)); order.verify(decoder).advance(); }
@Test public void testSetImageBitmap(){ ImageView imageView = mWXImage.initComponentHostView(Robolectric.setupActivity(TestActivity.class)); imageView.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); imageView.setImageBitmap(null); assertNull(imageView.getDrawable()); imageView.setImageBitmap(Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565)); assertNotNull(imageView.getDrawable()); }
@Override protected Config buildGlobalConfig() { return new Config.Builder() .setSdk(22) .setManifest("src/main/AndroidManifest.xml") .build(); }
@Test public void testReturnsCorrectBitmapSizeForRgb565() { int width = 34; int height = 1444; int size = Util.getBitmapByteSize(width, height, Bitmap.Config.RGB_565); assertEquals(width * height * 2, size); }
@Test public void testAdvancesDecoderBeforeAttemptingToGetFirstFrame() { when(gifEncoder.start(any(OutputStream.class))).thenReturn(true); when(decoder.getFrameCount()).thenReturn(1); when(decoder.getNextFrame()).thenReturn(Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888)); encoder.encode(resource, file, options); InOrder order = inOrder(decoder); order.verify(decoder).advance(); order.verify(decoder).getNextFrame(); }
@Test public void testWritesSingleFrameToEncoderAndReturnsTrueIfEncoderFinishes() { Bitmap frame = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); when(frameResource.get()).thenReturn(frame); when(decoder.getFrameCount()).thenReturn(1); when(decoder.getNextFrame()).thenReturn(frame); when(gifEncoder.start(any(OutputStream.class))).thenReturn(true); when(gifEncoder.addFrame(eq(frame))).thenReturn(true); when(gifEncoder.finish()).thenReturn(true); assertTrue(encoder.encode(resource, file, options)); verify(gifEncoder).addFrame(eq(frame)); }
@Test public void testReturnsFalseIfAddingFrameFails() { when(decoder.getFrameCount()).thenReturn(1); when(decoder.getNextFrame()).thenReturn(Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888)); when(gifEncoder.start(any(OutputStream.class))).thenReturn(true); when(gifEncoder.addFrame(any(Bitmap.class))).thenReturn(false); assertFalse(encoder.encode(resource, file, options)); }
@Test public void testSetsBitmapOnRemoteViewsWithViewIdWhenCreatedWithComponentName() { ComponentName componentName = mock(ComponentName.class); AppWidgetTarget target = new AppWidgetTarget(RuntimeEnvironment.application, viewId, views, componentName); Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); target.onResourceReady(bitmap, null /*glideAnimation*/); verify(views).setImageViewBitmap(eq(viewId), eq(bitmap)); }
@Test public void updatesNotificationManagerWithNotificationIdAndNotificationOnResourceReady() { target.onResourceReady(Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888), null /*glideAnimation*/); assertEquals(notificationId, shadowManager.updatedNotificationId); assertEquals(notificationTag, shadowManager.updatedNotificationTag); assertEquals(notification, shadowManager.updatedNotification); }
@Test public void testReturnsTargetForBitmaps() { Bitmap bitmap = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888); Target<Bitmap> target = factory.buildTarget(view, Bitmap.class); target.onResourceReady(bitmap, null); assertThat(target).isInstanceOf(BitmapImageViewTarget.class); }
@Test public void testReturnsTargetForBitmapDrawables() { BitmapDrawable drawable = new BitmapDrawable(RuntimeEnvironment.application.getResources(), Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_4444)); Target<BitmapDrawable> target = factory.buildTarget(view, BitmapDrawable.class); target.onResourceReady(drawable, null); assertThat(target).isInstanceOf(DrawableImageViewTarget.class); }
@Before public void setUp() { MockitoAnnotations.initMocks(this); bitmapToTransform = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); BitmapDrawable drawableToTransform = new BitmapDrawable(bitmapToTransform); context = RuntimeEnvironment.application; Glide.init(context, new GlideBuilder().setBitmapPool(bitmapPool)); when(drawableResourceToTransform.get()).thenReturn(drawableToTransform); transformation = new BitmapDrawableTransformation(wrapped); }
@Test public void testSetsBitmapOnRemoteViewsWithViewIdWhenCreatedWithWidgetIds() { int[] widgetIds = new int[] { 1 }; AppWidgetTarget target = new AppWidgetTarget(RuntimeEnvironment.application, viewId, views, widgetIds); Bitmap bitmap = Bitmap.createBitmap(100, 200, Bitmap.Config.RGB_565); target.onResourceReady(bitmap, null /*glideAnimation*/); verify(views).setImageViewBitmap(eq(viewId), eq(bitmap)); }
@Test @Config(sdk = LOLLIPOP) public void shouldFallbackToDefault_whenNetworkInfoFromNetworkNull() { NETWORK = getNetwork(); doReturn(null).when(connectivityManager).getNetworkInfo(NETWORK); RxNetworkInfo sut = create(NETWORK, connectivityManager); assertThat(sut).isEqualTo(DEFAULT_RXNETWORK_INFO); }
@Test public void testFitCenterWithSmallTallBitmap() { final int maxSide = 500; Bitmap smallTall = Bitmap.createBitmap(10, 400, Bitmap.Config.ARGB_8888); Bitmap transformed = TransformationUtils.fitCenter(bitmapPool, smallTall, maxSide, maxSide); assertHasOriginalAspectRatio(smallTall, transformed); assertBitmapFitsExactlyWithinBounds(maxSide, transformed); }
@Test public void testDoesDrawCurrentFrameIfOneIsAvailable() { Canvas canvas = mock(Canvas.class); Bitmap currentFrame = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_4444); when(frameLoader.getCurrentFrame()).thenReturn(currentFrame); drawable.draw(canvas); verify(canvas).drawBitmap(eq(currentFrame), (Rect) isNull(), isARect(), isAPaint()); verify(canvas, never()).drawBitmap(eq(firstFrame), (Rect) isNull(), isARect(), isAPaint()); }
@Test public void testReturnsNewDrawableFromConstantState() { Bitmap firstFrame = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); drawable = new GifDrawable(RuntimeEnvironment.application, mock(GifDecoder.class), bitmapPool, transformation, 100, 100, firstFrame); assertNotNull(drawable.getConstantState().newDrawable()); assertNotNull( drawable.getConstantState().newDrawable(RuntimeEnvironment.application.getResources())); }
@Test public void testAsksBitmapPoolForArgb8888IfInConfigIsNull() { Shadows.shadowOf(bitmap).setConfig(null); centerCrop.transform(context, resource, 10, 10); verify(pool).get(anyInt(), anyInt(), eq(Bitmap.Config.ARGB_8888)); verify(pool, never()).get(anyInt(), anyInt(), (Bitmap.Config) isNull()); }
@Test public void testSetsFrameTransformationOnFrameManager() { Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); drawable.setFrameTransformation(transformation, bitmap); verify(frameLoader).setFrameTransformation(eq(transformation), eq(bitmap)); }
@Test public void testReturnsCurrentTransformationInGetFrameTransformation() { @SuppressWarnings("unchecked") Transformation<Bitmap> newTransformation = mock(Transformation.class); Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); drawable.setFrameTransformation(newTransformation, bitmap); verify(frameLoader).setFrameTransformation(eq(newTransformation), eq(bitmap)); }
@Test public void testLoadBitmap_asBitmap() { Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); requestManager .asBitmap() .load(bitmap) .into(target); verify(target).onResourceReady(eq(bitmap), any(Transition.class)); }
@Test public void testPreFillHandlerDoesNotPostIfHasBitmapsButIsCancelled() { PreFillType size = new PreFillType.Builder(1).setConfig(Bitmap.Config.ARGB_8888).build(); Map<PreFillType, Integer> allocationOrder = new HashMap<>(); allocationOrder.put(size, 2); BitmapPreFillRunner handler = getHandler(allocationOrder); when(clock.now()).thenReturn(0L).thenReturn(0L).thenReturn(BitmapPreFillRunner.MAX_DURATION_MS); handler.cancel(); handler.run(); verify(mainHandler, never()).postDelayed(any(Runnable.class), anyLong()); }
protected void beforeTest(Sandbox sandbox, Spec spec) throws Throwable { SdkEnvironment sdkEnvironment = (SdkEnvironment) sandbox; RoboSpec roboSpec = (RoboSpec) spec; roboSpec.parallelUniverseInterface = getHooksInterface(sdkEnvironment); Class<TestLifecycle> cl = sdkEnvironment.bootstrappedClass(getTestLifecycleClass()); roboSpec.testLifecycle = ReflectionHelpers.newInstance(cl); final Config config = roboSpec.config; final AndroidManifest appManifest = roboSpec.getAppManifest(); roboSpec.parallelUniverseInterface.setSdkConfig((sdkEnvironment).getSdkConfig()); roboSpec.parallelUniverseInterface.resetStaticState(config); SdkConfig sdkConfig = roboSpec.sdkConfig; Class<?> androidBuildVersionClass = (sdkEnvironment).bootstrappedClass(Build.VERSION.class); ReflectionHelpers.setStaticField(androidBuildVersionClass, "SDK_INT", sdkConfig.getApiLevel()); ReflectionHelpers.setStaticField(androidBuildVersionClass, "RELEASE", sdkConfig.getAndroidVersion()); ReflectionHelpers.setStaticField(androidBuildVersionClass, "CODENAME", sdkConfig.getAndroidCodeName()); PackageResourceTable systemResourceTable = sdkEnvironment.getSystemResourceTable(getJarResolver()); PackageResourceTable appResourceTable = getAppResourceTable(appManifest); // This will always be non empty since every class has basic methods like toString. Method randomMethod = getTestClass().getJavaClass().getMethods()[0]; roboSpec.parallelUniverseInterface.setUpApplicationState( randomMethod, roboSpec.testLifecycle, appManifest, config, new RoutingResourceTable(getCompiletimeSdkResourceTable(), appResourceTable), new RoutingResourceTable(systemResourceTable, appResourceTable), new RoutingResourceTable(systemResourceTable)); roboSpec.testLifecycle.beforeTest(null); }