1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 var callbackPass = chrome.test.callbackPass;
6 var callbackFail = chrome.test.callbackFail;
7 var defaultFuzzFactor = 1;
9 function assertFuzzyEq(expected, actual, fuzzFactor, message) {
11 message = "Expected: " + expected + "; actual: " + actual + "; "
12 + "fuzzyFactor: " + fuzzFactor;
15 chrome.test.assertTrue(actual - fuzzFactor <= expected
16 && actual + fuzzFactor >= expected, message);
18 if (actual != expected) {
19 console.log("FUZZ: a factor of " + Math.abs(actual - expected) +
24 // This helper will verify that |check| returns true. If it does not, it will do
25 // a trip to the event loop and will try again until |check| returns true. At
26 // which points |callback| will be called.
27 // NOTE: if the test fails, it will timeout.
28 function eventLoopCheck(check, callback) {
32 setTimeout(callbackPass(function() { eventLoopCheck(check, callback); }));
36 // This help function will call the callback when the window passed to it will
37 // be loaded. The callback will have the AppWindow passed as a parameter.
38 function waitForLoad(win, callback) {
39 var window = win.contentWindow;
41 if (window.document.readyState == 'complete') {
46 window.addEventListener('load', callbackPass(function() {
47 window.removeEventListener('load', arguments.callee);
52 function assertConstraintsUnspecified(win) {
53 chrome.test.assertEq(null, win.innerBounds.minWidth);
54 chrome.test.assertEq(null, win.innerBounds.minHeight);
55 chrome.test.assertEq(null, win.innerBounds.maxWidth);
56 chrome.test.assertEq(null, win.innerBounds.maxHeight);
57 chrome.test.assertEq(null, win.outerBounds.minWidth);
58 chrome.test.assertEq(null, win.outerBounds.minHeight);
59 chrome.test.assertEq(null, win.outerBounds.maxWidth);
60 chrome.test.assertEq(null, win.outerBounds.maxHeight);
63 function assertBoundsConsistent(win) {
64 // Ensure that the inner and outer bounds are consistent. Since platforms
65 // have different frame padding, we cannot check the sizes precisely.
66 // It is a reasonable assumption that all platforms will have a title bar at
67 // the top of the window.
68 chrome.test.assertTrue(win.innerBounds.left >= win.outerBounds.left);
69 chrome.test.assertTrue(win.innerBounds.top > win.outerBounds.top);
70 chrome.test.assertTrue(win.innerBounds.width <= win.outerBounds.width);
71 chrome.test.assertTrue(win.innerBounds.height < win.outerBounds.height);
73 if (win.innerBounds.minWidth === null)
74 chrome.test.assertEq(null, win.outerBounds.minWidth);
76 chrome.test.assertTrue(
77 win.innerBounds.minWidth <= win.outerBounds.minWidth);
79 if (win.innerBounds.minHeight === null)
80 chrome.test.assertEq(null, win.outerBounds.minHeight);
82 chrome.test.assertTrue(
83 win.innerBounds.minHeight < win.outerBounds.minHeight);
85 if (win.innerBounds.maxWidth === null)
86 chrome.test.assertEq(null, win.outerBounds.maxWidth);
88 chrome.test.assertTrue(
89 win.innerBounds.maxWidth <= win.outerBounds.maxWidth);
91 if (win.innerBounds.maxHeight === null)
92 chrome.test.assertEq(null, win.outerBounds.maxHeight);
94 chrome.test.assertTrue(
95 win.innerBounds.maxHeight < win.outerBounds.maxHeight);
98 function testConflictingBoundsProperty(propertyName) {
100 var outerBounds = {};
101 innerBounds[propertyName] = 20;
102 outerBounds[propertyName] = 20;
103 chrome.app.window.create('test.html', {
104 innerBounds: innerBounds,
105 outerBounds: outerBounds
106 }, callbackFail('The ' + propertyName + ' property cannot be specified for ' +
107 'both inner and outer bounds.')
111 function assertBoundsEq(expectedBounds, actualBounds) {
112 chrome.test.assertEq(expectedBounds.left, actualBounds.left);
113 chrome.test.assertEq(expectedBounds.top, actualBounds.top);
114 chrome.test.assertEq(expectedBounds.width, actualBounds.width);
115 chrome.test.assertEq(expectedBounds.height, actualBounds.height);
118 function assertConstraintsEq(expectedConstraints, actualConstraints) {
119 chrome.test.assertEq(expectedConstraints.minWidth,
120 actualConstraints.minWidth);
121 chrome.test.assertEq(expectedConstraints.minHeight,
122 actualConstraints.minHeight);
123 chrome.test.assertEq(expectedConstraints.maxWidth,
124 actualConstraints.maxWidth);
125 chrome.test.assertEq(expectedConstraints.maxHeight,
126 actualConstraints.maxHeight);
129 function runSetBoundsTest(boundsType, initialState, changeFields,
130 expectedBounds, hasConstraints) {
131 var createOptions = {};
132 createOptions[boundsType] = initialState;
133 chrome.app.window.create('test.html', createOptions, callbackPass(
135 // Change the bounds.
136 if (typeof(changeFields.left) !== 'undefined' &&
137 typeof(changeFields.top) !== 'undefined') {
138 win[boundsType].setPosition(changeFields.left, changeFields.top);
139 } else if (typeof(changeFields.left) !== 'undefined')
140 win[boundsType].left = changeFields.left;
141 else if (typeof(changeFields.top) !== 'undefined')
142 win[boundsType].top = changeFields.top;
144 if (typeof(changeFields.width) !== 'undefined' &&
145 typeof(changeFields.height) !== 'undefined') {
146 win[boundsType].setSize(changeFields.width, changeFields.height);
147 } else if (typeof(changeFields.width) !== 'undefined')
148 win[boundsType].width = changeFields.width;
149 else if (typeof(changeFields.height) !== 'undefined')
150 win[boundsType].height = changeFields.height;
152 // Dummy call to wait for bounds to be changed in the browser.
153 chrome.test.waitForRoundTrip('msg', callbackPass(function(msg) {
154 assertBoundsConsistent(win);
155 assertBoundsEq(expectedBounds, win[boundsType]);
157 assertConstraintsUnspecified(win);
163 function runSetConstraintsTest(boundsType, initialState, changeFields,
164 expectedConstraints, expectedBounds) {
165 var createOptions = {};
166 createOptions[boundsType] = initialState;
167 chrome.app.window.create('test.html', createOptions, callbackPass(
169 assertConstraintsEq(initialState, win[boundsType]);
171 // Change the constraints.
172 if (typeof(changeFields.minWidth) !== 'undefined' &&
173 typeof(changeFields.minHeight) !== 'undefined') {
174 win[boundsType].setMinimumSize(changeFields.minWidth,
175 changeFields.minHeight);
176 } else if (typeof(changeFields.minWidth) !== 'undefined')
177 win[boundsType].minWidth = changeFields.minWidth;
178 else if (typeof(changeFields.minHeight) !== 'undefined')
179 win[boundsType].minHeight = changeFields.minHeight;
181 if (typeof(changeFields.maxWidth) !== 'undefined' &&
182 typeof(changeFields.maxHeight) !== 'undefined') {
183 win[boundsType].setMaximumSize(changeFields.maxWidth,
184 changeFields.maxHeight);
185 } else if (typeof(changeFields.maxWidth) !== 'undefined')
186 win[boundsType].maxWidth = changeFields.maxWidth;
187 else if (typeof(changeFields.maxHeight) !== 'undefined')
188 win[boundsType].maxHeight = changeFields.maxHeight;
190 // Dummy call to wait for the constraints to be changed in the browser.
191 chrome.test.waitForRoundTrip('msg', callbackPass(function(msg) {
192 assertBoundsConsistent(win);
193 assertConstraintsEq(expectedConstraints, win[boundsType]);
194 if (expectedBounds) {
195 chrome.test.assertEq(expectedBounds.width, win[boundsType].width);
196 chrome.test.assertEq(expectedBounds.height, win[boundsType].height);
203 function testCreate() {
204 chrome.test.runTests([
206 chrome.app.window.create('test.html',
208 callbackPass(function(win) {
209 chrome.test.assertEq(typeof win.contentWindow.window, 'object');
210 chrome.test.assertTrue(
211 typeof win.contentWindow.document !== 'undefined');
212 chrome.test.assertFalse(
213 'about:blank' === win.contentWindow.location.href);
214 var cw = win.contentWindow.chrome.app.window.current();
215 chrome.test.assertEq(cw, win);
216 chrome.test.assertEq('testId', cw.id);
217 win.contentWindow.close();
221 function badWindow() {
222 chrome.app.window.create('404.html', callbackPass(function(win) {
223 chrome.test.assertTrue(typeof win === 'undefined');
224 // TODO(mlamouri): because |win| is not defined, we can not close that
229 function loadEvent() {
230 chrome.app.window.create('test.html', callbackPass(function(win) {
231 win.contentWindow.onload = callbackPass(function() {
232 chrome.test.assertEq(document.readyState, 'complete');
233 win.contentWindow.close();
238 function multiWindow() {
239 chrome.test.assertTrue(null === chrome.app.window.current());
240 chrome.app.window.create('test.html',
242 callbackPass(function(win1) {
243 chrome.app.window.create('test.html',
245 callbackPass(function(win2) {
246 var cw1 = win1.contentWindow.chrome.app.window.current();
247 var cw2 = win2.contentWindow.chrome.app.window.current();
248 chrome.test.assertEq('testId1', cw1.id);
249 chrome.test.assertEq('testId2', cw2.id);
250 chrome.test.assertTrue(cw1 === win1);
251 chrome.test.assertTrue(cw2 === win2);
252 chrome.test.assertFalse(cw1 === cw2);
253 win1.contentWindow.close();
254 win2.contentWindow.close();
259 function hiddenAndNormal() {
260 chrome.app.window.create('test.html',
262 callbackPass(function(win1) {
263 chrome.app.window.create('test.html',
265 callbackPass(function(win2) {
266 win1.contentWindow.close();
267 win2.contentWindow.close();
274 function testDeprecatedBounds() {
275 chrome.test.runTests([
276 function contentSize() {
277 var options = { bounds: { left: 0, top: 0, width: 250, height: 200 } };
278 chrome.app.window.create('test.html', options, callbackPass(
280 var bounds = win.getBounds();
281 chrome.test.assertEq(options.bounds.width, bounds.width);
282 chrome.test.assertEq(options.bounds.height, bounds.height);
283 chrome.test.assertEq(options.bounds.width, win.innerBounds.width);
284 chrome.test.assertEq(options.bounds.height, win.innerBounds.height);
289 function windowPosition() {
290 var options = { bounds: { left: 0, top: 0, left: 250, top: 200 } };
291 chrome.app.window.create('test.html', options, callbackPass(
293 var bounds = win.getBounds();
294 chrome.test.assertEq(options.bounds.left, bounds.left);
295 chrome.test.assertEq(options.bounds.top, bounds.top);
296 chrome.test.assertEq(options.bounds.left, win.outerBounds.left);
297 chrome.test.assertEq(options.bounds.top, win.outerBounds.top);
304 bounds: { left: 0, top: 0, width: 250, height: 250 },
305 minWidth: 400, minHeight: 450
307 chrome.app.window.create('test.html', options, callbackPass(
309 var bounds = win.getBounds();
310 chrome.test.assertEq(options.minWidth, bounds.width);
311 chrome.test.assertEq(options.minHeight, bounds.height);
318 bounds: { left: 0, top: 0, width: 250, height: 250 },
319 maxWidth: 200, maxHeight: 150
321 chrome.app.window.create('test.html', options, callbackPass(
323 var bounds = win.getBounds();
324 chrome.test.assertEq(options.maxWidth, bounds.width);
325 chrome.test.assertEq(options.maxHeight, bounds.height);
330 function minAndMaxSize() {
332 bounds: { left: 0, top: 0, width: 250, height: 250 },
333 minWidth: 400, minHeight: 450,
334 maxWidth: 200, maxHeight: 150
336 chrome.app.window.create('test.html', options, callbackPass(
338 var bounds = win.getBounds();
339 chrome.test.assertEq(options.minWidth, bounds.width);
340 chrome.test.assertEq(options.minHeight, bounds.height);
345 function simpleSetBounds() {
346 chrome.app.window.create('test.html', {
347 bounds: { left: 0, top: 0, width: 250, height: 200 }
348 }, callbackPass(function(win) {
349 var newBounds = {width: 400, height: 450};
350 win.setBounds(newBounds);
351 chrome.test.waitForRoundTrip('msg', callbackPass(function() {
352 var bounds = win.getBounds();
353 chrome.test.assertEq(newBounds.width, bounds.width);
354 chrome.test.assertEq(newBounds.height, bounds.height);
360 function heightOnlySetBounds() {
361 chrome.app.window.create('test.html', {
362 bounds: { left: 0, top: 0, width: 300, height: 256 }
363 }, callbackPass(function(win) {
364 win.setBounds({ height: 300 });
365 chrome.test.waitForRoundTrip('msg', callbackPass(function() {
366 var bounds = win.getBounds();
367 chrome.test.assertEq(300, bounds.width);
368 chrome.test.assertEq(300, bounds.height);
376 function testInitialBounds() {
377 chrome.test.runTests([
378 function testNoOptions() {
379 chrome.app.window.create('test.html', {
380 }, callbackPass(function(win) {
381 chrome.test.assertTrue(win != null);
382 chrome.test.assertTrue(win.innerBounds.width > 0);
383 chrome.test.assertTrue(win.innerBounds.height > 0);
384 chrome.test.assertTrue(win.outerBounds.width > 0);
385 chrome.test.assertTrue(win.outerBounds.height > 0);
386 assertConstraintsUnspecified(win);
387 assertBoundsConsistent(win);
392 function testInnerBoundsOnly() {
399 chrome.app.window.create('test.html', {
400 innerBounds: innerBounds
401 }, callbackPass(function(win) {
402 chrome.test.assertTrue(win != null);
403 assertBoundsEq(innerBounds, win.innerBounds);
404 assertBoundsConsistent(win);
405 assertConstraintsUnspecified(win);
410 function testOuterBoundsOnly() {
417 chrome.app.window.create('test.html', {
418 outerBounds: outerBounds
419 }, callbackPass(function(win) {
420 chrome.test.assertTrue(win != null);
421 assertBoundsEq(outerBounds, win.outerBounds);
422 assertBoundsConsistent(win);
423 assertConstraintsUnspecified(win);
428 function testFrameless() {
435 chrome.app.window.create('test.html', {
436 outerBounds: outerBounds,
438 }, callbackPass(function(win) {
439 chrome.test.assertTrue(win != null);
440 assertBoundsEq(outerBounds, win.outerBounds);
441 assertBoundsEq(outerBounds, win.innerBounds);
442 assertConstraintsUnspecified(win);
447 function testInnerSizeAndOuterPos() {
456 chrome.app.window.create('test.html', {
457 innerBounds: innerBounds,
458 outerBounds: outerBounds
459 }, callbackPass(function(win) {
460 chrome.test.assertTrue(win != null);
461 chrome.test.assertEq(outerBounds.left, win.outerBounds.left);
462 chrome.test.assertEq(outerBounds.top, win.outerBounds.top);
463 chrome.test.assertEq(innerBounds.width, win.innerBounds.width);
464 chrome.test.assertEq(innerBounds.height, win.innerBounds.height);
465 assertBoundsConsistent(win);
466 assertConstraintsUnspecified(win);
471 function testInnerAndOuterBoundsEdgeCase() {
480 chrome.app.window.create('test.html', {
481 innerBounds: innerBounds,
482 outerBounds: outerBounds
483 }, callbackPass(function(win) {
484 chrome.test.assertTrue(win != null);
485 chrome.test.assertEq(innerBounds.left, win.innerBounds.left);
486 chrome.test.assertEq(innerBounds.height, win.innerBounds.height);
487 chrome.test.assertEq(outerBounds.top, win.outerBounds.top);
488 chrome.test.assertEq(outerBounds.width, win.outerBounds.width);
489 assertBoundsConsistent(win);
490 assertConstraintsUnspecified(win);
495 function testPositionOnly() {
500 chrome.app.window.create('test.html', {
501 outerBounds: outerBounds
502 }, callbackPass(function(win) {
503 chrome.test.assertTrue(win != null);
504 chrome.test.assertEq(outerBounds.left, win.outerBounds.left);
505 chrome.test.assertEq(outerBounds.top, win.outerBounds.top);
506 chrome.test.assertTrue(win.innerBounds.width > 0);
507 chrome.test.assertTrue(win.innerBounds.height > 0);
508 chrome.test.assertTrue(win.outerBounds.width > 0);
509 chrome.test.assertTrue(win.outerBounds.height > 0);
510 assertBoundsConsistent(win);
511 assertConstraintsUnspecified(win);
516 function testSizeOnly() {
521 chrome.app.window.create('test.html', {
522 outerBounds: outerBounds
523 }, callbackPass(function(win) {
524 chrome.test.assertTrue(win != null);
525 chrome.test.assertEq(outerBounds.width, win.outerBounds.width);
526 chrome.test.assertEq(outerBounds.height, win.outerBounds.height);
527 assertBoundsConsistent(win);
528 assertConstraintsUnspecified(win);
533 function testConflictingProperties() {
534 testConflictingBoundsProperty("width");
535 testConflictingBoundsProperty("height");
536 testConflictingBoundsProperty("left");
537 testConflictingBoundsProperty("top");
538 testConflictingBoundsProperty("minWidth");
539 testConflictingBoundsProperty("minHeight");
540 testConflictingBoundsProperty("maxWidth");
541 testConflictingBoundsProperty("maxHeight");
546 function testInitialConstraints() {
547 chrome.test.runTests([
548 function testMaxInnerConstraints() {
555 chrome.app.window.create('test.html', {
556 innerBounds: innerBounds
557 }, callbackPass(function(win) {
558 chrome.test.assertTrue(win != null);
559 chrome.test.assertEq(innerBounds.maxWidth, win.innerBounds.width);
560 chrome.test.assertEq(innerBounds.maxHeight, win.innerBounds.height);
561 chrome.test.assertEq(innerBounds.maxWidth, win.innerBounds.maxWidth);
562 chrome.test.assertEq(innerBounds.maxHeight, win.innerBounds.maxHeight);
563 assertBoundsConsistent(win);
568 function testMinInnerConstraints() {
575 chrome.app.window.create('test.html', {
576 innerBounds: innerBounds
577 }, callbackPass(function(win) {
578 chrome.test.assertTrue(win != null);
579 chrome.test.assertEq(innerBounds.minWidth, win.innerBounds.width);
580 chrome.test.assertEq(innerBounds.minHeight, win.innerBounds.height);
581 chrome.test.assertEq(innerBounds.minWidth, win.innerBounds.minWidth);
582 chrome.test.assertEq(innerBounds.minHeight, win.innerBounds.minHeight);
583 assertBoundsConsistent(win);
588 function testMaxOuterConstraints() {
595 chrome.app.window.create('test.html', {
596 outerBounds: outerBounds
597 }, callbackPass(function(win) {
598 chrome.test.assertTrue(win != null);
599 chrome.test.assertEq(outerBounds.maxWidth, win.outerBounds.width);
600 chrome.test.assertEq(outerBounds.maxHeight, win.outerBounds.height);
601 chrome.test.assertEq(outerBounds.maxWidth, win.outerBounds.maxWidth);
602 chrome.test.assertEq(outerBounds.maxHeight, win.outerBounds.maxHeight);
603 assertBoundsConsistent(win);
608 function testMinOuterConstraints() {
615 chrome.app.window.create('test.html', {
616 outerBounds: outerBounds
617 }, callbackPass(function(win) {
618 chrome.test.assertTrue(win != null);
619 chrome.test.assertEq(outerBounds.minWidth, win.outerBounds.width);
620 chrome.test.assertEq(outerBounds.minHeight, win.outerBounds.height);
621 chrome.test.assertEq(outerBounds.minWidth, win.outerBounds.minWidth);
622 chrome.test.assertEq(outerBounds.minHeight, win.outerBounds.minHeight);
623 assertBoundsConsistent(win);
628 function testMixedConstraints() {
637 chrome.app.window.create('test.html', {
638 innerBounds: innerBounds,
639 outerBounds: outerBounds
640 }, callbackPass(function(win) {
641 chrome.test.assertTrue(win != null);
642 chrome.test.assertEq(outerBounds.minWidth, win.outerBounds.width);
643 chrome.test.assertEq(innerBounds.minHeight, win.innerBounds.height);
644 chrome.test.assertEq(outerBounds.minWidth, win.outerBounds.minWidth);
645 chrome.test.assertEq(innerBounds.minHeight, win.innerBounds.minHeight);
646 assertBoundsConsistent(win);
651 function testBadConstraints() {
660 chrome.app.window.create('test.html', {
661 outerBounds: outerBounds
662 }, callbackPass(function(win) {
663 chrome.test.assertTrue(win != null);
664 chrome.test.assertEq(outerBounds.minWidth, win.outerBounds.width);
665 chrome.test.assertEq(outerBounds.minHeight, win.outerBounds.height);
666 chrome.test.assertEq(outerBounds.minWidth, win.outerBounds.minWidth);
667 chrome.test.assertEq(outerBounds.minHeight, win.outerBounds.minHeight);
668 chrome.test.assertEq(outerBounds.minWidth, win.outerBounds.maxWidth);
669 chrome.test.assertEq(outerBounds.minHeight, win.outerBounds.maxHeight);
670 assertBoundsConsistent(win);
675 function testFrameless() {
682 chrome.app.window.create('test.html', {
683 outerBounds: outerBounds,
685 }, callbackPass(function(win) {
686 chrome.test.assertTrue(win != null);
687 assertConstraintsEq(outerBounds, win.outerBounds);
688 assertConstraintsEq(outerBounds, win.innerBounds);
695 function testSetBounds() {
696 chrome.test.runTests([
697 function testLeft() {
698 var init = { left: 150, top: 100, width: 300, height: 200 };
699 var change = { left: 189 };
700 var expected = { left: 189, top: 100, width: 300, height: 200 };
701 runSetBoundsTest('innerBounds', init, change, expected);
702 runSetBoundsTest('outerBounds', init, change, expected);
705 function testLeftNull() {
706 var init = { left: 150, top: 100, width: 300, height: 200 };
707 var change = { left: null };
708 runSetBoundsTest('innerBounds', init, change, init);
709 runSetBoundsTest('outerBounds', init, change, init);
713 var init = { left: 150, top: 100, width: 300, height: 200 };
714 var change = { top: 167 };
715 var expected = { left: 150, top: 167, width: 300, height: 200 };
716 runSetBoundsTest('innerBounds', init, change, expected);
717 runSetBoundsTest('outerBounds', init, change, expected);
720 function testTopNull() {
721 var init = { left: 150, top: 100, width: 300, height: 200 };
722 var change = { top: null };
723 runSetBoundsTest('innerBounds', init, change, init);
724 runSetBoundsTest('outerBounds', init, change, init);
727 function testWidth() {
728 var init = { left: 150, top: 100, width: 300, height: 200 };
729 var change = { width: 245 };
730 var expected = { left: 150, top: 100, width: 245, height: 200 };
731 runSetBoundsTest('innerBounds', init, change, expected);
732 runSetBoundsTest('outerBounds', init, change, expected);
735 function testWidthNull() {
736 var init = { left: 150, top: 100, width: 300, height: 200 };
737 var change = { width: null };
738 runSetBoundsTest('innerBounds', init, change, init);
739 runSetBoundsTest('outerBounds', init, change, init);
742 function testHeight() {
743 var init = { left: 150, top: 100, width: 300, height: 200 };
744 var change = { height: 196 };
745 var expected = { left: 150, top: 100, width: 300, height: 196 };
746 runSetBoundsTest('innerBounds', init, change, expected);
747 runSetBoundsTest('outerBounds', init, change, expected);
750 function testHeightNull() {
751 var init = { left: 150, top: 100, width: 300, height: 200 };
752 var change = { height: null };
753 runSetBoundsTest('innerBounds', init, change, init);
754 runSetBoundsTest('outerBounds', init, change, init);
757 function testPosition() {
758 var init = { left: 150, top: 100, width: 300, height: 200 };
759 var change = { left: 162, top: 112 };
760 var expected = { left: 162, top: 112, width: 300, height: 200 };
761 runSetBoundsTest('innerBounds', init, change, expected);
762 runSetBoundsTest('outerBounds', init, change, expected);
765 function testPositionNull() {
766 var init = { left: 150, top: 100, width: 300, height: 200 };
767 var change = { left: null, top: null };
768 runSetBoundsTest('innerBounds', init, change, init);
769 runSetBoundsTest('outerBounds', init, change, init);
772 function testSize() {
773 var init = { left: 150, top: 100, width: 300, height: 200 };
774 var change = { width: 306, height: 216 };
775 var expected = { left: 150, top: 100, width: 306, height: 216 };
776 runSetBoundsTest('innerBounds', init, change, expected);
777 runSetBoundsTest('outerBounds', init, change, expected);
780 function testSizeNull() {
781 var init = { left: 150, top: 100, width: 300, height: 200 };
782 var change = { width: null, height: null };
783 runSetBoundsTest('innerBounds', init, change, init);
784 runSetBoundsTest('outerBounds', init, change, init);
787 function testMinSize() {
788 var init = { left: 150, top: 100, width: 300, height: 200,
789 minWidth: 235, minHeight: 170 };
790 var change = { width: 50, height: 60 };
791 var expected = { left: 150, top: 100, width: 235, height: 170 };
792 runSetBoundsTest('innerBounds', init, change, expected, true);
793 runSetBoundsTest('outerBounds', init, change, expected, true);
796 function testMaxSize() {
797 var init = { left: 150, top: 100, width: 300, height: 200,
798 maxWidth: 330, maxHeight: 230 };
799 var change = { width: 400, height: 300 };
800 var expected = { left: 150, top: 100, width: 330, height: 230 };
801 runSetBoundsTest('innerBounds', init, change, expected, true);
802 runSetBoundsTest('outerBounds', init, change, expected, true);
805 function testMinAndMaxSize() {
806 var init = { left: 150, top: 100, width: 300, height: 200,
807 minWidth: 120, minHeight: 170,
808 maxWidth: 330, maxHeight: 230 };
809 var change = { width: 225, height: 195 };
810 var expected = { left: 150, top: 100, width: 225, height: 195 };
811 runSetBoundsTest('innerBounds', init, change, expected, true);
812 runSetBoundsTest('outerBounds', init, change, expected, true);
817 function testSetSizeConstraints() {
818 chrome.test.runTests([
819 function testMinWidth() {
820 var init = { minWidth: 300, minHeight: 200,
821 maxWidth: 350, maxHeight: 250 };
822 var change = { minWidth: 111 };
823 var expected = { minWidth: 111, minHeight: 200,
824 maxWidth: 350, maxHeight: 250 };
825 runSetConstraintsTest('innerBounds', init, change, expected);
826 runSetConstraintsTest('outerBounds', init, change, expected);
829 function testClearMinWidth() {
830 var init = { minWidth: 300, minHeight: 200,
831 maxWidth: 350, maxHeight: 250 };
832 var change = { minWidth: null };
833 var expected = { minWidth: null, minHeight: 200,
834 maxWidth: 350, maxHeight: 250 };
835 runSetConstraintsTest('innerBounds', init, change, expected);
836 runSetConstraintsTest('outerBounds', init, change, expected);
839 function testMaxWidth() {
840 var init = { minWidth: 300, minHeight: 200,
841 maxWidth: 350, maxHeight: 250 };
842 var change = { maxWidth: 347 };
843 var expected = { minWidth: 300, minHeight: 200,
844 maxWidth: 347, maxHeight: 250 };
845 runSetConstraintsTest('innerBounds', init, change, expected);
846 runSetConstraintsTest('outerBounds', init, change, expected);
849 function testClearMaxWidth() {
850 var init = { minWidth: 300, minHeight: 200,
851 maxWidth: 350, maxHeight: 250 };
852 var change = { maxWidth: null };
853 var expected = { minWidth: 300, minHeight: 200,
854 maxWidth: null, maxHeight: 250 };
855 runSetConstraintsTest('innerBounds', init, change, expected);
856 runSetConstraintsTest('outerBounds', init, change, expected);
859 function testMinHeight() {
860 var init = { minWidth: 300, minHeight: 200,
861 maxWidth: 350, maxHeight: 250 };
862 var change = { minHeight: 198 };
863 var expected = { minWidth: 300, minHeight: 198,
864 maxWidth: 350, maxHeight: 250 };
865 runSetConstraintsTest('innerBounds', init, change, expected);
866 runSetConstraintsTest('outerBounds', init, change, expected);
869 function testClearMinHeight() {
870 var init = { minWidth: 300, minHeight: 200,
871 maxWidth: 350, maxHeight: 250 };
872 var change = { minHeight: null };
873 var expected = { minWidth: 300, minHeight: null,
874 maxWidth: 350, maxHeight: 250 };
875 runSetConstraintsTest('innerBounds', init, change, expected);
876 runSetConstraintsTest('outerBounds', init, change, expected);
879 function testMaxHeight() {
880 var init = { minWidth: 300, minHeight: 200,
881 maxWidth: 350, maxHeight: 250 };
882 var change = { maxHeight: 278 };
883 var expected = { minWidth: 300, minHeight: 200,
884 maxWidth: 350, maxHeight: 278 };
885 runSetConstraintsTest('innerBounds', init, change, expected);
886 runSetConstraintsTest('outerBounds', init, change, expected);
889 function testClearMaxHeight() {
890 var init = { minWidth: 300, minHeight: 200,
891 maxWidth: 350, maxHeight: 250 };
892 var change = { maxHeight: null };
893 var expected = { minWidth: 300, minHeight: 200,
894 maxWidth: 350, maxHeight: null };
895 runSetConstraintsTest('innerBounds', init, change, expected);
896 runSetConstraintsTest('outerBounds', init, change, expected);
899 function testSetMinSize() {
900 // This test expects the bounds to be changed.
901 var init = { width: 225, height: 125,
902 minWidth: null, minHeight: null,
903 maxWidth: null, maxHeight: null };
904 var change = { minWidth: 235, minHeight: 135 };
905 var expected = { width: 235, height: 135,
906 minWidth: 235, minHeight: 135,
907 maxWidth: null, maxHeight: null };
908 runSetConstraintsTest('innerBounds', init, change, expected, expected);
909 runSetConstraintsTest('outerBounds', init, change, expected, expected);
912 function testSetMaxSize() {
913 // This test expects the bounds to be changed.
914 var init = { width: 225, height: 125,
915 minWidth: null, minHeight: null,
916 maxWidth: null, maxHeight: null };
917 var change = { maxWidth: 198, maxHeight: 107 };
918 var expected = { width: 198, height: 107,
919 minWidth: null, minHeight: null,
920 maxWidth: 198, maxHeight: 107 };
921 runSetConstraintsTest('innerBounds', init, change, expected, expected);
922 runSetConstraintsTest('outerBounds', init, change, expected, expected);
925 function testChangeMinAndMaxSize() {
926 var init = { width: 325, height: 225,
927 minWidth: 300, minHeight: 200,
928 maxWidth: 350, maxHeight: 250 };
929 var change = { minWidth: 287, minHeight: 198,
930 maxWidth: 334, maxHeight: 278 };
931 runSetConstraintsTest('innerBounds', init, change, change, init);
932 runSetConstraintsTest('outerBounds', init, change, change, init);
935 function testClearMinAndMaxSize() {
936 var init = { width: 325, height: 225,
937 minWidth: 300, minHeight: 200,
938 maxWidth: 350, maxHeight: 250 };
939 var change = { minWidth: null, minHeight: null,
940 maxWidth: null, maxHeight: null };
941 runSetConstraintsTest('innerBounds', init, change, change, init);
942 runSetConstraintsTest('outerBounds', init, change, change, init);
945 function testClearConstraints() {
946 // This checks that bounds are not clipped once constraints are removed.
947 var createOptions = {
949 width: 325, height: 225,
950 minWidth: 300, minHeight: 200,
951 maxWidth: 350, maxHeight: 250
954 chrome.app.window.create('test.html', createOptions, callbackPass(
956 win.innerBounds.setMinimumSize(null, null);
957 win.innerBounds.setMaximumSize(null, null);
959 // Set the size smaller than the initial min.
960 win.innerBounds.setSize(234, 198);
962 // Dummy call to wait for bounds to be changed in the browser.
963 chrome.test.waitForRoundTrip('msg', callbackPass(function(msg) {
964 chrome.test.assertEq(234, win.innerBounds.width);
965 chrome.test.assertEq(198, win.innerBounds.height);
967 // Set the size larger than the initial max.
968 win.innerBounds.setSize(361, 278);
970 chrome.test.waitForRoundTrip('msg', callbackPass(function(msg) {
971 chrome.test.assertEq(361, win.innerBounds.width);
972 chrome.test.assertEq(278, win.innerBounds.height);
979 function testMinWidthLargerThanMaxWidth() {
980 var init = { width: 102, height: 103,
981 minWidth: 100, minHeight: 101,
982 maxWidth: 104, maxHeight: 105 };
983 var change = { minWidth: 200 };
984 var expected = { minWidth: 200, minHeight: 101,
985 maxWidth: 200, maxHeight: 105 };
986 runSetConstraintsTest('innerBounds', init, change, expected);
989 function testMinHeightLargerThanMaxHeight() {
990 var init = { width: 102, height: 103,
991 minWidth: 100, minHeight: 101,
992 maxWidth: 104, maxHeight: 105 };
993 var change = { minHeight: 200 };
994 var expected = { minWidth: 100, minHeight: 200,
995 maxWidth: 104, maxHeight: 200 };
996 runSetConstraintsTest('innerBounds', init, change, expected);
999 function testMaxWidthSmallerThanMinWidth() {
1000 var init = { width: 102, height: 103,
1001 minWidth: 100, minHeight: 101,
1002 maxWidth: 104, maxHeight: 105 };
1003 var change = { maxWidth: 50 };
1004 var expected = { minWidth: 100, minHeight: 101,
1005 maxWidth: 100, maxHeight: 105 };
1006 runSetConstraintsTest('innerBounds', init, change, expected);
1009 function testMaxHeightSmallerThanMinHeight() {
1010 var init = { width: 102, height: 103,
1011 minWidth: 100, minHeight: 101,
1012 maxWidth: 104, maxHeight: 105 };
1013 var change = { maxHeight: 50 };
1014 var expected = { minWidth: 100, minHeight: 101,
1015 maxWidth: 104, maxHeight: 101 };
1016 runSetConstraintsTest('innerBounds', init, change, expected);
1021 function testSingleton() {
1022 chrome.test.runTests([
1023 function noParameterWithId() {
1024 chrome.app.window.create(
1025 'test.html', { id: 'singleton-id' },
1026 callbackPass(function(win) {
1027 var w = win.contentWindow;
1029 chrome.app.window.create(
1030 'test.html', { id: 'singleton-id' },
1031 callbackPass(function(win) {
1032 var w2 = win.contentWindow;
1033 chrome.test.assertTrue(w === w2);
1045 function testCloseEvent() {
1046 chrome.test.runTests([
1048 chrome.app.window.create('test.html', callbackPass(function(win) {
1049 win.onClosed.addListener(callbackPass(function() {
1050 // Mission accomplished.
1052 win.contentWindow.close();
1058 function testMaximize() {
1059 chrome.test.runTests([
1061 chrome.app.window.create('test.html',
1062 { innerBounds: {width: 200, height: 200} },
1063 callbackPass(function(win) {
1064 // TODO(mlamouri): we should be able to use onMaximized here but to
1065 // make that happen we need to make sure the event is not fired when
1066 // .maximize() is called but when the maximizing is finished.
1067 // See crbug.com/316091
1068 function isWindowMaximized() {
1069 return win.contentWindow.outerHeight == screen.availHeight &&
1070 win.contentWindow.outerWidth == screen.availWidth;
1073 eventLoopCheck(isWindowMaximized, function() {
1082 function nonResizableWindow() {
1083 chrome.app.window.create('test.html',
1084 { innerBounds: {width: 200, height: 200},
1086 callbackPass(function(win) {
1087 // TODO(mlamouri): we should be able to use onMaximized here but to
1088 // make that happen we need to make sure the event is not fired when
1089 // .maximize() is called but when the maximizing is finished.
1090 // See crbug.com/316091
1091 function isWindowMaximized() {
1092 return win.contentWindow.outerHeight == screen.availHeight &&
1093 win.contentWindow.outerWidth == screen.availWidth;
1096 eventLoopCheck(isWindowMaximized, function() {
1107 function testMinimize() {
1108 chrome.test.runTests([
1110 chrome.app.window.create('test.html',
1111 { innerBounds: {width: 200, height: 200} },
1112 callbackPass(function(win) {
1113 function isWindowMinimized() {
1114 return win.isMinimized();
1118 eventLoopCheck(isWindowMinimized, function() {
1125 function checkSizeAfterRestore() {
1126 var bounds = { width: 200, height: 200,
1127 minWidth: 200, minHeight: 200,
1128 maxWidth: 200, maxHeight: 200 };
1129 chrome.app.window.create('test.html', { innerBounds: bounds },
1130 callbackPass(function(win) {
1131 function isWindowMinimized() {
1132 return win.isMinimized();
1135 function sizeIsSame() {
1136 return bounds.width == win.innerBounds.width &&
1137 bounds.height == win.innerBounds.height;
1141 eventLoopCheck(isWindowMinimized, function() {
1143 eventLoopCheck(sizeIsSame, function() {
1153 function testRestore() {
1154 chrome.test.runTests([
1156 chrome.app.window.create('test.html',
1157 { innerBounds: {width: 200, height: 200} },
1158 callbackPass(function(win) {
1159 var oldWidth = win.contentWindow.innerWidth;
1160 var oldHeight = win.contentWindow.innerHeight;
1162 // TODO(mlamouri): we should be able to use onMaximized here but to
1163 // make that happen we need to make sure the event is not fired when
1164 // .maximize() is called but when the maximizing is finished.
1165 // See crbug.com/316091
1166 function isWindowMaximized() {
1167 return win.contentWindow.outerHeight == screen.availHeight &&
1168 win.contentWindow.outerWidth == screen.availWidth;
1170 function isWindowRestored() {
1171 return win.contentWindow.innerHeight == oldHeight &&
1172 win.contentWindow.innerWidth == oldWidth;
1175 eventLoopCheck(isWindowMaximized, function() {
1176 eventLoopCheck(isWindowRestored, function() {
1190 function testRestoreAfterClose() {
1191 chrome.test.runTests([
1192 function restoredBoundsLowerThanNewMinSize() {
1193 chrome.app.window.create('test.html', {
1195 width: 100, height: 150,
1196 minWidth: 200, minHeight: 250,
1197 maxWidth: 200, maxHeight: 250
1200 }, callbackPass(function(win) {
1201 var w = win.contentWindow;
1202 assertFuzzyEq(200, w.innerWidth, defaultFuzzFactor);
1203 assertFuzzyEq(250, w.innerHeight, defaultFuzzFactor);
1205 win.onClosed.addListener(callbackPass(function() {
1206 chrome.app.window.create('test.html', {
1208 width: 500, height: 550,
1209 minWidth: 400, minHeight: 450,
1210 maxWidth: 600, maxHeight: 650
1213 }, callbackPass(function(win) {
1214 var w = win.contentWindow;
1215 assertFuzzyEq(400, w.innerWidth, defaultFuzzFactor);
1216 assertFuzzyEq(450, w.innerHeight, defaultFuzzFactor);
1227 function testRestoreAfterGeometryCacheChange() {
1228 chrome.test.runTests([
1229 function restorePositionAndSize() {
1230 chrome.app.window.create('test.html', {
1231 outerBounds: { left: 200, top: 200 },
1232 innerBounds: { width: 200, height: 200 },
1234 }, callbackPass(function(win) { waitForLoad(win, function(win) {
1235 var w = win.contentWindow;
1236 chrome.test.assertEq(200, w.screenX);
1237 chrome.test.assertEq(200, w.screenY);
1238 chrome.test.assertEq(200, w.innerHeight);
1239 chrome.test.assertEq(200, w.innerWidth);
1241 w.resizeTo(300, 300);
1244 chrome.app.window.create('test.html', {
1245 outerBounds: { left: 200, top: 200, width: 200, height: 200 },
1246 id: 'test-rb', frame: 'none'
1247 }, callbackPass(function(win2) { waitForLoad(win2, function(win2) {
1248 var w2 = win2.contentWindow;
1249 chrome.test.assertEq(200, w2.screenX);
1250 chrome.test.assertEq(200, w2.screenY);
1251 chrome.test.assertEq(200, w2.innerWidth);
1252 chrome.test.assertEq(200, w2.innerHeight);
1254 w2.resizeTo(100, 100);
1255 w2.moveTo(300, 300);
1257 chrome.test.sendMessage('ListenGeometryChange', function(reply) {
1258 win.onClosed.addListener(callbackPass(function() {
1259 chrome.app.window.create('test.html', {
1261 }, callbackPass(function(win) { waitForLoad(win, function(win) {
1262 var w = win.contentWindow;
1263 chrome.test.assertEq(100, w.screenX);
1264 chrome.test.assertEq(100, w.screenY);
1265 chrome.test.assertEq(300, w.outerWidth);
1266 chrome.test.assertEq(300, w.outerHeight);
1270 win2.onClosed.addListener(callbackPass(function() {
1271 chrome.app.window.create('test.html', {
1272 id: 'test-rb', frame: 'none'
1273 },callbackPass(function(win2) { waitForLoad(win2, function(win2) {
1274 var w = win2.contentWindow;
1275 chrome.test.assertEq(300, w.screenX);
1276 chrome.test.assertEq(300, w.screenY);
1277 chrome.test.assertEq(100, w.outerWidth);
1278 chrome.test.assertEq(100, w.outerHeight);
1291 function testFrameColors() {
1292 chrome.test.runTests([
1293 function testWithNoColor() {
1294 chrome.app.window.create('test.html', callbackPass(function(win) {
1295 chrome.test.assertEq(false, win.hasFrameColor);
1300 function testWithFrameNone() {
1301 chrome.app.window.create('test.html', {
1304 callbackPass(function(win) {
1305 chrome.test.assertEq(false, win.hasFrameColor);
1310 function testWithBlack() {
1311 chrome.app.window.create('test.html', {
1317 callbackPass(function(win) {
1318 chrome.test.assertEq(true, win.hasFrameColor);
1319 chrome.test.assertEq(0x000000, win.activeFrameColor);
1320 chrome.test.assertEq(0x000000, win.inactiveFrameColor);
1325 function testWithWhite() {
1326 chrome.app.window.create('test.html', {
1331 callbackPass(function(win) {
1332 chrome.test.assertEq(true, win.hasFrameColor);
1333 chrome.test.assertEq(0xFFFFFF, win.activeFrameColor);
1334 chrome.test.assertEq(0xFFFFFF, win.inactiveFrameColor);
1339 function testWithActiveInactive() {
1340 chrome.app.window.create('test.html', {
1344 inactiveColor: '#FFFFFF'
1347 callbackPass(function(win) {
1348 chrome.test.assertEq(true, win.hasFrameColor);
1349 chrome.test.assertEq(0x000000, win.activeFrameColor);
1350 chrome.test.assertEq(0xFFFFFF, win.inactiveFrameColor);
1355 function testWithWhiteShorthand() {
1356 chrome.app.window.create('test.html', {
1361 callbackPass(function(win) {
1362 chrome.test.assertEq(true, win.hasFrameColor);
1363 chrome.test.assertEq(0xFFFFFF, win.activeFrameColor);
1364 chrome.test.assertEq(0xFFFFFF, win.inactiveFrameColor);
1369 function testWithFrameNoneAndColor() {
1370 chrome.app.window.create('test.html', {
1376 callbackFail('Windows with no frame cannot have a color.'));
1379 function testWithInactiveColorAndNoColor() {
1380 chrome.app.window.create('test.html', {
1382 inactiveColor: '#FFF'
1385 callbackFail('frame.inactiveColor must be used with frame.color.'));
1388 function testWithInvalidColor() {
1389 chrome.app.window.create('test.html', {
1391 color: 'DontWorryBeHappy'
1394 callbackFail('The color specification could not be parsed.'));
1399 function testVisibleOnAllWorkspaces() {
1400 chrome.test.runTests([
1401 function setAndUnsetVisibleOnAllWorkspaces() {
1402 chrome.app.window.create('test.html', {
1403 visibleOnAllWorkspaces: true
1404 }, callbackPass(function(win) {
1405 win.setVisibleOnAllWorkspaces(false);
1406 win.setVisibleOnAllWorkspaces(true);
1407 chrome.test.sendMessage(
1408 'WaitForRoundTrip', callbackPass(function(reply) {}));
1414 chrome.app.runtime.onLaunched.addListener(function() {
1415 chrome.test.sendMessage('Launched', function(reply) {