Search in sources :

Example 1 with TestScheduler

use of rx.schedulers.TestScheduler in project MovieGuide by esoxjem.

the class MovieDetailsPresenterImplTest method shouldBeAbleToShowTrailers.

@Test
public void shouldBeAbleToShowTrailers() {
    TestScheduler testScheduler = new TestScheduler();
    TestSubscriber<List<Video>> testSubscriber = new TestSubscriber<>();
    Observable<List<Video>> responseObservable = Observable.just(videos).subscribeOn(testScheduler);
    responseObservable.subscribe(testSubscriber);
    when(movieDetailsInteractor.getTrailers(anyString())).thenReturn(responseObservable);
    movieDetailsPresenter.showTrailers(movie);
    testScheduler.triggerActions();
    testSubscriber.assertNoErrors();
    testSubscriber.assertCompleted();
    verify(view).showTrailers(videos);
}
Also used : TestSubscriber(rx.observers.TestSubscriber) List(java.util.List) TestScheduler(rx.schedulers.TestScheduler) Test(org.junit.Test)

Example 2 with TestScheduler

use of rx.schedulers.TestScheduler in project MovieGuide by esoxjem.

the class MoviesListingPresenterImplTest method shouldBeAbleToDisplayMovies.

@Test
public void shouldBeAbleToDisplayMovies() {
    TestScheduler testScheduler = new TestScheduler();
    TestSubscriber<List<Movie>> testSubscriber = new TestSubscriber<>();
    Observable<List<Movie>> responseObservable = Observable.just(movies).subscribeOn(testScheduler);
    responseObservable.subscribe(testSubscriber);
    when(interactor.fetchMovies()).thenReturn(responseObservable);
    presenter.setView(view);
    testScheduler.triggerActions();
    testSubscriber.assertNoErrors();
    testSubscriber.onCompleted();
    verify(view).showMovies(movies);
}
Also used : TestSubscriber(rx.observers.TestSubscriber) List(java.util.List) TestScheduler(rx.schedulers.TestScheduler) Test(org.junit.Test)

Example 3 with TestScheduler

use of rx.schedulers.TestScheduler in project apollo-android by apollographql.

the class RxApolloTest method queryWatcherNotCalledWhenCanceled.

@Test
public void queryWatcherNotCalledWhenCanceled() throws Exception {
    server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_WITH_ID));
    server.enqueue(mockResponse(FILE_EPISODE_HERO_NAME_CHANGE));
    final TestSubscriber<Response<EpisodeHeroNameQuery.Data>> testSubscriber = new TestSubscriber<>();
    TestScheduler scheduler = new TestScheduler();
    Subscription subscription = RxApollo.from(apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE))).watcher()).observeOn(scheduler).subscribe(testSubscriber);
    scheduler.triggerActions();
    apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(EMPIRE))).responseFetcher(NETWORK_ONLY).enqueue(null);
    subscription.unsubscribe();
    scheduler.triggerActions();
    testSubscriber.assertValueCount(1);
    assertThat(testSubscriber.getOnNextEvents().get(0).data().hero().name()).isEqualTo("R2-D2");
}
Also used : Utils.mockResponse(com.apollographql.apollo.Utils.mockResponse) Response(com.apollographql.apollo.api.Response) EpisodeHeroNameQuery(com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery) TestSubscriber(rx.observers.TestSubscriber) TestScheduler(rx.schedulers.TestScheduler) Subscription(rx.Subscription) Test(org.junit.Test)

Example 4 with TestScheduler

use of rx.schedulers.TestScheduler in project AnDevCon-RxPatterns by colintheshots.

the class Example16Test method test_anomalous_network_event.

@Test
public void test_anomalous_network_event() {
    // TestScheduler lets you advance time by hand
    TestScheduler scheduler = Schedulers.test();
    TestSubscriber<NetworkResponse> subscriber = new TestSubscriber<>();
    // Scheduler.Worker lets you schedule events in time
    Scheduler.Worker worker = scheduler.createWorker();
    // Subjects allow both input and output, so they can be swapped in for
    // Retrofit calls to unit test your code.
    final PublishSubject<NetworkResponse> networkSubject = PublishSubject.create();
    // schedule a first observable event to occur at 1000 ms
    worker.schedule(new Action0() {

        @Override
        public void call() {
            networkSubject.onError(new TimeoutException());
        }
    }, 10000, TimeUnit.MILLISECONDS);
    // subscribing so events appear
    networkSubject.subscribeOn(scheduler).subscribe(subscriber);
    scheduler.advanceTimeBy(20000, TimeUnit.MILLISECONDS);
    subscriber.awaitTerminalEvent();
    // we use the class-based assertError method, since it's easier to match
    subscriber.assertError(TimeoutException.class);
    subscriber.assertValueCount(0);
    subscriber.assertUnsubscribed();
}
Also used : Action0(rx.functions.Action0) TestScheduler(rx.schedulers.TestScheduler) Scheduler(rx.Scheduler) TestSubscriber(rx.observers.TestSubscriber) TestScheduler(rx.schedulers.TestScheduler) Override(java.lang.Override) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 5 with TestScheduler

use of rx.schedulers.TestScheduler in project android-oss by kickstarter.

the class SearchViewModelTest method testFeaturedPopularRefTags.

@Test
public void testFeaturedPopularRefTags() {
    final TestScheduler scheduler = new TestScheduler();
    final List<Project> projects = Arrays.asList(ProjectFactory.allTheWayProject(), ProjectFactory.almostCompletedProject(), ProjectFactory.backedProject());
    final MockApiClient apiClient = new MockApiClient() {

        @Override
        @NonNull
        public Observable<DiscoverEnvelope> fetchProjects(@NonNull final DiscoveryParams params) {
            return Observable.just(DiscoverEnvelopeFactory.discoverEnvelope(projects));
        }
    };
    final Environment env = environment().toBuilder().scheduler(scheduler).apiClient(apiClient).build();
    setUpEnvironment(env);
    // populate search and overcome debounce
    this.vm.inputs.search("");
    scheduler.advanceTimeBy(300, TimeUnit.MILLISECONDS);
    this.vm.inputs.projectClicked(projects.get(0));
    this.goToRefTag.assertValues(RefTag.searchPopularFeatured());
    this.goToProject.assertValues(projects.get(0));
}
Also used : Project(com.kickstarter.models.Project) DiscoverEnvelope(com.kickstarter.services.apiresponses.DiscoverEnvelope) MockApiClient(com.kickstarter.mock.services.MockApiClient) NonNull(androidx.annotation.NonNull) Environment(com.kickstarter.libs.Environment) TestScheduler(rx.schedulers.TestScheduler) DiscoveryParams(com.kickstarter.services.DiscoveryParams) Test(org.junit.Test)

Aggregations

TestScheduler (rx.schedulers.TestScheduler)26 Test (org.junit.Test)23 TestSubscriber (rx.observers.TestSubscriber)12 Environment (com.kickstarter.libs.Environment)11 NonNull (androidx.annotation.NonNull)8 MockApiClient (com.kickstarter.mock.services.MockApiClient)8 Project (com.kickstarter.models.Project)6 DiscoveryParams (com.kickstarter.services.DiscoveryParams)6 DiscoverEnvelope (com.kickstarter.services.apiresponses.DiscoverEnvelope)6 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)3 Override (java.lang.Override)3 List (java.util.List)3 Before (org.junit.Before)3 Subscription (rx.Subscription)3 Action0 (rx.functions.Action0)3 Intent (android.content.Intent)2 Utils.mockResponse (com.apollographql.apollo.Utils.mockResponse)2 Response (com.apollographql.apollo.api.Response)2 Update (com.kickstarter.models.Update)2 ApiClientType (com.kickstarter.services.ApiClientType)2