Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to make @InjectViewState optional #76

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 @@ -5,6 +5,59 @@ import org.junit.Test

class ViewStateNegativeTestKt : CompilerTest() {

//TODO: can't test it for kotlin sources
@Test
fun successWithoutInjectViewStateOnPresenter() {
@Language("JAVA") val presenter = """
import moxy.InjectViewState;
import moxy.MvpPresenter;
import moxy.view.TestView;

public class WithoutInjectViewStatePresenter extends MvpPresenter<TestView> {

}
""".toJavaFile()

val compilation = compileSourcesWithProcessor(presenter)
compilation.assertSucceededWithoutWarnings()
}

@Test
fun checkWithoutInjectViewState() {
@Language("JAVA") val presenter = """
import moxy.MvpPresenter;

public class WithoutInjectViewStatePresenter extends MvpPresenter<EmptyView> {

}
""".toJavaFile()

@Language("JAVA") val view = """
import moxy.MvpView;
import moxy.viewstate.strategy.AddToEndSingleStrategy;
import moxy.viewstate.strategy.StateStrategyType;

@StateStrategyType(AddToEndSingleStrategy.class)
public interface EmptyView extends MvpView {
}

""".toJavaFile()

val expected = """
import moxy.viewstate.MvpViewState;

public class EmptyView${"$$"}State extends MvpViewState<EmptyView> implements EmptyView {
}
""".toJavaFile()

val presenterCompilation = compileSourcesWithProcessor(presenter, view)
val viewStateCompilation = compileSources(view, expected)

assertExceptedFilesGenerated(
presenterCompilation.generatedFiles(),
viewStateCompilation.generatedFiles())
}

@Test
fun errorIfInjectViewStateNotOnPresenter() {
@Language("JAVA") val presenter = """
Expand Down
2 changes: 2 additions & 0 deletions moxy/src/main/java/moxy/InjectViewState.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package moxy;

import java.lang.annotation.Inherited;
import java.lang.annotation.Target;
import moxy.viewstate.MvpViewState;

Expand All @@ -13,6 +14,7 @@
* the code that would break your app).
*/
@Target(value = TYPE)
@Inherited
public @interface InjectViewState {

Class<? extends MvpViewState> value() default DefaultViewState.class;
Expand Down
1 change: 1 addition & 0 deletions moxy/src/main/java/moxy/MvpPresenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import moxy.locators.ViewStateLocator;
import moxy.viewstate.MvpViewState;

@InjectViewState
public abstract class MvpPresenter<View extends MvpView> {

private boolean isFirstLaunch = true;
Expand Down
11 changes: 5 additions & 6 deletions moxy/src/test/java/moxy/tests/LocalPresenterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mock;

Expand All @@ -19,7 +18,6 @@

import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;

Expand Down Expand Up @@ -76,14 +74,15 @@ public void checkWithInjectViewState() {
}

@Test
public void checkWithoutViewState() {
NoViewStatePresenter noViewStatePresenter = new NoViewStatePresenter();
noViewStatePresenter.attachView(mTestView);
public void checkWithoutInjectViewState() {
NoViewStatePresenter presenter = new NoViewStatePresenter();
presenter.attachView(mTestView);
try {
Field mViewState = MvpPresenter.class.getDeclaredField("viewState");

mViewState.setAccessible(true);
assertNull("ViewState is not null for NoViewStatePresenter", mViewState.get(noViewStatePresenter));
assertNotNull("ViewState is null for NoViewStatePresenter",
mViewState.get(presenter));
} catch (IllegalAccessException | NoSuchFieldException e) {
fail(e.getLocalizedMessage());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package example.com.androidxsample.view

import android.util.Log
import example.com.androidxsample.view.Logger.Companion.MOXY_TAG
import moxy.InjectViewState
import moxy.MvpPresenter

@InjectViewState
class MainPresenter constructor(
private val logger: Logger
) : MvpPresenter<MainView>() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package example.com.androidxsample.view.ktx

import example.com.androidxsample.view.Logger
import moxy.InjectViewState
import moxy.MvpPresenter

@InjectViewState
class KtxPresenter constructor(
private val logger: Logger
) : MvpPresenter<KtxView>() {
Expand Down