All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rx.marble.junit.MarbleRule Maven / Gradle / Ivy

There is a newer version: 1.3
Show newest version
package rx.marble.junit;


import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import rx.Observable;
import rx.marble.*;

import java.util.List;
import java.util.Map;

public class MarbleRule implements TestRule {

    private static ThreadLocal schedulerHolder = new ThreadLocal<>();

    public final MarbleScheduler scheduler;

    public MarbleRule() {
        scheduler = new MarbleScheduler();
    }

    public MarbleRule(long frameTimeFactor) {
        scheduler = new MarbleScheduler(frameTimeFactor);
    }

    public static  HotObservable hot(String marbles, Map values) {
        return schedulerHolder.get().createHotObservable(marbles, values);
    }

    public static HotObservable hot(String marbles) {
        return schedulerHolder.get().createHotObservable(marbles);
    }

    public static  ColdObservable cold(String marbles, Map values) {
        return schedulerHolder.get().createColdObservable(marbles, values);
    }

    public static ColdObservable cold(String marbles) {
        return schedulerHolder.get().createColdObservable(marbles);
    }

    public static ISetupTest expectObservable(Observable actual) {
        return schedulerHolder.get().expectObservable(actual);
    }

    public static ISetupTest expectObservable(Observable actual, String unsubscriptionMarbles) {
        return schedulerHolder.get().expectObservable(actual, unsubscriptionMarbles);
    }

    public static ISetupSubscriptionsTest expectSubscriptions(List subscriptions) {
        return schedulerHolder.get().expectSubscriptions(subscriptions);
    }

    @Override
    public Statement apply(final Statement base, Description description) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                schedulerHolder.set(scheduler);
                try {
                    base.evaluate();
                    scheduler.flush();
                } finally {
                    schedulerHolder.remove();
                }

            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy