Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
21478 rajender 1
/*
2
 * Copyright (C) 2011 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.graphics.Bitmap.Config;
21
import android.widget.ImageView;
22
import android.widget.ImageView.ScaleType;
23
 
24
import com.android.volley.NetworkResponse;
25
import com.android.volley.Response;
26
 
27
import org.junit.Test;
28
import org.junit.runner.RunWith;
29
import org.robolectric.RobolectricTestRunner;
30
import org.robolectric.shadows.ShadowBitmapFactory;
31
 
32
import java.io.ByteArrayOutputStream;
33
import java.io.IOException;
34
import java.io.InputStream;
35
 
36
import static org.junit.Assert.assertEquals;
37
import static org.junit.Assert.assertNotNull;
38
import static org.junit.Assert.assertTrue;
39
 
40
@RunWith(RobolectricTestRunner.class)
41
public class ImageRequestTest {
42
 
43
    @Test public void parseNetworkResponse_resizing() throws Exception {
44
        // This is a horrible hack but Robolectric doesn't have a way to provide
45
        // width and height hints for decodeByteArray. It works because the byte array
46
        // "file:fake" is ASCII encodable and thus the name in Robolectric's fake
47
        // bitmap creator survives as-is, and provideWidthAndHeightHints puts
48
        // "file:" + name in its lookaside map. I write all this because it will
49
        // probably break mysteriously at some point and I feel terrible about your
50
        // having to debug it.
51
        byte[] jpegBytes = "file:fake".getBytes();
52
        ShadowBitmapFactory.provideWidthAndHeightHints("fake", 1024, 500);
53
        NetworkResponse jpeg = new NetworkResponse(jpegBytes);
54
 
55
        // Scale the image uniformly (maintain the image's aspect ratio) so that
56
        // both dimensions (width and height) of the image will be equal to or
57
        // less than the corresponding dimension of the view.
58
        ScaleType scalteType = ScaleType.CENTER_INSIDE;
59
 
60
        // Exact sizes
61
        verifyResize(jpeg, 512, 250, scalteType, 512, 250); // exactly half
62
        verifyResize(jpeg, 511, 249, scalteType, 509, 249); // just under half
63
        verifyResize(jpeg, 1080, 500, scalteType, 1024, 500); // larger
64
        verifyResize(jpeg, 500, 500, scalteType, 500, 244); // keep same ratio
65
 
66
        // Specify only width, preserve aspect ratio
67
        verifyResize(jpeg, 512, 0, scalteType, 512, 250);
68
        verifyResize(jpeg, 800, 0, scalteType, 800, 390);
69
        verifyResize(jpeg, 1024, 0, scalteType, 1024, 500);
70
 
71
        // Specify only height, preserve aspect ratio
72
        verifyResize(jpeg, 0, 250, scalteType, 512, 250);
73
        verifyResize(jpeg, 0, 391, scalteType, 800, 391);
74
        verifyResize(jpeg, 0, 500, scalteType, 1024, 500);
75
 
76
        // No resize
77
        verifyResize(jpeg, 0, 0, scalteType, 1024, 500);
78
 
79
 
80
        // Scale the image uniformly (maintain the image's aspect ratio) so that
81
        // both dimensions (width and height) of the image will be equal to or
82
        // larger than the corresponding dimension of the view.
83
        scalteType = ScaleType.CENTER_CROP;
84
 
85
        // Exact sizes
86
        verifyResize(jpeg, 512, 250, scalteType, 512, 250);
87
        verifyResize(jpeg, 511, 249, scalteType, 511, 249);
88
        verifyResize(jpeg, 1080, 500, scalteType, 1024, 500);
89
        verifyResize(jpeg, 500, 500, scalteType, 1024, 500);
90
 
91
        // Specify only width
92
        verifyResize(jpeg, 512, 0, scalteType, 512, 250);
93
        verifyResize(jpeg, 800, 0, scalteType, 800, 390);
94
        verifyResize(jpeg, 1024, 0, scalteType, 1024, 500);
95
 
96
        // Specify only height
97
        verifyResize(jpeg, 0, 250, scalteType, 512, 250);
98
        verifyResize(jpeg, 0, 391, scalteType, 800, 391);
99
        verifyResize(jpeg, 0, 500, scalteType, 1024, 500);
100
 
101
        // No resize
102
        verifyResize(jpeg, 0, 0, scalteType, 1024, 500);
103
 
104
 
105
        // Scale in X and Y independently, so that src matches dst exactly. This
106
        // may change the aspect ratio of the src.
107
        scalteType = ScaleType.FIT_XY;
108
 
109
        // Exact sizes
110
        verifyResize(jpeg, 512, 250, scalteType, 512, 250);
111
        verifyResize(jpeg, 511, 249, scalteType, 511, 249);
112
        verifyResize(jpeg, 1080, 500, scalteType, 1024, 500);
113
        verifyResize(jpeg, 500, 500, scalteType, 500, 500);
114
 
115
        // Specify only width
116
        verifyResize(jpeg, 512, 0, scalteType, 512, 500);
117
        verifyResize(jpeg, 800, 0, scalteType, 800, 500);
118
        verifyResize(jpeg, 1024, 0, scalteType, 1024, 500);
119
 
120
        // Specify only height
121
        verifyResize(jpeg, 0, 250, scalteType, 1024, 250);
122
        verifyResize(jpeg, 0, 391, scalteType, 1024, 391);
123
        verifyResize(jpeg, 0, 500, scalteType, 1024, 500);
124
 
125
        // No resize
126
        verifyResize(jpeg, 0, 0, scalteType, 1024, 500);
127
    }
128
 
129
    private void verifyResize(NetworkResponse networkResponse, int maxWidth, int maxHeight,
130
                              ScaleType scaleType, int expectedWidth, int expectedHeight) {
131
        ImageRequest request = new ImageRequest("", null, maxWidth, maxHeight, scaleType,
132
                Config.RGB_565, null);
133
        Response<Bitmap> response = request.parseNetworkResponse(networkResponse);
134
        assertNotNull(response);
135
        assertTrue(response.isSuccess());
136
        Bitmap bitmap = response.result;
137
        assertNotNull(bitmap);
138
        assertEquals(expectedWidth, bitmap.getWidth());
139
        assertEquals(expectedHeight, bitmap.getHeight());
140
    }
141
 
142
    @Test public void findBestSampleSize() {
143
        // desired == actual == 1
144
        assertEquals(1, ImageRequest.findBestSampleSize(100, 150, 100, 150));
145
 
146
        // exactly half == 2
147
        assertEquals(2, ImageRequest.findBestSampleSize(280, 160, 140, 80));
148
 
149
        // just over half == 1
150
        assertEquals(1, ImageRequest.findBestSampleSize(1000, 800, 501, 401));
151
 
152
        // just under 1/4 == 4
153
        assertEquals(4, ImageRequest.findBestSampleSize(100, 200, 24, 50));
154
    }
155
 
156
    private static byte[] readInputStream(InputStream in) throws IOException {
157
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
158
        byte[] buffer = new byte[1024];
159
        int count;
160
        while ((count = in.read(buffer)) != -1) {
161
            bytes.write(buffer, 0, count);
162
        }
163
        in.close();
164
        return bytes.toByteArray();
165
    }
166
 
167
    @Test
168
    public void publicMethods() throws Exception {
169
        // Catch-all test to find API-breaking changes.
170
        assertNotNull(ImageRequest.class.getConstructor(String.class, Response.Listener.class,
171
                int.class, int.class, Bitmap.Config.class, Response.ErrorListener.class));
172
        assertNotNull(ImageRequest.class.getConstructor(String.class, Response.Listener.class,
173
                int.class, int.class, ImageView.ScaleType.class, Bitmap.Config.class,
174
                Response.ErrorListener.class));
175
    }
176
}