Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
18084 manas 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 android.graphics.Bitmap;
20
import android.widget.ImageView;
21
import com.android.volley.Request;
22
import com.android.volley.RequestQueue;
23
import org.junit.Assert;
24
import org.junit.Before;
25
import org.junit.Test;
26
import org.junit.runner.RunWith;
27
import org.robolectric.RobolectricTestRunner;
28
 
29
import static org.junit.Assert.assertNotNull;
30
import static org.mockito.Mockito.*;
31
 
32
@RunWith(RobolectricTestRunner.class)
33
public class ImageLoaderTest {
34
    private RequestQueue mRequestQueue;
35
    private ImageLoader.ImageCache mImageCache;
36
    private ImageLoader mImageLoader;
37
 
38
    @Before
39
    public void setUp() {
40
        mRequestQueue = mock(RequestQueue.class);
41
        mImageCache = mock(ImageLoader.ImageCache.class);
42
        mImageLoader = new ImageLoader(mRequestQueue, mImageCache);
43
    }
44
 
45
    @Test
46
    public void isCachedChecksCache() throws Exception {
47
        when(mImageCache.getBitmap(anyString())).thenReturn(null);
48
        Assert.assertFalse(mImageLoader.isCached("http://foo", 0, 0));
49
    }
50
 
51
    @Test
52
    public void getWithCacheHit() throws Exception {
53
        Bitmap bitmap = Bitmap.createBitmap(1, 1, null);
54
        ImageLoader.ImageListener listener = mock(ImageLoader.ImageListener.class);
55
        when(mImageCache.getBitmap(anyString())).thenReturn(bitmap);
56
        ImageLoader.ImageContainer ic = mImageLoader.get("http://foo", listener);
57
        Assert.assertSame(bitmap, ic.getBitmap());
58
        verify(listener).onResponse(ic, true);
59
    }
60
 
61
    @Test
62
    public void getWithCacheMiss() throws Exception {
63
        when(mImageCache.getBitmap(anyString())).thenReturn(null);
64
        ImageLoader.ImageListener listener = mock(ImageLoader.ImageListener.class);
65
        // Ask for the image to be loaded.
66
        mImageLoader.get("http://foo", listener);
67
        // Second pass to test deduping logic.
68
        mImageLoader.get("http://foo", listener);
69
        // Response callback should be called both times.
70
        verify(listener, times(2)).onResponse(any(ImageLoader.ImageContainer.class), eq(true));
71
        // But request should be enqueued only once.
72
        verify(mRequestQueue, times(1)).add(any(Request.class));
73
    }
74
 
75
    @Test
76
    public void publicMethods() throws Exception {
77
        // Catch API breaking changes.
78
        ImageLoader.getImageListener(null, -1, -1);
79
        mImageLoader.setBatchedResponseDelay(1000);
80
 
81
        assertNotNull(ImageLoader.class.getConstructor(RequestQueue.class,
82
                ImageLoader.ImageCache.class));
83
 
84
        assertNotNull(ImageLoader.class.getMethod("getImageListener", ImageView.class,
85
                int.class, int.class));
86
        assertNotNull(ImageLoader.class.getMethod("isCached", String.class, int.class, int.class));
87
        assertNotNull(ImageLoader.class.getMethod("isCached", String.class, int.class, int.class,
88
                ImageView.ScaleType.class));
89
        assertNotNull(ImageLoader.class.getMethod("get", String.class,
90
                ImageLoader.ImageListener.class));
91
        assertNotNull(ImageLoader.class.getMethod("get", String.class,
92
                ImageLoader.ImageListener.class, int.class, int.class));
93
        assertNotNull(ImageLoader.class.getMethod("get", String.class,
94
                ImageLoader.ImageListener.class, int.class, int.class, ImageView.ScaleType.class));
95
        assertNotNull(ImageLoader.class.getMethod("setBatchedResponseDelay", int.class));
96
 
97
        assertNotNull(ImageLoader.ImageListener.class.getMethod("onResponse",
98
                ImageLoader.ImageContainer.class, boolean.class));
99
    }
100
}
101