Make Document::HasFocus() use the BrowsingContext hierarchy.
Differential Revision: https://phabricator.services.mozilla.com/D114348
bugzilla-url: https://bugzilla.mozilla.org/show_bug.cgi?id=1709125
gecko-commit: 1d83f19a93b32d4856a8bd036fd488e01efd787c
gecko-reviewers: edgar
diff --git a/focus/hasfocus-different-site.html b/focus/hasfocus-different-site.html
new file mode 100644
index 0000000..4495778
--- /dev/null
+++ b/focus/hasfocus-different-site.html
@@ -0,0 +1,16 @@
+<!doctype html>
+<meta charset=utf-8>
+<title>document.hasFocus() with focus in an iframe</title>
+<script src=/resources/testharness.js></script>
+<script src=/resources/testharnessreport.js></script>
+<script>
+setup({explicit_done:true});
+window.onmessage = function(e) {
+ test(function() {
+ assert_equals(e.data, "PASS", 'Check result');
+ }, "Check result");
+ w.close();
+ done();
+};
+var w = window.open("support/hasfocus-different-site-outer.sub.html");
+</script>
diff --git a/focus/hasfocus-same-site.html b/focus/hasfocus-same-site.html
new file mode 100644
index 0000000..71a2e0f
--- /dev/null
+++ b/focus/hasfocus-same-site.html
@@ -0,0 +1,16 @@
+<!doctype html>
+<meta charset=utf-8>
+<title>document.hasFocus() with focus in an iframe</title>
+<script src=/resources/testharness.js></script>
+<script src=/resources/testharnessreport.js></script>
+<script>
+setup({explicit_done:true});
+window.onmessage = function(e) {
+ test(function() {
+ assert_equals(e.data, "PASS", 'Check result');
+ }, "Check result");
+ w.close();
+ done();
+};
+var w = window.open("support/hasfocus-same-site-outer.html");
+</script>
diff --git a/focus/support/hasfocus-different-site-outer.sub.html b/focus/support/hasfocus-different-site-outer.sub.html
new file mode 100644
index 0000000..bf91adb
--- /dev/null
+++ b/focus/support/hasfocus-different-site-outer.sub.html
@@ -0,0 +1,50 @@
+<!DOCTYPE html>
+<html>
+<head>
+<meta charset="utf-8">
+<title>hasFocus() outer</title>
+<script>
+var w = null;
+var hadInitialFocus = false;
+var hadLostFocus = false;
+window.onmessage = function(e) {
+ if (e.data == "restored") {
+ if (hadInitialFocus && hadLostFocus) {
+ let verdict = document.hasFocus() ? "PASS" : "FAIL: hasFocus false";
+ window.opener.postMessage(verdict, "*");
+ } else {
+ window.opener.postMessage("FAIL: should have failed already", "*");
+ }
+ } else if (e.data == "focused") {
+ w = window.open("hasfocus-other.html");
+ } else if (e.data == "noblur") {
+ window.opener.postMessage("FAIL: no inner blur", "*");
+ } else if (e.data == "opened") {
+ if (document.hasFocus()) {
+ w.close();
+ window.opener.postMessage("FAIL: focus not lost when other window opened", "*");
+ } else {
+ hadLostFocus = true;
+ window.frames[0].postMessage("hasfocus", "*");
+ }
+ } else if (e.data == "close") {
+ w.close();
+ } else if (e.data == "wrongfocus") {
+ w.close();
+ window.opener.postMessage("FAIL: hasFocus was true in iframe", "*");
+ }
+}
+window.onload = function() {
+ if (document.hasFocus()) {
+ hadInitialFocus = true;
+ window.frames[0].postMessage("focus", "*");
+ } else {
+ window.opener.postMessage("FAIL: did not have initial focus", "*");
+ }
+}
+</script>
+</head>
+<body>
+<iframe src="http://{{hosts[alt][www]}}:{{ports[http][0]}}/focus/support/hasfocus-inner.html"></iframe>
+</body>
+</html>
diff --git a/focus/support/hasfocus-inner.html b/focus/support/hasfocus-inner.html
new file mode 100644
index 0000000..40d42bb
--- /dev/null
+++ b/focus/support/hasfocus-inner.html
@@ -0,0 +1,41 @@
+<!DOCTYPE html>
+<html>
+<head>
+<meta charset="utf-8">
+<title>hasFocus inner</title>
+<script>
+var seenFocus = false;
+var seenBlur = false;
+window.onmessage = function(e) {
+ if (e.data == "focus") {
+ let input = document.getElementsByTagName("input")[0];
+ input.onblur = function() {
+ seenBlur = true;
+ }
+ input.onfocus = function() {
+ if (seenFocus) {
+ if (seenBlur) {
+ window.parent.postMessage("restored", "*");
+ } else {
+ window.parent.postMessage("noblur", "*");
+ }
+ } else {
+ seenFocus = true;
+ window.parent.postMessage("focused", "*");
+ }
+ }
+ input.focus();
+ } else if (e.data == "hasfocus") {
+ if (document.hasFocus()) {
+ window.parent.postMessage("wrongfocus", "*");
+ } else {
+ window.parent.postMessage("close", "*");
+ }
+ }
+}
+</script>
+</head>
+<body>
+<input>
+</body>
+</html>
diff --git a/focus/support/hasfocus-other.html b/focus/support/hasfocus-other.html
new file mode 100644
index 0000000..910b6d7
--- /dev/null
+++ b/focus/support/hasfocus-other.html
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html>
+<head>
+<title>Outer</title>
+<script>
+window.onload = function() {
+ window.opener.postMessage("opened", "*")
+}
+</script>
+</head>
+<body>
+</body>
+</html>
diff --git a/focus/support/hasfocus-same-site-outer.html b/focus/support/hasfocus-same-site-outer.html
new file mode 100644
index 0000000..d6f93d8
--- /dev/null
+++ b/focus/support/hasfocus-same-site-outer.html
@@ -0,0 +1,50 @@
+<!DOCTYPE html>
+<html>
+<head>
+<meta charset="utf-8">
+<title>hasFocus() outer</title>
+<script>
+var w = null;
+var hadInitialFocus = false;
+var hadLostFocus = false;
+window.onmessage = function(e) {
+ if (e.data == "restored") {
+ if (hadInitialFocus && hadLostFocus) {
+ let verdict = document.hasFocus() ? "PASS" : "FAIL: hasFocus false";
+ window.opener.postMessage(verdict, "*");
+ } else {
+ window.opener.postMessage("FAIL: should have failed already", "*");
+ }
+ } else if (e.data == "focused") {
+ w = window.open("hasfocus-other.html");
+ } else if (e.data == "noblur") {
+ window.opener.postMessage("FAIL: no inner blur", "*");
+ } else if (e.data == "opened") {
+ if (document.hasFocus()) {
+ w.close();
+ window.opener.postMessage("FAIL: focus not lost when other window opened", "*");
+ } else {
+ hadLostFocus = true;
+ window.frames[0].postMessage("hasfocus", "*");
+ }
+ } else if (e.data == "close") {
+ w.close();
+ } else if (e.data == "wrongfocus") {
+ w.close();
+ window.opener.postMessage("FAIL: hasFocus was true in iframe", "*");
+ }
+}
+window.onload = function() {
+ if (document.hasFocus()) {
+ hadInitialFocus = true;
+ window.frames[0].postMessage("focus", "*");
+ } else {
+ window.opener.postMessage("FAIL: did not have initial focus", "*");
+ }
+}
+</script>
+</head>
+<body>
+<iframe src="hasfocus-inner.html"></iframe>
+</body>
+</html>