This repository has been archived by the owner on Feb 26, 2022. It is now read-only.
/
test-panel.js
869 lines (748 loc) · 26.4 KB
/
test-panel.js
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';
const { Cc, Ci } = require("chrome");
const { Loader } = require('sdk/test/loader');
const { LoaderWithHookedConsole } = require("sdk/test/loader");
const timer = require("sdk/timers");
const self = require('sdk/self');
const { open, close, focus } = require('sdk/window/helpers');
const { isPrivate } = require('sdk/private-browsing');
const { isWindowPBSupported, isGlobalPBSupported } = require('sdk/private-browsing/utils');
const { defer } = require('sdk/core/promise');
const { getMostRecentBrowserWindow } = require('sdk/window/utils');
const { getWindow } = require('sdk/panel/window');
const { pb } = require('./private-browsing/helper');
const SVG_URL = self.data.url('mofo_logo.SVG');
function makeEmptyPrivateBrowserWindow(options) {
options = options || {};
return open('chrome://browser/content/browser.xul', {
features: {
chrome: true,
toolbar: true,
private: true
}
});
}
exports["test Panel"] = function(assert, done) {
const { Panel } = require('sdk/panel');
let panel = Panel({
contentURL: "about:buildconfig",
contentScript: "self.postMessage(1); self.on('message', function() self.postMessage(2));",
onMessage: function (message) {
assert.equal(this, panel, "The 'this' object is the panel.");
switch(message) {
case 1:
assert.pass("The panel was loaded.");
panel.postMessage('');
break;
case 2:
assert.pass("The panel posted a message and received a response.");
panel.destroy();
done();
break;
}
}
});
};
exports["test Panel Emit"] = function(assert, done) {
const { Panel } = require('sdk/panel');
let panel = Panel({
contentURL: "about:buildconfig",
contentScript: "self.port.emit('loaded');" +
"self.port.on('addon-to-content', " +
" function() self.port.emit('received'));",
});
panel.port.on("loaded", function () {
assert.pass("The panel was loaded and sent a first event.");
panel.port.emit("addon-to-content");
});
panel.port.on("received", function () {
assert.pass("The panel posted a message and received a response.");
panel.destroy();
done();
});
};
exports["test Panel Emit Early"] = function(assert, done) {
const { Panel } = require('sdk/panel');
let panel = Panel({
contentURL: "about:buildconfig",
contentScript: "self.port.on('addon-to-content', " +
" function() self.port.emit('received'));",
});
panel.port.on("received", function () {
assert.pass("The panel posted a message early and received a response.");
panel.destroy();
done();
});
panel.port.emit("addon-to-content");
};
exports["test Show Hide Panel"] = function(assert, done) {
const { Panel } = require('sdk/panel');
let panel = Panel({
contentScript: "self.postMessage('')",
contentScriptWhen: "end",
contentURL: "data:text/html;charset=utf-8,",
onMessage: function (message) {
panel.show();
},
onShow: function () {
assert.pass("The panel was shown.");
assert.equal(this, panel, "The 'this' object is the panel.");
assert.equal(this.isShowing, true, "panel.isShowing == true.");
panel.hide();
},
onHide: function () {
assert.pass("The panel was hidden.");
assert.equal(this, panel, "The 'this' object is the panel.");
assert.equal(this.isShowing, false, "panel.isShowing == false.");
panel.destroy();
done();
}
});
};
exports["test Document Reload"] = function(assert, done) {
const { Panel } = require('sdk/panel');
let content =
"<script>" +
"setTimeout(function () {" +
" window.location = 'about:blank';" +
"}, 250);" +
"</script>";
let messageCount = 0;
let panel = Panel({
contentURL: "data:text/html;charset=utf-8," + encodeURIComponent(content),
contentScript: "self.postMessage(window.location.href)",
onMessage: function (message) {
messageCount++;
if (messageCount == 1) {
assert.ok(/data:text\/html/.test(message), "First document had a content script");
}
else if (messageCount == 2) {
assert.equal(message, "about:blank", "Second document too");
panel.destroy();
done();
}
}
});
};
exports["test Parent Resize Hack"] = function(assert, done) {
const { Panel } = require('sdk/panel');
let browserWindow = Cc["@mozilla.org/appshell/window-mediator;1"].
getService(Ci.nsIWindowMediator).
getMostRecentWindow("navigator:browser");
let docShell = browserWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShell);
if (!("allowWindowControl" in docShell)) {
// bug 635673 is not fixed in this firefox build
assert.pass("allowWindowControl attribute that allow to fix browser window " +
"resize is not available on this build.");
return;
}
let previousWidth = browserWindow.outerWidth, previousHeight = browserWindow.outerHeight;
let content = "<script>" +
"function contentResize() {" +
" resizeTo(200,200);" +
" resizeBy(200,200);" +
"}" +
"</script>" +
"Try to resize browser window";
let panel = Panel({
contentURL: "data:text/html;charset=utf-8," + encodeURIComponent(content),
contentScript: "self.on('message', function(message){" +
" if (message=='resize') " +
" unsafeWindow.contentResize();" +
"});",
contentScriptWhen: "ready",
onMessage: function (message) {
},
onShow: function () {
panel.postMessage('resize');
timer.setTimeout(function () {
assert.equal(previousWidth,browserWindow.outerWidth,"Size doesn't change by calling resizeTo/By/...");
assert.equal(previousHeight,browserWindow.outerHeight,"Size doesn't change by calling resizeTo/By/...");
panel.destroy();
done();
},0);
}
});
panel.show();
}
exports["test Resize Panel"] = function(assert, done) {
const { Panel } = require('sdk/panel');
// These tests fail on Linux if the browser window in which the panel
// is displayed is not active. And depending on what other tests have run
// before this one, it might not be (the untitled window in which the test
// runner executes is often active). So we make sure the browser window
// is focused by focusing it before running the tests. Then, to be the best
// possible test citizen, we refocus whatever window was focused before we
// started running these tests.
let activeWindow = Cc["@mozilla.org/embedcomp/window-watcher;1"].
getService(Ci.nsIWindowWatcher).
activeWindow;
let browserWindow = Cc["@mozilla.org/appshell/window-mediator;1"].
getService(Ci.nsIWindowMediator).
getMostRecentWindow("navigator:browser");
function onFocus() {
browserWindow.removeEventListener("focus", onFocus, true);
let panel = Panel({
contentScript: "self.postMessage('')",
contentScriptWhen: "end",
contentURL: "data:text/html;charset=utf-8,",
height: 10,
width: 10,
onMessage: function (message) {
panel.show();
},
onShow: function () {
panel.resize(100,100);
panel.hide();
},
onHide: function () {
assert.ok((panel.width == 100) && (panel.height == 100),
"The panel was resized.");
if (activeWindow)
activeWindow.focus();
done();
}
});
}
if (browserWindow === activeWindow) {
onFocus();
}
else {
browserWindow.addEventListener("focus", onFocus, true);
browserWindow.focus();
}
};
exports["test Hide Before Show"] = function(assert, done) {
const { Panel } = require('sdk/panel');
let showCalled = false;
let panel = Panel({
onShow: function () {
showCalled = true;
},
onHide: function () {
assert.ok(!showCalled, 'must not emit show if was hidden before');
done();
}
});
panel.show();
panel.hide();
};
exports["test Several Show Hides"] = function(assert, done) {
const { Panel } = require('sdk/panel');
let hideCalled = 0;
let panel = Panel({
contentURL: "about:buildconfig",
onShow: function () {
panel.hide();
},
onHide: function () {
hideCalled++;
if (hideCalled < 3)
panel.show();
else {
assert.pass("onHide called three times as expected");
done();
}
}
});
panel.on('error', function(e) {
assert.fail('error was emitted:' + e.message + '\n' + e.stack);
});
panel.show();
};
exports["test Anchor And Arrow"] = function(assert, done) {
const { Panel } = require('sdk/panel');
let count = 0;
let queue = [];
let tab;
function newPanel(anchor) {
let panel = Panel({
contentURL: "data:text/html;charset=utf-8,<html><body style='padding: 0; margin: 0; " +
"background: gray; text-align: center;'>Anchor: " +
anchor.id + "</body></html>",
width: 200,
height: 100,
onShow: function () {
panel.destroy();
next();
}
});
queue.push({ panel: panel, anchor: anchor });
}
function next () {
if (!queue.length) {
assert.pass("All anchored panel test displayed");
tab.close(function () {
done();
});
return;
}
let { panel, anchor } = queue.shift();
panel.show(anchor);
}
let tabs= require("sdk/tabs");
let url = 'data:text/html;charset=utf-8,' +
'<html><head><title>foo</title></head><body>' +
'<style>div {background: gray; position: absolute; width: 300px; ' +
'border: 2px solid black;}</style>' +
'<div id="tl" style="top: 0px; left: 0px;">Top Left</div>' +
'<div id="tr" style="top: 0px; right: 0px;">Top Right</div>' +
'<div id="bl" style="bottom: 0px; left: 0px;">Bottom Left</div>' +
'<div id="br" style="bottom: 0px; right: 0px;">Bottom right</div>' +
'</body></html>';
tabs.open({
url: url,
onReady: function(_tab) {
tab = _tab;
let browserWindow = Cc["@mozilla.org/appshell/window-mediator;1"].
getService(Ci.nsIWindowMediator).
getMostRecentWindow("navigator:browser");
let window = browserWindow.content;
newPanel(window.document.getElementById('tl'));
newPanel(window.document.getElementById('tr'));
newPanel(window.document.getElementById('bl'));
newPanel(window.document.getElementById('br'));
let anchor = browserWindow.document.getElementById("identity-box");
newPanel(anchor);
next();
}
});
};
exports["test Panel Focus True"] = function(assert, done) {
const { Panel } = require('sdk/panel');
const FM = Cc["@mozilla.org/focus-manager;1"].
getService(Ci.nsIFocusManager);
let browserWindow = Cc["@mozilla.org/appshell/window-mediator;1"].
getService(Ci.nsIWindowMediator).
getMostRecentWindow("navigator:browser");
// Make sure there is a focused element
browserWindow.document.documentElement.focus();
// Get the current focused element
let focusedElement = FM.focusedElement;
let panel = Panel({
contentURL: "about:buildconfig",
focus: true,
onShow: function () {
assert.ok(focusedElement !== FM.focusedElement,
"The panel takes the focus away.");
done();
}
});
panel.show();
};
exports["test Panel Focus False"] = function(assert, done) {
const { Panel } = require('sdk/panel');
const FM = Cc["@mozilla.org/focus-manager;1"].
getService(Ci.nsIFocusManager);
let browserWindow = Cc["@mozilla.org/appshell/window-mediator;1"].
getService(Ci.nsIWindowMediator).
getMostRecentWindow("navigator:browser");
// Make sure there is a focused element
browserWindow.document.documentElement.focus();
// Get the current focused element
let focusedElement = FM.focusedElement;
let panel = Panel({
contentURL: "about:buildconfig",
focus: false,
onShow: function () {
assert.ok(focusedElement === FM.focusedElement,
"The panel does not take the focus away.");
done();
}
});
panel.show();
};
exports["test Panel Focus Not Set"] = function(assert, done) {
const { Panel } = require('sdk/panel');
const FM = Cc["@mozilla.org/focus-manager;1"].
getService(Ci.nsIFocusManager);
let browserWindow = Cc["@mozilla.org/appshell/window-mediator;1"].
getService(Ci.nsIWindowMediator).
getMostRecentWindow("navigator:browser");
// Make sure there is a focused element
browserWindow.document.documentElement.focus();
// Get the current focused element
let focusedElement = FM.focusedElement;
let panel = Panel({
contentURL: "about:buildconfig",
onShow: function () {
assert.ok(focusedElement !== FM.focusedElement,
"The panel takes the focus away.");
done();
}
});
panel.show();
};
exports["test Panel Text Color"] = function(assert, done) {
const { Panel } = require('sdk/panel');
let html = "<html><head><style>body {color: yellow}</style></head>" +
"<body><p>Foo</p></body></html>";
let panel = Panel({
contentURL: "data:text/html;charset=utf-8," + encodeURI(html),
contentScript: "self.port.emit('color', " +
"window.getComputedStyle(document.body.firstChild, null). " +
" getPropertyValue('color'));"
});
panel.port.on("color", function (color) {
assert.equal(color, "rgb(255, 255, 0)",
"The panel text color style is preserved when a style exists.");
panel.destroy();
done();
});
};
// Bug 696552: Ensure panel.contentURL modification support
exports["test Change Content URL"] = function(assert, done) {
const { Panel } = require('sdk/panel');
let panel = Panel({
contentURL: "about:blank",
contentScript: "self.port.emit('ready', document.location.href);"
});
let count = 0;
panel.port.on("ready", function (location) {
count++;
if (count == 1) {
assert.equal(location, "about:blank");
assert.equal(panel.contentURL, "about:blank");
panel.contentURL = "about:buildconfig";
}
else {
assert.equal(location, "about:buildconfig");
assert.equal(panel.contentURL, "about:buildconfig");
panel.destroy();
done();
}
});
};
function makeEventOrderTest(options) {
let expectedEvents = [];
return function(assert, done) {
const { Panel } = require('sdk/panel');
let panel = Panel({ contentURL: "about:buildconfig" });
function expect(event, cb) {
expectedEvents.push(event);
panel.on(event, function() {
assert.equal(event, expectedEvents.shift());
if (cb)
timer.setTimeout(cb, 1);
});
return {then: expect};
}
options.test(assert, done, expect, panel);
}
}
exports["test Automatic Destroy"] = function(assert) {
let loader = Loader(module);
let panel = loader.require("sdk/panel").Panel({
contentURL: "about:buildconfig",
contentScript:
"self.port.on('event', function() self.port.emit('event-back'));"
});
loader.unload();
panel.port.on("event-back", function () {
assert.fail("Panel should have been destroyed on module unload");
});
panel.port.emit("event");
assert.pass("check automatic destroy");
};
exports["test Wait For Init Then Show Then Destroy"] = makeEventOrderTest({
test: function(assert, done, expect, panel) {
expect('inited', function() { panel.show(); }).
then('show', function() { panel.destroy(); }).
then('hide', function() { done(); });
}
});
exports["test Show Then Wait For Init Then Destroy"] = makeEventOrderTest({
test: function(assert, done, expect, panel) {
panel.show();
expect('inited').
then('show', function() { panel.destroy(); }).
then('hide', function() { done(); });
}
});
exports["test Show Then Hide Then Destroy"] = makeEventOrderTest({
test: function(assert, done, expect, panel) {
panel.show();
expect('show', function() { panel.hide(); }).
then('hide', function() { panel.destroy(); done(); });
}
});
exports["test Content URL Option"] = function(assert) {
const { Panel } = require('sdk/panel');
const URL_STRING = "about:buildconfig";
const HTML_CONTENT = "<html><title>Test</title><p>This is a test.</p></html>";
let (panel = Panel({ contentURL: URL_STRING })) {
assert.pass("contentURL accepts a string URL.");
assert.equal(panel.contentURL, URL_STRING,
"contentURL is the string to which it was set.");
}
let dataURL = "data:text/html;charset=utf-8," + encodeURIComponent(HTML_CONTENT);
let (panel = Panel({ contentURL: dataURL })) {
assert.pass("contentURL accepts a data: URL.");
}
let (panel = Panel({})) {
assert.ok(panel.contentURL == null,
"contentURL is undefined.");
}
assert.throws(function () Panel({ contentURL: "foo" }),
/The `contentURL` option must be a valid URL./,
"Panel throws an exception if contentURL is not a URL.");
};
exports["test SVG Document"] = function(assert) {
let panel = require("sdk/panel").Panel({ contentURL: SVG_URL });
panel.show();
panel.hide();
panel.destroy();
assert.pass("contentURL accepts a svg document");
assert.equal(panel.contentURL, SVG_URL,
"contentURL is the string to which it was set.");
};
exports["test ContentScriptOptions Option"] = function(assert, done) {
let loader = Loader(module);
let panel = loader.require("sdk/panel").Panel({
contentScript: "self.postMessage( [typeof self.options.d, self.options] );",
contentScriptWhen: "end",
contentScriptOptions: {a: true, b: [1,2,3], c: "string", d: function(){ return 'test'}},
contentURL: "data:text/html;charset=utf-8,",
onMessage: function(msg) {
assert.equal( msg[0], 'undefined', 'functions are stripped from contentScriptOptions' );
assert.equal( typeof msg[1], 'object', 'object as contentScriptOptions' );
assert.equal( msg[1].a, true, 'boolean in contentScriptOptions' );
assert.equal( msg[1].b.join(), '1,2,3', 'array and numbers in contentScriptOptions' );
assert.equal( msg[1].c, 'string', 'string in contentScriptOptions' );
done();
}
});
};
exports["test console.log in Panel"] = function(assert, done) {
let text = 'console.log() in Panel works!';
let html = '<script>onload = function log(){\
console.log("' + text + '");\
}</script>';
let { loader } = LoaderWithHookedConsole(module, onMessage);
let { Panel } = loader.require('sdk/panel');
let panel = Panel({
contentURL: 'data:text/html;charset=utf-8,' + encodeURIComponent(html)
});
panel.show();
function onMessage(type, message) {
assert.equal(type, 'log', 'console.log() works');
assert.equal(message, text, 'console.log() works');
panel.destroy();
done();
}
};
if (isWindowPBSupported) {
exports.testPanelDoesNotShowInPrivateWindowNoAnchor = function(assert, done) {
let loader = Loader(module);
let { Panel } = loader.require("sdk/panel");
let browserWindow = getMostRecentBrowserWindow();
assert.equal(isPrivate(browserWindow), false, 'open window is not private');
let panel = Panel({
contentURL: SVG_URL
});
testShowPanel(assert, panel).
then(makeEmptyPrivateBrowserWindow).
then(focus).
then(function(window) {
assert.equal(isPrivate(window), true, 'opened window is private');
assert.pass('private window was focused');
return window;
}).
then(function(window) {
let { promise, resolve } = defer();
let showTries = 0;
let showCount = 0;
panel.on('show', function runTests() {
showCount++;
if (showTries == 2) {
panel.removeListener('show', runTests);
assert.equal(showCount, 1, 'show count is correct - 1');
resolve(window);
}
});
showTries++;
panel.show();
showTries++;
panel.show(browserWindow.gBrowser);
return promise;
}).
then(function(window) {
assert.equal(panel.isShowing, true, 'panel is still showing');
panel.hide();
assert.equal(panel.isShowing, false, 'panel is hidden');
return window;
}).
then(close).
then(function() {
assert.pass('private window was closed');
}).
then(testShowPanel.bind(null, assert, panel)).
then(done, assert.fail.bind(assert));
}
exports.testPanelDoesNotShowInPrivateWindowWithAnchor = function(assert, done) {
let loader = Loader(module);
let { Panel } = loader.require("sdk/panel");
let browserWindow = getMostRecentBrowserWindow();
assert.equal(isPrivate(browserWindow), false, 'open window is not private');
let panel = Panel({
contentURL: SVG_URL
});
testShowPanel(assert, panel).
then(makeEmptyPrivateBrowserWindow).
then(focus).
then(function(window) {
assert.equal(isPrivate(window), true, 'opened window is private');
assert.pass('private window was focused');
return window;
}).
then(function(window) {
let { promise, resolve } = defer();
let showTries = 0;
let showCount = 0;
panel.on('show', function runTests() {
showCount++;
if (showTries == 2) {
panel.removeListener('show', runTests);
assert.equal(showCount, 1, 'show count is correct - 1');
resolve(window);
}
});
showTries++;
panel.show(window.gBrowser);
showTries++;
panel.show(browserWindow.gBrowser);
return promise;
}).
then(function(window) {
assert.equal(panel.isShowing, true, 'panel is still showing');
panel.hide();
assert.equal(panel.isShowing, false, 'panel is hidden');
return window;
}).
then(close).
then(function() {
assert.pass('private window was closed');
}).
then(testShowPanel.bind(null, assert, panel)).
then(done, assert.fail.bind(assert));
}
}
function testShowPanel(assert, panel) {
let { promise, resolve } = defer();
assert.ok(!panel.isShowing, 'the panel is not showing [1]');
panel.once('show', function() {
assert.ok(panel.isShowing, 'the panel is showing');
panel.once('hide', function() {
assert.ok(!panel.isShowing, 'the panel is not showing [2]');
resolve(null);
});
panel.hide();
})
panel.show();
return promise;
}
exports['test Style Applied Only Once'] = function (assert, done) {
let loader = Loader(module);
let panel = loader.require("sdk/panel").Panel({
contentURL: "data:text/html;charset=utf-8,",
contentScript:
'self.port.on("check",function() { self.port.emit("count", document.getElementsByTagName("style").length); });' +
'self.port.on("ping", function (count) { self.port.emit("pong", count); });'
});
panel.port.on('count', function (styleCount) {
assert.equal(styleCount, 1, 'should only have one style');
done();
});
panel.port.on('pong', function (counter) {
panel[--counter % 2 ? 'hide' : 'show']();
panel.port.emit(!counter ? 'check' : 'ping', counter);
});
panel.on('show', init);
panel.show();
function init () {
panel.removeListener('show', init);
panel.port.emit('ping', 10);
}
};
if (isWindowPBSupported) {
exports.testGetWindow = function(assert, done) {
let activeWindow = getMostRecentBrowserWindow();
open(null, { features: {
toolbar: true,
chrome: true,
private: true
} }).then(function(window) {
assert.ok(isPrivate(window), 'window is private');
assert.equal(getWindow(window.gBrowser), null, 'private window elements returns null');
assert.equal(getWindow(activeWindow.gBrowser), activeWindow, 'non-private window elements returns window');
close(window).then(done);
})
}
}
else if (isGlobalPBSupported) {
exports.testGetWindow = function(assert, done) {
let activeWindow = getMostRecentBrowserWindow();
assert.equal(getWindow(activeWindow.gBrowser), activeWindow, 'non-private window elements returns window');
pb.once('start', function() {
assert.ok(isPrivate(activeWindow), 'window is private');
assert.equal(getWindow(activeWindow.gBrowser), activeWindow, 'private window elements returns window');
open(null, { features: {
toolbar: true,
chrome: true
} }).then(function(window) {
assert.ok(isPrivate(window), 'window is private');
assert.equal(getWindow(window.gBrowser), window, 'private window elements returns window');
assert.equal(getWindow(activeWindow.gBrowser), activeWindow, 'active window elements returns window');
pb.once('stop', done);
pb.deactivate();
})
});
pb.activate();
}
}
exports['test Only One Panel Open Concurrently'] = function (assert, done) {
const loader = Loader(module);
const { Panel } = loader.require('sdk/panel')
let panelA = Panel({
contentURL: 'about:buildconfig'
});
let panelB = Panel({
contentURL: 'about:buildconfig',
onShow: function () {
// When loading two panels simulataneously, only the second
// should be shown, never showing the first
assert.equal(panelA.isShowing, false, 'First panel is hidden');
assert.equal(panelB.isShowing, true, 'Second panel is showing');
panelC.show();
}
});
let panelC = Panel({
contentURL: 'about:buildconfig',
onShow: function () {
assert.equal(panelA.isShowing, false, 'First panel is hidden');
assert.equal(panelB.isShowing, false, 'Second panel is hidden');
assert.equal(panelC.isShowing, true, 'Third panel is showing');
done();
}
});
panelA.show();
panelB.show();
};
try {
require("sdk/panel");
}
catch (e) {
if (!/^Unsupported Application/.test(e.message))
throw e;
module.exports = {
"test Unsupported Application": function Unsupported (assert) {
assert.pass(e.message);
}
}
}
require("test").run(exports);