Skip to content

Commit

Permalink
More integration tests for MviFragment and destroying activity in onS…
Browse files Browse the repository at this point in the history
…tart or onCreate
  • Loading branch information
sockeqwe committed Apr 24, 2018
1 parent d52964a commit d2048d9
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.fragment;

import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.LifecycleTestPresenter;

import junit.framework.Assert;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class) public class MviFinishFragmentOnCreateTest {

@Rule public ActivityTestRule<MviFinishOnCreateContainerActivity> rule =
new ActivityTestRule<>(MviFinishOnCreateContainerActivity.class);

private static LifecycleTestPresenter presenter;

@Test public void screenOrientationChange() throws Exception {
// Context of the app under test.
MviFinishOnCreateContainerActivity portraitActivity = rule.getActivity();
Thread.sleep(2000);
Assert.assertNotNull(MviFinishOnCreateFragment.presenter);
Assert.assertEquals(1, MviFinishOnCreateFragment.presenterCreatedCount);
Assert.assertEquals(1, MviFinishOnCreateFragment.presenter.destoryInvoations);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.fragment;

import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.LifecycleTestPresenter;

import junit.framework.Assert;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class) public class MviFinishFragmentOnStartTest {

@Rule public ActivityTestRule<MviFinishOnStartContainerActivity> rule =
new ActivityTestRule<>(MviFinishOnStartContainerActivity.class);

private static LifecycleTestPresenter presenter;

@Test public void screenOrientationChange() throws Exception {
// Context of the app under test.
MviFinishOnStartContainerActivity portraitActivity = rule.getActivity();
Thread.sleep(2000);
Assert.assertNotNull(MviFinishOnStartFragment.presenter);
Assert.assertEquals(1, MviFinishOnStartFragment.presenterCreatedCount);
Assert.assertEquals(1, MviFinishOnStartFragment.presenter.destoryInvoations);
}

}
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 @@ -37,6 +37,8 @@
<activity android:name=".lifecycle.viewgroup.MviViewGroupContainerActivity" />
<activity android:name=".lifecycle.activity.MviFinishInOnCreateActivity" />
<activity android:name=".lifecycle.activity.MviFinishInOnStartActivity" />
<activity android:name=".lifecycle.fragment.MviFinishOnCreateContainerActivity"/>
<activity android:name=".lifecycle.fragment.MviFinishOnStartContainerActivity"/>
<activity
android:name=".backstack.BackstackActivity"
android:label="@string/title_activity_backstack"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle;

import android.util.Log;

import com.hannesdorfmann.mosby3.mvi.MviBasePresenter;

/**
Expand All @@ -26,48 +27,58 @@

public class LifecycleTestPresenter extends MviBasePresenter<LifecycleTestView, Object> {

public int attachViewInvokations = 0;
public LifecycleTestView attachedView;
public int detachViewInvokations = 0;
public int bindIntentInvocations = 0;
public int unbindIntentInvocations = 0;
public int destoryInvoations = 0;
public int attachViewInvokations = 0;
public LifecycleTestView attachedView;
public int detachViewInvokations = 0;
public int bindIntentInvocations = 0;
public int unbindIntentInvocations = 0;
public int destoryInvoations = 0;

@Override public void attachView(LifecycleTestView view) {
super.attachView(view);
attachViewInvokations++;
attachedView = view;
Log.d(getClass().getSimpleName(), "attachView " + attachViewInvokations + " " + attachedView+" in "+toString());
}
public LifecycleTestPresenter() {
Log.d(getClass().getSimpleName(), "constructor " + attachViewInvokations + " " + attachedView + " in " + toString());
}

@Override public void detachView() {
super.detachView();
attachedView = null;
detachViewInvokations++;
Log.d(getClass().getSimpleName(), "detachView " + detachViewInvokations+" in "+toString());
}

@Override public void destroy() {
super.destroy();
destoryInvoations++;
Log.d(getClass().getSimpleName(), "destroy Presenter " + destoryInvoations+" in "+toString());
}
@Override
public void attachView(LifecycleTestView view) {
super.attachView(view);
attachViewInvokations++;
attachedView = view;
Log.d(getClass().getSimpleName(), "attachView " + attachViewInvokations + " " + attachedView + " in " + toString());
}

@Override protected void bindIntents() {
if (bindIntentInvocations >= 1) {
throw new IllegalStateException(
"bindIntents() is called more than once. Invokations: " + bindIntentInvocations);
@Override
public void detachView() {
super.detachView();
attachedView = null;
detachViewInvokations++;
Log.d(getClass().getSimpleName(), "detachView " + detachViewInvokations + " in " + toString());
}
bindIntentInvocations++;
}

@Override protected void unbindIntents() {
super.unbindIntents();
if (unbindIntentInvocations >= 1) {
throw new IllegalStateException(
"unbindIntents() is called more than once. Invokations: " + unbindIntentInvocations);
@Override
public void destroy() {
super.destroy();
destoryInvoations++;
Log.d(getClass().getSimpleName(), "destroy Presenter " + destoryInvoations + " in " + toString());
}

unbindIntentInvocations++;
}
@Override
protected void bindIntents() {
if (bindIntentInvocations >= 1) {
throw new IllegalStateException(
"bindIntents() is called more than once. Invokations: " + bindIntentInvocations);
}
bindIntentInvocations++;
}

@Override
protected void unbindIntents() {
super.unbindIntents();
if (unbindIntentInvocations >= 1) {
throw new IllegalStateException(
"unbindIntents() is called more than once. Invokations: " + unbindIntentInvocations);
}

unbindIntentInvocations++;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;

import com.hannesdorfmann.mosby3.mvi.integrationtest.R;

public class MviFinishOnCreateContainerActivity extends FragmentActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lifecycle);

if (savedInstanceState == null) {
MviFinishOnCreateFragment f = new MviFinishOnCreateFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container, f).commitNow();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hannesdorfmann.mosby3.mvi.MviFragment;
import com.hannesdorfmann.mosby3.mvi.integrationtest.R;
import com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.LifecycleTestPresenter;
import com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.LifecycleTestView;

public class MviFinishOnCreateFragment extends MviFragment<LifecycleTestView, LifecycleTestPresenter> implements LifecycleTestView {


public static LifecycleTestPresenter presenter;
public static int presenterCreatedCount;

@NonNull
@Override
public LifecycleTestPresenter createPresenter() {
presenter = new LifecycleTestPresenter();
presenterCreatedCount++;
return presenter;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_mvi, container, false);
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().finish();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;

import com.hannesdorfmann.mosby3.mvi.integrationtest.R;

public class MviFinishOnStartContainerActivity extends FragmentActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lifecycle);

if (savedInstanceState == null) {
MviFinishOnStartFragment f = new MviFinishOnStartFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container, f).commitNow();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hannesdorfmann.mosby3.mvi.MviFragment;
import com.hannesdorfmann.mosby3.mvi.integrationtest.R;
import com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.LifecycleTestPresenter;
import com.hannesdorfmann.mosby3.mvi.integrationtest.lifecycle.LifecycleTestView;

public class MviFinishOnStartFragment extends MviFragment<LifecycleTestView, LifecycleTestPresenter> implements LifecycleTestView {


public static LifecycleTestPresenter presenter;
public static int presenterCreatedCount;

@NonNull
@Override
public LifecycleTestPresenter createPresenter() {
presenter = new LifecycleTestPresenter();
presenterCreatedCount++;
return presenter;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_mvi, container, false);
}

@Override
public void onStart() {
super.onStart();
getActivity().finish();
}
}

0 comments on commit d2048d9

Please sign in to comment.