Skip to content

Commit

Permalink
working on presenter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
David authored and David committed Sep 11, 2017
1 parent cb5a598 commit d9740ff
Show file tree
Hide file tree
Showing 17 changed files with 381 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;

import java.util.List;

import javax.inject.Inject;

import oxim.digital.reedly.configuration.RandomViewIdGenerator;
import oxim.digital.reedly.configuration.ViewIdGenerator;
import oxim.digital.reedly.dagger.activity.DaggerActivity;
import oxim.digital.reedly.domain.util.ActionRouter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package oxim.digital.reedly.base;

import android.os.Bundle;
import android.util.Log;

import javax.inject.Inject;

import oxim.digital.reedly.configuration.RandomViewIdGenerator;
import oxim.digital.reedly.configuration.ViewIdGenerator;
import oxim.digital.reedly.dagger.fragment.DaggerFragment;
import oxim.digital.reedly.domain.util.ActionRouter;
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/oxim/digital/reedly/base/BasePresenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import javax.inject.Inject;
import javax.inject.Named;

import oxim.digital.reedly.configuration.ViewActionQueue;
import oxim.digital.reedly.configuration.ViewActionHandler;
import oxim.digital.reedly.configuration.ViewActionQueueProvider;
import oxim.digital.reedly.dagger.application.module.ThreadingModule;
import oxim.digital.reedly.data.util.connectivity.ConnectivityReceiver;
Expand Down Expand Up @@ -42,7 +42,7 @@ public abstract class BasePresenter<View extends BaseView> implements ScopedPres
private Subscription viewActionsSubscription;

protected String viewId;
protected ViewActionQueue<View> viewActionQueue;
protected ViewActionHandler<View> viewActionQueue;

private CompositeSubscription subscriptions = new CompositeSubscription();

Expand Down Expand Up @@ -74,7 +74,7 @@ protected void onConnectivityChange(final boolean isConnected) {
public void activate() {
viewActionsSubscription = viewActionQueue.viewActionsObservable()
.observeOn(mainThreadScheduler)
.subscribe(this::onViewAction);
.subscribe(this::onViewAction, Throwable::printStackTrace);
viewActionQueue.resume();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package oxim.digital.reedly.configuration;

import java.util.UUID;

public final class RandomViewIdGenerator implements ViewIdGenerator{

@Override
public String newId() {
return UUID.randomUUID().toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package oxim.digital.reedly.configuration;

import rx.Completable;
import rx.Observable;
import rx.Single;
import rx.functions.Action1;

public interface ViewActionHandler<View> {

void subscribeTo(Observable<Action1<View>> observable, Action1<View> onCompleteAction, Action1<Throwable> errorAction);

void subscribeTo(Single<Action1<View>> single, Action1<Throwable> errorAction);

void subscribeTo(Completable completable, Action1<View> onCompleteAction, Action1<Throwable> errorAction);

void pause();

void resume();

void destroy();

Observable<Action1<View>> viewActionsObservable();
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import rx.subjects.PublishSubject;
import rx.subscriptions.CompositeSubscription;

public final class ViewActionQueue<View> {
public final class ViewActionQueue<View> implements ViewActionHandler<View> {

private final LinkedList<Action1<View>> viewActions = new LinkedList<>();
private final Object queueLock = new Object();
Expand All @@ -27,14 +27,17 @@ public ViewActionQueue(final Scheduler observeScheduler) {

private boolean isPaused;

@Override
public void subscribeTo(final Observable<Action1<View>> observable, final Action1<View> onCompleteAction, final Action1<Throwable> errorAction) {
subscriptions.add(observable.observeOn(observeScheduler).subscribe(this::onResult, errorAction::call, () -> onResult(onCompleteAction)));
}

@Override
public void subscribeTo(final Single<Action1<View>> single, final Action1<Throwable> errorAction) {
subscriptions.add(single.observeOn(observeScheduler).subscribe(this::onResult, errorAction::call));
}

@Override
public void subscribeTo(final Completable completable, final Action1<View> onCompleteAction, final Action1<Throwable> errorAction) {
subscriptions.add(completable.observeOn(observeScheduler).subscribe(() -> onResult(onCompleteAction), errorAction::call));
}
Expand All @@ -49,15 +52,18 @@ private void onResult(final Action1<View> resultAction) {
}
}

@Override
public void pause() {
isPaused = true;
}

@Override
public void resume() {
isPaused = false;
consumeQueue();
}

@Override
public void destroy() {
subscriptions.unsubscribe();
viewActionSubject.onCompleted();
Expand All @@ -74,6 +80,7 @@ private void consumeQueue() {
}
}

@Override
public Observable<Action1<View>> viewActionsObservable() {
return viewActionSubject;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,8 @@
package oxim.digital.reedly.configuration;

import java.util.HashMap;
import java.util.Map;
public interface ViewActionQueueProvider {

import rx.Scheduler;
ViewActionQueue queueFor(String queueId);

public final class ViewActionQueueProvider {

private final Scheduler mainScheduler;

public ViewActionQueueProvider(final Scheduler mainScheduler) {
this.mainScheduler = mainScheduler;
}

private final Map<String, ViewActionQueue> viewActionQueueMap = new HashMap<>();

public ViewActionQueue queueFor(final String queueId) {
final ViewActionQueue viewActionQueue = viewActionQueueMap.get(queueId);
if (viewActionQueue != null) {
return viewActionQueue;
}

final ViewActionQueue newQueue = new ViewActionQueue(mainScheduler);
viewActionQueueMap.put(queueId, newQueue);
return newQueue;
}

public void dispose(final String queueId) {
viewActionQueueMap.remove(queueId);
}
void dispose(String queueId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package oxim.digital.reedly.configuration;

import java.util.HashMap;
import java.util.Map;

import rx.Scheduler;

public final class ViewActionQueueProviderImpl implements ViewActionQueueProvider {

private final Scheduler mainScheduler;
private final Map<String, ViewActionQueue> viewActionQueueMap = new HashMap<>();

public ViewActionQueueProviderImpl(final Scheduler mainScheduler) {
this.mainScheduler = mainScheduler;
}

@Override
public ViewActionQueue queueFor(final String queueId) {
final ViewActionQueue viewActionQueue = viewActionQueueMap.get(queueId);
if (viewActionQueue != null) {
return viewActionQueue;
}

final ViewActionQueue newQueue = new ViewActionQueue(mainScheduler);
viewActionQueueMap.put(queueId, newQueue);
return newQueue;
}

@Override
public void dispose(final String queueId) {
viewActionQueueMap.remove(queueId);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package oxim.digital.reedly.configuration;

import java.util.UUID;
public interface ViewIdGenerator {

public final class ViewIdGenerator {

public String newId() {
return UUID.randomUUID().toString();
}
String newId();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

import dagger.Module;
import dagger.Provides;
import oxim.digital.reedly.configuration.RandomViewIdGenerator;
import oxim.digital.reedly.configuration.ViewActionQueueProvider;
import oxim.digital.reedly.configuration.ViewActionQueueProviderImpl;
import oxim.digital.reedly.configuration.ViewIdGenerator;
import oxim.digital.reedly.dagger.application.ForApplication;
import oxim.digital.reedly.dagger.application.ReedlyApplication;
Expand Down Expand Up @@ -39,13 +41,13 @@ Context provideContext() {
@Provides
@Singleton
ViewIdGenerator provideViewIdGenerator() {
return new ViewIdGenerator();
return new RandomViewIdGenerator();
}

@Provides
@Singleton
ViewActionQueueProvider provideViewActionQueueProvider(final @Named(ThreadingModule.MAIN_SCHEDULER) Scheduler mainScheduler) {
return new ViewActionQueueProvider(mainScheduler);
return new ViewActionQueueProviderImpl(mainScheduler);
}

@Provides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ FeedModelConverter provideFeedModelConverter() {
@Provides
@Singleton
FeedParser provideFeedParser(final CurrentTimeProvider currentTimeProvider, final ExternalParserWrapper externalParserWrapper) {
return new FeedParserImpl(currentTimeProvider, externalParserWrapper);
return new FeedParserImpl(externalParserWrapper);
}

@Provides
@Singleton
ExternalParserWrapper provideExternalParserWrapper(){
return new EarlParserWrapper();
ExternalParserWrapper provideExternalParserWrapper(final CurrentTimeProvider currentTimeProvider) {
return new EarlParserWrapper(currentTimeProvider);
}

@Provides
Expand Down
5 changes: 5 additions & 0 deletions app/src/test/java/oxim/digital/reedly/AppTestData.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@
public final class AppTestData {

public static final int TEST_FEED_ID = 33;
public static final int TEST_ARTICLE_ID = 44;
public static final String TEST_STRING = "bla";
public static final String TEST_LINK = "http://com.com/";
public static final long TEST_LONG_DATE = 1024l;

}
58 changes: 58 additions & 0 deletions app/src/test/java/oxim/digital/reedly/MockViewActionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package oxim.digital.reedly;

import oxim.digital.reedly.configuration.ViewActionHandler;
import rx.Completable;
import rx.Observable;
import rx.Scheduler;
import rx.Single;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import rx.subjects.PublishSubject;

public final class MockViewActionHandler<View> implements ViewActionHandler<View> {

private final PublishSubject<Action1<View>> subject = PublishSubject.create();
private final Scheduler scheduler = Schedulers.immediate();

@Override
public void subscribeTo(final Observable<Action1<View>> observable, final Action1<View> onCompleteAction, final Action1<Throwable> errorAction) {
observable.subscribeOn(scheduler)
.subscribe(this::onResult, errorAction::call);
}

@Override
public void subscribeTo(final Single<Action1<View>> single, final Action1<Throwable> errorAction) {
single.subscribeOn(scheduler)
.subscribe(this::onResult, errorAction::call);
}

@Override
public void subscribeTo(final Completable completable, final Action1<View> onCompleteAction, final Action1<Throwable> errorAction) {
completable.subscribeOn(scheduler)
.subscribe(() -> onResult(onCompleteAction), errorAction::call);
}

private void onResult(final Action1<View> resultAction) {
subject.onNext(resultAction);
}

@Override
public void pause() {
//noop
}

@Override
public void resume() {
//noop
}

@Override
public void destroy() {
//noop
}

@Override
public Observable<Action1<View>> viewActionsObservable() {
return subject;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public final class ArticleContentPresenterTest {

@Before
public void setUp() throws Exception {
// view is webview. this presenter does nothing

}
}
Loading

0 comments on commit d9740ff

Please sign in to comment.