diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /toolkit/components/windowwatcher/test/test_named_window.html | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/windowwatcher/test/test_named_window.html')
-rw-r--r-- | toolkit/components/windowwatcher/test/test_named_window.html | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/toolkit/components/windowwatcher/test/test_named_window.html b/toolkit/components/windowwatcher/test/test_named_window.html new file mode 100644 index 0000000000..af3536dd5e --- /dev/null +++ b/toolkit/components/windowwatcher/test/test_named_window.html @@ -0,0 +1,87 @@ +<!DOCTYPE HTML> +<html> +<!-- +Test that when content opens a new window with a name, that the +newly opened window actually gets that name, and that subsequent +attempts to open a window with that name will target the same +window. +--> +<head> + <meta charset="utf-8"> + <title>Test named windows</title> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <script src="head.js" type="application/javascript"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> +</head> +<body> + <a href="#" id="link">Click me</a> + + <script type="application/javascript"> + "use strict"; + + const NAME = "my_window"; + const TARGET_URL = location.href.replace("test_named_window.html", + "file_named_window.html"); + const TARGET_URL_2 = TARGET_URL + "#2"; + const TARGET_URL_3 = TARGET_URL + "#3"; + + /** + * Returns a Promise that resolves once some target has had + * some event dispatched on it. + * + * @param target + * The thing to wait for the event to be dispatched + * through. + * @param eventName + * The name of the event to wait for. + * @returns Promise + */ + function promiseEvent(target, eventName) { + return new Promise(resolve => { + target.addEventListener(eventName, function(e) { + resolve(e); + }, {capture: true, once: true}); + }); + } + + add_task(async function() { + // This magic value of 2 means that by default, when content tries + // to open a new window, it'll actually open in a new window instead + // of a new tab. + await SpecialPowers.pushPrefEnv({"set": [ + ["browser.link.open_newwindow", 2], + ]}); + + let win1 = window.open(TARGET_URL, "my_window"); + await promiseEvent(win1, "load"); + + let name = SpecialPowers.wrap(win1).docShell.name; + + is(name, NAME, "Should have the expected name"); + is(win1.location.href, new URL(TARGET_URL).href, + "Should have loaded target TARGET_URL in the original window"); + + let hashChange = promiseEvent(win1, "hashchange"); + let win2 = window.open(TARGET_URL_2, "my_window"); + await hashChange; + + is(win1, win2, "Should have gotten back the same window"); + is(win1.location.href, new URL(TARGET_URL_2).href, + "Should have re-targeted pre-existing window"); + + hashChange = promiseEvent(win1, "hashchange"); + let link = document.getElementById("link"); + link.setAttribute("target", NAME); + link.setAttribute("href", TARGET_URL_3); + link.click(); + + await hashChange; + + is(win1.location.href, new URL(TARGET_URL_3).href, + "Should have re-targeted pre-existing window"); + + win1.close(); + }); + </script> +</body> +</html> |