Skip to content

Commit

Permalink
sockeqwe#65 taking into account isChangingConfig()
Browse files Browse the repository at this point in the history
  • Loading branch information
sockeqwe committed Aug 26, 2015
1 parent 6d85d04 commit 46436b6
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 5 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ ext {
// Test Libs
robolectricVersion = '2.4'
junitVersion = '4.12'
mockitoVersion = '1.9.5'
mockitoVersion = '2.0.5-beta'
powermockVersion = '1.6.2'
}
10 changes: 10 additions & 0 deletions mvp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,14 @@ dependencies {

testCompile 'junit:junit:' + rootProject.ext.junitVersion
testCompile 'org.mockito:mockito-core:' + rootProject.ext.mockitoVersion


testCompile ('org.powermock:powermock-api-mockito:'+rootProject.ext.powermockVersion) {
exclude module: 'hamcrest-core'
exclude module: 'objenesis'
}
testCompile ('org.powermock:powermock-module-junit4:'+rootProject.ext.powermockVersion) {
exclude module: 'hamcrest-core'
exclude module: 'objenesis'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import com.hannesdorfmann.mosby.MosbyFragment;
import com.hannesdorfmann.mosby.mvp.delegate.FragmentMvpDelegateImpl;
Expand Down Expand Up @@ -82,7 +83,8 @@ public abstract class MvpFragment<V extends MvpView, P extends MvpPresenter<V>>
}

@Override public boolean isRetainingInstance() {
return getRetainInstance();
FragmentActivity activity = getActivity();
return getRetainInstance() && activity != null && activity.isChangingConfigurations();
}

@NonNull @Override public V getMvpView() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.hannesdorfmann.mosby.mvp.delegate;

import android.app.Activity;
import com.hannesdorfmann.mosby.mvp.MvpPresenter;
import com.hannesdorfmann.mosby.mvp.MvpView;

Expand Down Expand Up @@ -64,7 +65,8 @@ public interface MvpDelegateCallback<V extends MvpView, P extends MvpPresenter<V

/**
* Is the view retaining? This boolean flag is used for {@link MvpPresenter#detachView(boolean)}
* as parameter.
* as parameter. Usually you should take {@link Activity#isChangingConfigurations()} into
* account.
*
* @return true if the view is retaining, hence the presenter should be retaining as well.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2015 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.mosby.mvp;

import android.support.v4.app.FragmentActivity;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

/**
* @author Hannes Dorfmann
*/

@RunWith(PowerMockRunner.class) @PrepareForTest(MvpFragment.class)
public class MvpFragmentTest {

private MvpFragment<MvpView, MvpPresenter<MvpView>> fragment;
private FragmentActivity activity;

@Before public void init() {

activity = Mockito.mock(FragmentActivity.class);
fragment = PowerMockito.spy(new MvpFragment<MvpView, MvpPresenter<MvpView>>() {
@Override public MvpPresenter<MvpView> createPresenter() {
return null;
}
});
}

@Test public void retainingAndActivityNull() {
PowerMockito.when(fragment.getRetainInstance()).thenReturn(true);
PowerMockito.when(fragment.getActivity()).thenReturn(null);

Assert.assertFalse(fragment.isRetainingInstance());
}

@Test public void notRetainingAndActivityNull() {
PowerMockito.when(fragment.getRetainInstance()).thenReturn(false);
PowerMockito.when(fragment.getActivity()).thenReturn(null);

Assert.assertFalse(fragment.isRetainingInstance());
}

@Test public void retainingAndNotChangingConfig() {
PowerMockito.when(fragment.getRetainInstance()).thenReturn(true);
PowerMockito.when(activity.isChangingConfigurations()).thenReturn(false);

Assert.assertFalse(fragment.isRetainingInstance());
}

@Test public void retainingAndChangingConfig() {

PowerMockito.when(fragment.getActivity()).thenReturn(activity);
PowerMockito.when(fragment.getRetainInstance()).thenReturn(true);
PowerMockito.when(activity.isChangingConfigurations()).thenReturn(true);

Assert.assertTrue(fragment.isRetainingInstance());
}
}
4 changes: 2 additions & 2 deletions viewstate/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ android {
dependencies {
compile project(':mvp')

testCompile 'org.robolectric:robolectric:'+ rootProject.ext.robolectricVersion
testCompile 'junit:junit:' + rootProject.ext.junitVersion
testCompile 'org.mockito:mockito-core:1.9.5'
testCompile 'org.mockito:mockito-core:' + rootProject.ext.mockitoVersion
testCompile 'org.robolectric:robolectric:'+ rootProject.ext.robolectricVersion
}

0 comments on commit 46436b6

Please sign in to comment.