RepositoryPresenters { @SuppressWarnings({"unchecked", "UnusedParameters"}) @NonNull public static <T> RPLayout<T, RPMain<T>> repositoryPresenterOf(@Nullable final Class<T> type) { return new RepositoryPresenterCompiler(); } private  RepositoryPresenters(); @SuppressWarnings({"unchecked", "UnusedParameters"}) @NonNull static RPLayout<T, RPMain<T>> repositoryPresenterOf(@Nullable final Class<T> type);  }
@Test public void shouldBindRepositoryPresenterOfResult() { final RepositoryPresenter<Result<String>> resultRepositoryPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .bindWith(binder) .forResult(); resultRepositoryPresenter.bind(STRING_RESULT, 0, viewHolder); verify(binder).bind(STRING, view); }
@Test public void shouldBindRepositoryPresenterOfResultWithoutBinder() { final RepositoryPresenter<Result<String>> resultRepositoryPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .forResult(); resultRepositoryPresenter.bind(STRING_RESULT, 0, viewHolder); }
@Test public void shouldBindRepositoryPresenterOfResultList() { final RepositoryPresenter<Result<List<String>>> resultListRepositoryPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .bindWith(binder) .forResultList(); resultListRepositoryPresenter.bind(STRING_LIST_RESULT, 1, viewHolder); verify(binder).bind(SECOND_STRING, view); }
@Test public void shouldBindRepositoryPresenterOfItem() { final RepositoryPresenter<String> itemRepositoryPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .bindWith(binder) .forItem(); itemRepositoryPresenter.bind(STRING, 0, viewHolder); verify(binder).bind(STRING, view); }
@Test public void shouldBindRepositoryPresenterOfList() { final RepositoryPresenter<List<String>> listRepositoryPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .bindWith(binder) .forList(); listRepositoryPresenter.bind(STRING_LIST, 1, viewHolder); verify(binder).bind(SECOND_STRING, view); }
@Test public void shouldBindRepositoryPresenterOfCollection() { final RepositoryPresenter<String> repositoryPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .bindWith(binder) .forCollection(new Function<String, List<String>>() { @NonNull @Override public List<String> apply(@NonNull final String input) { return singletonList(valueOf(input.charAt(0))); } }); repositoryPresenter.bind(STRING, 0, viewHolder); verify(binder).bind(FIRST_STRING_CHARACTER, view); }
@Test public void shouldBindRepositoryPresenterCollectionOfCollection() { final RepositoryPresenter<String> repositoryPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .bindWith(binder) .bindCollectionWith(collectionBinder) .forCollection(new Function<String, List<String>>() { @NonNull @Override public List<String> apply(@NonNull final String input) { return singletonList(valueOf(input.charAt(0))); } }); repositoryPresenter.bind(STRING, 0, viewHolder); verify(binder).bind(FIRST_STRING_CHARACTER, view); verify(collectionBinder).bind(STRING, view); }
@Test public void shouldRecycleViewInRepositoryPresenter() { final RepositoryPresenter<List<String>> listRepositoryPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .bindWith(binder) .recycleWith(recycler) .forList(); listRepositoryPresenter.bind(STRING_LIST, 1, viewHolder); listRepositoryPresenter.recycle(viewHolder); verify(recycler).accept(view); }
@Test public void shouldHandleRecycleWithoutRecycler() { final RepositoryPresenter<List<String>> listRepositoryPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .bindWith(binder) .forList(); listRepositoryPresenter.bind(STRING_LIST, 1, viewHolder); listRepositoryPresenter.recycle(viewHolder); }
@Test public void shouldReturnZeroForCountOfRepositoryPresenterOfFailedResult() { final RepositoryPresenter<Result<String>> resultRepositoryPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .bindWith(binder) .forResult(); assertThat(resultRepositoryPresenter.getItemCount(FAILURE), is(0)); }
@Test public void shouldReturnOneForCountOfRepositoryPresenterOfSuccessfulResult() { final RepositoryPresenter<Result<String>> resultRepositoryPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .bindWith(binder) .forResult(); assertThat(resultRepositoryPresenter.getItemCount(STRING_RESULT), is(1)); }
@Test public void shouldReturnListSizeForCountOfRepositoryPresenterOfList() { final RepositoryPresenter<List<String>> listRepositoryPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .bindWith(binder) .forList(); assertThat(listRepositoryPresenter.getItemCount(STRING_LIST), is(STRING_LIST.size())); }
@Test public void shouldReturnZeroForCountOfRepositoryPresenterOfFailedResultList() { final RepositoryPresenter<Result<List<String>>> resultListRepositoryPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .bindWith(binder) .forResultList(); assertThat(resultListRepositoryPresenter.getItemCount(LIST_FAILURE), is(0)); }
@Test public void shouldReturnListSizeForCountOfRepositoryPresenterOfSuccessfulResultList() { final RepositoryPresenter<Result<List<String>>> resultListRepositoryPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .bindWith(binder) .forResultList(); assertThat(resultListRepositoryPresenter.getItemCount(STRING_LIST_RESULT), is(STRING_LIST.size())); }
@Test public void shouldGenerateLayoutForItemOfRepositoryPresenterOfResultList() { final RepositoryPresenter<Result<List<String>>> resultListRepositoryPresenter = repositoryPresenterOf(String.class) .layoutForItem(layoutForItem) .forResultList(); assertThat(resultListRepositoryPresenter.getLayoutResId(STRING_LIST_RESULT, 1), is(DYNAMIC_LAYOUT_ID)); }
@Test public void shouldReturnStableIdForRepositoryPresenterOfResult() { final RepositoryPresenter<Result<String>> resultRepositoryPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .stableIdForItem(Functions.<String, Long>staticFunction(STABLE_ID)) .forResult(); assertThat(resultRepositoryPresenter.getItemId(STRING_RESULT, 0), is(STABLE_ID)); }
@Test public void shouldReturnStableIdForRepositoryPresenterOfItem() { final RepositoryPresenter<String> resultRepositoryPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .stableIdForItem(Functions.<String, Long>staticFunction(STABLE_ID)) .forItem(); assertThat(resultRepositoryPresenter.getItemId(STRING, 0), is(STABLE_ID)); }
@Test public void shouldReturnStaticStableIdForRepositoryPresenterOfResult() { final RepositoryPresenter<Result<String>> resultRepositoryPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .stableId(STABLE_ID) .forResult(); assertThat(resultRepositoryPresenter.getItemId(STRING_RESULT, 0), is(STABLE_ID)); }
@Test public void shouldReturnStaticStableIdForRepositoryPresenterOfItem() { final RepositoryPresenter<String> resultRepositoryPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .stableId(STABLE_ID) .forItem(); assertThat(resultRepositoryPresenter.getItemId(STRING, 0), is(STABLE_ID)); }
@Test public void shouldReturnStableIdForRepositoryPresenterOfResultList() { final RepositoryPresenter<Result<List<String>>> resultListRepositoryPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .stableIdForItem(Functions.<String, Long>staticFunction(STABLE_ID)) .forResultList(); assertThat(resultListRepositoryPresenter.getItemId(STRING_LIST_RESULT, 1), is(STABLE_ID)); }
@Test public void shouldReturnStableIdForRepositoryPresenterOfList() { final RepositoryPresenter<List<String>> listRepositoryPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .stableIdForItem(Functions.<String, Long>staticFunction(STABLE_ID)) .forList(); assertThat(listRepositoryPresenter.getItemId(STRING_LIST, 1), is(STABLE_ID)); }
@Test public void shouldReturnStableIdForRepositoryPresenterOfListWithBinder() { final RepositoryPresenter<List<String>> resultRepositoryPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .stableIdForItem(Functions.<String, Long>staticFunction(STABLE_ID)) .bindWith(binder) .forList(); assertThat(resultRepositoryPresenter.getItemId(STRING_LIST, 1), is(STABLE_ID)); }
@Test public void shouldAllowStableIdMethodForAnySuperType() { repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .stableIdForItem(Functions.<CharSequence, Long>staticFunction(STABLE_ID)) .forResult(); }
@Test public void shouldHandleRebindWithNewData() { final RepositoryPresenter<String> resultRepositoryPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .bindWith(binder) .forItem(); resultRepositoryPresenter.bind(STRING, 0, viewHolder); verify(binder).bind(STRING, view); reset(binder); resultRepositoryPresenter.bind(SECOND_STRING, 0, viewHolder); verify(binder).bind(SECOND_STRING, view); }
@Test public void shouldHandleRebindWithSameData() { final RepositoryPresenter<String> resultRepositoryPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .bindWith(binder) .forItem(); resultRepositoryPresenter.bind(STRING, 0, viewHolder); verify(binder).bind(STRING, view); reset(binder); resultRepositoryPresenter.bind(STRING, 0, viewHolder); verify(binder).bind(STRING, view); }
@Test public void shouldRefuseFineGrainedEventsWithoutDiffWith() { final RepositoryPresenter<String> presenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .forItem(); final boolean fineGrained = presenter.getUpdates("String1", "String2", listUpdateCallback); assertThat(fineGrained, is(false)); }
@Test public void shouldNotifyFineGrainedEventsWithDiffWith() { final List<String> oldData = asList("A:1", "B:2", "C:3"); final List<String> newData = asList("B:2", "A:4", "C:5"); final DiffingLogic diffingLogic = new DiffingLogic(oldData, newData); final RepositoryPresenter<List<String>> diffingPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .diffWith(diffingLogic, false) .forList(); final boolean fineGrained = diffingPresenter.getUpdates(oldData, newData, listUpdateCallback); assertThat(fineGrained, is(true)); DiffUtil.calculateDiff(diffingLogic, false).dispatchUpdatesTo( verifyingWrapper(listUpdateCallback)); verifyNoMoreInteractions(listUpdateCallback); }
@Test public void shouldNotifyFineGrainedEventsWithDiffWithMoveDetection() { final List<String> oldData = asList("A:1", "B:2", "C:3", "D:0"); final List<String> newData = asList("B:2", "D:0", "A:4", "C:5"); final DiffingLogic diffingLogic = new DiffingLogic(oldData, newData); final RepositoryPresenter<List<String>> diffingPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .bindCollectionWith(Binders.<List<String>, View>nullBinder()) .diffWith(diffingLogic, true) .forCollection(Functions.<List<String>>identityFunction()); final boolean fineGrained = diffingPresenter.getUpdates(oldData, newData, listUpdateCallback); assertThat(fineGrained, is(true)); DiffUtil.calculateDiff(diffingLogic, true).dispatchUpdatesTo( verifyingWrapper(listUpdateCallback)); verifyNoMoreInteractions(listUpdateCallback); }
@Test public void shouldNotifySingleItemFineGrainedEventsWithDiff() { final Result<String> withA = success("A"); final Result<String> withB = success("B"); final Result<String> without = failure(); final RepositoryPresenter<Result<String>> diffingPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .diff() .forResult(); boolean fineGrained = diffingPresenter.getUpdates(withA, withB, listUpdateCallback); assertThat(fineGrained, is(true)); verify(listUpdateCallback).onChanged(0, 1, null); verifyNoMoreInteractions(listUpdateCallback); fineGrained = diffingPresenter.getUpdates(withA, without, listUpdateCallback); assertThat(fineGrained, is(true)); verify(listUpdateCallback).onRemoved(0, 1); verifyNoMoreInteractions(listUpdateCallback); fineGrained = diffingPresenter.getUpdates(without, withB, listUpdateCallback); assertThat(fineGrained, is(true)); verify(listUpdateCallback).onInserted(0, 1); verifyNoMoreInteractions(listUpdateCallback); }
@Test public void shouldNotifyBlanketChangeEventForSameObjectForOldAndNewData() { final List<String> oneList = asList("A:0", "B:1"); final DiffingLogic diffingLogic = new DiffingLogic(oneList, oneList); final RepositoryPresenter<List<String>> diffingPresenter = repositoryPresenterOf(String.class) .layout(LAYOUT_ID) .diffWith(diffingLogic, false) .forList(); final boolean fineGrained = diffingPresenter.getUpdates(oneList, oneList, listUpdateCallback); assertThat(fineGrained, is(true)); verify(listUpdateCallback).onChanged(0, oneList.size(), null); verifyNoMoreInteractions(listUpdateCallback); }