summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/favicons/browser_preferred_icons.js
blob: 25f548c717deedeb1a45c9aaa2533376739ed836 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

const ROOT =
  "http://mochi.test:8888/browser/browser/base/content/test/favicons/";

async function waitIcon(url) {
  let icon = await waitForFaviconMessage(true, url);
  is(icon.iconURL, url, "Should have seen the right icon.");
}

function createLinks(linkInfos) {
  return SpecialPowers.spawn(gBrowser.selectedBrowser, [linkInfos], links => {
    let doc = content.document;
    let head = doc.head;
    for (let l of links) {
      let link = doc.createElement("link");
      link.rel = "icon";
      link.href = l.href;
      if (l.type) {
        link.type = l.type;
      }
      if (l.size) {
        link.setAttribute("sizes", `${l.size}x${l.size}`);
      }
      head.appendChild(link);
    }
  });
}

add_setup(async function () {
  const URL = ROOT + "discovery.html";
  let iconPromise = waitIcon("http://mochi.test:8888/favicon.ico");
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, URL);
  await iconPromise;
  registerCleanupFunction(async function () {
    BrowserTestUtils.removeTab(tab);
  });
});

add_task(async function prefer_svg() {
  let promise = waitIcon(ROOT + "icon.svg");
  await createLinks([
    { href: ROOT + "icon.ico", type: "image/x-icon" },
    { href: ROOT + "icon.svg", type: "image/svg+xml" },
    {
      href: ROOT + "icon.png",
      type: "image/png",
      size: 16 * Math.ceil(window.devicePixelRatio),
    },
  ]);
  await promise;
});

add_task(async function prefer_sized() {
  let promise = waitIcon(ROOT + "moz.png");
  await createLinks([
    { href: ROOT + "icon.ico", type: "image/x-icon" },
    {
      href: ROOT + "moz.png",
      type: "image/png",
      size: 16 * Math.ceil(window.devicePixelRatio),
    },
    { href: ROOT + "icon2.ico", type: "image/x-icon" },
  ]);
  await promise;
});

add_task(async function prefer_last_ico() {
  let promise = waitIcon(ROOT + "file_generic_favicon.ico");
  await createLinks([
    { href: ROOT + "icon.ico", type: "image/x-icon" },
    { href: ROOT + "icon.png", type: "image/png" },
    { href: ROOT + "file_generic_favicon.ico", type: "image/x-icon" },
  ]);
  await promise;
});

add_task(async function fuzzy_ico() {
  let promise = waitIcon(ROOT + "file_generic_favicon.ico");
  await createLinks([
    { href: ROOT + "icon.ico", type: "image/x-icon" },
    { href: ROOT + "icon.png", type: "image/png" },
    {
      href: ROOT + "file_generic_favicon.ico",
      type: "image/vnd.microsoft.icon",
    },
  ]);
  await promise;
});

add_task(async function guess_svg() {
  let promise = waitIcon(ROOT + "icon.svg");
  await createLinks([
    { href: ROOT + "icon.svg" },
    {
      href: ROOT + "icon.png",
      type: "image/png",
      size: 16 * Math.ceil(window.devicePixelRatio),
    },
    { href: ROOT + "icon.ico", type: "image/x-icon" },
  ]);
  await promise;
});

add_task(async function guess_ico() {
  let promise = waitIcon(ROOT + "file_generic_favicon.ico");
  await createLinks([
    { href: ROOT + "file_generic_favicon.ico" },
    { href: ROOT + "icon.png", type: "image/png" },
  ]);
  await promise;
});

add_task(async function guess_invalid() {
  let promise = waitIcon(ROOT + "icon.svg");
  // Create strange links to make sure they don't break us
  await createLinks([
    { href: ROOT + "icon.svg" },
    { href: ROOT + "icon" },
    { href: ROOT + "icon?.svg" },
    { href: ROOT + "icon#.svg" },
    { href: "data:text/plain,icon" },
    { href: "file:///icon" },
    { href: "about:icon" },
  ]);
  await promise;
});

add_task(async function guess_bestSized() {
  let preferredWidth = 16 * Math.ceil(window.devicePixelRatio);
  let promise = waitIcon(ROOT + "moz.png");
  await createLinks([
    { href: ROOT + "icon.png", type: "image/png", size: preferredWidth - 1 },
    { href: ROOT + "icon2.png", type: "image/png" },
    { href: ROOT + "moz.png", type: "image/png", size: preferredWidth + 1 },
    { href: ROOT + "icon4.png", type: "image/png", size: preferredWidth + 2 },
  ]);
  await promise;
});