SentenceDetailPresenter extends AbstractMvpPresenter<SentenceDetail.View, SentenceDetailViewModel> implements SentenceDetail.Presenter<SentenceDetail.View, SentenceDetailViewModel> { @Subscribe(threadMode = ThreadMode.MAIN) @Override public void onFetchingAudioEvent(FetchingAudioEvent fetchingAudioEvent) { if (mView == null) { return; } if (fetchingAudioEvent.getFechingResult() == FetchingEvent.RESULT.REFRESHING) { mView.showFetchingResult(R.string.loading); return; } FetchingAudioEvent stickyEvent = getEventBus().getStickyEvent(FetchingAudioEvent.class); if (stickyEvent != null) { if (getViewModel().getCurrentAudioUrl() == null || !getViewModel().getCurrentAudioUrl().equals(stickyEvent.getAudioUrl()) || getViewModel().getPlayToken() != stickyEvent.getToken()) { stickyEvent.setFechingResult(FetchingEvent.RESULT.EXISTED); return; } switch (stickyEvent.getFechingResult()) { case FAIL: mView.showFetchingResult(R.string.download_fail); getViewModel().setAudioStatus(SentenceDetailViewModel.AUDIO_STATUS.DOWNLOADING_FAIL); break; case SUCCESS: getViewModel().setAudioStatus(SentenceDetailViewModel.AUDIO_STATUS.PLAYING); break; case NONE: getViewModel().setAudioStatus(SentenceDetailViewModel.AUDIO_STATUS.DOWNLOADING); break; } stickyEvent.setFechingResult(FetchingEvent.RESULT.EXISTED); updateAudioFabStatusAndPlayIfNeed(); } } SentenceDetailPresenter(SentenceDataRepository sentenceDataRepository, AudioPlayer audioPlayer); void setLoadSentencesUCAndHandler(LoadSentencesUC loadSentencesUC, UseCaseHandler useCaseHandler); void setUpdateFavoriteSentenceUCAndHandler(UpdateFavoriteSentenceUC updateFavoriteSentenceUC, UseCaseHandler useCaseHandler); void setSetPlayStyleUCAndHandler(SetPlayStyleUC setPlayStyleUC, UseCaseHandler useCaseHandler); void setGetPlayStyleUCAndHandler(GetPlayStyleUC getPlayStyleUCForPrepareMenu,
                                            GetPlayStyleUC getPlayStyleUCForPlayAudio,
                                            UseCaseHandler useCaseHandler); void setFetchSentenceAudioUCAndHandler(FetchSentenceAudioUC fetchSentenceAudioUC, UseCaseHandler useCaseHandler); @Override void loadSentences(boolean firstLoad); void loadCurrentSentense(LoadSentenceUC.LoadSentenceCallback callback); @Override void fetchSentenceAudio(); @Override void setPlayStyle(int style); @Override void setFavorite(@NonNull Sentence sentence, boolean favorite); @Override void onAudioFabButtonClicked(); @Subscribe(threadMode = ThreadMode.MAIN) @Override void onFetchingAudioEvent(FetchingAudioEvent fetchingAudioEvent); @Override void onDisplaySentence(Sentence sentence); @Override void onCreate(); @Override void onViewCreated(); @Override void onPrepareOptionsMenu(); @Override void onActivityCreated(); @Override void onResume(); @Override void onStop(); @Override void onDestroy(); @VisibleForTesting EventBus getEventBus();  }
@Test public void testOnFetchingAudioEvent_Refreshing() { FetchingAudioEvent event = new FetchingAudioEvent(true, FetchingEvent.RESULT.REFRESHING, "audio1", System.currentTimeMillis()); mSentenceDetailPresenter.onFetchingAudioEvent(event); verify(mView).showFetchingResult(R.string.loading); }
@Test public void testOnFetchingAudioEvent_NotCurrent_DifferentUrl() { mModel.setCurrentAudioUrl("audio1"); FetchingAudioEvent event = new FetchingAudioEvent(true, FetchingEvent.RESULT.SUCCESS, "audio2", System.currentTimeMillis()); when(mEventBus.getStickyEvent(any(Class.class))).thenReturn(event); mSentenceDetailPresenter.onFetchingAudioEvent(event); verify(mView, times(0)).updateAudioFabAnimator(any(SentenceDetailViewModel.AUDIO_STATUS.class)); }
@Test public void testOnFetchingAudioEvent_NotCurrent_DifferentToken() { mModel.setCurrentAudioUrl("audio1"); mModel.setPlayToken(System.currentTimeMillis() / 2); FetchingAudioEvent event = new FetchingAudioEvent(true, FetchingEvent.RESULT.SUCCESS, "audio1", System.currentTimeMillis()); when(mEventBus.getStickyEvent(any(Class.class))).thenReturn(event); mSentenceDetailPresenter.onFetchingAudioEvent(event); verify(mView, times(0)).updateAudioFabAnimator(any(SentenceDetailViewModel.AUDIO_STATUS.class)); }