Last active
December 27, 2015 14:29
-
-
Save sys1yagi/7340815 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.mydns.sys1yagi.android.mockitosample; | |
import java.io.IOException; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.os.AsyncTask; | |
import android.view.View; | |
import android.widget.ImageView; | |
public class ImageLoader { | |
@SuppressWarnings("rawtypes") | |
public void loadImage(final ImageView imageView, String imageUrl) { | |
Object maybeTask = imageView.getTag(); | |
if (maybeTask != null) { | |
if (maybeTask instanceof AsyncTask) { | |
((AsyncTask) maybeTask).cancel(true); | |
} | |
} | |
AsyncTask<String, Void, Bitmap> task = new AsyncTask<String, Void, Bitmap>() { | |
@Override | |
protected Bitmap doInBackground(String... params) { | |
try { | |
return BitmapFactory.decodeStream(new URL(params[0]) | |
.openStream()); | |
} catch (MalformedURLException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
protected void onPostExecute(Bitmap result) { | |
imageView.setTag(null); | |
if (imageView != null && !isCancelled()) { | |
imageView.setVisibility(View.VISIBLE); | |
imageView.setImageBitmap(result); | |
} | |
}; | |
}.execute(imageUrl); | |
imageView.setTag(task); | |
} | |
@SuppressWarnings("rawtypes") | |
public void cancel(ImageView imageView) { | |
Object maybeTask = imageView.getTag(); | |
if (maybeTask != null) { | |
if (maybeTask instanceof AsyncTask) { | |
((AsyncTask) maybeTask).cancel(true); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.mydns.sys1yagi.android.mockitosample; | |
import android.os.Bundle; | |
import android.support.v4.app.FragmentActivity; | |
import android.widget.ImageView; | |
public class PreviewActivity extends FragmentActivity { | |
public static final String EXTRA_URL = "url"; | |
private ImageView mImageView; | |
private ImageLoader mImageLoader = new ImageLoader(); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_preview); | |
mImageView = (ImageView)findViewById(R.id.image); | |
String url = getIntent().getStringExtra(EXTRA_URL); | |
mImageLoader.loadImage(mImageView, url); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
mImageLoader.cancel(mImageView); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.mydns.sys1yagi.android.mockitosample; | |
import static org.mockito.Matchers.any; | |
import static org.mockito.Matchers.anyString; | |
import static org.mockito.Matchers.eq; | |
import static org.mockito.Mockito.doNothing; | |
import static org.mockito.Mockito.verify; | |
import org.mockito.InjectMocks; | |
import org.mockito.MockitoAnnotations; | |
import org.mockito.Spy; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.test.ActivityUnitTestCase; | |
import android.widget.ImageView; | |
public class PreviewActivityTest extends ActivityUnitTestCase<PreviewActivity> { | |
@Spy | |
private ImageLoader mImageLoader = new ImageLoader(); | |
@InjectMocks | |
private PreviewActivity mPreviewActivity; | |
public PreviewActivityTest() { | |
super(PreviewActivity.class); | |
} | |
@Override | |
protected void setUp() throws Exception { | |
super.setUp(); | |
System.setProperty("dexmaker.dexcache", getInstrumentation() | |
.getTargetContext().getCacheDir().getPath()); | |
} | |
@Override | |
protected void setActivity(Activity testActivity) { | |
super.setActivity(testActivity); | |
mPreviewActivity = (PreviewActivity)testActivity; | |
MockitoAnnotations.initMocks(this); | |
} | |
public void testActivity() { | |
Intent intent = new Intent(); | |
String testString = "test"; | |
intent.putExtra(PreviewActivity.EXTRA_URL, testString); | |
startActivity(intent, null, null); | |
doNothing().when(mImageLoader).loadImage((ImageView) any(), anyString()); | |
getInstrumentation().callActivityOnResume(getActivity()); | |
verify(mImageLoader).loadImage((ImageView) any(), eq(testString)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment