summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/browser/browser_html_discover_view_prefs.js
blob: 474cd424b945309907c02484497ff3aaf58039f5 (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
/* eslint max-len: ["error", 80] */
"use strict";

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

AddonTestUtils.initMochitest(this);
const server = AddonTestUtils.createHttpServer();
const TEST_API_URL = `http://localhost:${server.identity.primaryPort}/discoapi`;

async function checkIfDiscoverVisible(expectVisible) {
  let requestCount = 0;
  let requestPromise = new Promise(resolve => {
    // Overwrites previous request handler, if any.
    server.registerPathHandler("/discoapi", (request, response) => {
      ++requestCount;
      response.write(`{"results": []}`);
      resolve();
    });
  });

  // Open about:addons with default view.
  let managerWindow = await open_manager(null);
  let categoryUtilities = new CategoryUtilities(managerWindow);

  is(
    categoryUtilities.isTypeVisible("discover"),
    expectVisible,
    "Visibility of discopane"
  );

  await wait_for_view_load(managerWindow);
  if (expectVisible) {
    is(
      categoryUtilities.selectedCategory,
      "discover",
      "Expected discopane as the default view"
    );
    await requestPromise;
    is(requestCount, 1, "Expected discovery API request");
  } else {
    // The next view (after discopane) is the extension list.
    is(
      categoryUtilities.selectedCategory,
      "extension",
      "Should fall back to another view when the discopane is disabled"
    );
    is(requestCount, 0, "Discovery API should not be requested");
  }

  await close_manager(managerWindow);
}

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["extensions.getAddons.discovery.api_url", TEST_API_URL],
      // Disable recommendations at the HTML about:addons view to avoid sending
      // a discovery API request from the fallback view (extension list) in the
      // showPane_false test.
      ["extensions.htmlaboutaddons.recommendations.enabled", false],
    ],
  });
});

add_task(async function showPane_true() {
  await SpecialPowers.pushPrefEnv({
    set: [[PREF_DISCOVER_ENABLED, true]],
    clear: [[PREF_UI_LASTCATEGORY]],
  });
  await checkIfDiscoverVisible(true);
  await SpecialPowers.popPrefEnv();
});

add_task(async function showPane_false() {
  await SpecialPowers.pushPrefEnv({
    set: [[PREF_DISCOVER_ENABLED, false]],
    clear: [[PREF_UI_LASTCATEGORY]],
  });
  await checkIfDiscoverVisible(false);
  await SpecialPowers.popPrefEnv();
});