Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hook for onChanged value #36

Merged
merged 3 commits into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add onChangedValue method to hook and receive each value
  • Loading branch information
jraska committed Feb 12, 2019
commit 01d9902f16a4a86fc6af932e200c738510711b90
30 changes: 25 additions & 5 deletions testing/src/main/java/com/jraska/livedata/TestObserver.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

public final class TestObserver<T> implements Observer<T> {
private final List<T> valueHistory = new ArrayList<>();
private final List<Consumer<TestObserver<T>>> onChangedConsumers = new ArrayList<>();
private final List<Consumer<T>> onChangedConsumers = new ArrayList<>();
private CountDownLatch valueLatch = new CountDownLatch(1);

private TestObserver() {
Expand All @@ -25,8 +25,8 @@ public void onChanged(@Nullable T value) {
valueHistory.add(value);
valueLatch.countDown();

for (Consumer<TestObserver<T>> consumer : onChangedConsumers) {
consumer.accept(this);
for (Consumer<T> consumer : onChangedConsumers) {
consumer.accept(value);
}
}

Expand Down Expand Up @@ -167,12 +167,18 @@ public <N> TestObserver<N> map(Function<T, N> mapper) {
newObserver.onChanged(mapper.apply(value));
}

onValueChanged(observer -> newObserver.onChanged(mapper.apply(observer.value())));
onValueChanged(new Map<>(newObserver, mapper));

return newObserver;
}

public TestObserver<T> onValueChanged(Consumer<TestObserver<T>> onObserverValue) {
/**
* Adds a Consumer which will be triggered on each value change to allow assertion on the value.
*
* @param onObserverValue Consumer to call when new value is received
* @return this
*/
public TestObserver<T> onValueChanged(Consumer<T> onObserverValue) {
onChangedConsumers.add(onObserverValue);
return this;
}
Expand Down Expand Up @@ -253,4 +259,18 @@ public static <T> TestObserver<T> test(LiveData<T> liveData) {
liveData.observeForever(observer);
return observer;
}

static final class Map<T, N> implements Consumer<T> {
private final TestObserver<N> newObserver;
private final Function<T, N> mapper;

Map(TestObserver<N> newObserver, Function<T, N> mapper) {
this.newObserver = newObserver;
this.mapper = mapper;
}

@Override public void accept(T value) {
newObserver.onChanged(mapper.apply(value));
}
}
}
27 changes: 27 additions & 0 deletions testing/src/test/java/com/jraska/livedata/TestObserverTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package com.jraska.livedata
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.lifecycle.MutableLiveData
import org.assertj.core.api.Assertions.assertThat
import org.junit.ComparisonFailure
import org.junit.Rule
import org.junit.Test
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger

class TestObserverTest {
@get:Rule val testRule = InstantTaskExecutorRule()
Expand Down Expand Up @@ -135,6 +137,22 @@ class TestObserverTest {
testObserver.map { it.toString() }.assertValue("4")
}

@Test
fun whenOnValueChanged_thenTriggersProperly() {
val triggeredCount = AtomicInteger()

TestObserver.test(testLiveData)
.map { it * 2 }
.onValueChanged { triggeredCount.incrementAndGet() }

val expectedTriggerCount = 10
repeat(expectedTriggerCount) {
testLiveData.value = 123456
}

assertThat(triggeredCount.get()).isEqualTo(expectedTriggerCount)
}

@Test(expected = AssertionError::class)
fun hasValuesAssertionFailsOnNoValue() {
TestObserver.test(testLiveData).assertHasValue()
Expand Down Expand Up @@ -178,6 +196,15 @@ class TestObserverTest {
testObserver.assertNever { it == 4 }
}

@Test(expected = ComparisonFailure::class)
fun whenOnValueChanged_thenFailsProperly() {
TestObserver.test(testLiveData)
.map { it * 2 }
.onValueChanged { assertThat(it).isEqualTo(1) }

testLiveData.value = 1
}

private fun setValueAsOne() = Runnable {
Thread.sleep(10)
testLiveData.value = 1
Expand Down