@Override public void applyAttributes() { super.applyAttributes(); applyImageAttribute(); }
@Implementation public void setImageResource(int resId) { this.resourceId = resId; setImageDrawable(buildDrawable(resId)); }
@Override public boolean matchesSafely(ImageView actual) { if (actual == null) { return false; } actualResourceId = shadowOf(actual).getResourceId(); return actualResourceId == expectedResourceId; }
/** * Build drawable, either LayerDrawable or BitmapDrawable. * * @param resourceId Resource id * @return Drawable */ protected Drawable buildDrawable(int resourceId) { if (isDrawableXml(resourceId)) { ResourceLoader resourceLoader = shadowOf(Robolectric.application).getResourceLoader(); int[] resourceIds = resourceLoader.getDrawableIds(resourceId); Drawable[] drawables = new Drawable[resourceIds.length]; for (int i = 0; i < resourceIds.length; i++) { drawables[i] = buildDrawable(resourceIds[i]); } if (resourceLoader.isAnimatableXml(resourceId)) { AnimationDrawable animationDrawable = new AnimationDrawable(); for (Drawable drawable : drawables) { animationDrawable.addFrame(drawable, -1); } return animationDrawable; } else { LayerDrawable layerDrawable = new LayerDrawable(drawables); shadowOf(layerDrawable).setLoadedFromResourceId(resourceId); return layerDrawable; } } else { return new BitmapDrawable(BitmapFactory.decodeResource( getResources(), resourceId)); } }
@Implementation public void setImageBitmap(Bitmap imageBitmap) { setImageDrawable(new BitmapDrawable(imageBitmap)); this.imageBitmap = imageBitmap; }
private void applyImageAttribute() { String source = attributeSet.getAttributeValue("android", "src"); if (source != null) { if (source.startsWith("@drawable/")) { setImageResource(attributeSet.getAttributeResourceValue( "android", "src", 0)); } } }
@Override public boolean matchesSafely(T actualImageView) { if (actualImageView == null) { return false; } ResourceLoader resourceLoader = ResourceLoader.getFrom(actualImageView.getContext()); int actualResourceId = shadowOf(actualImageView).getResourceId(); String actualName = nameOrUnset(resourceLoader, actualResourceId); String expectedName = nameOrUnset(resourceLoader, expectedResourceId); message = "[" + actualResourceId + " (" + actualName + ")] to equal [" + expectedResourceId + " (" + expectedName + ")]"; return actualResourceId == expectedResourceId; }