summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_browserAction_context.js
blob: c1f8184c78394d2d538fe47d018f98583bc79fc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

async function runTests(options) {
  async function background(getTests) {
    let manifest = browser.runtime.getManifest();
    let { manifest_version } = manifest;
    const action = manifest_version < 3 ? "browserAction" : "action";
    async function checkExtAPIDetails(expecting, details) {
      let title = await browser[action].getTitle(details);
      browser.test.assertEq(
        expecting.title,
        title,
        "expected value from getTitle"
      );

      let popup = await browser[action].getPopup(details);
      browser.test.assertEq(
        expecting.popup,
        popup,
        "expected value from getPopup"
      );

      let badge = await browser[action].getBadgeText(details);
      browser.test.assertEq(
        expecting.badge,
        badge,
        "expected value from getBadge"
      );

      let badgeBackgroundColor = await browser[action].getBadgeBackgroundColor(
        details
      );
      browser.test.assertEq(
        String(expecting.badgeBackgroundColor),
        String(badgeBackgroundColor),
        "expected value from getBadgeBackgroundColor"
      );

      let badgeTextColor = await browser[action].getBadgeTextColor(details);
      browser.test.assertEq(
        String(expecting.badgeTextColor),
        String(badgeTextColor),
        "expected value from getBadgeTextColor"
      );

      let enabled = await browser[action].isEnabled(details);
      browser.test.assertEq(
        expecting.enabled,
        enabled,
        "expected value from isEnabled"
      );
    }

    let tabs = [];
    let windows = [];
    let tests = getTests(tabs, windows);

    {
      let tabId = 0xdeadbeef;
      let calls = [
        () => browser[action].enable(tabId),
        () => browser[action].disable(tabId),
        () => browser[action].setTitle({ tabId, title: "foo" }),
        () => browser[action].setIcon({ tabId, path: "foo.png" }),
        () => browser[action].setPopup({ tabId, popup: "foo.html" }),
        () => browser[action].setBadgeText({ tabId, text: "foo" }),
        () =>
          browser[action].setBadgeBackgroundColor({
            tabId,
            color: [0xff, 0, 0, 0xff],
          }),
        () =>
          browser[action].setBadgeTextColor({
            tabId,
            color: [0, 0xff, 0xff, 0xff],
          }),
      ];

      for (let call of calls) {
        await browser.test.assertRejects(
          new Promise(resolve => resolve(call())),
          RegExp(`Invalid tab ID: ${tabId}`),
          "Expected invalid tab ID error"
        );
      }
    }

    // Runs the next test in the `tests` array, checks the results,
    // and passes control back to the outer test scope.
    function nextTest() {
      let test = tests.shift();

      test(async (expectTab, expectWindow, expectGlobal, expectDefault) => {
        expectGlobal = { ...expectDefault, ...expectGlobal };
        expectWindow = { ...expectGlobal, ...expectWindow };
        expectTab = { ...expectWindow, ...expectTab };

        // Check that the API returns the expected values, and then
        // run the next test.
        let [{ windowId, id: tabId }] = await browser.tabs.query({
          active: true,
          currentWindow: true,
        });
        await checkExtAPIDetails(expectTab, { tabId });
        await checkExtAPIDetails(expectWindow, { windowId });
        await checkExtAPIDetails(expectGlobal, {});

        // Check that the actual icon has the expected values, then
        // run the next test.
        browser.test.sendMessage("nextTest", expectTab, windowId, tests.length);
      });
    }

    browser.test.onMessage.addListener(msg => {
      if (msg != "runNextTest") {
        browser.test.fail("Expecting 'runNextTest' message");
      }

      nextTest();
    });

    let [{ id, windowId }] = await browser.tabs.query({
      active: true,
      currentWindow: true,
    });
    tabs.push(id);
    windows.push(windowId);
    nextTest();
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: options.manifest,

    files: options.files || {},

    background: `(${background})(${options.getTests})`,
  });

  function serializeColor([r, g, b, a]) {
    if (a === 255) {
      return `rgb(${r}, ${g}, ${b})`;
    }
    return `rgba(${r}, ${g}, ${b}, ${a / 255})`;
  }

  let browserActionId;
  async function checkWidgetDetails(details, windowId) {
    let { document } = Services.wm.getOuterWindowWithId(windowId);
    if (!browserActionId) {
      browserActionId = `${makeWidgetId(extension.id)}-browser-action`;
    }

    let node = document.getElementById(browserActionId);
    let button = node.firstElementChild;

    ok(button, "button exists");

    let title = details.title || options.manifest.name;

    // NOTE: resorting to waitForCondition to prevent frequent
    // intermittent failures due to multiple action API calls
    // being queued.
    if (getListStyleImage(button) !== details.icon) {
      info(`wait for action icon url to be set to ${details.icon}`);
      await TestUtils.waitForCondition(
        () => getListStyleImage(button) === details.icon,
        "Wait for the expected icon URL to be set"
      );
    }

    // NOTE: resorting to waitForCondition to prevent frequent
    // intermittent failures due to multiple action API calls
    // being queued.
    if (button.getAttribute("tooltiptext") !== title) {
      info(`wait for action tooltiptext to be set to ${title}`);
      await TestUtils.waitForCondition(
        () => button.getAttribute("tooltiptext") === title,
        "Wait for expected title to be set"
      );
    }

    is(getListStyleImage(button), details.icon, "icon URL is correct");
    is(button.getAttribute("tooltiptext"), title, "image title is correct");
    is(button.getAttribute("label"), title, "image label is correct");
    is(button.getAttribute("badge"), details.badge, "badge text is correct");
    is(
      button.getAttribute("disabled") == "true",
      !details.enabled,
      "disabled state is correct"
    );

    if (details.badge) {
      let badge = button.badgeLabel;
      let style = window.getComputedStyle(badge);
      let expected = {
        backgroundColor: serializeColor(details.badgeBackgroundColor),
        color: serializeColor(details.badgeTextColor),
      };
      for (let [prop, value] of Object.entries(expected)) {
        // NOTE: resorting to waitForCondition to prevent frequent
        // intermittent failures due to multiple action API calls
        // being queued.
        if (style[prop] !== value) {
          info(`wait for badge ${prop} to be set to ${value}`);
          await TestUtils.waitForCondition(
            () => window.getComputedStyle(badge)[prop] === value,
            `Wait for expected badge ${prop} to be set`
          );
        }
      }
    }

    // TODO: Popup URL.
  }

  let awaitFinish = new Promise(resolve => {
    extension.onMessage(
      "nextTest",
      async (expecting, windowId, testsRemaining) => {
        await promiseAnimationFrame();
        await checkWidgetDetails(expecting, windowId);

        if (testsRemaining) {
          extension.sendMessage("runNextTest");
        } else {
          resolve();
        }
      }
    );
  });

  await extension.startup();

  await awaitFinish;

  await extension.unload();
}

let tabSwitchTestData = {
  files: {
    "_locales/en/messages.json": {
      popup: {
        message: "default.html",
        description: "Popup",
      },

      title: {
        message: "Title",
        description: "Title",
      },
    },

    "default.png": imageBuffer,
    "global.png": imageBuffer,
    "1.png": imageBuffer,
    "2.png": imageBuffer,
  },

  getTests: function (tabs, windows) {
    let manifest = browser.runtime.getManifest();
    let { manifest_version } = manifest;
    const action = manifest_version < 3 ? "browserAction" : "action";

    let details = [
      {
        icon: browser.runtime.getURL("default.png"),
        popup: browser.runtime.getURL("default.html"),
        title: "Default Title",
        badge: "",
        badgeBackgroundColor: [0xd9, 0, 0, 255],
        badgeTextColor: [0xff, 0xff, 0xff, 0xff],
        enabled: true,
      },
      { icon: browser.runtime.getURL("1.png") },
      {
        icon: browser.runtime.getURL("2.png"),
        popup: browser.runtime.getURL("2.html"),
        title: "Title 2",
        badge: "2",
        badgeBackgroundColor: [0xff, 0, 0, 0xff],
        badgeTextColor: [0, 0xff, 0xff, 0xff],
        enabled: false,
      },
      {
        icon: browser.runtime.getURL("global.png"),
        popup: browser.runtime.getURL("global.html"),
        title: "Global Title",
        badge: "g",
        badgeBackgroundColor: [0, 0xff, 0, 0xff],
        badgeTextColor: [0xff, 0, 0xff, 0xff],
        enabled: false,
      },
      {
        icon: browser.runtime.getURL("global.png"),
        popup: browser.runtime.getURL("global.html"),
        title: "Global Title",
        badge: "g",
        badgeBackgroundColor: [0, 0xff, 0, 0xff],
        badgeTextColor: [0xff, 0, 0xff, 0xff],
      },
    ];

    let promiseTabLoad = details => {
      return new Promise(resolve => {
        browser.tabs.onUpdated.addListener(function listener(tabId, changed) {
          if (tabId == details.id && changed.url == details.url) {
            browser.tabs.onUpdated.removeListener(listener);
            resolve();
          }
        });
      });
    };

    return [
      async expect => {
        browser.test.log("Initial state, expect default properties.");

        expect(null, null, null, details[0]);
      },
      async expect => {
        browser.test.log(
          "Change the icon in the current tab. Expect default properties excluding the icon."
        );
        browser[action].setIcon({ tabId: tabs[0], path: "1.png" });

        expect(details[1], null, null, details[0]);
      },
      async expect => {
        browser.test.log("Create a new tab. Expect default properties.");
        let tab = await browser.tabs.create({
          active: true,
          url: "about:blank?0",
        });
        tabs.push(tab.id);

        browser.test.log("Await tab load.");
        let promise = promiseTabLoad({ id: tabs[1], url: "about:blank?0" });
        let { url } = await browser.tabs.get(tabs[1]);
        if (url === "about:blank") {
          await promise;
        }

        expect(null, null, null, details[0]);
      },
      async expect => {
        browser.test.log("Change properties. Expect new properties.");
        let tabId = tabs[1];
        browser[action].setIcon({ tabId, path: "2.png" });
        browser[action].setPopup({ tabId, popup: "2.html" });
        browser[action].setTitle({ tabId, title: "Title 2" });
        browser[action].setBadgeText({ tabId, text: "2" });
        browser[action].setBadgeBackgroundColor({
          tabId,
          color: "#ff0000",
        });
        browser[action].setBadgeTextColor({ tabId, color: "#00ffff" });
        browser[action].disable(tabId);

        expect(details[2], null, null, details[0]);
      },
      async expect => {
        browser.test.log(
          "Switch back to the first tab. Expect previously set properties."
        );
        await browser.tabs.update(tabs[0], { active: true });
        expect(details[1], null, null, details[0]);
      },
      async expect => {
        browser.test.log(
          "Change global values, expect those changes reflected."
        );
        browser[action].setIcon({ path: "global.png" });
        browser[action].setPopup({ popup: "global.html" });
        browser[action].setTitle({ title: "Global Title" });
        browser[action].setBadgeText({ text: "g" });
        browser[action].setBadgeBackgroundColor({
          color: [0, 0xff, 0, 0xff],
        });
        browser[action].setBadgeTextColor({
          color: [0xff, 0, 0xff, 0xff],
        });
        browser[action].disable();

        expect(details[1], null, details[3], details[0]);
      },
      async expect => {
        browser.test.log("Re-enable globally. Expect enabled.");
        browser[action].enable();

        expect(details[1], null, details[4], details[0]);
      },
      async expect => {
        browser.test.log(
          "Switch back to tab 2. Expect former tab values, and new global values from previous steps."
        );
        await browser.tabs.update(tabs[1], { active: true });

        expect(details[2], null, details[4], details[0]);
      },
      async expect => {
        browser.test.log(
          "Navigate to a new page. Expect tab-specific values to be cleared."
        );

        let promise = promiseTabLoad({ id: tabs[1], url: "about:blank?1" });
        browser.tabs.update(tabs[1], { url: "about:blank?1" });
        await promise;

        expect(null, null, details[4], details[0]);
      },
      async expect => {
        browser.test.log(
          "Delete tab, switch back to tab 1. Expect previous results again."
        );
        await browser.tabs.remove(tabs[1]);
        expect(details[1], null, details[4], details[0]);
      },
      async expect => {
        browser.test.log("Create a new tab. Expect new global properties.");
        let tab = await browser.tabs.create({
          active: true,
          url: "about:blank?2",
        });
        tabs.push(tab.id);
        expect(null, null, details[4], details[0]);
      },
      async expect => {
        browser.test.log("Delete tab.");
        await browser.tabs.remove(tabs[2]);
        expect(details[1], null, details[4], details[0]);
      },
    ];
  },
};

add_task(async function testTabSwitchContext() {
  await runTests({
    manifest: {
      browser_action: {
        default_icon: "default.png",
        default_popup: "__MSG_popup__",
        default_title: "Default __MSG_title__",
        default_area: "navbar",
      },

      default_locale: "en",

      permissions: ["tabs"],
    },
    ...tabSwitchTestData,
  });
});

add_task(async function testTabSwitchActionContext() {
  await SpecialPowers.pushPrefEnv({
    set: [["extensions.manifestV3.enabled", true]],
  });

  await runTests({
    manifest: {
      manifest_version: 3,
      action: {
        default_icon: "default.png",
        default_popup: "__MSG_popup__",
        default_title: "Default __MSG_title__",
        default_area: "navbar",
      },
      default_locale: "en",
      permissions: ["tabs"],
    },
    ...tabSwitchTestData,
  });
});

add_task(async function testDefaultTitle() {
  await runTests({
    manifest: {
      name: "Foo Extension",

      browser_action: {
        default_icon: "icon.png",
        default_area: "navbar",
      },

      permissions: ["tabs"],
    },

    files: {
      "icon.png": imageBuffer,
    },

    getTests: function (tabs, windows) {
      let details = [
        {
          title: "Foo Extension",
          popup: "",
          badge: "",
          badgeBackgroundColor: [0xd9, 0, 0, 255],
          badgeTextColor: [0xff, 0xff, 0xff, 0xff],
          icon: browser.runtime.getURL("icon.png"),
          enabled: true,
        },
        { title: "Foo Title" },
        { title: "Bar Title" },
      ];

      return [
        async expect => {
          browser.test.log("Initial state. Expect default properties.");

          expect(null, null, null, details[0]);
        },
        async expect => {
          browser.test.log("Change the tab title. Expect new title.");
          browser.browserAction.setTitle({
            tabId: tabs[0],
            title: "Foo Title",
          });

          expect(details[1], null, null, details[0]);
        },
        async expect => {
          browser.test.log("Change the global title. Expect same properties.");
          browser.browserAction.setTitle({ title: "Bar Title" });

          expect(details[1], null, details[2], details[0]);
        },
        async expect => {
          browser.test.log("Clear the tab title. Expect new global title.");
          browser.browserAction.setTitle({ tabId: tabs[0], title: null });

          expect(null, null, details[2], details[0]);
        },
        async expect => {
          browser.test.log("Clear the global title. Expect default title.");
          browser.browserAction.setTitle({ title: null });

          expect(null, null, null, details[0]);
        },
        async expect => {
          browser.test.assertRejects(
            browser.browserAction.setPopup({ popup: "about:addons" }),
            /Access denied for URL about:addons/,
            "unable to set popup to about:addons"
          );

          expect(null, null, null, details[0]);
        },
      ];
    },
  });
});

add_task(async function testBadgeColorPersistence() {
  const extension = ExtensionTestUtils.loadExtension({
    background() {
      browser.test.onMessage.addListener((msg, arg) => {
        browser.browserAction[msg](arg);
      });
    },
    manifest: {
      browser_action: {
        default_area: "navbar",
      },
    },
  });
  await extension.startup();

  function getBadgeForWindow(win) {
    const widget = getBrowserActionWidget(extension).forWindow(win).node;
    return widget.firstElementChild.badgeLabel;
  }

  let badge = getBadgeForWindow(window);
  const badgeChanged = new Promise(resolve => {
    const observer = new MutationObserver(() => resolve());
    observer.observe(badge, { attributes: true, attributeFilter: ["style"] });
  });

  extension.sendMessage("setBadgeText", { text: "hi" });
  extension.sendMessage("setBadgeBackgroundColor", { color: [0, 255, 0, 255] });

  await badgeChanged;

  is(badge.textContent, "hi", "badge text is set in first window");
  is(
    badge.style.backgroundColor,
    "rgb(0, 255, 0)",
    "badge color is set in first window"
  );

  let windowOpenedPromise = BrowserTestUtils.waitForNewWindow();
  let win = OpenBrowserWindow();
  await windowOpenedPromise;

  badge = getBadgeForWindow(win);
  is(badge.textContent, "hi", "badge text is set in new window");
  is(
    badge.style.backgroundColor,
    "rgb(0, 255, 0)",
    "badge color is set in new window"
  );

  await BrowserTestUtils.closeWindow(win);
  await extension.unload();
});

add_task(async function testPropertyRemoval() {
  await runTests({
    manifest: {
      name: "Generated extension",
      browser_action: {
        default_icon: "default.png",
        default_popup: "default.html",
        default_title: "Default Title",
        default_area: "navbar",
      },
    },

    files: {
      "default.png": imageBuffer,
      "global.png": imageBuffer,
      "global2.png": imageBuffer,
      "window.png": imageBuffer,
      "tab.png": imageBuffer,
    },

    getTests: function (tabs, windows) {
      let defaultIcon = "chrome://mozapps/skin/extensions/extensionGeneric.svg";
      let details = [
        {
          icon: browser.runtime.getURL("default.png"),
          popup: browser.runtime.getURL("default.html"),
          title: "Default Title",
          badge: "",
          badgeBackgroundColor: [0xd9, 0x00, 0x00, 0xff],
          badgeTextColor: [0xff, 0xff, 0xff, 0xff],
          enabled: true,
        },
        {
          icon: browser.runtime.getURL("global.png"),
          popup: browser.runtime.getURL("global.html"),
          title: "global",
          badge: "global",
          badgeBackgroundColor: [0x11, 0x11, 0x11, 0xff],
          badgeTextColor: [0x99, 0x99, 0x99, 0xff],
        },
        {
          icon: browser.runtime.getURL("window.png"),
          popup: browser.runtime.getURL("window.html"),
          title: "window",
          badge: "window",
          badgeBackgroundColor: [0x22, 0x22, 0x22, 0xff],
          badgeTextColor: [0x88, 0x88, 0x88, 0xff],
        },
        {
          icon: browser.runtime.getURL("tab.png"),
          popup: browser.runtime.getURL("tab.html"),
          title: "tab",
          badge: "tab",
          badgeBackgroundColor: [0x33, 0x33, 0x33, 0xff],
          badgeTextColor: [0x77, 0x77, 0x77, 0xff],
        },
        {
          icon: defaultIcon,
          popup: "",
          title: "",
          badge: "",
          badgeBackgroundColor: [0x33, 0x33, 0x33, 0xff],
          badgeTextColor: [0x77, 0x77, 0x77, 0xff],
        },
        {
          icon: browser.runtime.getURL("global2.png"),
          popup: browser.runtime.getURL("global2.html"),
          title: "global2",
          badge: "global2",
          badgeBackgroundColor: [0x44, 0x44, 0x44, 0xff],
          badgeTextColor: [0x66, 0x66, 0x66, 0xff],
        },
      ];

      return [
        async expect => {
          browser.test.log("Initial state, expect default properties.");
          expect(null, null, null, details[0]);
        },
        async expect => {
          browser.test.log("Set global values, expect the new values.");
          browser.browserAction.setIcon({ path: "global.png" });
          browser.browserAction.setPopup({ popup: "global.html" });
          browser.browserAction.setTitle({ title: "global" });
          browser.browserAction.setBadgeText({ text: "global" });
          browser.browserAction.setBadgeBackgroundColor({ color: "#111" });
          browser.browserAction.setBadgeTextColor({ color: "#999" });
          expect(null, null, details[1], details[0]);
        },
        async expect => {
          browser.test.log("Set window values, expect the new values.");
          let windowId = windows[0];
          browser.browserAction.setIcon({ windowId, path: "window.png" });
          browser.browserAction.setPopup({ windowId, popup: "window.html" });
          browser.browserAction.setTitle({ windowId, title: "window" });
          browser.browserAction.setBadgeText({ windowId, text: "window" });
          browser.browserAction.setBadgeBackgroundColor({
            windowId,
            color: "#222",
          });
          browser.browserAction.setBadgeTextColor({ windowId, color: "#888" });
          expect(null, details[2], details[1], details[0]);
        },
        async expect => {
          browser.test.log("Set tab values, expect the new values.");
          let tabId = tabs[0];
          browser.browserAction.setIcon({ tabId, path: "tab.png" });
          browser.browserAction.setPopup({ tabId, popup: "tab.html" });
          browser.browserAction.setTitle({ tabId, title: "tab" });
          browser.browserAction.setBadgeText({ tabId, text: "tab" });
          browser.browserAction.setBadgeBackgroundColor({
            tabId,
            color: "#333",
          });
          browser.browserAction.setBadgeTextColor({ tabId, color: "#777" });
          expect(details[3], details[2], details[1], details[0]);
        },
        async expect => {
          browser.test.log(
            "Set empty tab values, expect empty values except for colors."
          );
          let tabId = tabs[0];
          browser.browserAction.setIcon({ tabId, path: "" });
          browser.browserAction.setPopup({ tabId, popup: "" });
          browser.browserAction.setTitle({ tabId, title: "" });
          browser.browserAction.setBadgeText({ tabId, text: "" });
          await browser.test.assertRejects(
            browser.browserAction.setBadgeBackgroundColor({ tabId, color: "" }),
            /^Invalid badge background color: ""$/,
            "Expected invalid badge background color error"
          );
          await browser.test.assertRejects(
            browser.browserAction.setBadgeTextColor({ tabId, color: "" }),
            /^Invalid badge text color: ""$/,
            "Expected invalid badge text color error"
          );
          expect(details[4], details[2], details[1], details[0]);
        },
        async expect => {
          browser.test.log("Remove tab values, expect window values.");
          let tabId = tabs[0];
          browser.browserAction.setIcon({ tabId, path: null });
          browser.browserAction.setPopup({ tabId, popup: null });
          browser.browserAction.setTitle({ tabId, title: null });
          browser.browserAction.setBadgeText({ tabId, text: null });
          browser.browserAction.setBadgeBackgroundColor({ tabId, color: null });
          browser.browserAction.setBadgeTextColor({ tabId, color: null });
          expect(null, details[2], details[1], details[0]);
        },
        async expect => {
          browser.test.log("Remove window values, expect global values.");
          let windowId = windows[0];
          browser.browserAction.setIcon({ windowId, path: null });
          browser.browserAction.setPopup({ windowId, popup: null });
          browser.browserAction.setTitle({ windowId, title: null });
          browser.browserAction.setBadgeText({ windowId, text: null });
          browser.browserAction.setBadgeBackgroundColor({
            windowId,
            color: null,
          });
          browser.browserAction.setBadgeTextColor({ windowId, color: null });
          expect(null, null, details[1], details[0]);
        },
        async expect => {
          browser.test.log("Change global values, expect the new values.");
          browser.browserAction.setIcon({ path: "global2.png" });
          browser.browserAction.setPopup({ popup: "global2.html" });
          browser.browserAction.setTitle({ title: "global2" });
          browser.browserAction.setBadgeText({ text: "global2" });
          browser.browserAction.setBadgeBackgroundColor({ color: "#444" });
          browser.browserAction.setBadgeTextColor({ color: "#666" });
          expect(null, null, details[5], details[0]);
        },
        async expect => {
          browser.test.log("Remove global values, expect defaults.");
          browser.browserAction.setIcon({ path: null });
          browser.browserAction.setPopup({ popup: null });
          browser.browserAction.setBadgeText({ text: null });
          browser.browserAction.setTitle({ title: null });
          browser.browserAction.setBadgeBackgroundColor({ color: null });
          browser.browserAction.setBadgeTextColor({ color: null });
          expect(null, null, null, details[0]);
        },
      ];
    },
  });
});

add_task(async function testMultipleWindows() {
  await runTests({
    manifest: {
      browser_action: {
        default_icon: "default.png",
        default_popup: "default.html",
        default_title: "Default Title",
        default_area: "navbar",
      },
    },

    files: {
      "default.png": imageBuffer,
      "window1.png": imageBuffer,
      "window2.png": imageBuffer,
    },

    getTests: function (tabs, windows) {
      let details = [
        {
          icon: browser.runtime.getURL("default.png"),
          popup: browser.runtime.getURL("default.html"),
          title: "Default Title",
          badge: "",
          badgeBackgroundColor: [0xd9, 0x00, 0x00, 0xff],
          badgeTextColor: [0xff, 0xff, 0xff, 0xff],
          enabled: true,
        },
        {
          icon: browser.runtime.getURL("window1.png"),
          popup: browser.runtime.getURL("window1.html"),
          title: "window1",
          badge: "w1",
          badgeBackgroundColor: [0x11, 0x11, 0x11, 0xff],
          badgeTextColor: [0x99, 0x99, 0x99, 0xff],
        },
        {
          icon: browser.runtime.getURL("window2.png"),
          popup: browser.runtime.getURL("window2.html"),
          title: "window2",
          badge: "w2",
          badgeBackgroundColor: [0x22, 0x22, 0x22, 0xff],
          badgeTextColor: [0x88, 0x88, 0x88, 0xff],
        },
        { title: "tab" },
      ];

      return [
        async expect => {
          browser.test.log("Initial state, expect default properties.");
          expect(null, null, null, details[0]);
        },
        async expect => {
          browser.test.log("Set window values, expect the new values.");
          let windowId = windows[0];
          browser.browserAction.setIcon({ windowId, path: "window1.png" });
          browser.browserAction.setPopup({ windowId, popup: "window1.html" });
          browser.browserAction.setTitle({ windowId, title: "window1" });
          browser.browserAction.setBadgeText({ windowId, text: "w1" });
          browser.browserAction.setBadgeBackgroundColor({
            windowId,
            color: "#111",
          });
          browser.browserAction.setBadgeTextColor({ windowId, color: "#999" });
          expect(null, details[1], null, details[0]);
        },
        async expect => {
          browser.test.log("Create a new tab, expect window values.");
          let tab = await browser.tabs.create({ active: true });
          tabs.push(tab.id);
          expect(null, details[1], null, details[0]);
        },
        async expect => {
          browser.test.log("Set a tab title, expect it.");
          await browser.browserAction.setTitle({
            tabId: tabs[1],
            title: "tab",
          });
          expect(details[3], details[1], null, details[0]);
        },
        async expect => {
          browser.test.log("Open a new window, expect default values.");
          let { id } = await browser.windows.create();
          windows.push(id);
          expect(null, null, null, details[0]);
        },
        async expect => {
          browser.test.log("Set window values, expect the new values.");
          let windowId = windows[1];
          browser.browserAction.setIcon({ windowId, path: "window2.png" });
          browser.browserAction.setPopup({ windowId, popup: "window2.html" });
          browser.browserAction.setTitle({ windowId, title: "window2" });
          browser.browserAction.setBadgeText({ windowId, text: "w2" });
          browser.browserAction.setBadgeBackgroundColor({
            windowId,
            color: "#222",
          });
          browser.browserAction.setBadgeTextColor({ windowId, color: "#888" });
          expect(null, details[2], null, details[0]);
        },
        async expect => {
          browser.test.log(
            "Move tab from old window to the new one. Tab-specific data" +
              " is preserved but inheritance is from the new window"
          );
          await browser.tabs.move(tabs[1], { windowId: windows[1], index: -1 });
          await browser.tabs.update(tabs[1], { active: true });
          expect(details[3], details[2], null, details[0]);
        },
        async expect => {
          browser.test.log("Close the initial tab of the new window.");
          let [{ id }] = await browser.tabs.query({
            windowId: windows[1],
            index: 0,
          });
          await browser.tabs.remove(id);
          expect(details[3], details[2], null, details[0]);
        },
        async expect => {
          browser.test.log(
            "Move the previous tab to a 3rd window, the 2nd one will close."
          );
          await browser.windows.create({ tabId: tabs[1] });
          expect(details[3], null, null, details[0]);
        },
        async expect => {
          browser.test.log("Close the tab, go back to the 1st window.");
          await browser.tabs.remove(tabs[1]);
          expect(null, details[1], null, details[0]);
        },
        async expect => {
          browser.test.log(
            "Assert failures for bad parameters. Expect no change"
          );

          let calls = {
            setIcon: { path: "default.png" },
            setPopup: { popup: "default.html" },
            setTitle: { title: "Default Title" },
            setBadgeText: { text: "" },
            setBadgeBackgroundColor: { color: [0xd9, 0x00, 0x00, 0xff] },
            setBadgeTextColor: { color: [0xff, 0xff, 0xff, 0xff] },
            getPopup: {},
            getTitle: {},
            getBadgeText: {},
            getBadgeBackgroundColor: {},
          };
          for (let [method, arg] of Object.entries(calls)) {
            browser.test.assertThrows(
              () => browser.browserAction[method]({ ...arg, windowId: -3 }),
              /-3 is too small \(must be at least -2\)/,
              method + " with invalid windowId"
            );
            await browser.test.assertRejects(
              browser.browserAction[method]({
                ...arg,
                tabId: tabs[0],
                windowId: windows[0],
              }),
              /Only one of tabId and windowId can be specified/,
              method + " with both tabId and windowId"
            );
          }

          expect(null, details[1], null, details[0]);
        },
      ];
    },
  });
});

add_task(async function testDefaultBadgeTextColor() {
  await runTests({
    manifest: {
      browser_action: {
        default_icon: "default.png",
        default_popup: "default.html",
        default_title: "Default Title",
        default_area: "navbar",
      },
    },

    files: {
      "default.png": imageBuffer,
      "window1.png": imageBuffer,
      "window2.png": imageBuffer,
    },

    getTests: function (tabs, windows) {
      let details = [
        {
          icon: browser.runtime.getURL("default.png"),
          popup: browser.runtime.getURL("default.html"),
          title: "Default Title",
          badge: "",
          badgeBackgroundColor: [0xd9, 0x00, 0x00, 0xff],
          badgeTextColor: [0xff, 0xff, 0xff, 0xff],
          enabled: true,
        },
        {
          badgeBackgroundColor: [0xff, 0xff, 0x00, 0xff],
          badgeTextColor: [0x00, 0x00, 0x00, 0xff],
        },
        {
          badgeBackgroundColor: [0x00, 0x00, 0xff, 0xff],
          badgeTextColor: [0xff, 0xff, 0xff, 0xff],
        },
        {
          badgeBackgroundColor: [0xff, 0xff, 0xff, 0x00],
          badgeTextColor: [0x00, 0x00, 0x00, 0xff],
        },
        {
          badgeBackgroundColor: [0x00, 0x00, 0xff, 0xff],
          badgeTextColor: [0xff, 0x00, 0xff, 0xff],
        },
        { badgeBackgroundColor: [0xff, 0xff, 0xff, 0x00] },
        {
          badgeBackgroundColor: [0x00, 0x00, 0x00, 0x00],
          badgeTextColor: [0xff, 0xff, 0xff, 0xff],
        },
      ];

      return [
        async expect => {
          browser.test.log("Initial state, expect default properties.");
          expect(null, null, null, details[0]);
        },
        async expect => {
          browser.test.log("Set a global light bgcolor, expect black text.");
          browser.browserAction.setBadgeBackgroundColor({ color: "#ff0" });
          expect(null, null, details[1], details[0]);
        },
        async expect => {
          browser.test.log(
            "Set a window-specific dark bgcolor, expect white text."
          );
          let windowId = windows[0];
          browser.browserAction.setBadgeBackgroundColor({
            windowId,
            color: "#00f",
          });
          expect(null, details[2], details[1], details[0]);
        },
        async expect => {
          browser.test.log(
            "Set a tab-specific transparent-white bgcolor, expect black text."
          );
          let tabId = tabs[0];
          browser.browserAction.setBadgeBackgroundColor({
            tabId,
            color: "#fff0",
          });
          expect(details[3], details[2], details[1], details[0]);
        },
        async expect => {
          browser.test.log(
            "Set a window-specific text color, expect it in the tab."
          );
          let windowId = windows[0];
          browser.browserAction.setBadgeTextColor({ windowId, color: "#f0f" });
          expect(details[5], details[4], details[1], details[0]);
        },
        async expect => {
          browser.test.log(
            "Remove the window-specific text color, expect black again."
          );
          let windowId = windows[0];
          browser.browserAction.setBadgeTextColor({ windowId, color: null });
          expect(details[3], details[2], details[1], details[0]);
        },
        async expect => {
          browser.test.log(
            "Set a tab-specific transparent-black bgcolor, expect white text."
          );
          let tabId = tabs[0];
          browser.browserAction.setBadgeBackgroundColor({
            tabId,
            color: "#0000",
          });
          expect(details[6], details[2], details[1], details[0]);
        },
      ];
    },
  });
});

add_task(async function testNavigationClearsData() {
  let url = "http://example.com/";
  let default_title = "Default title";
  let tab_title = "Tab title";

  const {
    Management: {
      global: { tabTracker },
    },
  } = ChromeUtils.importESModule("resource://gre/modules/Extension.sys.mjs");
  let extension,
    tabs = [];
  async function addTab(...args) {
    let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, ...args);
    tabs.push(tab);
    return tab;
  }
  async function sendMessage(method, param, expect, msg) {
    extension.sendMessage({ method, param, expect, msg });
    await extension.awaitMessage("done");
  }
  async function expectTabSpecificData(tab, msg) {
    let tabId = tabTracker.getId(tab);
    await sendMessage("getBadgeText", { tabId }, "foo", msg);
    await sendMessage("getTitle", { tabId }, tab_title, msg);
  }
  async function expectDefaultData(tab, msg) {
    let tabId = tabTracker.getId(tab);
    await sendMessage("getBadgeText", { tabId }, "", msg);
    await sendMessage("getTitle", { tabId }, default_title, msg);
  }
  async function setTabSpecificData(tab) {
    let tabId = tabTracker.getId(tab);
    await expectDefaultData(
      tab,
      "Expect default data before setting tab-specific data."
    );
    await sendMessage("setBadgeText", { tabId, text: "foo" });
    await sendMessage("setTitle", { tabId, title: tab_title });
    await expectTabSpecificData(
      tab,
      "Expect tab-specific data after setting it."
    );
  }

  info("Load a tab before installing the extension");
  let tab1 = await addTab(url, true, true);

  extension = ExtensionTestUtils.loadExtension({
    manifest: {
      browser_action: { default_title },
    },
    background: function () {
      browser.test.onMessage.addListener(
        async ({ method, param, expect, msg }) => {
          let result = await browser.browserAction[method](param);
          if (expect !== undefined) {
            browser.test.assertEq(expect, result, msg);
          }
          browser.test.sendMessage("done");
        }
      );
    },
  });
  await extension.startup();

  info("Set tab-specific data to the existing tab.");
  await setTabSpecificData(tab1);

  info("Add a hash. Does not cause navigation.");
  await navigateTab(tab1, url + "#hash");
  await expectTabSpecificData(
    tab1,
    "Adding a hash does not clear tab-specific data"
  );

  info("Remove the hash. Causes navigation.");
  await navigateTab(tab1, url);
  await expectDefaultData(tab1, "Removing hash clears tab-specific data");

  info("Open a new tab, set tab-specific data to it.");
  let tab2 = await addTab("about:newtab", false, false);
  await setTabSpecificData(tab2);

  info("Load a page in that tab.");
  await navigateTab(tab2, url);
  await expectDefaultData(tab2, "Loading a page clears tab-specific data.");

  info("Set tab-specific data.");
  await setTabSpecificData(tab2);

  info("Push history state. Does not cause navigation.");
  await historyPushState(tab2, url + "/path");
  await expectTabSpecificData(
    tab2,
    "history.pushState() does not clear tab-specific data"
  );

  info("Navigate when the tab is not selected");
  gBrowser.selectedTab = tab1;
  await navigateTab(tab2, url);
  await expectDefaultData(
    tab2,
    "Navigating clears tab-specific data, even when not selected."
  );

  for (let tab of tabs) {
    await BrowserTestUtils.removeTab(tab);
  }
  await extension.unload();
});