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

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

AddonTestUtils.initMochitest(this);

const server = AddonTestUtils.createHttpServer();

const initialAutoUpdate = AddonManager.autoUpdateDefault;
registerCleanupFunction(() => {
  AddonManager.autoUpdateDefault = initialAutoUpdate;
});

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [["extensions.checkUpdateSecurity", false]],
  });

  Services.telemetry.clearEvents();
  registerCleanupFunction(() => {
    cleanupPendingNotifications();
  });
});

function loadDetailView(win, id) {
  let doc = win.document;
  let card = doc.querySelector(`addon-card[addon-id="${id}"]`);
  let loaded = waitForViewLoad(win);
  EventUtils.synthesizeMouseAtCenter(card, { clickCount: 1 }, win);
  return loaded;
}

add_task(async function testChangeAutoUpdates() {
  let id = "test@mochi.test";
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      name: "Test",
      browser_specific_settings: { gecko: { id } },
    },
    // Use permanent so the add-on can be updated.
    useAddonManager: "permanent",
  });

  await extension.startup();
  let addon = await AddonManager.getAddonByID(id);

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

  let getInputs = updateRow => ({
    default: updatesRow.querySelector('input[value="1"]'),
    on: updatesRow.querySelector('input[value="2"]'),
    off: updatesRow.querySelector('input[value="0"]'),
    checkForUpdate: updatesRow.querySelector('[action="update-check"]'),
  });

  await loadDetailView(win, id);

  let card = doc.querySelector(`addon-card[addon-id="${id}"]`);
  ok(card.querySelector("addon-details"), "The card now has details");

  let updatesRow = card.querySelector(".addon-detail-row-updates");
  let inputs = getInputs(updatesRow);
  is(addon.applyBackgroundUpdates, 1, "Default is set");
  ok(inputs.default.checked, "The default option is selected");
  ok(inputs.checkForUpdate.hidden, "Update check is hidden");

  inputs.on.click();
  is(addon.applyBackgroundUpdates, "2", "Updates are now enabled");
  ok(inputs.on.checked, "The on option is selected");
  ok(inputs.checkForUpdate.hidden, "Update check is hidden");

  inputs.off.click();
  is(addon.applyBackgroundUpdates, "0", "Updates are now disabled");
  ok(inputs.off.checked, "The off option is selected");
  ok(!inputs.checkForUpdate.hidden, "Update check is visible");

  // Go back to the list view and check the details view again.
  let loaded = waitForViewLoad(win);
  doc.querySelector(".back-button").click();
  await loaded;

  // Load the detail view again.
  await loadDetailView(win, id);

  card = doc.querySelector(`addon-card[addon-id="${id}"]`);
  updatesRow = card.querySelector(".addon-detail-row-updates");
  inputs = getInputs(updatesRow);

  ok(inputs.off.checked, "Off is still selected");

  // Disable global updates.
  let updated = BrowserTestUtils.waitForEvent(card, "update");
  AddonManager.autoUpdateDefault = false;
  await updated;

  // Updates are still the same.
  is(addon.applyBackgroundUpdates, "0", "Updates are now disabled");
  ok(inputs.off.checked, "The off option is selected");
  ok(!inputs.checkForUpdate.hidden, "Update check is visible");

  // Check default.
  inputs.default.click();
  is(addon.applyBackgroundUpdates, "1", "Default is set");
  ok(inputs.default.checked, "The default option is selected");
  ok(!inputs.checkForUpdate.hidden, "Update check is visible");

  inputs.on.click();
  is(addon.applyBackgroundUpdates, "2", "Updates are now enabled");
  ok(inputs.on.checked, "The on option is selected");
  ok(inputs.checkForUpdate.hidden, "Update check is hidden");

  // Enable updates again.
  updated = BrowserTestUtils.waitForEvent(card, "update");
  AddonManager.autoUpdateDefault = true;
  await updated;

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

async function setupExtensionWithUpdate(
  id,
  { releaseNotes, cancelUpdate } = {}
) {
  let serverHost = `http://localhost:${server.identity.primaryPort}`;
  let updatesPath = `/ext-updates-${id}.json`;

  let baseManifest = {
    name: "Updates",
    icons: { 48: "an-icon.png" },
    browser_specific_settings: {
      gecko: {
        id,
        update_url: serverHost + updatesPath,
      },
    },
  };

  let updateXpi = AddonTestUtils.createTempWebExtensionFile({
    manifest: {
      ...baseManifest,
      version: "2",
      // Include a permission in the updated extension, to make
      // sure that we trigger the permission prompt as expected
      // (and that we can accept or cancel the update by observing
      // the underlying observerService notification).
      permissions: ["http://*.example.com/*"],
    },
  });

  let releaseNotesExtra = {};
  if (releaseNotes) {
    let notesPath = "/notes.txt";
    server.registerPathHandler(notesPath, (request, response) => {
      if (releaseNotes == "ERROR") {
        response.setStatusLine(null, 404, "Not Found");
      } else {
        response.setStatusLine(null, 200, "OK");
        response.write(releaseNotes);
      }
      response.processAsync();
      response.finish();
    });
    releaseNotesExtra.update_info_url = serverHost + notesPath;
  }

  let xpiFilename = `/update-${id}.xpi`;
  server.registerFile(xpiFilename, updateXpi);
  AddonTestUtils.registerJSON(server, updatesPath, {
    addons: {
      [id]: {
        updates: [
          {
            version: "2",
            update_link: serverHost + xpiFilename,
            ...releaseNotesExtra,
          },
        ],
      },
    },
  });

  handlePermissionPrompt({ addonId: id, reject: cancelUpdate });

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      ...baseManifest,
      version: "1",
    },
    // Use permanent so the add-on can be updated.
    useAddonManager: "permanent",
  });
  await extension.startup();
  return extension;
}

function disableAutoUpdates(card) {
  // Check button should be hidden.
  let updateCheckButton = card.querySelector('button[action="update-check"]');
  ok(updateCheckButton.hidden, "The button is initially hidden");

  // Disable updates, update check button is now visible.
  card.querySelector('input[name="autoupdate"][value="0"]').click();
  ok(!updateCheckButton.hidden, "The button is now visible");

  // There shouldn't be an update shown to the user.
  assertUpdateState({ card, shown: false });
}

function checkForUpdate(card, expected) {
  let updateCheckButton = card.querySelector('button[action="update-check"]');
  let updateFound = BrowserTestUtils.waitForEvent(card, expected);
  updateCheckButton.click();
  return updateFound;
}

function installUpdate(card, expected) {
  // Install the update.
  let updateInstalled = BrowserTestUtils.waitForEvent(card, expected);
  let updated = BrowserTestUtils.waitForEvent(card, "update");
  card.querySelector('panel-item[action="install-update"]').click();
  return Promise.all([updateInstalled, updated]);
}

async function findUpdatesForAddonId(id) {
  let addon = await AddonManager.getAddonByID(id);
  await new Promise(resolve => {
    addon.findUpdates(
      { onUpdateAvailable: resolve },
      AddonManager.UPDATE_WHEN_USER_REQUESTED
    );
  });
}

function assertUpdateState({
  card,
  shown,
  expanded = true,
  releaseNotes = false,
}) {
  let menuButton = card.querySelector(".more-options-button");
  ok(
    menuButton.classList.contains("more-options-button-badged") == shown,
    "The menu button is badged"
  );
  let installButton = card.querySelector('panel-item[action="install-update"]');
  ok(
    installButton.hidden != shown,
    `The install button is ${shown ? "hidden" : "shown"}`
  );
  if (expanded) {
    let updateCheckButton = card.querySelector('button[action="update-check"]');
    ok(
      updateCheckButton.hidden == shown,
      `The update check button is ${shown ? "hidden" : "shown"}`
    );

    let { tabGroup } = card.details;
    is(tabGroup.hidden, false, "The tab group is shown");
    let notesBtn = tabGroup.querySelector('[name="release-notes"]');
    is(
      notesBtn.hidden,
      !releaseNotes,
      `The release notes button is ${releaseNotes ? "shown" : "hidden"}`
    );
  }
}

add_task(async function testUpdateAvailable() {
  let id = "update@mochi.test";
  let extension = await setupExtensionWithUpdate(id);

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

  await loadDetailView(win, id);

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

  // Disable updates and then check.
  disableAutoUpdates(card);
  await checkForUpdate(card, "update-found");

  // There should now be an update.
  assertUpdateState({ card, shown: true });

  // The version was 1.
  let versionRow = card.querySelector(".addon-detail-row-version");
  is(versionRow.lastChild.textContent, "1", "The version started as 1");

  await installUpdate(card, "update-installed");

  // The version is now 2.
  versionRow = card.querySelector(".addon-detail-row-version");
  is(versionRow.lastChild.textContent, "2", "The version has updated");

  // No update is shown again.
  assertUpdateState({ card, shown: false });

  // Check for updates again, there shouldn't be an update.
  await checkForUpdate(card, "no-update");

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

add_task(async function testReleaseNotesLoad() {
  Services.telemetry.clearEvents();
  let id = "update-with-notes@mochi.test";
  let extension = await setupExtensionWithUpdate(id, {
    releaseNotes: `
      <html xmlns="http://www.w3.org/1999/xhtml">
        <head><link rel="stylesheet" href="remove-me.css"/></head>
        <body>
          <script src="no-scripts.js"></script>
          <h1>My release notes</h1>
          <img src="http://example.com/tracker.png"/>
          <ul>
            <li onclick="alert('hi')">A thing</li>
          </ul>
          <a href="http://example.com/">Go somewhere</a>
        </body>
      </html>
    `,
  });

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

  await loadDetailView(win, id);

  let card = doc.querySelector("addon-card");
  let { deck, tabGroup } = card.details;

  // Disable updates and then check.
  disableAutoUpdates(card);
  await checkForUpdate(card, "update-found");

  // There should now be an update.
  assertUpdateState({ card, shown: true, releaseNotes: true });

  info("Check release notes");
  let notesBtn = tabGroup.querySelector('[name="release-notes"]');
  let notes = card.querySelector("update-release-notes");
  let loading = BrowserTestUtils.waitForEvent(notes, "release-notes-loading");
  let loaded = BrowserTestUtils.waitForEvent(notes, "release-notes-loaded");
  // Don't use notesBtn.click() since it causes an assertion to fail.
  // See bug 1551621 for more info.
  EventUtils.synthesizeMouseAtCenter(notesBtn, {}, win);
  await loading;
  is(
    doc.l10n.getAttributes(notes.firstElementChild).id,
    "release-notes-loading",
    "The loading message is shown"
  );
  await loaded;
  info("Checking HTML release notes");
  let [h1, ul, a] = notes.children;
  is(h1.tagName, "H1", "There's a heading");
  is(h1.textContent, "My release notes", "The heading has content");
  is(ul.tagName, "UL", "There's a list");
  is(ul.children.length, 1, "There's one item in the list");
  let [li] = ul.children;
  is(li.tagName, "LI", "There's a list item");
  is(li.textContent, "A thing", "The text is set");
  ok(!li.hasAttribute("onclick"), "The onclick was removed");
  ok(!notes.querySelector("link"), "The link tag was removed");
  ok(!notes.querySelector("script"), "The script tag was removed");
  is(a.textContent, "Go somewhere", "The link text is preserved");
  is(a.href, "http://example.com/", "The link href is preserved");

  info("Verify the link opened in a new tab");
  let tabOpened = BrowserTestUtils.waitForNewTab(gBrowser, a.href);
  a.click();
  let tab = await tabOpened;
  BrowserTestUtils.removeTab(tab);

  let originalContent = notes.innerHTML;

  info("Switch away and back to release notes");
  // Load details view.
  let detailsBtn = tabGroup.querySelector('.tab-button[name="details"]');
  let viewChanged = BrowserTestUtils.waitForEvent(deck, "view-changed");
  detailsBtn.click();
  await viewChanged;

  // Load release notes again, verify they weren't loaded.
  viewChanged = BrowserTestUtils.waitForEvent(deck, "view-changed");
  let notesCached = BrowserTestUtils.waitForEvent(
    notes,
    "release-notes-cached"
  );
  notesBtn.click();
  await viewChanged;
  await notesCached;
  is(notes.innerHTML, originalContent, "The content didn't change");

  info("Install the update to clean it up");
  await installUpdate(card, "update-installed");

  // There's no more update but release notes are still shown.
  assertUpdateState({ card, shown: false, releaseNotes: true });

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

add_task(async function testReleaseNotesError() {
  let id = "update-with-notes-error@mochi.test";
  let extension = await setupExtensionWithUpdate(id, { releaseNotes: "ERROR" });

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

  await loadDetailView(win, id);

  let card = doc.querySelector("addon-card");
  let { deck, tabGroup } = card.details;

  // Disable updates and then check.
  disableAutoUpdates(card);
  await checkForUpdate(card, "update-found");

  // There should now be an update.
  assertUpdateState({ card, shown: true, releaseNotes: true });

  info("Check release notes");
  let notesBtn = tabGroup.querySelector('[name="release-notes"]');
  let notes = card.querySelector("update-release-notes");
  let loading = BrowserTestUtils.waitForEvent(notes, "release-notes-loading");
  let errored = BrowserTestUtils.waitForEvent(notes, "release-notes-error");
  // Don't use notesBtn.click() since it causes an assertion to fail.
  // See bug 1551621 for more info.
  EventUtils.synthesizeMouseAtCenter(notesBtn, {}, win);
  await loading;
  is(
    doc.l10n.getAttributes(notes.firstElementChild).id,
    "release-notes-loading",
    "The loading message is shown"
  );
  await errored;
  is(
    doc.l10n.getAttributes(notes.firstElementChild).id,
    "release-notes-error",
    "The error message is shown"
  );

  info("Switch away and back to release notes");
  // Load details view.
  let detailsBtn = tabGroup.querySelector('.tab-button[name="details"]');
  let viewChanged = BrowserTestUtils.waitForEvent(deck, "view-changed");
  detailsBtn.click();
  await viewChanged;

  // Load release notes again, verify they weren't loaded.
  viewChanged = BrowserTestUtils.waitForEvent(deck, "view-changed");
  let notesCached = BrowserTestUtils.waitForEvent(
    notes,
    "release-notes-cached"
  );
  notesBtn.click();
  await viewChanged;
  await notesCached;

  info("Install the update to clean it up");
  await installUpdate(card, "update-installed");

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

add_task(async function testUpdateCancelled() {
  let id = "update@mochi.test";
  let extension = await setupExtensionWithUpdate(id, { cancelUpdate: true });

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

  await loadDetailView(win, "update@mochi.test");
  let card = doc.querySelector("addon-card");

  // Disable updates and then check.
  disableAutoUpdates(card);
  await checkForUpdate(card, "update-found");

  // There should now be an update.
  assertUpdateState({ card, shown: true });

  // The add-on starts as version 1.
  let versionRow = card.querySelector(".addon-detail-row-version");
  is(versionRow.lastChild.textContent, "1", "The version started as 1");

  // Force the install to be cancelled.
  let install = card.updateInstall;
  ok(install, "There was an install found");

  await installUpdate(card, "update-cancelled");

  // The add-on is still version 1.
  versionRow = card.querySelector(".addon-detail-row-version");
  is(versionRow.lastChild.textContent, "1", "The version hasn't changed");

  // The update has been removed.
  assertUpdateState({ card, shown: false });

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

add_task(async function testAvailableUpdates() {
  let ids = ["update1@mochi.test", "update2@mochi.test", "update3@mochi.test"];
  let addons = await Promise.all(ids.map(id => setupExtensionWithUpdate(id)));

  // Disable global add-on updates.
  AddonManager.autoUpdateDefault = false;

  let win = await loadInitialView("extension");
  let doc = win.document;
  let updatesMessage = doc.getElementById("updates-message");
  let categoryUtils = new CategoryUtilities(win);

  let availableCat = categoryUtils.get("available-updates");

  ok(availableCat.hidden, "Available updates is hidden");
  is(availableCat.badgeCount, 0, "There are no updates");
  ok(updatesMessage, "There is an updates message");
  is_element_hidden(updatesMessage, "The message is hidden");
  ok(!updatesMessage.message.textContent, "The message is empty");
  ok(!updatesMessage.button.textContent, "The button is empty");

  // Check for all updates.
  let updatesFound = TestUtils.topicObserved("EM-update-check-finished");
  doc.querySelector('#page-options [action="check-for-updates"]').click();

  is_element_visible(updatesMessage, "The message is visible");
  ok(!updatesMessage.message.textContent, "The message is empty");
  ok(updatesMessage.button.hidden, "The view updates button is hidden");

  // Make sure the message gets populated by fluent.
  await TestUtils.waitForCondition(
    () => updatesMessage.message.textContent,
    "wait for message text"
  );

  await updatesFound;

  // The button should be visible, and should get some text from fluent.
  ok(!updatesMessage.button.hidden, "The view updates button is visible");
  await TestUtils.waitForCondition(
    () => updatesMessage.button.textContent,
    "wait for button text"
  );

  // Wait for the available updates count to finalize, it's async.
  await BrowserTestUtils.waitForCondition(() => availableCat.badgeCount == 3);

  // The category shows the correct update count.
  ok(!availableCat.hidden, "Available updates is visible");
  is(availableCat.badgeCount, 3, "There are 3 updates");

  // Go to the available updates page.
  let loaded = waitForViewLoad(win);
  availableCat.click();
  await loaded;

  // Check the updates are shown.
  let cards = doc.querySelectorAll("addon-card");
  is(cards.length, 3, "There are 3 cards");

  // Each card should have an update.
  for (let card of cards) {
    assertUpdateState({ card, shown: true, expanded: false });
  }

  // Check the detail page for the first add-on.
  await loadDetailView(win, ids[0]);
  is(
    categoryUtils.getSelectedViewId(),
    "addons://list/extension",
    "The extensions category is selected"
  );

  // Go back to the last view.
  loaded = waitForViewLoad(win);
  doc.querySelector(".back-button").click();
  await loaded;

  // We're back on the updates view.
  is(
    categoryUtils.getSelectedViewId(),
    "addons://updates/available",
    "The available updates category is selected"
  );

  // Find the cards again.
  cards = doc.querySelectorAll("addon-card");
  is(cards.length, 3, "There are 3 cards");

  // Install the first update.
  await installUpdate(cards[0], "update-installed");
  assertUpdateState({ card: cards[0], shown: false, expanded: false });

  // The count goes down but the card stays.
  is(availableCat.badgeCount, 2, "There are only 2 updates now");
  is(
    doc.querySelectorAll("addon-card").length,
    3,
    "All 3 cards are still visible on the updates page"
  );

  // Install the other two updates.
  await installUpdate(cards[1], "update-installed");
  assertUpdateState({ card: cards[1], shown: false, expanded: false });
  await installUpdate(cards[2], "update-installed");
  assertUpdateState({ card: cards[2], shown: false, expanded: false });

  // The count goes down but the card stays.
  is(availableCat.badgeCount, 0, "There are no more updates");
  is(
    doc.querySelectorAll("addon-card").length,
    3,
    "All 3 cards are still visible on the updates page"
  );

  // Enable global add-on updates again.
  AddonManager.autoUpdateDefault = true;

  await closeView(win);
  await Promise.all(addons.map(addon => addon.unload()));
});

add_task(async function testUpdatesShownOnLoad() {
  let id = "has-update@mochi.test";
  let addon = await setupExtensionWithUpdate(id);

  // Find the update for our addon.
  AddonManager.autoUpdateDefault = false;
  await findUpdatesForAddonId(id);

  let win = await loadInitialView("extension");
  let categoryUtils = new CategoryUtilities(win);
  let updatesButton = categoryUtils.get("available-updates");

  ok(!updatesButton.hidden, "The updates button is shown");
  is(updatesButton.badgeCount, 1, "There is an update");

  let loaded = waitForViewLoad(win);
  updatesButton.click();
  await loaded;

  let cards = win.document.querySelectorAll("addon-card");

  is(cards.length, 1, "There is one update card");

  let card = cards[0];
  is(card.addon.id, id, "The update is for the expected add-on");

  await installUpdate(card, "update-installed");

  ok(!updatesButton.hidden, "The updates button is still shown");
  is(updatesButton.badgeCount, 0, "There are no more updates");

  info("Check that the updates section is hidden when re-opened");
  await closeView(win);
  win = await loadInitialView("extension");
  categoryUtils = new CategoryUtilities(win);
  updatesButton = categoryUtils.get("available-updates");

  ok(updatesButton.hidden, "Available updates is hidden");
  is(updatesButton.badgeCount, 0, "There are no updates");

  AddonManager.autoUpdateDefault = true;
  await closeView(win);
  await addon.unload();
});

add_task(async function testPromptOnBackgroundUpdateCheck() {
  const id = "test-prompt-on-background-check@mochi.test";
  const extension = await setupExtensionWithUpdate(id);

  AddonManager.autoUpdateDefault = false;

  const addon = await AddonManager.getAddonByID(id);
  await AddonTestUtils.promiseFindAddonUpdates(
    addon,
    AddonManager.UPDATE_WHEN_PERIODIC_UPDATE
  );
  let win = await loadInitialView("extension");

  let card = getAddonCard(win, id);

  const promisePromptInfo = promisePermissionPrompt(id);
  await installUpdate(card, "update-installed");
  const promptInfo = await promisePromptInfo;
  ok(promptInfo, "Got a permission prompt as expected");

  AddonManager.autoUpdateDefault = true;

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

add_task(async function testNoUpdateAvailableOnUnrelatedAddonCards() {
  let idNoUpdate = "no-update@mochi.test";

  let extensionNoUpdate = ExtensionTestUtils.loadExtension({
    useAddonManager: "temporary",
    manifest: {
      name: "TestAddonNoUpdate",
      browser_specific_settings: { gecko: { id: idNoUpdate } },
    },
  });
  await extensionNoUpdate.startup();

  let win = await loadInitialView("extension");

  let cardNoUpdate = getAddonCard(win, idNoUpdate);
  ok(cardNoUpdate, `Got AddonCard for ${idNoUpdate}`);

  // Assert that there is not an update badge
  assertUpdateState({ card: cardNoUpdate, shown: false, expanded: false });

  // Trigger a onNewInstall event by install another unrelated addon.
  const XPI_URL = `${SECURE_TESTROOT}../xpinstall/amosigned.xpi`;
  let install = await AddonManager.getInstallForURL(XPI_URL);
  await AddonManager.installAddonFromAOM(
    gBrowser.selectedBrowser,
    win.document.documentURIObject,
    install
  );

  // Cancel the install used to trigger the onNewInstall install event.
  await install.cancel();
  // Assert that the previously installed addon isn't marked with the
  // update available badge after installing an unrelated addon.
  assertUpdateState({ card: cardNoUpdate, shown: false, expanded: false });

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