Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / remoting / webapp / browser_test / bump_scroll_browser_test.js
blobf8dc7ccaa351cfd439284108ae0489e427323e9c
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.
5 /**
6 * @fileoverview
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.
13 'use strict';
15 /** @constructor */
16 browserTest.FakeDesktopViewport = function() {
17 /** @private */
18 this.pluginPosition_ = {
19 top: 0,
20 left: 0
22 /** @private */
23 this.bumpScroller_ = new base.EventSourceImpl();
24 this.bumpScroller_.defineEvents(Object.keys(remoting.BumpScroller.Events));
27 /**
28 * @param {number} top
29 * @param {number} left
30 * @return {void} nothing.
32 browserTest.FakeDesktopViewport.prototype.setPluginPositionForTesting =
33 function(top, left) {
34 this.pluginPosition_ = {
35 top: top,
36 left: left
40 /**
41 * @return {{top: number, left:number}} The top-left corner of the plugin.
43 browserTest.FakeDesktopViewport.prototype.getPluginPositionForTesting =
44 function() {
45 return this.pluginPosition_;
48 /** @return {base.EventSource} */
49 browserTest.FakeDesktopViewport.prototype.getBumpScrollerForTesting =
50 function() {
51 return this.bumpScroller_;
54 browserTest.FakeDesktopViewport.prototype.raiseEvent =
55 function() {
56 return this.bumpScroller_.raiseEvent.apply(this.bumpScroller_, arguments);
60 /** @constructor */
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.
65 /** @type {number} */
66 this.kHostDesktopSizeDelta = 10;
69 /**
70 * @param {{pin:string}} data
72 browserTest.Bump_Scroll.prototype.run = function(data) {
73 browserTest.expect(typeof data.pin == 'string');
75 if (!base.isAppsV2()) {
76 browserTest.fail(
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();
83 }).then(function() {
84 return browserTest.enterPIN(data.pin);
85 }).then(
86 this.noScrollWindowed.bind(this)
87 ).then(
88 this.activateFullscreen.bind(this)
89 ).then(
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.
93 ).then(
94 this.scrollDirection.bind(this, 1.0, 0.5) // Right edge
95 ).then(
96 this.scrollDirection.bind(this, 0.5, 1.0) // Bottom edge
97 ).then(
98 this.scrollDirection.bind(this, 0.0, 0.5) // Left edge
99 ).then(
100 this.scrollDirection.bind(this, 0.5, 0.0) // Top edge
101 ).then(
102 function(value) {
103 browserTest.disconnect();
104 return browserTest.pass(value);
106 function(error) {
107 browserTest.disconnect();
108 return browserTest.fail(error);
114 * @return {Promise}
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();
126 * @return {Promise}
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
140 * @return {Promise}
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 :
151 undefined;
152 /** @type {number} */
153 var expectedLeft = widthFraction === 0.0 ? 0 :
154 widthFraction === 1.0 ? -this.kHostDesktopSizeDelta :
155 undefined;
156 var result = this.verifyScroll(expectedTop, expectedLeft);
157 this.moveMouseTo(widthFraction * screen.width,
158 heightFraction * screen.height);
159 return result;
163 * @return {Promise}
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');
180 * @param {number} x
181 * @param {number} y
183 browserTest.Bump_Scroll.prototype.moveMouseTo = function(x, y) {
184 var e = {
185 bubbles: true,
186 cancelable: false,
187 view: window,
188 detail: 0,
189 screenX: x,
190 screenY: y,
191 clientX: x,
192 clientY: y,
193 ctrlKey: false,
194 altKey: false,
195 shiftKey: false,
196 metaKey: false,
197 button: 0,
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.
211 * @return {Promise}
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;
217 var that = this;
219 // No events raised (e.g. windowed mode).
220 var result = this.verifyNoScroll(fakeViewport)
222 .then(function() {
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, {});
229 return result;
231 }).then(function() {
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, {});
239 return result;
241 }).then(function() {
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);
248 return result;
250 }).then(function() {
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, {});
257 return result;
260 return result;
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.
272 * @return {Promise}
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 + ')'));
300 } else {
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() {
309 return stopped;
310 }, function() {
311 // If no started event is raised, the test might still pass if it asserted
312 // no scrolling.
313 if (expectedTop === undefined && expectedLeft === undefined) {
314 return Promise.resolve();
315 } else {
316 return Promise.reject(
317 new Error('Scroll expected but no start event fired.'));
319 }).then(function() {
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();
336 if (!bumpScroller) {
337 Promise.resolve(true);
339 return this.verifyScroll(undefined, undefined, desktopViewport);