|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 lukstbit <52494258+lukstbit@users.noreply.github.com> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify it under |
| 5 | + * the terms of the GNU General Public License as published by the Free Software |
| 6 | + * Foundation; either version 3 of the License, or (at your option) any later |
| 7 | + * version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 | + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 11 | + * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU General Public License along with |
| 14 | + * this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.ichi2.anki.browser |
| 18 | + |
| 19 | +import android.content.Context |
| 20 | +import android.widget.Spinner |
| 21 | +import android.widget.SpinnerAdapter |
| 22 | +import androidx.core.os.bundleOf |
| 23 | +import androidx.fragment.app.testing.FragmentScenario |
| 24 | +import androidx.test.espresso.Espresso.onView |
| 25 | +import androidx.test.espresso.assertion.ViewAssertions.matches |
| 26 | +import androidx.test.espresso.matcher.RootMatchers.isDialog |
| 27 | +import androidx.test.espresso.matcher.ViewMatchers.isChecked |
| 28 | +import androidx.test.espresso.matcher.ViewMatchers.isEnabled |
| 29 | +import androidx.test.espresso.matcher.ViewMatchers.isNotChecked |
| 30 | +import androidx.test.espresso.matcher.ViewMatchers.isNotEnabled |
| 31 | +import androidx.test.espresso.matcher.ViewMatchers.withId |
| 32 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 33 | +import com.ichi2.anki.CollectionManager.TR |
| 34 | +import com.ichi2.anki.R |
| 35 | +import com.ichi2.anki.RobolectricTest |
| 36 | +import com.ichi2.anki.libanki.Note |
| 37 | +import com.ichi2.anki.libanki.NoteId |
| 38 | +import com.ichi2.anki.ui.internationalization.toSentenceCase |
| 39 | +import kotlinx.coroutines.test.advanceUntilIdle |
| 40 | +import org.hamcrest.CoreMatchers.equalTo |
| 41 | +import org.hamcrest.MatcherAssert.assertThat |
| 42 | +import org.junit.Test |
| 43 | +import org.junit.runner.RunWith |
| 44 | + |
| 45 | +@RunWith(AndroidJUnit4::class) |
| 46 | +class FindAndReplaceDialogFragmentTest : RobolectricTest() { |
| 47 | + @Test |
| 48 | + fun `with no selected notes 'only selected notes' check box is not actionable`() = |
| 49 | + runTest { |
| 50 | + onFindReplaceFragment(emptyList()) { |
| 51 | + // nothing selected so checkbox 'Only selected notes' should be unchecked and not enabled |
| 52 | + onView(withId(R.id.only_selected_notes_check_box)) |
| 53 | + .inRoot(isDialog()) |
| 54 | + .check(matches(isNotEnabled())) |
| 55 | + onView(withId(R.id.only_selected_notes_check_box)) |
| 56 | + .inRoot(isDialog()) |
| 57 | + .check(matches(isNotChecked())) |
| 58 | + onView(withId(R.id.ignore_case_check_box)) |
| 59 | + .inRoot(isDialog()) |
| 60 | + .check(matches(isChecked())) |
| 61 | + // using input as regex is not checked at start |
| 62 | + onView(withId(R.id.input_as_regex_check_box)) |
| 63 | + .inRoot(isDialog()) |
| 64 | + .check(matches(isNotChecked())) |
| 65 | + // as nothing is selected the target selector has only the two default options |
| 66 | + assertThat(adapter.items, equalTo(targetContext.getDefaultTargets())) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun `shows expected find-replace targets for selected notes`() = |
| 72 | + runTest { |
| 73 | + val n1 = createFindReplaceTestNote("A", "Car", "Lion") |
| 74 | + val n2 = createFindReplaceTestNote("B", "Train", "Chicken") |
| 75 | + createFindReplaceTestNote("C", "Plane", "Vulture") |
| 76 | + onFindReplaceFragment(listOf(n1.id, n2.id)) { |
| 77 | + // 2 default field options + 4 fields(2 from each of the two notes selected above) |
| 78 | + // see createFindReplaceTestNote for test field names |
| 79 | + val allTargets = |
| 80 | + targetContext.getDefaultTargets() + |
| 81 | + listOf("Afield0", "Afield1", "Bfield0", "Bfield1") |
| 82 | + assertThat(adapter.items, equalTo(allTargets)) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // see #19929 |
| 87 | + @Test |
| 88 | + fun `'selected notes only' status is correct after fragment restore`() = |
| 89 | + runTest { |
| 90 | + val note = createFindReplaceTestNote("A", "kart", "kilogram") |
| 91 | + val file = IdsFile(targetContext.cacheDir, listOf(note.id)) |
| 92 | + val scenario = |
| 93 | + FragmentScenario.launch( |
| 94 | + fragmentClass = FindAndReplaceDialogFragment::class.java, |
| 95 | + fragmentArgs = bundleOf(FindAndReplaceDialogFragment.ARG_IDS to file), |
| 96 | + themeResId = R.style.Theme_Light, |
| 97 | + ) |
| 98 | + scenario.onFragment { fragment -> |
| 99 | + advanceUntilIdle() |
| 100 | + onView(withId(R.id.only_selected_notes_check_box)) |
| 101 | + .inRoot(isDialog()) |
| 102 | + .check(matches(isEnabled())) |
| 103 | + onView(withId(R.id.only_selected_notes_check_box)) |
| 104 | + .inRoot(isDialog()) |
| 105 | + .check(matches(isChecked())) |
| 106 | + // 2 default field options + 2 fields from the only note selected |
| 107 | + val allTargets = |
| 108 | + targetContext.getDefaultTargets() + listOf("Afield0", "Afield1") |
| 109 | + assertThat(fragment.adapter.items, equalTo(allTargets)) |
| 110 | + } |
| 111 | + scenario.recreate() |
| 112 | + scenario.onFragment { fragment -> |
| 113 | + advanceUntilIdle() |
| 114 | + onView(withId(R.id.only_selected_notes_check_box)) |
| 115 | + .inRoot(isDialog()) |
| 116 | + .check(matches(isEnabled())) |
| 117 | + onView(withId(R.id.only_selected_notes_check_box)) |
| 118 | + .inRoot(isDialog()) |
| 119 | + .check(matches(isChecked())) |
| 120 | + // check that the target list from before the recreate call wasn't reset |
| 121 | + val allTargets = targetContext.getDefaultTargets() + listOf("Afield0", "Afield1") |
| 122 | + assertThat(fragment.adapter.items, equalTo(allTargets)) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private fun onFindReplaceFragment( |
| 127 | + noteIds: List<NoteId>, |
| 128 | + action: FindAndReplaceDialogFragment.() -> Unit, |
| 129 | + ) { |
| 130 | + val file = IdsFile(targetContext.cacheDir, noteIds) |
| 131 | + FragmentScenario |
| 132 | + .launch( |
| 133 | + fragmentClass = FindAndReplaceDialogFragment::class.java, |
| 134 | + fragmentArgs = bundleOf(FindAndReplaceDialogFragment.ARG_IDS to file), |
| 135 | + themeResId = R.style.Theme_Light, |
| 136 | + ).onFragment { fragment -> fragment.action() } |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * 3 notetypes available(named A, B and C) each with two fields. |
| 141 | + * Fields names follow the pattern: "${NotetypeName}field${0/1}" (ex: "Afield1"). |
| 142 | + * "C" notetype has the same name for field 1 as notetype B! |
| 143 | + * second is a [Pair] representing the field data(the note has only two fields) |
| 144 | + */ |
| 145 | + private fun createFindReplaceTestNote( |
| 146 | + notetypeName: String, |
| 147 | + field0: String, |
| 148 | + field1: String, |
| 149 | + ): Note { |
| 150 | + addStandardNoteType("A", arrayOf("Afield0", "Afield1"), "", "") |
| 151 | + addStandardNoteType("B", arrayOf("Bfield0", "Bfield1"), "", "") |
| 152 | + addStandardNoteType("C", arrayOf("Cfield0", "Bfield1"), "", "") |
| 153 | + return addNoteUsingNoteTypeName(notetypeName, field0, field1) |
| 154 | + } |
| 155 | + |
| 156 | + val FindAndReplaceDialogFragment.adapter: SpinnerAdapter |
| 157 | + get() = dialog!!.findViewById<Spinner>(R.id.fields_selector).adapter as SpinnerAdapter |
| 158 | + |
| 159 | + private val SpinnerAdapter.items: List<String> |
| 160 | + get() = |
| 161 | + mutableListOf<String>().apply { |
| 162 | + for (position in 0 until count) { |
| 163 | + add(getItem(position) as String) |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + private fun Context.getDefaultTargets() = |
| 168 | + listOf( |
| 169 | + TR.browsingAllFields().toSentenceCase(this, R.string.sentence_all_fields), |
| 170 | + TR.editingTags(), |
| 171 | + ) |
| 172 | +} |
0 commit comments