package net.grandcentrix.thirtyinch.test;

import android.support.annotation.NonNull;

import net.grandcentrix.thirtyinch.TiPresenter;
import net.grandcentrix.thirtyinch.TiView;

import java.util.concurrent.Executor;

public class TiTestPresenter<V extends TiView> {

    private TiPresenter<V> mPresenter;

    public TiTestPresenter(final TiPresenter<V> presenter) {
        mPresenter = presenter;
    }

    /**
     * attaches the new view and takes care for removing the old view when one is attached
     */
    public void attachView(final V view) {
        detachView();

        mPresenter.setUiThreadExecutor(new Executor() {
            @Override
            public void execute(@NonNull final Runnable action) {
                action.run();
            }
        });
        mPresenter.attachView(view);
    }

    public void create() {
        mPresenter.create();
    }

    public void destroy() {
        detachView();
        mPresenter.destroy();
    }

    /**
     * moves the presenter into state {@link TiPresenter.State#VIEW_DETACHED}
     * from every state
     */
    public void detachView() {
        final TiPresenter.State state = mPresenter.getState();
        switch (state) {
            case INITIALIZED:
                mPresenter.create();
                break;
            case VIEW_DETACHED:
                // already there
                break;
            case VIEW_ATTACHED:
                mPresenter.detachView();
                mPresenter.setUiThreadExecutor(null);
                break;
            case DESTROYED:
                throw new IllegalStateException(
                        "Presenter is already destroyed, further lifecycle changes aren't allowed");
        }
    }
}
@@ -79,16 +79,53 @@
 onNull;%0A
+import java.util.concurrent.Executor;
 %0Aimport 
@@ -212,47 +212,468 @@
 ew;%0A
-%0A
 import 
-java.util.concurrent.Executor;%0A
+net.grandcentrix.thirtyinch.ViewAction;%0A%0A/**%0A * Designed for unit testing of %7B@link TiPresenter#sendToView(ViewAction)%7D.%0A * %3Cp%3E%0A * The problem is that %7B@link TiPresenter#sendToView(ViewAction)%7D needs a ui executor thread.%0A * Unfortunately a ui executor thread isn't available in unit test.%0A * %3C/p%3E%0A * %3Cp%3E%0A * This %7B@link TiTestPresenter%7D holds the %7B@link TiPresenter%7D under test and replace the %22ui executor%22 with a stubbed%0A * implementation.%0A * %3C/p%3E%0A */
 %0Apub

Add javadoc to TiTestPresenter