summaryrefslogtreecommitdiffstats
path: root/toolkit/components/search/tests/xpcshell/test_appProvided_icons.js
blob: f6cc8b241568b4402402c8cdd7db0fc82c2f90fe (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Tests to ensure that icons for application provided engines are correctly
 * loaded from remote settings.
 */

"use strict";

// A skeleton configuration that gets filled in from TESTS during `add_setup`.
let CONFIG = [
  {
    recordType: "defaultEngines",
    globalDefault: "engine_no_icon",
    specificDefaults: [],
  },
  {
    recordType: "engineOrders",
    orders: [],
  },
];

let TESTS = [
  {
    engineId: "engine_no_icon",
    expectedIcon: null,
  },
  {
    engineId: "engine_exact_match",
    icons: [
      {
        filename: "remoteIcon.ico",
        engineIdentifiers: ["engine_exact_match"],
        imageSize: 16,
      },
    ],
    expectedIcon: "remoteIcon.ico",
  },
  {
    engineId: "engine_begins_with",
    icons: [
      {
        filename: "remoteIcon.ico",
        engineIdentifiers: ["engine_begins*"],
        imageSize: 16,
      },
    ],
    expectedIcon: "remoteIcon.ico",
  },
  {
    engineId: "engine_non_default_sized_icon",
    icons: [
      {
        filename: "remoteIcon.ico",
        engineIdentifiers: [
          // This also tests multiple engine idenifiers works.
          "enterprise_shuttle",
          "engine_non_default_sized_icon",
        ],
        imageSize: 32,
      },
    ],
    expectedIcon: "remoteIcon.ico",
  },
  {
    engineId: "engine_multiple_icons",
    icons: [
      {
        filename: "bigIcon.ico",
        engineIdentifiers: ["engine_multiple_icons"],
        imageSize: 16,
      },
      {
        filename: "remoteIcon.ico",
        engineIdentifiers: ["engine_multiple_icons"],
        imageSize: 32,
      },
    ],
    expectedIcon: "bigIcon.ico",
  },
];

add_setup(async function () {
  let client = RemoteSettings("search-config-icons");
  let db = client.db;

  await db.clear();

  for (let test of TESTS) {
    CONFIG.push({
      identifier: test.engineId,
      recordType: "engine",
      base: {
        name: test.engineId + " name",
        urls: {
          search: {
            base: "https://example.com/" + test.engineId,
            searchTermParamName: "q",
          },
        },
      },
      variants: [{ environment: { allRegionsAndLocales: true } }],
    });

    if ("icons" in test) {
      for (let icon of test.icons) {
        await insertRecordIntoCollection(client, { ...icon });
      }
    }
  }

  await SearchTestUtils.useTestEngines("simple-engines", null, CONFIG);
  await Services.search.init();
});

for (let test of TESTS) {
  add_task(async function () {
    info("Testing engine: " + test.engineId);

    let engine = Services.search.getEngineByName(test.engineId + " name");
    if (test.expectedIcon) {
      let engineIconURL = await engine.getIconURL(16);
      Assert.notEqual(
        engineIconURL,
        null,
        "Should have an icon URL for the engine."
      );

      let response = await fetch(engineIconURL);
      let buffer = new Uint8Array(await response.arrayBuffer());

      let expectedBuffer = new Uint8Array(
        await getFileDataBuffer(test.expectedIcon)
      );

      Assert.equal(
        buffer.length,
        expectedBuffer.length,
        "Should have received matching buffer lengths for the expected icon"
      );
      Assert.ok(
        buffer.every((value, index) => value === expectedBuffer[index]),
        "Should have received matching data for the expected icon"
      );
    } else {
      Assert.equal(
        await engine.getIconURL(),
        null,
        "Should not have an icon URL for the engine."
      );
    }
  });
}