head.js (4045B)
1 const RELATIVE_DIR = "image/test/browser/"; 2 const TESTROOT = "http://example.com/browser/" + RELATIVE_DIR; 3 const TESTROOT2 = "http://example.org/browser/" + RELATIVE_DIR; 4 5 var chrome_root = getRootDirectory(gTestPath); 6 const CHROMEROOT = chrome_root; 7 8 function getImageLoading(doc, id) { 9 return doc.getElementById(id); 10 } 11 12 // Tries to get the Moz debug image, imgIContainerDebug. Only works 13 // in a debug build. If we succeed, we call func(). 14 function actOnMozImage(doc, id, func) { 15 var imgContainer = getImageLoading(doc, id).getRequest( 16 Ci.nsIImageLoadingContent.CURRENT_REQUEST 17 ).image; 18 var mozImage; 19 try { 20 mozImage = imgContainer.QueryInterface(Ci.imgIContainerDebug); 21 } catch (e) { 22 return false; 23 } 24 func(mozImage); 25 return true; 26 } 27 28 function assertPrefVal(name, val) { 29 let boolValue = Services.prefs.getBoolPref(name); 30 Assert.strictEqual(boolValue, val, `pref ${name} is set to ${val}`); 31 if (boolValue !== val) { 32 throw Error(`pref ${name} is not set to ${val}`); 33 } 34 } 35 36 function assertFileProcess() { 37 // Ensure that the file content process is enabled. 38 assertPrefVal("browser.tabs.remote.separateFileUriProcess", true); 39 } 40 41 function getPage() { 42 let filePage = undefined; 43 switch (Services.appinfo.OS) { 44 case "WINNT": 45 filePage = "file:///C:/"; 46 break; 47 case "Darwin": 48 filePage = "file:///tmp/"; 49 break; 50 case "Linux": 51 filePage = "file:///tmp/"; 52 break; 53 default: 54 throw new Error("Unsupported operating system"); 55 } 56 return filePage; 57 } 58 59 function getSize() { 60 let iconSize = undefined; 61 switch (Services.appinfo.OS) { 62 case "WINNT": 63 iconSize = 32; 64 break; 65 case "Darwin": 66 iconSize = 128; 67 break; 68 case "Linux": 69 iconSize = 128; 70 break; 71 default: 72 throw new Error("Unsupported operating system"); 73 } 74 return iconSize; 75 } 76 77 async function createMozIconInFile(ext, expectSuccess = true) { 78 const kPAGE = getPage(); 79 const kSize = expectSuccess ? getSize() : 24; // we get 24x24 when failing, 80 // e.g. when remoting is 81 // disabled and the sandbox 82 // headless is enabled 83 84 // open a tab in a file content process 85 let fileTab = await BrowserTestUtils.addTab(gBrowser, kPAGE, { 86 preferredRemoteType: "file", 87 }); 88 89 // get the browser for the file content process tab 90 let fileBrowser = gBrowser.getBrowserForTab(fileTab); 91 92 let checkIcon = async (_ext, _kSize, _expectSuccess) => { 93 const img = content.document.createElement("img"); 94 let waitLoad = new Promise(resolve => { 95 // only listen to successfull load event if we expect the image to 96 // actually load, e.g. with remoting disabled and sandbox headless 97 // enabled we dont expect it to work, and we will wait for onerror below 98 // to trigger. 99 if (_expectSuccess) { 100 img.addEventListener("load", resolve, { once: true }); 101 } 102 img.onerror = () => { 103 // With remoting enabled, 104 // Verified to work by forcing early `return NS_ERROR_NOT_AVAILABLE;` 105 // within `nsIconChannel::GetIcon(nsIURI* aURI, ByteBuf* aDataOut)` 106 // 107 // With remoting disabled and sandbox headless enabled, this should be 108 // the default path, since we don't add the "load" event listener. 109 ok(!_expectSuccess, "Error while loading moz-icon"); 110 resolve(); 111 }; 112 }); 113 img.setAttribute("src", `moz-icon://.${_ext}?size=${_kSize}`); 114 img.setAttribute("id", `moz-icon-${_ext}-${_kSize}`); 115 content.document.body.appendChild(img); 116 117 await waitLoad; 118 119 const icon = content.document.getElementById(`moz-icon-${_ext}-${_kSize}`); 120 Assert.notStrictEqual(icon, null, `got a valid ${_ext} moz-icon`); 121 is(icon.width, _kSize, `${_kSize} px width ${_ext} moz-icon`); 122 is(icon.height, _kSize, `${_kSize} px height ${_ext} moz-icon`); 123 }; 124 125 await BrowserTestUtils.browserLoaded(fileBrowser); 126 await SpecialPowers.spawn( 127 fileBrowser, 128 [ext, kSize, expectSuccess], 129 checkIcon 130 ); 131 await BrowserTestUtils.removeTab(fileTab); 132 }