diff --git a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/TranslationsTest.kt b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/TranslationsTest.kt index 29ee7c952918..b1ad6bcb12c4 100644 --- a/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/TranslationsTest.kt +++ b/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/TranslationsTest.kt @@ -8,6 +8,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.filters.MediumTest import junit.framework.TestCase.assertFalse import junit.framework.TestCase.assertTrue +import junit.framework.TestCase.fail import org.json.JSONObject import org.junit.After import org.junit.Before @@ -409,7 +410,7 @@ class TranslationsTest : BaseSessionTest() { @Test fun testListModelDownloadStates() { // Note: Test endpoint is using a mocked response - var modelStatesResult = TranslationsController.RuntimeTranslation.listModelDownloadStates() + val modelStatesResult = TranslationsController.RuntimeTranslation.listModelDownloadStates() try { sessionRule.waitForResult(modelStatesResult) assertTrue("Should not be able to list models.", true) @@ -610,7 +611,7 @@ class TranslationsTest : BaseSessionTest() { .build() try { sessionRule.waitForResult(RuntimeTranslation.manageLanguageModel(missingLanguage)) - assertTrue("Should not complete requests on an incompatible state.", false) + fail("Should not complete requests on an incompatible state.") } catch (e: RuntimeException) { // Wait call causes a runtime exception too. val te = e.cause as TranslationsException @@ -629,7 +630,7 @@ class TranslationsTest : BaseSessionTest() { .build() try { sessionRule.waitForResult(RuntimeTranslation.manageLanguageModel(allDownloadAttempt)) - assertTrue("Should not complete downloads in automation.", false) + fail("Should not complete downloads in automation.") } catch (e: RuntimeException) { // Wait call causes a runtime exception too. val te = e.cause as TranslationsException @@ -645,7 +646,7 @@ class TranslationsTest : BaseSessionTest() { .build() try { sessionRule.waitForResult(RuntimeTranslation.manageLanguageModel(allDeleteAttempt)) - assertTrue("Should not complete deletes in automation.", false) + fail("Should not complete deletes in automation.") } catch (e: RuntimeException) { // Wait call causes a runtime exception too. val te = e.cause as TranslationsException @@ -661,7 +662,7 @@ class TranslationsTest : BaseSessionTest() { .build() try { sessionRule.waitForResult(RuntimeTranslation.manageLanguageModel(malformedRequest)) - assertTrue("Should not complete malformed requests in automation.", false) + fail("Should not complete malformed requests in automation.") } catch (e: RuntimeException) { // Wait call causes a runtime exception too. val te = e.cause as TranslationsException @@ -670,6 +671,43 @@ class TranslationsTest : BaseSessionTest() { te.code == TranslationsException.ERROR_UNKNOWN, ) } + + val malformedCacheDownloadingRequest = ModelManagementOptions.Builder() + .operation(DOWNLOAD) + .operationLevel(RuntimeTranslation.CACHE) + .build() + try { + sessionRule.waitForResult(RuntimeTranslation.manageLanguageModel(malformedCacheDownloadingRequest)) + fail("Should not complete an invalid request.") + } catch (e: RuntimeException) { + // Wait call causes a runtime exception too. + val te = e.cause as TranslationsException + assertTrue( + "Correctly could not download the cache.", + te.code == ERROR_MODEL_COULD_NOT_DOWNLOAD, + ) + } + } + } + + @Test + fun testCacheClearing() { + // Test portion for Android Studio, where Remote Settings will be active + if (!sessionRule.env.isAutomation) { + mainSession.loadTestPath(TRANSLATIONS_EN) + mainSession.waitForPageStop() + // Will cause a download + val translate = sessionRule.session.sessionTranslation!!.translate("en", "es", null) + sessionRule.waitForResult(translate) + + // Try to clear the download + val clearDownloadCache = ModelManagementOptions.Builder() + .operation(DELETE) + .operationLevel(RuntimeTranslation.CACHE) + .build() + + sessionRule.waitForResult(RuntimeTranslation.manageLanguageModel(clearDownloadCache)) + assertTrue("Successfully translated and cleared the downloaded file", true) } } diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/TranslationsController.java b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/TranslationsController.java index f2681ce86ff4..2aa4fea035ef 100644 --- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/TranslationsController.java +++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/TranslationsController.java @@ -127,8 +127,6 @@ public class TranslationsController { * Manage the language model or models. Options are to download or delete a BCP 47 language or * all or cache. * - *
Bug 1869404 will add an option for deleting translations model "cache". - * * @param options contain language, operation, and operation level to perform on the model * @return the request proceeded as expected or an exception. */ diff --git a/mobile/shared/modules/geckoview/GeckoViewTranslations.sys.mjs b/mobile/shared/modules/geckoview/GeckoViewTranslations.sys.mjs index 0a1ada5c1bfb..94b3c6aee119 100644 --- a/mobile/shared/modules/geckoview/GeckoViewTranslations.sys.mjs +++ b/mobile/shared/modules/geckoview/GeckoViewTranslations.sys.mjs @@ -219,6 +219,18 @@ export const GeckoViewTranslationsSettings = { } ); } + if (operationLevel === "cache") { + await lazy.TranslationsParent.deleteCachedLanguageFiles().then( + function () { + aCallback.onSuccess(); + }, + function (error) { + aCallback.onError( + `COULD_NOT_DELETE - An issue occurred while deleting the cache: ${error}` + ); + } + ); + } } else if (operation === "download") { if (operationLevel === "all") { lazy.TranslationsParent.downloadAllFiles().then( @@ -251,6 +263,12 @@ export const GeckoViewTranslationsSettings = { } ); } + if (operationLevel === "cache") { + aCallback.onError( + `COULD_NOT_DOWNLOAD - Downloading the cache is not a valid option. Please check the parameters and try again. + Language: ${language}, Operation: ${operation}, Operation Level: ${operationLevel}` + ); + } } else { aCallback.onError( `ERROR_UNKNOWN - The request to manage models appears to be malformed. Please check the parameters and try again.