/** * Returns the resource id that this {@code BitmapDrawable} was loaded from. This lets * your tests assert that the bitmap is correct without having to actually load the bitmap. * * @return resource id from which this {@code BitmapDrawable} was loaded * @deprecated use ShadowBitmap#getCreatedFromResId() instead. */ @Deprecated @Override public int getCreatedFromResId() { ShadowBitmap shadowBitmap = Shadow.extract(realBitmapDrawable.getBitmap()); return shadowBitmap.getCreatedFromResId(); }
@Override public boolean matches(InspectableComponent value) { if (value.getComponentClass() != Image.class) { return false; } Image impl = (Image) value.getComponent(); final Drawable propValueDrawable = impl.drawable; return Shadows.shadowOf(((BitmapDrawable) propValueDrawable).getBitmap()) .getCreatedFromResId() == resId; } };
@Test public void testImageViewSrcIsSet() throws Exception { View mediaView = inflate(layout.main); ImageView imageView = mediaView.findViewById(R.id.image); BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable(); assertThat(shadowOf(drawable.getBitmap()).getCreatedFromResId()).isEqualTo(R.drawable.an_image); }
@Test public void testImageViewSrcIsSetFromMipmap() throws Exception { View mediaView = inflate(layout.main); ImageView imageView = mediaView.findViewById(R.id.mipmapImage); BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable(); assertThat(shadowOf(drawable.getBitmap()).getCreatedFromResId()) .isEqualTo(R.mipmap.robolectric); }
@Test public void decodeResource_shouldSetDescriptionAndCreatedFrom() { Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.an_image); ShadowBitmap shadowBitmap = shadowOf(bitmap); assertEquals("Bitmap for resource:org.robolectric:drawable/an_image", shadowBitmap.getDescription()); assertEquals(R.drawable.an_image, shadowBitmap.getCreatedFromResId()); assertEquals(64, bitmap.getWidth()); assertEquals(53, bitmap.getHeight()); }
ShadowBitmap shadowBitmap = (ShadowBitmap) shadowOf(bitmap); int loadedFromResourceId = shadowBitmap.getCreatedFromResId();
/** * Non-Android accessor that tells you the resource id that this {@code BitmapDrawable} was loaded from. This lets * your tests assert that the bitmap is correct without having to actually load the bitmap. * * @return resource id from which this {@code BitmapDrawable} was loaded * @deprecated use ShadowBitmap#getCreatedFromResId() instead. */ @Override public int getCreatedFromResId() { return shadowOf(realBitmapDrawable.getBitmap()).getCreatedFromResId(); }
/** * @return the resource ID of this view's background * @deprecated Use FEST assertions instead. */ @Deprecated public int getBackgroundResourceId() { Drawable drawable = realView.getBackground(); return drawable instanceof BitmapDrawable ? shadowOf(((BitmapDrawable) drawable).getBitmap()).getCreatedFromResId() : -1; }
/** * Non-Android accessor. * * @return the resource ID of this view's background * @deprecated Use FEST assertions instead. */ public int getBackgroundResourceId() { Drawable drawable = realView.getBackground(); return drawable instanceof BitmapDrawable ? shadowOf(((BitmapDrawable) drawable).getBitmap()).getCreatedFromResId() : -1; }
/** * Non-Android accessor that tells you the resource id that this {@code BitmapDrawable} was loaded from. This lets * your tests assert that the bitmap is correct without having to actually load the bitmap. * * @return resource id from which this {@code BitmapDrawable} was loaded * @deprecated use ShadowBitmap#getCreatedFromResId() instead. */ @Deprecated @Override public int getCreatedFromResId() { return shadowOf(realBitmapDrawable.getBitmap()).getCreatedFromResId(); }
/** * Returns the resource id that this {@code BitmapDrawable} was loaded from. This lets * your tests assert that the bitmap is correct without having to actually load the bitmap. * * @return resource id from which this {@code BitmapDrawable} was loaded * @deprecated use ShadowBitmap#getCreatedFromResId() instead. */ @Deprecated @Override public int getCreatedFromResId() { return shadowOf(realBitmapDrawable.getBitmap()).getCreatedFromResId(); }
/** * Non-Android accessor. * * @return the resource ID of this view's background * @deprecated Use FEST assertions instead. */ @Deprecated public int getBackgroundResourceId() { Drawable drawable = realView.getBackground(); return drawable instanceof BitmapDrawable ? shadowOf(((BitmapDrawable) drawable).getBitmap()).getCreatedFromResId() : -1; }
/** * Returns the resource id that this {@code BitmapDrawable} was loaded from. This lets * your tests assert that the bitmap is correct without having to actually load the bitmap. * * @return resource id from which this {@code BitmapDrawable} was loaded * @deprecated use ShadowBitmap#getCreatedFromResId() instead. */ @Deprecated @Override public int getCreatedFromResId() { ShadowBitmap shadowBitmap = Shadow.extract(realBitmapDrawable.getBitmap()); return shadowBitmap.getCreatedFromResId(); }
public class CustomImageViewAssert extends ImageViewAssert { protected CustomImageViewAssert(ImageView actual) { super(actual); } public CustomImageViewAssert hasDrawableWithId(int resId) { boolean hasDrawable = hasDrawableResourceId(actual.getDrawable(), resId); String errorMessage = String.format("Expected ImageView to have drawable with id <%d>", resId); Assertions.assertThat(hasDrawable).overridingErrorMessage(errorMessage).isTrue(); return this; } private static boolean hasDrawableResourceId(Drawable drawable, int expectedResId) { BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; Bitmap bitmap = bitmapDrawable.getBitmap(); ShadowBitmap shadowBitmap = (ShadowBitmap) shadowOf(bitmap); int loadedFromResourceId = shadowBitmap.getCreatedFromResId(); return expectedResId == loadedFromResourceId; } }