<!DOCTYPE html>
<html>
<body>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script>
async function languageChange() {
return new Promise(resolve => window.onlanguagechange = resolve);
}
async function languageChangeEvent() {
return new Promise(resolve => window.addEventListener('languagechange', resolve));
}
test(function() {
assert_true('language' in window.navigator);
assert_true('languages' in window.navigator);
assert_true(Array.isArray(window.navigator.languages));
assert_true(Object.isFrozen(window.navigator.languages));
assert_true('onlanguagechange' in window);
}, "Test that NavigatorLanguage API is present in window");
promise_test(async () => {
var languageChangePromise = languageChange();
testRunner.setAcceptLanguages('klingon');
await languageChangePromise;
assert_equals('klingon', window.navigator.language);
}, "Test that the languagechange event fires on window.onlanguagechange");
promise_test(async () => {
var languageChangePromise = languageChangeEvent();
testRunner.setAcceptLanguages('klingon-FR');
await languageChangePromise;
assert_equals('klingon-FR', window.navigator.language);
}, "Test that the languagechange event fires on window.addEventListener('languagechange')");
promise_test(async () => {
var promise = new Promise(resolve => window.languageResolver = resolve);
document.body.setAttribute('onlanguagechange', 'window.languageResolver();');
testRunner.setAcceptLanguages('klingon-US');
await promise;
assert_equals('klingon-US', window.navigator.language);
}, "Test that the languagechange event fires on body onlanguagechange attribute");
promise_test(async () => {
window.received = 0; // We need a global variable here.
document.body.setAttribute('onlanguagechange', 'window.received++;');
var languageChangePromise = languageChange();
testRunner.setAcceptLanguages('klingon');
await languageChangePromise;
assert_equals(window.received, 0);
assert_equals('klingon', window.navigator.language);
languageChangePromise = languageChange();
testRunner.setAcceptLanguages('klingon-FR');
await languageChangePromise;
assert_equals(window.received, 0);
assert_equals('klingon-FR', window.navigator.language);
}, "Test that the languagechange event fires on body onlanguagechange attribute XOR window.onlanguagechange");
promise_test(async () => {
var promise = new Promise(resolve => window.addEventListener('languagechange', function(e) {
assert_false(e.cancelable);
assert_false(e.bubbles);
resolve();
}));
testRunner.setAcceptLanguages('klingon');
await promise;
}, "Test properties of the fired event.");
promise_test(async () => {
var testValues = [
{ accept_languages: 'foo', languages: ['foo'] },
{ accept_languages: '', languages: ['en-US'] },
{ accept_languages: 'foo,bar', languages: [ 'foo', 'bar' ] },
{ accept_languages: ' foo , bar ', languages: [ 'foo', 'bar' ] },
{ accept_languages: ' foo ; bar ', languages: [ 'foo ; bar' ] },
{ accept_languages: '_foo_', languages: ['_foo_'] },
{ accept_languages: 'en_', languages: ['en-'] },
{ accept_languages: 'en__', languages: ['en-_'] },
{ accept_languages: 'en_US, fr_FR', languages: ['en-US', 'fr-FR'] },
{ accept_languages: 'en_US_CA', languages: ['en-US_CA'] },
];
for (var i = 0; i < testValues.length; ++i) {
var data = testValues[i];
var languageChangePromise = languageChange();
testRunner.setAcceptLanguages(data.accept_languages);
await languageChangePromise;
// ReduceAcceptLanguage experiment turns on, only returns one language.
assert_equals(navigator.languages.length, 1);
assert_equals(navigator.language, data.languages[0]);
assert_equals(navigator.languages[0], data.languages[0]);
}
}, "Test that navigator.languages reflects the accept languages value.");
promise_test(async () => {
var previous = navigator.languages;
assert_equals(navigator.languages, navigator.languages);
assert_equals(navigator.languages, previous);
var languageChangePromise = languageChange();
testRunner.setAcceptLanguages('fr-FR');
await languageChangePromise;
assert_not_equals(navigator.languages, previous);
}, "Test navigator.languages' caching behaviour.");
</script>
</body>
</html>