From feb85b3a1e41380888d3b01ff25bb14cdd736ab1 Mon Sep 17 00:00:00 2001 From: sockeqwe Date: Mon, 9 Apr 2018 19:19:41 +0200 Subject: [PATCH] =?UTF-8?q?ActivityMviDelegateImpl=20now=20checks=20if=20p?= =?UTF-8?q?resenter=20!=3D=20null=20if=20Activity.finish()=20is=20called?= =?UTF-8?q?=20in=20Activity.onCreate()=20=20#290=C2=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MviFinishInOnCreateActivityTest.java | 42 +++++++++++++++++++ .../MviFinishInOnStartActivityTest.java | 37 ++++++++++++++++ .../src/main/AndroidManifest.xml | 2 + .../activity/MviFinishInOnCreateActivity.java | 36 ++++++++++++++++ .../activity/MviFinishInOnStartActivity.java | 36 ++++++++++++++++ .../mosby3/ActivityMviDelegateImpl.java | 17 ++++---- 6 files changed, 162 insertions(+), 8 deletions(-) create mode 100644 mvi-integration-test/src/androidTest/java/com/hannesdorfmann/mosby3/mvi/integrationtest/lifecycle/activity/MviFinishInOnCreateActivityTest.java create mode 100644 mvi-integration-test/src/androidTest/java/com/hannesdorfmann/mosby3/mvi/integrationtest/lifecycle/activity/MviFinishInOnStartActivityTest.java create mode 100644 mvi-integration-test/src/main/java/com/hannesdorfmann/mosby3/mvi/integrationtest/lifecycle/activity/MviFinishInOnCreateActivity.java create mode 100644 mvi-integration-test/src/main/java/com/hannesdorfmann/mosby3/mvi/integrationtest/lifecycle/activity/MviFinishInOnStartActivity.java diff --git a/mvi-integration-test/src/androidTest/java/com/hannesdorfmann/mosby3/mvi/integrationtest/lifecycle/activity/MviFinishInOnCreateActivityTest.java b/mvi-integration-test/src/androidTest/java/com/hannesdorfmann/mosby3/mvi/integrationtest/lifecycle/activity/MviFinishInOnCreateActivityTest.java new file mode 100644 index 00000000..a8c0b6cd --- /dev/null +++ b/mvi-integration-test/src/androidTest/java/com/hannesdorfmann/mosby3/mvi/integrationtest/lifecycle/activity/MviFinishInOnCreateActivityTest.java @@ -0,0 +1,42 @@ +/* + * Copyright 2016 Hannes Dorfmann. + * + * 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 com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.activity; + +import android.content.pm.ActivityInfo; +import android.support.test.rule.ActivityTestRule; +import android.support.test.runner.AndroidJUnit4; + +import com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.LifecycleTestPresenter; + +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) public class MviFinishInOnCreateActivityTest { + + @Rule public ActivityTestRule rule = + new ActivityTestRule<>(MviFinishInOnCreateActivity.class); + + @Test public void finishInOnCreate() throws Exception { + // Context of the app under test. + MviFinishInOnCreateActivity activity = rule.getActivity(); + activity.onDestroyReached.blockingFirst(); // Waits until onDestroy() is reached + } +} diff --git a/mvi-integration-test/src/androidTest/java/com/hannesdorfmann/mosby3/mvi/integrationtest/lifecycle/activity/MviFinishInOnStartActivityTest.java b/mvi-integration-test/src/androidTest/java/com/hannesdorfmann/mosby3/mvi/integrationtest/lifecycle/activity/MviFinishInOnStartActivityTest.java new file mode 100644 index 00000000..505f7a4f --- /dev/null +++ b/mvi-integration-test/src/androidTest/java/com/hannesdorfmann/mosby3/mvi/integrationtest/lifecycle/activity/MviFinishInOnStartActivityTest.java @@ -0,0 +1,37 @@ +/* + * Copyright 2016 Hannes Dorfmann. + * + * 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 com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.activity; + +import android.support.test.rule.ActivityTestRule; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) public class MviFinishInOnStartActivityTest { + + @Rule public ActivityTestRule rule = + new ActivityTestRule<>(MviFinishInOnStartActivity.class); + + @Test public void finishInOnCreate() throws Exception { + // Context of the app under test. + MviFinishInOnStartActivity activity = rule.getActivity(); + activity.onDestroyReached.blockingFirst(); // Waits until onDestroy() is reached + } +} diff --git a/mvi-integration-test/src/main/AndroidManifest.xml b/mvi-integration-test/src/main/AndroidManifest.xml index 543dde47..48a3229b 100644 --- a/mvi-integration-test/src/main/AndroidManifest.xml +++ b/mvi-integration-test/src/main/AndroidManifest.xml @@ -35,6 +35,8 @@ + + implements LifecycleTestView { + + @NonNull + @Override + public LifecycleTestPresenter createPresenter() { + return new LifecycleTestPresenter(); + } + + public Subject onDestroyReached = BehaviorSubject.create(); + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + finish(); // finish imeediately is what we would like to test --> App should not crash + } + + + @Override + protected void onDestroy() { + super.onDestroy(); + onDestroyReached.onNext(true); + onDestroyReached.onComplete(); + } +} diff --git a/mvi-integration-test/src/main/java/com/hannesdorfmann/mosby3/mvi/integrationtest/lifecycle/activity/MviFinishInOnStartActivity.java b/mvi-integration-test/src/main/java/com/hannesdorfmann/mosby3/mvi/integrationtest/lifecycle/activity/MviFinishInOnStartActivity.java new file mode 100644 index 00000000..fecfc62c --- /dev/null +++ b/mvi-integration-test/src/main/java/com/hannesdorfmann/mosby3/mvi/integrationtest/lifecycle/activity/MviFinishInOnStartActivity.java @@ -0,0 +1,36 @@ +package com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.activity; + +import android.os.Bundle; +import android.support.annotation.NonNull; + +import com.hannesdorfmann.mosby3.mvi.MviActivity; +import com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.LifecycleTestPresenter; +import com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.LifecycleTestView; + +import io.reactivex.subjects.BehaviorSubject; +import io.reactivex.subjects.Subject; + +public class MviFinishInOnStartActivity extends MviActivity implements LifecycleTestView { + + @NonNull + @Override + public LifecycleTestPresenter createPresenter() { + return new LifecycleTestPresenter(); + } + + public Subject onDestroyReached = BehaviorSubject.create(); + + + @Override + protected void onStart() { + super.onStart(); + finish(); // finish --> this is what we would like to test --> App should not crash + } + + @Override + protected void onDestroy() { + super.onDestroy(); + onDestroyReached.onNext(true); + onDestroyReached.onComplete(); + } +} diff --git a/mvi/src/main/java/com/hannesdorfmann/mosby3/ActivityMviDelegateImpl.java b/mvi/src/main/java/com/hannesdorfmann/mosby3/ActivityMviDelegateImpl.java index a7af9f58..8c8d0a2b 100644 --- a/mvi/src/main/java/com/hannesdorfmann/mosby3/ActivityMviDelegateImpl.java +++ b/mvi/src/main/java/com/hannesdorfmann/mosby3/ActivityMviDelegateImpl.java @@ -207,16 +207,17 @@ static boolean retainPresenterInstance(boolean keepPresenterInstance, Activity a } @Override public void onDestroy() { - - boolean retainPresenterInstance = retainPresenterInstance(keepPresenterInstance, activity); - if (!retainPresenterInstance){ - presenter.destroy(); - if (mosbyViewId != null) { // mosbyViewId == null if keepPresenterInstance == false - PresenterManager.remove(activity, mosbyViewId); + if (presenter != null) { // Presenter is only null if Activity.finish() called before Activity.onStart() has been reached + boolean retainPresenterInstance = retainPresenterInstance(keepPresenterInstance, activity); + if (!retainPresenterInstance) { + presenter.destroy(); + if (mosbyViewId != null) { // mosbyViewId == null if keepPresenterInstance == false + PresenterManager.remove(activity, mosbyViewId); + } + Log.d(DEBUG_TAG, "Destroying Presenter permanently " + presenter); } - Log.d(DEBUG_TAG, "Destroying Presenter permanently " + presenter); - } + } presenter = null; activity = null; delegateCallback = null;