package io.demor.nuts.test.controller;

import android.test.AndroidTestCase;

import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import io.demor.nuts.lib.controller.ControllerCallback;
import io.demor.nuts.lib.controller.ProxyInvokeHandler;
import io.demor.nuts.lib.controller.TimeoutListener;

public class ControllerTimeoutTestCase extends AndroidTestCase {

    TestController mController = new ProxyInvokeHandler<>(TestController.IMPL).createProxy();

    public void testStopCallback() throws Exception {

        final CountDownLatch latch = new CountDownLatch(1);

        mController.sleep(5)
                .setTimeout(1, TimeUnit.SECONDS, new TimeoutListener() {
                    @Override
                    public boolean onTimeout(final Date startTime, final Date stopTime) {
                        latch.countDown();
                        return false;
                    }
                })
                .asyncUI(new ControllerCallback<Void>() {
                    @Override
                    public void onResult(final Void aVoid) {
                        latch.countDown();
                    }
                });

        latch.await();
        assertEquals(0, latch.getCount());
    }

    public void testContinueCallback() throws Exception {

        final CountDownLatch latch = new CountDownLatch(2);

        mController.sleep(5)
                .setTimeout(1, TimeUnit.SECONDS, new TimeoutListener() {
                    @Override
                    public boolean onTimeout(final Date startTime, final Date stopTime) {
                        latch.countDown();
                        return true;
                    }
                })
                .asyncUI(new ControllerCallback<Void>() {
                    @Override
                    public void onResult(final Void aVoid) {
                        latch.countDown();
                    }
                });

        latch.await(10, TimeUnit.SECONDS);
        assertEquals(0, latch.getCount());

    }
}

@@ -2056,16 +2056,797 @@
 unt());%0A
+    %7D%0A%0A    public void testNoTimeout() throws Exception %7B%0A        final CountDownLatch latch = new CountDownLatch(1);%0A%0A        mController.sleep(1)%0A                .setTimeout(5, TimeUnit.SECONDS, new TimeoutListener() %7B%0A                    @Override%0A                    public boolean onTimeout(final Date startTime, final Date stopTime) %7B%0A                        latch.countDown();%0A                        return true;%0A                    %7D%0A                %7D)%0A                .asyncUI(new ControllerCallback%3CVoid%3E() %7B%0A                    @Override%0A                    public void onResult(final Void aVoid) %7B%0A                        latch.countDown();%0A                    %7D%0A                %7D);%0A%0A        latch.await(3, TimeUnit.SECONDS);%0A        assertEquals(0, latch.getCount());
 %0A    %7D%0A%7D

add timeout test case