browser_resource_uri.js (3976B)
1 let uri = 2 "chrome://mochitests/content/browser/dom/l10n/tests/mochitest//document_l10n/non-system-principal/"; 3 let protocol = Services.io 4 .getProtocolHandler("resource") 5 .QueryInterface(Ci.nsIResProtocolHandler); 6 7 protocol.setSubstitution("l10n-test", Services.io.newURI(uri)); 8 9 // Since we want the mock source to work with all locales, we're going 10 // to register it for currently used locales, and we'll put the path that 11 // doesn't use the `{locale}` component to make it work irrelevant of 12 // what locale the mochitest is running in. 13 // 14 // Notice: we're using a `chrome://` protocol here only for convenience reasons. 15 // Real sources should use `resource://` protocol. 16 let locales = Services.locale.appLocalesAsBCP47; 17 18 // This source is actually using a real `FileSource` instead of a mocked one, 19 // because we want to test that fetching real I/O out of the `uri` works in non-system-principal. 20 let source = new L10nFileSource("test", "app", locales, `${uri}localization/`); 21 L10nRegistry.getInstance().registerSources([source]); 22 23 registerCleanupFunction(() => { 24 protocol.setSubstitution("l10n-test", null); 25 L10nRegistry.getInstance().removeSources(["test"]); 26 SpecialPowers.pushPrefEnv({ 27 set: [["dom.ipc.processPrelaunch.enabled", true]], 28 }); 29 }); 30 31 const kChildPage = getRootDirectory(gTestPath) + "test.html"; 32 33 const kAboutPagesRegistered = Promise.all([ 34 BrowserTestUtils.registerAboutPage( 35 registerCleanupFunction, 36 "test-about-l10n-child", 37 kChildPage, 38 Ci.nsIAboutModule.URI_MUST_LOAD_IN_CHILD | 39 Ci.nsIAboutModule.URI_SAFE_FOR_UNTRUSTED_CONTENT | 40 Ci.nsIAboutModule.ALLOW_SCRIPT 41 ), 42 ]); 43 44 add_task(async () => { 45 // Bug 1640333 - windows fails (sometimes) to ever get document.l10n.ready 46 // if e10s process caching is enabled 47 await SpecialPowers.pushPrefEnv({ 48 set: [ 49 ["dom.ipc.processPrelaunch.enabled", false], 50 ["dom.security.skip_about_page_has_csp_assert", true], 51 ], 52 }); 53 await kAboutPagesRegistered; 54 await BrowserTestUtils.withNewTab( 55 "about:test-about-l10n-child", 56 async browser => { 57 await SpecialPowers.spawn(browser, [], async function () { 58 let document = content.document; 59 let window = document.defaultView; 60 61 await document.testsReadyPromise; 62 63 let principal = SpecialPowers.wrap(document).nodePrincipal; 64 is( 65 principal.spec, 66 "about:test-about-l10n-child", 67 "correct content principal" 68 ); 69 70 let desc = document.getElementById("main-desc"); 71 72 // We can test here for a particular value because we're 73 // using a mock file source which is locale independent. 74 // 75 // If you're writing a test that verifies that a UI 76 // widget got real localization, you should not rely on 77 // the particular value, but rather on the content not 78 // being empty (to keep the test pass in non-en-US locales). 79 is(desc.textContent, "This is a mock page title"); 80 81 // Test for l10n.getAttributes 82 let label = document.getElementById("label1"); 83 let l10nArgs = document.l10n.getAttributes(label); 84 is(l10nArgs.id, "subtitle"); 85 is(l10nArgs.args.name, "Firefox"); 86 87 // Test for manual value formatting 88 let customMsg = document.getElementById("customMessage").textContent; 89 is(customMsg, "This is a custom message formatted from JS."); 90 91 // Since we applied the `data-l10n-id` attribute 92 // on `label` in this microtask, we have to wait for 93 // the next paint to verify that the MutationObserver 94 // applied the translation. 95 await new Promise(resolve => { 96 let verifyL10n = () => { 97 if (!label.textContent.includes("Firefox")) { 98 window.requestAnimationFrame(verifyL10n); 99 } else { 100 resolve(); 101 } 102 }; 103 104 window.requestAnimationFrame(verifyL10n); 105 }); 106 }); 107 } 108 ); 109 });