summaryrefslogtreecommitdiffstats
path: root/remote/cdp/test/browser/runtime/browser_evaluate_returnByValue.js
blob: 762d280b3110365306c97172a53124dfede9463f (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

add_task(async function returnByValueInvalidTypes({ client }) {
  const { Runtime } = client;

  await enableRuntime(client);

  for (const returnByValue of [null, 1, "foo", [], {}]) {
    await Assert.rejects(
      Runtime.evaluate({
        expression: "",
        returnByValue,
      }),
      err => err.message.includes("returnByValue: boolean value expected"),
      "returnByValue: boolean value expected"
    );
  }
});

add_task(async function returnByValueCyclicValue({ client }) {
  const { Runtime } = client;

  await enableRuntime(client);

  const expressions = ["const b = { a: 1}; b.b = b; b", "window"];

  for (const expression of expressions) {
    await Assert.rejects(
      Runtime.evaluate({
        expression,
        returnByValue: true,
      }),
      err => err.message.includes("Object reference chain is too long"),
      "Object reference chain is too long"
    );
  }
});

add_task(async function returnByValueNotPossible({ client }) {
  const { Runtime } = client;

  await enableRuntime(client);

  const expressions = ["Symbol(42)", "[Symbol(42)]", "{a: Symbol(42)}"];

  for (const expression of expressions) {
    await Assert.rejects(
      Runtime.evaluate({
        expression,
        returnByValue: true,
      }),
      err => err.message.includes("Object couldn't be returned by value"),
      "Object couldn't be returned by value"
    );
  }
});

add_task(async function returnByValue({ client }) {
  const { Runtime } = client;

  await enableRuntime(client);

  const values = [
    null,
    42,
    42.0,
    "42",
    true,
    false,
    { foo: true },
    { foo: { bar: 42, str: "str", array: [1, 2, 3] } },
    [42, "42", true],
    [{ foo: true }],
  ];

  for (const value of values) {
    const { result } = await Runtime.evaluate({
      expression: `(${JSON.stringify(value)})`,
      returnByValue: true,
    });

    Assert.deepEqual(
      result,
      {
        type: typeof value,
        value,
        description: value != null ? value.toString() : value,
      },
      `Returned expected value for ${JSON.stringify(value)}`
    );
  }
});

add_task(async function returnByValueNotSerializable({ client }) {
  const { Runtime } = client;

  await enableRuntime(client);

  const notSerializableNumbers = {
    number: ["-0", "NaN", "Infinity", "-Infinity"],
    bigint: ["42n"],
  };

  for (const type in notSerializableNumbers) {
    for (const unserializableValue of notSerializableNumbers[type]) {
      const { result } = await Runtime.evaluate({
        expression: `(${unserializableValue})`,
        returnByValue: true,
      });

      Assert.deepEqual(
        result,
        {
          type,
          unserializableValue,
          description: unserializableValue,
        },
        `Returned expected value for ${JSON.stringify(unserializableValue)}`
      );
    }
  }
});

// Test undefined individually as JSON.stringify doesn't return a string
add_task(async function returnByValueUndefined({ client }) {
  const { Runtime } = client;

  await enableRuntime(client);

  const { result } = await Runtime.evaluate({
    expression: "undefined",
    returnByValue: true,
  });

  Assert.deepEqual(
    result,
    {
      type: "undefined",
    },
    "Undefined type is correct"
  );
});