summaryrefslogtreecommitdiffstats
path: root/gfx/layers/apz/test/mochitest/browser_test_autoscrolling_in_extension_popup_window.js
blob: 911af7548d5f5657e184196d469db4d9d34b52e0 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

Services.scriptloader.loadSubScript(
  "chrome://mochitests/content/browser/browser/components/extensions/test/browser/head.js",
  this
);
Services.scriptloader.loadSubScript(
  "chrome://mochitests/content/browser/browser/components/extensions/test/browser/head_browserAction.js",
  this
);

Services.scriptloader.loadSubScript(
  "chrome://mochitests/content/browser/gfx/layers/apz/test/mochitest/apz_test_utils.js",
  this
);

Services.scriptloader.loadSubScript(
  "chrome://mochitests/content/browser/gfx/layers/apz/test/mochitest/apz_test_native_event_utils.js",
  this
);

add_task(async () => {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      browser_action: {
        default_popup: "popup.html",
        browser_style: true,
      },
    },

    files: {
      "popup.html": `
        <html>
          <head>
            <meta charset="utf-8">
            <script src="popup.js"></script>
            <style>
              * {
                padding: 0;
                margin: 0;
              }
              body {
                height: 400px;
                width: 200px;
                overflow-y: auto;
                overflow-x: hidden;
              }
              li {
                display: flex;
                justify-content: center;
                align-items: center;
                height: 30vh;
                font-size: 200%;
              }
              li:nth-child(even){
                background-color: #ccc;
              }
            </style>
          </head>
          <body>
            <ul>
              <li>1</li>
              <li>2</li>
              <li>3</li>
              <li>4</li>
              <li>5</li>
              <li>6</li>
              <li>7</li>
              <li>8</li>
              <li>9</li>
              <li>10</li>
            </ul>
          </body>
        </html>`,
      "popup.js": function () {
        window.addEventListener(
          "mousemove",
          () => {
            dump("Got a mousemove event in the popup content document\n");
            browser.test.sendMessage("received-mousemove");
          },
          { once: true }
        );
        window.addEventListener(
          "scroll",
          () => {
            dump("Got a scroll event in the popup content document\n");
            browser.test.sendMessage("received-scroll");
          },
          { once: true }
        );
      },
    },
  });

  await extension.startup();

  await SpecialPowers.pushPrefEnv({ set: [["apz.popups.enabled", true]] });

  // Open the popup window of the extension.
  const browserForPopup = await openBrowserActionPanel(
    extension,
    undefined,
    true
  );

  if (!browserForPopup.isRemoteBrowser) {
    await closeBrowserAction(extension);
    await extension.unload();
    ok(
      true,
      "Skipping this test since the popup window doesn't have remote contents"
    );
    return;
  }

  // Flush APZ repaints and waits for MozAfterPaint to make sure APZ state is
  // stable.
  await promiseApzFlushedRepaintsInPopup(browserForPopup);

  const { screenX, screenY, viewId, presShellId } = await SpecialPowers.spawn(
    browserForPopup,
    [],
    () => {
      const winUtils = SpecialPowers.getDOMWindowUtils(content.window);
      return {
        screenX: content.window.mozInnerScreenX * content.devicePixelRatio,
        screenY: content.window.mozInnerScreenY * content.devicePixelRatio,
        viewId: winUtils.getViewId(content.document.documentElement),
        presShellId: winUtils.getPresShellId(),
      };
    }
  );

  // Before starting autoscroll we need to make sure a mousemove event has been
  // processed in the popup content so that subsequent mousemoves for autoscroll
  // will be properly processed in autoscroll animation.
  const mousemoveEventPromise = extension.awaitMessage("received-mousemove");

  const nativeMouseEventPromise = promiseNativeMouseEventWithAPZ({
    type: "mousemove",
    target: browserForPopup,
    offsetX: 100,
    offsetY: 50,
  });

  await Promise.all([nativeMouseEventPromise, mousemoveEventPromise]);

  const scrollEventPromise = extension.awaitMessage("received-scroll");

  // Start autoscrolling.
  ok(
    browserForPopup.browsingContext.startApzAutoscroll(
      screenX + 100,
      screenY + 50,
      viewId,
      presShellId
    )
  );

  // Send sequential mousemove events to cause autoscrolling.
  for (let i = 0; i < 10; i++) {
    await promiseNativeMouseEventWithAPZ({
      type: "mousemove",
      target: browserForPopup,
      offsetX: 100,
      offsetY: 50 + i * 10,
    });
  }

  // Flush APZ repaints and waits for MozAfterPaint to make sure the scroll has
  // been reflected on the main thread.
  const apzPromise = promiseApzFlushedRepaintsInPopup(browserForPopup);

  await Promise.all([apzPromise, scrollEventPromise]);

  const scrollY = await SpecialPowers.spawn(browserForPopup, [], () => {
    return content.window.scrollY;
  });
  ok(scrollY > 0, "Autoscrolling works in the popup window");

  browserForPopup.browsingContext.stopApzAutoscroll(viewId, presShellId);

  await closeBrowserAction(extension);

  await extension.unload();
});