Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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