> o;
-
- RxDiffResult(Flowable> o) {
- this.o = o.share().observeOn(AndroidSchedulers.mainThread());
- }
-
- /**
- * @param onUpdate callback to update the data set
- * @return a {@link Completable} to apply
- */
- @NonNull
- public Completable applyDiff(@NonNull BiConsumer super A, ? super T> onUpdate) {
- return o.doOnNext(result -> result.applyDiff(onUpdate)).share().ignoreElements();
- }
-}
diff --git a/library/src/main/java/berlin/volders/rxdiff2/RxDiffUtil.java b/library/src/main/java/berlin/volders/rxdiff2/RxDiffUtil.java
deleted file mode 100644
index 9bbcdded290f260c58ecb5a9a1c8191f3463b544..0000000000000000000000000000000000000000
--- a/library/src/main/java/berlin/volders/rxdiff2/RxDiffUtil.java
+++ /dev/null
@@ -1,126 +0,0 @@
-/*
- * Copyright (C) 2017 volders GmbH with <3 in Berlin
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package berlin.volders.rxdiff2;
-
-import androidx.annotation.NonNull;
-import androidx.annotation.VisibleForTesting;
-
-import java.lang.ref.WeakReference;
-
-import io.reactivex.Completable;
-import io.reactivex.Flowable;
-import io.reactivex.functions.BiFunction;
-import io.reactivex.functions.Function;
-
-import static androidx.recyclerview.widget.DiffUtil.Callback;
-import static androidx.recyclerview.widget.RecyclerView.Adapter;
-
-/**
- * {@code RxDiffUtil} calculates and applies the diff between new and old data.
- * The {@code RxDiffUtil} instance should be applied to an flowable with the
- * {@link Flowable#to(Function)} method. After chaining all actions, this also
- * transforms the {@link Flowable} into a shared {@link Completable}.
- *
- * service.observeData()
- * .compose(transformer)
- * .onBackpressureLatest()
- * .to(RxDiffUtil.with(adapter))
- * .calculateDiff(callback)
- * .applyDiff(adapter::setUnsafe)
- * .subscribe();
- *
- *
- * @param type of the data set
- * @param type of the adapter
- */
-@SuppressWarnings("WeakerAccess")
-public class RxDiffUtil {
-
- @VisibleForTesting
- final WeakReference adapter;
- @VisibleForTesting
- final Flowable o;
-
- RxDiffUtil(WeakReference adapter, Flowable o) {
- this.adapter = adapter;
- this.o = o;
- }
-
- /**
- * @param cb callback to provide the {@link Callback} to calculate the diff
- * @return an {@link RxDiffResult} to apply to the adapter
- */
- @NonNull
- public RxDiffResult calculateDiff(@NonNull BiFunction cb) {
- return calculateDiff(cb, true);
- }
-
- /**
- * @param cb callback to provide the {@link Callback} to calculate the diff
- * @param dm should try to detect moved items
- * @return an {@link RxDiffResult} to apply to the adapter
- */
- @NonNull
- public RxDiffResult calculateDiff(@NonNull BiFunction cb, boolean dm) {
- return new RxDiffResult<>(o.lift(s -> new OnCalculateDiffSubscriber<>(s, adapter, cb, dm)));
- }
-
- /**
- * @param o old data to use to calculate the diff
- * @param cb callback to provide the {@link Callback} to calculate the diff
- * @return an {@link RxDiffResult} to apply to the adapter
- */
- @NonNull
- public RxDiffResult calculateDiff(@NonNull Function super A, ? extends T> o,
- @NonNull BiFunction cb) {
- return calculateDiff(o, cb, true);
- }
-
- /**
- * @param o old data to use to calculate the diff
- * @param cb callback to provide the {@link Callback} to calculate the diff
- * @param dm should try to detect moved items
- * @return an {@link RxDiffResult} to apply to the adapter
- */
- @NonNull
- public RxDiffResult calculateDiff(@NonNull Function super A, ? extends T> o,
- @NonNull BiFunction cb, boolean dm) {
- return calculateDiff((a, n) -> cb.apply(o.apply(a), n), dm);
- }
-
- /**
- * @param adapter the adapter to apply the diff to
- * @param type of the data set
- * @param type of the adapter
- * @return a transformer function to use with {@link Flowable#to(Function)}
- */
- @NonNull
- public static Function, RxDiffUtil>
- with(final A adapter) {
- WeakReference adapterReference = new WeakReference<>(adapter);
- return o -> new RxDiffUtil<>(adapterReference, o);
- }
-
- /**
- * Exception thrown if the {@link Flowable} modified with {@code RxDiffUtil}
- * is still active after the adapter was cleared.
- */
- public static class SubscriptionLeak extends IllegalStateException {
- SubscriptionLeak() {
- }
- }
-}
diff --git a/library/src/test/java/berlin/volders/rxdiff2/OnCalculateDiffResultTest.java b/library/src/test/java/berlin/volders/rxdiff2/OnCalculateDiffResultTest.java
deleted file mode 100644
index 5b4225548e4e1622a86776521eb7350f8142d0b1..0000000000000000000000000000000000000000
--- a/library/src/test/java/berlin/volders/rxdiff2/OnCalculateDiffResultTest.java
+++ /dev/null
@@ -1,141 +0,0 @@
-/*
- * Copyright (C) 2017 volders GmbH with <3 in Berlin
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package berlin.volders.rxdiff2;
-
-import androidx.recyclerview.widget.RecyclerView.Adapter;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
-
-import java.lang.ref.WeakReference;
-import java.util.ConcurrentModificationException;
-
-import io.reactivex.functions.Action;
-import io.reactivex.functions.BiConsumer;
-import io.reactivex.functions.BiFunction;
-
-import static androidx.recyclerview.widget.DiffUtil.Callback;
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.verify;
-
-@SuppressWarnings({"unchecked", "WeakerAccess"})
-@RunWith(MockitoJUnitRunner.class)
-public class OnCalculateDiffResultTest {
-
- @Mock
- Adapter adapter;
- @Mock
- BiFunction callback;
- @Mock
- Action producer;
- @Mock
- Callback cb;
- @Mock
- BiConsumer empty;
-
- OnCalculateDiffResult, ?> result;
-
- @Before
- public void setup() throws Exception {
- doReturn(cb).when(callback).apply(any(Adapter.class), any());
- result = new OnCalculateDiffResult<>(new WeakReference<>(adapter), null, callback, false, producer);
- }
-
- @Test
- public void applyDiff() throws Exception {
- result.applyDiff(empty);
-
- verify(producer).run();
- }
-
- @Test(expected = ConcurrentModificationException.class)
- public void applyDiff_concurrently() throws Exception {
- result.onChanged();
-
- result.applyDiff(empty);
- }
-
- @Test
- public void onChanged() {
- result.onChanged();
-
- assertThat(result.invalidated, is(true));
- }
-
- @Test
- public void onItemRangeChanged() {
- result.onItemRangeChanged(0, 1);
-
- assertThat(result.invalidated, is(true));
- }
-
- @Test
- public void onItemRangeChanged_payload() {
- result.onItemRangeChanged(0, 1, null);
-
- assertThat(result.invalidated, is(true));
- }
-
- @Test
- public void onItemRangeInserted() {
- result.onItemRangeInserted(0, 1);
-
- assertThat(result.invalidated, is(true));
- }
-
- @Test
- public void onItemRangeRemoved() {
- result.onItemRangeRemoved(0, 1);
-
- assertThat(result.invalidated, is(true));
- }
-
- @Test
- public void onItemRangeMoved() {
- result.onItemRangeMoved(0, 1, 2);
-
- assertThat(result.invalidated, is(true));
- }
-
- @Test
- public void nonLeaking() {
- assertThat(OnCalculateDiffResult.nonLeaking(new WeakReference<>(adapter)), is(adapter));
- }
-
- @Test(expected = RxDiffUtil.SubscriptionLeak.class)
- public void nonLeaking_leaking() {
- OnCalculateDiffResult.nonLeaking(new WeakReference(null));
- }
-
- @Test
- public void checkConcurrency() {
- result.checkConcurrency(adapter);
- }
-
- @Test(expected = ConcurrentModificationException.class)
- public void checkConcurrency_changed() {
- result.onChanged();
-
- result.checkConcurrency(adapter);
- }
-}
diff --git a/library/src/test/java/berlin/volders/rxdiff2/OnCalculateDiffSubscriberTest.java b/library/src/test/java/berlin/volders/rxdiff2/OnCalculateDiffSubscriberTest.java
deleted file mode 100644
index 6f73faf37327eacd154fe81b5d98ca1c95a629e4..0000000000000000000000000000000000000000
--- a/library/src/test/java/berlin/volders/rxdiff2/OnCalculateDiffSubscriberTest.java
+++ /dev/null
@@ -1,140 +0,0 @@
-/*
- * Copyright (C) 2018 Christian Schmitz
- * Copyright (C) 2017 volders GmbH with <3 in Berlin
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package berlin.volders.rxdiff2;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.ArgumentCaptor;
-import org.mockito.Captor;
-import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
-import org.reactivestreams.Subscriber;
-import org.reactivestreams.Subscription;
-
-import java.lang.ref.WeakReference;
-
-import io.reactivex.functions.BiFunction;
-
-import static androidx.recyclerview.widget.DiffUtil.Callback;
-import static androidx.recyclerview.widget.RecyclerView.Adapter;
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.CoreMatchers.notNullValue;
-import static org.junit.Assert.assertThat;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.doThrow;
-import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-
-@SuppressWarnings({"unchecked", "WeakerAccess"})
-@RunWith(MockitoJUnitRunner.class)
-public class OnCalculateDiffSubscriberTest {
-
- @Captor
- ArgumentCaptor> result;
-
- @Mock
- Adapter adapter;
- @Mock
- BiFunction callback;
- @Mock
- Callback cb;
- @Mock
- Subscriber subscriber;
- @Mock
- Subscription subscription;
-
- OnCalculateDiffSubscriber diff;
-
- @Before
- public void setup() throws Exception {
- doReturn(cb).when(callback).apply(any(Adapter.class), any());
- diff = new OnCalculateDiffSubscriber(subscriber, new WeakReference(adapter), callback, false);
- diff.onSubscribe(subscription);
- }
-
- @Test
- public void onSubscribe() {
- verify(subscriber).onSubscribe(diff);
- verify(subscription).request(1);
- }
-
- @Test
- public void onNext() {
- String value = "foobar";
-
- diff.onNext(value);
-
- verify(subscriber, never()).onError(any());
- verify(subscriber, never()).onComplete();
- verify(subscriber).onNext(result.capture());
- OnCalculateDiffResult actual = result.getValue();
- assertThat(actual.adapter.get(), is(diff.adapter.get()));
- assertThat(actual.o, is(value));
- assertThat(actual.diff, notNullValue());
- }
-
- @Test
- public void onNext_error() throws Exception {
- RuntimeException e = new RuntimeException();
- doThrow(e).when(callback).apply(any(), any());
-
-
- diff.onNext("");
-
- verify(subscriber).onError(e);
- verify(subscriber, never()).onComplete();
- verify(subscriber, never()).onNext(any());
- }
-
- @Test
- public void onCompleted() {
- diff.onComplete();
-
- verify(subscriber, never()).onError(any());
- verify(subscriber).onComplete();
- verify(subscriber, never()).onNext(any());
- }
-
- @Test
- public void onError() {
- Throwable e = new Throwable();
-
- diff.onError(e);
-
- verify(subscriber).onError(e);
- verify(subscriber, never()).onComplete();
- verify(subscriber, never()).onNext(any());
- }
-
- @Test
- public void cancel() {
- diff.cancel();
-
- verify(subscription).cancel();
- }
-
- @Test
- public void run() {
- diff.run();
-
- verify(subscription, times(2)).request(1);
- }
-}
diff --git a/library/src/test/java/berlin/volders/rxdiff2/RxDiffResultTest.java b/library/src/test/java/berlin/volders/rxdiff2/RxDiffResultTest.java
deleted file mode 100644
index 1d89410b926b60da8288ae8edcd0d2791ad5d544..0000000000000000000000000000000000000000
--- a/library/src/test/java/berlin/volders/rxdiff2/RxDiffResultTest.java
+++ /dev/null
@@ -1,102 +0,0 @@
-/*
- * Copyright (C) 2018 Christian Schmitz
- * Copyright (C) 2017 volders GmbH with <3 in Berlin
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package berlin.volders.rxdiff2;
-
-import androidx.recyclerview.widget.RecyclerView.Adapter;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.robolectric.RobolectricTestRunner;
-
-import java.lang.ref.WeakReference;
-import java.util.ConcurrentModificationException;
-
-import io.reactivex.functions.Action;
-import io.reactivex.functions.BiConsumer;
-import io.reactivex.functions.BiFunction;
-import io.reactivex.observers.TestObserver;
-import io.reactivex.processors.PublishProcessor;
-
-import static androidx.recyclerview.widget.DiffUtil.Callback;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.verify;
-import static org.mockito.MockitoAnnotations.initMocks;
-
-@SuppressWarnings({"unchecked", "WeakerAccess"})
-@RunWith(RobolectricTestRunner.class)
-public class RxDiffResultTest {
-
- @Mock
- BiConsumer action;
- @Mock
- Adapter adapter;
- @Mock
- BiFunction callback;
- @Mock
- Action producer;
- @Mock
- Callback cb;
-
- RxDiffResult rxDiffResult;
- PublishProcessor emitter;
-
- @Before
- public void setup() throws Exception {
- initMocks(this);
- doReturn(cb).when(callback).apply(any(Adapter.class), any());
- emitter = PublishProcessor.create();
- rxDiffResult = new RxDiffResult(emitter);
- }
-
- @Test
- public void applyDiff() throws Exception {
- TestObserver subscriber = rxDiffResult.applyDiff(action).test();
-
- emitResult(1);
- emitResult(2);
-
- verify(action).accept(adapter, 1);
- verify(action).accept(adapter, 2);
- subscriber.assertSubscribed()
- .assertNoErrors()
- .assertNotComplete()
- .assertNoValues();
- }
-
- @Test
- @SuppressWarnings("ResultOfMethodCallIgnored")
- public void applyDiff_concurrently() throws Exception {
- rxDiffResult.applyDiff(action).test();
- TestObserver subscriber = rxDiffResult.applyDiff(action).test();
-
- emitResult(1);
-
- verify(action).accept(adapter, 1);
- subscriber.assertSubscribed()
- .assertError(ConcurrentModificationException.class)
- .assertNotComplete()
- .assertNoValues();
- }
-
- void emitResult(int i) throws Exception {
- emitter.onNext(new OnCalculateDiffResult(new WeakReference(adapter), i, callback, true, producer));
- }
-}
diff --git a/library/src/test/java/berlin/volders/rxdiff2/RxDiffUtilAndroidTest.java b/library/src/test/java/berlin/volders/rxdiff2/RxDiffUtilAndroidTest.java
deleted file mode 100644
index 8d9ee434ca9a1b87569acf9c02399bbfaab5f333..0000000000000000000000000000000000000000
--- a/library/src/test/java/berlin/volders/rxdiff2/RxDiffUtilAndroidTest.java
+++ /dev/null
@@ -1,159 +0,0 @@
-/*
- * Copyright (C) 2018 Christian Schmitz
- * Copyright (C) 2017 volders GmbH with <3 in Berlin
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package berlin.volders.rxdiff2;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.robolectric.RobolectricTestRunner;
-
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.ConcurrentModificationException;
-import java.util.List;
-
-import berlin.volders.rxdiff2.test.AndroidTestAdapter;
-import berlin.volders.rxdiff2.test.AndroidTestFunction;
-import io.reactivex.Completable;
-import io.reactivex.Flowable;
-import io.reactivex.functions.Function;
-import io.reactivex.observers.TestObserver;
-
-import static io.reactivex.Flowable.fromIterable;
-
-@SuppressWarnings({"unchecked", "WeakerAccess"})
-@RunWith(RobolectricTestRunner.class)
-public class RxDiffUtilAndroidTest {
-
- static final List> values = Arrays.asList(
- Collections.emptyList(),
- Collections.singletonList("single"),
- Arrays.asList("one", "two"),
- Collections.emptyList(),
- Arrays.asList("one", "two", "three")
- );
-
- AndroidTestAdapter> adapter;
- Function>, Completable> rxDiff;
-
- @Before
- public void setup() {
- adapter = new AndroidTestAdapter<>(l -> l == null ? 0 : l.size(), values.get(0));
- rxDiff = new AndroidTestFunction<>(adapter, adapter);
- }
-
- @Test
- public void applyDiff_empty() {
- TestObserver observer = adapter.test();
- TestObserver subscriber = fromIterable(values)
- .take(1)
- .to(rxDiff)
- .test();
- subscriber.awaitTerminalEvent();
-
- subscriber.assertSubscribed()
- .assertNoErrors()
- .assertComplete()
- .assertNoValues();
- observer.assertValue(values.get(0));
- }
-
- @Test
- public void applyDiff_full() {
- TestObserver observer = adapter.test();
- TestObserver subscriber = fromIterable(values)
- .skip(1)
- .take(1)
- .to(rxDiff)
- .test();
- subscriber.awaitTerminalEvent();
-
- subscriber.assertSubscribed()
- .assertNoErrors()
- .assertComplete()
- .assertNoValues();
- observer.assertValue(values.get(1));
- }
-
- @Test
- public void applyDiff_full_empty() {
- TestObserver observer = adapter.test();
- TestObserver subscriber = fromIterable(values)
- .skip(2)
- .take(2)
- .to(rxDiff)
- .test();
- subscriber.awaitTerminalEvent();
-
- subscriber.assertSubscribed()
- .assertNoErrors()
- .assertComplete()
- .assertNoValues();
- observer.assertValues(values.get(2), values.get(3));
- }
-
- @Test
- public void applyDiff_full_full() {
- TestObserver observer = adapter.test();
- TestObserver subscriber = fromIterable(values)
- .skip(1)
- .take(2)
- .to(rxDiff)
- .test();
- subscriber.awaitTerminalEvent();
-
- subscriber.assertSubscribed()
- .assertNoErrors()
- .assertComplete()
- .assertNoValues();
- observer.assertValues(values.get(1), values.get(2));
- }
-
- @Test
- public void applyDiff_stream() {
- TestObserver observer = adapter.test();
- TestObserver subscriber = fromIterable(values)
- .to(rxDiff)
- .test();
- subscriber.awaitTerminalEvent();
-
- subscriber.assertSubscribed()
- .assertNoErrors()
- .assertComplete()
- .assertNoValues();
- observer.assertValues(values.toArray(new List[0]));
- }
-
- @Test
- public void applyDiff_concurrently() {
- rxDiff = new AndroidTestFunction<>(adapter, adapter.notifyOnGet());
-
- TestObserver observer = adapter.test();
- TestObserver subscriber = fromIterable(values)
- .skip(1)
- .to(rxDiff)
- .test();
- subscriber.awaitTerminalEvent();
-
- subscriber.assertSubscribed()
- .assertError(ConcurrentModificationException.class)
- .assertNotComplete()
- .assertNoValues();
- observer.assertNoValues();
- }
-}
diff --git a/library/src/test/java/berlin/volders/rxdiff2/RxDiffUtilTest.java b/library/src/test/java/berlin/volders/rxdiff2/RxDiffUtilTest.java
deleted file mode 100644
index 702bee97608bb7a655e0542a410ab0f18e7d0722..0000000000000000000000000000000000000000
--- a/library/src/test/java/berlin/volders/rxdiff2/RxDiffUtilTest.java
+++ /dev/null
@@ -1,140 +0,0 @@
-/*
- * Copyright (C) 2018 Christian Schmitz
- * Copyright (C) 2017 volders GmbH with <3 in Berlin
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package berlin.volders.rxdiff2;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.robolectric.RobolectricTestRunner;
-
-import java.lang.ref.WeakReference;
-
-import io.reactivex.Flowable;
-import io.reactivex.functions.BiFunction;
-import io.reactivex.functions.Function;
-
-import static androidx.recyclerview.widget.DiffUtil.Callback;
-import static androidx.recyclerview.widget.RecyclerView.Adapter;
-import static io.reactivex.Flowable.empty;
-import static io.reactivex.Flowable.never;
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.verify;
-import static org.mockito.MockitoAnnotations.initMocks;
-
-@SuppressWarnings({"unchecked", "WeakerAccess"})
-@RunWith(RobolectricTestRunner.class)
-public class RxDiffUtilTest {
-
- @Mock
- Adapter adapter;
- @Mock
- BiFunction callback;
- @Mock
- Function function;
- @Mock
- Callback cb;
-
- RxDiffUtil rxDiffUtil;
-
- @Before
- public void setup() throws Exception {
- initMocks(this);
- doReturn(cb).when(callback).apply(any(), any());
- rxDiffUtil = new RxDiffUtil(new WeakReference(adapter), never().startWith(1));
- }
-
- @Test
- public void calculateDiff_callback() throws Exception {
- OnCalculateDiffResult result = (OnCalculateDiffResult) rxDiffUtil
- .calculateDiff(callback)
- .o.test()
- .assertSubscribed()
- .assertNoErrors()
- .assertNotComplete()
- .assertValueCount(1)
- .values().get(0);
-
- assertThat(result.adapter.get(), is(adapter));
- assertThat(result.o, is(1));
- verify(callback).apply(any(Adapter.class), any());
- }
-
- @Test
- public void calculateDiff_callback_detectMoves() throws Exception {
- OnCalculateDiffResult result = (OnCalculateDiffResult) rxDiffUtil
- .calculateDiff(callback, false)
- .o.test()
- .assertSubscribed()
- .assertNoErrors()
- .assertNotComplete()
- .assertValueCount(1)
- .values().get(0);
-
- assertThat(result.adapter.get(), is(adapter));
- assertThat(result.o, is(1));
- verify(callback).apply(any(Adapter.class), any());
- }
-
- @Test
- public void calculateDiff_callback2() throws Exception {
- OnCalculateDiffResult result = (OnCalculateDiffResult) rxDiffUtil
- .calculateDiff(function, callback)
- .o.test()
- .assertSubscribed()
- .assertNoErrors()
- .assertNotComplete()
- .assertValueCount(1)
- .values().get(0);
-
- assertThat(result.adapter.get(), is(adapter));
- assertThat(result.o, is(1));
- verify(callback).apply(any(), any());
- verify(function).apply(any());
- }
-
- @Test
- public void calculateDiff_callback2_detectMoves() throws Exception {
- OnCalculateDiffResult result = (OnCalculateDiffResult) rxDiffUtil
- .calculateDiff(function, callback, false)
- .o.test()
- .assertSubscribed()
- .assertNoErrors()
- .assertNotComplete()
- .assertValueCount(1)
- .values().get(0);
-
- assertThat(result.adapter.get(), is(adapter));
- assertThat(result.o, is(1));
- verify(callback).apply(any(), any());
- verify(function).apply(any());
- }
-
- @Test
- public void with() throws Exception {
- Function, RxDiffUtil> factory = RxDiffUtil.with(adapter);
- Flowable