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);
59 /** @return {remoting.DesktopViewport} */
60 function getViewportForTesting() {
61 var desktopApp = /** @type {remoting.DesktopRemoting} */ (remoting.app);
62 var view = desktopApp.getConnectedViewForTesting();
64 return view.getViewportForTesting();
70 browserTest.Bump_Scroll = function() {
71 // To avoid dependencies on the actual host desktop size, we simulate a
72 // desktop larger or smaller than the client window. The exact value is
73 // arbitrary, but must be positive.
75 this.kHostDesktopSizeDelta = 10;
79 * @param {{pin:string}} data
81 browserTest.Bump_Scroll.prototype.run = function(data) {
82 browserTest.expect(typeof data.pin == 'string');
84 if (!base.isAppsV2()) {
86 'Bump-scroll requires full-screen, which can only be activated ' +
87 'programmatically in apps v2.')
90 this.testVerifyScroll().then(function() {
91 return browserTest.connectMe2Me();
93 return browserTest.enterPIN(data.pin);
95 this.noScrollWindowed.bind(this)
97 this.activateFullscreen.bind(this)
99 this.noScrollSmaller.bind(this)
100 // The order of these operations is important. Because the plugin starts
101 // scrolled to the top-left, it needs to be scrolled right and down first.
103 this.scrollDirection.bind(this, 1.0, 0.5) // Right edge
105 this.scrollDirection.bind(this, 0.5, 1.0) // Bottom edge
107 this.scrollDirection.bind(this, 0.0, 0.5) // Left edge
109 this.scrollDirection.bind(this, 0.5, 0.0) // Top edge
112 browserTest.disconnect();
113 return browserTest.pass(value);
116 browserTest.disconnect();
117 return browserTest.fail(error);
125 browserTest.Bump_Scroll.prototype.noScrollWindowed = function() {
126 var viewport = getViewportForTesting();
127 viewport.setPluginSizeForBumpScrollTesting(
128 window.innerWidth + this.kHostDesktopSizeDelta,
129 window.innerHeight + this.kHostDesktopSizeDelta);
130 this.moveMouseTo(0, 0);
131 return this.verifyNoScroll();
137 browserTest.Bump_Scroll.prototype.noScrollSmaller = function() {
138 var viewport = getViewportForTesting();
139 viewport.setPluginSizeForBumpScrollTesting(
140 window.innerWidth - this.kHostDesktopSizeDelta,
141 window.innerHeight - this.kHostDesktopSizeDelta);
142 this.moveMouseTo(0, 0);
143 return this.verifyNoScroll();
147 * @param {number} widthFraction
148 * @param {number} heightFraction
151 browserTest.Bump_Scroll.prototype.scrollDirection =
152 function(widthFraction, heightFraction) {
153 var viewport = getViewportForTesting();
154 viewport.setPluginSizeForBumpScrollTesting(
155 screen.width + this.kHostDesktopSizeDelta,
156 screen.height + this.kHostDesktopSizeDelta);
157 /** @type {number} */
158 var expectedTop = heightFraction === 0.0 ? 0 :
159 heightFraction == 1.0 ? -this.kHostDesktopSizeDelta :
161 /** @type {number} */
162 var expectedLeft = widthFraction === 0.0 ? 0 :
163 widthFraction === 1.0 ? -this.kHostDesktopSizeDelta :
165 var result = this.verifyScroll(expectedTop, expectedLeft);
166 this.moveMouseTo(widthFraction * screen.width,
167 heightFraction * screen.height);
174 browserTest.Bump_Scroll.prototype.activateFullscreen = function() {
175 return new Promise(function(fulfill, reject) {
176 remoting.fullscreen.activate(true, function() {
177 // The onFullscreen callback is invoked before the window has
178 // resized, so defer fulfilling the promise so that innerWidth
179 // and innerHeight are correct.
180 base.Promise.sleep(1000).then(fulfill);
182 base.Promise.sleep(5000).then(function(){
183 reject('Timed out waiting for full-screen');
192 browserTest.Bump_Scroll.prototype.moveMouseTo = function(x, y) {
207 relatedTarget: undefined
209 var event = document.createEvent('MouseEvents');
210 event.initMouseEvent('mousemove',
211 e.bubbles, e.cancelable, e.view, e.detail,
212 e.screenX, e.screenY, e.clientX, e.clientY,
213 e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
214 e.button, document.documentElement);
215 document.documentElement.dispatchEvent(event);
219 * verifyScroll() is complicated enough to warrant a test.
222 browserTest.Bump_Scroll.prototype.testVerifyScroll = function() {
223 var STARTED = remoting.BumpScroller.Events.bumpScrollStarted;
224 var STOPPED = remoting.BumpScroller.Events.bumpScrollStopped;
225 var fakeViewport = new browserTest.FakeDesktopViewport;
228 // No events raised (e.g. windowed mode).
229 var result = this.verifyNoScroll(fakeViewport)
232 // Start and end events raised, but no scrolling (e.g. full-screen mode
233 // with host desktop <= window size).
234 fakeViewport = new browserTest.FakeDesktopViewport;
235 var result = that.verifyNoScroll(fakeViewport);
236 fakeViewport.raiseEvent(STARTED, {});
237 fakeViewport.raiseEvent(STOPPED, {});
241 // Start and end events raised, with incorrect scrolling.
242 fakeViewport = new browserTest.FakeDesktopViewport;
243 var result = base.Promise.negate(
244 that.verifyScroll(2, 2, fakeViewport));
245 fakeViewport.raiseEvent(STARTED, {});
246 fakeViewport.setPluginPositionForTesting(1, 1);
247 fakeViewport.raiseEvent(STOPPED, {});
251 // Start event raised, but not end event.
252 fakeViewport = new browserTest.FakeDesktopViewport;
253 var result = base.Promise.negate(
254 that.verifyScroll(2, 2, fakeViewport));
255 fakeViewport.raiseEvent(STARTED, {});
256 fakeViewport.setPluginPositionForTesting(2, 2);
260 // Start and end events raised, with correct scrolling.
261 fakeViewport = new browserTest.FakeDesktopViewport;
262 var result = that.verifyScroll(2, 2, fakeViewport);
263 fakeViewport.raiseEvent(STARTED, {});
264 fakeViewport.setPluginPositionForTesting(2, 2);
265 fakeViewport.raiseEvent(STOPPED, {});
273 * Verify that a bump scroll operation takes place and that the top-left corner
274 * of the plugin is as expected when it completes.
275 * @param {number|undefined} expectedTop The expected vertical position of the
276 * plugin, or undefined if it is not expected to change.
277 * @param {number|undefined} expectedLeft The expected horizontal position of
278 * the plugin, or undefined if it is not expected to change.
279 * @param {browserTest.FakeDesktopViewport=} opt_desktopViewport
280 * DesktopViewport fake, for testing.
283 browserTest.Bump_Scroll.prototype.verifyScroll =
284 function (expectedTop, expectedLeft, opt_desktopViewport) {
285 var desktopViewport = opt_desktopViewport || getViewportForTesting();
286 base.debug.assert(desktopViewport != null);
287 var STARTED = remoting.BumpScroller.Events.bumpScrollStarted;
288 var STOPPED = remoting.BumpScroller.Events.bumpScrollStopped;
290 var initialPosition = desktopViewport.getPluginPositionForTesting();
291 var initialTop = initialPosition.top;
292 var initialLeft = initialPosition.left;
294 /** @return {Promise} */
295 var verifyPluginPosition = function() {
296 var position = desktopViewport.getPluginPositionForTesting();
297 if (expectedLeft === undefined) {
298 expectedLeft = initialLeft;
300 if (expectedTop === undefined) {
301 expectedTop = initialTop;
303 if (position.top != expectedTop || position.left != expectedLeft) {
304 return Promise.reject(
305 new Error('No or incorrect scroll detected: (' +
306 position.left + ',' + position.top + ' instead of ' +
307 expectedLeft + ',' + expectedTop + ')'));
309 return Promise.resolve();
313 var bumpScroller = desktopViewport.getBumpScrollerForTesting();
314 var started = browserTest.expectEvent(bumpScroller, STARTED, 1000);
315 var stopped = browserTest.expectEvent(bumpScroller, STOPPED, 5000);
316 return started.then(function() {
319 // If no started event is raised, the test might still pass if it asserted
321 if (expectedTop === undefined && expectedLeft === undefined) {
322 return Promise.resolve();
324 return Promise.reject(
325 new Error('Scroll expected but no start event fired.'));
328 return verifyPluginPosition();
333 * @param {browserTest.FakeDesktopViewport=} opt_desktopViewport
334 * DesktopViewport fake, for testing.
336 * @return {Promise<boolean>} A promise that resolves to true if no scrolling
337 * occurs within a timeout.
339 browserTest.Bump_Scroll.prototype.verifyNoScroll =
340 function(opt_desktopViewport) {
341 var desktopViewport = opt_desktopViewport || getViewportForTesting();
342 var bumpScroller = desktopViewport.getBumpScrollerForTesting();
344 Promise.resolve(true);
346 return this.verifyScroll(undefined, undefined, desktopViewport);