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

Add getViewOrThrow() for convenience #87

Merged
merged 2 commits into from
Apr 26, 2017
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 @@ -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()}.
* <p>
* If you don't care about the view being attached or detached you should either rethink your
Expand All @@ -314,6 +314,25 @@ public V getView() {
return mView;
}

/**
* Gets the currently attached view or throws an {@link IllegalStateException} if the view
* 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(
"The view is currently not attached. Use 'sendToView(ViewAction)' instead.");
}

return view;
}

public boolean isDestroyed() {
return mState == State.DESTROYED;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -273,7 +275,37 @@ 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("The view is currently not attached. Use 'sendToView(ViewAction)' instead."));
}
}

@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));
Expand Down