package net.grandcentrix.thirtyinch.rx;

import net.grandcentrix.thirtyinch.TiView;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import rx.observers.TestSubscriber;

import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.StringContains.containsString;
import static org.mockito.Mockito.mock;

@RunWith(JUnit4.class)
public class RxTiPresenterSubscriptionHandlerTest {

    private TiMockPresenter mPresenter;

    private RxTiPresenterSubscriptionHandler mSubscriptionHandler;

    private TiView mView;

    @Before
    public void setUp() throws Exception {
        mView = mock(TiView.class);
        mPresenter = new TiMockPresenter();
        mSubscriptionHandler = new RxTiPresenterSubscriptionHandler(mPresenter);
    }

    @After
    public void tearDown() throws Exception {
        mPresenter = null;
        mView = null;
        mSubscriptionHandler = null;
    }

    @Test
    public void testDetach() throws Exception {
        mPresenter.create();
        mPresenter.attachView(mView);
        TestSubscriber<Integer> testSubscriber = new TestSubscriber<>();

        mSubscriptionHandler.manageViewSubscription(testSubscriber);
        mPresenter.detachView();

        testSubscriber.assertUnsubscribed();
        assertThat(mPresenter.getView(), nullValue());
        assertThat(mPresenter.onDetachCalled, equalTo(1));
    }

    @Test
    public void testDetachBeforeAttach() throws Exception {
        mPresenter.create();
        TestSubscriber<Integer> testSubscriber = new TestSubscriber<>();

        mSubscriptionHandler.manageViewSubscription(testSubscriber);
        mPresenter.detachView();

        assertThat(testSubscriber.isUnsubscribed(), equalTo(false));
        assertThat(mPresenter.onDetachCalled, equalTo(0));
    }

    @Test
    public void testManageSubscription() throws Exception {
        mPresenter.create();
        TestSubscriber<Integer> testSubscriber = new TestSubscriber<>();

        mSubscriptionHandler.manageSubscription(testSubscriber);

        assertThat(testSubscriber.isUnsubscribed(), equalTo(false));

        mPresenter.destroy();

        testSubscriber.assertUnsubscribed();
    }

    @Test
    public void testManageSubscriptionDestroyed() throws Exception {
        mPresenter.create();
        mPresenter.destroy();
        TestSubscriber<Integer> testSubscriber = new TestSubscriber<>();

        try {
            mSubscriptionHandler.manageSubscription(testSubscriber);
            Assert.fail("no exception");
        } catch (IllegalStateException e) {
            assertThat(e.getMessage(), containsString("DESTROYED"));
        }
    }

    @Test
    public void testManageSubscriptionUnsubscribed() throws Exception {
        TestSubscriber<Integer> testSubscriber = new TestSubscriber<>();
        testSubscriber.unsubscribe();
        mSubscriptionHandler.manageSubscription(testSubscriber);
        testSubscriber.assertUnsubscribed();
    }

    @Test
    public void testManageViewSubscription() throws Exception {
        mPresenter.create();
        mPresenter.attachView(mock(TiView.class));
        TestSubscriber<Integer> testSubscriber = new TestSubscriber<>();

        mSubscriptionHandler.manageViewSubscription(testSubscriber);

        assertThat(testSubscriber.isUnsubscribed(), equalTo(false));

        mPresenter.detachView();

        testSubscriber.assertUnsubscribed();
    }

}
@@ -106,33 +106,8 @@
 er;%0A
-import org.junit.Assert;%0A
 impo
@@ -258,59 +258,8 @@
 r;%0A%0A
-import static org.hamcrest.CoreMatchers.nullValue;%0A
 impo
@@ -360,71 +360,8 @@
 To;%0A
-import static org.hamcrest.core.StringContains.containsString;%0A
 impo
@@ -995,37 +995,139 @@
 Test
-%0A    public void testDetach()
+(expected = AssertionError.class)%0A    public void tesManageViewSubscription_DetachBeforeAttach_ShouldThrowAssertError()%0A           
  thr
@@ -1175,46 +1175,8 @@
 ();%0A
-        mPresenter.attachView(mView);%0A
     
@@ -1401,173 +1401,136 @@
     
+%7D%0A%0A
     
-assertThat(mPresenter.getView(), nullValue());%0A        assertThat(mPresenter.onDetachCalled, equalTo(1));%0A    %7D%0A%0A    @Test%0A    public void testDetachBeforeAttach
+@Test(expected = IllegalStateException.class)%0A    public void testManageSubscription_AfterDestroy_ShouldThrowIllegalState
 () t
@@ -1568,32 +1568,62 @@
 enter.create();%0A
+        mPresenter.destroy();%0A
         TestSubs
@@ -1707,36 +1707,32 @@
 onHandler.manage
-View
 Subscription(tes
@@ -1753,57 +1753,226 @@
     
+%7D%0A%0A
     
-mPresenter.detachView();%0A%0A        assertThat(
+@Test%0A    public void testManageSubscription_WithAlreadyUnsubscribedSubscription_ShouldDoNothing()%0A            throws Exception %7B%0A        TestSubscriber%3CInteger%3E testSubscriber = new TestSubscriber%3C%3E();%0A        
 test
@@ -1974,35 +1974,33 @@
  testSubscriber.
-isU
+u
 nsubscribed(), e
@@ -1997,38 +1997,87 @@
 ribe
-d
 ()
-, equalTo(false)
+;%0A%0A        mSubscriptionHandler.manageSubscription(testSubscriber
 );%0A
+%0A
         
 asse
@@ -2076,56 +2076,42 @@
     
-assertThat(mPresenter.onDetachCalled, equalTo(0)
+testSubscriber.assertUnsubscribed(
 );%0A 
@@ -2156,32 +2156,62 @@
 nageSubscription
+_WithDestroy_ShouldUnsubscribe
 () throws Except
@@ -2376,33 +2376,32 @@
 estSubscriber);%0A
-%0A
         assertTh
@@ -2480,25 +2480,24 @@
 .destroy();%0A
-%0A
         test
@@ -2564,32 +2564,36 @@
  void testManage
+View
 SubscriptionDest
@@ -2592,17 +2592,41 @@
 tion
-Destroyed
+_WithDetachView_ShouldUnsubscribe
 () t
@@ -2687,32 +2687,40 @@
  mPresenter.
-destroy(
+attachView(mView
 );%0A        T
@@ -2788,26 +2788,8 @@
 );%0A%0A
-        try %7B%0A    
     
@@ -2811,32 +2811,36 @@
 onHandler.manage
+View
 Subscription(tes
@@ -2865,428 +2865,33 @@
     
-  
-  Assert.fail(%22no exception%22);%0A        %7D catch (IllegalStateException e) %7B%0A            assertThat(e.getMessage(), containsString(%22DESTROYED%22));%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void testManageSubscriptionUnsubscribed() throws Exception %7B%0A        TestSubscriber%3CInteger%3E testSubscriber = new TestSubscriber%3C%3E();%0A        testSubscriber.unsubscribe();%0A        mSubscriptionHandler.manageSubscription(testSubscriber);
+mPresenter.detachView();%0A
 %0A   
@@ -2991,16 +2991,45 @@
 cription
+_WithDetach_ShouldUnsubscribe
 () throw
@@ -3257,33 +3257,32 @@
 estSubscriber);%0A
-%0A
         assertTh
@@ -3360,33 +3360,32 @@
 r.detachView();%0A
-%0A
         testSubs

Rename tests