Bug 1898358 - Normalize ModelManagementOptions to Lower Case r=android-reviewers,geckoview-reviewers,calu

There is a bug where `ModelManagementOptions` accepts un-normalized
strings and will have errant behavior.

Differential Revision: https://phabricator.services.mozilla.com/D211295
This commit is contained in:
ohall-m 2024-05-23 17:53:55 +00:00
parent b9642088b1
commit 750da42c98
5 changed files with 61 additions and 8 deletions

View file

@ -17,4 +17,12 @@ enum class ModelOperation(val operation: String) {
* Delete the model(s).
*/
DELETE("delete"),
;
/**
* The operation will use the string literal on the engine.
*/
override fun toString(): String {
return operation
}
}

View file

@ -23,4 +23,12 @@ enum class OperationLevel(val operationLevel: String) {
* Complete the operation all models.
*/
ALL("all"),
;
/**
* The operation level will use the string literal on the engine.
*/
override fun toString(): String {
return operationLevel
}
}

View file

@ -352,10 +352,23 @@ class TranslationsTest : BaseSessionTest() {
fun testManageLanguageModel() {
val options = ModelManagementOptions.Builder()
.languageToManage("en")
.operation(TranslationsController.RuntimeTranslation.DOWNLOAD)
.operation(DOWNLOAD)
.build()
assertTrue("ModelManagementOptions builder options work as expected.", options.language == "en" && options.operation == DOWNLOAD)
assertTrue(
"ModelManagementOptions builder options work as expected.",
options.language == "en" && options.operation == DOWNLOAD,
)
val nonNormalizedOptions = ModelManagementOptions.Builder()
.languageToManage("EN")
.operation("DoWnLoAd")
.build()
assertTrue(
"ModelManagementOptions builder options work as expected on non-normalized options.",
nonNormalizedOptions.language == "en" && nonNormalizedOptions.operation == DOWNLOAD,
)
}
@Test
@ -641,6 +654,22 @@ class TranslationsTest : BaseSessionTest() {
te.code == ERROR_MODEL_COULD_NOT_DELETE,
)
}
val malformedRequest = ModelManagementOptions.Builder()
.operation("not-a-function")
.operationLevel("not-an-operation")
.build()
try {
sessionRule.waitForResult(RuntimeTranslation.manageLanguageModel(malformedRequest))
assertTrue("Should not complete malformed requests in automation.", false)
} catch (e: RuntimeException) {
// Wait call causes a runtime exception too.
val te = e.cause as TranslationsException
assertTrue(
"Correctly could not submit a malformed request.",
te.code == TranslationsException.ERROR_UNKNOWN,
)
}
}
}

View file

@ -18,6 +18,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import org.mozilla.gecko.EventDispatcher;
@ -410,9 +411,13 @@ public class TranslationsController {
*/
protected ModelManagementOptions(
final @NonNull RuntimeTranslation.ModelManagementOptions.Builder builder) {
this.language = builder.mLanguage;
this.operation = builder.mOperation;
this.operationLevel = builder.mOperationLevel;
if (builder.mLanguage != null) {
this.language = builder.mLanguage.toLowerCase(Locale.ROOT);
} else {
this.language = builder.mLanguage;
}
this.operation = builder.mOperation.toLowerCase(Locale.ROOT);
this.operationLevel = builder.mOperationLevel.toLowerCase(Locale.ROOT);
}
/** Serializer for Model Management Options */

View file

@ -218,10 +218,8 @@ export const GeckoViewTranslationsSettings = {
);
}
);
return;
}
}
if (operation === "download") {
} else if (operation === "download") {
if (operationLevel === "all") {
lazy.TranslationsParent.downloadAllFiles().then(
function () {
@ -253,6 +251,11 @@ export const GeckoViewTranslationsSettings = {
}
);
}
} else {
aCallback.onError(
`ERROR_UNKNOWN - The request to manage models appears to be malformed. Please check the parameters and try again.
Language: ${language}, Operation: ${operation}, Operation Level: ${operationLevel}`
);
}
break;
}