|
| 1 | +package com.bumptech.glide.integration.cronet; |
| 2 | + |
| 3 | +import static com.google.common.truth.Truth.assertThat; |
| 4 | +import static org.mockito.ArgumentMatchers.any; |
| 5 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 6 | +import static org.mockito.ArgumentMatchers.anyMap; |
| 7 | +import static org.mockito.ArgumentMatchers.anyString; |
| 8 | +import static org.mockito.ArgumentMatchers.isA; |
| 9 | +import static org.mockito.Matchers.eq; |
| 10 | +import static org.mockito.Matchers.isNull; |
| 11 | +import static org.mockito.Mockito.mock; |
| 12 | +import static org.mockito.Mockito.never; |
| 13 | +import static org.mockito.Mockito.timeout; |
| 14 | +import static org.mockito.Mockito.times; |
| 15 | +import static org.mockito.Mockito.verify; |
| 16 | +import static org.mockito.Mockito.when; |
| 17 | + |
| 18 | +import androidx.annotation.NonNull; |
| 19 | +import com.bumptech.glide.Priority; |
| 20 | +import com.bumptech.glide.load.HttpException; |
| 21 | +import com.bumptech.glide.load.data.DataFetcher.DataCallback; |
| 22 | +import com.bumptech.glide.load.model.GlideUrl; |
| 23 | +import com.bumptech.glide.load.model.LazyHeaders; |
| 24 | +import com.bumptech.glide.load.model.LazyHeaders.Builder; |
| 25 | +import com.google.common.collect.ImmutableList; |
| 26 | +import com.google.common.util.concurrent.MoreExecutors; |
| 27 | +import java.net.HttpURLConnection; |
| 28 | +import java.nio.ByteBuffer; |
| 29 | +import java.util.AbstractMap.SimpleImmutableEntry; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.Map.Entry; |
| 32 | +import java.util.concurrent.Executor; |
| 33 | +import org.chromium.net.CronetEngine; |
| 34 | +import org.chromium.net.CronetException; |
| 35 | +import org.chromium.net.UrlRequest; |
| 36 | +import org.chromium.net.UrlRequest.Callback; |
| 37 | +import org.chromium.net.UrlResponseInfo; |
| 38 | +import org.chromium.net.impl.UrlResponseInfoImpl; |
| 39 | +import org.junit.Before; |
| 40 | +import org.junit.Test; |
| 41 | +import org.junit.runner.RunWith; |
| 42 | +import org.mockito.ArgumentCaptor; |
| 43 | +import org.mockito.Matchers; |
| 44 | +import org.mockito.Mock; |
| 45 | +import org.mockito.MockitoAnnotations; |
| 46 | +import org.mockito.invocation.InvocationOnMock; |
| 47 | +import org.mockito.stubbing.Answer; |
| 48 | +import org.robolectric.RobolectricTestRunner; |
| 49 | + |
| 50 | +/** Tests for {@link ChromiumUrlFetcher}. */ |
| 51 | +@RunWith(RobolectricTestRunner.class) |
| 52 | +public class ChromiumUrlFetcherTest { |
| 53 | + @Mock private DataCallback<ByteBuffer> callback; |
| 54 | + @Mock private CronetEngine cronetEngine; |
| 55 | + @Mock private UrlRequest request; |
| 56 | + @Mock private UrlRequest.Builder mockUrlRequestBuilder; |
| 57 | + @Mock private ByteBufferParser<ByteBuffer> parser; |
| 58 | + @Mock private CronetRequestFactory cronetRequestFactory; |
| 59 | + |
| 60 | + private UrlRequest.Builder builder; |
| 61 | + private GlideUrl glideUrl; |
| 62 | + private ChromiumUrlFetcher<ByteBuffer> fetcher; |
| 63 | + private ChromiumRequestSerializer serializer; |
| 64 | + private ArgumentCaptor<UrlRequest.Callback> urlRequestListenerCaptor; |
| 65 | + |
| 66 | + @Before |
| 67 | + public void setUp() { |
| 68 | + MockitoAnnotations.initMocks(this); |
| 69 | + when(parser.getDataClass()).thenReturn(ByteBuffer.class); |
| 70 | + when(parser.parse(any(ByteBuffer.class))) |
| 71 | + .thenAnswer( |
| 72 | + new Answer<ByteBuffer>() { |
| 73 | + @Override |
| 74 | + public ByteBuffer answer(InvocationOnMock invocation) throws Throwable { |
| 75 | + return (ByteBuffer) invocation.getArguments()[0]; |
| 76 | + } |
| 77 | + }); |
| 78 | + when(cronetEngine.newUrlRequestBuilder( |
| 79 | + anyString(), any(UrlRequest.Callback.class), any(Executor.class))) |
| 80 | + .thenReturn(mockUrlRequestBuilder); |
| 81 | + when(mockUrlRequestBuilder.build()).thenReturn(request); |
| 82 | + |
| 83 | + glideUrl = new GlideUrl("http://www.google.com"); |
| 84 | + |
| 85 | + urlRequestListenerCaptor = ArgumentCaptor.forClass(UrlRequest.Callback.class); |
| 86 | + serializer = new ChromiumRequestSerializer(cronetRequestFactory, null /*dataLogger*/); |
| 87 | + fetcher = new ChromiumUrlFetcher<>(serializer, parser, glideUrl); |
| 88 | + builder = |
| 89 | + cronetEngine.newUrlRequestBuilder( |
| 90 | + glideUrl.toStringUrl(), |
| 91 | + mock(UrlRequest.Callback.class), |
| 92 | + MoreExecutors.directExecutor()); |
| 93 | + when(cronetRequestFactory.newRequest( |
| 94 | + anyString(), anyInt(), anyHeaders(), urlRequestListenerCaptor.capture())) |
| 95 | + .thenReturn(builder); |
| 96 | + when(builder.build()).thenReturn(request); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testLoadData_createsAndStartsRequest() { |
| 101 | + when(cronetRequestFactory.newRequest( |
| 102 | + eq(glideUrl.toStringUrl()), |
| 103 | + eq(UrlRequest.Builder.REQUEST_PRIORITY_LOWEST), |
| 104 | + anyHeaders(), |
| 105 | + any(UrlRequest.Callback.class))) |
| 106 | + .thenReturn(builder); |
| 107 | + |
| 108 | + fetcher.loadData(Priority.LOW, callback); |
| 109 | + |
| 110 | + verify(request).start(); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testLoadData_providesHeadersFromGlideUrl() { |
| 115 | + LazyHeaders.Builder headersBuilder = new Builder(); |
| 116 | + headersBuilder.addHeader("key", "value"); |
| 117 | + LazyHeaders headers = headersBuilder.build(); |
| 118 | + |
| 119 | + glideUrl = new GlideUrl("http://www.google.com", headers); |
| 120 | + fetcher = new ChromiumUrlFetcher<>(serializer, parser, glideUrl); |
| 121 | + fetcher.loadData(Priority.LOW, callback); |
| 122 | + |
| 123 | + verify(cronetRequestFactory) |
| 124 | + .newRequest( |
| 125 | + Matchers.eq(glideUrl.toStringUrl()), |
| 126 | + anyInt(), |
| 127 | + Matchers.eq(headers.getHeaders()), |
| 128 | + any(UrlRequest.Callback.class)); |
| 129 | + |
| 130 | + verify(request).start(); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void testLoadData_withInProgressRequest_doesNotStartNewRequest() { |
| 135 | + ChromiumUrlFetcher<ByteBuffer> firstFetcher = |
| 136 | + new ChromiumUrlFetcher<>(serializer, parser, glideUrl); |
| 137 | + ChromiumUrlFetcher<ByteBuffer> secondFetcher = |
| 138 | + new ChromiumUrlFetcher<>(serializer, parser, glideUrl); |
| 139 | + |
| 140 | + firstFetcher.loadData(Priority.LOW, callback); |
| 141 | + secondFetcher.loadData(Priority.HIGH, callback); |
| 142 | + |
| 143 | + verify(cronetRequestFactory, times(1)) |
| 144 | + .newRequest( |
| 145 | + Matchers.eq(glideUrl.toStringUrl()), |
| 146 | + anyInt(), |
| 147 | + anyMap(), |
| 148 | + any(UrlRequest.Callback.class)); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void testLoadData_withInProgressRequest_isNotifiedWhenRequestCompletes() throws Exception { |
| 153 | + ChromiumUrlFetcher<ByteBuffer> firstFetcher = |
| 154 | + new ChromiumUrlFetcher<>(serializer, parser, glideUrl); |
| 155 | + ChromiumUrlFetcher<ByteBuffer> secondFetcher = |
| 156 | + new ChromiumUrlFetcher<>(serializer, parser, glideUrl); |
| 157 | + |
| 158 | + DataCallback<ByteBuffer> firstCb = mock(DataCallback.class); |
| 159 | + DataCallback<ByteBuffer> secondCb = mock(DataCallback.class); |
| 160 | + firstFetcher.loadData(Priority.LOW, firstCb); |
| 161 | + secondFetcher.loadData(Priority.HIGH, secondCb); |
| 162 | + |
| 163 | + succeed(getInfo(10, 200), urlRequestListenerCaptor.getValue(), ByteBuffer.allocateDirect(10)); |
| 164 | + |
| 165 | + verify(firstCb, timeout(1000)).onDataReady(isA(ByteBuffer.class)); |
| 166 | + verify(secondCb, timeout(1000)).onDataReady(isA(ByteBuffer.class)); |
| 167 | + } |
| 168 | + |
| 169 | + @NonNull |
| 170 | + private UrlResponseInfo getInfo(int contentLength, int statusCode) { |
| 171 | + return new UrlResponseInfoImpl( |
| 172 | + ImmutableList.of(glideUrl.toStringUrl()), |
| 173 | + statusCode, |
| 174 | + "OK", |
| 175 | + ImmutableList.<Entry<String, String>>of( |
| 176 | + new SimpleImmutableEntry<>("Content-Length", Integer.toString(contentLength))), |
| 177 | + false, |
| 178 | + "", |
| 179 | + ""); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + public void testCancel_withMultipleInProgressRequests_doesNotCancelChromiumRequest() { |
| 184 | + ChromiumUrlFetcher<ByteBuffer> firstFetcher = |
| 185 | + new ChromiumUrlFetcher<>(serializer, parser, glideUrl); |
| 186 | + ChromiumUrlFetcher<ByteBuffer> secondFetcher = |
| 187 | + new ChromiumUrlFetcher<>(serializer, parser, glideUrl); |
| 188 | + |
| 189 | + firstFetcher.loadData(Priority.LOW, callback); |
| 190 | + secondFetcher.loadData(Priority.HIGH, callback); |
| 191 | + |
| 192 | + firstFetcher.cancel(); |
| 193 | + |
| 194 | + verify(request, never()).cancel(); |
| 195 | + } |
| 196 | + |
| 197 | + @Test |
| 198 | + public void testCancel_afterCancellingAllInProgressRequests_cancelsChromiumRequest() { |
| 199 | + ChromiumUrlFetcher<ByteBuffer> firstFetcher = |
| 200 | + new ChromiumUrlFetcher<>(serializer, parser, glideUrl); |
| 201 | + ChromiumUrlFetcher<ByteBuffer> secondFetcher = |
| 202 | + new ChromiumUrlFetcher<>(serializer, parser, glideUrl); |
| 203 | + |
| 204 | + firstFetcher.loadData(Priority.LOW, callback); |
| 205 | + secondFetcher.loadData(Priority.HIGH, callback); |
| 206 | + |
| 207 | + firstFetcher.cancel(); |
| 208 | + secondFetcher.cancel(); |
| 209 | + |
| 210 | + verify(request).cancel(); |
| 211 | + } |
| 212 | + |
| 213 | + @Test |
| 214 | + public void testCancel_withNoStartedRequest_doesNothing() { |
| 215 | + fetcher.cancel(); |
| 216 | + } |
| 217 | + |
| 218 | + @Test |
| 219 | + public void testCancel_withStartedRequest_cancelsRequest() { |
| 220 | + fetcher.loadData(Priority.LOW, callback); |
| 221 | + |
| 222 | + fetcher.cancel(); |
| 223 | + |
| 224 | + verify(request).cancel(); |
| 225 | + } |
| 226 | + |
| 227 | + @Test |
| 228 | + public void testRequestComplete_withNonNullException_callsCallbackWithException() { |
| 229 | + CronetException expected = new CronetException("test", /*cause=*/ null) {}; |
| 230 | + fetcher.loadData(Priority.LOW, callback); |
| 231 | + urlRequestListenerCaptor.getValue().onFailed(request, null, expected); |
| 232 | + |
| 233 | + verify(callback, timeout(1000)).onLoadFailed(eq(expected)); |
| 234 | + } |
| 235 | + |
| 236 | + @Test |
| 237 | + public void testRequestComplete_withNon200StatusCode_callsCallbackWithException() |
| 238 | + throws Exception { |
| 239 | + UrlResponseInfo info = getInfo(0, HttpURLConnection.HTTP_INTERNAL_ERROR); |
| 240 | + fetcher.loadData(Priority.LOW, callback); |
| 241 | + UrlRequest.Callback urlCallback = urlRequestListenerCaptor.getValue(); |
| 242 | + succeed(info, urlCallback, ByteBuffer.allocateDirect(0)); |
| 243 | + ArgumentCaptor<HttpException> captor = ArgumentCaptor.forClass(HttpException.class); |
| 244 | + verify(callback, timeout(1000)).onLoadFailed(captor.capture()); |
| 245 | + assertThat(captor.getValue()) |
| 246 | + .hasMessageThat() |
| 247 | + .isEqualTo("Http request failed with status code: 500"); |
| 248 | + } |
| 249 | + |
| 250 | + private void succeed(UrlResponseInfo info, Callback urlCallback, ByteBuffer byteBuffer) |
| 251 | + throws Exception { |
| 252 | + byteBuffer.position(byteBuffer.limit()); |
| 253 | + urlCallback.onResponseStarted(request, info); |
| 254 | + urlCallback.onReadCompleted(request, info, byteBuffer); |
| 255 | + urlCallback.onSucceeded(request, info); |
| 256 | + } |
| 257 | + |
| 258 | + @Test |
| 259 | + public void testRequestComplete_withUnauthorizedStatusCode_callsCallbackWithAuthError() |
| 260 | + throws Exception { |
| 261 | + UrlResponseInfo info = getInfo(0, HttpURLConnection.HTTP_FORBIDDEN); |
| 262 | + fetcher.loadData(Priority.LOW, callback); |
| 263 | + UrlRequest.Callback urlCallback = urlRequestListenerCaptor.getValue(); |
| 264 | + succeed(info, urlCallback, ByteBuffer.allocateDirect(0)); |
| 265 | + |
| 266 | + verifyAuthError(); |
| 267 | + } |
| 268 | + |
| 269 | + @Test |
| 270 | + public void testRequestComplete_whenCancelledAndUnauthorized_callsCallbackWithNullError() |
| 271 | + throws Exception { |
| 272 | + UrlResponseInfo info = getInfo(0, HttpURLConnection.HTTP_FORBIDDEN); |
| 273 | + fetcher.loadData(Priority.HIGH, callback); |
| 274 | + Callback urlCallback = urlRequestListenerCaptor.getValue(); |
| 275 | + urlCallback.onResponseStarted(request, info); |
| 276 | + urlCallback.onCanceled(request, info); |
| 277 | + |
| 278 | + verify(callback, timeout(1000)).onLoadFailed(isNull(Exception.class)); |
| 279 | + } |
| 280 | + |
| 281 | + private void verifyAuthError() { |
| 282 | + ArgumentCaptor<Exception> exceptionArgumentCaptor = ArgumentCaptor.forClass(Exception.class); |
| 283 | + verify(callback, timeout(1000)).onLoadFailed(exceptionArgumentCaptor.capture()); |
| 284 | + HttpException exception = (HttpException) exceptionArgumentCaptor.getValue(); |
| 285 | + assertThat(exception.getStatusCode()).isEqualTo(HttpURLConnection.HTTP_FORBIDDEN); |
| 286 | + } |
| 287 | + |
| 288 | + @Test |
| 289 | + public void testRequestComplete_with200AndCancelled_callsCallbackWithNullException() |
| 290 | + throws Exception { |
| 291 | + UrlResponseInfo info = getInfo(0, 200); |
| 292 | + fetcher.loadData(Priority.LOW, callback); |
| 293 | + Callback urlCallback = urlRequestListenerCaptor.getValue(); |
| 294 | + urlCallback.onResponseStarted(request, info); |
| 295 | + urlCallback.onCanceled(request, info); |
| 296 | + |
| 297 | + verify(callback, timeout(1000)).onLoadFailed(isNull(Exception.class)); |
| 298 | + } |
| 299 | + |
| 300 | + @Test |
| 301 | + public void testRequestComplete_with200NotCancelledMatchingLength_callsCallbackWithValidData() |
| 302 | + throws Exception { |
| 303 | + String data = "data"; |
| 304 | + ByteBuffer expected = ByteBuffer.wrap(data.getBytes()); |
| 305 | + ArgumentCaptor<ByteBuffer> captor = ArgumentCaptor.forClass(ByteBuffer.class); |
| 306 | + |
| 307 | + fetcher.loadData(Priority.LOW, callback); |
| 308 | + succeed( |
| 309 | + getInfo(expected.remaining(), 200), |
| 310 | + urlRequestListenerCaptor.getValue(), |
| 311 | + expected.duplicate()); |
| 312 | + |
| 313 | + verify(callback, timeout(1000)).onDataReady(captor.capture()); |
| 314 | + |
| 315 | + ByteBuffer received = captor.getValue(); |
| 316 | + |
| 317 | + assertThat( |
| 318 | + new String( |
| 319 | + received.array(), |
| 320 | + received.arrayOffset() + received.position(), |
| 321 | + received.remaining())) |
| 322 | + .isEqualTo(data); |
| 323 | + } |
| 324 | + |
| 325 | + private static Map<String, String> anyHeaders() { |
| 326 | + return anyMap(); |
| 327 | + } |
| 328 | +} |
0 commit comments