summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/browser/browser_cancel_starting_autoscrolling_requested_by_background_tab.js
blob: 989c91f2fb16d8280c2cf5fecccc79f70fc69694 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

add_task(async function testStopStartingAutoScroll() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["general.autoScroll", true],
      ["middlemouse.contentLoadURL", false],
      ["test.events.async.enabled", true],
      [
        "accessibility.mouse_focuses_formcontrol",
        !navigator.platform.includes("Mac"),
      ],
    ],
  });

  await BrowserTestUtils.withNewTab(
    "https://example.com/browser/toolkit/content/tests/browser/file_empty.html",
    async function (browser) {
      async function doTest({
        aInnerHTML,
        aDescription,
        aExpectedActiveElement,
      }) {
        await SpecialPowers.spawn(browser, [aInnerHTML], async contentHTML => {
          // eslint-disable-next-line no-unsanitized/property
          content.document.body.innerHTML = contentHTML;
          content.document.documentElement.scrollTop; // Flush layout.
          const iframe = content.document.querySelector("iframe");
          // If the test page has an iframe, we need to ensure it has loaded.
          if (!iframe || iframe.contentDocument?.readyState == "complete") {
            return;
          }
          // It's too late to check "load" event.  Let's check
          // Document#readyState instead.
          await ContentTaskUtils.waitForCondition(
            () => iframe.contentDocument?.readyState == "complete",
            "Waiting for loading the subdocument"
          );
        });

        let autoScroller;
        let onPopupShown = event => {
          if (event.originalTarget.id !== "autoscroller") {
            return false;
          }
          autoScroller = event.originalTarget;
          info(`${aDescription}: "popupshown" event is fired`);
          autoScroller.getBoundingClientRect(); // Flush layout of the autoscroller
          return true;
        };
        window.addEventListener("popupshown", onPopupShown, { capture: true });
        registerCleanupFunction(() => {
          window.removeEventListener("popupshown", onPopupShown, {
            capture: true,
          });
        });

        let waitForNewTabForeground = BrowserTestUtils.waitForEvent(
          gBrowser,
          "TabSwitchDone"
        );
        await EventUtils.promiseNativeMouseEvent({
          type: "mousedown",
          button: 1, // middle click
          target: browser,
          atCenter: true,
        });
        info(`${aDescription}: Waiting for active tab switched...`);
        await waitForNewTabForeground;
        // To confirm that autoscrolling won't start accidentally, we should
        // wait a while.
        // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
        await new Promise(resolve => setTimeout(resolve, 500));
        is(
          autoScroller,
          undefined,
          `${aDescription}: autoscroller should not be open because requested tab is now in background`
        );
        // Clean up
        await EventUtils.promiseNativeMouseEvent({
          type: "mouseup",
          button: 1, // middle click
          target: browser,
          atCenter: true,
        }); // release implicit capture
        EventUtils.synthesizeKey("KEY_Escape"); // To close unexpected autoscroller
        isnot(
          browser,
          gBrowser.selectedBrowser,
          `${aDescription}: The clicked tab shouldn't be foreground tab`
        );
        is(
          gBrowser.selectedTab,
          gBrowser.tabs[gBrowser.tabs.length - 1],
          `${aDescription}: The opened tab should be foreground tab`
        );
        await SpecialPowers.spawn(
          browser,
          [aExpectedActiveElement, aDescription],
          (expectedActiveElement, description) => {
            if (expectedActiveElement != null) {
              if (expectedActiveElement == "iframe") {
                // Check only whether the subdocument gets focus.
                return;
              }
              Assert.equal(
                content.document.activeElement,
                content.document.querySelector(expectedActiveElement),
                `${description}: Active element should be the result of querySelector("${expectedActiveElement}")`
              );
            } else {
              Assert.deepEqual(
                content.document.activeElement,
                new content.window.Object(null),
                `${description}: No element should be active`
              );
            }
          }
        );
        gBrowser.removeTab(gBrowser.tabs[gBrowser.tabs.length - 1]);
        await SimpleTest.promiseFocus(browser);
        if (aExpectedActiveElement == "iframe") {
          await SpecialPowers.spawn(browser, [aDescription], description => {
            // XXX Due to no `Assert#todo`, this checks opposite result.
            Assert.ok(
              !content.document
                .querySelector("iframe")
                .contentDocument.hasFocus(),
              `TODO: ${description}: The subdocument should have focus when the tab gets foreground`
            );
          });
        }
      }
      await doTest({
        aInnerHTML: `<div style="height: 10000px;" onmousedown="window.open('https://example.com/', '_blank')">Click to open new tab</div>`,
        aDescription: "Clicking non-focusable <div> with middle mouse button",
        aExpectedActiveElement: null,
      });
      await doTest({
        aInnerHTML: `<button style="height: 10000px; width: 100%" onmousedown="window.open('https://example.com/', '_blank')">Click to open new tab</button>`,
        aDescription: `Clicking focusable <button> with middle mouse button`,
        aExpectedActiveElement: navigator.platform.includes("Mac")
          ? null
          : "button",
      });
      await doTest({
        aInnerHTML: `<iframe style="height: 90vh; width: 90vw" srcdoc="<div onmousedown='window.open(\`https://example.com/\`, \`_blank\`)' style='width: 100%; height: 10000px'>Click to open new tab"></iframe>`,
        aDescription: `Clicking non-focusable <div> in <iframe> with middle mouse button`,
        aExpectedActiveElement: "iframe",
      });
    }
  );
});