@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) throws Throwable { GlideUrl glideUrl = (GlideUrl) invocationOnMock.getArguments()[0]; assertEquals(urlLoader.resultUrl, glideUrl.toStringUrl()); return new ModelLoader.LoadData<>(mock(Key.class), fetcher); } }); assertEquals(fetcher, urlLoader.buildLoadData(new GlideUrl(urlLoader.resultUrl), width, height, options).fetcher); }
@Override public LoadData<InputStream> buildLoadData(GlideUrl model, int width, int height, Options options) { // GlideUrls memoize parsed URLs so caching them saves a few object instantiations and time // spent parsing urls. GlideUrl url = model; if (modelCache != null) { url = modelCache.get(model, 0, 0); if (url == null) { modelCache.put(model, 0, 0, model); url = model; } } int timeout = options.get(TIMEOUT); return new LoadData<>(url, new HttpUrlFetcher(url, timeout)); }
@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) throws Throwable { 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); }
@Test public void testAddsNewUrlToCacheIfNotPresentInCache() { urlLoader.resultUrl = "fakeUrl"; Object model = new Object(); int width = 400; int height = 500; doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocationOnMock) throws Throwable { GlideUrl glideUrl = (GlideUrl) invocationOnMock.getArguments()[3]; assertEquals(urlLoader.resultUrl, glideUrl.toStringUrl()); return null; } }).when(modelCache).put(eq(model), eq(width), eq(height), any(GlideUrl.class)); urlLoader.buildLoadData(model, width, height, options); verify(modelCache).put(eq(model), eq(width), eq(height), any(GlideUrl.class)); }
@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); }
@Override public void registerComponents(Context context, Glide glide) { OkHttpClient client = new OkHttpClient .Builder() .addInterceptor(createInterceptor(new DispatchingProgressListener())) .build(); glide.register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(client)); }
public static GlideUrl getGlideUrlByUser(String url) { if (AccountHelper.isLogin()) { return new GlideUrl(url, new LazyHeaders .Builder() .addHeader("Cookie", AccountHelper.getCookie()) .build()); } else { return new GlideUrl(url); } }
public static void displayImageReferer(String url,ImageView imageView,String referer) { if(url==null){ return; } LazyHeaders.Builder builder=new LazyHeaders.Builder().addHeader("User-Agent", UserAgent); if(referer!=null){ builder.addHeader("Referer", referer); } /* .addHeader("key2", new LazyHeaderFactory() { @Override public String buildHeader() { String expensiveAuthHeader = computeExpensiveAuthHeader(); return expensiveAuthHeader; } }) */ GlideUrl glideUrl = new GlideUrl(url,builder.build()); Glide.with(MainApp.getContext()) .load(glideUrl) .placeholder(R.drawable.pictures_no) .thumbnail(0.2f) .diskCacheStrategy(DiskCacheStrategy.SOURCE) .into(imageView); }
@Override public void registerComponents(Context context, Registry registry) { registerMockModelLoader(GlideUrl.class, InputStream.class, new ByteArrayInputStream(new byte[0]), registry); registerMockModelLoader(File.class, InputStream.class, new ByteArrayInputStream(new byte[0]), registry); registerMockModelLoader(File.class, ParcelFileDescriptor.class, mock(ParcelFileDescriptor.class), registry); registerMockModelLoader(File.class, ByteBuffer.class, ByteBuffer.allocate(10), registry); }
@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, urlLoader.buildLoadData(model, width, height, options).fetcher); }
public static void init(Glide glide, OkHttpClient okHttpClient) { OkHttpClient.Builder builder; if (okHttpClient != null) { builder = okHttpClient.newBuilder(); } else { builder = new OkHttpClient.Builder(); } builder.addNetworkInterceptor(createInterceptor(new DispatchingProgressListener())); glide.getRegistry().replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(builder.build())); }
@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 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)); }
public ArtistImageFetcher(Context context, LastFMRestClient lastFMRestClient, ArtistImage model, ModelLoader<GlideUrl, InputStream> urlLoader, int width, int height) { this.context = context; this.lastFMRestClient = lastFMRestClient; this.model = model; this.urlLoader = urlLoader; this.width = width; this.height = height; }
private static List<Key> getAlternateKeys(List<String> alternateUrls) { List<Key> result = new ArrayList<>(alternateUrls.size()); for (String alternate : alternateUrls) { result.add(new GlideUrl(alternate)); } return result; }
@Override public boolean handles(GlideUrl url) { return true; }
@Override public LoadData<InputStream> buildLoadData(GlideUrl model, int width, int height, Options options) { return new LoadData<>(model, new OkHttpStreamFetcher(client, model)); }
@Override public ModelLoader<String, InputStream> build(MultiModelLoaderFactory multiFactory) { return new PixelsModelLoader(multiFactory.build(GlideUrl.class, InputStream.class), urlCache); }
public OkHttpStreamFetcher(OkHttpClient client, GlideUrl url) { this.client = client; this.url = url; }
@Override public void registerComponents(Context context, Registry registry) { registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory()); }
@Override public LoadData<InputStream> buildLoadData(GlideUrl model, int width, int height,Options options) { return new LoadData<>(model, new OkHttpStreamFetcher(client, model)); }
@Override public ModelLoader<GlideUrl, InputStream> build(MultiModelLoaderFactory multiFactory) { return new OkHttpUrlLoader(client); }
@Override public void registerComponents(Context context, Glide glide) { //glide.clearDiskCache(); glide.register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory()); glide.setMemoryCategory(MemoryCategory.NORMAL); }
@Override public void registerComponents(Context context, Registry registry) { registry.replace(GlideUrl.class, InputStream.class, new VolleyUrlLoader.Factory(context)); }
@SuppressWarnings("unused") public VolleyStreamFetcher(RequestQueue requestQueue, GlideUrl url) { this(requestQueue, url, DEFAULT_REQUEST_FACTORY); }
public VolleyStreamFetcher(RequestQueue requestQueue, GlideUrl url, VolleyRequestFactory requestFactory) { this.requestQueue = requestQueue; this.url = url; this.requestFactory = requestFactory; }
private DataFetcher<InputStream> getFetcher(Headers headers) { URL url = mockWebServer.url(DEFAULT_PATH).url(); return new VolleyStreamFetcher(requestQueue, new GlideUrl(url.toString(), headers)); }
public OkHttpStreamFetcher(Call.Factory client, GlideUrl url) { this.client = client; this.url = url; }
@Override public ModelLoader<Photo, InputStream> build(MultiModelLoaderFactory multiFactory) { return new FlickrModelLoader(multiFactory.build(GlideUrl.class, InputStream.class), modelCache); }
@Override public void registerComponents(Context context, Glide glide, Registry registry) { BaseApplication application = (BaseApplication) context.getApplicationContext(); //Glide 底层默认使用 HttpConnection 进行网络请求,这里替换为 Okhttp 后才能使用本框架,进行 Glide 的加载进度监听 registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(application.getOkHttpClient())); }