Add long running gmail memory benchmark for background tab.
[chromium-blink-merge.git] / remoting / webapp / browser_test / bump_scroll_browser_test.js
blobf59c30f7a4721a1b2e37b3aef354d62b738ef90e
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 /** @suppress {reportUnknownTypes} */
55 browserTest.FakeDesktopViewport.prototype.raiseEvent =
56 function() {
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();
64 if (view) {
65 return view.getViewportForTesting();
67 return null;
70 /** @constructor */
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.
75 /** @type {number} */
76 this.kHostDesktopSizeDelta = 10;
79 /**
80 * @param {{pin:string}} data
82 browserTest.Bump_Scroll.prototype.run = function(data) {
83 browserTest.expect(typeof data.pin == 'string');
85 if (!base.isAppsV2()) {
86 browserTest.fail(
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);
95 function cleanup() {
96 mockConnection.restore();
97 browserTest.disconnect();
100 this.testVerifyScroll().then(function() {
101 return browserTest.connectMe2Me();
102 }).then(function() {
103 return browserTest.enterPIN(data.pin);
104 }).then(
105 this.noScrollWindowed.bind(this)
106 ).then(
107 this.activateFullscreen.bind(this)
108 ).then(
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.
112 ).then(
113 this.scrollDirection.bind(this, 1.0, 0.5) // Right edge
114 ).then(
115 this.scrollDirection.bind(this, 0.5, 1.0) // Bottom edge
116 ).then(
117 this.scrollDirection.bind(this, 0.0, 0.5) // Left edge
118 ).then(
119 this.scrollDirection.bind(this, 0.5, 0.0) // Top edge
120 ).then(
121 function(value) {
122 cleanup();
123 return browserTest.pass();
125 function(error) {
126 cleanup();
127 return browserTest.fail(error);
133 * @return {Promise}
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();
145 * @return {Promise}
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
159 * @return {Promise}
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 :
170 undefined;
171 /** @type {number} */
172 var expectedLeft = widthFraction === 0.0 ? 0 :
173 widthFraction === 1.0 ? -this.kHostDesktopSizeDelta :
174 undefined;
175 var result = this.verifyScroll(expectedTop, expectedLeft);
176 this.moveMouseTo(widthFraction * screen.width,
177 heightFraction * screen.height);
178 return result;
182 * @return {Promise}
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');
199 * @param {number} x
200 * @param {number} y
202 browserTest.Bump_Scroll.prototype.moveMouseTo = function(x, y) {
203 var e = {
204 bubbles: true,
205 cancelable: false,
206 view: window,
207 detail: 0,
208 screenX: x,
209 screenY: y,
210 clientX: x,
211 clientY: y,
212 ctrlKey: false,
213 altKey: false,
214 shiftKey: false,
215 metaKey: false,
216 button: 0,
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.
230 * @return {Promise}
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;
236 var that = this;
238 // No events raised (e.g. windowed mode).
239 var result = this.verifyNoScroll(fakeViewport)
241 .then(function() {
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, {});
248 return result;
250 }).then(function() {
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, {});
258 return result;
260 }).then(function() {
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);
267 return result;
269 }).then(function() {
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, {});
276 return result;
279 return result;
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.
291 * @return {Promise}
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 + ')'));
318 } else {
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() {
327 return stopped;
328 }, function() {
329 // If no started event is raised, the test might still pass if it asserted
330 // no scrolling.
331 if (expectedTop === undefined && expectedLeft === undefined) {
332 return Promise.resolve();
333 } else {
334 return Promise.reject(
335 new Error('Scroll expected but no start event fired.'));
337 }).then(function() {
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();
353 if (!bumpScroller) {
354 Promise.resolve(true);
356 return this.verifyScroll(undefined, undefined, desktopViewport);