summaryrefslogtreecommitdiffstats
path: root/dom/events/test/test_bug1096146.html
blob: 85b7a35f0c440cb361a11774b89210d868670b02 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1096146
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 1096146</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body onload="runTests();">
<a target="_blank"
   href="https://bugzilla.mozilla.org/show_bug.cgi?id=1096146">Mozilla Bug 1096146</a>
<div id="content" style="display: none;">
</div>
<pre id="test">
<script type="application/javascript">

const kKeydownEvent = 0x1;
const kScrollEvent  = 0x2;

var gCurrentTest = 0;
var gNumEvents = 0;
var kTests = [
  {
    description: "no preventDefault at 'mozbrowserbeforekeydown'",
    expectedEvents: kKeydownEvent | kScrollEvent,
    resultEvents: 0x0,
    doPreventDefault: false
  },
  {
    description: "do preventDefault at 'mozbrowserbeforekeydown'",
    expectedEvents: 0x0,
    resultEvents: 0x0,
    doPreventDefault: true
  }
]

function frameScript()
{
  function handler(e) {
    sendSyncMessage("forwardevent", { type: e.type });
  }
  addEventListener('keydown', handler);
  addEventListener('scroll', handler);
}


function waitAndVerifyResult(count) {
  if (gNumEvents >= 3 || count > 10) {
    is(kTests[gCurrentTest].resultEvents,
       kTests[gCurrentTest].expectedEvents,
       "verify result");
    runTests();
	} else {
    SimpleTest.requestFlakyTimeout("We must delay to wait for scroll/keydown events.");
    setTimeout(() => waitAndVerifyResult(count + 1), 100);
  }
}

function testDefaultAction()
{
  synthesizeKey('VK_END', {}, document.getElementById("embedded").contentWindow);
  waitAndVerifyResult(0);
}

function prepareTest()
{
  gNumEvents = 0;

  var handler;
  if (kTests[gCurrentTest].doPreventDefault) {
    handler = preventDefaultHandler;
  } else {
    handler = noPreventDefaultHandler;
  }
  window.addEventListener("mozbrowserbeforekeydown", handler);

  var iframe = document.createElement("iframe");
  iframe.id = "embedded";
  iframe.src = "bug1096146_embedded.html";
  iframe.setAttribute("remote", "false");
  SpecialPowers.wrap(iframe).mozbrowser = true;

  iframe.addEventListener("mozbrowserloadend", function onloadend() {
    iframe.removeEventListener("mozbrowserloadend", onloadend);
    iframe.focus();
    var mm = SpecialPowers.getBrowserFrameMessageManager(iframe);
    mm.addMessageListener("forwardevent", function(msg) {
      var value = 0;
      switch(msg.json.type) {
        case "scroll":
          ++gNumEvents;
          value = kScrollEvent;
          break;
        case "keydown":
          ++gNumEvents;
          value = kKeydownEvent;
          break;
        default:
          ok(false, "unexpected event");
      }

      kTests[gCurrentTest].resultEvents =
        kTests[gCurrentTest].resultEvents | value;
    });
    mm.loadFrameScript("data:,(" + frameScript.toString() + ")();", false);
    runTests();
    return;
  });

  document.body.appendChild(iframe);
}

function preventDefaultHandler(evt)
{
  ok(true, "receive " + evt.type + " and do preventDefault.");
	++gNumEvents;
  evt.preventDefault();
}

function noPreventDefaultHandler(evt)
{
  ok(true, "receive " + evt.type + ".");
	++gNumEvents;
}

function teardownHandler()
{
  var handler;
  if (kTests[gCurrentTest].doPreventDefault) {
    handler = preventDefaultHandler;
  } else {
    handler = noPreventDefaultHandler;
  }
  window.removeEventListener("mozbrowserbeforekeydown", handler);
  document.body.removeChild(document.getElementById("embedded"));

  runTests();
}

var tests = [
  function addPermissions() {
    SpecialPowers.pushPermissions(
      [{ type: "before-after-keyboard-event", allow: true, context: document },
       { type: "browser", allow: true, context: document }],
      runTests);
  },
  function addPreferences() {
    SpecialPowers.pushPrefEnv(
      { "set": [["dom.beforeAfterKeyboardEvent.enabled", true],
                ["dom.mozBrowserFramesEnabled", true],
                ["dom.ipc.tabs.disabled", false],
                ["network.disable.ipc.security", true]] },
      runTests);
  },

  prepareTest,
  testDefaultAction,
  teardownHandler,

  function() {
    gCurrentTest++;
    runTests();
  },
  prepareTest,
  testDefaultAction,
  teardownHandler
];

function runTests()
{
  if (!tests.length) {
    SimpleTest.finish();
    return;
  }

  var test = tests.shift();
  test();
}

</script>
</pre>
</body>
</html>