summaryrefslogtreecommitdiffstats
path: root/gfx/layers/apz/test/mochitest/browser_test_select_zoom.js
blob: 84baf8e5acadefedd4f303b275e02cd85c8d6f36 (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
190
191
192
193
194
195
/* This test is a a mash up of
     https://searchfox.org/mozilla-central/rev/559b25eb41c1cbffcb90a34e008b8288312fcd25/gfx/layers/apz/test/mochitest/browser_test_group_fission.js
     https://searchfox.org/mozilla-central/rev/559b25eb41c1cbffcb90a34e008b8288312fcd25/gfx/layers/apz/test/mochitest/helper_basic_zoom.html
     https://searchfox.org/mozilla-central/rev/559b25eb41c1cbffcb90a34e008b8288312fcd25/browser/base/content/test/forms/browser_selectpopup.js
*/

/* import-globals-from helper_browser_test_utils.js */
Services.scriptloader.loadSubScript(
  new URL("helper_browser_test_utils.js", gTestPath).href,
  this
);

add_task(async function setup_pref() {
  await SpecialPowers.pushPrefEnv({
    set: [
      // Dropping the touch slop to 0 makes the tests easier to write because
      // we can just do a one-pixel drag to get over the pan threshold rather
      // than having to hard-code some larger value.
      ["apz.touch_start_tolerance", "0.0"],
      // The subtests in this test do touch-drags to pan the page, but we don't
      // want those pans to turn into fling animations, so we increase the
      // fling-min threshold velocity to an arbitrarily large value.
      ["apz.fling_min_velocity_threshold", "10000"],
    ],
  });
});

// This test opens a select popup after pinch (apz) zooming has happened.
add_task(async function () {
  function httpURL(filename) {
    let chromeURL = getRootDirectory(gTestPath) + filename;
    //return chromeURL;
    return chromeURL.replace(
      "chrome://mochitests/content/",
      "http://mochi.test:8888/"
    );
  }

  const pageUrl = httpURL("helper_test_select_zoom.html");
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, pageUrl);

  await SpecialPowers.spawn(tab.linkedBrowser, [], async () => {
    const input = content.document.getElementById("select");
    const focusPromise = new Promise(resolve => {
      input.addEventListener("focus", resolve, { once: true });
    });
    input.focus();
    await focusPromise;
  });

  await SpecialPowers.spawn(tab.linkedBrowser, [], async () => {
    await content.wrappedJSObject.waitUntilApzStable();
  });

  const initial_resolution = await SpecialPowers.spawn(
    tab.linkedBrowser,
    [],
    () => {
      return content.window.windowUtils.getResolution();
    }
  );

  const initial_rect = await SpecialPowers.spawn(tab.linkedBrowser, [], () => {
    return content.wrappedJSObject.getSelectRect();
  });

  ok(
    initial_resolution > 0,
    "The initial_resolution is " +
      initial_resolution +
      ", which is some sane value"
  );

  // First, get the position of the select popup when no translations have been applied.
  const selectPopup = await openSelectPopup();

  let popup_initial_rect = selectPopup.getBoundingClientRect();
  let popupInitialX = popup_initial_rect.left;
  let popupInitialY = popup_initial_rect.top;

  await hideSelectPopup();

  ok(popupInitialX > 0, "select position before zooming (x) " + popupInitialX);
  ok(popupInitialY > 0, "select position before zooming (y) " + popupInitialY);

  await SpecialPowers.spawn(tab.linkedBrowser, [], async () => {
    await content.wrappedJSObject.pinchZoomInWithTouch(150, 300);
  });

  // Flush state and get the resolution we're at now
  await SpecialPowers.spawn(tab.linkedBrowser, [], async () => {
    await content.wrappedJSObject.promiseApzFlushedRepaints();
  });

  const final_resolution = await SpecialPowers.spawn(
    tab.linkedBrowser,
    [],
    () => {
      return content.window.windowUtils.getResolution();
    }
  );

  ok(
    final_resolution > initial_resolution,
    "The final resolution (" +
      final_resolution +
      ") is greater after zooming in"
  );

  const final_rect = await SpecialPowers.spawn(tab.linkedBrowser, [], () => {
    return content.wrappedJSObject.getSelectRect();
  });

  await openSelectPopup();

  let popupRect = selectPopup.getBoundingClientRect();
  ok(
    Math.abs(popupRect.left - popupInitialX) > 1,
    "popup should have moved by more than one pixel (x) " +
      popupRect.left +
      " " +
      popupInitialX
  );
  ok(
    Math.abs(popupRect.top - popupInitialY) > 1,
    "popup should have moved by more than one pixel (y) " +
      popupRect.top +
      " " +
      popupInitialY
  );

  ok(
    Math.abs(
      final_rect.left - initial_rect.left - (popupRect.left - popupInitialX)
    ) < 1,
    "popup should have moved approximately the same as the element (x)"
  );
  let tolerance = navigator.platform.includes("Linux") ? final_rect.height : 1;
  ok(
    Math.abs(
      final_rect.top - initial_rect.top - (popupRect.top - popupInitialY)
    ) < tolerance,
    "popup should have moved approximately the same as the element (y)"
  );

  ok(
    true,
    "initial " +
      initial_rect.left +
      " " +
      initial_rect.top +
      " " +
      initial_rect.width +
      " " +
      initial_rect.height
  );
  ok(
    true,
    "final " +
      final_rect.left +
      " " +
      final_rect.top +
      " " +
      final_rect.width +
      " " +
      final_rect.height
  );

  ok(
    true,
    "initial popup " +
      popup_initial_rect.left +
      " " +
      popup_initial_rect.top +
      " " +
      popup_initial_rect.width +
      " " +
      popup_initial_rect.height
  );
  ok(
    true,
    "final popup " +
      popupRect.left +
      " " +
      popupRect.top +
      " " +
      popupRect.width +
      " " +
      popupRect.height
  );

  await hideSelectPopup();

  BrowserTestUtils.removeTab(tab);
});