@Test public void testLoadResource_returnsFileDescriptor() throws Exception { Context context = RuntimeEnvironment.application; Uri uri = Uri.parse("file://nothing"); ContentResolver contentResolver = context.getContentResolver(); ContentResolverShadow shadow = (ContentResolverShadow) Shadow.extract(contentResolver); AssetFileDescriptor assetFileDescriptor = mock(AssetFileDescriptor.class); ParcelFileDescriptor parcelFileDescriptor = mock(ParcelFileDescriptor.class); when(assetFileDescriptor.getParcelFileDescriptor()).thenReturn(parcelFileDescriptor); shadow.registerFileDescriptor(uri, assetFileDescriptor); FileDescriptorLocalUriFetcher fetcher = new FileDescriptorLocalUriFetcher(context.getContentResolver(), uri); fetcher.loadData(Priority.NORMAL, callback); verify(callback).onDataReady(eq(parcelFileDescriptor)); }
@Test public void testLoadResource_returnsFileDescriptor() throws Exception { Context context = RuntimeEnvironment.application; Uri uri = Uri.parse("file://nothing"); ContentResolver contentResolver = context.getContentResolver(); ContentResolverShadow shadow = Shadow.extract(contentResolver); AssetFileDescriptor assetFileDescriptor = mock(AssetFileDescriptor.class); ParcelFileDescriptor parcelFileDescriptor = mock(ParcelFileDescriptor.class); when(assetFileDescriptor.getParcelFileDescriptor()).thenReturn(parcelFileDescriptor); shadow.registerFileDescriptor(uri, assetFileDescriptor); FileDescriptorLocalUriFetcher fetcher = new FileDescriptorLocalUriFetcher(context.getContentResolver(), uri); fetcher.loadData(Priority.NORMAL, callback); verify(callback).onDataReady(eq(parcelFileDescriptor)); }
@Test public void testLoadResource_withNullFileDescriptor_callsLoadFailed() { Context context = RuntimeEnvironment.application; Uri uri = Uri.parse("file://nothing"); ContentResolver contentResolver = context.getContentResolver(); ContentResolverShadow shadow = Shadow.extract(contentResolver); shadow.registerFileDescriptor(uri, null /*fileDescriptor*/); FileDescriptorLocalUriFetcher fetcher = new FileDescriptorLocalUriFetcher(context.getContentResolver(), uri); fetcher.loadData(Priority.NORMAL, callback); verify(callback).onLoadFailed(isA(FileNotFoundException.class)); }
@Test public void testLoadResource_withNullFileDescriptor_callsLoadFailed() { Context context = RuntimeEnvironment.application; Uri uri = Uri.parse("file://nothing"); ContentResolver contentResolver = context.getContentResolver(); ContentResolverShadow shadow = (ContentResolverShadow) Shadow.extract(contentResolver); shadow.registerFileDescriptor(uri, null /*fileDescriptor*/); FileDescriptorLocalUriFetcher fetcher = new FileDescriptorLocalUriFetcher(context.getContentResolver(), uri); fetcher.loadData(Priority.NORMAL, callback); verify(callback).onLoadFailed(isA(FileNotFoundException.class)); }
@Override public DataFetcher<ParcelFileDescriptor> build(Uri uri) { return new FileDescriptorLocalUriFetcher(contentResolver, uri); }
@Override protected DataFetcher<ParcelFileDescriptor> getLocalUriFetcher(Context context, Uri uri) { return new FileDescriptorLocalUriFetcher(context, uri); }