MultiChoiceAdapter extends RecyclerView.Adapter<VH> implements MultiChoiceToolbar.Listener { @Override public void onBindViewHolder(final VH holder, final int position) { View mCurrentView = holder.itemView; if ((mIsInMultiChoiceMode || mIsInSingleClickMode) && isSelectableInMultiChoiceMode(position)) { mCurrentView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { processSingleClick(holder.getAdapterPosition()); } }); } else if (defaultItemViewClickListener(holder, position) != null) { mCurrentView.setOnClickListener(defaultItemViewClickListener(holder, position)); } mCurrentView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { processLongClick(holder.getAdapterPosition()); return true; } }); processUpdate(mCurrentView, holder.getAdapterPosition()); }  void setActive(@NonNull View view, boolean state); void deselectAll(); void selectAll(); boolean select(int position); boolean deselect(int position); void setSingleClickMode(boolean set); int getSelectedItemCount(); List<Integer> getSelectedItemList(); void setMultiChoiceSelectionListener(Listener listener); void setMultiChoiceToolbar(MultiChoiceToolbar multiChoiceToolbar); boolean isInSingleClickMode(); void onSaveInstanceState(@Nullable Bundle savedInstanceState); void onRestoreInstanceState(Bundle savedInstanceState); @Override void onClearButtonPressed(); @Override void onAttachedToRecyclerView(RecyclerView recyclerView); @Override void onBindViewHolder(final VH holder, final int position);  }
@Test public void testGivenIsNotSelectableInMultiChoiceModeAndIsInMultiChoiceModeWhenOnBindThenNoClickListenerSet() throws Exception { mMultiChoiceAdapter = new TestAdapter() { @Override protected boolean isSelectableInMultiChoiceMode(int position) { return false; } @Override protected View.OnClickListener defaultItemViewClickListener(TestViewHolder holder, int position) { return null; } }; mMultiChoiceAdapter.mIsInMultiChoiceMode = false; mMultiChoiceAdapter.mIsInSingleClickMode = true; mMultiChoiceAdapter.onBindViewHolder(mViewHolder, mTestPosition); verify(mViewHolder.itemView, times(0)).setOnClickListener(any(View.OnClickListener.class)); }
@Test public void testDisableItemShouldNotBeSelectedInMultiChoiceModeAndShouldHaveDefaultClickListenerIfNotNull() throws Exception { ArgumentCaptor<View.OnClickListener> clickListenerCapture = ArgumentCaptor.forClass(View.OnClickListener.class); mMultiChoiceAdapter = new TestAdapter() { @Override protected boolean isSelectableInMultiChoiceMode(int position) { return false; } }; mMultiChoiceAdapter.mIsInMultiChoiceMode = false; mMultiChoiceAdapter.mIsInSingleClickMode = true; mMultiChoiceAdapter.onBindViewHolder(mViewHolder, mTestPosition); verify(mViewHolder.itemView, times(1)).setOnClickListener(clickListenerCapture.capture()); assertEquals("The setOnClickListener doesn't have the correct onClickListener object", ((TestAdapter) mMultiChoiceAdapter) .getOnClickListener(), clickListenerCapture.getValue()); }
@Test public void testAdapterIsUsingTheDefaultClickListenerIfNotInMultiAndSingleClickModeAndTheItemIsSelectable() throws Exception { ArgumentCaptor<View.OnClickListener> clickListenerCapture = ArgumentCaptor.forClass(View.OnClickListener.class); mMultiChoiceAdapter.mIsInMultiChoiceMode = false; mMultiChoiceAdapter.mIsInSingleClickMode = false; when(mMockItemList.containsKey(RecyclerView.NO_POSITION)).thenReturn(true); when(mMockItemList.get(RecyclerView.NO_POSITION)).thenReturn(MultiChoiceAdapter.State.ACTIVE); mMultiChoiceAdapter.onBindViewHolder(mViewHolder, mTestPosition); verify(mViewHolder.itemView, times(1)).setOnClickListener(clickListenerCapture.capture()); assertEquals("The setOnClickListener doesn't have the correct onClickListener object", ((TestAdapter) mMultiChoiceAdapter) .getOnClickListener(), clickListenerCapture.getValue()); }
@Test public void testAdapterIsUsingNotTheDefaultClickListenerIfNotInMultiAndSingleClickModeAndTheItemIsSelectableAndTheDefaultClickIsNull() throws Exception { mMultiChoiceAdapter = new TestAdapter() { @Override protected View.OnClickListener defaultItemViewClickListener(TestViewHolder holder, int position) { return null; } }; mMultiChoiceAdapter.mIsInMultiChoiceMode = false; mMultiChoiceAdapter.mIsInSingleClickMode = false; mMultiChoiceAdapter.onBindViewHolder(mViewHolder, 1); verify(mViewHolder.itemView, times(0)).setOnClickListener(any(View.OnClickListener.class)); }
@Test public void testAdapterShouldCallOnUpdateItemListener() throws Exception { when(mMockItemList.containsKey(RecyclerView.NO_POSITION)).thenReturn(true); when(mMockItemList.get(RecyclerView.NO_POSITION)).thenReturn(MultiChoiceAdapter.State.ACTIVE); mMultiChoiceAdapter.onBindViewHolder(mViewHolder, mTestPosition); verify(mMockItemList, times(1)).get(RecyclerView.NO_POSITION); verify(mMockItemList, times(1)).containsKey(RecyclerView.NO_POSITION); verify(mMockItemView, times(1)).setAlpha(SELECTED_ALPHA); }
@Test public void testGivenIsNotInMultiChoiceModeAndItemIsInactiveWhenOnBindViewHolderAndPerformClickThenNoActivationOnTheItem() throws Exception { mMultiChoiceAdapter.mIsInMultiChoiceMode = false; mMultiChoiceAdapter.mIsInSingleClickMode = false; when(mMockItemList.containsKey(RecyclerView.NO_POSITION)).thenReturn(true); when(mMockItemList.get(RecyclerView.NO_POSITION)).thenReturn(MultiChoiceAdapter.State.INACTIVE); mMultiChoiceAdapter.onBindViewHolder(mViewHolder, RecyclerView.NO_POSITION); mViewHolder.itemView.callOnClick(); verify(mMockItemList, times(0)).put(RecyclerView.NO_POSITION, MultiChoiceAdapter.State.ACTIVE); verify(mMockItemView, times(0)).setAlpha(SELECTED_ALPHA); }
@Test public void testGivenIsInSingleClickModeAndThemItemIsInactiveWhenOnBindThenClickListenerSetAndItemActive() throws Exception { ArgumentCaptor<View.OnClickListener> clickListenerCapture = ArgumentCaptor.forClass(View.OnClickListener.class); when(mMockItemList.containsKey(RecyclerView.NO_POSITION)).thenReturn(true); when(mMockItemList.get(RecyclerView.NO_POSITION)).thenReturn(MultiChoiceAdapter.State.INACTIVE); mMultiChoiceAdapter.mIsInSingleClickMode = true; mMultiChoiceAdapter.mIsInMultiChoiceMode = false; mMultiChoiceAdapter.onBindViewHolder(mViewHolder, mTestPosition); verify(mMockItemView, times(1)).setOnClickListener(clickListenerCapture.capture()); clickListenerCapture.getValue().onClick(mMockItemView); verify(mMockItemList, times(1)).put(RecyclerView.NO_POSITION, MultiChoiceAdapter.State.ACTIVE); }
@Test public void testGivenIsInMultiChoiceModeAndThemItemIsInactiveWhenOnBindThenClickListenerSetAndItemActive() throws Exception { ArgumentCaptor<View.OnClickListener> clickListenerCapture = ArgumentCaptor.forClass(View.OnClickListener.class); when(mMockItemList.containsKey(RecyclerView.NO_POSITION)).thenReturn(true); when(mMockItemList.get(RecyclerView.NO_POSITION)).thenReturn(MultiChoiceAdapter.State.INACTIVE); mMultiChoiceAdapter.mIsInSingleClickMode = false; mMultiChoiceAdapter.mIsInMultiChoiceMode = true; mMultiChoiceAdapter.onBindViewHolder(mViewHolder, mTestPosition); verify(mMockItemView, times(1)).setOnClickListener(clickListenerCapture.capture()); clickListenerCapture.getValue().onClick(mMockItemView); verify(mMockItemList, times(1)).put(RecyclerView.NO_POSITION, MultiChoiceAdapter.State.ACTIVE); }