summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/xpcshell/test_AddonRepository_paging.js
blob: 0773917d84a0c24e89e1f7fc810ab8645e8c8a7f (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
// Test that AMO api results that are returned in muliple pages are
// properly handled.
add_task(async function test_paged_api() {
  const MAX_ADDON = 3;

  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "2");

  let testserver = createHttpServer();
  const PORT = testserver.identity.primaryPort;

  const EMPTY_RESPONSE = {
    next: null,
    results: [],
  };

  function name(n) {
    return `Addon ${n}`;
  }
  function id(n) {
    return `test${n}@tests.mozilla.org`;
  }
  function summary(n) {
    return `Summary for addon ${n}`;
  }
  function description(n) {
    return `Description for addon ${n}`;
  }

  testserver.registerPathHandler("/empty", (request, response) => {
    response.setHeader("content-type", "application/json");
    response.write(JSON.stringify(EMPTY_RESPONSE));
  });

  testserver.registerPrefixHandler("/addons/", (request, response) => {
    let [page] = /\d+/.exec(request.path);
    page = page ? parseInt(page, 10) : 0;
    page = Math.min(page, MAX_ADDON);

    let result = {
      next:
        page == MAX_ADDON
          ? null
          : `http://localhost:${PORT}/addons/${page + 1}`,
      results: [
        {
          name: name(page),
          type: "extension",
          guid: id(page),
          summary: summary(page),
          description: description(page),
        },
      ],
    };

    response.setHeader("content-type", "application/json");
    response.write(JSON.stringify(result));
  });

  Services.prefs.setCharPref(
    PREF_GETADDONS_BYIDS,
    `http://localhost:${PORT}/addons/0`
  );

  await promiseStartupManager();

  for (let i = 0; i <= MAX_ADDON; i++) {
    await promiseInstallWebExtension({
      manifest: {
        browser_specific_settings: { gecko: { id: id(i) } },
      },
    });
  }

  await AddonManagerPrivate.backgroundUpdateCheck();

  let ids = [];
  for (let i = 0; i <= MAX_ADDON; i++) {
    ids.push(id(i));
  }
  let addons = await AddonRepository.getAddonsByIDs(ids);

  equal(addons.length, MAX_ADDON + 1);
  for (let i = 0; i <= MAX_ADDON; i++) {
    equal(addons[i].name, name(i));
    equal(addons[i].id, id(i));
    equal(addons[i].description, summary(i));
    equal(addons[i].fullDescription, description(i));
  }

  await promiseShutdownManager();
});