SentenceListPresenter extends AbstractMvpPresenter<SentenceList.View, SentenceListViewModel> implements SentenceList.Presenter<SentenceList.View, SentenceListViewModel> { @Override public void loadSentences(final boolean firstLoad) { if (firstLoad) { mView.setLoadingIndicator(true); mLoadSentenceCallback = new LoadSentencesUC.LoadSentencesCallback() { @Override public void onSentencesGet(List<? extends Sentence> sentences) { if (mView != null) { if (sentences == null || sentences.isEmpty()) { mView.showNoSentences(getViewModel().isFavoriteList()); } else { mView.showSentenceList(sentences); if (getViewModel().isNeedAutoFocused()) { int needFocusedPosition = findNeedFocusedPosition(sentences); if (needFocusedPosition >= 0) { mView.setSelection(needFocusedPosition); } setNeedAutoFocused(false); } } } } }; } mLoadSentencesUC.requestParams(new LoadSentencesUC.LoadSentencesRequestParms(firstLoad, getViewModel().isFavoriteList())) .callback(mLoadSentenceCallback) .execute(mLoadSentencesUseCaseHandler); } SentenceListPresenter(); void setLoadSentencesUCAndHandler(LoadSentencesUC loadSentencesUC, UseCaseHandler useCaseHandler); void setUpdateFavoriteSentenceUCAndHandler(UpdateFavoriteSentenceUC updateFavoriteSentenceUC, UseCaseHandler useCaseHandler); @Override void loadSentences(final boolean firstLoad); @Override void openSentence(long sentenceId); @Override void setFavorite(@NonNull Sentence sentence, boolean favorite); @Override void onCreate(); @Override void onActivityCreated(); @Subscribe @Override void onSentenceChanged(SentenceChangedEvent event); @Override void setNeedAutoFocused(boolean needAutoFocused); @Override void onDestroy(); @VisibleForTesting EventBus getEventBus();  }
@Test public void testLoadSentences_First_EmptyList() { mSentenceListPresenter.loadSentences(true); verify(mView).setLoadingIndicator(true); verify(mLoadSentencesUC).requestParams(mLoadSentencesRequestParmsCaptor.capture()); assertFalse(mLoadSentencesRequestParmsCaptor.getValue().isFavorite()); assertTrue(mLoadSentencesRequestParmsCaptor.getValue().isFirstLoad()); verify(mLoadSentencesUC).callback(mLoadSentencesCallbackCaptor.capture()); LoadSentencesUC.LoadSentencesCallback callback = mLoadSentencesCallbackCaptor.getValue(); assertNotNull(callback); verify(mLoadSentencesUC).execute(mLoadSentencesUseCaseHandler); callback.onSentencesGet(new ArrayList<Sentence>()); verify(mView).showNoSentences(false); }
@Test public void testLoadSentences_First_NotEmptyList_NoNeedAutoFocused() { mSentenceListPresenter.loadSentences(true); verify(mView).setLoadingIndicator(true); verify(mLoadSentencesUC).requestParams(mLoadSentencesRequestParmsCaptor.capture()); assertFalse(mLoadSentencesRequestParmsCaptor.getValue().isFavorite()); assertTrue(mLoadSentencesRequestParmsCaptor.getValue().isFirstLoad()); verify(mLoadSentencesUC).callback(mLoadSentencesCallbackCaptor.capture()); LoadSentencesUC.LoadSentencesCallback callback = mLoadSentencesCallbackCaptor.getValue(); assertNotNull(callback); verify(mLoadSentencesUC).execute(mLoadSentencesUseCaseHandler); when(mSentences.size()).thenReturn(3); callback.onSentencesGet(mSentences); verify(mView).showSentenceList(mSentences); }
@Test public void testLoadSentences_NotFirst() { mSentenceListPresenter.loadSentences(false); verify(mView, times(0)).setLoadingIndicator(true); verify(mLoadSentencesUC).requestParams(mLoadSentencesRequestParmsCaptor.capture()); assertFalse(mLoadSentencesRequestParmsCaptor.getValue().isFavorite()); assertFalse(mLoadSentencesRequestParmsCaptor.getValue().isFirstLoad()); verify(mLoadSentencesUC).callback(mLoadSentencesCallbackCaptor.capture()); LoadSentencesUC.LoadSentencesCallback callback = mLoadSentencesCallbackCaptor.getValue(); assertNull(callback); verify(mLoadSentencesUC).execute(mLoadSentencesUseCaseHandler); }