diff options
Diffstat (limited to 'intl/l10n/test/mochitest/localization')
3 files changed, 267 insertions, 0 deletions
diff --git a/intl/l10n/test/mochitest/localization/test_formatMessages.html b/intl/l10n/test/mochitest/localization/test_formatMessages.html new file mode 100644 index 0000000000..6e5a9f3f41 --- /dev/null +++ b/intl/l10n/test/mochitest/localization/test_formatMessages.html @@ -0,0 +1,101 @@ +<!DOCTYPE HTML> +<html> +<head> + <meta charset="utf-8"> + <title>Test Localization.prototype.formatMessages API</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"> + <script type="application/javascript"> + "use strict"; + const mockSource = L10nFileSource.createMock("test", "app", ["en-US"], "/localization/{locale}/", [ + { + path: "/localization/en-US/mock.ftl", + source: ` +key1 = Value + .title = Title 1 + .accesskey = K +key2 = + .label = This is a label for { $user } +` + } + ]); + let registry = new L10nRegistry({ + bundleOptions: { + useIsolating: false + } + }); + registry.registerSources([mockSource]); + + function getAttributeByName(attributes, name) { + return attributes.find(attr => attr.name === name); + } + + (async () => { + SimpleTest.waitForExplicitFinish(); + + const loc = new Localization( + ['mock.ftl'], + false, + registry, + ); + + { + // Simple mix works. + let msgs = await loc.formatMessages([ + {id: "key1"}, + {id: "key2", args: { user: "Amy"}}, + ]); + { + is(msgs[0].value, "Value"); + let attr0 = getAttributeByName(msgs[0].attributes, "title"); + is(attr0.name, "title"); + is(attr0.value, "Title 1"); + let attr1 = getAttributeByName(msgs[0].attributes, "accesskey"); + is(attr1.name, "accesskey"); + is(attr1.value, "K"); + } + + { + is(msgs[1].value, null); + let attr0 = getAttributeByName(msgs[1].attributes, "label"); + is(attr0.name, "label"); + is(attr0.value, "This is a label for Amy"); + } + } + + { + // Missing arguments cause exception in automation. + try { + let msgs = await loc.formatMessages([ + {id: "key1"}, + {id: "key2"}, + ]); + ok(false, "Missing argument didn't cause an exception."); + } catch (e) { + is(e.message, + "[fluent][resolver] errors in en-US/key2: Resolver error: Unknown variable: $user", + "Missing key causes an exception."); + } + } + + { + // Missing keys cause exception in automation. + try { + let msgs = await loc.formatMessages([ + { id: "key1" }, + { id: "key4" }, + ]); + ok(false, "Missing key didn't cause an exception."); + } catch (e) { + is(e.message, + "[fluent] Missing message in locale en-US: key4", + "Missing key causes an exception."); + } + } + SimpleTest.finish(); + })(); + </script> +</head> +<body> +</body> +</html> diff --git a/intl/l10n/test/mochitest/localization/test_formatValue.html b/intl/l10n/test/mochitest/localization/test_formatValue.html new file mode 100644 index 0000000000..e1ee02fa7a --- /dev/null +++ b/intl/l10n/test/mochitest/localization/test_formatValue.html @@ -0,0 +1,81 @@ +<!DOCTYPE HTML> +<html> +<head> + <meta charset="utf-8"> + <title>Test Localization.prototype.formatValue API</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"> + <script type="application/javascript"> + "use strict"; + const mockSource = L10nFileSource.createMock("test", "app", ["en-US"], "/localization/{locale}/", [ + { + path: "/localization/en-US/mock.ftl", + source: ` +key1 = Value +key2 = Value { $user } +key3 = Value { $count } +` + } + ]); + let registry = new L10nRegistry({ + bundleOptions: { + useIsolating: false + } + }); + registry.registerSources([mockSource]); + + (async () => { + SimpleTest.waitForExplicitFinish(); + + const loc = new Localization( + ['mock.ftl'], + false, + registry, + ); + + { + // Simple value works. + let val = await loc.formatValue("key1"); + is(val, "Value"); + } + + { + // Value with a string argument works. + let val = await loc.formatValue("key2", { user: "John" }); + is(val, "Value John"); + } + + { + // Value with a number argument works. + let val = await loc.formatValue("key3", { count: -3.21 }); + is(val, "Value -3.21"); + } + + { + // Verify that in automation, a missing + // argument causes an exception. + try { + let val = await loc.formatValue("key3"); + ok(false, "Missing argument didn't cause an exception."); + } catch (e) { + is(e.message, + "[fluent][resolver] errors in en-US/key3: Resolver error: Unknown variable: $count", + "Missing key causes an exception."); + } + } + + { + // Incorrect argument type works. + // Due to how WebIDL handles union types, it'll convert + // the argument to a string `[object Object]`. + let val = await loc.formatValue("key2", { user: { name: true } }); + is(val, "Value [object Object]"); + } + + SimpleTest.finish(); + })(); + </script> +</head> +<body> +</body> +</html> diff --git a/intl/l10n/test/mochitest/localization/test_formatValues.html b/intl/l10n/test/mochitest/localization/test_formatValues.html new file mode 100644 index 0000000000..819d418b2e --- /dev/null +++ b/intl/l10n/test/mochitest/localization/test_formatValues.html @@ -0,0 +1,85 @@ +<!DOCTYPE HTML> +<html> +<head> + <meta charset="utf-8"> + <title>Test Localization.prototype.formatValues API</title> + <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"> + <script type="application/javascript"> + "use strict"; + const mockSource = L10nFileSource.createMock("test", "app", ["en-US"], "/localization/{locale}/", [ + { + path: "/localization/en-US/mock.ftl", + source: ` +key1 = Value +key2 = Value { $user } +key3 = Value { $count } +` + } + ]); + let registry = new L10nRegistry({ + bundleOptions: { + useIsolating: false + } + }); + registry.registerSources([mockSource]); + + (async () => { + SimpleTest.waitForExplicitFinish(); + + const loc = new Localization( + ['mock.ftl'], + false, + registry, + ); + + { + // Simple mix works. + let vals = await loc.formatValues([ + {id: "key1"}, + {id: "key2", args: { user: "Amy"}}, + {id: "key3", args: { count: -32.12 }}, + ]); + is(vals[0], "Value"); + is(vals[1], "Value Amy"); + is(vals[2], "Value -32.12"); + } + + { + // Missing arguments cause exception in automation. + try { + let vals = await loc.formatValues([ + {id: "key1"}, + {id: "key2"}, + {id: "key3", args: { count: -32.12 }}, + ]); + ok(false, "Missing argument didn't cause an exception."); + } catch (e) { + is(e.message, + "[fluent][resolver] errors in en-US/key2: Resolver error: Unknown variable: $user", + "Missing key causes an exception."); + } + } + + { + // Missing keys cause exception in automation. + try { + let vals = await loc.formatValues([ + { id: "key1" }, + { id: "key4", args: { count: -32.12 } }, + ]); + ok(false, "Missing key didn't cause an exception."); + } catch (e) { + is(e.message, + "[fluent] Missing message in locale en-US: key4", + "Missing key causes an exception."); + } + } + + SimpleTest.finish(); + })(); + </script> +</head> +<body> +</body> +</html> |