summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser/browser_webconsole_lenient_this_warning.js
blob: 1d8914b8452599316d7bd3e5a22964fcfd7dba88 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Check that calling the LenientThis warning is only called when expected.
const TEST_URI = `data:text/html;charset=utf8,<!DOCTYPE html>${encodeURI(`
    <h1>LenientThis warning</h1>
    <script>
      const el = document.createElement('div');
      globalThis.htmlDivElementProto = Object.getPrototypeOf(el);
      function triggerLenientThisWarning(){
        Object.getOwnPropertyDescriptor(
          Object.getPrototypeOf(globalThis.htmlDivElementProto),
          'onmouseenter'
        ).get.call()
      }
    </script>`)}`;

add_task(async function () {
  const hud = await openNewTabAndConsole(TEST_URI);

  const expectedWarningMessageText =
    "Ignoring get or set of property that has [LenientThis] ";

  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    const global = content.wrappedJSObject;
    global.console.log(global.htmlDivElementProto);
  });

  info("Wait for a bit so any warning message could be displayed");
  await wait(1000);
  await waitFor(() => findConsoleAPIMessage(hud, "HTMLDivElementPrototype"));

  ok(
    !findWarningMessage(hud, expectedWarningMessageText, ".warn"),
    "Displaying the HTMLDivElementPrototype does not trigger the LenientThis warning"
  );

  info(
    "Call a LenientThis getter with the wrong `this` to trigger a warning message"
  );
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    content.wrappedJSObject.triggerLenientThisWarning();
  });

  await waitFor(() =>
    findWarningMessage(hud, expectedWarningMessageText, ".warn")
  );
  ok(
    true,
    "Calling the LenientThis getter with an unexpected `this` did triggered the warning"
  );

  info(
    "Clear the console and call the LenientThis getter with an unexpected `this` again"
  );
  await clearOutput(hud);
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], function () {
    content.wrappedJSObject.triggerLenientThisWarning();
  });
  info("Wait for a bit so any warning message could be displayed");
  await wait(1000);
  ok(
    !findWarningMessage(hud, expectedWarningMessageText, ".warn"),
    "Calling the LenientThis getter a second time did not trigger the warning again"
  );
});