下面列出了io.reactivex.observers.TestObserver#await ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void testMovieList(final IMovieCollection movies) throws InterruptedException {
final ICollectionObserver collectionObserver = mock(ICollectionObserver.class);
movies.addObserver(collectionObserver);
assertNotNull("getMovies() should not return null!", movies.getMovies());
assertFalse("isLoading should be false.", movies.isLoading());
assertFalse("isLoaded should be false.", movies.isLoaded());
final TestObserver<Void> testSubscriber = movies.load().test();
testSubscriber.await();
testSubscriber.assertNoErrors();
assertFalse("isLoading should be false.", movies.isLoading());
assertTrue("isLoaded should be true.", movies.isLoaded());
assertFalse("hasNexPage should be false.", movies.hasNexPage());
verify(collectionObserver).onUpdate(eq(movies), eq(Action.AppendRange), eq(null), anyList());
final List<Movie> similarMoviesResult = movies.getMovies();
assertNotNull("getMovies() should not return null after load()!", similarMoviesResult);
verifyMovieList(similarMoviesResult);
}
private void testMovieList(final IMovieCollection movies) throws InterruptedException {
final ICollectionObserver collectionObserver = mock(ICollectionObserver.class);
movies.addObserver(collectionObserver);
assertNotNull("getMovies() should not return null!", movies.getMovies());
assertFalse("isLoading should be false.", movies.isLoading());
assertFalse("isLoaded should be false.", movies.isLoaded());
final TestObserver<Void> testSubscriber = movies.load().test();
testSubscriber.await();
testSubscriber.assertNoErrors();
assertFalse("isLoading should be false.", movies.isLoading());
assertTrue("isLoaded should be true.", movies.isLoaded());
assertFalse("hasNexPage should be false.", movies.hasNexPage());
verify(collectionObserver).onUpdate(eq(movies), eq(Action.AppendItem), any(), eq(null));
final List<Movie> similarMoviesResult = movies.getMovies();
assertNotNull("getMovies() should not return null after load()!", similarMoviesResult);
verifyMovieList(similarMoviesResult);
}
@Test
public void testConfiguration() throws Exception {
final TestObserver<Configuration> testSubscriber = service.getConfiguration().test();
testSubscriber.await();
testSubscriber.assertNoErrors();
testSubscriber.assertComplete();
final List<Configuration> result = testSubscriber.values();
assertNotNull("Configuration result is null.", result);
final Configuration config = result.get(0);
assertNotNull("Configuration data is null.", config);
assertNotNull("Configuration for images is null.", config.getImageConfig());
final String imageBaseUrl = config.getImageConfig().getBaseUrl();
assertNotNull("Configuration for image base URL is null.", imageBaseUrl);
}
@Test
public void testNowPlaying() throws Exception {
final TestObserver<PagingEnvelope<MovieData>> testSubscriber = service.getMovieNowPlaying(null).test();
testSubscriber.await();
testSubscriber.assertNoErrors();
testSubscriber.assertComplete();
final List<PagingEnvelope<MovieData>> result = testSubscriber.values();
assertNotNull("Now playing result is null.", result);
final PagingEnvelope<MovieData> movies = result.get(0);
assertNotNull("Now Playing movie data is null.", movies);
assertEquals("Page of the Now Playing movie is null.", movies.getPage(), 1);
assertNotNull("Now playing movie's results is null.", movies.getResults());
}
@Test
public void testMovieDetails() throws Exception {
final int id = 209112;
final TestObserver<MovieDetailsData> testSubscriber = service.getMovieDetails(id).test();
testSubscriber.await();
testSubscriber.assertNoErrors();
testSubscriber.assertComplete();
final List<MovieDetailsData> result = testSubscriber.values();
assertNotNull("Movie details result is null.", result);
final MovieDetailsData movieDetails = result.get(0);
assertNotNull("Movie details data is null.", movieDetails);
assertEquals("Movie details' ID does not match.", movieDetails.getId(), id);
assertNotNull("Movie details' title does not match.", movieDetails.getTitle());
}
@Test
public void testSimilarMovies() throws Exception {
final int id = 209112;
final TestObserver<PagingEnvelope<MovieData>> testSubscriber = service.getSimilarMovies(id, null).test();
testSubscriber.await();
testSubscriber.assertNoErrors();
testSubscriber.assertComplete();
final List<PagingEnvelope<MovieData>> result = testSubscriber.values();
assertNotNull("Similar movies result is null.", result);
final PagingEnvelope<MovieData> movies = result.get(0);
assertNotNull("Similar movies data is null.", movies);
assertEquals("Similar movies page is null.", movies.getPage(), 1);
assertNotNull("Similar movies' results is null.", movies.getResults());
}
@SuppressWarnings("PMD.NcssCount")
@Test
public void testAddFavoriteMovie() throws Exception {
final IFavoriteStore favoriteStore = mock(IFavoriteStore.class);
when(favoriteStore.addFavoriteMovie(any())).thenReturn(Single.just(true));
final IFavoriteMovieCollection favorite = new FavoriteMovieCollection(favoriteStore, entityStore);
final ICollectionObserver collectionObserver = mock(ICollectionObserver.class);
favorite.addObserver(collectionObserver);
final Movie movie = new Movie(service, entityStore, movie1Data);
assertFalse("isFavorite should be false.", movie.isFavorite());
assertNotNull("getMovies() should not return null!", favorite.getMovies());
assertTrue("Favorite movie list should be empty.", favorite.getMovies().isEmpty());
final IObserver objectObserver = mock(IObserver.class);
movie.addObserver(objectObserver);
final TestObserver<Boolean> testSubscriber = favorite.addToFavorite(movie).test();
testSubscriber.await();
testSubscriber.assertNoErrors();
verify(favoriteStore).addFavoriteMovie(eq(movie1Data));
verify(collectionObserver).onUpdate(eq(favorite), eq(Action.AddItemToFront), eq(movie), eq(null));
verify(objectObserver).onUpdate(eq(movie), any());
assertTrue("isFavorite should be false.", movie.isFavorite());
assertEquals("Favorite movie list should contain exact one item.",
1, favorite.getMovies().size());
verifyMovie1(favorite.getMovies().get(0));
}