ChangesDetector { public void newData(@Nonnull ChangesAdapter adapter, @Nonnull List<T> values, boolean force) { checkNotNull(adapter); checkNotNull(values); final H[] list = apply(values); final LinkedList<H> objects = new LinkedList<>(); Collections.addAll(objects, mItems); clearState(); int successPosition = 0; for (; successPosition < list.length;) { final H item = list[successPosition]; final int i = indexOfItem(objects, item); if (i < 0) { executeAction(adapter, ACTION_INSERT, successPosition); successPosition += 1; } else if (i == 0) { if (force || !mDetector.same(item, objects.get(0))) { executeAction(adapter, ACTION_CHANGED, successPosition); } else { executeAction(adapter, ACTION_NONE, successPosition); } objects.remove(0); successPosition += 1; } else { final H first = objects.get(0); if (existInList(list, successPosition + 1, first)) { adapter.notifyItemMoved(i + successPosition, successPosition); if (force || !mDetector.same(item, objects.get(i))) { executeAction(adapter, ACTION_CHANGED, successPosition); } else { executeAction(adapter, ACTION_NONE, successPosition); } objects.remove(i); successPosition += 1; } else { executeAction(adapter, ACTION_REMOVE, successPosition); objects.remove(0); } } } executeAction(adapter, ACTION_NONE, successPosition); if (objects.size() > 0) { adapter.notifyItemRangeRemoved(successPosition, objects.size()); objects.clear(); } mItems = list; } ChangesDetector(@Nonnull Detector<T, H> detector); void newData(@Nonnull ChangesAdapter adapter,
                        @Nonnull List<T> values,
                        boolean force);  }
@Test public void testStart() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(1)), false); verify(adapter).notifyItemRangeInserted(0, 1); }
@Test public void testAfterChangeOrder_orderIsChangeIsNotified1() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(0), new Cat(1)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(0)), false); verify(adapter).notifyItemMoved(1, 0); verifyNoMoreInteractions(adapter); }
@Test public void testAfterChangeOrder_orderIsChangeIsNotified2() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(0), new Cat(1), new Cat(2)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(0), new Cat(2)), false); verify(adapter).notifyItemMoved(1, 0); verifyNoMoreInteractions(adapter); }
@Test public void testAfterChangeOrder_orderIsChangeIsNotified3() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(0), new Cat(1), new Cat(2)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(2), new Cat(0)), false); final InOrder inOrder = inOrder(adapter); inOrder.verify(adapter).notifyItemMoved(1, 0); inOrder.verify(adapter).notifyItemMoved(2, 1); verifyNoMoreInteractions(adapter); }
@Test public void testAfterChangeOrderAndModifyItem_orderAndChangeAreNotified() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(0), new Cat(1)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(0).withName("zero_")), false); final InOrder inOrder = inOrder(adapter); inOrder.verify(adapter).notifyItemMoved(1, 0); inOrder.verify(adapter).notifyItemRangeChanged(1, 1); verifyNoMoreInteractions(adapter); }
@Test public void testAfterChangeOrderAndDeleteSomeItem_orderAndDeleteAreNotified1() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(0), new Cat(1), new Cat(2)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(0)), false); final InOrder inOrder = inOrder(adapter); inOrder.verify(adapter).notifyItemMoved(1, 0); inOrder.verify(adapter).notifyItemRangeRemoved(2, 1); verifyNoMoreInteractions(adapter); }
@Test public void testAfterChangeOrderAndDeleteSomeItem_orderAndDeleteAreNotified2() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(0), new Cat(1), new Cat(2)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(2)), false); verify(adapter).notifyItemRangeRemoved(0, 1); verifyNoMoreInteractions(adapter); }
@Test public void testAfterChangeOrderAndDeleteSomeItem_orderAndDeleteAreNotified3() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(0), new Cat(1), new Cat(2)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(2), new Cat(0)), false); final InOrder inOrder = inOrder(adapter); inOrder.verify(adapter).notifyItemMoved(2, 0); inOrder.verify(adapter).notifyItemRangeRemoved(2, 1); verifyNoMoreInteractions(adapter); }
@Test public void testAfterChangeOrderAndDeleteSomeItem_orderAndDeleteAreNotified4() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(0), new Cat(1), new Cat(2)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(2), new Cat(1), new Cat(0)), false); final InOrder inOrder = inOrder(adapter); inOrder.verify(adapter).notifyItemMoved(2, 0); inOrder.verify(adapter).notifyItemMoved(2, 1); verifyNoMoreInteractions(adapter); }
@Test public void testAfterInsertAtFirstPlace_firstPlaceIsMarkedAsInserted() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(1)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(0), new Cat(1)), true); verify(adapter).notifyItemRangeInserted(0, 1); }
@Test public void testForce() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(1)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(1)), true); verify(adapter).notifyItemRangeChanged(0, 1); }
@Test public void testForce2() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(1)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(2)), true); final InOrder inOrder = inOrder(adapter); inOrder.verify(adapter).notifyItemRangeChanged(0, 1); inOrder.verify(adapter).notifyItemRangeInserted(1, 1); }
@Test public void testAddItemAtTheEnd() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(1)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(2)), false); verify(adapter).notifyItemRangeInserted(1, 1); }
@Test public void testAddTwoItemsAtTheEnd() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(1)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(2), new Cat(3)), false); verify(adapter).notifyItemRangeInserted(1, 2); }
@Test public void testAddItemAtTheBegining() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(2)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(2)), false); verify(adapter).notifyItemRangeInserted(0, 1); }
@Test public void testAddTwoItemsAtTheBegining() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(3)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(2), new Cat(3)), false); verify(adapter).notifyItemRangeInserted(0, 2); }
@Test public void testAddItemInTheMiddle() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(3)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(2), new Cat(3)), false); verify(adapter).notifyItemRangeInserted(1, 1); }
@Test public void testAddTwoItemsInTheMiddle() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(4)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(2), new Cat(3), new Cat(4)), false); verify(adapter).notifyItemRangeInserted(1, 2); }
@Test public void testItemChanged() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(3)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(1).withName("one1"), new Cat(3)), false); verify(adapter).notifyItemRangeChanged(0, 1); }
@Test public void testTwoItemsChangedAtStart() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(2), new Cat(3)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(1).withName("one1"), new Cat(2).withName("two1"), new Cat(3)), false); verify(adapter).notifyItemRangeChanged(0, 2); }
@Test public void testTwoItemsChangedInMiddle() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(2), new Cat(3), new Cat(4)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(2).withName("two1"), new Cat(3).withName("tree1"), new Cat(4)), false); verify(adapter).notifyItemRangeChanged(1, 2); }
@Test public void testTwoItemsChangedAtTheEnd() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(2), new Cat(3)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(2).withName("two1"), new Cat(3).withName("tree1")), false); verify(adapter).notifyItemRangeChanged(1, 2); }
@Test public void testItemDeleted1() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(2), new Cat(3)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(2), new Cat(3)), false); verify(adapter).notifyItemRangeRemoved(0, 1); }
@Test public void testItemDeleted2() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(2), new Cat(3)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(3)), false); verify(adapter).notifyItemRangeRemoved(1, 1); }
@Test public void testItemDeleted4() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(2), new Cat(3), new Cat(4)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(3), new Cat(4)), false); verify(adapter).notifyItemRangeRemoved(0, 2); }
@Test public void testItemDeleted5() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(2), new Cat(3), new Cat(4)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(4)), false); verify(adapter).notifyItemRangeRemoved(1, 2); }
@Test public void testItemDeleted6() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(2), new Cat(3), new Cat(4)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(2)), false); verify(adapter).notifyItemRangeRemoved(2, 2); }
@Test public void testItemDeleted3() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(2), new Cat(3)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(2)), false); verify(adapter).notifyItemRangeRemoved(2, 1); }
@Test public void testItemSwapped() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(3)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(2), new Cat(3)), false); final InOrder inOrder = inOrder(adapter); inOrder.verify(adapter).notifyItemRangeInserted(0, 1); inOrder.verify(adapter).notifyItemRangeRemoved(1, 1); }
@Test public void testItemSwappedTwo() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(2), new Cat(5)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(3), new Cat(4), new Cat(5)), false); final InOrder inOrder = inOrder(adapter); inOrder.verify(adapter).notifyItemRangeInserted(0, 2); inOrder.verify(adapter).notifyItemRangeRemoved(2, 2); }
@Test public void testItemRemovedAndAdded() throws Exception { detector.newData(adapter, ImmutableList.of(new Cat(1), new Cat(4)), false); reset(adapter); detector.newData(adapter, ImmutableList.of(new Cat(2), new Cat(3), new Cat(4)), false); final InOrder inOrder = inOrder(adapter); inOrder.verify(adapter).notifyItemRangeInserted(0, 2); inOrder.verify(adapter).notifyItemRangeRemoved(2, 1); }