com.hannesdorfmann.mosby3.mvp.MvpPresenter Java Examples

The following examples show how to use com.hannesdorfmann.mosby3.mvp.MvpPresenter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MvpViewStateConductorLifecycleTest.java    From mosby-conductor with Apache License 2.0 6 votes vote down vote up
@Test public void createAndSetNewViewState() {
  MvpPresenter<MvpView> presenter = Mockito.mock(MvpPresenter.class);
  Controller controller = PowerMockito.mock(Controller.class);
  View view = Mockito.mock(View.class);
  MvpView mvpView = Mockito.mock(MvpView.class);
  ViewState<MvpView> viewState = Mockito.mock(ViewState.class);

  MvpViewStateConductorDelegateCallback<MvpView, MvpPresenter<MvpView>, ViewState<MvpView>>
      callback = Mockito.mock(MvpViewStateConductorDelegateCallback.class);

  Mockito.when(callback.getPresenter()).thenReturn(presenter);
  Mockito.when(callback.getMvpView()).thenReturn(mvpView);
  Mockito.when(callback.getViewState()).thenReturn(null);
  Mockito.when(callback.createViewState()).thenReturn(viewState);

  MvpViewStateConductorLifecycleListener<MvpView, MvpPresenter<MvpView>, ViewState<MvpView>>
      lifecycleListener = new MvpViewStateConductorLifecycleListener<>(callback);

  lifecycleListener.postCreateView(controller, view);
  lifecycleListener.preAttach(controller, view);

  Mockito.verify(callback, Mockito.times(1)).getViewState();
  Mockito.verify(callback, Mockito.times(1)).createViewState();
  Mockito.verify(callback, Mockito.times(1)).setViewState(viewState);
  Mockito.verify(callback, Mockito.times(1)).onNewViewStateInstance();
}
 
Example #2
Source File: FragmentMvpDelegateImplTest.java    From mosby with Apache License 2.0 6 votes vote down vote up
@Before public void initComponents() {
  view = new MvpView() {
  };

  presenter = Mockito.mock(MvpPresenter.class);
  callback = Mockito.mock(PartialMvpDelegateCallbackImpl.class);
  Mockito.doCallRealMethod().when(callback).setPresenter(presenter);
  Mockito.doCallRealMethod().when(callback).getPresenter();

  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);

  delegate = new FragmentMvpDelegateImpl<>(fragment, callback, true, true);
}
 
Example #3
Source File: ViewGroupMvpDelegateImplTest.java    From mosby with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #4
Source File: ActivityMvpDelegateImplTest.java    From mosby with Apache License 2.0 6 votes vote down vote up
private void finishActivity(ActivityMvpDelegateImpl<MvpView, MvpPresenter<MvpView>> delegate,
    Bundle bundle, boolean expectKeepPresenter, int detachViewCount, int destroyCount,
    boolean changingConfigurations, boolean isFinishing) {
  Mockito.when(callback.getPresenter()).thenReturn(presenter);
  Mockito.when(activity.isChangingConfigurations()).thenReturn(changingConfigurations);
  Mockito.when(activity.isFinishing()).thenReturn(isFinishing);

  delegate.onPause();
  delegate.onSaveInstanceState(bundle);
  delegate.onStop();
  delegate.onDestroy();
  delegate.onRestart();

  Mockito.verify(presenter, Mockito.times(detachViewCount)).detachView();
  if (!expectKeepPresenter) Mockito.verify(presenter, Mockito.times(destroyCount)).destroy();
}
 
Example #5
Source File: ActivityMvpDelegateImplTest.java    From mosby with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #6
Source File: ActivityScopedCache.java    From mosby with Apache License 2.0 6 votes vote down vote up
/**
 * Put the presenter in the internal cache
 *
 * @param viewId The mosby internal View id of the {@link MvpView} which the presenter is
 * associated to.
 * @param presenter The Presenter
 */
public void putPresenter(@NonNull String viewId,
    @NonNull MvpPresenter<? extends MvpView> presenter) {

  if (viewId == null) {
    throw new NullPointerException("ViewId is null");
  }

  if (presenter == null) {
    throw new NullPointerException("Presenter is null");
  }

  PresenterHolder presenterHolder = presenterMap.get(viewId);
  if (presenterHolder == null) {
    presenterHolder = new PresenterHolder();
    presenterHolder.presenter = presenter;
    presenterMap.put(viewId, presenterHolder);
  } else {
    presenterHolder.presenter = presenter;
  }
}
 
Example #7
Source File: ActivityMvpViewStateDelegateImplTestNew.java    From mosby with Apache License 2.0 6 votes vote down vote up
private void finishActivity(ActivityMvpDelegateImpl<MvpView, MvpPresenter<MvpView>> delegate,
    Bundle bundle, int detachViewCount, int destroyViewCount, boolean changingConfigurations,
    boolean isFinishing) {
  Mockito.when(callback.getPresenter()).thenReturn(presenter);
  Mockito.when(callback.getViewState()).thenReturn(viewState);
  Mockito.when(activity.isChangingConfigurations()).thenReturn(changingConfigurations);
  Mockito.when(activity.isFinishing()).thenReturn(isFinishing);

  delegate.onPause();
  delegate.onSaveInstanceState(bundle);
  delegate.onStop();
  delegate.onDestroy();
  delegate.onRestart();

  Mockito.verify(presenter, Mockito.times(detachViewCount)).detachView();
  Mockito.verify(presenter, Mockito.times(destroyViewCount)).destroy();
}
 
Example #8
Source File: ActivityMvpViewStateDelegateImplTestNew.java    From mosby with Apache License 2.0 6 votes vote down vote up
@Test public void appStartWithViewStateFromMemoryAndBundleButPreferViewStateFromMemory() {

    ActivityMvpViewStateDelegateImpl<MvpView, MvpPresenter<MvpView>, ViewState<MvpView>> delegate =
        new ActivityMvpViewStateDelegateImpl<>(activity, callback, true);

    Mockito.doAnswer(new Answer() {
      @Override public Object answer(InvocationOnMock invocation) throws Throwable {
        viewState = Mockito.spy(new SimpleRestorableViewState());
        return viewState;
      }
    }).when(callback).createViewState();

    startActivity(delegate, null, 1, 1, 1, 1, 1, 0, null, 0, 1, 0);
    Bundle bundle = BundleMocker.create();
    finishActivity(delegate, bundle, 1, 0,true, false);
    startActivity(delegate, bundle, 1, 2, 2, 1, 2, 1, true, 1, 1, 1);
    finishActivity(delegate, bundle,  2, 1,false, true);
  }
 
Example #9
Source File: ActivityMvpViewStateDelegateImplTestNew.java    From mosby with Apache License 2.0 6 votes vote down vote up
@Test public void appStartAfterProcessDeathAndViewStateRecreationFromBundle() {
  ActivityMvpViewStateDelegateImpl<MvpView, MvpPresenter<MvpView>, ViewState<MvpView>> delegate =
      new ActivityMvpViewStateDelegateImpl<>(activity, callback, true);

  Mockito.doAnswer(new Answer() {
    @Override public Object answer(InvocationOnMock invocation) throws Throwable {
      viewState = Mockito.spy(new SimpleRestorableViewState());
      return viewState;
    }
  }).when(callback).createViewState();

  Bundle bundle = BundleMocker.create();
  bundle.putString(ActivityMvpViewStateDelegateImpl.KEY_MOSBY_VIEW_ID, "123456789");

  startActivity(delegate, bundle, 1, 1, 1, 1, 1, 1, false, 1, 0, 1);
}
 
Example #10
Source File: ActivityMvpViewStateDelegateImplTestNew.java    From mosby with Apache License 2.0 6 votes vote down vote up
@Before public void initComponents() {
  view = new MvpView() {
  };

  viewState = Mockito.mock(ViewState.class);

  presenter = Mockito.mock(MvpPresenter.class);
  callback = Mockito.spy(PartialMvpViewStateDelegateCallbackImpl.class);
  activity = Mockito.mock(Activity.class);
  application = Mockito.mock(Application.class);

  Mockito.doCallRealMethod().when(callback).setPresenter(presenter);
  Mockito.doCallRealMethod().when(callback).getPresenter();
  Mockito.doCallRealMethod().when(callback).setViewState(viewState);
  Mockito.doCallRealMethod().when(callback).getViewState();

  Mockito.when(callback.getMvpView()).thenReturn(view);
  Mockito.when(activity.getApplication()).thenReturn(application);

  Mockito.when(callback.createPresenter()).thenReturn(presenter);
  Mockito.when(callback.createViewState()).thenReturn(viewState);
}
 
Example #11
Source File: MvpConductorLifecycleListenerTest.java    From mosby-conductor with Apache License 2.0 6 votes vote down vote up
@Test public void nullPointerExceptionGetPresenter() {
  MvpPresenter<MvpView> presenter = Mockito.mock(MvpPresenter.class);
  Controller controller = Mockito.mock(Controller.class);
  View view = Mockito.mock(View.class);

  MvpConductorDelegateCallback<MvpView, MvpPresenter<MvpView>> callback =
      Mockito.mock(MvpConductorDelegateCallback.class);

  MvpConductorLifecycleListener<MvpView, MvpPresenter<MvpView>> lifecycleListener =
      new MvpConductorLifecycleListener<>(callback);

  Mockito.when(callback.getPresenter()).thenReturn(null);

  try {
    lifecycleListener.preDestroyView(controller, view);
    Assert.fail();
  } catch (NullPointerException e) {
    Assert.assertEquals(e.getMessage(),
        "Presenter returned from getPresenter() is null in " + callback);
  }
}
 
Example #12
Source File: MvpConductorLifecycleListenerTest.java    From mosby-conductor with Apache License 2.0 6 votes vote down vote up
@Test public void dontCreatePresenterIfOneExists() {
  MvpPresenter<MvpView> presenter = Mockito.mock(MvpPresenter.class);
  Controller controller = Mockito.mock(Controller.class);
  View view = Mockito.mock(View.class);
  MvpView mvpView = Mockito.mock(MvpView.class);
  MvpConductorDelegateCallback<MvpView, MvpPresenter<MvpView>> callback =
      Mockito.mock(MvpConductorDelegateCallback.class);

  MvpConductorLifecycleListener<MvpView, MvpPresenter<MvpView>> lifecycleListener =
      new MvpConductorLifecycleListener<>(callback);

  Mockito.when(callback.getPresenter()).thenReturn(presenter);
  Mockito.when(callback.getMvpView()).thenReturn(mvpView);

  lifecycleListener.postCreateView(controller, view);

  Mockito.verify(callback, Mockito.times(1)).getPresenter();
  Mockito.verify(callback, Mockito.never()).createPresenter();
  Mockito.verify(callback, Mockito.never()).setPresenter(presenter);
}
 
Example #13
Source File: MvpConductorLifecycleListenerTest.java    From mosby-conductor with Apache License 2.0 6 votes vote down vote up
@Test public void nullPointerExceptionOnGetMvpView() {

    MvpPresenter<MvpView> presenter = Mockito.mock(MvpPresenter.class);
    Controller controller = Mockito.mock(Controller.class);
    View view = Mockito.mock(View.class);

    MvpConductorDelegateCallback<MvpView, MvpPresenter<MvpView>> callback =
        Mockito.mock(MvpConductorDelegateCallback.class);

    Mockito.when(callback.getPresenter()).thenReturn(presenter);
    Mockito.when(callback.getMvpView()).thenReturn(null);

    MvpConductorLifecycleListener<MvpView, MvpPresenter<MvpView>> lifecycleListener =
        new MvpConductorLifecycleListener<>(callback);

    try {
      lifecycleListener.postCreateView(controller, view);
      Assert.fail("NullpointerException expected");
    } catch (NullPointerException e) {
      Assert.assertEquals(e.getMessage(),
          "MVP View returned from getMvpView() is null in " + callback);
    }
  }
 
Example #14
Source File: MvpConductorLifecycleListenerTest.java    From mosby-conductor with Apache License 2.0 6 votes vote down vote up
@Test public void createAndSetNewPresenterAndAttachView() {

    MvpPresenter<MvpView> presenter = Mockito.mock(MvpPresenter.class);
    Controller controller = Mockito.mock(Controller.class);
    View view = Mockito.mock(View.class);
    MvpView mvpView = Mockito.mock(MvpView.class);

    MvpConductorDelegateCallback<MvpView, MvpPresenter<MvpView>> callback =
        Mockito.mock(MvpConductorDelegateCallback.class);

    Mockito.when(callback.getPresenter()).thenReturn(null);
    Mockito.when(callback.createPresenter()).thenReturn(presenter);
    Mockito.when(callback.getMvpView()).thenReturn(mvpView);

    MvpConductorLifecycleListener<MvpView, MvpPresenter<MvpView>> lifecycleListener =
        new MvpConductorLifecycleListener<>(callback);

    lifecycleListener.postCreateView(controller, view);

    Mockito.verify(callback, Mockito.times(1)).getPresenter();
    Mockito.verify(callback, Mockito.times(1)).createPresenter();
    Mockito.verify(callback, Mockito.times(1)).setPresenter(presenter);
    Mockito.verify(presenter, Mockito.times(1)).attachView(mvpView);
  }
 
Example #15
Source File: MvpConductorLifecycleListenerTest.java    From mosby-conductor with Apache License 2.0 6 votes vote down vote up
@Test public void nullPointerExceptionOnCreatePresenter() {

    Controller controller = Mockito.mock(Controller.class);
    View view = Mockito.mock(View.class);

    MvpConductorDelegateCallback<MvpView, MvpPresenter<MvpView>> callback =
        Mockito.mock(MvpConductorDelegateCallback.class);

    Mockito.when(callback.getPresenter()).thenReturn(null);
    Mockito.when(callback.createPresenter()).thenReturn(null);

    MvpConductorLifecycleListener<MvpView, MvpPresenter<MvpView>> lifecycleListener =
        new MvpConductorLifecycleListener<>(callback);

    try {
      lifecycleListener.postCreateView(controller, view);
      Assert.fail("NullpointerException expected");
    } catch (NullPointerException e) {
      Assert.assertEquals(e.getMessage(),
          "Presenter returned from createPresenter() is null in " + callback);
    }
  }
 
Example #16
Source File: MvpViewStateConductorLifecycleTest.java    From mosby-conductor with Apache License 2.0 6 votes vote down vote up
@Test public void nullPointerExceptionOnSaveViewState() {
  Controller controller = PowerMockito.mock(Controller.class);

  MvpViewStateConductorDelegateCallback<MvpView, MvpPresenter<MvpView>, ViewState<MvpView>>
      callback = Mockito.mock(MvpViewStateConductorDelegateCallback.class);

  MvpViewStateConductorLifecycleListener<MvpView, MvpPresenter<MvpView>, ViewState<MvpView>>
      lifecycleListener = new MvpViewStateConductorLifecycleListener<>(callback);

  Mockito.when(callback.getViewState()).thenReturn(null);
  Bundle bundle = new Bundle();

  try {
    lifecycleListener.onSaveViewState(controller, bundle);
    Assert.fail("NullPointerException expected");
  } catch (NullPointerException e) {
    Assert.assertEquals(e.getMessage(), "ViewState from getViewState() is null in " + callback);
  }
}
 
Example #17
Source File: MvpViewStateConductorLifecycleTest.java    From mosby-conductor with Apache License 2.0 6 votes vote down vote up
@Test public void nullPointerOnCreateViewState() {

    MvpPresenter<MvpView> presenter = Mockito.mock(MvpPresenter.class);
    Controller controller = PowerMockito.mock(Controller.class);
    View view = Mockito.mock(View.class);
    MvpView mvpView = Mockito.mock(MvpView.class);

    MvpViewStateConductorDelegateCallback<MvpView, MvpPresenter<MvpView>, ViewState<MvpView>>
        callback = Mockito.mock(MvpViewStateConductorDelegateCallback.class);

    Mockito.when(callback.getPresenter()).thenReturn(presenter);
    Mockito.when(callback.getMvpView()).thenReturn(mvpView);
    Mockito.when(callback.getViewState()).thenReturn(null);
    Mockito.when(callback.createViewState()).thenReturn(null);

    MvpViewStateConductorLifecycleListener<MvpView, MvpPresenter<MvpView>, ViewState<MvpView>>
        lifecycleListener = new MvpViewStateConductorLifecycleListener<>(callback);

    try {
      lifecycleListener.preAttach(controller, view);
      Assert.fail("NullPointerException expected");
    } catch (NullPointerException e) {
      Assert.assertEquals(e.getMessage(),
          "ViewState from createViewState() is null in " + callback);
    }
  }
 
Example #18
Source File: MvpViewStateConductorLifecycleTest.java    From mosby-conductor with Apache License 2.0 6 votes vote down vote up
@Test public void dontRestoreViewStateBecauseViewStateInMemory() {
  MvpPresenter<MvpView> presenter = Mockito.mock(MvpPresenter.class);
  Controller controller = PowerMockito.mock(Controller.class);
  View view = Mockito.mock(View.class);
  MvpView mvpView = Mockito.mock(MvpView.class);
  ViewState<MvpView> viewState = Mockito.mock(ViewState.class);

  MvpViewStateConductorDelegateCallback<MvpView, MvpPresenter<MvpView>, ViewState<MvpView>>
      callback = Mockito.mock(MvpViewStateConductorDelegateCallback.class);

  MvpViewStateConductorLifecycleListener<MvpView, MvpPresenter<MvpView>, ViewState<MvpView>>
      lifecycleListener = new MvpViewStateConductorLifecycleListener<>(callback);

  Mockito.when(callback.getPresenter()).thenReturn(presenter);
  Mockito.when(callback.getMvpView()).thenReturn(mvpView);
  Mockito.when(callback.getViewState()).thenReturn(viewState);

  Bundle bundle = new Bundle();

  lifecycleListener.changingConfigurations = true; // Simulate screen orientation change
  lifecycleListener.postCreateView(controller, view);
  lifecycleListener.onRestoreViewState(controller, bundle);

  Mockito.verify(callback, Mockito.never()).createViewState();
  Mockito.verify(viewState, Mockito.times(1)).apply(Mockito.eq(mvpView), Mockito.eq(true));
}
 
Example #19
Source File: FragmentMvpViewStateDelegateImplTest.java    From mosby with Apache License 2.0 5 votes vote down vote up
private void finishFragment(
    FragmentMvpViewStateDelegateImpl<MvpView, MvpPresenter<MvpView>, ViewState<MvpView>> delegate,
    Bundle bundle) {
  delegate.onPause();
  delegate.onSaveInstanceState(bundle);
  delegate.onStop();
  delegate.onDestroyView();
  delegate.onDestroy();
  delegate.onDetach();
}
 
Example #20
Source File: MvpViewStateConductorLifecycleTest.java    From mosby-conductor with Apache License 2.0 5 votes vote down vote up
@Test public void saveViewState() {

    MvpPresenter<ModelView> presenter = Mockito.mock(MvpPresenter.class);
    Controller controller = PowerMockito.mock(Controller.class);
    View view = Mockito.mock(View.class);
    ModelView mvpView = Mockito.mock(ModelView.class);
    Activity activity = Mockito.mock(Activity.class);

    ParcelableDataLceViewState<Model, ModelView> viewState =
        Mockito.spy(new ParcelableDataLceViewState<Model, ModelView>());

    MvpViewStateConductorDelegateCallback<ModelView, MvpPresenter<ModelView>, ViewState<ModelView>>
        callback = Mockito.spy(new SemiFunctionalCallback(viewState));

    Mockito.when(callback.getPresenter()).thenReturn(presenter);
    Mockito.when(callback.getMvpView()).thenReturn(mvpView);
    Mockito.when(callback.createViewState())
        .thenReturn(new ParcelableDataLceViewState<Model, ModelView>());

    PowerMockito.when(controller.getActivity()).thenReturn(activity);
    Mockito.when(activity.isChangingConfigurations()).thenReturn(false);

    MvpViewStateConductorLifecycleListener<ModelView, MvpPresenter<ModelView>, ViewState<ModelView>>
        lifecycleListener = new MvpViewStateConductorLifecycleListener<>(callback);

    Bundle bundle = Mockito.mock(Bundle.class);

    lifecycleListener.onSaveViewState(controller, bundle);
    lifecycleListener.preDestroyView(controller, view);

    Mockito.verify(bundle, Mockito.times(1))
        .putParcelable(Mockito.eq(ParcelableDataLceViewState.KEY_BUNDLE_VIEW_STATE),
            Mockito.eq(viewState));
  }
 
Example #21
Source File: PresenterManager.java    From mosby with Apache License 2.0 5 votes vote down vote up
/**
 * Puts the presenter into the internal cache
 *
 * @param activity The parent activity
 * @param viewId the view id (mosby internal)
 * @param presenter the presenter
 */
public static void putPresenter(@NonNull Activity activity, @NonNull String viewId,
    @NonNull MvpPresenter<? extends MvpView> presenter) {
  if (activity == null) {
    throw new NullPointerException("Activity is null");
  }

  ActivityScopedCache scopedCache = getOrCreateActivityScopedCache(activity);
  scopedCache.putPresenter(viewId, presenter);
}
 
Example #22
Source File: PresenterManagerTest.java    From mosby with Apache License 2.0 5 votes vote down vote up
@Test
public void putGetRemovePresenter(){
  Activity activity = Mockito.mock(Activity.class);
  Application application = Mockito.mock(Application.class);
  Mockito.when(activity.getApplication()).thenReturn(application);

  MvpPresenter<MvpView> presenter = new MvpPresenter<MvpView>() {
    @Override public void attachView(MvpView view) {
    }

    @Override public void detachView(boolean retainInstance) {
    }

    @Override public void detachView() {
    }

    @Override public void destroy() {
    }
  };

  String viewId ="123";
  Assert.assertNull(PresenterManager.getPresenter(activity, viewId));

  PresenterManager.putPresenter(activity, viewId, presenter);
  Assert.assertTrue(presenter == PresenterManager.getPresenter(activity, viewId));

  PresenterManager.remove(activity, viewId);
  Assert.assertNull(PresenterManager.getPresenter(activity, viewId));
}
 
Example #23
Source File: ActivityMvpDelegateImplTest.java    From mosby with Apache License 2.0 5 votes vote down vote up
@Test public void appStartWithScreenOrientationChangeAndFinallyFinishing() {

    ActivityMvpDelegateImpl<MvpView, MvpPresenter<MvpView>> delegate =
        new ActivityMvpDelegateImpl<>(activity, callback, true);

    startActivity(delegate, null, 1, 1, 1);
    Bundle bundle = BundleMocker.create();
    finishActivity(delegate, bundle, true, 1, 0, true, false);
    startActivity(delegate, bundle, 1, 2, 2);
    finishActivity(delegate, bundle, false, 2, 1, false, true);
  }
 
Example #24
Source File: ActivityMvpDelegateImplTest.java    From mosby with Apache License 2.0 5 votes vote down vote up
@Test public void appStartFinishing() {
  ActivityMvpDelegateImpl<MvpView, MvpPresenter<MvpView>> delegate =
      new ActivityMvpDelegateImpl<>(activity, callback, true);

  startActivity(delegate, null, 1, 1, 1);
  Bundle bundle = BundleMocker.create();
  finishActivity(delegate, bundle, false, 1, 1, false, true);
}
 
Example #25
Source File: ActivityMvpDelegateImplTest.java    From mosby with Apache License 2.0 5 votes vote down vote up
@Test public void dontKeepPresenter() {
  ActivityMvpDelegateImpl<MvpView, MvpPresenter<MvpView>> delegate =
      new ActivityMvpDelegateImpl<>(activity, callback, false);
  startActivity(delegate, null, 1, 1, 1);
  Bundle bundle = BundleMocker.create();
  finishActivity(delegate, bundle, false, 1, 1, true, false);
  startActivity(delegate, bundle, 2, 2, 2);
  finishActivity(delegate, bundle, false, 2, 2, false, true);
}
 
Example #26
Source File: ActivityMvpDelegateImplTest.java    From mosby with Apache License 2.0 5 votes vote down vote up
private void startActivity(ActivityMvpDelegateImpl<MvpView, MvpPresenter<MvpView>> delegate,
    Bundle bundle, int createPresenter, int setPresenter, int attachView) {

  delegate.onCreate(bundle);
  delegate.onContentChanged();
  delegate.onPostCreate(bundle);
  delegate.onStart();
  delegate.onResume();

  Mockito.verify(callback, Mockito.times(createPresenter)).createPresenter();
  Mockito.verify(callback, Mockito.times(setPresenter)).setPresenter(presenter);
  Mockito.verify(presenter, Mockito.times(attachView)).attachView(view);
}
 
Example #27
Source File: MvpViewStateConductorLifecycleTest.java    From mosby-conductor with Apache License 2.0 5 votes vote down vote up
@Test public void dontRestoreViewStateBecauseFirstTimeControllerStarts() {

    MvpPresenter<ModelView> presenter = Mockito.mock(MvpPresenter.class);
    Controller controller = PowerMockito.mock(Controller.class);
    View view = Mockito.mock(View.class);
    ModelView mvpView = Mockito.mock(ModelView.class);

    MvpViewStateConductorDelegateCallback<ModelView, MvpPresenter<ModelView>, ViewState<ModelView>>
        callback = Mockito.mock(MvpViewStateConductorDelegateCallback.class);

    ParcelableDataLceViewState<Model, MvpLceView<Model>> viewState =
        Mockito.spy(new ParcelableDataLceViewState<Model, MvpLceView<Model>>());

    Mockito.when(callback.getPresenter()).thenReturn(presenter);
    Mockito.when(callback.getMvpView()).thenReturn(mvpView);
    Mockito.when(callback.createViewState())
        .thenReturn(new ParcelableDataLceViewState<Model, ModelView>());

    MvpViewStateConductorLifecycleListener<ModelView, MvpPresenter<ModelView>, ViewState<ModelView>>
        lifecycleListener = new MvpViewStateConductorLifecycleListener<>(callback);

    Bundle bundle = Mockito.mock(Bundle.class);

    lifecycleListener.postCreateView(controller, view);
    // lifecycleListener.onRestoreViewState(controller, bundle); Not called because first time starts
    lifecycleListener.preAttach(controller, view);

    Mockito.verify(viewState, Mockito.never()).apply(Mockito.eq(mvpView), Mockito.anyBoolean());
    Mockito.verify(callback, Mockito.times(1)).createViewState();
    Mockito.verify(callback, Mockito.times(1)).setViewState(Mockito.any(ViewState.class));
  }
 
Example #28
Source File: MvpViewStateConductorLifecycleTest.java    From mosby-conductor with Apache License 2.0 5 votes vote down vote up
@Test public void restoreViewStateFromBundle() {

    MvpPresenter<ModelView> presenter = Mockito.mock(MvpPresenter.class);
    Controller controller = PowerMockito.mock(Controller.class);
    View view = Mockito.mock(View.class);
    ModelView mvpView = Mockito.mock(ModelView.class);

    MvpViewStateConductorDelegateCallback<ModelView, MvpPresenter<ModelView>, ViewState<ModelView>>
        callback = Mockito.spy(SemiFunctionalCallback.class);

    ParcelableDataLceViewState<Model, MvpLceView<Model>> viewState =
        Mockito.spy(new ParcelableDataLceViewState<Model, MvpLceView<Model>>());

    Mockito.when(callback.getPresenter()).thenReturn(presenter);
    Mockito.when(callback.getMvpView()).thenReturn(mvpView);
    Mockito.when(callback.createViewState())
        .thenReturn(new ParcelableDataLceViewState<Model, ModelView>());

    MvpViewStateConductorLifecycleListener<ModelView, MvpPresenter<ModelView>, ViewState<ModelView>>
        lifecycleListener = new MvpViewStateConductorLifecycleListener<>(callback);

    Bundle bundle = Mockito.mock(Bundle.class);
    Mockito.when(bundle.getParcelable(AbsParcelableLceViewState.KEY_BUNDLE_VIEW_STATE))
        .thenReturn(viewState);
    viewState.saveInstanceState(bundle);

    lifecycleListener.postCreateView(controller, view);
    lifecycleListener.onRestoreViewState(controller, bundle);
    lifecycleListener.preAttach(controller, view);

    Mockito.verify(callback, Mockito.times(1)).setRestoringViewState(true);
    Mockito.verify(viewState, Mockito.times(1)).apply(Mockito.eq(mvpView), Mockito.eq(false));
    Mockito.verify(callback, Mockito.times(1)).setRestoringViewState(false);
    Mockito.verify(callback, Mockito.times(1)).onViewStateInstanceRestored(false);
  }
 
Example #29
Source File: ViewGroupMvpDelegateImplTest.java    From mosby with Apache License 2.0 5 votes vote down vote up
@Test public void dontKeepPresenter() {
  delegate =
      new ViewGroupMvpDelegateImpl<MvpView, MvpPresenter<MvpView>>(androidView, callback, false);
  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);
}
 
Example #30
Source File: ViewGroupMvpDelegateImplTest.java    From mosby with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
}