CloseWatcher: improve sendCloseRequest test helper

By targeting document.body, we can use it even in the popover test, and we don't need the extra <div> in various files.

Bug: 1171318
Change-Id: I1b9708da4874df680cd6a431019d3f33f41af4a3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4728408
Reviewed-by: Nate Chapin <japhet@chromium.org>
Commit-Queue: Domenic Denicola <domenic@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1178209}
diff --git a/close-watcher/abortsignal.html b/close-watcher/abortsignal.html
index ddd9452..9229b37 100644
--- a/close-watcher/abortsignal.html
+++ b/close-watcher/abortsignal.html
@@ -4,11 +4,11 @@
 <script src="/resources/testdriver.js"></script>
 <script src="/resources/testdriver-vendor.js"></script>
 <script src="/resources/testdriver-actions.js"></script>
+<script src="resources/helpers.js"></script>
 
-<div id='d' style='height: 100px; width: 100px'></div>
+<body>
 <script>
-// *not* \uu001B; see https://w3c.github.io/webdriver/#keyboard-actions
-const ESC = '\uE00C';
+// TODO(domenic): maybe update createRecordingCloseWatcher() to allow passing args and use it?
 
 test(() => {
   let watcher = new CloseWatcher({ signal: AbortSignal.abort() });
@@ -60,7 +60,7 @@
   watcher.oncancel = () => oncancel_called = true;
   watcher.onclose = () => onclose_called = true;
 
-  await test_driver.send_keys(document.getElementById('d'), ESC);
+  await sendCloseRequest();
 
   assert_false(oncancel_called);
   assert_false(onclose_called);
@@ -75,7 +75,7 @@
   watcher.onclose = () => onclose_called = true;
 
   controller.abort();
-  await test_driver.send_keys(document.getElementById('d'), ESC);
+  await sendCloseRequest();
 
   assert_false(oncancel_called);
   assert_false(onclose_called);
@@ -89,7 +89,7 @@
   watcher.oncancel = () => oncancel_call_count_++;
   watcher.onclose = () => onclose_call_count_++;
 
-  await test_driver.send_keys(document.getElementById('d'), ESC);
+  await sendCloseRequest();
   controller.abort();
 
   assert_equals(oncancel_call_count_, 0);
diff --git a/close-watcher/basic.html b/close-watcher/basic.html
index abaeb54..9951e54 100644
--- a/close-watcher/basic.html
+++ b/close-watcher/basic.html
@@ -6,7 +6,7 @@
 <script src="/resources/testdriver-actions.js"></script>
 <script src="resources/helpers.js"></script>
 
-<div id='d' style='height: 100px; width: 100px'></div>
+<body>
 <script>
 test(t => {
   let events = [];
diff --git a/close-watcher/esc-key.html b/close-watcher/esc-key.html
index c09a5e1..16fcce6 100644
--- a/close-watcher/esc-key.html
+++ b/close-watcher/esc-key.html
@@ -13,7 +13,7 @@
   assume that Esc is the close request for the platform being tested.
 -->
 
-<div id='d' style='height: 100px; width: 100px'></div>
+<body>
 <script>
 promise_test(async t => {
   let events = [];
diff --git a/close-watcher/event-properties.html b/close-watcher/event-properties.html
index 062693e..6a3dbeb 100644
--- a/close-watcher/event-properties.html
+++ b/close-watcher/event-properties.html
@@ -6,7 +6,7 @@
 <script src="/resources/testdriver-actions.js"></script>
 <script src="resources/helpers.js"></script>
 
-<div id='d' style='height: 100px; width: 100px'></div>
+<body>
 <script>
 promise_test(async t => {
   let closeEvent, cancelEvent;
diff --git a/close-watcher/inside-event-listeners.html b/close-watcher/inside-event-listeners.html
index 2699927..ac037fc 100644
--- a/close-watcher/inside-event-listeners.html
+++ b/close-watcher/inside-event-listeners.html
@@ -6,7 +6,7 @@
 <script src="/resources/testdriver-actions.js"></script>
 <script src="resources/helpers.js"></script>
 
-<div id='d' style='height: 100px; width: 100px'></div>
+<body>
 <script>
 promise_test(async t => {
   let events = [];
diff --git a/close-watcher/popover-closewatcher-multiple-plus-free.html b/close-watcher/popover-closewatcher-multiple-plus-free.html
index 2982e29..4913b14 100644
--- a/close-watcher/popover-closewatcher-multiple-plus-free.html
+++ b/close-watcher/popover-closewatcher-multiple-plus-free.html
@@ -6,6 +6,7 @@
 <script src="/resources/testdriver.js"></script>
 <script src="/resources/testdriver-vendor.js"></script>
 <script src="/resources/testdriver-actions.js"></script>
+<script src="resources/helpers.js"></script>
 
 <button id=b0>b0</button>
 
@@ -20,8 +21,6 @@
 </div>
 
 <script>
-const escapeKey = '\uE00C';
-
 promise_test(async () => {
   p1.showPopover();
   await test_driver.click(b1);
@@ -31,12 +30,12 @@
   assert_true(p2.matches(':popover-open'), 'p2 should be open.');
   assert_true(p3.matches(':popover-open'), 'p3 should be open.');
 
-  await test_driver.send_keys(p3, escapeKey);
+  await sendCloseRequest();
   assert_true(p1.matches(':popover-open'), 'first escape: p1 should be open.');
   assert_false(p2.matches(':popover-open'), 'first escape: p2 should be closed.');
   assert_false(p3.matches(':popover-open'), 'first escape: p3 should be closed.');
 
-  await test_driver.send_keys(p1, escapeKey);
+  await sendCloseRequest();
   assert_false(p1.matches(':popover-open'), 'second escape: p1 should be closed.');
   assert_false(p2.matches(':popover-open'), 'second escape: p2 should be closed.');
   assert_false(p3.matches(':popover-open'), 'second escape: p3 should be closed.');
diff --git a/close-watcher/popover-closewatcher.html b/close-watcher/popover-closewatcher.html
index cdf707a..b40ea2e 100644
--- a/close-watcher/popover-closewatcher.html
+++ b/close-watcher/popover-closewatcher.html
@@ -6,6 +6,7 @@
 <script src="/resources/testdriver.js"></script>
 <script src="/resources/testdriver-vendor.js"></script>
 <script src="/resources/testdriver-actions.js"></script>
+<script src="resources/helpers.js"></script>
 
 <button id=b0>b0</button>
 
@@ -20,8 +21,6 @@
 </div>
 
 <script>
-const escapeKey = '\uE00C';
-
 promise_test(async () => {
   p1.showPopover();
   p2.showPopover();
@@ -30,7 +29,7 @@
   assert_true(p2.matches(':popover-open'), 'p2 should be open.');
   assert_true(p3.matches(':popover-open'), 'p3 should be open.');
 
-  await test_driver.send_keys(p3, escapeKey);
+  await sendCloseRequest();
   assert_false(p1.matches(':popover-open'), 'p1 should be closed.');
   assert_false(p2.matches(':popover-open'), 'p2 should be closed.');
   assert_false(p3.matches(':popover-open'), 'p3 should be closed.');
@@ -47,17 +46,17 @@
   assert_true(p2.matches(':popover-open'), 'p2 should be open.');
   assert_true(p3.matches(':popover-open'), 'p3 should be open.');
 
-  await test_driver.send_keys(p3, escapeKey);
+  await sendCloseRequest();
   assert_true(p1.matches(':popover-open'), 'first escape: p1 should be open.');
   assert_true(p2.matches(':popover-open'), 'first escape: p2 should be open.');
   assert_false(p3.matches(':popover-open'), 'first escape: p3 should be closed.');
 
-  await test_driver.send_keys(p2, escapeKey);
+  await sendCloseRequest();
   assert_true(p1.matches(':popover-open'), 'second escape: p1 should be open.');
   assert_false(p2.matches(':popover-open'), 'second escape: p2 should be closed.');
   assert_false(p3.matches(':popover-open'), 'second escape: p3 should be closed.');
 
-  await test_driver.send_keys(p1, escapeKey);
+  await sendCloseRequest();
   assert_false(p1.matches(':popover-open'), 'third escape: p1 should be closed.');
   assert_false(p2.matches(':popover-open'), 'third escape: p2 should be closed.');
   assert_false(p3.matches(':popover-open'), 'third escape: p3 should be closed.');
diff --git a/close-watcher/resources/helpers.js b/close-watcher/resources/helpers.js
index ade8437..de64336 100644
--- a/close-watcher/resources/helpers.js
+++ b/close-watcher/resources/helpers.js
@@ -1,5 +1,3 @@
-// TODO(domenic): consider using these in all test files.
-
 window.createRecordingCloseWatcher = (t, events, name) => {
   const prefix = name === undefined ? "" : name + " ";;
 
@@ -16,10 +14,11 @@
 };
 
 window.sendEscKey = () => {
-  // *not* \uu001B; see https://w3c.github.io/webdriver/#keyboard-actions
-  const ESC = '\uE00C';
-
-  return test_driver.send_keys(document.getElementById("d"), ESC);
+  // Esc is \uE00C, *not* \uu001B; see https://w3c.github.io/webdriver/#keyboard-actions.
+  //
+  // It's important to target document.body, and not any element that might stop receiving events
+  // if a popover or dialog is making that element inert.
+  return test_driver.send_keys(document.body, '\uE00C');
 };
 
 // For now, we always use the Esc keypress as our close request. In
diff --git a/close-watcher/user-activation-multiple-plus-free.html b/close-watcher/user-activation-multiple-plus-free.html
index 728f37f..a94b479 100644
--- a/close-watcher/user-activation-multiple-plus-free.html
+++ b/close-watcher/user-activation-multiple-plus-free.html
@@ -6,7 +6,7 @@
 <script src="/resources/testdriver-actions.js"></script>
 <script src="resources/helpers.js"></script>
 
-<div id="d" style="height: 100px; width: 100px;"></div>
+<body>
 <script>
 
 // This test needs to be separate from user-activation.html since, unlike those,
diff --git a/close-watcher/user-activation.html b/close-watcher/user-activation.html
index 996a8f8..4591f54 100644
--- a/close-watcher/user-activation.html
+++ b/close-watcher/user-activation.html
@@ -6,7 +6,7 @@
 <script src="/resources/testdriver-actions.js"></script>
 <script src="resources/helpers.js"></script>
 
-<div id="d" style="height: 100px; width: 100px;"></div>
+<body>
 <script>
 promise_test(async t => {
   const events = [];