Skip to content
This repository has been archived by the owner on Mar 16, 2021. It is now read-only.

Add manageViewSubscriptions and manageSubscriptions #43

Merged
merged 7 commits into from
Dec 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,18 @@ 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}
*/
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);
addSubscriptions(mPresenterSubscriptions, subscriptions);
}

/**
Expand All @@ -78,11 +75,29 @@ public void manageSubscription(@NonNull final Subscription subscription) {
*
* @throws IllegalStateException when no view is attached
*/
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);
}

/**
* Adds all subscriptions to the given compositeSubscription if not already unsubscribed
*/
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]);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* 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.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

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)
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 testManageSubscription_AfterDestroy_ShouldThrowIllegalState() throws Exception {
mPresenter.create();
mPresenter.destroy();
TestSubscriber<Integer> testSubscriber = new TestSubscriber<>();

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

@Test
public void testManageSubscription_WithAlreadyUnsubscribedSubscription_ShouldDoNothing()
throws Exception {
TestSubscriber<Integer> testSubscriber = new TestSubscriber<>();
testSubscriber.unsubscribe();

mSubscriptionHandler.manageSubscription(testSubscriber);

testSubscriber.assertUnsubscribed();
}

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

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

mPresenter.destroy();
testSubscriber.assertUnsubscribed();
}

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

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

testSubscriber.assertUnsubscribed();
}

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

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

mPresenter.detachView();
testSubscriber.assertUnsubscribed();
}

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

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

testSubscriber.assertUnsubscribed();
}

@Test
public void testManageViewSubscriptions_WithOneAlreadyUnsubscribed_ShouldNotAddToSubscription()
throws Exception {
mPresenter.create();
mPresenter.attachView(mView);
TestSubscriber<Void> firstSubscriber = new TestSubscriber<>();
TestSubscriber<Void> 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<Void> firstSubscriber = new TestSubscriber<>();
TestSubscriber<Void> secondSubscriber = new TestSubscriber<>();
TestSubscriber<Void> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,30 @@
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;

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
Expand Down Expand Up @@ -147,80 +142,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<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();
}
}
}