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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
|
/* 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';
module.metadata = {
'engines': {
'Firefox': '*'
}
};
const { Cc, Ci } = require("chrome");
const { Loader } = require('sdk/test/loader');
const { LoaderWithHookedConsole } = require("sdk/test/loader");
const { setTimeout } = require("sdk/timers");
const self = require('sdk/self');
const { open, close, focus, ready } = require('sdk/window/helpers');
const { isPrivate } = require('sdk/private-browsing');
const { isWindowPBSupported } = require('sdk/private-browsing/utils');
const { defer, all } = require('sdk/core/promise');
const { getMostRecentBrowserWindow } = require('sdk/window/utils');
const { URL } = require('sdk/url');
const { wait } = require('./event/helpers');
const packaging = require('@loader/options');
const { cleanUI, after, isTravisCI } = require("sdk/test/utils");
const { platform } = require('sdk/system');
const fixtures = require('./fixtures')
const SVG_URL = fixtures.url('mofo_logo.SVG');
const Isolate = fn => '(' + fn + ')()';
function ignorePassingDOMNodeWarning(type, message) {
if (type !== 'warn' || !message.startsWith('Passing a DOM node'))
console[type](message);
}
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', () => 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', " +
" () => 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', " +
" () => 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 url2 = "data:text/html;charset=utf-8,page2";
let content =
"<script>" +
"window.addEventListener('message', function({ data }) {"+
" if (data == 'move') window.location = '" + url2 + "';" +
'}, false);' +
"</script>";
let messageCount = 0;
let panel = Panel({
// using URL here is intentional, see bug 859009
contentURL: URL("data:text/html;charset=utf-8," + encodeURIComponent(content)),
contentScript: "self.postMessage(window.location.href);" +
// initiate change to url2
"self.port.once('move', () => document.defaultView.postMessage('move', '*'));",
onMessage: function (message) {
messageCount++;
assert.notEqual(message, "about:blank", "about:blank is not a message " + messageCount);
if (messageCount == 1) {
assert.ok(/data:text\/html/.test(message), "First document had a content script; " + message);
panel.port.emit('move');
assert.pass('move message was sent');
return;
}
else if (messageCount == 2) {
assert.equal(message, url2, "Second document too; " + message);
panel.destroy();
done();
}
}
});
assert.pass('Panel was created');
};
// Test disabled because of bug 910230
/*
exports["test Parent Resize Hack"] = function(assert, done) {
const { Panel } = require('sdk/panel');
let browserWindow = getMostRecentBrowserWindow();
let previousWidth = browserWindow.outerWidth;
let previousHeight = browserWindow.outerHeight;
let content = "<script>" +
"function contentResize() {" +
" resizeTo(200,200);" +
" resizeBy(200,200);" +
" window.postMessage('resize-attempt', '*');" +
"}" +
"</script>" +
"Try to resize browser window";
let panel = Panel({
contentURL: "data:text/html;charset=utf-8," + encodeURIComponent(content),
contentScriptWhen: "ready",
contentScript: Isolate(() => {
self.on('message', message => {
if (message === 'resize') unsafeWindow.contentResize();
});
window.addEventListener('message', ({ data }) => self.postMessage(data));
}),
onMessage: function (message) {
if (message !== "resize-attempt") return;
assert.equal(browserWindow, getMostRecentBrowserWindow(),
"The browser window is still the same");
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/...");
try {
panel.destroy();
}
catch (e) {
assert.fail(e);
throw e;
}
done();
},
onShow: () => panel.postMessage('resize')
});
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) {
// Make sure that attempting to resize a panel while it isn't
// visible doesn't cause an error.
panel.resize(1, 1);
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 hideCalled = false;
let panel1 = Panel({
onShow: function () {
showCalled = true;
},
onHide: function () {
hideCalled = true;
}
});
panel1.show();
panel1.hide();
let panel2 = Panel({
onShow: function () {
if (showCalled) {
assert.ok(hideCalled, 'should not emit show without also emitting hide');
} else {
assert.ok(!hideCalled, 'should not emit hide without also emitting show');
}
panel1.destroy();
panel2.destroy();
done();
},
});
panel2.show();
};
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) {
let { loader } = LoaderWithHookedConsole(module, ignorePassingDOMNodeWarning);
let { Panel } = loader.require('sdk/panel');
let count = 0;
let url = 'data:text/html;charset=utf-8,' +
'<html><head><title>foo</title></head><body>' +
'</body></html>';
let panel = yield new Promise(resolve => {
let browserWindow = getMostRecentBrowserWindow();
let anchor = browserWindow.document.getElementById("identity-box");
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: () => resolve(panel)
});
panel.show(null, anchor);
});
assert.pass("All anchored panel test displayed");
panel.destroy();
assert.pass("panel was destroyed.");
};
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 866333
exports["test watch event name"] = 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('watch', 'test');"
});
panel.port.on("watch", function (msg) {
assert.equal(msg, "test", 'watch event name works');
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)
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', () => self.port.emit('event-back'));"
});
loader.unload();
assert.throws(() => {
panel.port.emit("event");
}, /already have been unloaded/, "check automatic destroy");
};
exports["test Show Then Destroy"] = makeEventOrderTest({
test: function(assert, done, expect, panel) {
panel.show();
expect('show', function() { panel.destroy(); }).
then('hide', function() { done(); });
}
});
// TODO: Re-enable and fix this intermittent test
// See Bug 1111695 https://bugzilla.mozilla.org/show_bug.cgi?id=1111695
/*
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 dataURL = "data:text/html;charset=utf-8," + encodeURIComponent(HTML_CONTENT);
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.");
panel.destroy();
panel = Panel({ contentURL: dataURL });
assert.pass("contentURL accepts a data: URL.");
panel.destroy();
panel = Panel({});
assert.ok(panel.contentURL == null, "contentURL is undefined.");
panel.destroy();
assert.throws(() => 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 } = LoaderWithHookedConsole(module, ignorePassingDOMNodeWarning);
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(null, 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 } = LoaderWithHookedConsole(module, ignorePassingDOMNodeWarning);
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(null, window.gBrowser);
showTries++;
panel.show(null, 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);
}
};
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();
};
exports['test passing DOM node as first argument'] = function (assert, done) {
let warned = defer();
let shown = defer();
function onMessage(type, message) {
if (type != 'warn') return;
let warning = 'Passing a DOM node to Panel.show() method is an unsupported ' +
'feature that will be soon replaced. ' +
'See: https://bugzilla.mozilla.org/show_bug.cgi?id=878877';
assert.equal(type, 'warn',
'the message logged is a warning');
assert.equal(message, warning,
'the warning content is correct');
warned.resolve();
}
let { loader } = LoaderWithHookedConsole(module, onMessage);
let { Panel } = loader.require('sdk/panel');
let { ActionButton } = loader.require('sdk/ui/button/action');
let { getNodeView } = loader.require('sdk/view/core');
let { document } = getMostRecentBrowserWindow();
let panel = Panel({
onShow: function() {
let panelNode = document.getElementById('mainPopupSet').lastChild;
assert.equal(panelNode.anchorNode, buttonNode,
'the panel is properly anchored to the button');
shown.resolve();
}
});
let button = ActionButton({
id: 'panel-button',
label: 'panel button',
icon: './icon.png'
});
let buttonNode = getNodeView(button);
all([warned.promise, shown.promise]).
then(loader.unload).
then(done, assert.fail)
panel.show(buttonNode);
};
// This test is checking that `onpupshowing` events emitted by panel's children
// are not considered.
// See Bug 886329
exports['test nested popups'] = function (assert, done) {
let loader = Loader(module);
let { Panel } = loader.require('sdk/panel');
let { getActiveView } = loader.require('sdk/view/core');
let url = '<select><option>1<option>2<option>3</select>';
let getContentWindow = panel => {
return getActiveView(panel).querySelector('iframe').contentWindow;
}
let panel = Panel({
contentURL: 'data:text/html;charset=utf-8,' + encodeURIComponent(url),
onShow: () => {
ready(getContentWindow(panel)).then(({ window, document }) => {
let select = document.querySelector('select');
let event = document.createEvent('UIEvent');
event.initUIEvent('popupshowing', true, true, window, null);
select.dispatchEvent(event);
assert.equal(
select,
getContentWindow(panel).document.querySelector('select'),
'select is still loaded in panel'
);
done();
});
}
});
panel.show();
};
exports['test emits on url changes'] = function (assert, done) {
let loader = Loader(module);
let { Panel } = loader.require('sdk/panel');
let uriA = 'data:text/html;charset=utf-8,A';
let uriB = 'data:text/html;charset=utf-8,B';
let panel = Panel({
contentURL: uriA,
contentScript: 'new ' + function() {
self.port.on('hi', function() {
self.port.emit('bye', document.URL);
});
}
});
panel.contentURL = uriB;
panel.port.emit('hi', 'hi')
panel.port.on('bye', function(uri) {
assert.equal(uri, uriB, 'message was delivered to new uri');
loader.unload();
done();
});
};
exports['test panel can be constructed without any arguments'] = function (assert) {
const { Panel } = require('sdk/panel');
let panel = Panel();
assert.ok(true, "Creating a panel with no arguments does not throw");
};
exports['test panel CSS'] = function(assert, done) {
const { merge } = require("sdk/util/object");
let loader = Loader(module, null, null, {
modules: {
"sdk/self": merge({}, self, {
data: merge({}, self.data, fixtures)
})
}
});
const { Panel } = loader.require('sdk/panel');
const { getActiveView } = loader.require('sdk/view/core');
const getContentWindow = panel =>
getActiveView(panel).querySelector('iframe').contentWindow;
let panel = Panel({
contentURL: 'data:text/html;charset=utf-8,' +
'<div style="background: silver">css test</div>',
contentStyle: 'div { height: 100px; }',
contentStyleFile: [fixtures.url("include-file.css"), "./border-style.css"],
onShow: () => {
ready(getContentWindow(panel)).then(({ window, document }) => {
let div = document.querySelector('div');
assert.equal(div.clientHeight, 100,
"Panel contentStyle worked");
assert.equal(div.offsetHeight, 120,
"Panel contentStyleFile worked");
assert.equal(window.getComputedStyle(div).borderTopStyle, "dashed",
"Panel contentStyleFile with relative path worked");
loader.unload();
done();
}).then(null, assert.fail);
}
});
panel.show();
};
exports['test panel contentScriptFile'] = function(assert, done) {
const { merge } = require("sdk/util/object");
let loader = Loader(module, null, null, {
modules: {
"sdk/self": merge({}, self, {
data: merge({}, self.data, {url: fixtures.url})
})
}
});
const { Panel } = loader.require('sdk/panel');
const { getActiveView } = loader.require('sdk/view/core');
const getContentWindow = panel =>
getActiveView(panel).querySelector('iframe').contentWindow;
let whenMessage = defer();
let whenShown = defer();
let panel = Panel({
contentURL: './test.html',
contentScriptFile: "./test-contentScriptFile.js",
onMessage: (message) => {
assert.equal(message, "msg from contentScriptFile",
"Panel contentScriptFile with relative path worked");
whenMessage.resolve();
},
onShow: () => {
ready(getContentWindow(panel)).then(({ document }) => {
assert.equal(document.title, 'foo',
"Panel contentURL with relative path worked");
whenShown.resolve();
});
}
});
all([whenMessage.promise, whenShown.promise]).
then(loader.unload).
then(done, assert.fail);
panel.show();
};
exports['test panel CSS list'] = function(assert, done) {
const loader = Loader(module);
const { Panel } = loader.require('sdk/panel');
const { getActiveView } = loader.require('sdk/view/core');
const getContentWindow = panel =>
getActiveView(panel).querySelector('iframe').contentWindow;
let panel = Panel({
contentURL: 'data:text/html;charset=utf-8,' +
'<div style="width:320px; max-width: 480px!important">css test</div>',
contentStyleFile: [
// Highlight evaluation order in this list
"data:text/css;charset=utf-8,div { border: 1px solid black; }",
"data:text/css;charset=utf-8,div { border: 10px solid black; }",
// Highlight evaluation order between contentStylesheet & contentStylesheetFile
"data:text/css;charset=utf-8s,div { height: 1000px; }",
// Highlight precedence between the author and user style sheet
"data:text/css;charset=utf-8,div { width: 200px; max-width: 640px!important}",
],
contentStyle: [
"div { height: 10px; }",
"div { height: 100px; }"
],
onShow: () => {
ready(getContentWindow(panel)).then(({ window, document }) => {
let div = document.querySelector('div');
let style = window.getComputedStyle(div);
assert.equal(div.clientHeight, 100,
'Panel contentStyle list is evaluated after contentStyleFile');
assert.equal(div.offsetHeight, 120,
'Panel contentStyleFile list works');
assert.equal(style.width, '320px',
'add-on author/page author stylesheet precedence works');
assert.equal(style.maxWidth, '480px',
'add-on author/page author stylesheet !important precedence works');
loader.unload();
}).then(done, assert.fail);
}
});
panel.show();
};
exports['test panel contextmenu validation'] = function(assert) {
const loader = Loader(module);
const { Panel } = loader.require('sdk/panel');
let panel = Panel({});
assert.equal(panel.contextMenu, false,
'contextMenu option is `false` by default');
panel.destroy();
panel = Panel({
contextMenu: false
});
assert.equal(panel.contextMenu, false,
'contextMenu option is `false`');
panel.contextMenu = true;
assert.equal(panel.contextMenu, true,
'contextMenu option accepts boolean values');
panel.destroy();
panel = Panel({
contextMenu: true
});
assert.equal(panel.contextMenu, true,
'contextMenu option is `true`');
panel.contextMenu = false;
assert.equal(panel.contextMenu, false,
'contextMenu option accepts boolean values');
assert.throws(() =>
Panel({contextMenu: 1}),
/The option "contextMenu" must be one of the following types: boolean, undefined, null/,
'contextMenu only accepts boolean or nil values');
panel = Panel();
assert.throws(() =>
panel.contextMenu = 1,
/The option "contextMenu" must be one of the following types: boolean, undefined, null/,
'contextMenu only accepts boolean or nil values');
loader.unload();
}
exports['test panel contextmenu enabled'] = function*(assert) {
const loader = Loader(module);
const { Panel } = loader.require('sdk/panel');
const { getActiveView } = loader.require('sdk/view/core');
const { getContentDocument } = loader.require('sdk/panel/utils');
let contextmenu = getMostRecentBrowserWindow().
document.getElementById("contentAreaContextMenu");
let panel = Panel({contextMenu: true});
panel.show();
yield wait(panel, 'show');
let view = getActiveView(panel);
let window = getContentDocument(view).defaultView;
let { sendMouseEvent } = window.QueryInterface(Ci.nsIInterfaceRequestor).
getInterface(Ci.nsIDOMWindowUtils);
yield ready(window);
assert.equal(contextmenu.state, 'closed',
'contextmenu must be closed');
sendMouseEvent('contextmenu', 20, 20, 2, 1, 0);
yield wait(contextmenu, 'popupshown');
assert.equal(contextmenu.state, 'open',
'contextmenu is opened');
contextmenu.hidePopup();
loader.unload();
}
exports['test panel contextmenu disabled'] = function*(assert) {
const loader = Loader(module);
const { Panel } = loader.require('sdk/panel');
const { getActiveView } = loader.require('sdk/view/core');
const { getContentDocument } = loader.require('sdk/panel/utils');
let contextmenu = getMostRecentBrowserWindow().
document.getElementById("contentAreaContextMenu");
let listener = () => assert.fail('popupshown should never be called');
let panel = Panel();
panel.show();
yield wait(panel, 'show');
let view = getActiveView(panel);
let window = getContentDocument(view).defaultView;
let { sendMouseEvent } = window.QueryInterface(Ci.nsIInterfaceRequestor).
getInterface(Ci.nsIDOMWindowUtils);
yield ready(window);
assert.equal(contextmenu.state, 'closed',
'contextmenu must be closed');
sendMouseEvent('contextmenu', 20, 20, 2, 1, 0);
contextmenu.addEventListener('popupshown', listener);
yield wait(1000);
contextmenu.removeEventListener('popupshown', listener);
assert.equal(contextmenu.state, 'closed',
'contextmenu was never open');
loader.unload();
}
exports["test panel addon global object"] = function*(assert) {
const { merge } = require("sdk/util/object");
let loader = Loader(module, null, null, {
modules: {
"sdk/self": merge({}, self, {
data: merge({}, self.data, {url: fixtures.url})
})
}
});
const { Panel } = loader.require('sdk/panel');
let panel = Panel({
contentURL: "./test-trusted-document.html"
});
panel.show();
yield wait(panel, "show");
panel.port.emit('addon-to-document', 'ok');
yield wait(panel.port, "document-to-addon");
assert.pass("Received an event from the document");
loader.unload();
}
exports["test panel load doesn't show"] = function*(assert) {
let loader = Loader(module);
let panel = loader.require("sdk/panel").Panel({
contentScript: "addEventListener('load', function(event) { self.postMessage('load'); });",
contentScriptWhen: "start",
contentURL: "data:text/html;charset=utf-8,",
});
let shown = defer();
let messaged = defer();
panel.once("show", function() {
shown.resolve();
});
panel.once("message", function() {
messaged.resolve();
});
panel.show();
yield all([shown.promise, messaged.promise]);
assert.ok(true, "Saw panel display");
panel.on("show", function() {
assert.fail("Should not have seen another show event")
});
messaged = defer();
panel.once("message", function() {
assert.ok(true, "Saw panel reload");
messaged.resolve();
});
panel.contentURL = "data:text/html;charset=utf-8,<html/>";
yield messaged.promise;
loader.unload();
}
exports["test Panel without contentURL and contentScriptWhen=start should show"] = function*(assert) {
let loader = Loader(module);
let panel = loader.require("sdk/panel").Panel({
contentScriptWhen: "start",
// No contentURL, the bug only shows up when contentURL is not explicitly set.
});
yield new Promise(resolve => {
panel.once("show", resolve);
panel.show();
});
assert.pass("Received show event");
loader.unload();
}
exports["test Panel links"] = function*(assert) {
const loader = Loader(module);
const { Panel } = loader.require('sdk/panel');
const { getActiveView } = loader.require('sdk/view/core');
const tabs = loader.require('sdk/tabs');
const synthesizeClick = (panel, options) => {
let { contentWindow } = getActiveView(panel).querySelector('iframe');
let event = new contentWindow.MouseEvent('click', options);
contentWindow.document.querySelector('a').dispatchEvent(event);
}
const linkURL = 'data:text/html;charset=utf-8,' +
encodeURIComponent('<html><a href="#">foo</a></html>');
const contentURL = 'data:text/html;charset=utf-8,' +
encodeURIComponent(`<html><a href="${linkURL}">page</a></html>`);
let panel = Panel({
contentURL,
contentScript: Isolate(() => self.postMessage(document.URL))
});
panel.show();
let url = yield wait(panel, 'message');
assert.equal(url, contentURL,
'content URL loaded');
synthesizeClick(panel, { bubbles: true });
url = yield wait(panel, 'message');
assert.equal(url, linkURL,
'link URL loaded in the panel after click');
synthesizeClick(panel, {
bubbles: true,
[platform === 'darwin' ? 'metaKey' : 'ctrlKey']: true
});
let tab = yield wait(tabs, 'ready');
assert.equal(tab.url, linkURL + '#',
'link URL loaded in a new tab after click + accel');
loader.unload();
}
exports["test Panel script allow property"] = function*(assert) {
const loader = Loader(module);
const { Panel } = loader.require('sdk/panel');
const { getActiveView } = loader.require('sdk/view/core');
const contentURL = 'data:text/html;charset=utf-8,' +
encodeURIComponent(`<body onclick='postMessage("got script click", "*")'>
<script>
document.body.appendChild(document.createElement('unwanted'))
</script>
</body>`);
let panel = Panel({
contentURL,
allow: {script: false},
});
panel.show();
yield wait(panel, 'show');
let { contentWindow } = getActiveView(panel).querySelector('iframe');
assert.equal(contentWindow.document.body.lastElementChild.localName, "script",
"Script should not have executed");
panel.allow.script = true;
let p = wait(contentWindow, "message");
let event = new contentWindow.MouseEvent('click', {});
contentWindow.document.body.dispatchEvent(event);
let msg = yield p;
assert.equal(msg.data, "got script click", "Should have seen script click");
loader.unload();
};
after(exports, function*(name, assert) {
yield cleanUI();
assert.pass("ui was cleaned.");
});
if (isTravisCI) {
module.exports = {
"test skip on jpm": (assert) => assert.pass("skipping this file with jpm")
};
}
require("sdk/test").run(exports);
|