PackageListActivityPresenterImpl implements IPackageListActivityPresenter { @Override public void loadPackages(boolean refresh) { if (mView != null) { mView.hideContentView(); mView.showWaitDialog(); mPackageInteractor.getInstalledPackages(refresh) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( items -> { if (mView != null) { if (items.size() > 0) { mView.setInstalledPackages(items); mView.hideWaitDialog(); mView.showContentView(); } else { mView.hideWaitDialog(); mView.setError("No Packages"); } } }, error -> { if (mView != null) { mView.setError(error.getMessage()); mView.hideWaitDialog(); } }); } } @Inject  PackageListActivityPresenterImpl(IPackageInteractor interactor); @Override void setView(IPackageListView view); @Override void clearView(); @Override void setTwoPane(); @Override void init(); @Override void loadPackages(boolean refresh); @Override void packageSelected(InstalledPackageHeader header);  }
@Test public void testLoadPackages() { ArrayList<InstalledPackageHeader> toReturn = new ArrayList<>(); toReturn.add(Mockito.mock(InstalledPackageHeader.class)); when(mPackageInteractor.getInstalledPackages(false)).thenReturn(Observable.just(toReturn)); mPresenter.loadPackages(false); verify(mView, times(1)).hideContentView(); verify(mView, times(1)).showWaitDialog(); verify(mView, times(1)).setInstalledPackages(toReturn); verify(mView, times(1)).showContentView(); verify(mView, times(1)).hideWaitDialog(); }
@Test public void testLoadPackagesFail() { String errorMessage = "Test Error"; when(mPackageInteractor.getInstalledPackages(false)).thenReturn(Observable.error(new Throwable(errorMessage))); mPresenter.loadPackages(false); verify(mView, times(1)).hideContentView(); verify(mView, times(1)).showWaitDialog(); verify(mView, times(1)).hideWaitDialog(); verify(mView, times(1)).setError(errorMessage); verify(mView, never()).setInstalledPackages(anyListOf(InstalledPackageHeader.class)); verify(mView, never()).showContentView(); }
@Test public void testEmptyListOfPackages() { String errorMessage = "No Packages"; when(mPackageInteractor.getInstalledPackages(false)).thenReturn(Observable.error(new Throwable(errorMessage))); mPresenter.loadPackages(false); verify(mView, times(1)).hideContentView(); verify(mView, times(1)).showWaitDialog(); verify(mView, times(1)).hideWaitDialog(); verify(mView, times(1)).setError(errorMessage); verify(mView, never()).setInstalledPackages(anyListOf(InstalledPackageHeader.class)); verify(mView, never()).showContentView(); }