Search in sources :

Example 16 with MvpView

use of com.hannesdorfmann.mosby3.mvp.MvpView in project mosby by sockeqwe.

the class ViewGroupMvpDelegateImplTest method initComponents.

@Before
public void initComponents() {
    view = new MvpView() {
    };
    savedState = null;
    presenter = Mockito.mock(MvpPresenter.class);
    callback = Mockito.mock(PartialViewGroupMvpDelegateCallbackImpl.class);
    Mockito.doCallRealMethod().when(callback).setPresenter(presenter);
    Mockito.doCallRealMethod().when(callback).getPresenter();
    Mockito.doCallRealMethod().when(callback).superOnSaveInstanceState();
    activity = Mockito.mock(FragmentActivity.class);
    application = Mockito.mock(Application.class);
    androidView = Mockito.mock(View.class);
    Mockito.when(callback.getMvpView()).thenReturn(view);
    Mockito.when(callback.getContext()).thenReturn(activity);
    Mockito.when(activity.getApplication()).thenReturn(application);
    Mockito.when(androidView.isInEditMode()).thenReturn(false);
    delegate = new ViewGroupMvpDelegateImpl<>(androidView, callback, true);
}
Also used : FragmentActivity(android.support.v4.app.FragmentActivity) MvpView(com.hannesdorfmann.mosby3.mvp.MvpView) MvpPresenter(com.hannesdorfmann.mosby3.mvp.MvpPresenter) Application(android.app.Application) View(android.view.View) MvpView(com.hannesdorfmann.mosby3.mvp.MvpView) Before(org.junit.Before)

Example 17 with MvpView

use of com.hannesdorfmann.mosby3.mvp.MvpView in project mosby by sockeqwe.

the class ViewGroupMvpDelegateImplTest method dontKeepPresenterIfSecondPresenterInPresenterManager.

/**
 * Checks if two Views one that keeps presenter, the other who doesn't keep presenter during
 * screen orientation changes work properly
 *
 * https://github.com/sockeqwe/mosby/issues/231
 */
@Test
public void dontKeepPresenterIfSecondPresenterInPresenterManager() {
    MvpView view1 = new MvpView() {
    };
    MvpPresenter<MvpView> presenter1 = Mockito.mock(MvpPresenter.class);
    PartialViewGroupMvpDelegateCallbackImpl callback1 = Mockito.mock(PartialViewGroupMvpDelegateCallbackImpl.class);
    Mockito.doCallRealMethod().when(callback1).setPresenter(presenter1);
    Mockito.doCallRealMethod().when(callback1).getPresenter();
    View androidView1 = Mockito.mock(View.class);
    Mockito.when(callback1.getMvpView()).thenReturn(view1);
    Mockito.when(callback1.getContext()).thenReturn(activity);
    Mockito.when(androidView1.isInEditMode()).thenReturn(false);
    Mockito.when(callback1.createPresenter()).thenReturn(presenter1);
    ViewGroupMvpDelegateImpl<MvpView, MvpPresenter<MvpView>> keepDeelgate = new ViewGroupMvpDelegateImpl<MvpView, MvpPresenter<MvpView>>(androidView1, callback1, true);
    delegate = new ViewGroupMvpDelegateImpl<MvpView, MvpPresenter<MvpView>>(androidView, callback, false);
    keepDeelgate.onAttachedToWindow();
    startViewGroup(1, 1, 1);
    finishViewGroup(1, 1, true, false);
    delegate = new ViewGroupMvpDelegateImpl<MvpView, MvpPresenter<MvpView>>(androidView, callback, false);
    startViewGroup(2, 2, 2);
    finishViewGroup(2, 2, false, true);
    keepDeelgate.onDetachedFromWindow();
}
Also used : MvpView(com.hannesdorfmann.mosby3.mvp.MvpView) MvpPresenter(com.hannesdorfmann.mosby3.mvp.MvpPresenter) View(android.view.View) MvpView(com.hannesdorfmann.mosby3.mvp.MvpView) Test(org.junit.Test)

Example 18 with MvpView

use of com.hannesdorfmann.mosby3.mvp.MvpView in project mosby by sockeqwe.

the class ActivityMvpDelegateImplTest method initComponents.

@Before
public void initComponents() {
    view = new MvpView() {
    };
    presenter = Mockito.mock(MvpPresenter.class);
    callback = Mockito.mock(PartialMvpDelegateCallbackImpl.class);
    activity = Mockito.mock(Activity.class);
    application = Mockito.mock(Application.class);
    Mockito.doCallRealMethod().when(callback).setPresenter(presenter);
    Mockito.doCallRealMethod().when(callback).getPresenter();
    Mockito.when(callback.getMvpView()).thenReturn(view);
    Mockito.when(activity.getApplication()).thenReturn(application);
    Mockito.when(callback.createPresenter()).thenReturn(presenter);
}
Also used : MvpView(com.hannesdorfmann.mosby3.mvp.MvpView) MvpPresenter(com.hannesdorfmann.mosby3.mvp.MvpPresenter) Activity(android.app.Activity) Application(android.app.Application) Before(org.junit.Before)

Example 19 with MvpView

use of com.hannesdorfmann.mosby3.mvp.MvpView in project mosby by sockeqwe.

the class FragmentMvpViewStateDelegateImpl method onViewCreated.

@Override
public void onViewCreated(View view, Bundle bundle) {
    super.onViewCreated(view, bundle);
    if (bundle != null && keepPresenterInstanceDuringScreenOrientationChanges) {
        mosbyViewId = bundle.getString(KEY_MOSBY_VIEW_ID);
        if (DEBUG) {
            Log.d(DEBUG_TAG, "MosbyView ID = " + mosbyViewId + " for MvpView: " + delegateCallback.getMvpView());
        }
    }
    if (mosbyViewId != null) {
        VS viewState = PresenterManager.getViewState(fragment.getActivity(), mosbyViewId);
        if (viewState != null) {
            //
            // ViewState restored from PresenterManager
            //
            delegateCallback.setViewState(viewState);
            applyViewState = true;
            applyViewStateFromMemory = true;
            if (DEBUG) {
                Log.d(DEBUG_TAG, "ViewState reused from Mosby internal cache for view: " + delegateCallback.getMvpView() + " viewState: " + viewState);
            }
            return;
        }
    }
    VS viewState = delegateCallback.createViewState();
    if (viewState == null) {
        throw new NullPointerException("ViewState returned from createViewState() is null! MvpView that has returned null as ViewState is: " + delegateCallback.getMvpView());
    }
    if (bundle != null && viewState instanceof RestorableViewState) {
        // A little bit hacky that we need an instance of the viewstate to restore a view state
        // (may creates another view state object) but I don't know any better way :)
        RestorableViewState restoredViewState = ((RestorableViewState) viewState).restoreInstanceState(bundle);
        if (restoredViewState != null) {
            //
            // ViewState restored from bundle
            //
            viewState = (VS) restoredViewState;
            delegateCallback.setViewState(viewState);
            applyViewState = true;
            applyViewStateFromMemory = false;
            if (keepPresenterInstanceDuringScreenOrientationChanges) {
                if (mosbyViewId == null) {
                    throw new IllegalStateException("The (internal) Mosby View id is null although bundle is not null. This should never happen. This seems to be a Mosby internal error. Please report this issue at https://github.com/sockeqwe/mosby/issues");
                }
                PresenterManager.putViewState(fragment.getActivity(), mosbyViewId, viewState);
            }
            if (DEBUG) {
                Log.d(DEBUG_TAG, "Recreated ViewState from bundle for view: " + delegateCallback.getMvpView() + " viewState: " + viewState);
            }
            return;
        }
    }
    //
    if (keepPresenterInstanceDuringScreenOrientationChanges) {
        if (mosbyViewId == null) {
            throw new IllegalStateException("The (internal) Mosby View id is null. This should never happen. This seems to be a Mosby internal error. Please report this issue at https://github.com/sockeqwe/mosby/issues");
        }
        PresenterManager.putViewState(fragment.getActivity(), mosbyViewId, viewState);
    }
    delegateCallback.setViewState(viewState);
    applyViewState = false;
    applyViewStateFromMemory = false;
    if (DEBUG) {
        Log.d(DEBUG_TAG, "Created a new ViewState instance for view: " + delegateCallback.getMvpView() + " viewState: " + viewState);
    }
}
Also used : RestorableViewState(com.hannesdorfmann.mosby3.mvp.viewstate.RestorableViewState)

Example 20 with MvpView

use of com.hannesdorfmann.mosby3.mvp.MvpView in project mosby by sockeqwe.

the class FragmentMvpViewStateDelegateImplTest method initComponents.

@Before
public void initComponents() {
    view = new MvpView() {
    };
    viewState = Mockito.mock(ViewState.class);
    presenter = Mockito.mock(MvpPresenter.class);
    callback = Mockito.spy(PartialMvpViewStateDelegateCallbackImpl.class);
    Mockito.doCallRealMethod().when(callback).setPresenter(presenter);
    Mockito.doCallRealMethod().when(callback).getPresenter();
    Mockito.doCallRealMethod().when(callback).setViewState(viewState);
    Mockito.doCallRealMethod().when(callback).getViewState();
    fragment = PowerMockito.mock(Fragment.class);
    activity = Mockito.mock(FragmentActivity.class);
    application = Mockito.mock(Application.class);
    Mockito.when(callback.getMvpView()).thenReturn(view);
    Mockito.when(fragment.getActivity()).thenReturn(activity);
    Mockito.when(activity.getApplication()).thenReturn(application);
    Mockito.when(callback.createPresenter()).thenReturn(presenter);
    Mockito.when(callback.createViewState()).thenReturn(viewState);
    delegate = new FragmentMvpViewStateDelegateImpl<>(fragment, callback, true, true);
}
Also used : FragmentActivity(android.support.v4.app.FragmentActivity) MvpView(com.hannesdorfmann.mosby3.mvp.MvpView) MvpPresenter(com.hannesdorfmann.mosby3.mvp.MvpPresenter) ViewState(com.hannesdorfmann.mosby3.mvp.viewstate.ViewState) Fragment(android.support.v4.app.Fragment) Application(android.app.Application) Before(org.junit.Before)

Aggregations

MvpView (com.hannesdorfmann.mosby3.mvp.MvpView)21 MvpPresenter (com.hannesdorfmann.mosby3.mvp.MvpPresenter)18 Test (org.junit.Test)15 Bundle (android.os.Bundle)10 ViewState (com.hannesdorfmann.mosby3.mvp.viewstate.ViewState)9 Application (android.app.Application)7 Before (org.junit.Before)6 Fragment (android.support.v4.app.Fragment)4 FragmentActivity (android.support.v4.app.FragmentActivity)4 View (android.view.View)4 Activity (android.app.Activity)3 RestorableViewState (com.hannesdorfmann.mosby3.mvp.viewstate.RestorableViewState)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 Object (org.omg.CORBA.Object)2 Parcelable (android.os.Parcelable)1 RestorableParcelableViewState (com.hannesdorfmann.mosby3.mvp.viewstate.RestorableParcelableViewState)1