From de89adb35afb7507005f2f9c5b4ad68cd44622fb Mon Sep 17 00:00:00 2001 From: StefMa Date: Tue, 13 Dec 2016 16:51:16 +0100 Subject: [PATCH 1/7] Add manageViewSubscriptions and manageSubscriptions to handle multiple subscriptions at once --- .../rx/RxTiPresenterSubscriptionHandler.java | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/rx/src/main/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandler.java b/rx/src/main/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandler.java index 7e4cc709..31a869f2 100644 --- a/rx/src/main/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandler.java +++ b/rx/src/main/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandler.java @@ -54,10 +54,11 @@ public void onChange(final TiPresenter.State state, } /** - * Add your subscriptions here and they will automatically unsubscribed when {@link - * TiPresenter#destroy()} gets called + * Add your subscriptions here and they will automatically unsubscribed when + * {@link TiPresenter#destroy()} gets called * * @throws IllegalStateException when the presenter has reached {@link net.grandcentrix.thirtyinch.TiPresenter.State#DESTROYED} + * @see #manageSubscriptions(Subscription...) */ public void manageSubscription(@NonNull final Subscription subscription) { if (mPresenterSubscriptions == null) { @@ -71,12 +72,26 @@ public void manageSubscription(@NonNull final Subscription subscription) { mPresenterSubscriptions.add(subscription); } + /** + * Add your subscriptions here and they will automatically unsubscribed when + * {@link TiPresenter#destroy()} gets called + * + * @throws IllegalStateException when the presenter has reached {@link net.grandcentrix.thirtyinch.TiPresenter.State#DESTROYED} + * @see #manageSubscription(Subscription) + **/ + public void manageSubscriptions(@NonNull final Subscription... subscriptions) { + for (final Subscription subscription : subscriptions) { + manageSubscription(subscription); + } + } + /** * Add your subscriptions for View events to this method to get them automatically cleaned up * in {@link TiPresenter#detachView()}. typically call this in {@link * TiPresenter#attachView(TiView)} where you subscribe to the UI events. * * @throws IllegalStateException when no view is attached + * @see #manageViewSubscriptions(Subscription...) */ public void manageViewSubscription(@NonNull final Subscription subscription) { if (mUiSubscriptions == null) { @@ -85,4 +100,20 @@ public void manageViewSubscription(@NonNull final Subscription subscription) { } mUiSubscriptions.add(subscription); } + + /** + * Add your subscriptions for View events to this method to get them automatically cleaned up + * in {@link TiPresenter#detachView()}. typically call this in {@link + * TiPresenter#attachView(TiView)} where you subscribe to the UI events. + * + * @throws IllegalStateException when no view is attached + * @see #manageViewSubscription(Subscription) + */ + public void manageViewSubscriptions(@NonNull final Subscription... subscriptions) { + for (final Subscription subscription : subscriptions) { + manageViewSubscription(subscription); + } + } + + } From 60f2da65a8489cb1d4bde37febf388b0d1998f1e Mon Sep 17 00:00:00 2001 From: StefMa Date: Wed, 14 Dec 2016 11:07:58 +0100 Subject: [PATCH 2/7] Split RxUtilsTest into two differnt Test classes --- .../RxTiPresenterSubscriptionHandlerTest.java | 120 ++++++++++++++++++ ...sTest.java => RxTiPresenterUtilsTest.java} | 105 +-------------- 2 files changed, 125 insertions(+), 100 deletions(-) create mode 100644 rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandlerTest.java rename rx/src/test/java/net/grandcentrix/thirtyinch/rx/{RxUtilsTest.java => RxTiPresenterUtilsTest.java} (52%) diff --git a/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandlerTest.java b/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandlerTest.java new file mode 100644 index 00000000..44acdd05 --- /dev/null +++ b/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandlerTest.java @@ -0,0 +1,120 @@ +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 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 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 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 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 testSubscriber = new TestSubscriber<>(); + testSubscriber.unsubscribe(); + mSubscriptionHandler.manageSubscription(testSubscriber); + testSubscriber.assertUnsubscribed(); + } + + @Test + public void testManageViewSubscription() throws Exception { + mPresenter.create(); + mPresenter.attachView(mock(TiView.class)); + TestSubscriber testSubscriber = new TestSubscriber<>(); + + mSubscriptionHandler.manageViewSubscription(testSubscriber); + + assertThat(testSubscriber.isUnsubscribed(), equalTo(false)); + + mPresenter.detachView(); + + testSubscriber.assertUnsubscribed(); + } + +} \ No newline at end of file diff --git a/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxUtilsTest.java b/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterUtilsTest.java similarity index 52% rename from rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxUtilsTest.java rename to rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterUtilsTest.java index 6deb7629..9ce07a54 100644 --- a/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxUtilsTest.java +++ b/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterUtilsTest.java @@ -1,26 +1,12 @@ -/* - * Copyright (C) 2016 grandcentrix GmbH - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - 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 java.util.Arrays; import java.util.Collections; @@ -28,25 +14,19 @@ import rx.Observable; 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; -public class RxUtilsTest { +@RunWith(JUnit4.class) +public class RxTiPresenterUtilsTest { 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 @@ -147,80 +127,5 @@ public void testDeliverToViewViewReady() throws Exception { testSubscriber.assertReceivedOnNext(Arrays.asList(1, 2, 3)); } - @Test - public void testDetach() throws Exception { - mPresenter.create(); - mPresenter.attachView(mView); - TestSubscriber 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 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 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 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 testSubscriber = new TestSubscriber<>(); - testSubscriber.unsubscribe(); - mSubscriptionHandler.manageSubscription(testSubscriber); - testSubscriber.assertUnsubscribed(); - } - - @Test - public void testManageViewSubscription() throws Exception { - mPresenter.create(); - mPresenter.attachView(mock(TiView.class)); - TestSubscriber testSubscriber = new TestSubscriber<>(); - - mSubscriptionHandler.manageViewSubscription(testSubscriber); - - assertThat(testSubscriber.isUnsubscribed(), equalTo(false)); - - mPresenter.detachView(); - - testSubscriber.assertUnsubscribed(); - } -} +} \ No newline at end of file From 0ce42d11c24a8e914cd17393e00e57f078e500dc Mon Sep 17 00:00:00 2001 From: StefMa Date: Wed, 14 Dec 2016 11:30:37 +0100 Subject: [PATCH 3/7] Rename tests --- .../RxTiPresenterSubscriptionHandlerTest.java | 57 ++++++++----------- 1 file changed, 23 insertions(+), 34 deletions(-) diff --git a/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandlerTest.java b/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandlerTest.java index 44acdd05..dadedd29 100644 --- a/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandlerTest.java +++ b/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandlerTest.java @@ -3,7 +3,6 @@ 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; @@ -11,10 +10,8 @@ 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) @@ -40,80 +37,72 @@ public void tearDown() throws Exception { mSubscriptionHandler = null; } - @Test - public void testDetach() throws Exception { + @Test(expected = AssertionError.class) + public void tesManageViewSubscription_DetachBeforeAttach_ShouldThrowAssertError() + throws Exception { mPresenter.create(); - mPresenter.attachView(mView); TestSubscriber 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 { + @Test(expected = IllegalStateException.class) + public void testManageSubscription_AfterDestroy_ShouldThrowIllegalState() throws Exception { mPresenter.create(); + mPresenter.destroy(); TestSubscriber testSubscriber = new TestSubscriber<>(); - mSubscriptionHandler.manageViewSubscription(testSubscriber); - mPresenter.detachView(); + mSubscriptionHandler.manageSubscription(testSubscriber); + } - assertThat(testSubscriber.isUnsubscribed(), equalTo(false)); - assertThat(mPresenter.onDetachCalled, equalTo(0)); + @Test + public void testManageSubscription_WithAlreadyUnsubscribedSubscription_ShouldDoNothing() + throws Exception { + TestSubscriber testSubscriber = new TestSubscriber<>(); + testSubscriber.unsubscribe(); + + mSubscriptionHandler.manageSubscription(testSubscriber); + + testSubscriber.assertUnsubscribed(); } @Test - public void testManageSubscription() throws Exception { + public void testManageSubscription_WithDestroy_ShouldUnsubscribe() throws Exception { mPresenter.create(); TestSubscriber testSubscriber = new TestSubscriber<>(); mSubscriptionHandler.manageSubscription(testSubscriber); - assertThat(testSubscriber.isUnsubscribed(), equalTo(false)); mPresenter.destroy(); - testSubscriber.assertUnsubscribed(); } @Test - public void testManageSubscriptionDestroyed() throws Exception { + public void testManageViewSubscription_WithDetachView_ShouldUnsubscribe() throws Exception { mPresenter.create(); - mPresenter.destroy(); + mPresenter.attachView(mView); TestSubscriber testSubscriber = new TestSubscriber<>(); - try { - mSubscriptionHandler.manageSubscription(testSubscriber); - Assert.fail("no exception"); - } catch (IllegalStateException e) { - assertThat(e.getMessage(), containsString("DESTROYED")); - } - } + mSubscriptionHandler.manageViewSubscription(testSubscriber); + mPresenter.detachView(); - @Test - public void testManageSubscriptionUnsubscribed() throws Exception { - TestSubscriber testSubscriber = new TestSubscriber<>(); - testSubscriber.unsubscribe(); - mSubscriptionHandler.manageSubscription(testSubscriber); testSubscriber.assertUnsubscribed(); } @Test - public void testManageViewSubscription() throws Exception { + public void testManageViewSubscription_WithDetach_ShouldUnsubscribe() throws Exception { mPresenter.create(); mPresenter.attachView(mock(TiView.class)); TestSubscriber testSubscriber = new TestSubscriber<>(); mSubscriptionHandler.manageViewSubscription(testSubscriber); - assertThat(testSubscriber.isUnsubscribed(), equalTo(false)); mPresenter.detachView(); - testSubscriber.assertUnsubscribed(); } From deffb38c92f3cdea062cf1f69a0d7faeb45993b3 Mon Sep 17 00:00:00 2001 From: StefMa Date: Wed, 14 Dec 2016 15:27:30 +0100 Subject: [PATCH 4/7] Add test and merge methods --- .../rx/RxTiPresenterSubscriptionHandler.java | 48 +++++++------------ .../RxTiPresenterSubscriptionHandlerTest.java | 47 +++++++++++++++--- 2 files changed, 57 insertions(+), 38 deletions(-) diff --git a/rx/src/main/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandler.java b/rx/src/main/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandler.java index 31a869f2..4db75945 100644 --- a/rx/src/main/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandler.java +++ b/rx/src/main/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandler.java @@ -58,31 +58,14 @@ public void onChange(final TiPresenter.State state, * {@link TiPresenter#destroy()} gets called * * @throws IllegalStateException when the presenter has reached {@link net.grandcentrix.thirtyinch.TiPresenter.State#DESTROYED} - * @see #manageSubscriptions(Subscription...) */ - public void manageSubscription(@NonNull final Subscription subscription) { + public void manageSubscription(@NonNull final Subscription... subscriptions) { if (mPresenterSubscriptions == null) { throw new IllegalStateException("subscription handling doesn't work" + " when the presenter has reached the DESTROYED state"); } - if (subscription.isUnsubscribed()) { - return; - } - mPresenterSubscriptions.add(subscription); - } - - /** - * Add your subscriptions here and they will automatically unsubscribed when - * {@link TiPresenter#destroy()} gets called - * - * @throws IllegalStateException when the presenter has reached {@link net.grandcentrix.thirtyinch.TiPresenter.State#DESTROYED} - * @see #manageSubscription(Subscription) - **/ - public void manageSubscriptions(@NonNull final Subscription... subscriptions) { - for (final Subscription subscription : subscriptions) { - manageSubscription(subscription); - } + addSubscriptions(mPresenterSubscriptions, subscriptions); } /** @@ -91,29 +74,30 @@ public void manageSubscriptions(@NonNull final Subscription... subscriptions) { * TiPresenter#attachView(TiView)} where you subscribe to the UI events. * * @throws IllegalStateException when no view is attached - * @see #manageViewSubscriptions(Subscription...) */ - public void manageViewSubscription(@NonNull final Subscription subscription) { + public void manageViewSubscription(@NonNull final Subscription... subscriptions) { if (mUiSubscriptions == null) { throw new IllegalStateException("view subscriptions can't be handled" + " when there is no view"); } - mUiSubscriptions.add(subscription); + + addSubscriptions(mUiSubscriptions, subscriptions); } /** - * Add your subscriptions for View events to this method to get them automatically cleaned up - * in {@link TiPresenter#detachView()}. typically call this in {@link - * TiPresenter#attachView(TiView)} where you subscribe to the UI events. - * - * @throws IllegalStateException when no view is attached - * @see #manageViewSubscription(Subscription) + * Adds all subscriptions to the given compositeSubscription if not already unsubscribed */ - public void manageViewSubscriptions(@NonNull final Subscription... subscriptions) { - for (final Subscription subscription : subscriptions) { - manageViewSubscription(subscription); + private static void addSubscriptions(final CompositeSubscription compositeSubscription, + final Subscription... subscriptions) { + //noinspection ForLoopReplaceableByForEach + for (int i = 0; i < subscriptions.length; i++) { + final Subscription subscription = subscriptions[i]; + if (subscription.isUnsubscribed()) { + continue; + } + + compositeSubscription.add(subscriptions[i]); } } - } diff --git a/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandlerTest.java b/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandlerTest.java index dadedd29..d46e5a2a 100644 --- a/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandlerTest.java +++ b/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandlerTest.java @@ -82,28 +82,63 @@ public void testManageSubscription_WithDestroy_ShouldUnsubscribe() throws Except } @Test - public void testManageViewSubscription_WithDetachView_ShouldUnsubscribe() throws Exception { + public void testManageViewSubscription_WithDetachSingleSub_ShouldUnsubscribe() + throws Exception { mPresenter.create(); mPresenter.attachView(mView); TestSubscriber testSubscriber = new TestSubscriber<>(); mSubscriptionHandler.manageViewSubscription(testSubscriber); - mPresenter.detachView(); + assertThat(testSubscriber.isUnsubscribed(), equalTo(false)); + mPresenter.detachView(); testSubscriber.assertUnsubscribed(); } @Test - public void testManageViewSubscription_WithDetach_ShouldUnsubscribe() throws Exception { + public void testManageViewSubscription_WithDetachView_ShouldUnsubscribe() throws Exception { mPresenter.create(); - mPresenter.attachView(mock(TiView.class)); + mPresenter.attachView(mView); TestSubscriber testSubscriber = new TestSubscriber<>(); mSubscriptionHandler.manageViewSubscription(testSubscriber); - assertThat(testSubscriber.isUnsubscribed(), equalTo(false)); - mPresenter.detachView(); + testSubscriber.assertUnsubscribed(); } + @Test + public void testManageViewSubscriptions_WithOneAlreadyUnsubscribed_ShouldNotAddToSubscription() + throws Exception { + mPresenter.create(); + mPresenter.attachView(mView); + TestSubscriber firstSubscriber = new TestSubscriber<>(); + TestSubscriber secondSubscriber = new TestSubscriber<>(); + secondSubscriber.unsubscribe(); + + mSubscriptionHandler.manageViewSubscription(firstSubscriber, secondSubscriber); + + assertThat(firstSubscriber.isUnsubscribed(), equalTo(false)); + secondSubscriber.assertUnsubscribed(); + } + + @Test + public void testManagerViewSubscriptions_WithDetach_ShouldUnsubcribe() throws Exception { + mPresenter.create(); + mPresenter.attachView(mView); + TestSubscriber firstSubscriber = new TestSubscriber<>(); + TestSubscriber secondSubscriber = new TestSubscriber<>(); + TestSubscriber thirdSubscriber = new TestSubscriber<>(); + + mSubscriptionHandler + .manageViewSubscription(firstSubscriber, secondSubscriber, thirdSubscriber); + assertThat(firstSubscriber.isUnsubscribed(), equalTo(false)); + assertThat(secondSubscriber.isUnsubscribed(), equalTo(false)); + assertThat(thirdSubscriber.isUnsubscribed(), equalTo(false)); + + mPresenter.detachView(); + firstSubscriber.assertUnsubscribed(); + secondSubscriber.assertUnsubscribed(); + thirdSubscriber.assertUnsubscribed(); + } } \ No newline at end of file From b6b11abf52f770117d1189cfc429f2c11b115767 Mon Sep 17 00:00:00 2001 From: StefMa Date: Wed, 14 Dec 2016 18:00:46 +0100 Subject: [PATCH 5/7] Add copyright header --- .../rx/RxTiPresenterSubscriptionHandlerTest.java | 15 +++++++++++++++ .../thirtyinch/rx/RxTiPresenterUtilsTest.java | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandlerTest.java b/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandlerTest.java index d46e5a2a..1b43cf74 100644 --- a/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandlerTest.java +++ b/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandlerTest.java @@ -1,3 +1,18 @@ +/* + * Copyright (C) 2016 grandcentrix GmbH + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package net.grandcentrix.thirtyinch.rx; import net.grandcentrix.thirtyinch.TiView; diff --git a/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterUtilsTest.java b/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterUtilsTest.java index 9ce07a54..4590d4c5 100644 --- a/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterUtilsTest.java +++ b/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterUtilsTest.java @@ -1,3 +1,18 @@ +/* + * Copyright (C) 2016 grandcentrix GmbH + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package net.grandcentrix.thirtyinch.rx; import net.grandcentrix.thirtyinch.TiView; From 95218fa11b58dcc3b514d24d5989cd2bb6bcd160 Mon Sep 17 00:00:00 2001 From: StefMa Date: Thu, 15 Dec 2016 11:31:22 +0100 Subject: [PATCH 6/7] Fix Typo --- .../RxTiPresenterSubscriptionHandlerTest.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandlerTest.java b/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandlerTest.java index 1b43cf74..e2febc9e 100644 --- a/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandlerTest.java +++ b/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandlerTest.java @@ -52,18 +52,6 @@ public void tearDown() throws Exception { mSubscriptionHandler = null; } - @Test(expected = AssertionError.class) - public void tesManageViewSubscription_DetachBeforeAttach_ShouldThrowAssertError() - throws Exception { - mPresenter.create(); - TestSubscriber testSubscriber = new TestSubscriber<>(); - - mSubscriptionHandler.manageViewSubscription(testSubscriber); - mPresenter.detachView(); - - testSubscriber.assertUnsubscribed(); - } - @Test(expected = IllegalStateException.class) public void testManageSubscription_AfterDestroy_ShouldThrowIllegalState() throws Exception { mPresenter.create(); @@ -96,6 +84,18 @@ public void testManageSubscription_WithDestroy_ShouldUnsubscribe() throws Except testSubscriber.assertUnsubscribed(); } + @Test(expected = AssertionError.class) + public void testManageViewSubscription_DetachBeforeAttach_ShouldThrowAssertError() + throws Exception { + mPresenter.create(); + TestSubscriber testSubscriber = new TestSubscriber<>(); + + mSubscriptionHandler.manageViewSubscription(testSubscriber); + mPresenter.detachView(); + + testSubscriber.assertUnsubscribed(); + } + @Test public void testManageViewSubscription_WithDetachSingleSub_ShouldUnsubscribe() throws Exception { From dd0f620d9092e779f1500e22e08e944542520ef5 Mon Sep 17 00:00:00 2001 From: StefMa Date: Thu, 15 Dec 2016 11:34:08 +0100 Subject: [PATCH 7/7] Check exception message --- .../rx/RxTiPresenterSubscriptionHandlerTest.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandlerTest.java b/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandlerTest.java index e2febc9e..8e5076e0 100644 --- a/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandlerTest.java +++ b/rx/src/test/java/net/grandcentrix/thirtyinch/rx/RxTiPresenterSubscriptionHandlerTest.java @@ -25,8 +25,10 @@ import rx.observers.TestSubscriber; +import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; @RunWith(JUnit4.class) @@ -52,13 +54,18 @@ public void tearDown() throws Exception { mSubscriptionHandler = null; } - @Test(expected = IllegalStateException.class) + @Test public void testManageSubscription_AfterDestroy_ShouldThrowIllegalState() throws Exception { mPresenter.create(); mPresenter.destroy(); TestSubscriber testSubscriber = new TestSubscriber<>(); - mSubscriptionHandler.manageSubscription(testSubscriber); + try { + mSubscriptionHandler.manageSubscription(testSubscriber); + fail("no exception"); + } catch (IllegalStateException e) { + assertThat(e.getMessage(), containsString("DESTROYED")); + } } @Test