summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/test/mochitest/browser_dbg_break-on-dom-event-01.js
blob: a066b7d6bc0835403a4a41e5c3efc5279cacc0cc (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Tests that the break-on-dom-events request works.
 */

const TAB_URL = EXAMPLE_URL + "doc_event-listeners-01.html";

var gClient, gThreadClient, gInput, gButton;

function test() {
  if (!DebuggerServer.initialized) {
    DebuggerServer.init();
    DebuggerServer.addBrowserActors();
  }

  let transport = DebuggerServer.connectPipe();
  gClient = new DebuggerClient(transport);
  gClient.connect().then(([aType, aTraits]) => {
    is(aType, "browser",
      "Root actor should identify itself as a browser.");

    addTab(TAB_URL)
      .then(() => attachThreadActorForUrl(gClient, TAB_URL))
      .then(setupGlobals)
      .then(pauseDebuggee)
      .then(testBreakOnAll)
      .then(testBreakOnDisabled)
      .then(testBreakOnNone)
      .then(testBreakOnClick)
      .then(() => gClient.close())
      .then(finish)
      .then(null, aError => {
        ok(false, "Got an error: " + aError.message + "\n" + aError.stack);
      });
  });
}

function setupGlobals(aThreadClient) {
  gThreadClient = aThreadClient;
  gInput = content.document.querySelector("input");
  gButton = content.document.querySelector("button");
}

function pauseDebuggee() {
  let deferred = promise.defer();

  gClient.addOneTimeListener("paused", (aEvent, aPacket) => {
    is(aPacket.type, "paused",
      "We should now be paused.");
    is(aPacket.why.type, "debuggerStatement",
      "The debugger statement was hit.");

    deferred.resolve();
  });

  // Spin the event loop before causing the debuggee to pause, to allow
  // this function to return first.
  executeSoon(triggerButtonClick);

  return deferred.promise;
}

// Test pause on all events.
function testBreakOnAll() {
  let deferred = promise.defer();

  // Test calling pauseOnDOMEvents from a paused state.
  gThreadClient.pauseOnDOMEvents("*", (aPacket) => {
    is(aPacket.error, undefined,
      "The pause-on-any-event request completed successfully.");

    gClient.addOneTimeListener("paused", (aEvent, aPacket) => {
      is(aPacket.why.type, "pauseOnDOMEvents",
        "A hidden breakpoint was hit.");
      is(aPacket.frame.callee.name, "keyupHandler",
        "The keyupHandler is entered.");

      gClient.addOneTimeListener("paused", (aEvent, aPacket) => {
        is(aPacket.why.type, "pauseOnDOMEvents",
          "A hidden breakpoint was hit.");
        is(aPacket.frame.callee.name, "clickHandler",
          "The clickHandler is entered.");

        gClient.addOneTimeListener("paused", (aEvent, aPacket) => {
          is(aPacket.why.type, "pauseOnDOMEvents",
            "A hidden breakpoint was hit.");
          is(aPacket.frame.callee.name, "onchange",
            "The onchange handler is entered.");

          gThreadClient.resume(deferred.resolve);
        });

        gThreadClient.resume(triggerInputChange);
      });

      gThreadClient.resume(triggerButtonClick);
    });

    gThreadClient.resume(triggerInputKeyup);
  });

  return deferred.promise;
}

// Test that removing events from the array disables them.
function testBreakOnDisabled() {
  let deferred = promise.defer();

  // Test calling pauseOnDOMEvents from a running state.
  gThreadClient.pauseOnDOMEvents(["click"], (aPacket) => {
    is(aPacket.error, undefined,
      "The pause-on-click-only request completed successfully.");

    gClient.addListener("paused", unexpectedListener);

    // This non-capturing event listener is guaranteed to run after the page's
    // capturing one had a chance to execute and modify window.foobar.
    once(gInput, "keyup").then(() => {
      is(content.wrappedJSObject.foobar, "keyupHandler",
        "No hidden breakpoint was hit.");

      gClient.removeListener("paused", unexpectedListener);
      deferred.resolve();
    });

    triggerInputKeyup();
  });

  return deferred.promise;
}

// Test that specifying an empty event array clears all hidden breakpoints.
function testBreakOnNone() {
  let deferred = promise.defer();

  // Test calling pauseOnDOMEvents from a running state.
  gThreadClient.pauseOnDOMEvents([], (aPacket) => {
    is(aPacket.error, undefined,
      "The pause-on-none request completed successfully.");

    gClient.addListener("paused", unexpectedListener);

    // This non-capturing event listener is guaranteed to run after the page's
    // capturing one had a chance to execute and modify window.foobar.
    once(gInput, "keyup").then(() => {
      is(content.wrappedJSObject.foobar, "keyupHandler",
        "No hidden breakpoint was hit.");

      gClient.removeListener("paused", unexpectedListener);
      deferred.resolve();
    });

    triggerInputKeyup();
  });

  return deferred.promise;
}

// Test pause on a single event.
function testBreakOnClick() {
  let deferred = promise.defer();

  // Test calling pauseOnDOMEvents from a running state.
  gThreadClient.pauseOnDOMEvents(["click"], (aPacket) => {
    is(aPacket.error, undefined,
      "The pause-on-click request completed successfully.");

    gClient.addOneTimeListener("paused", (aEvent, aPacket) => {
      is(aPacket.why.type, "pauseOnDOMEvents",
        "A hidden breakpoint was hit.");
      is(aPacket.frame.callee.name, "clickHandler",
        "The clickHandler is entered.");

      gThreadClient.resume(deferred.resolve);
    });

    triggerButtonClick();
  });

  return deferred.promise;
}

function unexpectedListener() {
  gClient.removeListener("paused", unexpectedListener);
  ok(false, "An unexpected hidden breakpoint was hit.");
  gThreadClient.resume(testBreakOnClick);
}

function triggerInputKeyup() {
  // Make sure that the focus is not on the input box so that a focus event
  // will be triggered.
  window.focus();
  gBrowser.selectedBrowser.focus();
  gButton.focus();

  // Focus the element and wait for focus event.
  once(gInput, "focus").then(() => {
    executeSoon(() => {
      EventUtils.synthesizeKey("e", { shiftKey: 1 }, content);
    });
  });

  gInput.focus();
}

function triggerButtonClick() {
  EventUtils.sendMouseEvent({ type: "click" }, gButton);
}

function triggerInputChange() {
  gInput.focus();
  gInput.value = "foo";
  gInput.blur();
}

registerCleanupFunction(function () {
  gClient = null;
  gThreadClient = null;
  gInput = null;
  gButton = null;
});