1 // Copyright 2014 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.
7 * @suppress {checkTypes}
8 * Browser test for the scenario below:
9 * 1. Enter full-screen mode
10 * 2. Move the mouse to each edge; verify that the desktop bump-scrolls.
16 browserTest
.FakeDesktopViewport = function() {
18 this.pluginPosition_
= {
23 this.bumpScroller_
= new base
.EventSourceImpl();
24 this.bumpScroller_
.defineEvents(Object
.keys(remoting
.BumpScroller
.Events
));
29 * @param {number} left
30 * @return {void} nothing.
32 browserTest
.FakeDesktopViewport
.prototype.setPluginPositionForTesting
=
34 this.pluginPosition_
= {
41 * @return {{top: number, left:number}} The top-left corner of the plugin.
43 browserTest
.FakeDesktopViewport
.prototype.getPluginPositionForTesting
=
45 return this.pluginPosition_
;
48 /** @return {base.EventSource} */
49 browserTest
.FakeDesktopViewport
.prototype.getBumpScrollerForTesting
=
51 return this.bumpScroller_
;
54 browserTest
.FakeDesktopViewport
.prototype.raiseEvent
=
56 return this.bumpScroller_
.raiseEvent
.apply(this.bumpScroller_
, arguments
);
61 browserTest
.Bump_Scroll = function() {
62 // To avoid dependencies on the actual host desktop size, we simulate a
63 // desktop larger or smaller than the client window. The exact value is
64 // arbitrary, but must be positive.
66 this.kHostDesktopSizeDelta
= 10;
70 * @param {{pin:string}} data
72 browserTest
.Bump_Scroll
.prototype.run = function(data
) {
73 browserTest
.expect(typeof data
.pin
== 'string');
75 if (!base
.isAppsV2()) {
77 'Bump-scroll requires full-screen, which can only be activated ' +
78 'programmatically in apps v2.')
81 this.testVerifyScroll().then(function() {
82 return browserTest
.connectMe2Me();
84 return browserTest
.enterPIN(data
.pin
);
86 this.noScrollWindowed
.bind(this)
88 this.activateFullscreen
.bind(this)
90 this.noScrollSmaller
.bind(this)
91 // The order of these operations is important. Because the plugin starts
92 // scrolled to the top-left, it needs to be scrolled right and down first.
94 this.scrollDirection
.bind(this, 1.0, 0.5) // Right edge
96 this.scrollDirection
.bind(this, 0.5, 1.0) // Bottom edge
98 this.scrollDirection
.bind(this, 0.0, 0.5) // Left edge
100 this.scrollDirection
.bind(this, 0.5, 0.0) // Top edge
103 browserTest
.disconnect();
104 return browserTest
.pass(value
);
107 browserTest
.disconnect();
108 return browserTest
.fail(error
);
116 browserTest
.Bump_Scroll
.prototype.noScrollWindowed = function() {
117 var viewport
= remoting
.desktopConnectedView
.getViewportForTesting();
118 viewport
.setPluginSizeForBumpScrollTesting(
119 window
.innerWidth
+ this.kHostDesktopSizeDelta
,
120 window
.innerHeight
+ this.kHostDesktopSizeDelta
);
121 this.moveMouseTo(0, 0);
122 return this.verifyNoScroll();
128 browserTest
.Bump_Scroll
.prototype.noScrollSmaller = function() {
129 var viewport
= remoting
.desktopConnectedView
.getViewportForTesting();
130 viewport
.setPluginSizeForBumpScrollTesting(
131 window
.innerWidth
- this.kHostDesktopSizeDelta
,
132 window
.innerHeight
- this.kHostDesktopSizeDelta
);
133 this.moveMouseTo(0, 0);
134 return this.verifyNoScroll();
138 * @param {number} widthFraction
139 * @param {number} heightFraction
142 browserTest
.Bump_Scroll
.prototype.scrollDirection
=
143 function(widthFraction
, heightFraction
) {
144 var viewport
= remoting
.desktopConnectedView
.getViewportForTesting();
145 viewport
.setPluginSizeForBumpScrollTesting(
146 screen
.width
+ this.kHostDesktopSizeDelta
,
147 screen
.height
+ this.kHostDesktopSizeDelta
);
148 /** @type {number} */
149 var expectedTop
= heightFraction
=== 0.0 ? 0 :
150 heightFraction
== 1.0 ? -this.kHostDesktopSizeDelta
:
152 /** @type {number} */
153 var expectedLeft
= widthFraction
=== 0.0 ? 0 :
154 widthFraction
=== 1.0 ? -this.kHostDesktopSizeDelta
:
156 var result
= this.verifyScroll(expectedTop
, expectedLeft
);
157 this.moveMouseTo(widthFraction
* screen
.width
,
158 heightFraction
* screen
.height
);
165 browserTest
.Bump_Scroll
.prototype.activateFullscreen = function() {
166 return new Promise(function(fulfill
, reject
) {
167 remoting
.fullscreen
.activate(true, function() {
168 // The onFullscreen callback is invoked before the window has
169 // resized, so defer fulfilling the promise so that innerWidth
170 // and innerHeight are correct.
171 base
.Promise
.sleep(1000).then(fulfill
);
173 base
.Promise
.sleep(5000).then(function(){
174 reject('Timed out waiting for full-screen');
183 browserTest
.Bump_Scroll
.prototype.moveMouseTo = function(x
, y
) {
198 relatedTarget
: undefined
200 var event
= document
.createEvent('MouseEvents');
201 event
.initMouseEvent('mousemove',
202 e
.bubbles
, e
.cancelable
, e
.view
, e
.detail
,
203 e
.screenX
, e
.screenY
, e
.clientX
, e
.clientY
,
204 e
.ctrlKey
, e
.altKey
, e
.shiftKey
, e
.metaKey
,
205 e
.button
, document
.documentElement
);
206 document
.documentElement
.dispatchEvent(event
);
210 * verifyScroll() is complicated enough to warrant a test.
213 browserTest
.Bump_Scroll
.prototype.testVerifyScroll = function() {
214 var STARTED
= remoting
.BumpScroller
.Events
.bumpScrollStarted
;
215 var STOPPED
= remoting
.BumpScroller
.Events
.bumpScrollStopped
;
216 var fakeViewport
= new browserTest
.FakeDesktopViewport
;
219 // No events raised (e.g. windowed mode).
220 var result
= this.verifyNoScroll(fakeViewport
)
223 // Start and end events raised, but no scrolling (e.g. full-screen mode
224 // with host desktop <= window size).
225 fakeViewport
= new browserTest
.FakeDesktopViewport
;
226 var result
= that
.verifyNoScroll(fakeViewport
);
227 fakeViewport
.raiseEvent(STARTED
, {});
228 fakeViewport
.raiseEvent(STOPPED
, {});
232 // Start and end events raised, with incorrect scrolling.
233 fakeViewport
= new browserTest
.FakeDesktopViewport
;
234 var result
= base
.Promise
.negate(
235 that
.verifyScroll(2, 2, fakeViewport
));
236 fakeViewport
.raiseEvent(STARTED
, {});
237 fakeViewport
.setPluginPositionForTesting(1, 1);
238 fakeViewport
.raiseEvent(STOPPED
, {});
242 // Start event raised, but not end event.
243 fakeViewport
= new browserTest
.FakeDesktopViewport
;
244 var result
= base
.Promise
.negate(
245 that
.verifyScroll(2, 2, fakeViewport
));
246 fakeViewport
.raiseEvent(STARTED
, {});
247 fakeViewport
.setPluginPositionForTesting(2, 2);
251 // Start and end events raised, with correct scrolling.
252 fakeViewport
= new browserTest
.FakeDesktopViewport
;
253 var result
= that
.verifyScroll(2, 2, fakeViewport
);
254 fakeViewport
.raiseEvent(STARTED
, {});
255 fakeViewport
.setPluginPositionForTesting(2, 2);
256 fakeViewport
.raiseEvent(STOPPED
, {});
264 * Verify that a bump scroll operation takes place and that the top-left corner
265 * of the plugin is as expected when it completes.
266 * @param {number|undefined} expectedTop The expected vertical position of the
267 * plugin, or undefined if it is not expected to change.
268 * @param {number|undefined} expectedLeft The expected horizontal position of
269 * the plugin, or undefined if it is not expected to change.
270 * @param {browserTest.FakeDesktopViewport=} opt_desktopViewport
271 * DesktopViewport fake, for testing.
274 browserTest
.Bump_Scroll
.prototype.verifyScroll
=
275 function (expectedTop
, expectedLeft
, opt_desktopViewport
) {
276 var desktopViewport
= opt_desktopViewport
||
277 remoting
.desktopConnectedView
.getViewportForTesting();
278 base
.debug
.assert(desktopViewport
!= null);
279 var STARTED
= remoting
.BumpScroller
.Events
.bumpScrollStarted
;
280 var STOPPED
= remoting
.BumpScroller
.Events
.bumpScrollStopped
;
282 var initialPosition
= desktopViewport
.getPluginPositionForTesting();
283 var initialTop
= initialPosition
.top
;
284 var initialLeft
= initialPosition
.left
;
286 /** @return {Promise} */
287 var verifyPluginPosition = function() {
288 var position
= desktopViewport
.getPluginPositionForTesting();
289 if (expectedLeft
=== undefined) {
290 expectedLeft
= initialLeft
;
292 if (expectedTop
=== undefined) {
293 expectedTop
= initialTop
;
295 if (position
.top
!= expectedTop
|| position
.left
!= expectedLeft
) {
296 return Promise
.reject(
297 new Error('No or incorrect scroll detected: (' +
298 position
.left
+ ',' + position
.top
+ ' instead of ' +
299 expectedLeft
+ ',' + expectedTop
+ ')'));
301 return Promise
.resolve();
305 var bumpScroller
= desktopViewport
.getBumpScrollerForTesting();
306 var started
= browserTest
.expectEvent(bumpScroller
, STARTED
, 1000);
307 var stopped
= browserTest
.expectEvent(bumpScroller
, STOPPED
, 5000);
308 return started
.then(function() {
311 // If no started event is raised, the test might still pass if it asserted
313 if (expectedTop
=== undefined && expectedLeft
=== undefined) {
314 return Promise
.resolve();
316 return Promise
.reject(
317 new Error('Scroll expected but no start event fired.'));
320 return verifyPluginPosition();
325 * @param {browserTest.FakeDesktopViewport=} opt_desktopViewport
326 * DesktopViewport fake, for testing.
328 * @return {Promise<boolean>} A promise that resolves to true if no scrolling
329 * occurs within a timeout.
331 browserTest
.Bump_Scroll
.prototype.verifyNoScroll
=
332 function(opt_desktopViewport
) {
333 var desktopViewport
= opt_desktopViewport
||
334 remoting
.desktopConnectedView
.getViewportForTesting();
335 var bumpScroller
= desktopViewport
.getBumpScrollerForTesting();
337 Promise
.resolve(true);
339 return this.verifyScroll(undefined, undefined, desktopViewport
);