head.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. /* import-globals-from ../../framework/test/shared-head.js */
  4. "use strict";
  5. // shared-head.js handles imports, constants, and utility functions
  6. Services.scriptloader.loadSubScript(
  7. "chrome://mochitests/content/browser/devtools/client/framework/test/shared-head.js",
  8. this);
  9. const { NetUtil } = require("resource://gre/modules/NetUtil.jsm");
  10. const Editor = require("devtools/client/sourceeditor/editor");
  11. const {getClientCssProperties} = require("devtools/shared/fronts/css-properties");
  12. flags.testing = true;
  13. SimpleTest.registerCleanupFunction(() => {
  14. flags.testing = false;
  15. });
  16. function promiseWaitForFocus() {
  17. return new Promise(resolve =>
  18. waitForFocus(resolve));
  19. }
  20. function setup(cb, additionalOpts = {}) {
  21. cb = cb || function () {};
  22. let def = promise.defer();
  23. const opt = "chrome,titlebar,toolbar,centerscreen,resizable,dialog=no";
  24. const url = "data:application/vnd.mozilla.xul+xml;charset=UTF-8," +
  25. "<?xml version='1.0'?>" +
  26. "<?xml-stylesheet href='chrome://global/skin/global.css'?>" +
  27. "<window xmlns='http://www.mozilla.org/keymaster/gatekeeper" +
  28. "/there.is.only.xul' title='Editor' width='600' height='500'>" +
  29. "<box flex='1'/></window>";
  30. let win = Services.ww.openWindow(null, url, "_blank", opt, null);
  31. let opts = {
  32. value: "Hello.",
  33. lineNumbers: true,
  34. foldGutter: true,
  35. gutters: ["CodeMirror-linenumbers", "breakpoints", "CodeMirror-foldgutter"],
  36. cssProperties: getClientCssProperties()
  37. };
  38. for (let o in additionalOpts) {
  39. opts[o] = additionalOpts[o];
  40. }
  41. win.addEventListener("load", function onLoad() {
  42. win.removeEventListener("load", onLoad, false);
  43. waitForFocus(function () {
  44. let box = win.document.querySelector("box");
  45. let editor = new Editor(opts);
  46. editor.appendTo(box)
  47. .then(() => {
  48. def.resolve({
  49. ed: editor,
  50. win: win,
  51. edWin: editor.container.contentWindow.wrappedJSObject
  52. });
  53. cb(editor, win);
  54. }, err => ok(false, err.message));
  55. }, win);
  56. }, false);
  57. return def.promise;
  58. }
  59. function ch(exp, act, label) {
  60. is(exp.line, act.line, label + " (line)");
  61. is(exp.ch, act.ch, label + " (ch)");
  62. }
  63. function teardown(ed, win) {
  64. ed.destroy();
  65. win.close();
  66. while (gBrowser.tabs.length > 1) {
  67. gBrowser.removeCurrentTab();
  68. }
  69. finish();
  70. }
  71. /**
  72. * Some tests may need to import one or more of the test helper scripts.
  73. * A test helper script is simply a js file that contains common test code that
  74. * is either not common-enough to be in head.js, or that is located in a
  75. * separate directory.
  76. * The script will be loaded synchronously and in the test's scope.
  77. * @param {String} filePath The file path, relative to the current directory.
  78. * Examples:
  79. * - "helper_attributes_test_runner.js"
  80. * - "../../../commandline/test/helpers.js"
  81. */
  82. function loadHelperScript(filePath) {
  83. let testDir = gTestPath.substr(0, gTestPath.lastIndexOf("/"));
  84. Services.scriptloader.loadSubScript(testDir + "/" + filePath, this);
  85. }
  86. /**
  87. * This method returns the portion of the input string `source` up to the
  88. * [line, ch] location.
  89. */
  90. function limit(source, [line, ch]) {
  91. line++;
  92. let list = source.split("\n");
  93. if (list.length < line) {
  94. return source;
  95. }
  96. if (line == 1) {
  97. return list[0].slice(0, ch);
  98. }
  99. return [...list.slice(0, line - 1), list[line - 1].slice(0, ch)].join("\n");
  100. }
  101. function read(url) {
  102. let scriptableStream = Cc["@mozilla.org/scriptableinputstream;1"]
  103. .getService(Ci.nsIScriptableInputStream);
  104. let channel = NetUtil.newChannel({
  105. uri: url,
  106. loadUsingSystemPrincipal: true
  107. });
  108. let input = channel.open2();
  109. scriptableStream.init(input);
  110. let data = "";
  111. while (input.available()) {
  112. data = data.concat(scriptableStream.read(input.available()));
  113. }
  114. scriptableStream.close();
  115. input.close();
  116. return data;
  117. }
  118. /**
  119. * This function is called by the CodeMirror test runner to report status
  120. * messages from the CM tests.
  121. * @see codemirror.html
  122. */
  123. function codemirrorSetStatus(statusMsg, type, customMsg) {
  124. switch (type) {
  125. case "expected":
  126. case "ok":
  127. ok(1, statusMsg);
  128. break;
  129. case "error":
  130. case "fail":
  131. ok(0, statusMsg);
  132. break;
  133. default:
  134. info(statusMsg);
  135. break;
  136. }
  137. if (customMsg && typeof customMsg == "string" && customMsg != statusMsg) {
  138. info(customMsg);
  139. }
  140. }