summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/bugs/test_resize_move_windows.html
blob: 11b2d90f218db5b7ca2d400a16c7c770038a900b (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=565541
-->
<head>
  <title>Test for Bug 565541</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=565541">Mozilla Bug 565541</a>
<p id="display"></p>
<div id="content" style="display: none">
  
</div>
<pre id="test">
<script>

/** Test for Bug 565541 **/

SimpleTest.waitForExplicitFinish();

var previousX, previousY, previousWidth, previousHeight;

["innerWidth", "innerHeight", "screenX", "screenY", "outerWidth",
 "outerHeight"].map(function(name) {
  window[name+"Getter"] = Object.getOwnPropertyDescriptor(window, name).get;
});

function backValues()
{
  previousX = window.screenX;
  previousY = window.screenY;
  previousWidth = window.outerWidth;
  previousHeight = window.outerHeight;
}

function restoreValues()
{
  window.moveTo(previousX, previousY);
  window.resizeTo(previousWidth, previousHeight);
}

function getNewWidth(aWindow)
{
  return (aWindow.innerWidth > (screen.width / 2)) ? 100 : screen.width;
}

function getNewHeight(aWindow)
{
  return (aWindow.innerHeight > (screen.height / 2)) ? 100 : screen.height;
}

function getNewX(aWindow)
{
  return (aWindow.screenX > ((screen.width - aWindow.outerWidth) / 2))
    ? 0 : screen.width - aWindow.outerWidth;
}

function getNewY(aWindow)
{
  return (aWindow.screenY > ((screen.height - aWindow.outerHeight) / 2))
    ? 0 : screen.height - aWindow.outerHeight;
}

/**
 * hitEventLoop is called when we want to check something but we can't rely on
 * an event or a specific number of event loop hiting.
 * This method can be called by specifying a condition, a test (using SimpleTest
 * API), how many times the event loop has to be hit and what to call next.
 * If times < 0, the event loop will be hit as long as the condition isn't
 * true or the test doesn't time out.
 */
function hitEventLoop(condition, test, times) {
  return new Promise(function(resolve, reject) {
    function doMagic() {
      if (condition() || times == 0) {
        test();
        resolve();
        return;
      }

      times--;
      setTimeout(doMagic, 0);
    }

    doMagic();
  });
}

function checkChangeIsDisabled(aWindow) {
  // We want to check that nothing has changed. Having a high value would take
  // too much time. Worse thing that could happen is random green.
  var hits = 5;

  function getProp(propName) {
    return window[propName + "Getter"].call(aWindow);
  }

  var originalWidth = getProp("innerWidth");
  var originalHeight = getProp("innerHeight");

  var originalX = getProp("screenX");
  var originalY = getProp("screenY");

  var oWidth = getProp("outerWidth");
  var oHeight = getProp("outerHeight");

  function changeCondition() {
    return aWindow.innerWidth != originalWidth ||
           aWindow.innerHeight != originalHeight ||
           aWindow.screenX != originalX || aWindow.screenY != originalY ||
           aWindow.outerWidth != oWidth || aWindow.outerHeight != oHeight;
  }

  function changeTest() {
    is(getProp("innerWidth"), originalWidth,
       "Window width shouldn't have changed");
    is(getProp("innerHeight"), originalHeight,
       "Window height shouldn't have changed");
    is(getProp("screenX"), originalX,
       "Window x position shouldn't have changed");
    is(getProp("screenY"), originalY,
       "Window y position shouldn't have changed");
    is(getProp("outerWidth"), oWidth,
       "Window outerWidth shouldn't have changed");
    is(getProp("outerHeight"), oHeight,
       "Window outerHeight shouldn't have changed");
  }

  /**
   * Size changes.
   */
  var newWidth = getNewWidth(aWindow);
  var newHeight = getNewHeight(aWindow);

  aWindow.resizeTo(newWidth, newHeight);

  aWindow.resizeBy(newWidth - aWindow.innerWidth,
                   newHeight - aWindow.innerHeight);

  aWindow.sizeToContent();

  /**
   * Position checks.
   */
  var newX = getNewX(aWindow);
  var newY = getNewY(aWindow);

  aWindow.moveTo(newX, newY);

  aWindow.moveBy(newX - aWindow.screenX,
                 newY - aWindow.screenY);

  /**
   * Outer width/height checks.
   */
  aWindow.resizeTo(aWindow.outerWidth * 2, aWindow.outerHeight * 2);

  // We did a lot of changes. Now, we are going to wait and see if something
  // happens.
  // NOTE: if this happens to fail, you will have to check manually which
  // operation has been accepted.
  return hitEventLoop(changeCondition, changeTest, hits);
}

function checkChangeIsEnabled(aWindow, aNext)
{
  info(`checkChangeIsEnabled start`);
  // Something should happen. We are not going to go to the next test until
  // it does.
  var hits = -1;

  var prevWidth;
  var prevHeight;

  var prevX;
  var prevY;

  var oWidth;
  var oHeight;

  function sizeChangeCondition() {
    return aWindow.outerWidth != prevWidth && aWindow.outerHeight != prevHeight;
  }

  function sizeChangeTest() {
    isnot(aWindow.outerWidth, prevWidth, "Window width should have changed");
    isnot(aWindow.outerHeight, prevHeight, "Window height should have changed");

    prevWidth = aWindow.outerWidth;
    prevHeight = aWindow.outerHeight;
  }

  function posChangeCondition() {
    // With GTK, sometimes, only one dimension changes.
    if (navigator.platform.includes('Linux')) {
      return aWindow.screenX != prevX || aWindow.screenY != prevY;
    }
    return aWindow.screenX != prevX && aWindow.screenY != prevY;
  }

  function posChangeTest() {
    // With GTK, sometimes, only one dimension changes.
    if (navigator.platform.includes('Linux')) {
      // With GTK, sometimes, aWindow.screenX changes during two calls.
      // So we call it once and save the returned value.
      var x = aWindow.screenX;
      var y = aWindow.screenY;
      if (x != prevX) {
        isnot(x, prevX, "Window x position should have changed");
      }
      if (y != prevY) {
        isnot(y, prevY, "Window y position should have changed");
      }
    } else {
      isnot(aWindow.screenX, prevX, "Window x position should have changed");
      isnot(aWindow.screenY, prevY, "Window y position should have changed");
    }

    prevX = aWindow.screenX;
    prevY = aWindow.screenY;
  }

  function outerChangeCondition() {
    return aWindow.outerWidth != oWidth && aWindow.outerHeight != oHeight;
  }

  function outerChangeTest() {
    isnot(aWindow.outerWidth, oWidth, "Window outerWidth should have changed");
    isnot(aWindow.outerHeight, oHeight, "Window outerHeight should have changed");
  }

  /**
   * Size checks.
   */
  prevWidth = aWindow.outerWidth;
  prevHeight = aWindow.outerHeight;

  aWindow.resizeTo(getNewWidth(aWindow), getNewHeight(aWindow));
  hitEventLoop(sizeChangeCondition, sizeChangeTest, hits)
  .then(function() {
    aWindow.resizeTo(getNewWidth(aWindow), getNewHeight(aWindow));
    return hitEventLoop(sizeChangeCondition, sizeChangeTest, hits);
  })
  .then(function () {
    aWindow.resizeBy(getNewWidth(aWindow) - aWindow.innerWidth,
                     getNewHeight(aWindow) - aWindow.innerHeight);
    return hitEventLoop(sizeChangeCondition, sizeChangeTest, hits);
  })
  .then(function() {
    aWindow.sizeToContent();
    return hitEventLoop(sizeChangeCondition, sizeChangeTest, hits);
  })
  .then(function() {
    /**
     * Position checks.
     */
    prevX = aWindow.screenX;
    prevY = aWindow.screenY;

    aWindow.moveTo(getNewX(aWindow), getNewY(aWindow));
    return hitEventLoop(posChangeCondition, posChangeTest, hits);
  })
  .then(function() {
    prevX = aWindow.screenX;
    prevY = aWindow.screenY;

    aWindow.moveBy(getNewX(aWindow) - aWindow.screenX,
                   getNewY(aWindow) - aWindow.screenY);
    return hitEventLoop(posChangeCondition, posChangeTest, hits);
  })
  .then(function() {
    /**
     * Outer width/height checks.
     */
    oWidth = aWindow.outerWidth;
    oHeight = aWindow.outerHeight;

    aWindow.resizeTo(oWidth * 2, oHeight * 2);
    return hitEventLoop(outerChangeCondition, outerChangeTest, hits);
  })
  .then(function() {
    let origWidth = oWidth;
    let origHeight = oHeight;

    oWidth = aWindow.outerWidth;
    oHeight = aWindow.outerHeight;

    aWindow.resizeTo(origWidth, origHeight);
    return hitEventLoop(outerChangeCondition, outerChangeTest, hits);
  })
  .then(aNext);
}

SpecialPowers.pushPrefEnv({
  "set": [["dom.disable_window_move_resize", false]]},
                          function() {
SimpleTest.waitForFocus(function() {
  if (screen.width <= 200 || screen.height <= 200) {
    todo(false, "The screen needs to be bigger than 200px*200px to run this test.");
    SimpleTest.finish();
    return;
  }

  backValues();

  // The current window can't change it's own size and position.
  checkChangeIsDisabled(window).then(function() {
    info(`Change was disabled`);
    // We create a window and check that it can change its own size and position.
    // However, passing size/position parameters to window.open should work.
    var w = window.open("file_resize_move_windows_1.html", '',
      'width=170,height=170,screenX=65,screenY=65');

    SimpleTest.waitForFocus(function() {
      w.check(function() {
        // The current window can change the size and position of the created one.
        checkChangeIsEnabled(w, function() {
          w.close();

          // If we call window.open with an empty string as a third parameter,
          // by default, it will create a new tab instead of a new window.
          // In that case, we shouldn't allow the caller to change the size/position.
          w = window.open("file_resize_move_windows_2.html", '', '');
          SimpleTest.waitForFocus(function() {
            w.check(function() {

              // The current window can't change the size and position of the new tab.
              checkChangeIsDisabled(w).then(function() {
                w.close();

                restoreValues();
                SimpleTest.finish();
              });
            });
          }, w, false);
        });
      })
    }, w, false);
  });
});
}); // SpecialPowers.pushPrefEnv()

</script>
</pre>
</body>
</html>