summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/chrome/test_resize_move_windows.xhtml
blob: bf445238156deef70ecdc43b156df9901bf400c0 (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
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=565541
-->
<window title="Mozilla Bug 565541"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>

  <!-- test results are displayed in the html:body -->
  <body xmlns="http://www.w3.org/1999/xhtml">
  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=565541"
     target="_blank">Mozilla Bug 565541</a>
  </body>

  <!-- test code goes here -->
  <script type="application/javascript">
  <![CDATA[

  /** Test for Bug 565541 **/
  var previousX, previousY, previousWidth, previousHeight;

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

  function restoreValues()
  {
    window.top.moveTo(previousX, previousY);
    window.top.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 hitten and what to call next.
   * If times < 0, the event loop will be hitten as long as the condition isn't
   * true or the test doesn't time out.
   */
  function hitEventLoop(condition, test, times, next) {
    if (condition() || times == 0) {
      test();
      next();
      return;
    }

    setTimeout(hitEventLoop, 0, condition, test, times - 1, next);
  }

  function checkChangeIsEnabled(aWindow, aNext)
  {
    // 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.innerWidth != prevWidth && aWindow.innerHeight != prevHeight;
    }

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

      prevWidth = aWindow.innerWidth;
      prevHeight = aWindow.innerHeight;
    }

    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 posChangeConditionIgnoreLinux() {
      if (posChangeCondition()) {
        return true;
      }

      if (navigator.platform.includes('Linux')) {
        return true;
      }
    }

    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");

      aWindow.resizeTo(oWidth, oHeight);
    }

    /**
     * Size checks.
     */
    prevWidth = aWindow.innerWidth;
    prevHeight = aWindow.innerHeight;

    aWindow.resizeTo(getNewWidth(aWindow), getNewHeight(aWindow));
    hitEventLoop(sizeChangeCondition, sizeChangeTest, hits, function () {
      aWindow.resizeBy(getNewWidth(aWindow) - aWindow.innerWidth,
                       getNewHeight(aWindow) - aWindow.innerHeight);

    hitEventLoop(sizeChangeCondition, sizeChangeTest, hits, function () {
      prevWidth = aWindow.innerWidth;
      prevHeight = aWindow.innerHeight;
      aWindow.sizeToContent();

    hitEventLoop(sizeChangeCondition, sizeChangeTest, hits, function () {
    /**
     * Position checks.
     */
      prevX = aWindow.screenX;
      prevY = aWindow.screenY;

      aWindow.moveBy(getNewX(aWindow) - aWindow.screenX,
                     getNewY(aWindow) - aWindow.screenY);

    hitEventLoop(posChangeConditionIgnoreLinux, posChangeTest, hits, function () {
    /**
     * Outer width/height checks.
     */
      oWidth = aWindow.outerWidth;
      oHeight = aWindow.outerHeight;

      aWindow.resizeTo(oWidth * 2, oHeight* 2);

    hitEventLoop(outerChangeCondition, outerChangeTest, hits, aNext);
    });
    });
    });
    });
  }

  SimpleTest.waitForExplicitFinish();

  function test() {
    SimpleTest.waitForFocus(function() {
      if (screen.width <= 200 || screen.height <= 200) {
        todo(false, "The screen is too small to run this test.");
        SimpleTest.finish();
        return;
      }

      backValues();

      // We are in a chrome context, we can change the size and position.
      checkChangeIsEnabled(window.top, async function() {
        // We create a window and check that the size and position can be set with
        // window.open parameters and can be changed by the created window.
        var w = window.open("file_resize_move_windows_1.html", '',
          'width=170,height=170,screenX=25,screenY=25');

        await SimpleTest.promiseWaitForCondition(() => w.document.readyState == "complete");
        SimpleTest.waitForFocus(function() {
          w.wrappedJSObject.ok = SimpleTest.ok;
          w.wrappedJSObject.check();
          // The current window can change the size and position of the created one.
          checkChangeIsEnabled(w, async 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 a chrome context, the size and position can change.
            w = window.open("file_resize_move_windows_2.html", '', '');
            await SimpleTest.promiseWaitForCondition(() => w.document.readyState == "complete");
            SimpleTest.waitForFocus(function() {
              // The current window can change the size and position of the new tab.
              // Because we are in a chrome context.
              checkChangeIsEnabled(w, function() {
                w.close();
                restoreValues();
                SimpleTest.finish();
              });
            }, w, false);
          });
        }, w, false);
      });
    });
  }

  addLoadEvent(function onLoad() {
    test();
  });
  ]]>
  </script>
</window>