1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.volley.toolbox; 18 19 import static org.junit.Assert.assertNotNull; 20 import static org.mockito.Mockito.any; 21 import static org.mockito.Mockito.anyString; 22 import static org.mockito.Mockito.eq; 23 import static org.mockito.Mockito.mock; 24 import static org.mockito.Mockito.times; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import android.graphics.Bitmap; 29 import android.widget.ImageView; 30 import com.android.volley.Request; 31 import com.android.volley.RequestQueue; 32 import org.junit.Assert; 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.mockito.Mockito; 37 import org.robolectric.RobolectricTestRunner; 38 39 @RunWith(RobolectricTestRunner.class) 40 public class ImageLoaderTest { 41 private RequestQueue mRequestQueue; 42 private ImageLoader.ImageCache mImageCache; 43 private ImageLoader mImageLoader; 44 45 @Before setUp()46 public void setUp() { 47 mRequestQueue = mock(RequestQueue.class); 48 mImageCache = mock(ImageLoader.ImageCache.class); 49 mImageLoader = new ImageLoader(mRequestQueue, mImageCache); 50 } 51 52 @Test isCachedChecksCache()53 public void isCachedChecksCache() throws Exception { 54 when(mImageCache.getBitmap(anyString())).thenReturn(null); 55 Assert.assertFalse(mImageLoader.isCached("http://foo", 0, 0)); 56 } 57 58 @Test getWithCacheHit()59 public void getWithCacheHit() throws Exception { 60 Bitmap bitmap = Bitmap.createBitmap(1, 1, null); 61 ImageLoader.ImageListener listener = mock(ImageLoader.ImageListener.class); 62 when(mImageCache.getBitmap(anyString())).thenReturn(bitmap); 63 ImageLoader.ImageContainer ic = mImageLoader.get("http://foo", listener); 64 Assert.assertSame(bitmap, ic.getBitmap()); 65 verify(listener).onResponse(ic, true); 66 } 67 68 @Test getWithCacheMiss()69 public void getWithCacheMiss() throws Exception { 70 when(mImageCache.getBitmap(anyString())).thenReturn(null); 71 ImageLoader.ImageListener listener = mock(ImageLoader.ImageListener.class); 72 // Ask for the image to be loaded. 73 mImageLoader.get("http://foo", listener); 74 // Second pass to test deduping logic. 75 mImageLoader.get("http://foo", listener); 76 // Response callback should be called both times. 77 verify(listener, times(2)).onResponse(any(ImageLoader.ImageContainer.class), eq(true)); 78 // But request should be enqueued only once. 79 verify(mRequestQueue, times(1)).add(Mockito.<Request<?>>any()); 80 } 81 82 @Test publicMethods()83 public void publicMethods() throws Exception { 84 // Catch API breaking changes. 85 ImageLoader.getImageListener(null, -1, -1); 86 mImageLoader.setBatchedResponseDelay(1000); 87 88 assertNotNull( 89 ImageLoader.class.getConstructor(RequestQueue.class, ImageLoader.ImageCache.class)); 90 91 assertNotNull( 92 ImageLoader.class.getMethod( 93 "getImageListener", ImageView.class, int.class, int.class)); 94 assertNotNull(ImageLoader.class.getMethod("isCached", String.class, int.class, int.class)); 95 assertNotNull( 96 ImageLoader.class.getMethod( 97 "isCached", String.class, int.class, int.class, ImageView.ScaleType.class)); 98 assertNotNull( 99 ImageLoader.class.getMethod("get", String.class, ImageLoader.ImageListener.class)); 100 assertNotNull( 101 ImageLoader.class.getMethod( 102 "get", 103 String.class, 104 ImageLoader.ImageListener.class, 105 int.class, 106 int.class)); 107 assertNotNull( 108 ImageLoader.class.getMethod( 109 "get", 110 String.class, 111 ImageLoader.ImageListener.class, 112 int.class, 113 int.class, 114 ImageView.ScaleType.class)); 115 assertNotNull(ImageLoader.class.getMethod("setBatchedResponseDelay", int.class)); 116 117 assertNotNull( 118 ImageLoader.ImageListener.class.getMethod( 119 "onResponse", ImageLoader.ImageContainer.class, boolean.class)); 120 } 121 } 122