ArtworkFetcherManager { public Subscription fetch(ArtInfo artInfo, CompletionListener l) { if (artInfo.forArtist) { if (StringUtils.isEmpty(artInfo.artistName)) { return Observable.<Bitmap>error(new Exception("Invalid artInfo: " + "must have artistName set")).subscribe(l); } else { return fetchArtistImage(artInfo).observeOn(mObserveOn).subscribe(l); } } else { if ((StringUtils.isEmpty(artInfo.artistName) && StringUtils.isEmpty(artInfo.albumName)) && (Uri.EMPTY.equals(artInfo.artworkUri))) { return Observable.<Bitmap>error(new Exception("Invalid artInfo: must have artistName " + "and albumName set or valid artworkUri")).subscribe(l); } else { return fetchAlbumCover(artInfo).observeOn(mObserveOn).subscribe(l); } } } @Inject  ArtworkFetcherManager(
            @ForApplication Context mContext,
            ArtworkPreferences mPreferences,
            BitmapDiskCache mL2Cache,
            ConnectivityManager mConnectivityManager,
            @Named("ObserveOnScheduler") Scheduler mObserveOn,
            @Named("SubscribeOnScheduler") Scheduler mSubscribeOn,
            LastFM mLastFM,
            CoverArtArchive mCoverArtArchive,
            OkHttpClient mOkHttpClient
    ); Subscription fetch(ArtInfo artInfo, CompletionListener l); void clearCaches(); void onDestroy(); static boolean isLocalArtwork(Uri u);  }
@Test public void testAlbumFetching2NoUriLastFMErrorFails() throws IOException { ArtInfo artInfo = ArtInfo.forAlbum("alvvays", "alvvays", null); when(mPreferences.getBoolean(ArtworkPreferences.ONLY_ON_WIFI, true)).thenReturn(false); NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnectedOrConnecting()).thenReturn(true); when(mConnectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); when(mL2Cache.containsKey(artInfo.cacheKey())).thenReturn(false); when(mPreferences.getBoolean(ArtworkPreferences.DOWNLOAD_MISSING_ARTWORK, true)).thenReturn(true); when(mPreferences.getBoolean(ArtworkPreferences.PREFER_DOWNLOAD_ARTWORK, false)).thenReturn(true); mServer.enqueue(new MockResponse().setResponseCode(400).setBody(IOUtils.toString(getClass().getClassLoader() .getResourceAsStream("lfm_error_album_not_found.xml")))); mFetcher.fetch(artInfo, mListener); verify(mListener, never()).onNext(Matchers.<Bitmap>any()); verify(mListener, never()).onCompleted(); verify(mListener).onError(Matchers.<Throwable>any()); assertThat(mFetcher.originalUri).isNullOrEmpty(); verifyZeroInteractions(mContext); }
@Test public void testAlbumFetching3UriOnlyAlwaysFetchesWhenOnline() throws IOException { ArtInfo artInfo = ArtInfo.forAlbum(null, null, Uri.parse(mServer.url("/alvvays.jpg").toString())); System.out.println(artInfo); when(mPreferences.getBoolean(ArtworkPreferences.ONLY_ON_WIFI, true)).thenReturn(false); NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnectedOrConnecting()).thenReturn(true); when(mConnectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); when(mL2Cache.containsKey(artInfo.cacheKey())).thenReturn(false); when(mPreferences.getBoolean(ArtworkPreferences.DOWNLOAD_MISSING_ARTWORK, true)).thenReturn(false); when(mPreferences.getBoolean(ArtworkPreferences.PREFER_DOWNLOAD_ARTWORK, false)).thenReturn(false); mServer.enqueue(new MockResponse().setBody(new Buffer().readFrom(getClass().getClassLoader() .getResourceAsStream("alvvays_alvvays_thumb.jpg")))); mFetcher.fetch(artInfo, mListener); verify(mListener).onNext(Matchers.<Bitmap>any()); verify(mListener).onCompleted(); verify(mListener, never()).onError(Matchers.<Throwable>any()); assertThat(mFetcher.originalUri).isNullOrEmpty(); verifyZeroInteractions(mContext); }
@Test public void testAlbumFetching4LastFMNoMbidFallsback() throws IOException { ArtInfo artInfo = ArtInfo.forAlbum("various artists", "guther d", Uri.parse("content: when(mPreferences.getBoolean(ArtworkPreferences.ONLY_ON_WIFI, true)).thenReturn(false); NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnectedOrConnecting()).thenReturn(true); when(mConnectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); when(mL2Cache.containsKey(artInfo.cacheKey())).thenReturn(false); when(mPreferences.getBoolean(ArtworkPreferences.DOWNLOAD_MISSING_ARTWORK, true)).thenReturn(true); when(mPreferences.getBoolean(ArtworkPreferences.PREFER_DOWNLOAD_ARTWORK, false)).thenReturn(true); mServer.enqueue(new MockResponse().setBody(IOUtils.toString(getClass().getClassLoader() .getResourceAsStream("lfm_album_gunther_d_botsautomix.xml")))); final ContentResolver contentResolver = mock(ContentResolver.class); when(contentResolver.openInputStream(Uri.parse("content: getClass().getClassLoader().getResourceAsStream("alvvays_alvvays_thumb.jpg")); when(mContext.getContentResolver()).thenReturn(contentResolver); mFetcher.fetch(artInfo, mListener); verify(mListener).onNext(Matchers.<Bitmap>any()); verify(mListener).onCompleted(); verify(mListener, never()).onError(Matchers.<Throwable>any()); assertThat(mFetcher.originalUri).isNullOrEmpty(); verify(mContext).getContentResolver(); }
@Test public void testAlbumFetching2LastFMErrorNoUriFails() throws IOException { ArtInfo artInfo = ArtInfo.forAlbum("alvvays", "alvvays", null); when(mPreferences.getBoolean(ArtworkPreferences.ONLY_ON_WIFI, true)).thenReturn(false); NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnectedOrConnecting()).thenReturn(true); when(mConnectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); when(mL2Cache.containsKey(artInfo.cacheKey())).thenReturn(false); when(mPreferences.getBoolean(ArtworkPreferences.DOWNLOAD_MISSING_ARTWORK, true)).thenReturn(true); when(mPreferences.getBoolean(ArtworkPreferences.PREFER_DOWNLOAD_ARTWORK, false)).thenReturn(true); mServer.enqueue(new MockResponse().setResponseCode(400).setBody(IOUtils.toString(getClass().getClassLoader() .getResourceAsStream("lfm_error_album_not_found.xml")))); mFetcher.fetch(artInfo, mListener); verify(mListener, never()).onNext(Matchers.<Bitmap>any()); verify(mListener, never()).onCompleted(); verify(mListener).onError(Matchers.<Throwable>any()); assertThat(mFetcher.originalUri).isNullOrEmpty(); verifyZeroInteractions(mContext); }
@Test public void testArtistFetching1() throws IOException { ArtInfo artInfo = ArtInfo.forArtist("alvvays", null); when(mPreferences.getBoolean(ArtworkPreferences.ONLY_ON_WIFI, true)).thenReturn(false); NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnectedOrConnecting()).thenReturn(true); when(mConnectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); when(mL2Cache.containsKey(artInfo.cacheKey())).thenReturn(false); when(mPreferences.getBoolean(ArtworkPreferences.DOWNLOAD_MISSING_ARTIST_IMAGES, true)).thenReturn(true); mServer.enqueue(new MockResponse().setBody(IOUtils.toString(getClass().getClassLoader() .getResourceAsStream("lfm_artist_alvvays.xml")))); mServer.enqueue(new MockResponse().setBody(new Buffer().readFrom(getClass().getClassLoader() .getResourceAsStream("alvvays_alvvays_thumb.jpg")))); mFetcher.fetch(artInfo, mListener); verify(mListener).onNext(Matchers.<Bitmap>any()); verify(mListener).onCompleted(); verify(mListener, never()).onError(Matchers.<Throwable>any()); assertThat(mFetcher.originalUri).isEqualTo("http: verifyZeroInteractions(mContext); }
@Test public void testArtistFetching2NoImagesFails() throws IOException { ArtInfo artInfo = ArtInfo.forArtist("pretty lights feat. talib kweli", null); when(mPreferences.getBoolean(ArtworkPreferences.ONLY_ON_WIFI, true)).thenReturn(false); NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnectedOrConnecting()).thenReturn(true); when(mConnectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); when(mL2Cache.containsKey(artInfo.cacheKey())).thenReturn(false); when(mPreferences.getBoolean(ArtworkPreferences.DOWNLOAD_MISSING_ARTIST_IMAGES, true)).thenReturn(true); mServer.enqueue(new MockResponse().setBody(IOUtils.toString(getClass().getClassLoader() .getResourceAsStream("lfm_artist_pretty_lights_feat_talib_kweli.xml")))); mFetcher.fetch(artInfo, mListener); verify(mListener, never()).onNext(Matchers.<Bitmap>any()); verify(mListener, never()).onCompleted(); verify(mListener).onError(Matchers.<Throwable>any()); assertThat(mFetcher.originalUri).isNullOrEmpty(); verifyZeroInteractions(mContext); }
@Test public void testAlbumFetching1NoPreferDownloadUsesProvidedUri() throws IOException { ArtInfo artInfo = ArtInfo.forAlbum("alvvays", "alvvays", Uri.parse("content: when(mPreferences.getBoolean(ArtworkPreferences.ONLY_ON_WIFI, true)).thenReturn(false); NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnectedOrConnecting()).thenReturn(true); when(mConnectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); when(mL2Cache.containsKey(artInfo.cacheKey())).thenReturn(false); when(mPreferences.getBoolean(ArtworkPreferences.DOWNLOAD_MISSING_ARTWORK, true)).thenReturn(true); when(mPreferences.getBoolean(ArtworkPreferences.PREFER_DOWNLOAD_ARTWORK, false)).thenReturn(false); final ContentResolver contentResolver = mock(ContentResolver.class); when(contentResolver.openInputStream(Uri.parse("content: getClass().getClassLoader().getResourceAsStream("alvvays_alvvays_thumb.jpg")); when(mContext.getContentResolver()).thenReturn(contentResolver); mFetcher.fetch(artInfo, mListener); verify(mListener).onNext(Matchers.<Bitmap>any()); verify(mListener).onCompleted(); verify(mListener, never()).onError(Matchers.<Throwable>any()); assertThat(mFetcher.originalUri).isNullOrEmpty(); verify(mContext).getContentResolver(); }
@Test public void testAlbumFetching1NoPreferDownloadUsesProvidedUri2() throws IOException { ArtInfo artInfo = ArtInfo.forAlbum("alvvays", "alvvays", Uri.parse(mServer.url("/alvvays.jpg").toString())); when(mPreferences.getBoolean(ArtworkPreferences.ONLY_ON_WIFI, true)).thenReturn(false); NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnectedOrConnecting()).thenReturn(true); when(mConnectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); when(mL2Cache.containsKey(artInfo.cacheKey())).thenReturn(false); when(mPreferences.getBoolean(ArtworkPreferences.DOWNLOAD_MISSING_ARTWORK, true)).thenReturn(true); when(mPreferences.getBoolean(ArtworkPreferences.PREFER_DOWNLOAD_ARTWORK, false)).thenReturn(false); mServer.enqueue(new MockResponse().setBody(new Buffer().readFrom(getClass().getClassLoader() .getResourceAsStream("alvvays_alvvays_thumb.jpg")))); mFetcher.fetch(artInfo, mListener); verify(mListener).onNext(Matchers.<Bitmap>any()); verify(mListener).onCompleted(); verify(mListener, never()).onError(Matchers.<Throwable>any()); assertThat(mFetcher.originalUri).isNullOrEmpty(); verifyZeroInteractions(mContext); }
@Test public void testAlbumFetching1YesPreferDownload() throws IOException { ArtInfo artInfo = ArtInfo.forAlbum("alvvays", "alvvays", Uri.parse("content: when(mPreferences.getBoolean(ArtworkPreferences.ONLY_ON_WIFI, true)).thenReturn(false); NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnectedOrConnecting()).thenReturn(true); when(mConnectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); when(mL2Cache.containsKey(artInfo.cacheKey())).thenReturn(false); when(mPreferences.getBoolean(ArtworkPreferences.DOWNLOAD_MISSING_ARTWORK, true)).thenReturn(true); when(mPreferences.getBoolean(ArtworkPreferences.PREFER_DOWNLOAD_ARTWORK, false)).thenReturn(true); mServer.enqueue(new MockResponse().setBody(IOUtils.toString(getClass().getClassLoader() .getResourceAsStream("lfm_album_alvvays_alvvays.xml")))); mServer.enqueue(new MockResponse().setBody(IOUtils.toString(getClass().getClassLoader() .getResourceAsStream("caa_release_0ea1ee01-54b6-439f-bf01-250375741813.json")))); mServer.enqueue(new MockResponse().setBody(new Buffer().readFrom(getClass().getClassLoader() .getResourceAsStream("alvvays_alvvays_thumb.jpg")))); mFetcher.fetch(artInfo, mListener); verify(mListener).onNext(Matchers.<Bitmap>any()); verify(mListener).onCompleted(); verify(mListener, never()).onError(Matchers.<Throwable>any()); assertThat(mFetcher.originalUri).isEqualTo("http: verifyZeroInteractions(mContext); }
@Test public void testAlbumFetching1YesPreferDownloadLastFMErrorFallsback() throws IOException { ArtInfo artInfo = ArtInfo.forAlbum("alvvays", "alvvays", Uri.parse("content: when(mPreferences.getBoolean(ArtworkPreferences.ONLY_ON_WIFI, true)).thenReturn(false); NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnectedOrConnecting()).thenReturn(true); when(mConnectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); when(mL2Cache.containsKey(artInfo.cacheKey())).thenReturn(false); when(mPreferences.getBoolean(ArtworkPreferences.DOWNLOAD_MISSING_ARTWORK, true)).thenReturn(true); when(mPreferences.getBoolean(ArtworkPreferences.PREFER_DOWNLOAD_ARTWORK, false)).thenReturn(true); mServer.enqueue(new MockResponse().setResponseCode(400).setBody(IOUtils.toString(getClass().getClassLoader() .getResourceAsStream("lfm_error_album_not_found.xml")))); final ContentResolver contentResolver = mock(ContentResolver.class); when(contentResolver.openInputStream(Uri.parse("content: getClass().getClassLoader().getResourceAsStream("alvvays_alvvays_thumb.jpg")); when(mContext.getContentResolver()).thenReturn(contentResolver); mFetcher.fetch(artInfo, mListener); verify(mListener).onNext(Matchers.<Bitmap>any()); verify(mListener).onCompleted(); verify(mListener, never()).onError(Matchers.<Throwable>any()); assertThat(mFetcher.originalUri).isNullOrEmpty(); verify(mContext).getContentResolver(); }
@Test public void testAlbumFetching1YesPreferDownloadCaaErrorFallsBackToLastFM() throws IOException { ArtInfo artInfo = ArtInfo.forAlbum("alvvays", "alvvays", Uri.parse("content: when(mPreferences.getBoolean(ArtworkPreferences.ONLY_ON_WIFI, true)).thenReturn(false); NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnectedOrConnecting()).thenReturn(true); when(mConnectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); when(mL2Cache.containsKey(artInfo.cacheKey())).thenReturn(false); when(mPreferences.getBoolean(ArtworkPreferences.DOWNLOAD_MISSING_ARTWORK, true)).thenReturn(true); when(mPreferences.getBoolean(ArtworkPreferences.PREFER_DOWNLOAD_ARTWORK, false)).thenReturn(true); mServer.enqueue(new MockResponse().setBody(IOUtils.toString(getClass().getClassLoader() .getResourceAsStream("lfm_album_alvvays_alvvays.xml")))); mServer.enqueue(new MockResponse().setResponseCode(404)); mServer.enqueue(new MockResponse().setBody(new Buffer().readFrom(getClass().getClassLoader() .getResourceAsStream("alvvays_alvvays_thumb.jpg")))); mFetcher.fetch(artInfo, mListener); verify(mListener).onNext(Matchers.<Bitmap>any()); verify(mListener).onCompleted(); verify(mListener, never()).onError(Matchers.<Throwable>any()); assertThat(mFetcher.originalUri).isEqualTo("http: verifyZeroInteractions(mContext); }
@Test public void testAlbumFetching1NoWantArtDownloadsRegardlessWhenRemoteUri() throws IOException { ArtInfo artInfo = ArtInfo.forAlbum("alvvays", "alvvays", Uri.parse(mServer.url("/alvvays.jpg").toString())); when(mPreferences.getBoolean(ArtworkPreferences.ONLY_ON_WIFI, true)).thenReturn(false); NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnectedOrConnecting()).thenReturn(true); when(mConnectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); when(mL2Cache.containsKey(artInfo.cacheKey())).thenReturn(false); when(mPreferences.getBoolean(ArtworkPreferences.DOWNLOAD_MISSING_ARTWORK, true)).thenReturn(false); when(mPreferences.getBoolean(ArtworkPreferences.PREFER_DOWNLOAD_ARTWORK, false)).thenReturn(true); mServer.enqueue(new MockResponse().setBody(new Buffer().readFrom(getClass().getClassLoader() .getResourceAsStream("alvvays_alvvays_thumb.jpg")))); mFetcher.fetch(artInfo, mListener); verify(mListener).onNext(Matchers.<Bitmap>any()); verify(mListener).onCompleted(); verify(mListener, never()).onError(Matchers.<Throwable>any()); assertThat(mFetcher.originalUri).isNullOrEmpty(); verifyZeroInteractions(mContext); }
@Test public void testAlbumFetching1NoPreferDownloadGetsLocalArtWhenOffline() throws IOException { ArtInfo artInfo = ArtInfo.forAlbum("alvvays", "alvvays", Uri.parse("content: when(mPreferences.getBoolean(ArtworkPreferences.ONLY_ON_WIFI, true)).thenReturn(false); NetworkInfo networkInfo = mock(NetworkInfo.class); when(networkInfo.isConnectedOrConnecting()).thenReturn(false); when(mConnectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo); when(mL2Cache.containsKey(artInfo.cacheKey())).thenReturn(false); when(mPreferences.getBoolean(ArtworkPreferences.DOWNLOAD_MISSING_ARTWORK, true)).thenReturn(true); when(mPreferences.getBoolean(ArtworkPreferences.PREFER_DOWNLOAD_ARTWORK, false)).thenReturn(false); final ContentResolver contentResolver = mock(ContentResolver.class); when(contentResolver.openInputStream(Uri.parse("content: getClass().getClassLoader().getResourceAsStream("alvvays_alvvays_thumb.jpg")); when(mContext.getContentResolver()).thenReturn(contentResolver); mFetcher.fetch(artInfo, mListener); verify(mListener).onNext(Matchers.<Bitmap>any()); verify(mListener).onCompleted(); verify(mListener, never()).onError(Matchers.<Throwable>any()); assertThat(mFetcher.originalUri).isNullOrEmpty(); verify(mContext).getContentResolver(); }