forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhead.js
More file actions
1304 lines (1128 loc) · 34.3 KB
/
Copy pathhead.js
File metadata and controls
1304 lines (1128 loc) · 34.3 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- 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/ */
/**
* The Mochitest API documentation
* @module mochitest
*/
/**
* The mochitest API to wait for certain events.
* @module mochitest/waits
* @parent mochitest
*/
/**
* The mochitest API predefined asserts.
* @module mochitest/asserts
* @parent mochitest
*/
/**
* The mochitest API for interacting with the debugger.
* @module mochitest/actions
* @parent mochitest
*/
/**
* Helper methods for the mochitest API.
* @module mochitest/helpers
* @parent mochitest
*/
// shared-head.js handles imports, constants, and utility functions
Services.scriptloader.loadSubScript(
"chrome://mochitests/content/browser/devtools/client/shared/test/shared-head.js",
this
);
var { Toolbox } = require("devtools/client/framework/toolbox");
var { Task } = require("devtools/shared/task");
const sourceUtils = {
isLoaded: source => source.get("loadedState") === "loaded"
};
const EXAMPLE_URL =
"http://example.com/browser/devtools/client/debugger/new/test/mochitest/examples/";
Services.prefs.setBoolPref("devtools.debugger.new-debugger-frontend", true);
registerCleanupFunction(() => {
Services.prefs.clearUserPref("devtools.debugger.new-debugger-frontend");
delete window.resumeTest;
});
function log(msg, data) {
info(`${msg} ${!data ? "" : JSON.stringify(data)}`);
}
function logThreadEvents(dbg, event) {
const thread = dbg.toolbox.threadClient;
thread.addListener(event, function onEvent(eventName, ...args) {
info(`Thread event '${eventName}' fired.`);
});
}
async function waitFor(condition) {
await BrowserTestUtils.waitForCondition(condition, "waitFor", 10, 500);
return condition();
}
// Wait until an action of `type` is dispatched. This is different
// then `_afterDispatchDone` because it doesn't wait for async actions
// to be done/errored. Use this if you want to listen for the "start"
// action of an async operation (somewhat rare).
function waitForNextDispatch(store, type) {
return new Promise(resolve => {
store.dispatch({
// Normally we would use `services.WAIT_UNTIL`, but use the
// internal name here so tests aren't forced to always pass it
// in
type: "@@service/waitUntil",
predicate: action => action.type === type,
run: (dispatch, getState, action) => {
resolve(action);
}
});
});
}
// Wait until an action of `type` is dispatched. If it's part of an
// async operation, wait until the `status` field is "done" or "error"
function _afterDispatchDone(store, type) {
return new Promise(resolve => {
store.dispatch({
// Normally we would use `services.WAIT_UNTIL`, but use the
// internal name here so tests aren't forced to always pass it
// in
type: "@@service/waitUntil",
predicate: action => {
if (action.type === type) {
return action.status
? action.status === "done" || action.status === "error"
: true;
}
},
run: (dispatch, getState, action) => {
resolve(action);
}
});
});
}
/**
* Wait for a specific action type to be dispatch.
* If an async action, will wait for it to be done.
*
* @memberof mochitest/waits
* @param {Object} dbg
* @param {String} type
* @param {Number} eventRepeat
* @return {Promise}
* @static
*/
function waitForDispatch(dbg, type, eventRepeat = 1) {
let count = 0;
return Task.spawn(function*() {
info(`Waiting for ${type} to dispatch ${eventRepeat} time(s)`);
while (count < eventRepeat) {
yield _afterDispatchDone(dbg.store, type);
count++;
info(`${type} dispatched ${count} time(s)`);
}
});
}
/**
* Waits for specific thread events.
*
* @memberof mochitest/waits
* @param {Object} dbg
* @param {String} eventName
* @return {Promise}
* @static
*/
function waitForThreadEvents(dbg, eventName) {
info(`Waiting for thread event '${eventName}' to fire.`);
const thread = dbg.toolbox.threadClient;
return new Promise(function(resolve, reject) {
thread.addListener(eventName, function onEvent(eventName, ...args) {
info(`Thread event '${eventName}' fired.`);
thread.removeListener(eventName, onEvent);
resolve.apply(resolve, args);
});
});
}
/**
* Waits for `predicate(state)` to be true. `state` is the redux app state.
*
* @memberof mochitest/waits
* @param {Object} dbg
* @param {Function} predicate
* @return {Promise}
* @static
*/
function waitForState(dbg, predicate, msg) {
return new Promise(resolve => {
info(`Waiting for state change: ${msg || ""}`);
if (predicate(dbg.store.getState())) {
info(`Finished waiting for state change: ${msg || ""}`);
return resolve();
}
const unsubscribe = dbg.store.subscribe(() => {
const result = predicate(dbg.store.getState());
if (result) {
info(`Finished waiting for state change: ${msg || ""}`);
unsubscribe();
resolve(result);
}
});
});
}
/**
* Waits for sources to be loaded.
*
* @memberof mochitest/waits
* @param {Object} dbg
* @param {Array} sources
* @return {Promise}
* @static
*/
async function waitForSources(dbg, ...sources) {
const { selectors: { getSources }, store } = dbg;
if (sources.length === 0) {
return Promise.resolve();
}
info(`Waiting on sources: ${sources.join(", ")}`);
await Promise.all(
sources.map(url => {
if (!sourceExists(dbg, url)) {
return waitForState(
dbg,
() => sourceExists(dbg, url),
`source ${url} exists`
);
}
})
);
info(`Finished waiting on sources: ${sources.join(", ")}`);
}
/**
* Waits for a source to be loaded.
*
* @memberof mochitest/waits
* @param {Object} dbg
* @param {String} source
* @return {Promise}
* @static
*/
function waitForSource(dbg, url) {
return waitForState(
dbg,
state => {
const sources = dbg.selectors.getSources(state);
return sources.find(s => (s.get("url") || "").includes(url));
},
`source exists`
);
}
async function waitForElement(dbg, name) {
await waitUntil(() => findElement(dbg, name));
return findElement(dbg, name);
}
async function waitForElementWithSelector(dbg, selector) {
await waitUntil(() => findElementWithSelector(dbg, selector));
return findElementWithSelector(dbg, selector);
}
function waitForSelectedSource(dbg, url) {
return waitForState(
dbg,
state => {
const source = dbg.selectors.getSelectedSource(state);
const isLoaded = source && sourceUtils.isLoaded(source);
if (!isLoaded) {
return false;
}
if (!url) {
return true;
}
const newSource = findSource(dbg, url, { silent: true });
if (newSource.id != source.id) {
return false;
}
// wait for async work to be done
const hasSymbols = dbg.selectors.hasSymbols(state, source);
const hasSourceMetaData = dbg.selectors.hasSourceMetaData(
state,
source.id
);
const hasPausePoints = dbg.selectors.hasPausePoints(state, source.id);
return hasSymbols && hasSourceMetaData && hasPausePoints;
},
"selected source"
);
}
/**
* Assert that the debugger is not currently paused.
* @memberof mochitest/asserts
* @static
*/
function assertNotPaused(dbg) {
ok(!isPaused(dbg), "client is not paused");
}
function getVisibleSelectedFrameLine(dbg) {
const { selectors: { getVisibleSelectedFrame }, getState } = dbg;
const frame = getVisibleSelectedFrame(getState());
return frame && frame.location.line;
}
/**
* Assert that the debugger is paused at the correct location.
*
* @memberof mochitest/asserts
* @param {Object} dbg
* @param {String} source
* @param {Number} line
* @static
*/
function assertPausedLocation(dbg) {
const { selectors: { getSelectedSource }, getState } = dbg;
ok(
isSelectedFrameSelected(dbg, getState()),
"top frame's source is selected"
);
// Check the pause location
const pauseLine = getVisibleSelectedFrameLine(dbg);
assertDebugLine(dbg, pauseLine);
ok(isVisibleInEditor(dbg, getCM(dbg).display.gutters), "gutter is visible");
}
function assertDebugLine(dbg, line) {
// Check the debug line
const lineInfo = getCM(dbg).lineInfo(line - 1);
const source = dbg.selectors.getSelectedSource(dbg.getState());
if (source && source.loadedState == "loading") {
const url = source.url;
ok(
false,
`Looks like the source ${url} is still loading. Try adding waitForLoadedSource in the test.`
);
return;
}
if (!lineInfo.wrapClass) {
const pauseLine = getVisibleSelectedFrameLine(dbg);
ok(false, `Expected pause line on line ${line}, it is on ${pauseLine}`);
return;
}
ok(
lineInfo.wrapClass.includes("new-debug-line"),
"Line is highlighted as paused"
);
const debugLine =
findElement(dbg, "debugLine") || findElement(dbg, "debugErrorLine");
is(
findAllElements(dbg, "debugLine").length +
findAllElements(dbg, "debugErrorLine").length,
1,
"There is only one line"
);
ok(isVisibleInEditor(dbg, debugLine), "debug line is visible");
const markedSpans = lineInfo.handle.markedSpans;
if (markedSpans && markedSpans.length > 0) {
const marker = markedSpans[0].marker;
ok(
marker.className.includes("debug-expression"),
"expression is highlighted as paused"
);
}
}
/**
* Assert that the debugger is highlighting the correct location.
*
* @memberof mochitest/asserts
* @param {Object} dbg
* @param {String} source
* @param {Number} line
* @static
*/
function assertHighlightLocation(dbg, source, line) {
const { selectors: { getSelectedSource }, getState } = dbg;
source = findSource(dbg, source);
// Check the selected source
is(
getSelectedSource(getState()).get("url"),
source.url,
"source url is correct"
);
// Check the highlight line
const lineEl = findElement(dbg, "highlightLine");
ok(lineEl, "Line is highlighted");
is(
findAllElements(dbg, "highlightLine").length,
1,
"Only 1 line is highlighted"
);
ok(isVisibleInEditor(dbg, lineEl), "Highlighted line is visible");
ok(
getCM(dbg)
.lineInfo(line - 1)
.wrapClass.includes("highlight-line"),
"Line is highlighted"
);
}
/**
* Returns boolean for whether the debugger is paused.
*
* @memberof mochitest/asserts
* @param {Object} dbg
* @static
*/
function isPaused(dbg) {
const { selectors: { isPaused }, getState } = dbg;
return !!isPaused(getState());
}
async function waitForLoadedScopes(dbg) {
const scopes = await waitForElement(dbg, "scopes");
// Since scopes auto-expand, we can assume they are loaded when there is a tree node
// with the aria-level attribute equal to "2".
await waitUntil(() => scopes.querySelector(`.tree-node[aria-level="2"]`));
}
/**
* Waits for the debugger to be fully paused.
*
* @memberof mochitest/waits
* @param {Object} dbg
* @static
*/
async function waitForPaused(dbg, url) {
const { getSelectedScope } = dbg.selectors;
await waitForState(
dbg,
state => isPaused(dbg) && !!getSelectedScope(state),
"paused"
);
await waitForLoadedScopes(dbg);
await waitForSelectedSource(dbg, url);
}
/*
* useful for when you want to see what is happening
* e.g await waitForever()
*/
function waitForever() {
return new Promise(r => {});
}
/*
* useful for waiting for a short amount of time as
* a placeholder for a better waitForX handler.
*
* e.g await waitForTime(500)
*/
function waitForTime(ms) {
return new Promise(r => setTimeout(r, ms));
}
function isSelectedFrameSelected(dbg, state) {
const frame = dbg.selectors.getVisibleSelectedFrame(state);
// Make sure the source text is completely loaded for the
// source we are paused in.
const sourceId = frame.location.sourceId;
const source = dbg.selectors.getSelectedSource(state);
if (!source) {
return false;
}
const isLoaded = source.has("loadedState") && sourceUtils.isLoaded(source);
if (!isLoaded) {
return false;
}
return source.id == sourceId;
}
function createDebuggerContext(toolbox) {
const panel = toolbox.getPanel("jsdebugger");
const win = panel.panelWin;
const { store, client, selectors, actions } = panel.getVarsForTests();
return {
actions: actions,
selectors: selectors,
getState: store.getState,
store: store,
client: client,
toolbox: toolbox,
win: win,
panel: panel
};
}
/**
* Clear all the debugger related preferences.
*/
function clearDebuggerPreferences() {
Services.prefs.clearUserPref("devtools.debugger.pause-on-exceptions");
Services.prefs.clearUserPref("devtools.debugger.pause-on-caught-exceptions");
Services.prefs.clearUserPref("devtools.debugger.tabs");
Services.prefs.clearUserPref("devtools.debugger.pending-selected-location");
Services.prefs.clearUserPref("devtools.debugger.pending-breakpoints");
Services.prefs.clearUserPref("devtools.debugger.expressions");
Services.prefs.clearUserPref("devtools.debugger.call-stack-visible");
Services.prefs.clearUserPref("devtools.debugger.scopes-visible");
}
/**
* Intilializes the debugger.
*
* @memberof mochitest
* @param {String} url
* @return {Promise} dbg
* @static
*/
async function initDebugger(url) {
clearDebuggerPreferences();
const toolbox = await openNewTabAndToolbox(EXAMPLE_URL + url, "jsdebugger");
return createDebuggerContext(toolbox);
}
async function initPane(url, pane) {
clearDebuggerPreferences();
return openNewTabAndToolbox(EXAMPLE_URL + url, pane);
}
window.resumeTest = undefined;
/**
* Pause the test and let you interact with the debugger.
* The test can be resumed by invoking `resumeTest` in the console.
*
* @memberof mochitest
* @static
*/
function pauseTest() {
info("Test paused. Invoke resumeTest to continue.");
return new Promise(resolve => (resumeTest = resolve));
}
// Actions
/**
* Returns a source that matches the URL.
*
* @memberof mochitest/actions
* @param {Object} dbg
* @param {String} url
* @return {Object} source
* @static
*/
function findSource(dbg, url, { silent } = { silent: false }) {
if (typeof url !== "string") {
// Support passing in a source object itelf all APIs that use this
// function support both styles
const source = url;
return source;
}
const sources = dbg.selectors.getSources(dbg.getState());
const source = sources.find(s => (s.get("url") || "").includes(url));
if (!source) {
if (silent) {
return false;
}
throw new Error(`Unable to find source: ${url}`);
}
return source.toJS();
}
function sourceExists(dbg, url) {
return !!findSource(dbg, url, { silent: true });
}
function waitForLoadedSource(dbg, url) {
return waitForState(
dbg,
state => findSource(dbg, url, { silent: true }).loadedState == "loaded",
"loaded source"
);
}
function waitForLoadedSources(dbg) {
return waitForState(
dbg,
state => {
const sources = dbg.selectors
.getSources(state)
.valueSeq()
.toJS();
return !sources.some(source => source.loadedState == "loading");
},
"loaded source"
);
}
/**
* Selects the source.
*
* @memberof mochitest/actions
* @param {Object} dbg
* @param {String} url
* @param {Number} line
* @return {Promise}
* @static
*/
async function selectSource(dbg, url, line) {
const source = findSource(dbg, url);
await dbg.actions.selectLocation({ sourceId: source.id, line });
return waitForSelectedSource(dbg, url);
}
function closeTab(dbg, url) {
const source = findSource(dbg, url);
return dbg.actions.closeTab(source.url);
}
/**
* Steps over.
*
* @memberof mochitest/actions
* @param {Object} dbg
* @return {Promise}
* @static
*/
async function stepOver(dbg) {
const pauseLine = getVisibleSelectedFrameLine(dbg);
info(`Stepping over from ${pauseLine}`);
await dbg.actions.stepOver();
return waitForPaused(dbg);
}
/**
* Steps in.
*
* @memberof mochitest/actions
* @param {Object} dbg
* @return {Promise}
* @static
*/
async function stepIn(dbg) {
const pauseLine = getVisibleSelectedFrameLine(dbg);
info(`Stepping in from ${pauseLine}`);
await dbg.actions.stepIn();
return waitForPaused(dbg);
}
/**
* Steps out.
*
* @memberof mochitest/actions
* @param {Object} dbg
* @return {Promise}
* @static
*/
async function stepOut(dbg) {
const pauseLine = getVisibleSelectedFrameLine(dbg);
info(`Stepping out from ${pauseLine}`);
await dbg.actions.stepOut();
return waitForPaused(dbg);
}
/**
* Resumes.
*
* @memberof mochitest/actions
* @param {Object} dbg
* @return {Promise}
* @static
*/
function resume(dbg) {
const pauseLine = getVisibleSelectedFrameLine(dbg);
info(`Resuming from ${pauseLine}`);
return dbg.actions.resume();
}
function deleteExpression(dbg, input) {
info(`Delete expression "${input}"`);
return dbg.actions.deleteExpression({ input });
}
/**
* Reloads the debuggee.
*
* @memberof mochitest/actions
* @param {Object} dbg
* @param {Array} sources
* @return {Promise}
* @static
*/
async function reload(dbg, ...sources) {
const navigated = waitForDispatch(dbg, "NAVIGATE");
await dbg.client.reload();
await navigated;
return waitForSources(dbg, ...sources);
}
/**
* Navigates the debuggee to another url.
*
* @memberof mochitest/actions
* @param {Object} dbg
* @param {String} url
* @param {Array} sources
* @return {Promise}
* @static
*/
async function navigate(dbg, url, ...sources) {
info(`Navigating to ${url}`)
const navigated = waitForDispatch(dbg, "NAVIGATE");
await dbg.client.navigate(url);
await navigated;
return waitForSources(dbg, ...sources);
}
/**
* Adds a breakpoint to a source at line/col.
*
* @memberof mochitest/actions
* @param {Object} dbg
* @param {String} source
* @param {Number} line
* @param {Number} col
* @return {Promise}
* @static
*/
function addBreakpoint(dbg, source, line, column) {
source = findSource(dbg, source);
const sourceId = source.id;
dbg.actions.addBreakpoint({ sourceId, line, column });
return waitForDispatch(dbg, "ADD_BREAKPOINT");
}
function disableBreakpoint(dbg, source, line, column) {
source = findSource(dbg, source);
const sourceId = source.id;
dbg.actions.disableBreakpoint({ sourceId, line, column });
return waitForDispatch(dbg, "DISABLE_BREAKPOINT");
}
async function loadAndAddBreakpoint(dbg, filename, line, column) {
const { selectors: { getBreakpoint, getBreakpoints }, getState } = dbg;
await waitForSources(dbg, filename);
ok(true, "Original sources exist");
const source = findSource(dbg, filename);
await selectSource(dbg, source);
// Test that breakpoint is not off by a line.
await addBreakpoint(dbg, source, line);
is(getBreakpoints(getState()).size, 1, "One breakpoint exists");
ok(
getBreakpoint(getState(), { sourceId: source.id, line, column }),
"Breakpoint has correct line"
);
return source;
}
async function invokeWithBreakpoint(dbg, fnName, filename, { line, column }, handler) {
const { selectors: { getBreakpoints }, getState } = dbg;
const source = await loadAndAddBreakpoint(dbg, filename, line, column);
const invokeResult = invokeInTab(fnName);
let invokeFailed = await Promise.race([
waitForPaused(dbg),
invokeResult.then(() => new Promise(() => {}), () => true)
]);
if (invokeFailed) {
return invokeResult;
}
assertPausedLocation(dbg);
await removeBreakpoint(dbg, source.id, line, column);
is(getBreakpoints(getState()).size, 0, "Breakpoint reverted");
await handler(source);
await resume(dbg);
// If the invoke errored later somehow, capture here so the error is reported nicely.
await invokeResult;
}
async function expandAllScopes(dbg) {
const scopes = await waitForElement(dbg, "scopes");
const scopeElements = scopes.querySelectorAll(
`.tree-node[aria-level="1"][data-expandable="true"]:not([aria-expanded="true"])`
);
const indices = Array.from(scopeElements, el => {
return Array.prototype.indexOf.call(el.parentNode.childNodes, el);
}).reverse();
for (const index of indices) {
await toggleScopeNode(dbg, index + 1);
}
}
async function assertScopes(dbg, items) {
await expandAllScopes(dbg);
for (const [i, val] of items.entries()) {
if (Array.isArray(val)) {
is(getScopeLabel(dbg, i + 1), val[0]);
is(
getScopeValue(dbg, i + 1),
val[1],
`"${val[0]}" has the expected "${val[1]}" value`
);
} else {
is(getScopeLabel(dbg, i + 1), val);
}
}
is(getScopeLabel(dbg, items.length + 1), "Window");
}
/**
* Removes a breakpoint from a source at line/col.
*
* @memberof mochitest/actions
* @param {Object} dbg
* @param {String} source
* @param {Number} line
* @param {Number} col
* @return {Promise}
* @static
*/
function removeBreakpoint(dbg, sourceId, line, column) {
dbg.actions.removeBreakpoint({ sourceId, line, column });
return waitForDispatch(dbg, "REMOVE_BREAKPOINT");
}
/**
* Toggles the Pause on exceptions feature in the debugger.
*
* @memberof mochitest/actions
* @param {Object} dbg
* @param {Boolean} pauseOnExceptions
* @param {Boolean} ignoreCaughtExceptions
* @return {Promise}
* @static
*/
async function togglePauseOnExceptions(
dbg,
pauseOnExceptions,
ignoreCaughtExceptions
) {
const command = dbg.actions.pauseOnExceptions(
pauseOnExceptions,
ignoreCaughtExceptions
);
if (!isPaused(dbg)) {
await waitForThreadEvents(dbg, "resumed");
}
return command;
}
function waitForActive(dbg) {
return waitForState(dbg, state => !dbg.selectors.isPaused(state), "active");
}
// Helpers
/**
* Invokes a global function in the debuggee tab.
*
* @memberof mochitest/helpers
* @param {String} fnc The name of a global function on the content window to
* call. This is applied to structured clones of the
* remaining arguments to invokeInTab.
* @param {Any} ...args Remaining args to serialize and pass to fnc.
* @return {Promise}
* @static
*/
function invokeInTab(fnc, ...args) {
info(`Invoking in tab: ${fnc}(${args.map(uneval).join(",")})`);
return ContentTask.spawn(gBrowser.selectedBrowser, { fnc, args }, function*({
fnc,
args
}) {
content.wrappedJSObject[fnc](...args); // eslint-disable-line mozilla/no-cpows-in-tests, max-len
});
}
const isLinux = Services.appinfo.OS === "Linux";
const isMac = Services.appinfo.OS === "Darwin";
const cmdOrCtrl = isLinux ? { ctrlKey: true } : { metaKey: true };
const shiftOrAlt = isMac
? { accelKey: true, shiftKey: true }
: { accelKey: true, altKey: true };
const cmdShift = isMac
? { accelKey: true, shiftKey: true, metaKey: true }
: { accelKey: true, shiftKey: true, ctrlKey: true };
// On Mac, going to beginning/end only works with meta+left/right. On
// Windows, it only works with home/end. On Linux, apparently, either
// ctrl+left/right or home/end work.
const endKey = isMac
? { code: "VK_RIGHT", modifiers: cmdOrCtrl }
: { code: "VK_END" };
const startKey = isMac
? { code: "VK_LEFT", modifiers: cmdOrCtrl }
: { code: "VK_HOME" };
const keyMappings = {
debugger: { code: "s", modifiers: shiftOrAlt },
inspector: { code: "c", modifiers: shiftOrAlt },
quickOpen: { code: "p", modifiers: cmdOrCtrl },
quickOpenFunc: { code: "o", modifiers: cmdShift },
quickOpenLine: { code: ":", modifiers: cmdOrCtrl },
fileSearch: { code: "f", modifiers: cmdOrCtrl },
Enter: { code: "VK_RETURN" },
ShiftEnter: { code: "VK_RETURN", modifiers: shiftOrAlt },
Up: { code: "VK_UP" },
Down: { code: "VK_DOWN" },
Right: { code: "VK_RIGHT" },
Left: { code: "VK_LEFT" },
End: endKey,
Start: startKey,
Tab: { code: "VK_TAB" },
Escape: { code: "VK_ESCAPE" },
Delete: { code: "VK_DELETE" },
pauseKey: { code: "VK_F8" },
resumeKey: { code: "VK_F8" },
stepOverKey: { code: "VK_F10" },
stepInKey: { code: "VK_F11", modifiers: { ctrlKey: isLinux } },
stepOutKey: {
code: "VK_F11",
modifiers: { ctrlKey: isLinux, shiftKey: true }
}
};
/**
* Simulates a key press in the debugger window.
*
* @memberof mochitest/helpers
* @param {Object} dbg
* @param {String} keyName
* @return {Promise}
* @static
*/
function pressKey(dbg, keyName) {
const keyEvent = keyMappings[keyName];
const { code, modifiers } = keyEvent;
return EventUtils.synthesizeKey(code, modifiers || {}, dbg.win);
}
function type(dbg, string) {
string.split("").forEach(char => EventUtils.synthesizeKey(char, {}, dbg.win));
}
/*
* Checks to see if the inner element is visible inside the editor.
*
* @memberof mochitest/helpers
* @param {Object} dbg
* @param {HTMLElement} inner element
* @return {boolean}
* @static
*/
function isVisibleInEditor(dbg, element) {
return isVisible(findElement(dbg, "codeMirror"), element);
}
/*
* Checks to see if the inner element is visible inside the
* outer element.
*
* Note, the inner element does not need to be entirely visible,
* it is possible for it to be somewhat clipped by the outer element's
* bounding element or for it to span the entire length, starting before the
* outer element and ending after.
*
* @memberof mochitest/helpers
* @param {HTMLElement} outer element
* @param {HTMLElement} inner element
* @return {boolean}