summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/bugs/test_errorReporting.html
blob: aa299c98ccc390d7dd56227bf96c8bccabfdaed3 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1070842
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 1070842</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="application/javascript">

  /** Test for error reporting behavior. **/
  SimpleTest.waitForExplicitFinish();

  function testErrorReportingHelper(f, rgxp, preventDefault) {
    return new Promise(function(resolve, reject) {
      window.addEventListener('error', function(e) {
        ok(rgxp.test(e.message), "Should get message matching " + rgxp + ". Got: " + e.message);
        var expectedMessages;
        if (preventDefault) {
          e.preventDefault();
          expectedMessages = [];
        } else {
          expectedMessages = [{message: rgxp}];
        }
        SimpleTest.monitorConsole(resolve, expectedMessages, /* forbidUnexpectedMsgs = */ true);
        setTimeout(SimpleTest.endMonitorConsole.bind(SimpleTest), 0);
      }, {once: true});

      // Notify the test harness to avoid treating the next exception as a test failure.
      SimpleTest.expectUncaughtException();

      // Invoke the function async so that the exception doesn't get eaten by
      // the Promise machinery.
      setTimeout(f, 0);
    });
  }
  function testErrorReporting(f, rgxp) {
    return new Promise(function(resolve, reject) {
        testErrorReportingHelper.bind(null, f, rgxp, false)().then(
        testErrorReportingHelper.bind(null, f, rgxp, true)).then(
        resolve);
    });
  }

  function go() {
    var otherWin = $('emptyFrame').contentWindow;
    var clickMe = $('clickMe');
    testErrorReporting.bind(null, () => { throw Error("Simple Error") }, /Simple Error/)().then(
    testErrorReporting.bind(null, () => otherWin.eval('throw Error("Cross Global Error")'), /Cross Global Error/)).then(
    testErrorReporting.bind(null, () => clickMe.dispatchEvent(new MouseEvent('click')), /thrwan/)).then(
    testErrorReporting.bind(null, () => { clickMe.setAttribute('onclick', ' '); /* Make sure we recompile. */
                                          clickMe.setAttribute('onclick', '?');
                                          clickMe.onclick; }, /SyntaxError/)).then(
    SimpleTest.finish.bind(SimpleTest));
  }

  </script>
</head>
<body onload="go();">
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1070842">Mozilla Bug 1070842</a>
<p id="display"></p>
<div id="content" style="display: none">
<button id="clickMe" onclick="thrwan.error;">Click Me</button>
<iframe id="emptyFrame"></iframe>

</div>
<pre id="test">
</pre>
</body>
</html>