Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),

### Added

- Edit existing personal dictionary words in a prefilled dialog, with validation,
duplicate feedback and a single offline dictionary update (#120).
- Warn when the selected keyboard declares that it disables Android's
system spellchecker on Android 12 and newer. The warning refreshes when
changing keyboards and offers a keyboard picker; older Android versions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import fr.grammalecteandroid.engine.GrammalecteDictionaryPreferences
import fr.grammalecteandroid.engine.GrammalectePersonalDictionaryPreferences
import fr.grammalecteandroid.engine.GrammalecteQuickJsEngine
import fr.grammalecteandroid.engine.GrammalecteRulePreferences
import fr.grammalecteandroid.engine.PersonalDictionaryEditResult
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
Expand Down Expand Up @@ -278,6 +279,56 @@ class GrammalecteEngineInstrumentedTest {
}
}

@Test
fun replacingPersonalDictionaryWordPreservesEntriesAndRefreshesOtherEngines() {
val context = ApplicationProvider.getApplicationContext<Context>()
val preferences = GrammalectePersonalDictionaryPreferences(context)
val original = "grammalecteandroidique"
val replacement = "grammalecteandroidissime"
val other = "grammalectepersonnelique"
preferences.replace(listOf(original, other))
val editor = GrammalecteQuickJsEngine(context)
val reader = GrammalecteQuickJsEngine(context)

try {
val initial = listOf(original, other).sorted()
assertEquals(initial, editor.personalDictionaryWords())
assertEquals(initial, reader.personalDictionaryWords())

assertEquals(PersonalDictionaryEditResult.DUPLICATE, editor.replacePersonalDictionaryWord(original, other))
assertEquals(PersonalDictionaryEditResult.UNCHANGED, editor.replacePersonalDictionaryWord(original, original))
assertEquals(PersonalDictionaryEditResult.MISSING, editor.replacePersonalDictionaryWord("absent", replacement))
assertTrue(runCatching { editor.replacePersonalDictionaryWord(original, "deux mots") }.isFailure)
// JavaScript rejects an embedded BOM as whitespace even though Kotlin does not.
assertTrue(runCatching { editor.replacePersonalDictionaryWord(original, "mot\uFEFFinvalide") }.isFailure)
assertEquals(initial, preferences.words())
assertEquals(initial, editor.personalDictionaryWords())
assertTrue(editor.checkWord(original, "fr-FR", 5).valid)

assertEquals(
PersonalDictionaryEditResult.REPLACED,
editor.replacePersonalDictionaryWord(original, replacement),
)
val updated = listOf(replacement, other).sorted()
assertEquals(updated, editor.personalDictionaryWords())
assertEquals(updated, preferences.words())
assertEquals(updated, reader.personalDictionaryWords())
assertTrue(reader.checkWord(replacement, "fr-FR", 5).valid)
assertFalse(reader.checkWord(original, "fr-FR", 5).valid)

val reopened = GrammalecteQuickJsEngine(context)
try {
assertEquals(updated, reopened.personalDictionaryWords())
} finally {
reopened.close()
}
} finally {
reader.close()
editor.close()
preferences.reset()
}
}

@Test
fun resetToDefaultsPersistsAcrossEngineRestart() {
val context =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package fr.zefut.grammalecte

import android.content.Context
import androidx.compose.ui.semantics.LiveRegionMode
import androidx.compose.ui.semantics.SemanticsProperties
import androidx.compose.ui.test.SemanticsMatcher
import androidx.compose.ui.test.assert
import androidx.compose.ui.test.assertTextContains
import androidx.compose.ui.test.hasTestTag
import androidx.compose.ui.test.junit4.v2.createEmptyComposeRule
import androidx.compose.ui.test.onAllNodesWithTag
import androidx.compose.ui.test.onAllNodesWithText
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performScrollToNode
import androidx.compose.ui.test.performTextReplacement
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import fr.grammalecteandroid.engine.GrammalectePersonalDictionaryPreferences
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class PersonalDictionaryActivityInstrumentedTest {
@get:Rule val composeRule = createEmptyComposeRule()

@Test
fun editValidatesCancelsAndPersistsAcrossRecreation() {
val context = ApplicationProvider.getApplicationContext<Context>()
val preferences = GrammalectePersonalDictionaryPreferences(context)
val original = "grammalecteandroidique"
val other = "grammalectepersonnelique"
val replacement = "grammalecteandroidissime"
val initial = listOf(original, other).sorted()
preferences.replace(initial)

try {
ActivityScenario.launch(PersonalDictionaryActivity::class.java).use { scenario ->
waitForWordCount()
openEdit(original)
composeRule.onNodeWithTag("dictionary-edit-input").assertTextContains(original)
composeRule.onNodeWithTag("dictionary-edit-input").performTextReplacement(replacement)
composeRule.onNodeWithText("Annuler").performClick()
composeRule.onNodeWithTag("dictionary-edit-input").assertDoesNotExist()
assertEquals(initial, preferences.words())

openEdit(original)
submit("")
assertError("Saisissez un mot.")
submit("a".repeat(65))
assertError("Le mot ne doit pas dépasser 64 caractères.")
submit("deux mots")
assertError("Saisissez un seul mot à la fois.")
submit(other)
composeRule.waitUntil(30_000) {
composeRule.onAllNodesWithTag("dictionary-edit-error").fetchSemanticsNodes().isNotEmpty()
}
assertError("Ce mot est déjà dans le dictionnaire.")
assertEquals(initial, preferences.words())
composeRule.onNodeWithTag("dictionary-edit-input").assertTextContains(other)

submit(replacement)
composeRule.waitUntil(30_000) {
composeRule.onAllNodesWithTag("dictionary-edit-input").fetchSemanticsNodes().isEmpty()
}
val updated = listOf(replacement, other).sorted()
assertEquals(updated, preferences.words())

scenario.recreate()
waitForWordCount()
openEdit(replacement)
composeRule.onNodeWithTag("dictionary-edit-input").assertTextContains(replacement)
composeRule.onNodeWithText("Annuler").performClick()
assertEquals(updated, preferences.words())
}
} finally {
preferences.reset()
}
}

private fun waitForWordCount() {
composeRule.waitUntil(30_000) {
composeRule.onAllNodesWithText("2 mots enregistrés.").fetchSemanticsNodes().isNotEmpty()
}
}

private fun openEdit(word: String) {
val tag = "dictionary-edit-$word"
composeRule.onNodeWithTag("personal-dictionary").performScrollToNode(hasTestTag(tag))
composeRule.onNodeWithTag(tag).performClick()
}

private fun submit(word: String) {
composeRule.onNodeWithTag("dictionary-edit-input").performTextReplacement(word)
composeRule.onNodeWithText("Enregistrer").performClick()
}

private fun assertError(message: String) {
composeRule
.onNodeWithTag("dictionary-edit-error")
.assertTextContains(message)
.assert(SemanticsMatcher.expectValue(SemanticsProperties.LiveRegion, LiveRegionMode.Polite))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package fr.zefut.grammalecte
import androidx.compose.ui.test.assertIsEnabled
import androidx.compose.ui.test.assertIsNotEnabled
import androidx.compose.ui.test.assertTextContains
import androidx.compose.ui.test.hasTestTag
import androidx.compose.ui.test.junit4.v2.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performScrollToNode
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Assert.assertEquals
import org.junit.Rule
Expand All @@ -32,15 +34,22 @@ class PersonalDictionaryScreenInstrumentedTest {
status = "1 mot",
statusIsError = false,
busy = false,
edit = null,
onInputChange = { entered = it },
onAdd = { added++ },
onRemove = { removed = it },
onEdit = {},
onEditInputChange = {},
onEditConfirm = {},
onEditCancel = {},
onBack = { back++ },
)
}

composeRule.onNodeWithText("Ajouter").assertIsEnabled().performClick()
composeRule.onNodeWithTag("personal-dictionary").performScrollToNode(hasTestTag("dictionary-edit-grammalecte"))
composeRule.onNodeWithText("Supprimer").assertIsEnabled().performClick()
composeRule.onNodeWithTag("personal-dictionary").performScrollToNode(hasTestTag("dictionary-heading"))
composeRule.onNodeWithContentDescription("Retour").performClick()
composeRule.runOnIdle {
assertEquals(1, added)
Expand All @@ -58,9 +67,14 @@ class PersonalDictionaryScreenInstrumentedTest {
status = "Ce mot est déjà dans le dictionnaire.",
statusIsError = true,
busy = false,
edit = null,
onInputChange = {},
onAdd = {},
onRemove = {},
onEdit = {},
onEditInputChange = {},
onEditConfirm = {},
onEditCancel = {},
onBack = {},
)
}
Expand All @@ -79,16 +93,23 @@ class PersonalDictionaryScreenInstrumentedTest {
status = "Chargement…",
statusIsError = false,
busy = true,
edit = null,
onInputChange = {},
onAdd = {},
onRemove = {},
onEdit = {},
onEditInputChange = {},
onEditConfirm = {},
onEditCancel = {},
onBack = {},
)
}

composeRule.onNodeWithTag("dictionary-input").assertIsNotEnabled()
composeRule.onNodeWithText("Ajouter").assertIsNotEnabled()
composeRule.onNodeWithText("Supprimer").assertIsNotEnabled()
composeRule.onNodeWithTag("dictionary-status").assertTextContains("Chargement…")
composeRule.onNodeWithTag("personal-dictionary").performScrollToNode(hasTestTag("dictionary-edit-grammalecte"))
composeRule.onNodeWithText("Modifier").assertIsNotEnabled()
composeRule.onNodeWithText("Supprimer").assertIsNotEnabled()
}
}
Loading