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

/* eslint-disable mozilla/no-arbitrary-setTimeout */

const ROOTURI =
  "http://mochi.test:8888/browser/browser/base/content/test/favicons/";
const ICON = "moz.png";
const DATAURL =
  "data:image/x-icon;base64,AAABAAEAEBAAAAAAAABoBQAAFgAAACgAAAAQAAAAIAAAAAEACAAAAAAAAAEAAAAAAAAAAAAAAAEAAAABAAAAAAAAAACAAACAAAAAgIAAgAAAAIAAgACAgAAAwMDAAMDcwADwyqYABAQEAAgICAAMDAwAERERABYWFgAcHBwAIiIiACkpKQBVVVUATU1NAEJCQgA5OTkAgHz/AFBQ/wCTANYA/+zMAMbW7wDW5+cAkKmtAAAAMwAAAGYAAACZAAAAzAAAMwAAADMzAAAzZgAAM5kAADPMAAAz/wAAZgAAAGYzAABmZgAAZpkAAGbMAABm/wAAmQAAAJkzAACZZgAAmZkAAJnMAACZ/wAAzAAAAMwzAADMZgAAzJkAAMzMAADM/wAA/2YAAP+ZAAD/zAAzAAAAMwAzADMAZgAzAJkAMwDMADMA/wAzMwAAMzMzADMzZgAzM5kAMzPMADMz/wAzZgAAM2YzADNmZgAzZpkAM2bMADNm/wAzmQAAM5kzADOZZgAzmZkAM5nMADOZ/wAzzAAAM8wzADPMZg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dHetEHx4K7B/5+QH5+fkdDBL5+SBE/wwdJfkf+fn5AR8g+fkfEArsCh/5+QEeJR/5+SAeBwAACgoe+SAlHwFAEhAfAAAAAPcKHh8eASYBHhAMAAAAAAAA9EMdIB8gHh0dBwAAAAAAAAAA7BAdQ+wHAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP//AADwfwAAwH8AAMB/AAAAPwAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAQAAgAcAAIAPAADADwAA8D8AAP//AAA=";

let iconDiscoveryTests = [
  {
    text: "rel icon discovered",
    icons: [{}],
  },
  {
    text: "rel may contain additional rels separated by spaces",
    icons: [{ rel: "abcdefg icon qwerty" }],
  },
  {
    text: "rel is case insensitive",
    icons: [{ rel: "ICON" }],
  },
  {
    text: "rel shortcut-icon not discovered",
    expectedIcon: ROOTURI + ICON,
    icons: [
      // We will prefer the later icon if detected
      {},
      { rel: "shortcut-icon", href: "nothere.png" },
    ],
  },
  {
    text: "relative href works",
    icons: [{ href: "moz.png" }],
  },
  {
    text: "404'd icon is removed properly",
    pass: false,
    icons: [{ href: "notthere.png" }],
  },
  {
    text: "data: URIs work",
    icons: [{ href: DATAURL, type: "image/x-icon" }],
  },
  {
    text: "type may have optional parameters (RFC2046)",
    icons: [{ type: "image/png; charset=utf-8" }],
  },
  {
    text: "apple-touch-icon discovered",
    richIcon: true,
    icons: [{ rel: "apple-touch-icon" }],
  },
  {
    text: "apple-touch-icon-precomposed discovered",
    richIcon: true,
    icons: [{ rel: "apple-touch-icon-precomposed" }],
  },
  {
    text: "fluid-icon discovered",
    richIcon: true,
    icons: [{ rel: "fluid-icon" }],
  },
  {
    text: "unknown icon not discovered",
    expectedIcon: ROOTURI + ICON,
    richIcon: true,
    icons: [
      // We will prefer the larger icon if detected
      { rel: "apple-touch-icon", sizes: "32x32" },
      { rel: "unknown-icon", sizes: "128x128", href: "notthere.png" },
    ],
  },
];

add_task(async function() {
  let url = ROOTURI + "discovery.html";
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, url);

  for (let testCase of iconDiscoveryTests) {
    info(`Running test "${testCase.text}"`);

    if (testCase.pass === undefined) {
      testCase.pass = true;
    }

    if (testCase.icons.length > 1 && !testCase.expectedIcon) {
      ok(false, "Invalid test data, missing expectedIcon");
      continue;
    }

    let expectedIcon = testCase.expectedIcon || testCase.icons[0].href || ICON;
    expectedIcon = new URL(expectedIcon, ROOTURI).href;

    let iconPromise = waitForFaviconMessage(!testCase.richIcon, expectedIcon);

    await SpecialPowers.spawn(
      gBrowser.selectedBrowser,
      [[testCase.icons, ROOTURI + ICON]],
      ([icons, defaultIcon]) => {
        let doc = content.document;
        let head = doc.head;

        for (let icon of icons) {
          let link = doc.createElement("link");
          link.rel = icon.rel || "icon";
          link.href = icon.href || defaultIcon;
          link.type = icon.type || "image/png";
          if (icon.sizes) {
            link.sizes = icon.sizes;
          }
          head.appendChild(link);
        }
      }
    );

    try {
      let { iconURL } = await iconPromise;
      ok(testCase.pass, testCase.text);
      is(iconURL, expectedIcon, "Should have seen the expected icon.");
    } catch (e) {
      ok(!testCase.pass, testCase.text);
    }

    await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
      let links = content.document.querySelectorAll("link");
      for (let link of links) {
        link.remove();
      }
    });
  }

  BrowserTestUtils.removeTab(tab);
});