This repository has been archived by the owner on Feb 26, 2022. It is now read-only.
/
test-context-menu.js
2582 lines (2147 loc) · 73.1 KB
/
test-context-menu.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
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
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et: */
/* 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/. */
let {Cc,Ci} = require("chrome");
const { Loader } = require('sdk/test/loader');
const timer = require("sdk/timers");
// These should match the same constants in the module.
const ITEM_CLASS = "addon-context-menu-item";
const SEPARATOR_CLASS = "addon-context-menu-separator";
const OVERFLOW_THRESH_DEFAULT = 10;
const OVERFLOW_THRESH_PREF =
"extensions.addon-sdk.context-menu.overflowThreshold";
const OVERFLOW_MENU_CLASS = "addon-content-menu-overflow-menu";
const OVERFLOW_POPUP_CLASS = "addon-content-menu-overflow-popup";
const TEST_DOC_URL = module.uri.replace(/\.js$/, ".html");
// Destroying items that were previously created should cause them to be absent
// from the menu.
exports.testConstructDestroy = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
// Create an item.
let item = new loader.cm.Item({ label: "item" });
test.assertEqual(item.parentMenu, loader.cm.contentContextMenu,
"item's parent menu should be correct");
test.showMenu(null, function (popup) {
// It should be present when the menu is shown.
test.checkMenu([item], [], []);
popup.hidePopup();
// Destroy the item. Multiple destroys should be harmless.
item.destroy();
item.destroy();
test.showMenu(null, function (popup) {
// It should be removed from the menu.
test.checkMenu([item], [], [item]);
test.done();
});
});
};
// Destroying an item twice should not cause an error.
exports.testDestroyTwice = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let item = new loader.cm.Item({ label: "item" });
item.destroy();
item.destroy();
test.pass("Destroying an item twice should not cause an error.");
test.done();
};
// CSS selector contexts should cause their items to be present in the menu
// when the menu is invoked on nodes that match the selectors.
exports.testSelectorContextMatch = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let item = new loader.cm.Item({
label: "item",
data: "item",
context: loader.cm.SelectorContext("img")
});
test.withTestDoc(function (window, doc) {
test.showMenu(doc.getElementById("image"), function (popup) {
test.checkMenu([item], [], []);
test.done();
});
});
};
// CSS selector contexts should cause their items to be present in the menu
// when the menu is invoked on nodes that have ancestors that match the
// selectors.
exports.testSelectorAncestorContextMatch = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let item = new loader.cm.Item({
label: "item",
data: "item",
context: loader.cm.SelectorContext("a[href]")
});
test.withTestDoc(function (window, doc) {
test.showMenu(doc.getElementById("span-link"), function (popup) {
test.checkMenu([item], [], []);
test.done();
});
});
};
// CSS selector contexts should cause their items to be absent from the menu
// when the menu is not invoked on nodes that match or have ancestors that
// match the selectors.
exports.testSelectorContextNoMatch = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let item = new loader.cm.Item({
label: "item",
data: "item",
context: loader.cm.SelectorContext("img")
});
test.showMenu(null, function (popup) {
test.checkMenu([item], [item], []);
test.done();
});
};
// Page contexts should cause their items to be present in the menu when the
// menu is not invoked on an active element.
exports.testPageContextMatch = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let items = [
new loader.cm.Item({
label: "item 0"
}),
new loader.cm.Item({
label: "item 1",
context: undefined
}),
new loader.cm.Item({
label: "item 2",
context: loader.cm.PageContext()
}),
new loader.cm.Item({
label: "item 3",
context: [loader.cm.PageContext()]
})
];
test.showMenu(null, function (popup) {
test.checkMenu(items, [], []);
test.done();
});
};
// Page contexts should cause their items to be absent from the menu when the
// menu is invoked on an active element.
exports.testPageContextNoMatch = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let items = [
new loader.cm.Item({
label: "item 0"
}),
new loader.cm.Item({
label: "item 1",
context: undefined
}),
new loader.cm.Item({
label: "item 2",
context: loader.cm.PageContext()
}),
new loader.cm.Item({
label: "item 3",
context: [loader.cm.PageContext()]
})
];
test.withTestDoc(function (window, doc) {
test.showMenu(doc.getElementById("image"), function (popup) {
test.checkMenu(items, items, []);
test.done();
});
});
};
// Selection contexts should cause items to appear when a selection exists.
exports.testSelectionContextMatch = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let item = loader.cm.Item({
label: "item",
context: loader.cm.SelectionContext()
});
test.withTestDoc(function (window, doc) {
window.getSelection().selectAllChildren(doc.body);
test.showMenu(null, function (popup) {
test.checkMenu([item], [], []);
test.done();
});
});
};
// Selection contexts should cause items to appear when a selection exists in
// a text field.
exports.testSelectionContextMatchInTextField = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let item = loader.cm.Item({
label: "item",
context: loader.cm.SelectionContext()
});
test.withTestDoc(function (window, doc) {
let textfield = doc.getElementById("textfield");
textfield.setSelectionRange(0, textfield.value.length);
test.showMenu(textfield, function (popup) {
test.checkMenu([item], [], []);
test.done();
});
});
};
// Selection contexts should not cause items to appear when a selection does
// not exist in a text field.
exports.testSelectionContextNoMatchInTextField = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let item = loader.cm.Item({
label: "item",
context: loader.cm.SelectionContext()
});
test.withTestDoc(function (window, doc) {
let textfield = doc.getElementById("textfield");
textfield.setSelectionRange(0, 0);
test.showMenu(textfield, function (popup) {
test.checkMenu([item], [item], []);
test.done();
});
});
};
// Selection contexts should not cause items to appear when a selection does
// not exist.
exports.testSelectionContextNoMatch = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let item = loader.cm.Item({
label: "item",
context: loader.cm.SelectionContext()
});
test.showMenu(null, function (popup) {
test.checkMenu([item], [item], []);
test.done();
});
};
// Selection contexts should cause items to appear when a selection exists even
// for newly opened pages
exports.testSelectionContextInNewTab = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let item = loader.cm.Item({
label: "item",
context: loader.cm.SelectionContext()
});
test.withTestDoc(function (window, doc) {
let link = doc.getElementById("targetlink");
link.click();
test.delayedEventListener(this.tabBrowser, "load", function () {
let browser = test.tabBrowser.selectedBrowser;
let window = browser.contentWindow;
let doc = browser.contentDocument;
window.getSelection().selectAllChildren(doc.body);
test.showMenu(null, function (popup) {
test.checkMenu([item], [], []);
popup.hidePopup();
test.tabBrowser.removeTab(test.tabBrowser.selectedTab);
test.tabBrowser.selectedTab = test.tab;
test.showMenu(null, function (popup) {
test.checkMenu([item], [item], []);
test.done();
});
});
}, true);
});
};
// Selection contexts should work when right clicking a form button
exports.testSelectionContextButtonMatch = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let item = loader.cm.Item({
label: "item",
context: loader.cm.SelectionContext()
});
test.withTestDoc(function (window, doc) {
window.getSelection().selectAllChildren(doc.body);
let button = doc.getElementById("button");
test.showMenu(button, function (popup) {
test.checkMenu([item], [], []);
test.done();
});
});
};
//Selection contexts should work when right clicking a form button
exports.testSelectionContextButtonNoMatch = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let item = loader.cm.Item({
label: "item",
context: loader.cm.SelectionContext()
});
test.withTestDoc(function (window, doc) {
let button = doc.getElementById("button");
test.showMenu(button, function (popup) {
test.checkMenu([item], [item], []);
test.done();
});
});
};
// URL contexts should cause items to appear on pages that match.
exports.testURLContextMatch = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let items = [
loader.cm.Item({
label: "item 0",
context: loader.cm.URLContext(TEST_DOC_URL)
}),
loader.cm.Item({
label: "item 1",
context: loader.cm.URLContext([TEST_DOC_URL, "*.bogus.com"])
}),
loader.cm.Item({
label: "item 2",
context: loader.cm.URLContext([new RegExp(".*\\.html")])
})
];
test.withTestDoc(function (window, doc) {
test.showMenu(null, function (popup) {
test.checkMenu(items, [], []);
test.done();
});
});
};
// URL contexts should not cause items to appear on pages that do not match.
exports.testURLContextNoMatch = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let items = [
loader.cm.Item({
label: "item 0",
context: loader.cm.URLContext("*.bogus.com")
}),
loader.cm.Item({
label: "item 1",
context: loader.cm.URLContext(["*.bogus.com", "*.gnarly.com"])
}),
loader.cm.Item({
label: "item 2",
context: loader.cm.URLContext([new RegExp(".*\\.js")])
})
];
test.withTestDoc(function (window, doc) {
test.showMenu(null, function (popup) {
test.checkMenu(items, items, []);
test.done();
});
});
};
// Removing a non-matching URL context after its item is created and the page is
// loaded should cause the item's content script to be evaluated when the
// context menu is next opened.
exports.testURLContextRemove = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let shouldBeEvaled = false;
let context = loader.cm.URLContext("*.bogus.com");
let item = loader.cm.Item({
label: "item",
context: context,
contentScript: 'self.postMessage("ok"); self.on("context", function () true);',
onMessage: function (msg) {
test.assert(shouldBeEvaled,
"content script should be evaluated when expected");
test.assertEqual(msg, "ok", "Should have received the right message");
shouldBeEvaled = false;
}
});
test.withTestDoc(function (window, doc) {
test.showMenu(null, function (popup) {
test.checkMenu([item], [item], []);
item.context.remove(context);
shouldBeEvaled = true;
test.hideMenu(function () {
test.showMenu(null, function (popup) {
test.checkMenu([item], [], []);
test.assert(!shouldBeEvaled,
"content script should have been evaluated");
test.hideMenu(function () {
// Shouldn't get evaluated again
test.showMenu(null, function (popup) {
test.checkMenu([item], [], []);
test.done();
});
});
});
});
});
});
};
// Closing a page after it's been used with a worker should cause the worker
// to be destroyed
/*exports.testWorkerDestroy = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let loadExpected = false;
let item = loader.cm.Item({
label: "item",
contentScript: 'self.postMessage("loaded"); self.on("detach", function () { console.log("saw detach"); self.postMessage("detach") });',
onMessage: function (msg) {
console.log("Saw " + msg)
switch (msg) {
case "loaded":
test.assert(loadExpected, "Should have seen the load event at the right time");
loadExpected = false;
break;
case "detach":
test.done();
break;
}
}
});
test.withTestDoc(function (window, doc) {
loadExpected = true;
test.showMenu(null, function (popup) {
test.assert(!loadExpected, "Should have seen a message");
test.checkMenu([item], [], []);
test.closeTab();
});
});
};*/
// Content contexts that return true should cause their items to be present
// in the menu.
exports.testContentContextMatch = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let item = new loader.cm.Item({
label: "item",
contentScript: 'self.on("context", function () true);'
});
test.showMenu(null, function (popup) {
test.checkMenu([item], [], []);
test.done();
});
};
// Content contexts that return false should cause their items to be absent
// from the menu.
exports.testContentContextNoMatch = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let item = new loader.cm.Item({
label: "item",
contentScript: 'self.on("context", function () false);'
});
test.showMenu(null, function (popup) {
test.checkMenu([item], [item], []);
test.done();
});
};
// Content contexts that return true should cause their items to be present
// in the menu when context clicking an active element.
exports.testContentContextMatchActiveElement = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let items = [
new loader.cm.Item({
label: "item 1",
contentScript: 'self.on("context", function () true);'
}),
new loader.cm.Item({
label: "item 2",
context: undefined,
contentScript: 'self.on("context", function () true);'
}),
// These items will always be hidden by the declarative usage of PageContext
new loader.cm.Item({
label: "item 3",
context: loader.cm.PageContext(),
contentScript: 'self.on("context", function () true);'
}),
new loader.cm.Item({
label: "item 4",
context: [loader.cm.PageContext()],
contentScript: 'self.on("context", function () true);'
})
];
test.withTestDoc(function (window, doc) {
test.showMenu(doc.getElementById("image"), function (popup) {
test.checkMenu(items, [items[2], items[3]], []);
test.done();
});
});
};
// Content contexts that return false should cause their items to be absent
// from the menu when context clicking an active element.
exports.testContentContextNoMatchActiveElement = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let items = [
new loader.cm.Item({
label: "item 1",
contentScript: 'self.on("context", function () false);'
}),
new loader.cm.Item({
label: "item 2",
context: undefined,
contentScript: 'self.on("context", function () false);'
}),
// These items will always be hidden by the declarative usage of PageContext
new loader.cm.Item({
label: "item 3",
context: loader.cm.PageContext(),
contentScript: 'self.on("context", function () false);'
}),
new loader.cm.Item({
label: "item 4",
context: [loader.cm.PageContext()],
contentScript: 'self.on("context", function () false);'
})
];
test.withTestDoc(function (window, doc) {
test.showMenu(doc.getElementById("image"), function (popup) {
test.checkMenu(items, items, []);
test.done();
});
});
};
// Content contexts that return a string should cause their items to be present
// in the menu and the items' labels to be updated.
exports.testContentContextMatchString = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let item = new loader.cm.Item({
label: "first label",
contentScript: 'self.on("context", function () "second label");'
});
test.showMenu(null, function (popup) {
test.checkMenu([item], [], []);
test.assertEqual(item.label, "second label",
"item's label should be updated");
test.done();
});
};
// Ensure that contentScriptFile is working correctly
exports.testContentScriptFile = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
// Reject remote files
test.assertRaises(function() {
new loader.cm.Item({
label: "item",
contentScriptFile: "http://mozilla.com/context-menu.js"
});
},
"The 'contentScriptFile' option must be a local file URL " +
"or an array of local file URLs.",
"Item throws when contentScriptFile is a remote URL");
// But accept files from data folder
let item = new loader.cm.Item({
label: "item",
contentScriptFile: require("sdk/self").data.url("test-context-menu.js")
});
test.showMenu(null, function (popup) {
test.checkMenu([item], [], []);
test.done();
});
};
// The args passed to context listeners should be correct.
exports.testContentContextArgs = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let callbacks = 0;
let item = new loader.cm.Item({
label: "item",
contentScript: 'self.on("context", function (node) {' +
' self.postMessage(node.tagName);' +
' return false;' +
'});',
onMessage: function (tagName) {
test.assertEqual(tagName, "HTML", "node should be an HTML element");
if (++callbacks == 2) test.done();
}
});
test.showMenu(null, function () {
if (++callbacks == 2) test.done();
});
};
// Multiple contexts imply intersection, not union, and content context
// listeners should not be called if all declarative contexts are not current.
exports.testMultipleContexts = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let item = new loader.cm.Item({
label: "item",
context: [loader.cm.SelectorContext("a[href]"), loader.cm.PageContext()],
contentScript: 'self.on("context", function () self.postMessage());',
onMessage: function () {
test.fail("Context listener should not be called");
}
});
test.withTestDoc(function (window, doc) {
test.showMenu(doc.getElementById("span-link"), function (popup) {
test.checkMenu([item], [item], []);
test.done();
});
});
};
// Once a context is removed, it should no longer cause its item to appear.
exports.testRemoveContext = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let ctxt = loader.cm.SelectorContext("img");
let item = new loader.cm.Item({
label: "item",
context: ctxt
});
test.withTestDoc(function (window, doc) {
test.showMenu(doc.getElementById("image"), function (popup) {
// The item should be present at first.
test.checkMenu([item], [], []);
popup.hidePopup();
// Remove the img context and check again.
item.context.remove(ctxt);
test.showMenu(doc.getElementById("image"), function (popup) {
test.checkMenu([item], [item], []);
test.done();
});
});
});
};
// Lots of items should overflow into the overflow submenu.
exports.testOverflow = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let items = [];
for (let i = 0; i < OVERFLOW_THRESH_DEFAULT + 1; i++) {
let item = new loader.cm.Item({ label: "item " + i });
items.push(item);
}
test.showMenu(null, function (popup) {
test.checkMenu(items, [], []);
test.done();
});
};
// Module unload should cause all items to be removed.
exports.testUnload = function (test) {
test = new TestHelper(test);
let loader = test.newLoader();
let item = new loader.cm.Item({ label: "item" });
test.showMenu(null, function (popup) {
// The menu should contain the item.
test.checkMenu([item], [], []);
popup.hidePopup();
// Unload the module.
loader.unload();
test.showMenu(null, function (popup) {
// The item should be removed from the menu.
test.checkMenu([item], [], [item]);
test.done();
});
});
};
// Using multiple module instances to add items without causing overflow should
// work OK. Assumes OVERFLOW_THRESH_DEFAULT <= 2.
exports.testMultipleModulesAdd = function (test) {
test = new TestHelper(test);
let loader0 = test.newLoader();
let loader1 = test.newLoader();
// Use each module to add an item, then unload each module in turn.
let item0 = new loader0.cm.Item({ label: "item 0" });
let item1 = new loader1.cm.Item({ label: "item 1" });
test.showMenu(null, function (popup) {
// The menu should contain both items.
test.checkMenu([item0, item1], [], []);
popup.hidePopup();
// Unload the first module.
loader0.unload();
test.showMenu(null, function (popup) {
// The first item should be removed from the menu.
test.checkMenu([item0, item1], [], [item0]);
popup.hidePopup();
// Unload the second module.
loader1.unload();
test.showMenu(null, function (popup) {
// Both items should be removed from the menu.
test.checkMenu([item0, item1], [], [item0, item1]);
test.done();
});
});
});
};
// Using multiple module instances to add items causing overflow should work OK.
exports.testMultipleModulesAddOverflow = function (test) {
test = new TestHelper(test);
let loader0 = test.newLoader();
let loader1 = test.newLoader();
// Use module 0 to add OVERFLOW_THRESH_DEFAULT items.
let items0 = [];
for (let i = 0; i < OVERFLOW_THRESH_DEFAULT; i++) {
let item = new loader0.cm.Item({ label: "item 0 " + i });
items0.push(item);
}
// Use module 1 to add one item.
let item1 = new loader1.cm.Item({ label: "item 1" });
let allItems = items0.concat(item1);
test.showMenu(null, function (popup) {
// The menu should contain all items in overflow.
test.checkMenu(allItems, [], []);
popup.hidePopup();
// Unload the first module.
loader0.unload();
test.showMenu(null, function (popup) {
// The first items should be removed from the menu, which should not
// overflow.
test.checkMenu(allItems, [], items0);
popup.hidePopup();
// Unload the second module.
loader1.unload();
test.showMenu(null, function (popup) {
// All items should be removed from the menu.
test.checkMenu(allItems, [], allItems);
test.done();
});
});
});
};
// Using multiple module instances to modify the menu without causing overflow
// should work OK. This test creates two loaders and:
// loader0 create item -> loader1 create item -> loader0.unload ->
// loader1.unload
exports.testMultipleModulesDiffContexts1 = function (test) {
test = new TestHelper(test);
let loader0 = test.newLoader();
let loader1 = test.newLoader();
let item0 = new loader0.cm.Item({
label: "item 0",
context: loader0.cm.SelectorContext("img")
});
let item1 = new loader1.cm.Item({ label: "item 1" });
test.showMenu(null, function (popup) {
// The menu should contain item1.
test.checkMenu([item0, item1], [item0], []);
popup.hidePopup();
// Unload module 0.
loader0.unload();
test.showMenu(null, function (popup) {
// item0 should be removed from the menu.
test.checkMenu([item0, item1], [], [item0]);
popup.hidePopup();
// Unload module 1.
loader1.unload();
test.showMenu(null, function (popup) {
// Both items should be removed from the menu.
test.checkMenu([item0, item1], [], [item0, item1]);
test.done();
});
});
});
};
// Using multiple module instances to modify the menu without causing overflow
// should work OK. This test creates two loaders and:
// loader1 create item -> loader0 create item -> loader0.unload ->
// loader1.unload
exports.testMultipleModulesDiffContexts2 = function (test) {
test = new TestHelper(test);
let loader0 = test.newLoader();
let loader1 = test.newLoader();
let item1 = new loader1.cm.Item({ label: "item 1" });
let item0 = new loader0.cm.Item({
label: "item 0",
context: loader0.cm.SelectorContext("img")
});
test.showMenu(null, function (popup) {
// The menu should contain item1.
test.checkMenu([item0, item1], [item0], []);
popup.hidePopup();
// Unload module 0.
loader0.unload();
test.showMenu(null, function (popup) {
// item0 should be removed from the menu.
test.checkMenu([item0, item1], [], [item0]);
popup.hidePopup();
// Unload module 1.
loader1.unload();
test.showMenu(null, function (popup) {
// Both items should be removed from the menu.
test.checkMenu([item0, item1], [], [item0, item1]);
test.done();
});
});
});
};
// Using multiple module instances to modify the menu without causing overflow
// should work OK. This test creates two loaders and:
// loader0 create item -> loader1 create item -> loader1.unload ->
// loader0.unload
exports.testMultipleModulesDiffContexts3 = function (test) {
test = new TestHelper(test);
let loader0 = test.newLoader();
let loader1 = test.newLoader();
let item0 = new loader0.cm.Item({
label: "item 0",
context: loader0.cm.SelectorContext("img")
});
let item1 = new loader1.cm.Item({ label: "item 1" });
test.showMenu(null, function (popup) {
// The menu should contain item1.
test.checkMenu([item0, item1], [item0], []);
popup.hidePopup();
// Unload module 1.
loader1.unload();
test.showMenu(null, function (popup) {
// item1 should be removed from the menu.
test.checkMenu([item0, item1], [item0], [item1]);
popup.hidePopup();
// Unload module 0.
loader0.unload();
test.showMenu(null, function (popup) {
// Both items should be removed from the menu.
test.checkMenu([item0, item1], [], [item0, item1]);
test.done();
});
});
});
};
// Using multiple module instances to modify the menu without causing overflow
// should work OK. This test creates two loaders and:
// loader1 create item -> loader0 create item -> loader1.unload ->
// loader0.unload
exports.testMultipleModulesDiffContexts4 = function (test) {
test = new TestHelper(test);
let loader0 = test.newLoader();
let loader1 = test.newLoader();