summaryrefslogtreecommitdiffstats
path: root/devtools/shared/commands/script/tests/browser_script_command_execute_last_result.js
blob: aebdaeb1684890a2d42ec9e1ba2b652065eec4ae (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
/* Any copyright is dedicated to the Public Domain.
 http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Testing that last evaluation result can be accessed with `$_`

add_task(async () => {
  const tab = await addTab(`data:text/html;charset=utf-8,`);

  const commands = await CommandsFactory.forTab(tab);
  await commands.targetCommand.startListening();

  info("$_ returns undefined if nothing has evaluated yet");
  let response = await commands.scriptCommand.execute("$_");
  basicResultCheck(response, "$_", { type: "undefined" });

  info("$_ returns last value and performs basic arithmetic");
  response = await commands.scriptCommand.execute("2+2");
  basicResultCheck(response, "2+2", 4);

  response = await commands.scriptCommand.execute("$_");
  basicResultCheck(response, "$_", 4);

  response = await commands.scriptCommand.execute("$_ + 2");
  basicResultCheck(response, "$_ + 2", 6);

  response = await commands.scriptCommand.execute("$_ + 4");
  basicResultCheck(response, "$_ + 4", 10);

  info("$_ has correct references to objects");
  response = await commands.scriptCommand.execute("var foo = {bar:1}; foo;");
  basicResultCheck(response, "var foo = {bar:1}; foo;", {
    type: "object",
    class: "Object",
    actor: /[a-z]/,
  });
  checkObject(response.result.getGrip().preview.ownProperties, {
    bar: {
      value: 1,
    },
  });

  response = await commands.scriptCommand.execute("$_");
  basicResultCheck(response, "$_", {
    type: "object",
    class: "Object",
    actor: /[a-z]/,
  });
  checkObject(response.result.getGrip().preview.ownProperties, {
    bar: {
      value: 1,
    },
  });

  info(
    "Update a property value and check that evaluating $_ returns the expected object instance"
  );
  await ContentTask.spawn(gBrowser.selectedBrowser, null, () => {
    content.wrappedJSObject.foo.bar = "updated_value";
  });

  response = await commands.scriptCommand.execute("$_");
  basicResultCheck(response, "$_", {
    type: "object",
    class: "Object",
    actor: /[a-z]/,
  });
  checkObject(response.result.getGrip().preview.ownProperties, {
    bar: {
      value: "updated_value",
    },
  });

  await commands.destroy();
});

function basicResultCheck(response, input, output) {
  checkObject(response, {
    input,
    result: output,
  });
  ok(!response.exception, "no eval exception");
  ok(!response.helperResult, "no helper result");
}