From 2b7ce81da900a48dca12ddc4be7f554303481d1e Mon Sep 17 00:00:00 2001 From: Tarekk Mohamed Abdalla Date: Tue, 4 May 2021 22:23:44 +0200 Subject: [PATCH] test: tag sorting + test for new tag + test for existing tag --- .../anki/dialogs/tags/TagsDialogTest.java | 67 +++++++++++++++++-- 1 file changed, 62 insertions(+), 5 deletions(-) diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/dialogs/tags/TagsDialogTest.java b/AnkiDroid/src/test/java/com/ichi2/anki/dialogs/tags/TagsDialogTest.java index 0814547ffa0e..7e24b8982964 100644 --- a/AnkiDroid/src/test/java/com/ichi2/anki/dialogs/tags/TagsDialogTest.java +++ b/AnkiDroid/src/test/java/com/ichi2/anki/dialogs/tags/TagsDialogTest.java @@ -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; @@ -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 allTags = Arrays.asList("a", "b", "d", "e"); final List checkedTags = Arrays.asList("a", "b"); @@ -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); @@ -190,10 +193,64 @@ public void test_AddNewTag_shouldBeVisibleInRecyclerView() { recycler.measure(0, 0); recycler.layout(0, 0, 100, 1000); - TagsArrayAdapter.ViewHolder itemView = RecyclerViewUtils.viewHolderAt(recycler, 2); + 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 allTags = Arrays.asList("a", "b", "d", "e"); + final List 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 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()); }); }