diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-12 05:43:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-12 05:43:14 +0000 |
commit | 8dd16259287f58f9273002717ec4d27e97127719 (patch) | |
tree | 3863e62a53829a84037444beab3abd4ed9dfc7d0 /toolkit/components/places/tests/favicons | |
parent | Releasing progress-linux version 126.0.1-1~progress7.99u1. (diff) | |
download | firefox-8dd16259287f58f9273002717ec4d27e97127719.tar.xz firefox-8dd16259287f58f9273002717ec4d27e97127719.zip |
Merging upstream version 127.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
16 files changed, 437 insertions, 1111 deletions
diff --git a/toolkit/components/places/tests/favicons/head_favicons.js b/toolkit/components/places/tests/favicons/head_favicons.js index afd2c4924f..73dd2a61ed 100644 --- a/toolkit/components/places/tests/favicons/head_favicons.js +++ b/toolkit/components/places/tests/favicons/head_favicons.js @@ -15,6 +15,9 @@ const systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal(); +// Used in createFavicon(). +let uniqueFaviconId = 0; + /** * Checks that the favicon for the given page matches the provided data. * @@ -24,6 +27,7 @@ const systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal(); * Expected MIME type of the icon, for example "image/png". * @param aExpectedData * Expected icon data, expressed as an array of byte values. + * If set null, skip the test for the favicon data. * @param aCallback * This function is called after the check finished. */ @@ -37,7 +41,9 @@ function checkFaviconDataForPage( aPageURI, async function (aURI, aDataLen, aData, aMimeType) { Assert.equal(aExpectedMimeType, aMimeType); - Assert.ok(compareArrays(aExpectedData, aData)); + if (aExpectedData) { + Assert.ok(compareArrays(aExpectedData, aData)); + } await check_guid_for_uri(aPageURI); aCallback(); } @@ -76,3 +82,82 @@ function promiseFaviconChanged(aExpectedPageURI, aExpectedFaviconURI) { }); }); } + +/** + * Create favicon file to temp directory. + * + * @param {string} aFileName + * File name that will be created in temp directory. + * @returns {object} + * { + * file: nsIFile, + * uri: nsIURI, + * data: byte Array, + * mimetype: String, + * } + */ +async function createFavicon(aFileName) { + // Copy the favicon file we have to the specified file in temp directory. + let originalFaviconFile = do_get_file("favicon-normal16.png"); + let tempDir = Services.dirsvc.get("TmpD", Ci.nsIFile); + let faviconFile = tempDir.clone(); + faviconFile.append(aFileName); + await IOUtils.copy(originalFaviconFile.path, faviconFile.path); + + // Append some data that sniffers/encoders will ignore that will distinguish + // the different favicons we'll create. + let stream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance( + Ci.nsIFileOutputStream + ); + const WRONLY_PERMISSION = 0o600; + stream.init( + faviconFile, + FileUtils.MODE_WRONLY | FileUtils.MODE_APPEND, + WRONLY_PERMISSION, + 0 + ); + uniqueFaviconId++; + let uniqueStr = "uid:" + uniqueFaviconId; + stream.write(uniqueStr, uniqueStr.length); + stream.close(); + + Assert.equal(faviconFile.leafName.substr(0, aFileName.length), aFileName); + + return { + file: faviconFile, + uri: uri(faviconFile), + data: readFileData(faviconFile), + mimeType: "image/png", + }; +} + +/** + * Create nsIURI for given favicon object. + * + * @param {object} aFavicon + * Favicon object created by createFavicon(). + * @returns {nsIURI} + */ +async function createDataURLForFavicon(aFavicon) { + let dataURL = await toDataURL(aFavicon.data, aFavicon.mimeType); + return uri(dataURL); +} + +/** + * Create data URL string from given byte array and type. + * + * @param {Array} data + * Byte array. + * @param {string} type + * The type of this data. + * @returns {string} + */ +function toDataURL(data, type) { + let blob = new Blob([new Uint8Array(data)], { type }); + return new Promise((resolve, reject) => { + let reader = new FileReader(); + reader.addEventListener("load", () => resolve(reader.result)); + reader.addEventListener("error", reject); + reader.readAsDataURL(blob); + }); +} diff --git a/toolkit/components/places/tests/favicons/test_cached-favicon_mime_type.js b/toolkit/components/places/tests/favicons/test_cached-favicon_mime_type.js index c1f6689a70..52eb81ae85 100644 --- a/toolkit/components/places/tests/favicons/test_cached-favicon_mime_type.js +++ b/toolkit/components/places/tests/favicons/test_cached-favicon_mime_type.js @@ -68,14 +68,12 @@ add_task(async function () { info("Test that the content type of a favicon we add is correct."); let testURI = uri("http://mozilla.org/"); // Add the data before opening - PlacesUtils.favicons.replaceFaviconDataFromDataURL( + await PlacesTestUtils.addVisits(testURI); + await PlacesTestUtils.setFaviconForPage( + testURI, testIconURI, - testFaviconData, - 0, - systemPrincipal + testFaviconData ); - await PlacesTestUtils.addVisits(testURI); - await setFaviconForPage(testURI, testIconURI); // Open the channel let channel = NetUtil.newChannel({ uri: PlacesUtils.favicons.getFaviconLinkForIcon(testIconURI).spec, @@ -120,14 +118,12 @@ add_task(async function test_userpass() { CACHED_ICON_NORMAL, CACHED_ICON_USERPASS, ]) { - PlacesUtils.favicons.replaceFaviconDataFromDataURL( + await PlacesTestUtils.addVisits(pageURI); + await PlacesTestUtils.setFaviconForPage( + pageURI, iconURI, - testFaviconData, - 0, - systemPrincipal + testFaviconData ); - await PlacesTestUtils.addVisits(pageURI); - await setFaviconForPage(pageURI, iconURI); // Open the channel let channel = NetUtil.newChannel({ diff --git a/toolkit/components/places/tests/favicons/test_expire_on_new_icons.js b/toolkit/components/places/tests/favicons/test_expire_on_new_icons.js index d5a7c42ba3..f0089d737b 100644 --- a/toolkit/components/places/tests/favicons/test_expire_on_new_icons.js +++ b/toolkit/components/places/tests/favicons/test_expire_on_new_icons.js @@ -28,23 +28,23 @@ add_task(async function test_expire_associated() { ]; for (let icon of favicons) { - let data = readFileData(do_get_file(icon.name)); - PlacesUtils.favicons.replaceFaviconData( - NetUtil.newURI(TEST_URL + icon.name), - data, + let dataURL = await readFileDataAsDataURL( + do_get_file(icon.name), icon.mimeType ); - await setFaviconForPage(TEST_URL, TEST_URL + icon.name); + await PlacesTestUtils.setFaviconForPage( + TEST_URL, + TEST_URL + icon.name, + dataURL + ); if (icon.expired) { await expireIconRelationsForPage(TEST_URL); // Add the same icon to another page. - PlacesUtils.favicons.replaceFaviconData( - NetUtil.newURI(TEST_URL + icon.name), - data, - icon.mimeType, - icon.expire + await PlacesTestUtils.setFaviconForPage( + TEST_URL2, + TEST_URL + icon.name, + dataURL ); - await setFaviconForPage(TEST_URL2, TEST_URL + icon.name); } } @@ -88,13 +88,7 @@ add_task(async function test_expire_root() { // Insert an expired icon. let iconURI = NetUtil.newURI(pageURI.spec + "favicon-normal16.png"); - PlacesUtils.favicons.replaceFaviconDataFromDataURL( - iconURI, - SMALLPNG_DATA_URI.spec, - 0, - systemPrincipal - ); - await setFaviconForPage(pageURI, iconURI); + await PlacesTestUtils.setFaviconForPage(pageURI, iconURI, SMALLPNG_DATA_URI); Assert.equal( await countEntries("moz_icons_to_pages"), 1, @@ -105,13 +99,11 @@ add_task(async function test_expire_root() { // Now insert a new root icon. let rootIconURI = NetUtil.newURI(pageURI.spec + "favicon.ico"); - PlacesUtils.favicons.replaceFaviconDataFromDataURL( + await PlacesTestUtils.setFaviconForPage( + pageURI, rootIconURI, - SMALLPNG_DATA_URI.spec, - 0, - systemPrincipal + SMALLPNG_DATA_URI ); - await setFaviconForPage(pageURI, rootIconURI); // Only the root icon should have survived. Assert.equal( diff --git a/toolkit/components/places/tests/favicons/test_favicons_conversions.js b/toolkit/components/places/tests/favicons/test_favicons_conversions.js index 28a0fffb7f..dd8a35c5ac 100644 --- a/toolkit/components/places/tests/favicons/test_favicons_conversions.js +++ b/toolkit/components/places/tests/favicons/test_favicons_conversions.js @@ -43,29 +43,24 @@ async function checkFaviconDataConversion( }); let faviconURI = NetUtil.newURI("http://places.test/icon/" + aFileName); let fileData = readFileOfLength(aFileName, aFileLength); + let fileDataURL = await fileDataToDataURL(fileData, aFileMimeType); + await PlacesTestUtils.setFaviconForPage( + pageURI.spec, + faviconURI.spec, + fileDataURL + ); - PlacesUtils.favicons.replaceFaviconData(faviconURI, fileData, aFileMimeType); await new Promise(resolve => { - PlacesUtils.favicons.setAndFetchFaviconForPage( - pageURI, - faviconURI, - true, - PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE, - (aURI, aDataLen, aData, aMimeType) => { - if (!aExpectConversion) { - Assert.ok(compareArrays(aData, fileData)); - Assert.equal(aMimeType, aFileMimeType); - } else { - if (!aVaryOnWindows || !isWindows) { - let expectedFile = do_get_file("expected-" + aFileName + ".png"); - Assert.ok(compareArrays(aData, readFileData(expectedFile))); - } - Assert.equal(aMimeType, "image/png"); - } - resolve(); - }, - Services.scriptSecurityManager.getSystemPrincipal() - ); + if (!aExpectConversion) { + checkFaviconDataForPage(pageURI, aFileMimeType, fileData, resolve); + } else if (!aVaryOnWindows || !isWindows) { + let expectedFile = do_get_file("expected-" + aFileName + ".png"); + let expectedData = readFileData(expectedFile); + checkFaviconDataForPage(pageURI, "image/png", expectedData, resolve); + } else { + // Not check the favicon data. + checkFaviconDataForPage(pageURI, "image/png", null, resolve); + } }); } diff --git a/toolkit/components/places/tests/favicons/test_favicons_protocols_ref.js b/toolkit/components/places/tests/favicons/test_favicons_protocols_ref.js index aa0241a3d2..4d359f7307 100644 --- a/toolkit/components/places/tests/favicons/test_favicons_protocols_ref.js +++ b/toolkit/components/places/tests/favicons/test_favicons_protocols_ref.js @@ -9,21 +9,16 @@ const ICON32_URL = "http://places.test/favicon-normal32.png"; add_task(async function () { await PlacesTestUtils.addVisits(PAGE_URL); // Add 2 differently sized favicons for this page. - - let data = readFileData(do_get_file("favicon-normal16.png")); - PlacesUtils.favicons.replaceFaviconData( - Services.io.newURI(ICON16_URL), - data, + let dataURL16 = await readFileDataAsDataURL( + do_get_file("favicon-normal16.png"), "image/png" ); - await setFaviconForPage(PAGE_URL, ICON16_URL); - data = readFileData(do_get_file("favicon-normal32.png")); - PlacesUtils.favicons.replaceFaviconData( - Services.io.newURI(ICON32_URL), - data, + await PlacesTestUtils.setFaviconForPage(PAGE_URL, ICON16_URL, dataURL16); + let dataURL32 = await readFileDataAsDataURL( + do_get_file("favicon-normal32.png"), "image/png" ); - await setFaviconForPage(PAGE_URL, ICON32_URL); + await PlacesTestUtils.setFaviconForPage(PAGE_URL, ICON32_URL, dataURL32); const PAGE_ICON_URL = "page-icon:" + PAGE_URL; diff --git a/toolkit/components/places/tests/favicons/test_getFaviconDataForPage.js b/toolkit/components/places/tests/favicons/test_getFaviconDataForPage.js index 80f498f33f..8c6fb0a2bb 100644 --- a/toolkit/components/places/tests/favicons/test_getFaviconDataForPage.js +++ b/toolkit/components/places/tests/favicons/test_getFaviconDataForPage.js @@ -60,12 +60,8 @@ add_task(async function test_fallback() { info("Set icon for the root"); await PlacesTestUtils.addVisits(ROOT_URL); let data = readFileData(do_get_file("favicon-normal16.png")); - PlacesUtils.favicons.replaceFaviconData( - NetUtil.newURI(ROOT_ICON_URL), - data, - "image/png" - ); - await setFaviconForPage(ROOT_URL, ROOT_ICON_URL); + let dataURL = await fileDataToDataURL(data, "image/png"); + await PlacesTestUtils.setFaviconForPage(ROOT_URL, ROOT_ICON_URL, dataURL); info("check fallback icons"); await new Promise(resolve => { @@ -96,12 +92,8 @@ add_task(async function test_fallback() { info("Now add a proper icon for the page"); await PlacesTestUtils.addVisits(SUBPAGE_URL); let data32 = readFileData(do_get_file("favicon-normal32.png")); - PlacesUtils.favicons.replaceFaviconData( - NetUtil.newURI(ICON32_URL), - data32, - "image/png" - ); - await setFaviconForPage(SUBPAGE_URL, ICON32_URL); + let dataURL32 = await fileDataToDataURL(data32, "image/png"); + await PlacesTestUtils.setFaviconForPage(SUBPAGE_URL, ICON32_URL, dataURL32); info("check no fallback icons"); await new Promise(resolve => { diff --git a/toolkit/components/places/tests/favicons/test_getFaviconURLForPage.js b/toolkit/components/places/tests/favicons/test_getFaviconURLForPage.js index e8f459cb08..3fa11ae6c0 100644 --- a/toolkit/components/places/tests/favicons/test_getFaviconURLForPage.js +++ b/toolkit/components/places/tests/favicons/test_getFaviconURLForPage.js @@ -57,13 +57,11 @@ add_task(async function test_fallback() { info("Set icon for the root"); await PlacesTestUtils.addVisits(ROOT_URL); - let data = readFileData(do_get_file("favicon-normal16.png")); - PlacesUtils.favicons.replaceFaviconData( - NetUtil.newURI(ROOT_ICON_URL), - data, + let dataURL = await readFileDataAsDataURL( + do_get_file("favicon-normal16.png"), "image/png" ); - await setFaviconForPage(ROOT_URL, ROOT_ICON_URL); + await PlacesTestUtils.setFaviconForPage(ROOT_URL, ROOT_ICON_URL, dataURL); info("check fallback icons"); Assert.equal( @@ -79,13 +77,11 @@ add_task(async function test_fallback() { info("Now add a proper icon for the page"); await PlacesTestUtils.addVisits(SUBPAGE_URL); - let data32 = readFileData(do_get_file("favicon-normal32.png")); - PlacesUtils.favicons.replaceFaviconData( - NetUtil.newURI(ICON32_URL), - data32, + let dataURL32 = await readFileDataAsDataURL( + do_get_file("favicon-normal32.png"), "image/png" ); - await setFaviconForPage(SUBPAGE_URL, ICON32_URL); + await PlacesTestUtils.setFaviconForPage(SUBPAGE_URL, ICON32_URL, dataURL32); info("check no fallback icons"); Assert.equal( diff --git a/toolkit/components/places/tests/favicons/test_heavy_favicon.js b/toolkit/components/places/tests/favicons/test_heavy_favicon.js index 09adcaf6fa..4541de9bff 100644 --- a/toolkit/components/places/tests/favicons/test_heavy_favicon.js +++ b/toolkit/components/places/tests/favicons/test_heavy_favicon.js @@ -26,8 +26,8 @@ add_task(async function () { let pageURI = uri("http://foo.bar/"); await PlacesTestUtils.addVisits(pageURI); - PlacesUtils.favicons.replaceFaviconData(icon.uri, icon.data, icon.mimetype); - await setFaviconForPage(pageURI, icon.uri); + let dataURI = await fileDataToDataURL(icon.data, icon.mimetype); + await PlacesTestUtils.setFaviconForPage(pageURI.spec, icon.uri.spec, dataURI); Assert.equal( await getFaviconUrlForPage(pageURI), icon.uri.spec, diff --git a/toolkit/components/places/tests/favicons/test_incremental_vacuum.js b/toolkit/components/places/tests/favicons/test_incremental_vacuum.js index ab93121d47..2c1a5ad3f9 100644 --- a/toolkit/components/places/tests/favicons/test_incremental_vacuum.js +++ b/toolkit/components/places/tests/favicons/test_incremental_vacuum.js @@ -16,10 +16,9 @@ add_task(async function () { let url = "http://foo.bar/"; await PlacesTestUtils.addVisits(url); for (let i = 0; i < 10; ++i) { - let iconUri = NetUtil.newURI("http://mozilla.org/" + i); - let data = readFileData(icon.file); - PlacesUtils.favicons.replaceFaviconData(iconUri, data, icon.mimetype); - await setFaviconForPage(url, iconUri); + let iconUri = "http://mozilla.org/" + i; + let dataURL = await readFileDataAsDataURL(icon.file, icon.mimetype); + await PlacesTestUtils.setFaviconForPage(url, iconUri, dataURL); } let promise = TestUtils.topicObserved("places-favicons-expired"); diff --git a/toolkit/components/places/tests/favicons/test_multiple_frames.js b/toolkit/components/places/tests/favicons/test_multiple_frames.js index 5c7f585715..a6aae572de 100644 --- a/toolkit/components/places/tests/favicons/test_multiple_frames.js +++ b/toolkit/components/places/tests/favicons/test_multiple_frames.js @@ -14,9 +14,15 @@ add_task(async function () { let faviconURI = NetUtil.newURI("http://places.test/icon/favicon-multi.ico"); // Fake window. let win = { devicePixelRatio: 1.0 }; - let icoData = readFileData(do_get_file("favicon-multi.ico")); - PlacesUtils.favicons.replaceFaviconData(faviconURI, icoData, "image/x-icon"); - await setFaviconForPage(pageURI, faviconURI); + let icoDataURL = await readFileDataAsDataURL( + do_get_file("favicon-multi.ico"), + "image/x-icon" + ); + await PlacesTestUtils.setFaviconForPage( + pageURI.spec, + faviconURI.spec, + icoDataURL + ); for (let size of [16, 32, 64]) { let file = do_get_file(`favicon-multi-frame${size}.png`); diff --git a/toolkit/components/places/tests/favicons/test_page-icon_protocol.js b/toolkit/components/places/tests/favicons/test_page-icon_protocol.js index 287484868f..2241dec990 100644 --- a/toolkit/components/places/tests/favicons/test_page-icon_protocol.js +++ b/toolkit/components/places/tests/favicons/test_page-icon_protocol.js @@ -76,25 +76,12 @@ var gFavicon; add_task(async function setup() { await PlacesTestUtils.addVisits(TEST_URI); - - PlacesUtils.favicons.replaceFaviconDataFromDataURL( + await PlacesTestUtils.setFaviconForPage( + TEST_URI, ICON_URI, ICON_DATAURL, - (Date.now() + 8640000) * 1000, - Services.scriptSecurityManager.getSystemPrincipal() + (Date.now() + 8640000) * 1000 ); - - await new Promise(resolve => { - PlacesUtils.favicons.setAndFetchFaviconForPage( - TEST_URI, - ICON_URI, - false, - PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE, - resolve, - Services.scriptSecurityManager.getSystemPrincipal() - ); - }); - gDefaultFavicon = await fetchIconForSpec( PlacesUtils.favicons.defaultFavicon.spec ); @@ -133,13 +120,11 @@ add_task(async function subpage_url_fallback() { add_task(async function svg_icon() { let faviconURI = NetUtil.newURI("http://places.test/favicon.svg"); - PlacesUtils.favicons.replaceFaviconDataFromDataURL( + await PlacesTestUtils.setFaviconForPage( + TEST_URI, faviconURI, - SMALLSVG_DATA_URI.spec, - 0, - Services.scriptSecurityManager.getSystemPrincipal() + SMALLSVG_DATA_URI ); - await setFaviconForPage(TEST_URI, faviconURI); let svgIcon = await fetchIconForSpec(SMALLSVG_DATA_URI.spec); info(svgIcon.contentType); let pageIcon = await fetchIconForSpec("page-icon:" + TEST_URI.spec); @@ -270,23 +255,8 @@ add_task(async function test_with_user_pass() { for (const { pageURI, iconURI } of testData) { for (const loadingIconURISpec of [PAGE_ICON_NORMAL, PAGE_ICON_USERPASS]) { - PlacesUtils.favicons.replaceFaviconDataFromDataURL( - iconURI, - ICON_DATAURL, - 0, - systemPrincipal - ); await PlacesTestUtils.addVisits(pageURI); - await new Promise(resolve => { - PlacesUtils.favicons.setAndFetchFaviconForPage( - pageURI, - iconURI, - false, - PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE, - resolve, - Services.scriptSecurityManager.getSystemPrincipal() - ); - }); + await PlacesTestUtils.setFaviconForPage(pageURI, iconURI, ICON_DATAURL); let { data, contentType } = await fetchIconForSpec(loadingIconURISpec); Assert.equal(contentType, gFavicon.contentType); diff --git a/toolkit/components/places/tests/favicons/test_replaceFaviconData.js b/toolkit/components/places/tests/favicons/test_replaceFaviconData.js deleted file mode 100644 index 2e9835eaa9..0000000000 --- a/toolkit/components/places/tests/favicons/test_replaceFaviconData.js +++ /dev/null @@ -1,395 +0,0 @@ -/* Any copyright is dedicated to the Public Domain. - * http://creativecommons.org/publicdomain/zero/1.0/ */ - -/* - * Tests for replaceFaviconData() - */ - -var iconsvc = PlacesUtils.favicons; - -var originalFavicon = { - file: do_get_file("favicon-normal16.png"), - uri: uri(do_get_file("favicon-normal16.png")), - data: readFileData(do_get_file("favicon-normal16.png")), - mimetype: "image/png", -}; - -var uniqueFaviconId = 0; -function createFavicon(fileName) { - let tempdir = Services.dirsvc.get("TmpD", Ci.nsIFile); - - // remove any existing file at the path we're about to copy to - let outfile = tempdir.clone(); - outfile.append(fileName); - try { - outfile.remove(false); - } catch (e) {} - - originalFavicon.file.copyToFollowingLinks(tempdir, fileName); - - let stream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance( - Ci.nsIFileOutputStream - ); - stream.init(outfile, 0x02 | 0x08 | 0x10, 0o600, 0); - - // append some data that sniffers/encoders will ignore that will distinguish - // the different favicons we'll create - uniqueFaviconId++; - let uniqueStr = "uid:" + uniqueFaviconId; - stream.write(uniqueStr, uniqueStr.length); - stream.close(); - - Assert.equal(outfile.leafName.substr(0, fileName.length), fileName); - - return { - file: outfile, - uri: uri(outfile), - data: readFileData(outfile), - mimetype: "image/png", - }; -} - -function checkCallbackSucceeded( - callbackMimetype, - callbackData, - sourceMimetype, - sourceData -) { - Assert.equal(callbackMimetype, sourceMimetype); - Assert.ok(compareArrays(callbackData, sourceData)); -} - -function run_test() { - // check that the favicon loaded correctly - Assert.equal(originalFavicon.data.length, 286); - run_next_test(); -} - -add_task(async function test_replaceFaviconData_validHistoryURI() { - info("test replaceFaviconData for valid history uri"); - - let pageURI = uri("http://test1.bar/"); - await PlacesTestUtils.addVisits(pageURI); - - let favicon = createFavicon("favicon1.png"); - - iconsvc.replaceFaviconData(favicon.uri, favicon.data, favicon.mimetype); - - await new Promise(resolve => { - iconsvc.setAndFetchFaviconForPage( - pageURI, - favicon.uri, - true, - PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE, - function test_replaceFaviconData_validHistoryURI_check( - aURI, - aDataLen, - aData, - aMimeType - ) { - dump("GOT " + aMimeType + "\n"); - checkCallbackSucceeded( - aMimeType, - aData, - favicon.mimetype, - favicon.data - ); - checkFaviconDataForPage( - pageURI, - favicon.mimetype, - favicon.data, - function test_replaceFaviconData_validHistoryURI_callback() { - favicon.file.remove(false); - resolve(); - } - ); - }, - systemPrincipal - ); - }); - - await PlacesUtils.history.clear(); -}); - -add_task(async function test_replaceFaviconData_overrideDefaultFavicon() { - info("test replaceFaviconData to override a later setAndFetchFaviconForPage"); - - let pageURI = uri("http://test2.bar/"); - await PlacesTestUtils.addVisits(pageURI); - - let firstFavicon = createFavicon("favicon2.png"); - let secondFavicon = createFavicon("favicon3.png"); - - iconsvc.replaceFaviconData( - firstFavicon.uri, - secondFavicon.data, - secondFavicon.mimetype - ); - - await new Promise(resolve => { - iconsvc.setAndFetchFaviconForPage( - pageURI, - firstFavicon.uri, - true, - PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE, - function test_replaceFaviconData_overrideDefaultFavicon_check( - aURI, - aDataLen, - aData, - aMimeType - ) { - checkCallbackSucceeded( - aMimeType, - aData, - secondFavicon.mimetype, - secondFavicon.data - ); - checkFaviconDataForPage( - pageURI, - secondFavicon.mimetype, - secondFavicon.data, - function test_replaceFaviconData_overrideDefaultFavicon_callback() { - firstFavicon.file.remove(false); - secondFavicon.file.remove(false); - resolve(); - } - ); - }, - systemPrincipal - ); - }); - - await PlacesUtils.history.clear(); -}); - -add_task(async function test_replaceFaviconData_replaceExisting() { - info( - "test replaceFaviconData to override a previous setAndFetchFaviconForPage" - ); - - let pageURI = uri("http://test3.bar"); - await PlacesTestUtils.addVisits(pageURI); - - let firstFavicon = createFavicon("favicon4.png"); - let secondFavicon = createFavicon("favicon5.png"); - - await new Promise(resolve => { - iconsvc.setAndFetchFaviconForPage( - pageURI, - firstFavicon.uri, - true, - PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE, - function test_replaceFaviconData_replaceExisting_firstSet_check( - aURI, - aDataLen, - aData, - aMimeType - ) { - checkCallbackSucceeded( - aMimeType, - aData, - firstFavicon.mimetype, - firstFavicon.data - ); - checkFaviconDataForPage( - pageURI, - firstFavicon.mimetype, - firstFavicon.data, - function test_replaceFaviconData_overrideDefaultFavicon_firstCallback() { - iconsvc.replaceFaviconData( - firstFavicon.uri, - secondFavicon.data, - secondFavicon.mimetype - ); - PlacesTestUtils.promiseAsyncUpdates().then(() => { - checkFaviconDataForPage( - pageURI, - secondFavicon.mimetype, - secondFavicon.data, - function test_replaceFaviconData_overrideDefaultFavicon_secondCallback() { - firstFavicon.file.remove(false); - secondFavicon.file.remove(false); - resolve(); - }, - systemPrincipal - ); - }); - } - ); - }, - systemPrincipal - ); - }); - - await PlacesUtils.history.clear(); -}); - -add_task(async function test_replaceFaviconData_unrelatedReplace() { - info("test replaceFaviconData to not make unrelated changes"); - - let pageURI = uri("http://test4.bar/"); - await PlacesTestUtils.addVisits(pageURI); - - let favicon = createFavicon("favicon6.png"); - let unrelatedFavicon = createFavicon("favicon7.png"); - - iconsvc.replaceFaviconData( - unrelatedFavicon.uri, - unrelatedFavicon.data, - unrelatedFavicon.mimetype - ); - - await new Promise(resolve => { - iconsvc.setAndFetchFaviconForPage( - pageURI, - favicon.uri, - true, - PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE, - function test_replaceFaviconData_unrelatedReplace_check( - aURI, - aDataLen, - aData, - aMimeType - ) { - checkCallbackSucceeded( - aMimeType, - aData, - favicon.mimetype, - favicon.data - ); - checkFaviconDataForPage( - pageURI, - favicon.mimetype, - favicon.data, - function test_replaceFaviconData_unrelatedReplace_callback() { - favicon.file.remove(false); - unrelatedFavicon.file.remove(false); - resolve(); - } - ); - }, - systemPrincipal - ); - }); - - await PlacesUtils.history.clear(); -}); - -add_task(async function test_replaceFaviconData_badInputs() { - info("test replaceFaviconData to throw on bad inputs"); - let icon = createFavicon("favicon8.png"); - - Assert.throws( - () => iconsvc.replaceFaviconData(icon.uri, icon.data, ""), - /NS_ERROR_ILLEGAL_VALUE/ - ); - Assert.throws( - () => iconsvc.replaceFaviconData(icon.uri, icon.data, "not-an-image"), - /NS_ERROR_ILLEGAL_VALUE/ - ); - Assert.throws( - () => iconsvc.replaceFaviconData(null, icon.data, icon.mimetype), - /NS_ERROR_ILLEGAL_VALUE/ - ); - Assert.throws( - () => iconsvc.replaceFaviconData(icon.uri, [], icon.mimetype), - /NS_ERROR_ILLEGAL_VALUE/ - ); - Assert.throws( - () => iconsvc.replaceFaviconData(icon.uri, null, icon.mimetype), - /NS_ERROR_XPC_CANT_CONVERT_PRIMITIVE_TO_ARRAY/ - ); - - icon.file.remove(false); - await PlacesUtils.history.clear(); -}); - -add_task(async function test_replaceFaviconData_twiceReplace() { - info("test replaceFaviconData on multiple replacements"); - - let pageURI = uri("http://test5.bar/"); - await PlacesTestUtils.addVisits(pageURI); - - let firstFavicon = createFavicon("favicon9.png"); - let secondFavicon = createFavicon("favicon10.png"); - - iconsvc.replaceFaviconData( - firstFavicon.uri, - firstFavicon.data, - firstFavicon.mimetype - ); - iconsvc.replaceFaviconData( - firstFavicon.uri, - secondFavicon.data, - secondFavicon.mimetype - ); - - await new Promise(resolve => { - iconsvc.setAndFetchFaviconForPage( - pageURI, - firstFavicon.uri, - true, - PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE, - function test_replaceFaviconData_twiceReplace_check( - aURI, - aDataLen, - aData, - aMimeType - ) { - checkCallbackSucceeded( - aMimeType, - aData, - secondFavicon.mimetype, - secondFavicon.data - ); - checkFaviconDataForPage( - pageURI, - secondFavicon.mimetype, - secondFavicon.data, - function test_replaceFaviconData_twiceReplace_callback() { - firstFavicon.file.remove(false); - secondFavicon.file.remove(false); - resolve(); - }, - systemPrincipal - ); - }, - systemPrincipal - ); - }); - - await PlacesUtils.history.clear(); -}); - -add_task(async function test_replaceFaviconData_rootOverwrite() { - info("test replaceFaviconData doesn't overwrite root = 1"); - - async function getRootValue(url) { - let db = await PlacesUtils.promiseDBConnection(); - let rows = await db.execute( - "SELECT root FROM moz_icons WHERE icon_url = :url", - { url } - ); - return rows[0].getResultByName("root"); - } - - const PAGE_URL = "http://rootoverwrite.bar/"; - let pageURI = Services.io.newURI(PAGE_URL); - const ICON_URL = "http://rootoverwrite.bar/favicon.ico"; - let iconURI = Services.io.newURI(ICON_URL); - - await PlacesTestUtils.addVisits(pageURI); - - let icon = createFavicon("favicon9.png"); - PlacesUtils.favicons.replaceFaviconData(iconURI, icon.data, icon.mimetype); - await PlacesTestUtils.addFavicons(new Map([[PAGE_URL, ICON_URL]])); - - Assert.equal(await getRootValue(ICON_URL), 1, "Check root == 1"); - let icon2 = createFavicon("favicon10.png"); - PlacesUtils.favicons.replaceFaviconData(iconURI, icon2.data, icon2.mimetype); - // replaceFaviconData doesn't have a callback, but we must wait its updated. - await PlacesTestUtils.promiseAsyncUpdates(); - Assert.equal(await getRootValue(ICON_URL), 1, "Check root did not change"); - - await PlacesUtils.history.clear(); -}); diff --git a/toolkit/components/places/tests/favicons/test_replaceFaviconDataFromDataURL.js b/toolkit/components/places/tests/favicons/test_replaceFaviconDataFromDataURL.js deleted file mode 100644 index c1b83fc8a7..0000000000 --- a/toolkit/components/places/tests/favicons/test_replaceFaviconDataFromDataURL.js +++ /dev/null @@ -1,537 +0,0 @@ -/* Any copyright is dedicated to the Public Domain. - * http://creativecommons.org/publicdomain/zero/1.0/ */ - -/* - * Tests for replaceFaviconData() - */ - -var iconsvc = PlacesUtils.favicons; - -var originalFavicon = { - file: do_get_file("favicon-normal16.png"), - uri: uri(do_get_file("favicon-normal16.png")), - data: readFileData(do_get_file("favicon-normal16.png")), - mimetype: "image/png", -}; - -var uniqueFaviconId = 0; -function createFavicon(fileName) { - let tempdir = Services.dirsvc.get("TmpD", Ci.nsIFile); - - // remove any existing file at the path we're about to copy to - let outfile = tempdir.clone(); - outfile.append(fileName); - try { - outfile.remove(false); - } catch (e) {} - - originalFavicon.file.copyToFollowingLinks(tempdir, fileName); - - let stream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance( - Ci.nsIFileOutputStream - ); - stream.init(outfile, 0x02 | 0x08 | 0x10, 0o600, 0); - - // append some data that sniffers/encoders will ignore that will distinguish - // the different favicons we'll create - uniqueFaviconId++; - let uniqueStr = "uid:" + uniqueFaviconId; - stream.write(uniqueStr, uniqueStr.length); - stream.close(); - - Assert.equal(outfile.leafName.substr(0, fileName.length), fileName); - - return { - file: outfile, - uri: uri(outfile), - data: readFileData(outfile), - mimetype: "image/png", - }; -} - -function createDataURLForFavicon(favicon) { - return "data:" + favicon.mimetype + ";base64," + toBase64(favicon.data); -} - -function checkCallbackSucceeded( - callbackMimetype, - callbackData, - sourceMimetype, - sourceData -) { - Assert.equal(callbackMimetype, sourceMimetype); - Assert.ok(compareArrays(callbackData, sourceData)); -} - -function run_test() { - // check that the favicon loaded correctly - Assert.equal(originalFavicon.data.length, 286); - run_next_test(); -} - -add_task(async function test_replaceFaviconDataFromDataURL_validHistoryURI() { - info("test replaceFaviconDataFromDataURL for valid history uri"); - - let pageURI = uri("http://test1.bar/"); - await PlacesTestUtils.addVisits(pageURI); - - let favicon = createFavicon("favicon1.png"); - iconsvc.replaceFaviconDataFromDataURL( - favicon.uri, - createDataURLForFavicon(favicon), - 0, - Services.scriptSecurityManager.getSystemPrincipal() - ); - - await new Promise(resolve => { - iconsvc.setAndFetchFaviconForPage( - pageURI, - favicon.uri, - true, - PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE, - function test_replaceFaviconDataFromDataURL_validHistoryURI_check( - aURI, - aDataLen, - aData, - aMimeType - ) { - checkCallbackSucceeded( - aMimeType, - aData, - favicon.mimetype, - favicon.data - ); - checkFaviconDataForPage( - pageURI, - favicon.mimetype, - favicon.data, - function test_replaceFaviconDataFromDataURL_validHistoryURI_callback() { - favicon.file.remove(false); - resolve(); - } - ); - }, - Services.scriptSecurityManager.getSystemPrincipal() - ); - }); - - await PlacesUtils.history.clear(); -}); - -add_task( - async function test_replaceFaviconDataFromDataURL_overrideDefaultFavicon() { - info( - "test replaceFaviconDataFromDataURL to override a later setAndFetchFaviconForPage" - ); - - let pageURI = uri("http://test2.bar/"); - await PlacesTestUtils.addVisits(pageURI); - - let firstFavicon = createFavicon("favicon2.png"); - let secondFavicon = createFavicon("favicon3.png"); - - iconsvc.replaceFaviconDataFromDataURL( - firstFavicon.uri, - createDataURLForFavicon(secondFavicon), - 0, - Services.scriptSecurityManager.getSystemPrincipal() - ); - - await new Promise(resolve => { - iconsvc.setAndFetchFaviconForPage( - pageURI, - firstFavicon.uri, - true, - PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE, - function test_replaceFaviconDataFromDataURL_overrideDefaultFavicon_check( - aURI, - aDataLen, - aData, - aMimeType - ) { - checkCallbackSucceeded( - aMimeType, - aData, - secondFavicon.mimetype, - secondFavicon.data - ); - checkFaviconDataForPage( - pageURI, - secondFavicon.mimetype, - secondFavicon.data, - function test_replaceFaviconDataFromDataURL_overrideDefaultFavicon_callback() { - firstFavicon.file.remove(false); - secondFavicon.file.remove(false); - resolve(); - } - ); - }, - Services.scriptSecurityManager.getSystemPrincipal() - ); - }); - - await PlacesUtils.history.clear(); - } -); - -add_task(async function test_replaceFaviconDataFromDataURL_replaceExisting() { - info( - "test replaceFaviconDataFromDataURL to override a previous setAndFetchFaviconForPage" - ); - - let pageURI = uri("http://test3.bar"); - await PlacesTestUtils.addVisits(pageURI); - - let firstFavicon = createFavicon("favicon4.png"); - let secondFavicon = createFavicon("favicon5.png"); - - await new Promise(resolve => { - iconsvc.setAndFetchFaviconForPage( - pageURI, - firstFavicon.uri, - true, - PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE, - function test_replaceFaviconDataFromDataURL_replaceExisting_firstSet_check( - aURI, - aDataLen, - aData, - aMimeType - ) { - checkCallbackSucceeded( - aMimeType, - aData, - firstFavicon.mimetype, - firstFavicon.data - ); - checkFaviconDataForPage( - pageURI, - firstFavicon.mimetype, - firstFavicon.data, - function test_replaceFaviconDataFromDataURL_replaceExisting_firstCallback() { - iconsvc.replaceFaviconDataFromDataURL( - firstFavicon.uri, - createDataURLForFavicon(secondFavicon), - 0, - Services.scriptSecurityManager.getSystemPrincipal() - ); - checkFaviconDataForPage( - pageURI, - secondFavicon.mimetype, - secondFavicon.data, - function test_replaceFaviconDataFromDataURL_replaceExisting_secondCallback() { - firstFavicon.file.remove(false); - secondFavicon.file.remove(false); - resolve(); - } - ); - } - ); - }, - Services.scriptSecurityManager.getSystemPrincipal() - ); - }); - - await PlacesUtils.history.clear(); -}); - -add_task(async function test_replaceFaviconDataFromDataURL_unrelatedReplace() { - info("test replaceFaviconDataFromDataURL to not make unrelated changes"); - - let pageURI = uri("http://test4.bar/"); - await PlacesTestUtils.addVisits(pageURI); - - let favicon = createFavicon("favicon6.png"); - let unrelatedFavicon = createFavicon("favicon7.png"); - - iconsvc.replaceFaviconDataFromDataURL( - unrelatedFavicon.uri, - createDataURLForFavicon(unrelatedFavicon), - 0, - Services.scriptSecurityManager.getSystemPrincipal() - ); - - await new Promise(resolve => { - iconsvc.setAndFetchFaviconForPage( - pageURI, - favicon.uri, - true, - PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE, - function test_replaceFaviconDataFromDataURL_unrelatedReplace_check( - aURI, - aDataLen, - aData, - aMimeType - ) { - checkCallbackSucceeded( - aMimeType, - aData, - favicon.mimetype, - favicon.data - ); - checkFaviconDataForPage( - pageURI, - favicon.mimetype, - favicon.data, - function test_replaceFaviconDataFromDataURL_unrelatedReplace_callback() { - favicon.file.remove(false); - unrelatedFavicon.file.remove(false); - resolve(); - } - ); - }, - Services.scriptSecurityManager.getSystemPrincipal() - ); - }); - - await PlacesUtils.history.clear(); -}); - -add_task(async function test_replaceFaviconDataFromDataURL_badInputs() { - info("test replaceFaviconDataFromDataURL to throw on bad inputs"); - - let favicon = createFavicon("favicon8.png"); - - let ex = null; - try { - iconsvc.replaceFaviconDataFromDataURL( - favicon.uri, - "", - 0, - Services.scriptSecurityManager.getSystemPrincipal() - ); - } catch (e) { - ex = e; - } finally { - Assert.ok(!!ex); - } - - ex = null; - try { - iconsvc.replaceFaviconDataFromDataURL( - null, - createDataURLForFavicon(favicon), - 0, - Services.scriptSecurityManager.getSystemPrincipal() - ); - } catch (e) { - ex = e; - } finally { - Assert.ok(!!ex); - } - - favicon.file.remove(false); - - await PlacesUtils.history.clear(); -}); - -add_task(async function test_replaceFaviconDataFromDataURL_twiceReplace() { - info("test replaceFaviconDataFromDataURL on multiple replacements"); - - let pageURI = uri("http://test5.bar/"); - await PlacesTestUtils.addVisits(pageURI); - - let firstFavicon = createFavicon("favicon9.png"); - let secondFavicon = createFavicon("favicon10.png"); - - iconsvc.replaceFaviconDataFromDataURL( - firstFavicon.uri, - createDataURLForFavicon(firstFavicon), - 0, - Services.scriptSecurityManager.getSystemPrincipal() - ); - iconsvc.replaceFaviconDataFromDataURL( - firstFavicon.uri, - createDataURLForFavicon(secondFavicon), - 0, - Services.scriptSecurityManager.getSystemPrincipal() - ); - - await new Promise(resolve => { - iconsvc.setAndFetchFaviconForPage( - pageURI, - firstFavicon.uri, - true, - PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE, - function test_replaceFaviconDataFromDataURL_twiceReplace_check( - aURI, - aDataLen, - aData, - aMimeType - ) { - checkCallbackSucceeded( - aMimeType, - aData, - secondFavicon.mimetype, - secondFavicon.data - ); - checkFaviconDataForPage( - pageURI, - secondFavicon.mimetype, - secondFavicon.data, - function test_replaceFaviconDataFromDataURL_twiceReplace_callback() { - firstFavicon.file.remove(false); - secondFavicon.file.remove(false); - resolve(); - } - ); - }, - Services.scriptSecurityManager.getSystemPrincipal() - ); - }); - - await PlacesUtils.history.clear(); -}); - -add_task( - async function test_replaceFaviconDataFromDataURL_afterRegularAssign() { - info("test replaceFaviconDataFromDataURL after replaceFaviconData"); - - let pageURI = uri("http://test6.bar/"); - await PlacesTestUtils.addVisits(pageURI); - - let firstFavicon = createFavicon("favicon11.png"); - let secondFavicon = createFavicon("favicon12.png"); - - iconsvc.replaceFaviconData( - firstFavicon.uri, - firstFavicon.data, - firstFavicon.mimetype - ); - iconsvc.replaceFaviconDataFromDataURL( - firstFavicon.uri, - createDataURLForFavicon(secondFavicon), - 0, - Services.scriptSecurityManager.getSystemPrincipal() - ); - - await new Promise(resolve => { - iconsvc.setAndFetchFaviconForPage( - pageURI, - firstFavicon.uri, - true, - PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE, - function test_replaceFaviconDataFromDataURL_afterRegularAssign_check( - aURI, - aDataLen, - aData, - aMimeType - ) { - checkCallbackSucceeded( - aMimeType, - aData, - secondFavicon.mimetype, - secondFavicon.data - ); - checkFaviconDataForPage( - pageURI, - secondFavicon.mimetype, - secondFavicon.data, - function test_replaceFaviconDataFromDataURL_afterRegularAssign_callback() { - firstFavicon.file.remove(false); - secondFavicon.file.remove(false); - resolve(); - } - ); - }, - Services.scriptSecurityManager.getSystemPrincipal() - ); - }); - - await PlacesUtils.history.clear(); - } -); - -add_task( - async function test_replaceFaviconDataFromDataURL_beforeRegularAssign() { - info("test replaceFaviconDataFromDataURL before replaceFaviconData"); - - let pageURI = uri("http://test7.bar/"); - await PlacesTestUtils.addVisits(pageURI); - - let firstFavicon = createFavicon("favicon13.png"); - let secondFavicon = createFavicon("favicon14.png"); - - iconsvc.replaceFaviconDataFromDataURL( - firstFavicon.uri, - createDataURLForFavicon(firstFavicon), - 0, - Services.scriptSecurityManager.getSystemPrincipal() - ); - iconsvc.replaceFaviconData( - firstFavicon.uri, - secondFavicon.data, - secondFavicon.mimetype - ); - - await new Promise(resolve => { - iconsvc.setAndFetchFaviconForPage( - pageURI, - firstFavicon.uri, - true, - PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE, - function test_replaceFaviconDataFromDataURL_beforeRegularAssign_check( - aURI, - aDataLen, - aData, - aMimeType - ) { - checkCallbackSucceeded( - aMimeType, - aData, - secondFavicon.mimetype, - secondFavicon.data - ); - checkFaviconDataForPage( - pageURI, - secondFavicon.mimetype, - secondFavicon.data, - function test_replaceFaviconDataFromDataURL_beforeRegularAssign_callback() { - firstFavicon.file.remove(false); - secondFavicon.file.remove(false); - resolve(); - } - ); - }, - Services.scriptSecurityManager.getSystemPrincipal() - ); - }); - - await PlacesUtils.history.clear(); - } -); - -/* toBase64 copied from image/test/unit/test_encoder_png.js */ - -/* Convert data (an array of integers) to a Base64 string. */ -const toBase64Table = - // eslint-disable-next-line no-useless-concat - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + "0123456789+/"; -const base64Pad = "="; -function toBase64(data) { - let result = ""; - let length = data.length; - let i; - // Convert every three bytes to 4 ascii characters. - for (i = 0; i < length - 2; i += 3) { - result += toBase64Table[data[i] >> 2]; - result += toBase64Table[((data[i] & 0x03) << 4) + (data[i + 1] >> 4)]; - result += toBase64Table[((data[i + 1] & 0x0f) << 2) + (data[i + 2] >> 6)]; - result += toBase64Table[data[i + 2] & 0x3f]; - } - - // Convert the remaining 1 or 2 bytes, pad out to 4 characters. - if (length % 3) { - i = length - (length % 3); - result += toBase64Table[data[i] >> 2]; - if (length % 3 == 2) { - result += toBase64Table[((data[i] & 0x03) << 4) + (data[i + 1] >> 4)]; - result += toBase64Table[(data[i + 1] & 0x0f) << 2]; - result += base64Pad; - } else { - result += toBase64Table[(data[i] & 0x03) << 4]; - result += base64Pad + base64Pad; - } - } - - return result; -} diff --git a/toolkit/components/places/tests/favicons/test_root_icons.js b/toolkit/components/places/tests/favicons/test_root_icons.js index f0487cc162..5a101ed6a6 100644 --- a/toolkit/components/places/tests/favicons/test_root_icons.js +++ b/toolkit/components/places/tests/favicons/test_root_icons.js @@ -9,13 +9,11 @@ add_task(async function () { let pageURI = NetUtil.newURI("http://www.places.test/page/"); await PlacesTestUtils.addVisits(pageURI); let faviconURI = NetUtil.newURI("http://www.places.test/favicon.ico"); - PlacesUtils.favicons.replaceFaviconDataFromDataURL( + await PlacesTestUtils.setFaviconForPage( + pageURI, faviconURI, - SMALLPNG_DATA_URI.spec, - 0, - systemPrincipal + SMALLPNG_DATA_URI ); - await setFaviconForPage(pageURI, faviconURI); // Sanity checks. Assert.equal(await getFaviconUrlForPage(pageURI), faviconURI.spec); @@ -70,22 +68,18 @@ add_task(async function test_removePagesByTimeframe() { // Add a normal icon to the most recent page. let faviconURI = NetUtil.newURI(`${BASE_URL}/page/favicon.ico`); - PlacesUtils.favicons.replaceFaviconDataFromDataURL( + await PlacesTestUtils.setFaviconForPage( + pageURI, faviconURI, - SMALLSVG_DATA_URI.spec, - 0, - systemPrincipal + SMALLSVG_DATA_URI ); - await setFaviconForPage(pageURI, faviconURI); // Add a root icon to the most recent page. let rootIconURI = NetUtil.newURI(`${BASE_URL}/favicon.ico`); - PlacesUtils.favicons.replaceFaviconDataFromDataURL( + await PlacesTestUtils.setFaviconForPage( + pageURI, rootIconURI, - SMALLPNG_DATA_URI.spec, - 0, - systemPrincipal + SMALLPNG_DATA_URI ); - await setFaviconForPage(pageURI, rootIconURI); // Sanity checks. Assert.equal( @@ -141,13 +135,11 @@ add_task(async function test_different_host() { let pageURI = NetUtil.newURI("http://places.test/page/"); await PlacesTestUtils.addVisits(pageURI); let faviconURI = NetUtil.newURI("http://mozilla.test/favicon.ico"); - PlacesUtils.favicons.replaceFaviconDataFromDataURL( + await PlacesTestUtils.setFaviconForPage( + pageURI, faviconURI, - SMALLPNG_DATA_URI.spec, - 0, - systemPrincipal + SMALLPNG_DATA_URI ); - await setFaviconForPage(pageURI, faviconURI); Assert.equal( await getFaviconUrlForPage(pageURI), @@ -166,16 +158,25 @@ add_task(async function test_different_host() { add_task(async function test_same_size() { // Add two icons with the same size, one is a root icon. Check that the // non-root icon is preferred when a smaller size is requested. - let data = readFileData(do_get_file("favicon-normal32.png")); + let dataURL = await readFileDataAsDataURL( + do_get_file("favicon-normal32.png"), + "image/png" + ); let pageURI = NetUtil.newURI("http://new_places.test/page/"); await PlacesTestUtils.addVisits(pageURI); let faviconURI = NetUtil.newURI("http://new_places.test/favicon.ico"); - PlacesUtils.favicons.replaceFaviconData(faviconURI, data, "image/png"); - await setFaviconForPage(pageURI, faviconURI); + await PlacesTestUtils.setFaviconForPage( + pageURI.spec, + faviconURI.spec, + dataURL + ); faviconURI = NetUtil.newURI("http://new_places.test/another_icon.ico"); - PlacesUtils.favicons.replaceFaviconData(faviconURI, data, "image/png"); - await setFaviconForPage(pageURI, faviconURI); + await PlacesTestUtils.setFaviconForPage( + pageURI.spec, + faviconURI.spec, + dataURL + ); Assert.equal( await getFaviconUrlForPage(pageURI, 20), @@ -207,13 +208,7 @@ add_task(async function test_root_on_different_host() { // Root favicon for TEST_URL1. const ICON_URL = "http://places1.test/favicon.ico"; let iconURI = NetUtil.newURI(ICON_URL); - PlacesUtils.favicons.replaceFaviconDataFromDataURL( - iconURI, - SMALLPNG_DATA_URI.spec, - 0, - systemPrincipal - ); - await setFaviconForPage(pageURI1, iconURI); + await PlacesTestUtils.setFaviconForPage(pageURI1, iconURI, SMALLPNG_DATA_URI); Assert.equal(await getRootValue(ICON_URL), 1, "Check root == 1"); Assert.equal( await getFaviconUrlForPage(pageURI1, 16), @@ -222,13 +217,7 @@ add_task(async function test_root_on_different_host() { ); // Same favicon for TEST_URL2. - PlacesUtils.favicons.replaceFaviconDataFromDataURL( - iconURI, - SMALLPNG_DATA_URI.spec, - 0, - systemPrincipal - ); - await setFaviconForPage(pageURI2, iconURI); + await PlacesTestUtils.setFaviconForPage(pageURI2, iconURI, SMALLPNG_DATA_URI); Assert.equal(await getRootValue(ICON_URL), 1, "Check root == 1"); Assert.equal( await getFaviconUrlForPage(pageURI2, 16), diff --git a/toolkit/components/places/tests/favicons/test_setFaviconForPage.js b/toolkit/components/places/tests/favicons/test_setFaviconForPage.js new file mode 100644 index 0000000000..22364e5410 --- /dev/null +++ b/toolkit/components/places/tests/favicons/test_setFaviconForPage.js @@ -0,0 +1,245 @@ +/* Any copyright is dedicated to the Public Domain. + * https://creativecommons.org/publicdomain/zero/1.0/ */ + +/* + * Tests for setFaviconForPage() + */ + +add_task(async function test_validHistoryURI() { + let pageURI = uri("http://test1.bar/"); + await PlacesTestUtils.addVisits(pageURI); + + let favicon = await createFavicon("favicon1.png"); + + await doTestSetFaviconForPage({ + pageURI, + faviconURI: favicon.uri, + dataURL: await createDataURLForFavicon(favicon), + expectedFaviconData: favicon.data, + expectedFaviconMimeType: favicon.mimeType, + }); + + await IOUtils.remove(favicon.file.path); + await PlacesUtils.history.clear(); +}); + +add_task(async function test_overrideDefaultFavicon() { + let pageURI = uri("http://test2.bar/"); + await PlacesTestUtils.addVisits(pageURI); + + let firstFavicon = await createFavicon("favicon2.png"); + let secondFavicon = await createFavicon("favicon3.png"); + + await doTestSetFaviconForPage({ + pageURI, + faviconURI: firstFavicon.uri, + dataURL: await createDataURLForFavicon(secondFavicon), + expectedFaviconData: secondFavicon.data, + expectedFaviconMimeType: secondFavicon.mimeType, + }); + + await IOUtils.remove(firstFavicon.file.path); + await IOUtils.remove(secondFavicon.file.path); + await PlacesUtils.history.clear(); +}); + +add_task(async function test_replaceExisting() { + let pageURI = uri("http://test3.bar"); + await PlacesTestUtils.addVisits(pageURI); + + let firstFavicon = await createFavicon("favicon4.png"); + let secondFavicon = await createFavicon("favicon5.png"); + + await new Promise(resolve => { + PlacesUtils.favicons.setAndFetchFaviconForPage( + pageURI, + firstFavicon.uri, + true, + PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE, + function test_replaceExisting_firstSet_check( + aURI, + aDataLen, + aData, + aMimeType + ) { + Assert.equal(aMimeType, firstFavicon.mimeType); + Assert.ok(compareArrays(aData, firstFavicon.data)); + checkFaviconDataForPage( + pageURI, + firstFavicon.mimeType, + firstFavicon.data, + resolve + ); + }, + Services.scriptSecurityManager.getSystemPrincipal() + ); + }); + + await doTestSetFaviconForPage({ + pageURI, + faviconURI: firstFavicon.uri, + dataURL: await createDataURLForFavicon(secondFavicon), + expectedFaviconData: secondFavicon.data, + expectedFaviconMimeType: secondFavicon.mimeType, + }); + + await IOUtils.remove(firstFavicon.file.path); + await IOUtils.remove(secondFavicon.file.path); + await PlacesUtils.history.clear(); +}); + +add_task(async function test_twiceReplace() { + let pageURI = uri("http://test5.bar/"); + await PlacesTestUtils.addVisits(pageURI); + + let firstFavicon = await createFavicon("favicon9.png"); + let secondFavicon = await createFavicon("favicon10.png"); + + let firstFaviconDataURL = await createDataURLForFavicon(firstFavicon); + await new Promise(resolve => { + PlacesUtils.favicons.setFaviconForPage( + pageURI, + firstFavicon.uri, + firstFaviconDataURL, + null, + resolve + ); + }); + + await doTestSetFaviconForPage({ + pageURI, + faviconURI: firstFavicon.uri, + dataURL: await createDataURLForFavicon(secondFavicon), + expectedFaviconData: secondFavicon.data, + expectedFaviconMimeType: secondFavicon.mimeType, + }); + + await IOUtils.remove(firstFavicon.file.path); + await IOUtils.remove(secondFavicon.file.path); + await PlacesUtils.history.clear(); +}); + +add_task(async function test_userpass() { + let pageURI = uri("http://user:pass@test1.bar/"); + await PlacesTestUtils.addVisits(pageURI); + + let favicon = await createFavicon("favicon1.png"); + let faviconURI = uri("http://user:pass@test1.bar/favicon1.png"); + + await doTestSetFaviconForPage({ + pageURI, + faviconURI, + dataURL: await createDataURLForFavicon(favicon), + expectedFaviconData: favicon.data, + expectedFaviconMimeType: favicon.mimeType, + }); + + await IOUtils.remove(favicon.file.path); + await PlacesUtils.history.clear(); +}); + +add_task(async function test_svg() { + let pageURI = uri("http://example.com/"); + await PlacesTestUtils.addVisits(pageURI); + + const svgContent = "<svg><rect width='1px' height='1px%'/></svg>"; + + await doTestSetFaviconForPage({ + pageURI, + faviconURI: uri("http://example.com/favicon.svg"), + dataURL: uri(`data:image/svg+xml;utf8,${svgContent}`), + expectedFaviconData: Array.from(new TextEncoder().encode(svgContent)), + expectedFaviconMimeType: "image/svg+xml", + }); + + await PlacesUtils.history.clear(); +}); + +add_task(async function test_invalidPageURI() { + await PlacesTestUtils.addVisits(uri("http://example.com/")); + let favicon = await createFavicon("favicon-invalidPageURI.png"); + + for (let invalidURI of [null, "", "http://example.com"]) { + try { + info(`Invalid page URI test for [${invalidURI}]`); + PlacesUtils.favicons.setFaviconForPage( + invalidURI, + favicon.uri, + await createDataURLForFavicon(favicon) + ); + Assert.ok(false, "Error should happened"); + } catch (e) { + Assert.ok(true, `Expected error happend [${e.message}]`); + } + } +}); + +add_task(async function test_invalidFaviconURI() { + let pageURI = uri("http://example.com/"); + await PlacesTestUtils.addVisits(pageURI); + let favicon = await createFavicon("favicon-invalidFaviconURI.png"); + + for (let invalidURI of [null, "", favicon.uri.spec]) { + try { + info(`Invalid favicon URI test for [${invalidURI}]`); + PlacesUtils.favicons.setFaviconForPage( + pageURI, + invalidURI, + await createDataURLForFavicon(favicon) + ); + Assert.ok(false, "Error should happened"); + } catch (e) { + Assert.ok(true, `Expected error happend [${e.message}]`); + } + } +}); + +add_task(async function test_invalidFaviconDataURI() { + let pageURI = uri("http://example.com/"); + await PlacesTestUtils.addVisits(pageURI); + let favicon = createFavicon("favicon-invalidFaviconDataURI.png"); + + for (let invalidURI of [ + null, + "", + "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==", + ]) { + try { + info(`Invalid favicon data URI test for [${invalidURI}]`); + PlacesUtils.favicons.setFaviconForPage(pageURI, favicon.uri, invalidURI); + Assert.ok(false, "Error should happened"); + } catch (e) { + Assert.ok(true, `Expected error happend [${e.message}]`); + } + } +}); + +async function doTestSetFaviconForPage({ + pageURI, + faviconURI, + dataURL, + expectedFaviconData, + expectedFaviconMimeType, +}) { + let result = await new Promise(resolve => { + PlacesUtils.favicons.setFaviconForPage( + pageURI, + faviconURI, + dataURL, + null, + resolve + ); + }); + + info("Check the result of setFaviconForPage"); + Assert.equal(result, 0); + + await new Promise(resolve => { + checkFaviconDataForPage( + pageURI, + expectedFaviconMimeType, + expectedFaviconData, + resolve + ); + }); +} diff --git a/toolkit/components/places/tests/favicons/xpcshell.toml b/toolkit/components/places/tests/favicons/xpcshell.toml index 997aa48e0b..2716e72fda 100644 --- a/toolkit/components/places/tests/favicons/xpcshell.toml +++ b/toolkit/components/places/tests/favicons/xpcshell.toml @@ -57,10 +57,6 @@ support-files = [ ["test_query_result_favicon_changed_on_child.js"] -["test_replaceFaviconData.js"] - -["test_replaceFaviconDataFromDataURL.js"] - ["test_root_icons.js"] ["test_setAndFetchFaviconForPage.js"] @@ -69,4 +65,6 @@ support-files = [ ["test_setAndFetchFaviconForPage_redirects.js"] +["test_setFaviconForPage.js"] + ["test_svg_favicon.js"] |