Skip to content

Commit

Permalink
ActivityMviDelegateImpl now checks if presenter != null if Activity.f…
Browse files Browse the repository at this point in the history
…inish() is called in Activity.onCreate() #290
  • Loading branch information
sockeqwe committed Apr 9, 2018
1 parent 513d1e9 commit 2b0e764
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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<MviFinishInOnCreateActivity> 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
}
}
Original file line number Diff line number Diff line change
@@ -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<MviFinishInOnStartActivity> 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
}
}
2 changes: 2 additions & 0 deletions mvi-integration-test/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
<activity android:name=".lifecycle.fragment.childfragment.MviLifecycleChildFragmentActivity" />
<activity android:name=".lifecycle.fragment.backstack.MviLifecycleBackstackActivity" />
<activity android:name=".lifecycle.viewgroup.MviViewGroupContainerActivity" />
<activity android:name=".lifecycle.activity.MviFinishInOnCreateActivity" />
<activity android:name=".lifecycle.activity.MviFinishInOnStartActivity" />
<activity
android:name=".backstack.BackstackActivity"
android:label="@string/title_activity_backstack"
Expand Down
Original file line number Diff line number Diff line change
@@ -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 MviFinishInOnCreateActivity extends MviActivity<LifecycleTestView, LifecycleTestPresenter> implements LifecycleTestView {

@NonNull
@Override
public LifecycleTestPresenter createPresenter() {
return new LifecycleTestPresenter();
}

public Subject<Boolean> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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<LifecycleTestView, LifecycleTestPresenter> implements LifecycleTestView {

@NonNull
@Override
public LifecycleTestPresenter createPresenter() {
return new LifecycleTestPresenter();
}

public Subject<Boolean> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 2b0e764

Please sign in to comment.