package com.xtremelabs.robolectric.shadows;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import com.xtremelabs.robolectric.res.ViewLoader;
import com.xtremelabs.robolectric.util.Implementation;
import com.xtremelabs.robolectric.util.Implements;

@SuppressWarnings({"UnusedDeclaration"})
@Implements(ImageView.class)
public class ShadowImageView extends ShadowView {
    private Drawable imageDrawable;
    private int alpha;
    private int resourceId;
    private Bitmap imageBitmap;
    private ImageView.ScaleType scaleType;

    @Implementation
    public void setImageBitmap(Bitmap imageBitmap) {
        this.imageBitmap = imageBitmap;
    }

    public Bitmap getImageBitmap() {
        return imageBitmap;
    }

    @Implementation
    public void setImageDrawable(Drawable drawable) {
        this.imageDrawable = drawable;
    }

    @Implementation
    public void setImageResource(int resId) {
        this.resourceId = resId;
    }

    @Implementation
    public void setAlpha(int alpha) {
        this.alpha = alpha;
    }

    @Implementation
    public ImageView.ScaleType getScaleType() {
        return scaleType;
    }

    @Implementation
    public void setScaleType(ImageView.ScaleType scaleType) {
        this.scaleType = scaleType;
    }

    public Drawable getImageDrawable() {
        return imageDrawable;
    }

    public int getAlpha() {
        return alpha;
    }

    public int getResourceId() {
        return resourceId;
    }

    @Override public void applyViewNodeAttributes(ViewLoader.ViewNode viewNode) {
        super.applyViewNodeAttributes(viewNode);
        applyImageAttribute(viewNode);
    }

    private void applyImageAttribute(ViewLoader.ViewNode viewNode) {
        String source = viewNode.getAttributeValue("android:src");
        if (source != null) {
            if (source.startsWith("@drawable/")) {
                Integer resId = viewNode.getResourceId(source);
                setImageResource(resId);
            }
        }
    }
}

@@ -38,16 +38,48 @@
 adows;%0A%0A
+import android.content.Context;%0A
 import a
@@ -164,74 +164,58 @@
 oid.
-widget.ImageView;%0Aimport com.xtremelabs.robolectric.res.ViewLoader
+util.AttributeSet;%0Aimport android.widget.ImageView
 ;%0Aim
@@ -602,16 +602,196 @@
 eType;%0A%0A
+    @Override public void __constructor__(Context context, AttributeSet attributeSet) %7B%0A        super.__constructor__(context, attributeSet);%0A        applyImageAttribute();%0A    %7D%0A%0A
     @Imp
@@ -1725,185 +1725,8 @@
  %7D%0A%0A
-    @Override public void applyViewNodeAttributes(ViewLoader.ViewNode viewNode) %7B%0A        super.applyViewNodeAttributes(viewNode);%0A        applyImageAttribute(viewNode);%0A    %7D%0A%0A
     
@@ -1762,36 +1762,8 @@
 ute(
-ViewLoader.ViewNode viewNode
 ) %7B%0A
@@ -1786,24 +1786,28 @@
 ource = 
-viewNode
+attributeSet
 .getAttr
@@ -1829,9 +1829,12 @@
 roid
-:
+%22, %22
 src%22
@@ -1937,44 +1937,24 @@
     
-Integer resId = viewNode.
+setIma
 ge
-t
 Resource
 Id(s
@@ -1953,58 +1953,68 @@
 urce
-Id(source);%0A                setImageResource(resId
+(attributeSet.getAttributeResourceValue(%22android%22, %22src%22, 0)
 );%0A 

apply image attribute in constructor of shadow image view