summaryrefslogtreecommitdiffstats
path: root/devtools/shared/webconsole/test/chrome/test_jsterm.html
blob: 65e7b519d8de9c7259524face140fa69f6ebe67e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<!DOCTYPE HTML>
<html lang="en">
<head>
  <meta charset="utf8">
  <title>Test for JavaScript terminal functionality</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="common.js"></script>
  <!-- Any copyright is dedicated to the Public Domain.
     - http://creativecommons.org/publicdomain/zero/1.0/ -->
</head>
<body>
<p>Test for JavaScript terminal functionality</p>

<iframe id="content-iframe" src="http://example.com/chrome/devtools/shared/webconsole/test/chrome/sandboxed_iframe.html"></iframe>

<script class="testbody" type="text/javascript">
"use strict";

SimpleTest.waitForExplicitFinish();

let gState;

const {MAX_AUTOCOMPLETE_ATTEMPTS,MAX_AUTOCOMPLETIONS} = require("devtools/shared/webconsole/js-property-provider");


function evaluateJS(input, options = {}) {
  return gState.webConsoleFront.evaluateJSAsync(input, options);
} 

async function startTest()
{
  removeEventListener("load", startTest);

  const {state} = await attachConsoleToTab(["PageError"]);
  onAttach(state);
}

function onAttach(aState)
{
  top.foobarObject = Object.create(null);
  top.foobarObject.foo = 1;
  top.foobarObject.foobar = 2;
  top.foobarObject.foobaz = 3;
  top.foobarObject.omg = 4;
  top.foobarObject.omgfoo = 5;
  top.foobarObject.strfoo = "foobarz";
  top.foobarObject.omgstr = "foobarz" +
    (new Array(DevToolsServer.LONG_STRING_LENGTH * 2)).join("abb");

  top.largeObject1 = Object.create(null);
  for (let i = 0; i < MAX_AUTOCOMPLETE_ATTEMPTS + 1; i++) {
    top.largeObject1['a' + i] = i;
  }

  top.largeObject2 = Object.create(null);
  for (let i = 0; i < MAX_AUTOCOMPLETIONS * 2; i++) {
    top.largeObject2['a' + i] = i;
  }

  gState = aState;

  const tests = [doSimpleEval, doWindowEval, doEvalWithException,
               doEvalWithHelper, doEvalString, doEvalLongString,
               doEvalWithBinding, doEvalWithBindingFrame,
               forceLexicalInit];

  runTests(tests, testEnd);
}

async function doSimpleEval() {
  info("test eval '2+2'");
  const response = await evaluateJS("2+2");
  checkObject(response, {
    input: "2+2",
    result: 4,
  });

  ok(!response.exception, "no eval exception");
  ok(!response.helperResult, "no helper result");

  nextTest();
}

async function doWindowEval() {
  info("test eval 'document'");
  const response = await evaluateJS("document");
  checkObject(response, {
    input: "document",
    result: {
      type: "object",
      class: "HTMLDocument",
      actor: /[a-z]/,
    },
  });

  ok(!response.exception, "no eval exception");
  ok(!response.helperResult, "no helper result");

  nextTest();
}

async function doEvalWithException() {
  info("test eval with exception");
  const response = await evaluateJS("window.doTheImpossible()");
  checkObject(response, {
    input: "window.doTheImpossible()",
    result: {
      type: "undefined",
    },
    exceptionMessage: /doTheImpossible/,
  });

  ok(response.exception, "js eval exception");
  ok(!response.helperResult, "no helper result");

  nextTest();
}

async function doEvalWithHelper() {
  info("test eval with helper");
  const response = await evaluateJS("clear()");
  checkObject(response, {
    input: "clear()",
    result: {
      type: "undefined",
    },
    helperResult: { type: "clearOutput" },
  });

  ok(!response.exception, "no eval exception");

  nextTest();
}

async function doEvalString() {
  const response = await evaluateJS("window.foobarObject.strfoo");
  checkObject(response, {
    input: "window.foobarObject.strfoo",
    result: "foobarz",
  });

  nextTest();
}

async function doEvalLongString() {
  const response = await evaluateJS("window.foobarObject.omgstr");
  const str = top.foobarObject.omgstr;
  const initial = str.substring(0, DevToolsServer.LONG_STRING_INITIAL_LENGTH);

  checkObject(response, {
    input: "window.foobarObject.omgstr",
    result: {
      type: "longString",
      initial: initial,
      length: str.length,
    },
  });

  nextTest();
}

async function doEvalWithBinding() {
  const response = await evaluateJS("document;");
  const documentActor = response.result.actorID;

  info("running a command with _self as document using selectedObjectActor");
  const selectedObjectSame = await evaluateJS("_self === document", {
    selectedObjectActor: documentActor
  });
  checkObject(selectedObjectSame, {
    result: true
  });

  nextTest();
}

async function doEvalWithBindingFrame() {
  const frameWin = top.document.querySelector("iframe").contentWindow;
  frameWin.fooFrame = { bar: 1 };

  const response = await evaluateJS(
    "document.querySelector('iframe').contentWindow.fooFrame"
  );
  const iframeObjectActor = response.result.actorID;
  ok(iframeObjectActor, "There is an actor associated with the response");

  await evaluateJS("this.temp1 = _self;", {
    selectedObjectActor: iframeObjectActor
  });
  ok(top.temp1 && top.temp1.bar === 1,
    "Global matches the top global with bindObjectActor");
  ok(!frameWin.temp1,
    "Global doesn't match the object's global with bindObjectActor");

  nextTest()
}

async function forceLexicalInit() {
  info("test that failed let/const bindings are initialized to undefined");

  const testData = [
    {
        stmt: "let foopie = wubbalubadubdub",
        vars: ["foopie"]
    },
    {
        stmt: "let {z, w={n}=null} = {}",
        vars: ["z", "w"]
    },
    {
        stmt: "let [a, b, c] = null",
        vars: ["a", "b", "c"]
    },
    {
        stmt: "const nein1 = rofl, nein2 = copter",
        vars: ["nein1", "nein2"]
    },
    {
        stmt: "const {ha} = null",
        vars: ["ha"]
    },
    {
        stmt: "const [haw=[lame]=null] = []",
        vars: ["haw"]
    },
    {
        stmt: "const [rawr, wat=[lame]=null] = []",
        vars: ["rawr", "haw"]
    },
    {
        stmt: "let {zzz: xyz=99, zwz: wb} = nexistepas()",
        vars: ["xyz", "wb"]
    },
    {
        stmt: "let {c3pdoh=101} = null",
        vars: ["c3pdoh"]
    }
  ];

  for (const data of testData) {
      const response = await evaluateJS(data.stmt);
      checkObject(response, {
        input: data.stmt,
        result: undefined,
      });
      ok(response.exception, "expected exception");
      for (const varName of data.vars) {
          const response2 = await evaluateJS(varName);
          checkObject(response2, {
            input: varName,
            result: undefined,
          });
          ok(!response2.exception, "unexpected exception");
      }
  }

  nextTest();
}

function testEnd()
{
  // If this is the first run, reload the page and do it again.
  // Otherwise, end the test.
  closeDebugger(gState, function() {
    gState = null;
    SimpleTest.finish();
  });
}

addEventListener("load", startTest);
</script>
</body>
</html>