head.js (4956B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 /* exported promiseWaitForFocus, setup, ch, teardown, loadHelperScript, 5 limit, ch, read, codemirrorSetStatus */ 6 7 "use strict"; 8 9 // shared-head.js handles imports, constants, and utility functions 10 Services.scriptloader.loadSubScript( 11 "chrome://mochitests/content/browser/devtools/client/shared/test/shared-head.js", 12 this 13 ); 14 15 const Editor = require("resource://devtools/client/shared/sourceeditor/editor.js"); 16 17 function promiseWaitForFocus(el) { 18 return new Promise(resolve => waitForFocus(resolve, el)); 19 } 20 21 async function setup(additionalOpts = {}) { 22 try { 23 const opt = "chrome,titlebar,toolbar,centerscreen,resizable,dialog=no"; 24 const win = Services.ww.openWindow( 25 null, 26 CHROME_URL_ROOT + "head.xhtml", 27 "_blank", 28 opt, 29 null 30 ); 31 const opts = { 32 value: "Hello.", 33 lineNumbers: true, 34 foldGutter: true, 35 gutters: [ 36 "CodeMirror-linenumbers", 37 "breakpoints", 38 "CodeMirror-foldgutter", 39 ], 40 cssProperties: getClientCssProperties(), 41 ...additionalOpts, 42 }; 43 44 await once(win, "load"); 45 await promiseWaitForFocus(win); 46 47 const box = win.document.querySelector("box"); 48 const editor = new Editor(opts); 49 await editor.appendTo(box); 50 51 return { 52 ed: editor, 53 win, 54 edWin: editor.container.contentWindow.wrappedJSObject, 55 }; 56 } catch (o_O) { 57 ok(false, o_O.message); 58 return null; 59 } 60 } 61 62 function ch(exp, act, label) { 63 is(exp.line, act.line, label + " (line)"); 64 is(exp.ch, act.ch, label + " (ch)"); 65 } 66 67 function teardown(ed, win) { 68 ed.destroy(); 69 win.close(); 70 71 while (gBrowser.tabs.length > 1) { 72 gBrowser.removeCurrentTab(); 73 } 74 finish(); 75 } 76 77 /** 78 * Some tests may need to import one or more of the test helper scripts. 79 * A test helper script is simply a js file that contains common test code that 80 * is either not common-enough to be in head.js, or that is located in a 81 * separate directory. 82 * The script will be loaded synchronously and in the test's scope. 83 * 84 * @param {string} filePath The file path, relative to the current directory. 85 * Examples: 86 * - "helper_attributes_test_runner.js" 87 */ 88 function loadHelperScript(filePath) { 89 const testDir = gTestPath.substr(0, gTestPath.lastIndexOf("/")); 90 Services.scriptloader.loadSubScript(testDir + "/" + filePath, this); 91 } 92 93 /** 94 * This method returns the portion of the input string `source` up to the 95 * [line, ch] location. 96 */ 97 function limit(source, [line, char]) { 98 line++; 99 const list = source.split("\n"); 100 if (list.length < line) { 101 return source; 102 } 103 if (line == 1) { 104 return list[0].slice(0, char); 105 } 106 return [...list.slice(0, line - 1), list[line - 1].slice(0, char)].join("\n"); 107 } 108 109 function read(url) { 110 const scriptableStream = Cc[ 111 "@mozilla.org/scriptableinputstream;1" 112 ].getService(Ci.nsIScriptableInputStream); 113 114 const channel = NetUtil.newChannel({ 115 uri: url, 116 loadUsingSystemPrincipal: true, 117 }); 118 const input = channel.open(); 119 scriptableStream.init(input); 120 121 let data = ""; 122 while (input.available()) { 123 data = data.concat(scriptableStream.read(input.available())); 124 } 125 scriptableStream.close(); 126 input.close(); 127 128 return data; 129 } 130 131 /** 132 * This function is called by the CodeMirror test runner to report status 133 * messages from the CM tests. 134 * 135 * @see codemirror.html 136 */ 137 function codemirrorSetStatus(statusMsg, type, customMsg) { 138 switch (type) { 139 case "expected": 140 case "ok": 141 ok(1, statusMsg); 142 break; 143 case "error": 144 case "fail": 145 ok(0, statusMsg); 146 break; 147 default: 148 info(statusMsg); 149 break; 150 } 151 152 if (customMsg && typeof customMsg == "string" && customMsg != statusMsg) { 153 info(customMsg); 154 } 155 } 156 157 async function runCodeMirrorTest(uri) { 158 const actorURI = 159 "chrome://mochitests/content/browser/devtools/client/shared/sourceeditor/test/CodeMirrorTestActors.sys.mjs"; 160 161 const { CodeMirrorTestParent } = ChromeUtils.importESModule(actorURI); 162 163 ChromeUtils.registerWindowActor("CodeMirrorTest", { 164 parent: { 165 esModuleURI: actorURI, 166 }, 167 child: { 168 esModuleURI: actorURI, 169 events: { 170 DOMWindowCreated: {}, 171 }, 172 }, 173 }); 174 175 const donePromise = new Promise(resolve => { 176 CodeMirrorTestParent.setCallback((name, data) => { 177 switch (name) { 178 case "setStatus": { 179 const { statusMsg, type, customMsg } = data; 180 codemirrorSetStatus(statusMsg, type, customMsg); 181 break; 182 } 183 case "done": 184 resolve(!data.failed); 185 break; 186 } 187 }); 188 }); 189 190 await addTab(uri); 191 const result = await donePromise; 192 ok(result, "CodeMirror tests all passed"); 193 while (gBrowser.tabs.length > 1) { 194 gBrowser.removeCurrentTab(); 195 } 196 197 ChromeUtils.unregisterWindowActor("CodeMirrorTest"); 198 }