test_jsterm.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf8">
  5. <title>Test for JavaScript terminal functionality</title>
  6. <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  7. <script type="text/javascript;version=1.8" src="common.js"></script>
  8. <!-- Any copyright is dedicated to the Public Domain.
  9. - http://creativecommons.org/publicdomain/zero/1.0/ -->
  10. </head>
  11. <body>
  12. <p>Test for JavaScript terminal functionality</p>
  13. <iframe id="content-iframe" src="http://example.com/chrome/devtools/shared/webconsole/test/sandboxed_iframe.html"></iframe>
  14. <script class="testbody" type="text/javascript;version=1.8">
  15. SimpleTest.waitForExplicitFinish();
  16. let gState;
  17. let {MAX_AUTOCOMPLETE_ATTEMPTS,MAX_AUTOCOMPLETIONS} = require("devtools/shared/webconsole/js-property-provider");
  18. // This test runs all of its assertions twice - once with
  19. // evaluateJS and once with evaluateJSAsync.
  20. let evaluatingSync = true;
  21. function evaluateJS(input, options = {}) {
  22. return new Promise((resolve, reject) => {
  23. if (evaluatingSync) {
  24. gState.client.evaluateJS(input, resolve, options);
  25. } else {
  26. gState.client.evaluateJSAsync(input, resolve, options);
  27. }
  28. });
  29. }
  30. function startTest()
  31. {
  32. removeEventListener("load", startTest);
  33. attachConsoleToTab(["PageError"], onAttach);
  34. }
  35. function onAttach(aState, aResponse)
  36. {
  37. top.foobarObject = Object.create(null);
  38. top.foobarObject.foo = 1;
  39. top.foobarObject.foobar = 2;
  40. top.foobarObject.foobaz = 3;
  41. top.foobarObject.omg = 4;
  42. top.foobarObject.omgfoo = 5;
  43. top.foobarObject.strfoo = "foobarz";
  44. top.foobarObject.omgstr = "foobarz" +
  45. (new Array(DebuggerServer.LONG_STRING_LENGTH * 2)).join("abb");
  46. top.largeObject1 = Object.create(null);
  47. for (let i = 0; i < MAX_AUTOCOMPLETE_ATTEMPTS + 1; i++) {
  48. top.largeObject1['a' + i] = i;
  49. }
  50. top.largeObject2 = Object.create(null);
  51. for (let i = 0; i < MAX_AUTOCOMPLETIONS * 2; i++) {
  52. top.largeObject2['a' + i] = i;
  53. }
  54. gState = aState;
  55. let tests = [doSimpleEval, doWindowEval, doEvalWithException,
  56. doEvalWithHelper, doEvalString, doEvalLongString,
  57. doEvalWithBinding, doEvalWithBindingFrame,
  58. forceLexicalInit].map(t => {
  59. return Task.async(t);
  60. });
  61. runTests(tests, testEnd);
  62. }
  63. function* doSimpleEval() {
  64. info("test eval '2+2'");
  65. let response = yield evaluateJS("2+2");
  66. checkObject(response, {
  67. from: gState.actor,
  68. input: "2+2",
  69. result: 4,
  70. });
  71. ok(!response.exception, "no eval exception");
  72. ok(!response.helperResult, "no helper result");
  73. nextTest();
  74. }
  75. function* doWindowEval() {
  76. info("test eval 'document'");
  77. let response = yield evaluateJS("document");
  78. checkObject(response, {
  79. from: gState.actor,
  80. input: "document",
  81. result: {
  82. type: "object",
  83. class: "XULDocument",
  84. actor: /[a-z]/,
  85. },
  86. });
  87. ok(!response.exception, "no eval exception");
  88. ok(!response.helperResult, "no helper result");
  89. nextTest();
  90. }
  91. function* doEvalWithException() {
  92. info("test eval with exception");
  93. let response = yield evaluateJS("window.doTheImpossible()");
  94. checkObject(response, {
  95. from: gState.actor,
  96. input: "window.doTheImpossible()",
  97. result: {
  98. type: "undefined",
  99. },
  100. exceptionMessage: /doTheImpossible/,
  101. });
  102. ok(response.exception, "js eval exception");
  103. ok(!response.helperResult, "no helper result");
  104. nextTest();
  105. }
  106. function* doEvalWithHelper() {
  107. info("test eval with helper");
  108. let response = yield evaluateJS("clear()");
  109. checkObject(response, {
  110. from: gState.actor,
  111. input: "clear()",
  112. result: {
  113. type: "undefined",
  114. },
  115. helperResult: { type: "clearOutput" },
  116. });
  117. ok(!response.exception, "no eval exception");
  118. nextTest();
  119. }
  120. function* doEvalString() {
  121. let response = yield evaluateJS("window.foobarObject.strfoo");
  122. checkObject(response, {
  123. from: gState.actor,
  124. input: "window.foobarObject.strfoo",
  125. result: "foobarz",
  126. });
  127. nextTest();
  128. }
  129. function* doEvalLongString() {
  130. let response = yield evaluateJS("window.foobarObject.omgstr");
  131. let str = top.foobarObject.omgstr;
  132. let initial = str.substring(0, DebuggerServer.LONG_STRING_INITIAL_LENGTH);
  133. checkObject(response, {
  134. from: gState.actor,
  135. input: "window.foobarObject.omgstr",
  136. result: {
  137. type: "longString",
  138. initial: initial,
  139. length: str.length,
  140. },
  141. });
  142. nextTest();
  143. }
  144. function* doEvalWithBinding() {
  145. let response = yield evaluateJS("document;");
  146. let documentActor = response.result.actor;
  147. info("running a command with _self as document using bindObjectActor");
  148. let bindObjectSame = yield evaluateJS("_self === document", {
  149. bindObjectActor: documentActor
  150. });
  151. checkObject(bindObjectSame, {
  152. result: true
  153. });
  154. info("running a command with _self as document using selectedObjectActor");
  155. let selectedObjectSame = yield evaluateJS("_self === document", {
  156. selectedObjectActor: documentActor
  157. });
  158. checkObject(selectedObjectSame, {
  159. result: true
  160. });
  161. nextTest();
  162. }
  163. function* doEvalWithBindingFrame() {
  164. let frameWin = top.document.querySelector("iframe").contentWindow;
  165. frameWin.fooFrame = { bar: 1 };
  166. let response = yield evaluateJS(
  167. "document.querySelector('iframe').contentWindow.fooFrame"
  168. );
  169. let iframeObjectActor = response.result.actor;
  170. ok(iframeObjectActor, "There is an actor associated with the response");
  171. let bindObjectGlobal = yield evaluateJS("this.temp0 = _self;", {
  172. bindObjectActor: iframeObjectActor
  173. });
  174. ok(!top.temp0,
  175. "Global doesn't match the top global with bindObjectActor");
  176. ok(frameWin.temp0 && frameWin.temp0.bar === 1,
  177. "Global matches the object's global with bindObjectActor");
  178. let selectedObjectGlobal = yield evaluateJS("this.temp1 = _self;", {
  179. selectedObjectActor: iframeObjectActor
  180. });
  181. ok(top.temp1 && top.temp1.bar === 1,
  182. "Global matches the top global with bindObjectActor");
  183. ok(!frameWin.temp1,
  184. "Global doesn't match the object's global with bindObjectActor");
  185. nextTest()
  186. }
  187. function* forceLexicalInit() {
  188. info("test that failed let/const bindings are initialized to undefined");
  189. const testData = [
  190. {
  191. stmt: "let foopie = wubbalubadubdub",
  192. vars: ["foopie"]
  193. },
  194. {
  195. stmt: "let {z, w={n}=null} = {}",
  196. vars: ["z", "w"]
  197. },
  198. {
  199. stmt: "let [a, b, c] = null",
  200. vars: ["a", "b", "c"]
  201. },
  202. {
  203. stmt: "const nein1 = rofl, nein2 = copter",
  204. vars: ["nein1", "nein2"]
  205. },
  206. {
  207. stmt: "const {ha} = null",
  208. vars: ["ha"]
  209. },
  210. {
  211. stmt: "const [haw=[lame]=null] = []",
  212. vars: ["haw"]
  213. },
  214. {
  215. stmt: "const [rawr, wat=[lame]=null] = []",
  216. vars: ["rawr", "haw"]
  217. },
  218. {
  219. stmt: "let {zzz: xyz=99, zwz: wb} = nexistepas()",
  220. vars: ["xyz", "wb"]
  221. },
  222. {
  223. stmt: "let {c3pdoh=101} = null",
  224. vars: ["c3pdoh"]
  225. }
  226. ];
  227. for (let data of testData) {
  228. let response = yield evaluateJS(data.stmt);
  229. checkObject(response, {
  230. from: gState.actor,
  231. input: data.stmt,
  232. result: undefined,
  233. });
  234. ok(response.exception, "expected exception");
  235. for (let varName of data.vars) {
  236. let response2 = yield evaluateJS(varName);
  237. checkObject(response2, {
  238. from: gState.actor,
  239. input: varName,
  240. result: undefined,
  241. });
  242. ok(!response2.exception, "unexpected exception");
  243. }
  244. }
  245. nextTest();
  246. }
  247. function testEnd()
  248. {
  249. // If this is the first run, reload the page and do it again.
  250. // Otherwise, end the test.
  251. closeDebugger(gState, function() {
  252. gState = null;
  253. if (evaluatingSync) {
  254. evaluatingSync = false;
  255. startTest();
  256. } else {
  257. SimpleTest.finish();
  258. }
  259. });
  260. }
  261. addEventListener("load", startTest);
  262. </script>
  263. </body>
  264. </html>