summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/browser/browser_html_list_view.js
blob: 527b8cfddce434523e360da83df54d63c262a850 (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
/* eslint max-len: ["error", 80] */

const { AddonTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/AddonTestUtils.sys.mjs"
);

AddonTestUtils.initMochitest(this);

let promptService;

const SUPPORT_URL = Services.urlFormatter.formatURL(
  Services.prefs.getStringPref("app.support.baseURL")
);
const REMOVE_SUMO_URL = SUPPORT_URL + "cant-remove-addon";

function getTestCards(root) {
  return root.querySelectorAll('addon-card[addon-id$="@mochi.test"]');
}

function getCardByAddonId(root, id) {
  return root.querySelector(`addon-card[addon-id="${id}"]`);
}

function isEmpty(el) {
  return !el.children.length;
}

function waitForThemeChange(list) {
  // Wait for two move events. One theme will be enabled and another disabled.
  let moveCount = 0;
  return BrowserTestUtils.waitForEvent(list, "move", () => ++moveCount == 2);
}

let mockProvider;

add_setup(async function () {
  mockProvider = new MockProvider(["extension", "sitepermission"]);
  promptService = mockPromptService();
});

let extensionsCreated = 0;

function createExtensions(manifestExtras) {
  return manifestExtras.map(extra =>
    ExtensionTestUtils.loadExtension({
      manifest: {
        name: "Test extension",
        browser_specific_settings: {
          gecko: { id: `test-${extensionsCreated++}@mochi.test` },
        },
        icons: {
          32: "test-icon.png",
        },
        ...extra,
      },
      useAddonManager: "temporary",
    })
  );
}

add_task(async function testExtensionList() {
  let id = "test@mochi.test";
  let headingId = "test_mochi_test-heading";
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      name: "Test extension",
      browser_specific_settings: { gecko: { id } },
      icons: {
        32: "test-icon.png",
      },
    },
    useAddonManager: "temporary",
  });
  await extension.startup();

  let addon = await AddonManager.getAddonByID(id);
  ok(addon, "The add-on can be found");

  let win = await loadInitialView("extension");
  let doc = win.document;

  // Find the addon-list to listen for events.
  let list = doc.querySelector("addon-list");

  // There shouldn't be any disabled extensions.
  let disabledSection = getSection(doc, "extension-disabled-section");
  ok(isEmpty(disabledSection), "The disabled section is empty");

  // The loaded extension should be in the enabled list.
  let enabledSection = getSection(doc, "extension-enabled-section");
  ok(
    enabledSection && !isEmpty(enabledSection),
    "The enabled section isn't empty"
  );
  let card = getCardByAddonId(enabledSection, id);
  ok(card, "The card is in the enabled section");

  // Check the properties of the card.
  is(card.addonNameEl.textContent, "Test extension", "The name is set");
  is(
    card.querySelector("h3").id,
    headingId,
    "The add-on name has the correct id"
  );
  is(
    card.querySelector(".card").getAttribute("aria-labelledby"),
    headingId,
    "The card is labelled by the heading"
  );
  let icon = card.querySelector(".addon-icon");
  ok(icon.src.endsWith("/test-icon.png"), "The icon is set");

  // Disable the extension.
  let disableToggle = card.querySelector('[action="toggle-disabled"]');
  ok(disableToggle.pressed, "The disable toggle is pressed");
  is(
    doc.l10n.getAttributes(disableToggle).id,
    "extension-enable-addon-button-label",
    "The toggle has the enable label"
  );
  ok(disableToggle.getAttribute("aria-label"), "There's an aria-label");
  ok(!disableToggle.hidden, "The toggle is visible");

  let disabled = BrowserTestUtils.waitForEvent(list, "move");
  disableToggle.click();
  await disabled;
  is(
    card.parentNode,
    disabledSection,
    "The card is now in the disabled section"
  );

  // The disable button is now enabled.
  ok(!disableToggle.pressed, "The disable toggle is not pressed");
  is(
    doc.l10n.getAttributes(disableToggle).id,
    "extension-enable-addon-button-label",
    "The button has the same enable label"
  );
  ok(disableToggle.getAttribute("aria-label"), "There's an aria-label");

  // Remove the add-on.
  let removeButton = card.querySelector('[action="remove"]');
  is(
    doc.l10n.getAttributes(removeButton).id,
    "remove-addon-button",
    "The button has the remove label"
  );
  // There is a support link when the add-on isn't removeable, verify we don't
  // always include one.
  ok(!removeButton.querySelector("a"), "There isn't a link in the item");

  // Remove but cancel.
  let cancelled = BrowserTestUtils.waitForEvent(card, "remove-cancelled");
  removeButton.click();
  await cancelled;

  let removed = BrowserTestUtils.waitForEvent(list, "remove");
  // Tell the mock prompt service that the prompt was accepted.
  promptService._response = 0;
  removeButton.click();
  await removed;

  addon = await AddonManager.getAddonByID(id);
  ok(
    addon && !!(addon.pendingOperations & AddonManager.PENDING_UNINSTALL),
    "The addon is pending uninstall"
  );

  // Ensure that a pending uninstall bar has been created for the
  // pending uninstall extension, and pressing the undo button will
  // refresh the list and render a card to the re-enabled extension.
  assertHasPendingUninstalls(list, 1);
  assertHasPendingUninstallAddon(list, addon);

  // Add a second pending uninstall extension.
  info("Install a second test extension and wait for addon card rendered");
  let added = BrowserTestUtils.waitForEvent(list, "add");
  const extension2 = ExtensionTestUtils.loadExtension({
    manifest: {
      name: "Test extension 2",
      browser_specific_settings: { gecko: { id: "test-2@mochi.test" } },
      icons: {
        32: "test-icon.png",
      },
    },
    useAddonManager: "temporary",
  });
  await extension2.startup();

  await added;
  ok(
    getCardByAddonId(list, extension2.id),
    "Got a card added for the second extension"
  );

  info("Uninstall the second test extension and wait for addon card removed");
  removed = BrowserTestUtils.waitForEvent(list, "remove");
  const addon2 = await AddonManager.getAddonByID(extension2.id);
  addon2.uninstall(true);
  await removed;

  ok(
    !getCardByAddonId(list, extension2.id),
    "Addon card for the second extension removed"
  );

  assertHasPendingUninstalls(list, 2);
  assertHasPendingUninstallAddon(list, addon2);

  // Addon2 was enabled before entering the pending uninstall state,
  // wait for its startup after pressing undo.
  let addon2Started = AddonTestUtils.promiseWebExtensionStartup(addon2.id);
  await testUndoPendingUninstall(list, addon);
  await testUndoPendingUninstall(list, addon2);
  info("Wait for the second pending uninstal add-ons startup");
  await addon2Started;

  ok(
    getCardByAddonId(disabledSection, addon.id),
    "The card for the first extension is in the disabled section"
  );
  ok(
    getCardByAddonId(enabledSection, addon2.id),
    "The card for the second extension is in the enabled section"
  );

  await extension2.unload();
  await extension.unload();

  // Install a theme and verify that it is not listed in the pending
  // uninstall message bars while the list extensions view is loaded.
  const themeXpi = AddonTestUtils.createTempWebExtensionFile({
    manifest: {
      name: "My theme",
      browser_specific_settings: { gecko: { id: "theme@mochi.test" } },
      theme: {},
    },
  });
  const themeAddon = await AddonManager.installTemporaryAddon(themeXpi);
  // Leave it pending uninstall, the following assertions related to
  // the pending uninstall message bars will fail if the theme is listed.
  await themeAddon.uninstall(true);

  // Install a third addon to verify that is being fully removed once the
  // about:addons page is closed.
  const xpi = AddonTestUtils.createTempWebExtensionFile({
    manifest: {
      name: "Test extension 3",
      browser_specific_settings: { gecko: { id: "test-3@mochi.test" } },
      icons: {
        32: "test-icon.png",
      },
    },
  });

  added = BrowserTestUtils.waitForEvent(list, "add");
  const addon3 = await AddonManager.installTemporaryAddon(xpi);
  await added;
  ok(
    getCardByAddonId(list, addon3.id),
    "Addon card for the third extension added"
  );

  removed = BrowserTestUtils.waitForEvent(list, "remove");
  addon3.uninstall(true);
  await removed;
  ok(
    !getCardByAddonId(list, addon3.id),
    "Addon card for the third extension removed"
  );

  assertHasPendingUninstalls(list, 1);
  ok(
    addon3 && !!(addon3.pendingOperations & AddonManager.PENDING_UNINSTALL),
    "The third addon is pending uninstall"
  );

  await closeView(win);

  ok(
    !(await AddonManager.getAddonByID(addon3.id)),
    "The third addon has been fully uninstalled"
  );

  ok(
    themeAddon.pendingOperations & AddonManager.PENDING_UNINSTALL,
    "The theme addon is pending after the list extension view is closed"
  );

  await themeAddon.uninstall();

  ok(
    !(await AddonManager.getAddonByID(themeAddon.id)),
    "The theme addon is fully uninstalled"
  );
});

add_task(async function testMouseSupport() {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      name: "Test extension",
      browser_specific_settings: { gecko: { id: "test@mochi.test" } },
    },
    useAddonManager: "temporary",
  });
  await extension.startup();

  let win = await loadInitialView("extension");
  let doc = win.document;

  let [card] = getTestCards(doc);
  is(card.addon.id, "test@mochi.test", "The right card is found");

  let panel = card.querySelector("panel-list");

  ok(!panel.open, "The panel is initially closed");
  await BrowserTestUtils.synthesizeMouseAtCenter(
    "addon-card[addon-id$='@mochi.test'] button[action='more-options']",
    { type: "mousedown" },
    win.docShell.browsingContext
  );
  ok(panel.open, "The panel is now open");

  await closeView(win);
  await extension.unload();
});

add_task(async function testKeyboardSupport() {
  let id = "test@mochi.test";
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      name: "Test extension",
      browser_specific_settings: { gecko: { id } },
    },
    useAddonManager: "temporary",
  });
  await extension.startup();

  let win = await loadInitialView("extension");
  let doc = win.document;

  // Some helpers.
  let tab = event => EventUtils.synthesizeKey("VK_TAB", event);
  let space = () => EventUtils.synthesizeKey(" ", {});
  let isFocused = (el, msg) => is(doc.activeElement, el, msg);

  // Find the addon-list to listen for events.
  let list = doc.querySelector("addon-list");
  let enabledSection = getSection(doc, "extension-enabled-section");
  let disabledSection = getSection(doc, "extension-disabled-section");

  // Find the card.
  let [card] = getTestCards(list);
  is(card.addon.id, "test@mochi.test", "The right card is found");

  // Focus the more options menu button.
  let moreOptionsButton = card.querySelector('[action="more-options"]');
  moreOptionsButton.focus();
  isFocused(moreOptionsButton, "The more options button is focused");

  // Test opening and closing the menu.
  let moreOptionsMenu = card.querySelector("panel-list");
  let expandButton = moreOptionsMenu.querySelector('[action="expand"]');
  let removeButton = card.querySelector('[action="remove"]');
  is(moreOptionsMenu.open, false, "The menu is closed");
  let shown = BrowserTestUtils.waitForEvent(moreOptionsMenu, "shown");
  space();
  await shown;
  is(moreOptionsMenu.open, true, "The menu is open");
  isFocused(removeButton, "The remove button is now focused");
  tab({ shiftKey: true });
  is(moreOptionsMenu.open, true, "The menu stays open");
  isFocused(expandButton, "The focus has looped to the bottom");
  tab();
  is(moreOptionsMenu.open, true, "The menu stays open");
  isFocused(removeButton, "The focus has looped to the top");

  let hidden = BrowserTestUtils.waitForEvent(moreOptionsMenu, "hidden");
  EventUtils.synthesizeKey("Escape", {});
  await hidden;
  isFocused(moreOptionsButton, "Escape closed the menu");

  // Disable the add-on.
  let disableButton = card.querySelector('[action="toggle-disabled"]');
  tab({ shiftKey: true });
  isFocused(disableButton, "The disable toggle is focused");
  is(card.parentNode, enabledSection, "The card is in the enabled section");
  space();
  // Wait for the add-on state to change.
  let [disabledAddon] = await AddonTestUtils.promiseAddonEvent("onDisabled");
  is(disabledAddon.id, id, "The right add-on was disabled");
  is(
    card.parentNode,
    enabledSection,
    "The card is still in the enabled section"
  );
  isFocused(disableButton, "The disable button is still focused");
  let moved = BrowserTestUtils.waitForEvent(list, "move");
  // Click outside the list to clear any focus.
  EventUtils.synthesizeMouseAtCenter(
    doc.querySelector(".header-name"),
    {},
    win
  );
  await moved;
  is(
    card.parentNode,
    disabledSection,
    "The card moved when keyboard focus left the list"
  );

  // Remove the add-on.
  moreOptionsButton.focus();
  shown = BrowserTestUtils.waitForEvent(moreOptionsMenu, "shown");
  space();
  is(moreOptionsMenu.open, true, "The menu is open");
  await shown;
  isFocused(removeButton, "The remove button is focused");
  let removed = BrowserTestUtils.waitForEvent(list, "remove");
  space();
  await removed;
  is(card.parentNode, null, "The card is no longer on the page");

  await extension.unload();
  await closeView(win);
});

add_task(async function testOpenDetailFromNameKeyboard() {
  let id = "details@mochi.test";
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      name: "Detail extension",
      browser_specific_settings: { gecko: { id } },
    },
    useAddonManager: "temporary",
  });
  await extension.startup();

  let win = await loadInitialView("extension");

  let card = getCardByAddonId(win.document, id);

  info("focus the add-on's name, which should be an <a>");
  card.addonNameEl.focus();

  let detailsLoaded = waitForViewLoad(win);
  EventUtils.synthesizeKey("KEY_Enter", {}, win);
  await detailsLoaded;

  card = getCardByAddonId(win.document, id);
  is(
    card.addonNameEl.textContent,
    "Detail extension",
    "The right detail view is laoded"
  );

  await extension.unload();
  await closeView(win);
});

add_task(async function testExtensionReordering() {
  let extensions = createExtensions([
    { name: "Extension One" },
    { name: "This is last" },
    { name: "An extension, is first" },
  ]);

  await Promise.all(extensions.map(extension => extension.startup()));

  let win = await loadInitialView("extension");
  let doc = win.document;

  // Get a reference to the addon-list for events.
  let list = doc.querySelector("addon-list");

  // Find the related cards, they should all have @mochi.test ids.
  let enabledSection = getSection(doc, "extension-enabled-section");
  let cards = getTestCards(enabledSection);

  is(cards.length, 3, "Each extension has an addon-card");

  let order = Array.from(cards).map(card => card.addon.name);
  Assert.deepEqual(
    order,
    ["An extension, is first", "Extension One", "This is last"],
    "The add-ons are sorted by name"
  );

  // Disable the second extension.
  let disabledSection = getSection(doc, "extension-disabled-section");
  ok(isEmpty(disabledSection), "The disabled section is initially empty");

  // Disable the add-ons in a different order.
  let reorderedCards = [cards[1], cards[0], cards[2]];
  for (let { addon } of reorderedCards) {
    let moved = BrowserTestUtils.waitForEvent(list, "move");
    await addon.disable();
    await moved;
  }

  order = Array.from(getTestCards(disabledSection)).map(
    card => card.addon.name
  );
  Assert.deepEqual(
    order,
    ["An extension, is first", "Extension One", "This is last"],
    "The add-ons are sorted by name"
  );

  // All of our installed add-ons are disabled, install a new one.
  let [newExtension] = createExtensions([{ name: "Extension New" }]);
  let added = BrowserTestUtils.waitForEvent(list, "add");
  await newExtension.startup();
  await added;

  let [newCard] = getTestCards(enabledSection);
  is(
    newCard.addon.name,
    "Extension New",
    "The new add-on is in the enabled list"
  );

  // Enable everything again.
  for (let { addon } of cards) {
    let moved = BrowserTestUtils.waitForEvent(list, "move");
    await addon.enable();
    await moved;
  }

  order = Array.from(getTestCards(enabledSection)).map(card => card.addon.name);
  Assert.deepEqual(
    order,
    [
      "An extension, is first",
      "Extension New",
      "Extension One",
      "This is last",
    ],
    "The add-ons are sorted by name"
  );

  // Remove the new extension.
  let removed = BrowserTestUtils.waitForEvent(list, "remove");
  await newExtension.unload();
  await removed;
  is(newCard.parentNode, null, "The new card has been removed");

  await Promise.all(extensions.map(extension => extension.unload()));
  await closeView(win);
});

add_task(async function testThemeList() {
  let theme = ExtensionTestUtils.loadExtension({
    manifest: {
      browser_specific_settings: { gecko: { id: "theme@mochi.test" } },
      name: "My theme",
      theme: {},
    },
    useAddonManager: "temporary",
  });

  let win = await loadInitialView("theme");
  let doc = win.document;

  let list = doc.querySelector("addon-list");

  let cards = getTestCards(list);
  is(cards.length, 0, "There are no test themes to start");

  let added = BrowserTestUtils.waitForEvent(list, "add");
  await theme.startup();
  await added;

  cards = getTestCards(list);
  is(cards.length, 1, "There is now one custom theme");

  let [card] = cards;
  is(card.addon.name, "My theme", "The card is for the test theme");

  let enabledSection = getSection(doc, "theme-enabled-section");
  let disabledSection = getSection(doc, "theme-disabled-section");

  await TestUtils.waitForCondition(
    () => enabledSection.querySelectorAll("addon-card").length == 1
  );

  is(
    card.parentNode,
    enabledSection,
    "The new theme card is in the enabled section"
  );
  is(
    enabledSection.querySelectorAll("addon-card").length,
    1,
    "There is one enabled theme"
  );

  let toggleThemeEnabled = async () => {
    let themesChanged = waitForThemeChange(list);
    card.querySelector('[action="toggle-disabled"]').click();
    await themesChanged;

    await TestUtils.waitForCondition(
      () => enabledSection.querySelectorAll("addon-card").length == 1
    );
  };

  await toggleThemeEnabled();

  is(
    card.parentNode,
    disabledSection,
    "The card is now in the disabled section"
  );
  is(
    enabledSection.querySelectorAll("addon-card").length,
    1,
    "There is one enabled theme"
  );

  // Re-enable the theme.
  await toggleThemeEnabled();
  is(card.parentNode, enabledSection, "Card is back in the Enabled section");

  // Remove theme and verify that the default theme is re-enabled.
  let removed = BrowserTestUtils.waitForEvent(list, "remove");
  // Confirm removal.
  promptService._response = 0;
  card.querySelector('[action="remove"]').click();
  await removed;
  is(card.parentNode, null, "Card has been removed from the view");
  await TestUtils.waitForCondition(
    () => enabledSection.querySelectorAll("addon-card").length == 1
  );

  let defaultTheme = getCardByAddonId(doc, "default-theme@mozilla.org");
  is(defaultTheme.parentNode, enabledSection, "The default theme is reenabled");

  await testUndoPendingUninstall(list, card.addon);
  await TestUtils.waitForCondition(
    () => enabledSection.querySelectorAll("addon-card").length == 1
  );
  is(defaultTheme.parentNode, disabledSection, "The default theme is disabled");
  ok(getCardByAddonId(enabledSection, theme.id), "Theme should be reenabled");

  await theme.unload();
  await closeView(win);
});

add_task(async function testBuiltInThemeButtons() {
  let win = await loadInitialView("theme");
  let doc = win.document;

  // Find the addon-list to listen for events.
  let list = doc.querySelector("addon-list");
  let enabledSection = getSection(doc, "theme-enabled-section");
  let disabledSection = getSection(doc, "theme-disabled-section");

  let defaultTheme = getCardByAddonId(doc, "default-theme@mozilla.org");
  let darkTheme = getCardByAddonId(doc, "firefox-compact-dark@mozilla.org");

  // Check that themes are in the expected spots.
  is(defaultTheme.parentNode, enabledSection, "The default theme is enabled");
  is(darkTheme.parentNode, disabledSection, "The dark theme is disabled");

  // The default theme shouldn't have remove or disable options.
  let defaultButtons = {
    toggleDisabled: defaultTheme.querySelector('[action="toggle-disabled"]'),
    remove: defaultTheme.querySelector('[action="remove"]'),
  };
  is(defaultButtons.toggleDisabled.hidden, true, "Disable is hidden");
  is(defaultButtons.remove.hidden, true, "Remove is hidden");

  // The dark theme should have an enable button, but not remove.
  let darkButtons = {
    toggleDisabled: darkTheme.querySelector('[action="toggle-disabled"]'),
    remove: darkTheme.querySelector('[action="remove"]'),
  };
  is(darkButtons.toggleDisabled.hidden, false, "Enable is visible");
  is(darkButtons.remove.hidden, true, "Remove is hidden");

  // Enable the dark theme and check the buttons again.
  let themesChanged = waitForThemeChange(list);
  darkButtons.toggleDisabled.click();
  await themesChanged;

  await TestUtils.waitForCondition(
    () => enabledSection.querySelectorAll("addon-card").length == 1
  );

  // Check the buttons.
  is(defaultButtons.toggleDisabled.hidden, false, "Enable is visible");
  is(defaultButtons.remove.hidden, true, "Remove is hidden");
  is(darkButtons.toggleDisabled.hidden, false, "Disable is visible");
  is(darkButtons.remove.hidden, true, "Remove is hidden");

  // Disable the dark theme.
  themesChanged = waitForThemeChange(list);
  darkButtons.toggleDisabled.click();
  await themesChanged;

  await TestUtils.waitForCondition(
    () => enabledSection.querySelectorAll("addon-card").length == 1
  );

  // The themes are back to their starting posititons.
  is(defaultTheme.parentNode, enabledSection, "Default is enabled");
  is(darkTheme.parentNode, disabledSection, "Dark is disabled");

  await closeView(win);
});

add_task(async function testSideloadRemoveButton() {
  const id = "sideload@mochi.test";
  mockProvider.createAddons([
    {
      id,
      name: "Sideloaded",
      permissions: 0,
    },
  ]);

  let win = await loadInitialView("extension");
  let doc = win.document;

  let card = getCardByAddonId(doc, id);

  let moreOptionsPanel = card.querySelector("panel-list");
  let moreOptionsButton = card.querySelector('[action="more-options"]');
  let panelOpened = BrowserTestUtils.waitForEvent(moreOptionsPanel, "shown");
  EventUtils.synthesizeMouseAtCenter(moreOptionsButton, {}, win);
  await panelOpened;

  // Verify the remove button is visible with a SUMO link.
  let removeButton = card.querySelector('[action="remove"]');
  ok(removeButton.disabled, "Remove is disabled");
  ok(!removeButton.hidden, "Remove is visible");

  // Remove but cancel.
  let prevented = BrowserTestUtils.waitForEvent(card, "remove-disabled");
  removeButton.click();
  await prevented;

  // reopen the panel
  panelOpened = BrowserTestUtils.waitForEvent(moreOptionsPanel, "shown");
  EventUtils.synthesizeMouseAtCenter(moreOptionsButton, {}, win);
  await panelOpened;

  let sumoLink = removeButton.querySelector("a");
  ok(sumoLink, "There's a link");
  is(
    doc.l10n.getAttributes(removeButton).id,
    "remove-addon-disabled-button",
    "The can't remove text is shown"
  );
  sumoLink.focus();
  is(doc.activeElement, sumoLink, "The link can be focused");

  let newTabOpened = BrowserTestUtils.waitForNewTab(gBrowser, REMOVE_SUMO_URL);
  sumoLink.click();
  BrowserTestUtils.removeTab(await newTabOpened);

  await closeView(win);
});

add_task(async function testOnlyTypeIsShown() {
  let win = await loadInitialView("theme");
  let doc = win.document;

  // Find the addon-list to listen for events.
  let list = doc.querySelector("addon-list");

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      name: "Test extension",
      browser_specific_settings: { gecko: { id: "test@mochi.test" } },
    },
    useAddonManager: "temporary",
  });

  let skipped = BrowserTestUtils.waitForEvent(
    list,
    "skip-add",
    e => e.detail == "type-mismatch"
  );
  await extension.startup();
  await skipped;

  let cards = getTestCards(list);
  is(cards.length, 0, "There are no test extension cards");

  await extension.unload();
  await closeView(win);
});

add_task(async function testPluginIcons() {
  const pluginIconUrl = "chrome://global/skin/icons/plugin.svg";

  let win = await loadInitialView("plugin");
  let doc = win.document;

  // Check that the icons are set to the plugin icon.
  let icons = doc.querySelectorAll(".card-heading-icon");
  ok(!!icons.length, "There are some plugins listed");

  for (let icon of icons) {
    is(icon.src, pluginIconUrl, "Plugins use the plugin icon");
  }

  await closeView(win);
});

add_task(async function testExtensionGenericIcon() {
  const extensionIconUrl =
    "chrome://mozapps/skin/extensions/extensionGeneric.svg";

  let id = "test@mochi.test";
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      name: "Test extension",
      browser_specific_settings: { gecko: { id } },
    },
    useAddonManager: "temporary",
  });
  await extension.startup();

  let win = await loadInitialView("extension");
  let doc = win.document;

  let card = getCardByAddonId(doc, id);
  let icon = card.querySelector(".addon-icon");
  is(icon.src, extensionIconUrl, "Extensions without icon use the generic one");

  await extension.unload();
  await closeView(win);
});

add_task(async function testSectionHeadingKeys() {
  mockProvider.createAddons([
    {
      id: "test-theme",
      name: "Test Theme",
      type: "theme",
    },
    {
      id: "test-extension-disabled",
      name: "Test Disabled Extension",
      type: "extension",
      userDisabled: true,
    },
    {
      id: "test-plugin-disabled",
      name: "Test Disabled Plugin",
      type: "plugin",
      userDisabled: true,
    },
    {
      id: "test-locale",
      name: "Test Enabled Locale",
      type: "locale",
    },
    {
      id: "test-locale-disabled",
      name: "Test Disabled Locale",
      type: "locale",
      userDisabled: true,
    },
    {
      id: "test-dictionary",
      name: "Test Enabled Dictionary",
      type: "dictionary",
    },
    {
      id: "test-dictionary-disabled",
      name: "Test Disabled Dictionary",
      type: "dictionary",
      userDisabled: true,
    },
    {
      id: "test-sitepermission",
      name: "Test Enabled Site Permission",
      type: "sitepermission",
    },
    {
      id: "test-sitepermission-disabled",
      name: "Test Disabled Site Permission",
      type: "sitepermission",
      userDisabled: true,
    },
  ]);

  for (let type of [
    "extension",
    "theme",
    "plugin",
    "locale",
    "dictionary",
    "sitepermission",
  ]) {
    info(`loading view for addon type ${type}`);
    let win = await loadInitialView(type);
    let doc = win.document;

    for (let status of ["enabled", "disabled"]) {
      let section = getSection(doc, `${type}-${status}-section`);
      let el = section?.querySelector(".list-section-heading");
      isnot(el, null, `Should have ${status} heading for ${type} section`);
      is(
        el && doc.l10n.getAttributes(el).id,
        win.getL10nIdMapping(`${type}-${status}-heading`),
        `Should have correct ${status} heading for ${type} section`
      );
    }

    await closeView(win);
  }
});

add_task(async function testDisabledDimming() {
  const id = "disabled@mochi.test";
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      name: "Disable me",
      browser_specific_settings: { gecko: { id } },
    },
    useAddonManager: "temporary",
  });
  await extension.startup();

  let addon = await AddonManager.getAddonByID(id);

  let win = await loadInitialView("extension");
  let doc = win.document;
  let pageHeader = doc.querySelector("addon-page-header");

  // Ensure there's no focus on the list.
  EventUtils.synthesizeMouseAtCenter(pageHeader, {}, win);

  const checkOpacity = (card, expected, msg) => {
    let { opacity } = card.ownerGlobal.getComputedStyle(card.firstElementChild);
    let normalize = val => Math.floor(val * 10);
    is(normalize(opacity), normalize(expected), msg);
  };
  const waitForTransition = card =>
    BrowserTestUtils.waitForEvent(
      card.firstElementChild,
      "transitionend",
      e => e.propertyName === "opacity" && e.target.classList.contains("card")
    );

  let card = getCardByAddonId(doc, id);
  checkOpacity(card, "1", "The opacity is 1 when enabled");

  // Disable the add-on, check again.
  let list = doc.querySelector("addon-list");
  let moved = BrowserTestUtils.waitForEvent(list, "move");
  await addon.disable();
  await moved;

  let disabledSection = getSection(doc, "extension-disabled-section");
  is(card.parentNode, disabledSection, "The card is in the disabled section");
  checkOpacity(card, "0.6", "The opacity is dimmed when disabled");

  // Click on the menu button, this should un-dim the card.
  let transitionEnded = waitForTransition(card);
  let moreOptionsButton = card.querySelector(".more-options-button");
  EventUtils.synthesizeMouseAtCenter(moreOptionsButton, {}, win);
  await transitionEnded;
  checkOpacity(card, "1", "The opacity is 1 when the menu is open");

  // Close the menu, opacity should return.
  transitionEnded = waitForTransition(card);
  EventUtils.synthesizeMouseAtCenter(pageHeader, {}, win);
  await transitionEnded;
  checkOpacity(card, "0.6", "The card is dimmed again");

  await closeView(win);
  await extension.unload();
});

add_task(async function testEmptyMessage() {
  let tests = [
    {
      type: "extension",
      message: "Get extensions and themes on ",
    },
    {
      type: "theme",
      message: "Get extensions and themes on ",
    },
    {
      type: "plugin",
      message: "Get extensions and themes on ",
    },
    {
      type: "locale",
      message: "Get language packs on ",
    },
    {
      type: "dictionary",
      message: "Get dictionaries on ",
    },
  ];

  for (let test of tests) {
    let win = await loadInitialView(test.type);
    let doc = win.document;
    let enabledSection = getSection(doc, `${test.type}-enabled-section`);
    let disabledSection = getSection(doc, `${test.type}-disabled-section`);
    const message = doc.querySelector("#empty-addons-message");

    // Test if the correct locale has been applied.
    ok(
      message.textContent.startsWith(test.message),
      `View ${test.type} has correct empty list message`
    );

    // With at least one enabled/disabled add-on (see testSectionHeadingKeys),
    // the message is hidden.
    is_element_hidden(message, "Empty addons message hidden");

    // The test runner (Mochitest) relies on add-ons that should not be removed.
    // Simulate the scenario of zero add-ons by clearing all rendered sections.
    while (enabledSection.firstChild) {
      enabledSection.firstChild.remove();
    }

    while (disabledSection.firstChild) {
      disabledSection.firstChild.remove();
    }

    // Message should now be displayed
    is_element_visible(message, "Empty addons message visible");

    await closeView(win);
  }
});