package com.hosaka.rxanimation;

import android.app.Instrumentation;
import android.os.SystemClock;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.view.View;
import android.view.animation.AlphaAnimation;

import com.hosaka.rxanimation.animation.AnimationEvent;
import com.hosaka.rxanimation.animation.RxAnimation;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;

import static com.google.common.truth.Truth.assertThat;

@RunWith(AndroidJUnit4.class)
public final class RxAnimationTest {
    @Rule
    public final ActivityTestRule<TestActivity> activityRule =
            new ActivityTestRule<>(TestActivity.class);

    private final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
    private View child;

    @Before
    public void setUp() {
        TestActivity activity = activityRule.getActivity();
        child = activity.child;
    }

    @Test
    public void events() throws InterruptedException {
        RecordingObserver<Object> o = new RecordingObserver<>();

        final AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
        alphaAnimation.setStartOffset(0);
        alphaAnimation.setDuration(100);

        final Subscription subscription = RxAnimation.events(alphaAnimation, child).subscribeOn(AndroidSchedulers.mainThread()).subscribe(o);
        {   // ロードが完了するまで待つ
            waitForLoadingFinished(500);
            // Stack Start event
            assertThat(((AnimationEvent) o.takeNext()).kind() == AnimationEvent.Kind.START);
            // Stack End event
            assertThat(((AnimationEvent) o.takeNext()).kind() == AnimationEvent.Kind.END);
        }
        // Checking complete
        {
            waitForLoadingFinished(4000);
            o.assertOnCompleted();
        }
        subscription.unsubscribe();
        o.assertNoMoreEvents();
    }

    public static void waitForLoadingFinished(int timeout) {
        long t = SystemClock.elapsedRealtime();
        do {
            SystemClock.sleep(100);
        } while (SystemClock.elapsedRealtime() - t < timeout);
    }


}

@@ -1958,17 +1958,1274 @@
 inished(
-4
+2000);%0A            o.assertOnCompleted();%0A        %7D%0A        subscription.unsubscribe();%0A        o.assertNoMoreEvents();%0A    %7D%0A%0A    @Test%0A    public void repeat() throws InterruptedException %7B%0A        RecordingObserver%3CObject%3E o = new RecordingObserver%3C%3E();%0A%0A        final AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);%0A        alphaAnimation.setStartOffset(0);%0A        alphaAnimation.setDuration(100);%0A        alphaAnimation.setRepeatCount(2);%0A        final Subscription subscription = RxAnimation.events(alphaAnimation, child).subscribeOn(AndroidSchedulers.mainThread()).subscribe(o);%0A        %7B   // %E3%83%AD%E3%83%BC%E3%83%89%E3%81%8C%E5%AE%8C%E4%BA%86%E3%81%99%E3%82%8B%E3%81%BE%E3%81%A7%E5%BE%85%E3%81%A4%0A            waitForLoadingFinished(500);%0A            // Stack Start event%0A            assertThat(((AnimationEvent) o.takeNext()).kind() == AnimationEvent.Kind.START);%0A            // Stack Repeat event%0A            assertThat(((AnimationEvent) o.takeNext()).kind() == AnimationEvent.Kind.REPEAT);%0A            // One more repeat%0A            assertThat(((AnimationEvent) o.takeNext()).kind() == AnimationEvent.Kind.REPEAT);%0A            // Stack End event%0A            assertThat(((AnimationEvent) o.takeNext()).kind() == AnimationEvent.Kind.END);%0A        %7D%0A        // Checking complete%0A        %7B%0A            waitForLoadingFinished(2
 000);%0A  

add repeat test