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 /** @suppress {reportUnknownTypes} */
55 browserTest.FakeDesktopViewport.prototype.raiseEvent =
57 return this.bumpScroller_.raiseEvent.apply(this.bumpScroller_, arguments);
60 /** @return {remoting.DesktopViewport} */
61 function getViewportForTesting() {
62 var desktopApp = /** @type {remoting.DesktopRemoting} */ (remoting.app);
63 var view = desktopApp.getConnectedViewForTesting();
65 return view.getViewportForTesting();
71 browserTest.Bump_Scroll = function() {
72 // To avoid dependencies on the actual host desktop size, we simulate a
73 // desktop larger or smaller than the client window. The exact value is
74 // arbitrary, but must be positive.
76 this.kHostDesktopSizeDelta = 10;
80 * @param {{pin:string}} data
82 browserTest.Bump_Scroll.prototype.run = function(data) {
83 browserTest.expect(typeof data.pin == 'string');
85 if (!base.isAppsV2()) {
87 'Bump-scroll requires full-screen, which can only be activated ' +
88 'programmatically in apps v2.');
91 var mockConnection = new remoting.MockConnection();
92 mockConnection.plugin().mock$useDefaultBehavior(
93 remoting.MockClientPlugin.AuthMethod.PIN);
96 mockConnection.restore();
97 browserTest.disconnect();
100 this.testVerifyScroll().then(function() {
101 return browserTest.connectMe2Me();
103 return browserTest.enterPIN(data.pin);
105 this.noScrollWindowed.bind(this)
107 this.activateFullscreen.bind(this)
109 this.noScrollSmaller.bind(this)
110 // The order of these operations is important. Because the plugin starts
111 // scrolled to the top-left, it needs to be scrolled right and down first.
113 this.scrollDirection.bind(this, 1.0, 0.5) // Right edge
115 this.scrollDirection.bind(this, 0.5, 1.0) // Bottom edge
117 this.scrollDirection.bind(this, 0.0, 0.5) // Left edge
119 this.scrollDirection.bind(this, 0.5, 0.0) // Top edge
123 return browserTest.pass();
127 return browserTest.fail(error);
135 browserTest.Bump_Scroll.prototype.noScrollWindowed = function() {
136 var viewport = getViewportForTesting();
137 viewport.setPluginSizeForBumpScrollTesting(
138 window.innerWidth + this.kHostDesktopSizeDelta,
139 window.innerHeight + this.kHostDesktopSizeDelta);
140 this.moveMouseTo(0, 0);
141 return this.verifyNoScroll();
147 browserTest.Bump_Scroll.prototype.noScrollSmaller = function() {
148 var viewport = getViewportForTesting();
149 viewport.setPluginSizeForBumpScrollTesting(
150 window.innerWidth - this.kHostDesktopSizeDelta,
151 window.innerHeight - this.kHostDesktopSizeDelta);
152 this.moveMouseTo(0, 0);
153 return this.verifyNoScroll();
157 * @param {number} widthFraction
158 * @param {number} heightFraction
161 browserTest.Bump_Scroll.prototype.scrollDirection =
162 function(widthFraction, heightFraction) {
163 var viewport = getViewportForTesting();
164 viewport.setPluginSizeForBumpScrollTesting(
165 screen.width + this.kHostDesktopSizeDelta,
166 screen.height + this.kHostDesktopSizeDelta);
167 /** @type {number} */
168 var expectedTop = heightFraction === 0.0 ? 0 :
169 heightFraction == 1.0 ? -this.kHostDesktopSizeDelta :
171 /** @type {number} */
172 var expectedLeft = widthFraction === 0.0 ? 0 :
173 widthFraction === 1.0 ? -this.kHostDesktopSizeDelta :
175 var result = this.verifyScroll(expectedTop, expectedLeft);
176 this.moveMouseTo(widthFraction * screen.width,
177 heightFraction * screen.height);
184 browserTest.Bump_Scroll.prototype.activateFullscreen = function() {
185 return new Promise(function(fulfill, reject) {
186 remoting.fullscreen.activate(true, function() {
187 // The onFullscreen callback is invoked before the window has
188 // resized, so defer fulfilling the promise so that innerWidth
189 // and innerHeight are correct.
190 base.Promise.sleep(1000).then(fulfill);
192 base.Promise.sleep(5000).then(function(){
193 reject('Timed out waiting for full-screen');
202 browserTest.Bump_Scroll.prototype.moveMouseTo = function(x, y) {
217 relatedTarget: undefined
219 var event = document.createEvent('MouseEvents');
220 event.initMouseEvent('mousemove',
221 e.bubbles, e.cancelable, e.view, e.detail,
222 e.screenX, e.screenY, e.clientX, e.clientY,
223 e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
224 e.button, document.documentElement);
225 document.documentElement.dispatchEvent(event);
229 * verifyScroll() is complicated enough to warrant a test.
232 browserTest.Bump_Scroll.prototype.testVerifyScroll = function() {
233 var STARTED = remoting.BumpScroller.Events.bumpScrollStarted;
234 var STOPPED = remoting.BumpScroller.Events.bumpScrollStopped;
235 var fakeViewport = new browserTest.FakeDesktopViewport;
238 // No events raised (e.g. windowed mode).
239 var result = this.verifyNoScroll(fakeViewport)
242 // Start and end events raised, but no scrolling (e.g. full-screen mode
243 // with host desktop <= window size).
244 fakeViewport = new browserTest.FakeDesktopViewport;
245 var result = that.verifyNoScroll(fakeViewport);
246 fakeViewport.raiseEvent(STARTED, {});
247 fakeViewport.raiseEvent(STOPPED, {});
251 // Start and end events raised, with incorrect scrolling.
252 fakeViewport = new browserTest.FakeDesktopViewport;
253 var result = base.Promise.negate(
254 that.verifyScroll(2, 2, fakeViewport));
255 fakeViewport.raiseEvent(STARTED, {});
256 fakeViewport.setPluginPositionForTesting(1, 1);
257 fakeViewport.raiseEvent(STOPPED, {});
261 // Start event raised, but not end event.
262 fakeViewport = new browserTest.FakeDesktopViewport;
263 var result = base.Promise.negate(
264 that.verifyScroll(2, 2, fakeViewport));
265 fakeViewport.raiseEvent(STARTED, {});
266 fakeViewport.setPluginPositionForTesting(2, 2);
270 // Start and end events raised, with correct scrolling.
271 fakeViewport = new browserTest.FakeDesktopViewport;
272 var result = that.verifyScroll(2, 2, fakeViewport);
273 fakeViewport.raiseEvent(STARTED, {});
274 fakeViewport.setPluginPositionForTesting(2, 2);
275 fakeViewport.raiseEvent(STOPPED, {});
283 * Verify that a bump scroll operation takes place and that the top-left corner
284 * of the plugin is as expected when it completes.
285 * @param {number|undefined} expectedTop The expected vertical position of the
286 * plugin, or undefined if it is not expected to change.
287 * @param {number|undefined} expectedLeft The expected horizontal position of
288 * the plugin, or undefined if it is not expected to change.
289 * @param {browserTest.FakeDesktopViewport=} opt_desktopViewport
290 * DesktopViewport fake, for testing.
293 browserTest.Bump_Scroll.prototype.verifyScroll =
294 function (expectedTop, expectedLeft, opt_desktopViewport) {
295 var desktopViewport = opt_desktopViewport || getViewportForTesting();
296 console.assert(desktopViewport != null, '|desktopViewport| is null.');
297 var STARTED = remoting.BumpScroller.Events.bumpScrollStarted;
298 var STOPPED = remoting.BumpScroller.Events.bumpScrollStopped;
300 var initialPosition = desktopViewport.getPluginPositionForTesting();
301 var initialTop = initialPosition.top;
302 var initialLeft = initialPosition.left;
304 /** @return {Promise} */
305 var verifyPluginPosition = function() {
306 var position = desktopViewport.getPluginPositionForTesting();
307 if (expectedLeft === undefined) {
308 expectedLeft = initialLeft;
310 if (expectedTop === undefined) {
311 expectedTop = initialTop;
313 if (position.top != expectedTop || position.left != expectedLeft) {
314 return Promise.reject(
315 new Error('No or incorrect scroll detected: (' +
316 position.left + ',' + position.top + ' instead of ' +
317 expectedLeft + ',' + expectedTop + ')'));
319 return Promise.resolve();
323 var bumpScroller = desktopViewport.getBumpScrollerForTesting();
324 var started = browserTest.expectEvent(bumpScroller, STARTED, 1000);
325 var stopped = browserTest.expectEvent(bumpScroller, STOPPED, 5000);
326 return started.then(function() {
329 // If no started event is raised, the test might still pass if it asserted
331 if (expectedTop === undefined && expectedLeft === undefined) {
332 return Promise.resolve();
334 return Promise.reject(
335 new Error('Scroll expected but no start event fired.'));
338 return verifyPluginPosition();
343 * @param {browserTest.FakeDesktopViewport=} opt_desktopViewport
344 * DesktopViewport fake, for testing.
346 * @return {Promise<boolean>} A promise that resolves to true if no scrolling
347 * occurs within a timeout.
349 browserTest.Bump_Scroll.prototype.verifyNoScroll =
350 function(opt_desktopViewport) {
351 var desktopViewport = opt_desktopViewport || getViewportForTesting();
352 var bumpScroller = desktopViewport.getBumpScrollerForTesting();
354 Promise.resolve(true);
356 return this.verifyScroll(undefined, undefined, desktopViewport);