@Override public LoadData<Data> buildLoadData(@NonNull Integer model, int width, int height, @NonNull Options options) { Uri uri = getResourceUri(model); return uri == null ? null : uriLoader.buildLoadData(uri, width, height, options); }
@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); }
@Override public LoadData<Data> buildLoadData(@NonNull String model, int width, int height, @NonNull Options options) { Uri uri = parseUri(model); if (uri == null || !uriLoader.handles(uri)) { return null; } return uriLoader.buildLoadData(uri, width, height, options); }
@Test public void testDoesNotThrowOnInvalidOrMissingId() { assertThat(loader.buildLoadData(1234, 0, 0, options)).isNull(); verify(uriLoader, never()).buildLoadData(any(Uri.class), anyInt(), anyInt(), any(Options.class)); } }
@Test public void testReturnsUrlFromCacheIfPresent() { Object model = new Object(); int width = 100; int height = 200; GlideUrl expectedUrl = mock(GlideUrl.class); when(modelCache.get(eq(model), eq(width), eq(height))).thenReturn(expectedUrl); when(wrapped.buildLoadData(eq(expectedUrl), eq(width), eq(height), eq(options))) .thenReturn(new ModelLoader.LoadData<>(mock(Key.class), fetcher)); assertEquals( fetcher, Preconditions.checkNotNull(urlLoader.buildLoadData(model, width, height, options)).fetcher); }
@Nullable @Override public LoadData<Data> buildLoadData( @NonNull WaitModel<Model> waitModel, int width, int height, @NonNull Options options) { LoadData<Data> wrappedLoadData = wrapped .buildLoadData(waitModel.wrapped, width, height, options); if (wrappedLoadData == null) { return null; } return new LoadData<>( wrappedLoadData.sourceKey, new WaitFetcher<>(wrappedLoadData.fetcher, waitModel.latch)); }
@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 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)); } }
@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 testHandlesResourceUris() { Uri resourceUri = Uri.parse("android.resource://com.bumptech.glide.tests/raw/ic_launcher"); when(uriLoader.buildLoadData(eq(resourceUri), eq(IMAGE_SIDE), eq(IMAGE_SIDE), eq(options))) .thenReturn(new ModelLoader.LoadData<>(key, fetcher)); assertTrue(loader.handles(resourceUri.toString())); assertEquals( fetcher, Preconditions.checkNotNull( loader.buildLoadData(resourceUri.toString(), IMAGE_SIDE, IMAGE_SIDE, options)).fetcher); }
@Test public void testDoesNotInteractWithModelCacheIfNull() { TestLoader urlLoader = new TestLoader(wrapped, null); urlLoader.resultUrl = "fakeUrl"; int width = 456; int height = 789; when(wrapped.buildLoadData(any(GlideUrl.class), eq(width), eq(height), eq(options))) .thenReturn(new ModelLoader.LoadData<>(mock(Key.class), fetcher)); assertEquals( fetcher, Preconditions.checkNotNull( urlLoader.buildLoadData(new Object(), width, height, options)).fetcher); }
@Test public void testHandlesHttps() { String url = "https://www.google.com"; Uri expected = Uri.parse(url); when(uriLoader.buildLoadData(eq(expected), eq(IMAGE_SIDE), eq(IMAGE_SIDE), eq(options))) .thenReturn(new ModelLoader.LoadData<>(key, fetcher)); assertTrue(loader.handles(url)); assertEquals( fetcher, Preconditions.checkNotNull( loader.buildLoadData(url, IMAGE_SIDE, IMAGE_SIDE, options)).fetcher); }
@Test public void testHandlesFileUris() { File f = RuntimeEnvironment.application.getCacheDir(); Uri expected = Uri.fromFile(f); when(uriLoader.buildLoadData(eq(expected), eq(IMAGE_SIDE), eq(IMAGE_SIDE), eq(options))) .thenReturn(new ModelLoader.LoadData<>(key, fetcher)); assertTrue(loader.handles(f.getAbsolutePath())); assertEquals( fetcher, Preconditions.checkNotNull( loader.buildLoadData(expected.toString(), IMAGE_SIDE, IMAGE_SIDE, options)).fetcher); }
@Test public void testHandlesHttp() { String url = "http://www.google.com"; Uri expected = Uri.parse(url); when(uriLoader.buildLoadData(eq(expected), eq(IMAGE_SIDE), eq(IMAGE_SIDE), eq(options))) .thenReturn(new ModelLoader.LoadData<>(key, fetcher)); assertTrue(loader.handles(url)); assertEquals( fetcher, Preconditions.checkNotNull( loader.buildLoadData(url, IMAGE_SIDE, IMAGE_SIDE, options)).fetcher); }
@Test public void testHandlesContent() { String content = "content://com.bumptech.glide"; Uri expected = Uri.parse(content); when(uriLoader.buildLoadData(eq(expected), eq(IMAGE_SIDE), eq(IMAGE_SIDE), eq(options))) .thenReturn(new ModelLoader.LoadData<>(key, fetcher)); assertTrue(loader.handles(content)); assertEquals( fetcher, Preconditions.checkNotNull( loader.buildLoadData(content, IMAGE_SIDE, IMAGE_SIDE, options)).fetcher); }
@Test public void testCanHandleComplexFilePaths() { String testPath = "/storage/emulated/0/DCIM/Camera/IMG_20140520_100001:nopm:.jpg,mimeType=image/jpeg," + "2448x3264,orientation=0,date=Tue"; Uri expected = Uri.fromFile(new File(testPath)); when(uriLoader.buildLoadData(eq(expected), eq(IMAGE_SIDE), eq(IMAGE_SIDE), eq(options))) .thenReturn(new ModelLoader.LoadData<>(key, fetcher)); assertTrue(loader.handles(testPath)); assertEquals( fetcher, Preconditions.checkNotNull( loader.buildLoadData(testPath, IMAGE_SIDE, IMAGE_SIDE, options)).fetcher); }
@Test public void testCanHandleId() { int id = android.R.drawable.star_off; Uri contentUri = Uri.parse("android.resource://android/drawable/star_off"); when(uriLoader.buildLoadData(eq(contentUri), anyInt(), anyInt(), any(Options.class))) .thenReturn(new ModelLoader.LoadData<>(key, fetcher)); assertTrue(loader.handles(id)); assertEquals( fetcher, Preconditions.checkNotNull(loader.buildLoadData(id, 100, 100, new Options())).fetcher); }
@Test public void testHandlesPaths() { // TODO fix drive letter parsing somehow assumeTrue("it will fail with schema being the drive letter (C:\\... -> C)", !Util.isWindows()); File f = RuntimeEnvironment.application.getCacheDir(); Uri expected = Uri.fromFile(f); when(uriLoader.buildLoadData(eq(expected), eq(IMAGE_SIDE), eq(IMAGE_SIDE), eq(options))) .thenReturn(new ModelLoader.LoadData<>(key, fetcher)); assertTrue(loader.handles(f.getAbsolutePath())); assertEquals( fetcher, Preconditions.checkNotNull( loader.buildLoadData(f.getAbsolutePath(), IMAGE_SIDE, IMAGE_SIDE, options)).fetcher); }