browser_preferred_icons.js (4073B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 const ROOT = 5 "http://mochi.test:8888/browser/browser/base/content/test/favicons/"; 6 7 async function waitIcon(url) { 8 let icon = await waitForFaviconMessage(true, url); 9 is(icon.iconURL, url, "Should have seen the right icon."); 10 } 11 12 function createLinks(linkInfos) { 13 return SpecialPowers.spawn(gBrowser.selectedBrowser, [linkInfos], links => { 14 let doc = content.document; 15 let head = doc.head; 16 for (let l of links) { 17 let link = doc.createElement("link"); 18 link.rel = "icon"; 19 link.href = l.href; 20 if (l.type) { 21 link.type = l.type; 22 } 23 if (l.size) { 24 link.setAttribute("sizes", `${l.size}x${l.size}`); 25 } 26 head.appendChild(link); 27 } 28 }); 29 } 30 31 add_setup(async function () { 32 const URL = ROOT + "discovery.html"; 33 let iconPromise = waitIcon("http://mochi.test:8888/favicon.ico"); 34 let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, URL); 35 await iconPromise; 36 registerCleanupFunction(async function () { 37 BrowserTestUtils.removeTab(tab); 38 }); 39 }); 40 41 add_task(async function prefer_svg() { 42 let promise = waitIcon(ROOT + "icon.svg"); 43 await createLinks([ 44 { href: ROOT + "icon.ico", type: "image/x-icon" }, 45 { href: ROOT + "icon.svg", type: "image/svg+xml" }, 46 { 47 href: ROOT + "icon.png", 48 type: "image/png", 49 size: 16 * Math.ceil(window.devicePixelRatio), 50 }, 51 ]); 52 await promise; 53 }); 54 55 add_task(async function prefer_sized() { 56 let promise = waitIcon(ROOT + "moz.png"); 57 await createLinks([ 58 { href: ROOT + "icon.ico", type: "image/x-icon" }, 59 { 60 href: ROOT + "moz.png", 61 type: "image/png", 62 size: 16 * Math.ceil(window.devicePixelRatio), 63 }, 64 { href: ROOT + "icon2.ico", type: "image/x-icon" }, 65 ]); 66 await promise; 67 }); 68 69 add_task(async function prefer_last_ico() { 70 let promise = waitIcon(ROOT + "file_generic_favicon.ico"); 71 await createLinks([ 72 { href: ROOT + "icon.ico", type: "image/x-icon" }, 73 { href: ROOT + "icon.png", type: "image/png" }, 74 { href: ROOT + "file_generic_favicon.ico", type: "image/x-icon" }, 75 ]); 76 await promise; 77 }); 78 79 add_task(async function fuzzy_ico() { 80 let promise = waitIcon(ROOT + "file_generic_favicon.ico"); 81 await createLinks([ 82 { href: ROOT + "icon.ico", type: "image/x-icon" }, 83 { href: ROOT + "icon.png", type: "image/png" }, 84 { 85 href: ROOT + "file_generic_favicon.ico", 86 type: "image/vnd.microsoft.icon", 87 }, 88 ]); 89 await promise; 90 }); 91 92 add_task(async function guess_svg() { 93 let promise = waitIcon(ROOT + "icon.svg"); 94 await createLinks([ 95 { href: ROOT + "icon.svg" }, 96 { 97 href: ROOT + "icon.png", 98 type: "image/png", 99 size: 16 * Math.ceil(window.devicePixelRatio), 100 }, 101 { href: ROOT + "icon.ico", type: "image/x-icon" }, 102 ]); 103 await promise; 104 }); 105 106 add_task(async function guess_ico() { 107 let promise = waitIcon(ROOT + "file_generic_favicon.ico"); 108 await createLinks([ 109 { href: ROOT + "file_generic_favicon.ico" }, 110 { href: ROOT + "icon.png", type: "image/png" }, 111 ]); 112 await promise; 113 }); 114 115 add_task(async function guess_invalid() { 116 let promise = waitIcon(ROOT + "icon.svg"); 117 // Create strange links to make sure they don't break us 118 await createLinks([ 119 { href: ROOT + "icon.svg" }, 120 { href: ROOT + "icon" }, 121 { href: ROOT + "icon?.svg" }, 122 { href: ROOT + "icon#.svg" }, 123 { href: "data:text/plain,icon" }, 124 { href: "file:///icon" }, 125 { href: "about:icon" }, 126 ]); 127 await promise; 128 }); 129 130 add_task(async function guess_bestSized() { 131 let preferredWidth = 16 * Math.ceil(window.devicePixelRatio); 132 let promise = waitIcon(ROOT + "moz.png"); 133 await createLinks([ 134 { href: ROOT + "icon.png", type: "image/png", size: preferredWidth - 1 }, 135 { href: ROOT + "icon2.png", type: "image/png" }, 136 { href: ROOT + "moz.png", type: "image/png", size: preferredWidth + 1 }, 137 { href: ROOT + "icon4.png", type: "image/png", size: preferredWidth + 2 }, 138 ]); 139 await promise; 140 });