diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /browser/base/content/test/favicons/browser_favicon_change_not_in_document.js | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | browser/base/content/test/favicons/browser_favicon_change_not_in_document.js | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/browser/base/content/test/favicons/browser_favicon_change_not_in_document.js b/browser/base/content/test/favicons/browser_favicon_change_not_in_document.js new file mode 100644 index 0000000000..b8215dcc3e --- /dev/null +++ b/browser/base/content/test/favicons/browser_favicon_change_not_in_document.js @@ -0,0 +1,148 @@ +"use strict"; + +const TEST_ROOT = + "http://mochi.test:8888/browser/browser/base/content/test/favicons/"; +const TEST_URL = TEST_ROOT + "file_favicon_change_not_in_document.html"; + +// Runs the given task in the document of the browser. +function runInDoc(browser, task) { + return ContentTask.spawn(browser, `(${task.toString()})();`, scriptStr => { + let script = content.document.createElement("script"); + script.textContent = scriptStr; + content.document.body.appendChild(script); + + // Link events are dispatched asynchronously so allow the event loop to run + // to ensure that any events are actually dispatched before returning. + return new Promise(resolve => content.setTimeout(resolve, 0)); + }); +} + +/* + * This test tests a link element won't fire DOMLinkChanged/DOMLinkAdded unless + * it is added to the DOM. See more details in bug 1083895. + * + * Note that there is debounce logic in FaviconLoader.sys.mjs, adding a new + * icon link after the icon parsing timeout will trigger a new icon extraction + * cycle. Hence, there should be two favicons loads in this test as it appends + * a new link to the DOM in the timeout callback defined in the test HTML page. + * However, the not-yet-added link element with href as "http://example.org/other-icon" + * should not fire the DOMLinkAdded event, nor should it fire the DOMLinkChanged + * event after its href gets updated later. + */ +add_task(async function () { + let extraTab = (gBrowser.selectedTab = BrowserTestUtils.addTab( + gBrowser, + TEST_ROOT + )); + let domLinkAddedFired = 0; + let domLinkChangedFired = 0; + const linkAddedHandler = event => domLinkAddedFired++; + const linkChangedhandler = event => domLinkChangedFired++; + BrowserTestUtils.addContentEventListener( + gBrowser.selectedBrowser, + "DOMLinkAdded", + linkAddedHandler + ); + BrowserTestUtils.addContentEventListener( + gBrowser.selectedBrowser, + "DOMLinkChanged", + linkChangedhandler + ); + + let expectedFavicon = TEST_ROOT + "file_generic_favicon.ico"; + let faviconPromise = waitForFavicon(extraTab.linkedBrowser, expectedFavicon); + + BrowserTestUtils.startLoadingURIString(extraTab.linkedBrowser, TEST_URL); + await BrowserTestUtils.browserLoaded(extraTab.linkedBrowser); + + await faviconPromise; + + is( + domLinkAddedFired, + 2, + "Should fire the correct number of DOMLinkAdded event." + ); + is(domLinkChangedFired, 0, "Should not fire any DOMLinkChanged event."); + + gBrowser.removeTab(extraTab); +}); + +/* + * This verifies that creating and manipulating link elements inside document + * fragments doesn't trigger the link events. + */ +add_task(async function () { + let extraTab = (gBrowser.selectedTab = BrowserTestUtils.addTab( + gBrowser, + TEST_ROOT + )); + let browser = extraTab.linkedBrowser; + + let domLinkAddedFired = 0; + let domLinkChangedFired = 0; + const linkAddedHandler = event => domLinkAddedFired++; + const linkChangedhandler = event => domLinkChangedFired++; + BrowserTestUtils.addContentEventListener( + browser, + "DOMLinkAdded", + linkAddedHandler + ); + BrowserTestUtils.addContentEventListener( + browser, + "DOMLinkChanged", + linkChangedhandler + ); + + BrowserTestUtils.startLoadingURIString(browser, TEST_ROOT + "blank.html"); + await BrowserTestUtils.browserLoaded(browser); + + is(domLinkAddedFired, 0, "Should have been no link add events"); + is(domLinkChangedFired, 0, "Should have been no link change events"); + + await runInDoc(browser, () => { + let fragment = document + .createRange() + .createContextualFragment( + '<link type="image/ico" href="file_generic_favicon.ico" rel="icon">' + ); + fragment.firstElementChild.setAttribute("type", "image/png"); + }); + + is(domLinkAddedFired, 0, "Should have been no link add events"); + is(domLinkChangedFired, 0, "Should have been no link change events"); + + await runInDoc(browser, () => { + let fragment = document.createDocumentFragment(); + let link = document.createElement("link"); + link.setAttribute("href", "file_generic_favicon.ico"); + link.setAttribute("rel", "icon"); + link.setAttribute("type", "image/ico"); + + fragment.appendChild(link); + link.setAttribute("type", "image/png"); + }); + + is(domLinkAddedFired, 0, "Should have been no link add events"); + is(domLinkChangedFired, 0, "Should have been no link change events"); + + let expectedFavicon = TEST_ROOT + "file_generic_favicon.ico"; + let faviconPromise = waitForFavicon(browser, expectedFavicon); + + // Moving an element from the fragment into the DOM should trigger the add + // events and start favicon loading. + await runInDoc(browser, () => { + let fragment = document + .createRange() + .createContextualFragment( + '<link type="image/ico" href="file_generic_favicon.ico" rel="icon">' + ); + document.head.appendChild(fragment); + }); + + is(domLinkAddedFired, 1, "Should have been one link add events"); + is(domLinkChangedFired, 0, "Should have been no link change events"); + + await faviconPromise; + + gBrowser.removeTab(extraTab); +}); |