aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/android/volley/toolbox/ImageLoaderTest.java
diff options
context:
space:
mode:
authorFicus Kirkpatrick <ficus@android.com>2015-02-18 18:14:45 -0800
committerFicus Kirkpatrick <ficus@android.com>2015-02-19 19:11:34 -0800
commit6c9de79451d6f9410c006e4b17d3d07fae12b273 (patch)
treef16c5c7815bde80e4974a997ad9989a49dc7343b /src/test/java/com/android/volley/toolbox/ImageLoaderTest.java
parentbc57e129fb55278b03503e1e4f61f3b51853f974 (diff)
downloadvolley-6c9de79451d6f9410c006e4b17d3d07fae12b273.tar.gz
A little work on unit testing.
Add emma and mockito to the Maven build, and test a couple random things.
Diffstat (limited to 'src/test/java/com/android/volley/toolbox/ImageLoaderTest.java')
-rw-r--r--src/test/java/com/android/volley/toolbox/ImageLoaderTest.java80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/test/java/com/android/volley/toolbox/ImageLoaderTest.java b/src/test/java/com/android/volley/toolbox/ImageLoaderTest.java
new file mode 100644
index 0000000..47c55a6
--- /dev/null
+++ b/src/test/java/com/android/volley/toolbox/ImageLoaderTest.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.volley.toolbox;
+
+import android.graphics.Bitmap;
+import com.android.volley.Request;
+import com.android.volley.RequestQueue;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.RobolectricTestRunner;
+
+import static org.mockito.Mockito.*;
+
+@RunWith(RobolectricTestRunner.class)
+public class ImageLoaderTest {
+ private RequestQueue mRequestQueue;
+ private ImageLoader.ImageCache mImageCache;
+ private ImageLoader mImageLoader;
+
+ @Before
+ public void setUp() {
+ mRequestQueue = mock(RequestQueue.class);
+ mImageCache = mock(ImageLoader.ImageCache.class);
+ mImageLoader = new ImageLoader(mRequestQueue, mImageCache);
+ }
+
+ @Test
+ public void isCachedChecksCache() throws Exception {
+ when(mImageCache.getBitmap(anyString())).thenReturn(null);
+ Assert.assertFalse(mImageLoader.isCached("http://foo", 0, 0));
+ }
+
+ @Test
+ public void getWithCacheHit() throws Exception {
+ Bitmap bitmap = Bitmap.createBitmap(1, 1, null);
+ ImageLoader.ImageListener listener = mock(ImageLoader.ImageListener.class);
+ when(mImageCache.getBitmap(anyString())).thenReturn(bitmap);
+ ImageLoader.ImageContainer ic = mImageLoader.get("http://foo", listener);
+ Assert.assertSame(bitmap, ic.getBitmap());
+ verify(listener).onResponse(ic, true);
+ }
+
+ @Test
+ public void getWithCacheMiss() throws Exception {
+ when(mImageCache.getBitmap(anyString())).thenReturn(null);
+ ImageLoader.ImageListener listener = mock(ImageLoader.ImageListener.class);
+ // Ask for the image to be loaded.
+ mImageLoader.get("http://foo", listener);
+ // Second pass to test deduping logic.
+ mImageLoader.get("http://foo", listener);
+ // Response callback should be called both times.
+ verify(listener, times(2)).onResponse(any(ImageLoader.ImageContainer.class), eq(true));
+ // But request should be enqueued only once.
+ verify(mRequestQueue, times(1)).add(any(Request.class));
+ }
+
+ @Test
+ public void publicMethods() throws Exception {
+ // Catch API breaking changes.
+ ImageLoader.getImageListener(null, -1, -1);
+ mImageLoader.setBatchedResponseDelay(1000);
+ }
+}
+