From dbe54f87804f19faef0cfef640042ebbfe0299c1 Mon Sep 17 00:00:00 2001 From: Tarekk Mohamed Abdalla Date: Mon, 3 May 2021 21:32:44 +0200 Subject: [PATCH] test: regression #8762 --- .../ichi2/anki/dialogs/tags/TagsDialog.java | 4 +- .../anki/dialogs/tags/TagsDialogTest.java | 55 +++++++++++++++++-- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/tags/TagsDialog.java b/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/tags/TagsDialog.java index d4f233f82335..cb0f045a6893 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/tags/TagsDialog.java +++ b/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/tags/TagsDialog.java @@ -25,6 +25,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.annotation.VisibleForTesting; import androidx.appcompat.widget.SearchView; import androidx.appcompat.widget.Toolbar; import androidx.recyclerview.widget.LinearLayoutManager; @@ -269,7 +270,8 @@ private EditText requireDialogInputEditText(@NonNull MaterialDialog dialog) { return editText; } - public void addTag(String tag) { + @VisibleForTesting + protected void addTag(String tag) { if (!TextUtils.isEmpty(tag)) { String feedbackText; if (mTags.add(tag)) { 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 ef99d6586160..facd297cbe2c 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 @@ -17,6 +17,7 @@ import android.os.Bundle; import android.view.View; +import android.widget.CheckedTextView; import android.widget.RadioGroup; import com.afollestad.materialdialogs.DialogAction; @@ -33,10 +34,12 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; +import androidx.appcompat.widget.Toolbar; import androidx.fragment.app.testing.FragmentScenario; import androidx.lifecycle.Lifecycle; import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LifecycleRegistry; +import androidx.recyclerview.widget.RecyclerView; import androidx.test.ext.junit.runners.AndroidJUnit4; import static com.ichi2.anki.dialogs.tags.TagsDialogListener.*; @@ -45,6 +48,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.IsNull.notNullValue; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -87,7 +91,7 @@ public void testTagsDialogCustomStudyOptionInterface() { assertThat(dialog, notNullValue()); final View body = dialog.getCustomView(); - final RadioGroup mOptionsGroup = body.findViewById(R.id.tags_dialog_options_radiogroup); + final RadioGroup mOptionsGroup = body.findViewById(R.id.tags_dialog_options_radiogroup); assertEquals(mOptionsGroup.getVisibility(), View.VISIBLE); @@ -113,7 +117,6 @@ public void testTagsDialogCustomStudyOptionFragmentAPI() { .getArguments(); - FragmentScenario scenario = FragmentScenario.launch(TagsDialog.class, args, R.style.Theme_AppCompat); scenario.moveToState(Lifecycle.State.STARTED); @@ -123,8 +126,6 @@ public void testTagsDialogCustomStudyOptionFragmentAPI() { assertThat(dialog, notNullValue()); - - AtomicReference> returnedList = new AtomicReference<>(); AtomicInteger returnedOption = new AtomicInteger(); @@ -135,9 +136,8 @@ public void testTagsDialogCustomStudyOptionFragmentAPI() { }); - final View body = dialog.getCustomView(); - final RadioGroup mOptionsGroup = body.findViewById(R.id.tags_dialog_options_radiogroup); + final RadioGroup mOptionsGroup = body.findViewById(R.id.tags_dialog_options_radiogroup); assertEquals(mOptionsGroup.getVisibility(), View.VISIBLE); @@ -152,4 +152,47 @@ public void testTagsDialogCustomStudyOptionFragmentAPI() { }); } + + // regression test #8762 + @Test + public void test_AddNewTag_shouldBeVisibleInRecyclerView() { + 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 NEW_TAG = "c"; + + f.addTag(NEW_TAG); + + // workaround robolectric recyclerView issue + // update recycler + recycler.measure(0, 0); + recycler.layout(0, 0, 100, 1000); + + TagsArrayAdapter.ViewHolder vh = (TagsArrayAdapter.ViewHolder) recycler.findViewHolderForAdapterPosition(2); + CheckedTextView itemView = (CheckedTextView) vh.itemView; + + assertEquals(NEW_TAG, itemView.getText()); + assertTrue(itemView.isChecked()); + }); + } + } \ No newline at end of file