@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") private static List<Key> getAlternateKeys(Collection<String> alternateUrls) { List<Key> result = new ArrayList<>(alternateUrls.size()); for (String alternate : alternateUrls) { result.add(new GlideUrl(alternate)); } return result; }
@Override public LoadData<InputStream> buildLoadData(@NonNull URL model, int width, int height, @NonNull Options options) { return glideUrlLoader.buildLoadData(new GlideUrl(model), width, height, options); }
@Override public LoadData<Data> buildLoadData(@NonNull Uri uri, int width, int height, @NonNull Options options) { GlideUrl glideUrl = new GlideUrl(uri.toString()); return urlLoader.buildLoadData(glideUrl, width, height, options); }
@Override public LoadData<InputStream> buildLoadData(@NonNull Uri model, int width, int height, @NonNull Options options) { return urlLoader.buildLoadData(new GlideUrl(model.toString()), width, height, options); }
@Test(expected = IllegalArgumentException.class) public void testThrowsIfGivenStringURLIsEmpty() { new GlideUrl(""); }
@Test(expected = NullPointerException.class) public void testThrowsIfGivenURLIsNull() { new GlideUrl((URL) null); }
@Test(expected = IllegalArgumentException.class) public void testThrowsIfGivenStringUrlIsNull() { new GlideUrl((String) null); }
private HttpUrlFetcher getFetcher(Headers headers) { URL url = mockWebServer.url(DEFAULT_PATH).url(); return new HttpUrlFetcher(new GlideUrl(url, headers), TIMEOUT_TIME_MS, HttpUrlFetcher.DEFAULT_CONNECTION_FACTORY); } }
private DataFetcher<InputStream> getFetcher(Headers headers) { URL url = mockWebServer.url(DEFAULT_PATH).url(); return new VolleyStreamFetcher(requestQueue, new GlideUrl(url.toString(), headers)); }
@Test public void testHandlesMostlyInvalidHttpUris() { Uri mostlyInvalidHttpUri = Uri.parse( "http://myserver_url.com:80http://myserver_url.com/webapp/images/no_image.png?size=100"); assertTrue(loader.handles(mostlyInvalidHttpUri)); loader.buildLoadData(mostlyInvalidHttpUri, IMAGE_SIDE, IMAGE_SIDE, OPTIONS); verify(urlLoader) .buildLoadData(eq(new GlideUrl(mostlyInvalidHttpUri.toString())), eq(IMAGE_SIDE), eq(IMAGE_SIDE), eq(OPTIONS)); } }
@Override @Nullable public LoadData<InputStream> buildLoadData(@NonNull Model model, int width, int height, @NonNull Options options) { GlideUrl result = null; if (modelCache != null) { result = modelCache.get(model, width, height); } if (result == null) { String stringURL = getUrl(model, width, height, options); if (TextUtils.isEmpty(stringURL)) { return null; } result = new GlideUrl(stringURL, getHeaders(model, width, height, options)); if (modelCache != null) { modelCache.put(model, width, height, result); } } // TODO: this is expensive and slow to calculate every time, we should either cache these, or // try to come up with a way to avoid finding them when not necessary. List<String> alternateUrls = getAlternateUrls(model, width, height, options); LoadData<InputStream> concreteLoaderData = concreteLoader.buildLoadData(result, width, height, options); if (concreteLoaderData == null || alternateUrls.isEmpty()) { return concreteLoaderData; } else { return new LoadData<>(concreteLoaderData.sourceKey, getAlternateKeys(alternateUrls), concreteLoaderData.fetcher); } }
@Test public void testHandlesHttpsUris() throws MalformedURLException { Uri httpsUri = Uri.parse("https://www.google.com"); loader.buildLoadData(httpsUri, IMAGE_SIDE, IMAGE_SIDE, OPTIONS); assertTrue(loader.handles(httpsUri)); verify(urlLoader) .buildLoadData(eq(new GlideUrl(httpsUri.toString())), eq(IMAGE_SIDE), eq(IMAGE_SIDE), eq(OPTIONS)); }
@Test public void testHandlesHttpUris() throws MalformedURLException { Uri httpUri = Uri.parse("http://www.google.com"); loader.buildLoadData(httpUri, IMAGE_SIDE, IMAGE_SIDE, OPTIONS); assertTrue(loader.handles(httpUri)); verify(urlLoader) .buildLoadData(eq(new GlideUrl(httpUri.toString())), eq(IMAGE_SIDE), eq(IMAGE_SIDE), eq(OPTIONS)); }
@Test public void testIssue133() throws MalformedURLException { // u00e0=à final String original = "http://www.commitstrip.com/wp-content/uploads/2014/07/" + "Excel-\u00E0-toutes-les-sauces-650-finalenglish.jpg"; final String escaped = "http://www.commitstrip.com/wp-content/uploads/2014/07/" + "Excel-%C3%A0-toutes-les-sauces-650-finalenglish.jpg"; GlideUrl glideUrlFromString = new GlideUrl(original); assertEquals(escaped, glideUrlFromString.toURL().toString()); GlideUrl glideUrlFromEscapedString = new GlideUrl(escaped); assertEquals(escaped, glideUrlFromEscapedString.toURL().toString()); GlideUrl glideUrlFromUrl = new GlideUrl(new URL(original)); assertEquals(escaped, glideUrlFromUrl.toURL().toString()); GlideUrl glideUrlFromEscapedUrl = new GlideUrl(new URL(escaped)); assertEquals(escaped, glideUrlFromEscapedUrl.toURL().toString()); }
@Test public void testProducesEquivalentUrlFromString() throws MalformedURLException { String stringUrl = "http://www.google.com"; GlideUrl glideUrl = new GlideUrl(stringUrl); URL url = glideUrl.toURL(); assertEquals(stringUrl, url.toString()); }
@Test public void testProducesEquivalentStringFromURL() throws MalformedURLException { String expected = "http://www.washingtonpost.com"; URL url = new URL(expected); GlideUrl glideUrl = new GlideUrl(url); assertEquals(expected, glideUrl.toStringUrl()); }
@Test public void issue_2583() throws MalformedURLException { String original = "http://api.met.no/weatherapi/weathericon/1.1/?symbol=9;content_type=image/png"; GlideUrl glideUrl = new GlideUrl(original); assertThat(glideUrl.toURL().toString()).isEqualTo(original); assertThat(glideUrl.toStringUrl()).isEqualTo(original); }
@Test public void testBuildsNewUrlIfNotPresentInCache() { int width = 10; int height = 11; urlLoader.resultUrl = "fakeUrl"; when(wrapped.buildLoadData(any(GlideUrl.class), eq(width), eq(height), eq(options))) .thenAnswer(new Answer<ModelLoader.LoadData<InputStream>>() { @Override public ModelLoader.LoadData<InputStream> answer(InvocationOnMock invocationOnMock) { GlideUrl glideUrl = (GlideUrl) invocationOnMock.getArguments()[0]; assertEquals(urlLoader.resultUrl, glideUrl.toStringUrl()); return new ModelLoader.LoadData<>(mock(Key.class), fetcher); } }); assertEquals( fetcher, Preconditions.checkNotNull( urlLoader.buildLoadData( new GlideUrl(urlLoader.resultUrl), width, height, options)).fetcher); }