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

fix: sorting new tag added in TagsDialog #8766

Merged
merged 3 commits into from
Jun 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,29 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.recyclerview.widget.RecyclerView;

import static androidx.annotation.VisibleForTesting.*;

public class TagsArrayAdapter extends RecyclerView.Adapter<TagsArrayAdapter.ViewHolder> implements Filterable {
public static class ViewHolder extends RecyclerView.ViewHolder {
private final CheckedTextView mTagItemCheckedTextView;
public ViewHolder(CheckedTextView ctv) {
super(ctv);
mTagItemCheckedTextView = ctv;
}

@VisibleForTesting(otherwise = NONE)
public String getText() {
return ((CheckedTextView) itemView).getText().toString();
}


@VisibleForTesting(otherwise = NONE)
public boolean isChecked() {
return ((CheckedTextView) itemView).isChecked();
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,12 @@ protected void addTag(String tag) {
mNoTagsTextView.setVisibility(View.GONE);
}
mTags.add(tag);
mTagsArrayAdapter.sortData();
feedbackText = getString(R.string.tag_editor_add_feedback, tag, mPositiveText);
} else {
feedbackText = getString(R.string.tag_editor_add_feedback_existing, tag);
}
mTags.check(tag);
mTagsArrayAdapter.sortData();
mTagsArrayAdapter.notifyDataSetChanged();
// Show a snackbar to let the user know the tag was added successfully
UIUtils.showSnackbar(getActivity(), feedbackText, false, -1, null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

import android.os.Bundle;
import android.view.View;
import android.widget.CheckedTextView;
import android.widget.RadioGroup;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.ichi2.anki.R;
import com.ichi2.anki.dialogs.tags.TagsDialog.DialogType;
import com.ichi2.testutils.RecyclerViewUtils;

import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -47,6 +47,8 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
Expand Down Expand Up @@ -157,8 +159,9 @@ public void testTagsDialogCustomStudyOptionFragmentAPI() {


// regression test #8762
// test for #8763
@Test
public void test_AddNewTag_shouldBeVisibleInRecyclerView() {
public void test_AddNewTag_shouldBeVisibleInRecyclerView_andSortedCorrectly() {
final DialogType type = DialogType.ADD_TAG;
final List<String> allTags = Arrays.asList("a", "b", "d", "e");
final List<String> checkedTags = Arrays.asList("a", "b");
Expand All @@ -181,7 +184,7 @@ public void test_AddNewTag_shouldBeVisibleInRecyclerView() {
final View body = dialog.getCustomView();
RecyclerView recycler = body.findViewById(R.id.tags_dialog_tags_list);

final String NEW_TAG = "c";
final String NEW_TAG = "zzzz";

f.addTag(NEW_TAG);

Expand All @@ -190,11 +193,64 @@ public void test_AddNewTag_shouldBeVisibleInRecyclerView() {
recycler.measure(0, 0);
recycler.layout(0, 0, 100, 1000);

TagsArrayAdapter.ViewHolder vh = (TagsArrayAdapter.ViewHolder) recycler.findViewHolderForAdapterPosition(2);
CheckedTextView itemView = (CheckedTextView) vh.itemView;
TagsArrayAdapter.ViewHolder lastItem = RecyclerViewUtils.viewHolderAt(recycler, 4);
TagsArrayAdapter.ViewHolder newTagItemItem = RecyclerViewUtils.viewHolderAt(recycler, 2);

assertEquals(NEW_TAG, itemView.getText());
assertTrue(itemView.isChecked());
assertEquals(5, recycler.getAdapter().getItemCount());

assertEquals(NEW_TAG, newTagItemItem.getText());
assertTrue(newTagItemItem.isChecked());

assertNotEquals(NEW_TAG, lastItem.getText());
assertFalse(lastItem.isChecked());
});
}


// test for #8763
@Test
public void test_AddNewTag_existingTag_shouldBeSelectedAndSorted() {
final DialogType type = DialogType.ADD_TAG;
final List<String> allTags = Arrays.asList("a", "b", "d", "e");
final List<String> checkedTags = Arrays.asList("a", "b");

Bundle args = new TagsDialog(whatever())
.withArguments(type, checkedTags, allTags)
.getArguments();

final TagsDialogListener mockListener = mock(TagsDialogListener.class);

TagsDialogFactory factory = new TagsDialogFactory(mockListener);
FragmentScenario<TagsDialog> scenario = FragmentScenario.launch(TagsDialog.class, args, R.style.Theme_AppCompat, factory);

scenario.moveToState(Lifecycle.State.STARTED);

scenario.onFragment((f) -> {
MaterialDialog dialog = (MaterialDialog) f.getDialog();
assertThat(dialog, notNullValue());

final View body = dialog.getCustomView();
RecyclerView recycler = body.findViewById(R.id.tags_dialog_tags_list);

final String EXISTING_TAG = "e";

f.addTag(EXISTING_TAG);

// workaround robolectric recyclerView issue
// update recycler
recycler.measure(0, 0);
recycler.layout(0, 0, 100, 1000);

TagsArrayAdapter.ViewHolder lastItem = RecyclerViewUtils.viewHolderAt(recycler, 3);
TagsArrayAdapter.ViewHolder newTagItemItem = RecyclerViewUtils.viewHolderAt(recycler, 2);

assertEquals(4, recycler.getAdapter().getItemCount());

assertEquals(EXISTING_TAG, newTagItemItem.getText());
assertTrue(newTagItemItem.isChecked());

assertNotEquals(EXISTING_TAG, lastItem.getText());
assertFalse(lastItem.isChecked());
});
}

Expand Down
27 changes: 27 additions & 0 deletions AnkiDroid/src/test/java/com/ichi2/testutils/RecyclerViewUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright (c) 2021 Tarek Mohamed Abdalla <[email protected]>

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 3 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.ichi2.testutils;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;

public class RecyclerViewUtils {

public static <VH extends ViewHolder> VH viewHolderAt(RecyclerView recyclerView, int position) {
return (VH) recyclerView.findViewHolderForAdapterPosition(position);
}

}