From 8010bca07f4b99e55248b3fbfcd89277064daee1 Mon Sep 17 00:00:00 2001 From: Robert Berghegger Date: Wed, 26 Apr 2017 14:22:25 +0200 Subject: [PATCH 1/2] Adds a getViewOrThrow() method to the TiPresenter which either returns the attached view or throws an IllegalStateException. --- .../grandcentrix/thirtyinch/TiPresenter.java | 22 ++++++++++++- .../thirtyinch/TiPresenterTest.java | 33 ++++++++++++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/thirtyinch/src/main/java/net/grandcentrix/thirtyinch/TiPresenter.java b/thirtyinch/src/main/java/net/grandcentrix/thirtyinch/TiPresenter.java index 7b8d90b3..a8953c8e 100644 --- a/thirtyinch/src/main/java/net/grandcentrix/thirtyinch/TiPresenter.java +++ b/thirtyinch/src/main/java/net/grandcentrix/thirtyinch/TiPresenter.java @@ -300,7 +300,7 @@ public State getState() { } /** - * Returns the currently attached view. The view is attached between the lifecycle callbacks + * Gets the currently attached view. The view is attached between the lifecycle callbacks * {@link #onAttachView(TiView)} and {@link #onSleep()}. *

* If you don't care about the view being attached or detached you should either rethink your @@ -314,6 +314,26 @@ public V getView() { return mView; } + /** + * Gets the currently attached view or throws an {@link IllegalStateException} if the view + * is not attached. If you want to invoke methods on your view even when the view isn't + * currently attached or your can't be sure that the view is currently attached you should + * better use {@link #sendToView(ViewAction)} where the action will be executed when the view is + * attached. + * + * @return the currently attached view of this presenter + */ + @NonNull + public V getViewOrThrow() { + + final V view = getView(); + if (view == null) { + throw new IllegalStateException("view is not attached"); + } + + return view; + } + public boolean isDestroyed() { return mState == State.DESTROYED; } diff --git a/thirtyinch/src/test/java/net/grandcentrix/thirtyinch/TiPresenterTest.java b/thirtyinch/src/test/java/net/grandcentrix/thirtyinch/TiPresenterTest.java index e2e42668..999f5939 100644 --- a/thirtyinch/src/test/java/net/grandcentrix/thirtyinch/TiPresenterTest.java +++ b/thirtyinch/src/test/java/net/grandcentrix/thirtyinch/TiPresenterTest.java @@ -29,8 +29,10 @@ import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertNotSame; +import static junit.framework.Assert.assertNull; import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.fail; +import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.MatcherAssert.assertThat; @@ -273,7 +275,36 @@ protected void onDestroy() { } @Test - public void testGetView() throws Exception { + public void testGetViewOrThrow() { + mPresenter.create(); + mPresenter.attachView(mView); + mPresenter.detachView(); + + try { + mPresenter.getViewOrThrow(); + failBecauseExceptionWasNotThrown(IllegalStateException.class); + } catch (IllegalStateException e) { + assertThat(e.getMessage(), equalTo("view is not attached")); + } + } + + @Test + public void testGetViewOrThrowReturnsView() { + mPresenter.create(); + mPresenter.attachView(mView); + assertThat(mPresenter.getViewOrThrow(), equalTo(mView)); + } + + @Test + public void testGetViewReturnsNull() { + mPresenter.create(); + mPresenter.attachView(mView); + mPresenter.detachView(); + assertNull(mPresenter.getView()); + } + + @Test + public void testGetViewReturnsView() { mPresenter.create(); mPresenter.attachView(mView); assertThat(mPresenter.getView(), equalTo(mView)); From 62a047110a2def29275287f256d9415e9a9ecf8b Mon Sep 17 00:00:00 2001 From: Robert Berghegger Date: Wed, 26 Apr 2017 15:44:08 +0200 Subject: [PATCH 2/2] Applies suggestions from pull request. --- .../java/net/grandcentrix/thirtyinch/TiPresenter.java | 11 +++++------ .../net/grandcentrix/thirtyinch/TiPresenterTest.java | 3 ++- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/thirtyinch/src/main/java/net/grandcentrix/thirtyinch/TiPresenter.java b/thirtyinch/src/main/java/net/grandcentrix/thirtyinch/TiPresenter.java index a8953c8e..e702e92e 100644 --- a/thirtyinch/src/main/java/net/grandcentrix/thirtyinch/TiPresenter.java +++ b/thirtyinch/src/main/java/net/grandcentrix/thirtyinch/TiPresenter.java @@ -316,19 +316,18 @@ public V getView() { /** * Gets the currently attached view or throws an {@link IllegalStateException} if the view - * is not attached. If you want to invoke methods on your view even when the view isn't - * currently attached or your can't be sure that the view is currently attached you should - * better use {@link #sendToView(ViewAction)} where the action will be executed when the view is - * attached. + * is not attached. Use this method if you are sure that a view is currently attached to the + * presenter. If you're not sure you should better use {@link #sendToView(ViewAction)} where the + * action will be executed when the view is attached. * * @return the currently attached view of this presenter */ @NonNull public V getViewOrThrow() { - final V view = getView(); if (view == null) { - throw new IllegalStateException("view is not attached"); + throw new IllegalStateException( + "The view is currently not attached. Use 'sendToView(ViewAction)' instead."); } return view; diff --git a/thirtyinch/src/test/java/net/grandcentrix/thirtyinch/TiPresenterTest.java b/thirtyinch/src/test/java/net/grandcentrix/thirtyinch/TiPresenterTest.java index 999f5939..e121e654 100644 --- a/thirtyinch/src/test/java/net/grandcentrix/thirtyinch/TiPresenterTest.java +++ b/thirtyinch/src/test/java/net/grandcentrix/thirtyinch/TiPresenterTest.java @@ -284,7 +284,8 @@ public void testGetViewOrThrow() { mPresenter.getViewOrThrow(); failBecauseExceptionWasNotThrown(IllegalStateException.class); } catch (IllegalStateException e) { - assertThat(e.getMessage(), equalTo("view is not attached")); + assertThat(e.getMessage(), + equalTo("The view is currently not attached. Use 'sendToView(ViewAction)' instead.")); } }