| 
									
										
										
										
											2021-09-14 15:22:52 -04:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright (c) Microsoft Corporation. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-10-11 10:52:17 -04:00
										 |  |  | import type { Page } from 'playwright-core'; | 
					
						
							| 
									
										
										
										
											2021-09-14 15:22:52 -04:00
										 |  |  | import { test as it, expect } from './pageTest'; | 
					
						
							| 
									
										
										
										
											2021-11-23 02:59:32 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-25 22:56:51 +01:00
										 |  |  | it.skip(({ isElectron, browserMajorVersion, isAndroid }) => { | 
					
						
							| 
									
										
										
										
											2021-09-14 15:22:52 -04:00
										 |  |  |   // Old Electron has flaky wheel events.
 | 
					
						
							| 
									
										
										
										
											2022-02-25 22:56:51 +01:00
										 |  |  |   return (isElectron && browserMajorVersion <= 11) || isAndroid; | 
					
						
							| 
									
										
										
										
											2021-09-14 15:22:52 -04:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2022-03-10 19:42:52 +01:00
										 |  |  | it('should dispatch wheel events @smoke', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2021-09-14 15:22:52 -04:00
										 |  |  |   await page.setContent(`<div style="width: 5000px; height: 5000px;"></div>`); | 
					
						
							|  |  |  |   await page.mouse.move(50, 60); | 
					
						
							|  |  |  |   await listenForWheelEvents(page, 'div'); | 
					
						
							|  |  |  |   await page.mouse.wheel(0, 100); | 
					
						
							| 
									
										
										
										
											2021-11-02 13:56:12 -07:00
										 |  |  |   await page.waitForFunction('window.scrollY === 100'); | 
					
						
							| 
									
										
										
										
											2021-09-14 15:22:52 -04:00
										 |  |  |   expect(await page.evaluate('window.lastEvent')).toEqual({ | 
					
						
							|  |  |  |     deltaX: 0, | 
					
						
							|  |  |  |     deltaY: 100, | 
					
						
							|  |  |  |     clientX: 50, | 
					
						
							|  |  |  |     clientY: 60, | 
					
						
							|  |  |  |     deltaMode: 0, | 
					
						
							|  |  |  |     ctrlKey: false, | 
					
						
							|  |  |  |     shiftKey: false, | 
					
						
							|  |  |  |     altKey: false, | 
					
						
							|  |  |  |     metaKey: false, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should scroll when nobody is listening', async ({ page, server }) => { | 
					
						
							| 
									
										
										
										
											2021-09-14 15:22:52 -04:00
										 |  |  |   await page.goto(server.PREFIX + '/input/scrollable.html'); | 
					
						
							|  |  |  |   await page.mouse.move(50, 60); | 
					
						
							|  |  |  |   await page.mouse.wheel(0, 100); | 
					
						
							|  |  |  |   await page.waitForFunction('window.scrollY === 100'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should set the modifiers', async ({ page }) => { | 
					
						
							| 
									
										
										
										
											2021-09-14 15:22:52 -04:00
										 |  |  |   await page.setContent(`<div style="width: 5000px; height: 5000px;"></div>`); | 
					
						
							|  |  |  |   await page.mouse.move(50, 60); | 
					
						
							|  |  |  |   await listenForWheelEvents(page, 'div'); | 
					
						
							|  |  |  |   await page.keyboard.down('Shift'); | 
					
						
							|  |  |  |   await page.mouse.wheel(0, 100); | 
					
						
							|  |  |  |   expect(await page.evaluate('window.lastEvent')).toEqual({ | 
					
						
							|  |  |  |     deltaX: 0, | 
					
						
							|  |  |  |     deltaY: 100, | 
					
						
							|  |  |  |     clientX: 50, | 
					
						
							|  |  |  |     clientY: 60, | 
					
						
							|  |  |  |     deltaMode: 0, | 
					
						
							|  |  |  |     ctrlKey: false, | 
					
						
							|  |  |  |     shiftKey: true, | 
					
						
							|  |  |  |     altKey: false, | 
					
						
							|  |  |  |     metaKey: false, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should scroll horizontally', async ({ page }) => { | 
					
						
							| 
									
										
										
										
											2021-09-14 15:22:52 -04:00
										 |  |  |   await page.setContent(`<div style="width: 5000px; height: 5000px;"></div>`); | 
					
						
							|  |  |  |   await page.mouse.move(50, 60); | 
					
						
							|  |  |  |   await listenForWheelEvents(page, 'div'); | 
					
						
							|  |  |  |   await page.mouse.wheel(100, 0); | 
					
						
							|  |  |  |   expect(await page.evaluate('window.lastEvent')).toEqual({ | 
					
						
							|  |  |  |     deltaX: 100, | 
					
						
							|  |  |  |     deltaY: 0, | 
					
						
							|  |  |  |     clientX: 50, | 
					
						
							|  |  |  |     clientY: 60, | 
					
						
							|  |  |  |     deltaMode: 0, | 
					
						
							|  |  |  |     ctrlKey: false, | 
					
						
							|  |  |  |     shiftKey: false, | 
					
						
							|  |  |  |     altKey: false, | 
					
						
							|  |  |  |     metaKey: false, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await page.waitForFunction('window.scrollX === 100'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 18:58:08 +02:00
										 |  |  | it('should work when the event is canceled', async ({ page }) => { | 
					
						
							| 
									
										
										
										
											2021-09-14 15:22:52 -04:00
										 |  |  |   await page.setContent(`<div style="width: 5000px; height: 5000px;"></div>`); | 
					
						
							|  |  |  |   await page.mouse.move(50, 60); | 
					
						
							|  |  |  |   await listenForWheelEvents(page, 'div'); | 
					
						
							|  |  |  |   await page.evaluate(() => { | 
					
						
							|  |  |  |     document.querySelector('div').addEventListener('wheel', e => e.preventDefault()); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   await page.mouse.wheel(0, 100); | 
					
						
							|  |  |  |   expect(await page.evaluate('window.lastEvent')).toEqual({ | 
					
						
							|  |  |  |     deltaX: 0, | 
					
						
							|  |  |  |     deltaY: 100, | 
					
						
							|  |  |  |     clientX: 50, | 
					
						
							|  |  |  |     clientY: 60, | 
					
						
							|  |  |  |     deltaMode: 0, | 
					
						
							|  |  |  |     ctrlKey: false, | 
					
						
							|  |  |  |     shiftKey: false, | 
					
						
							|  |  |  |     altKey: false, | 
					
						
							|  |  |  |     metaKey: false, | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2021-12-15 17:33:09 -08:00
										 |  |  |   // Give the page a chance to scroll.
 | 
					
						
							| 
									
										
										
										
											2021-09-14 15:22:52 -04:00
										 |  |  |   await page.waitForTimeout(100); | 
					
						
							| 
									
										
										
										
											2021-12-15 17:33:09 -08:00
										 |  |  |   // Ensure that it did not.
 | 
					
						
							| 
									
										
										
										
											2021-09-14 15:22:52 -04:00
										 |  |  |   expect(await page.evaluate('window.scrollY')).toBe(0); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function listenForWheelEvents(page: Page, selector: string) { | 
					
						
							|  |  |  |   await page.evaluate(selector => { | 
					
						
							|  |  |  |     document.querySelector(selector).addEventListener('wheel', (e: WheelEvent) => { | 
					
						
							|  |  |  |       window['lastEvent'] = { | 
					
						
							|  |  |  |         deltaX: e.deltaX, | 
					
						
							|  |  |  |         deltaY: e.deltaY, | 
					
						
							|  |  |  |         clientX: e.clientX, | 
					
						
							|  |  |  |         clientY: e.clientY, | 
					
						
							|  |  |  |         deltaMode: e.deltaMode, | 
					
						
							|  |  |  |         ctrlKey: e.ctrlKey, | 
					
						
							|  |  |  |         shiftKey: e.shiftKey, | 
					
						
							|  |  |  |         altKey: e.altKey, | 
					
						
							|  |  |  |         metaKey: e.metaKey, | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }, { passive: false }); | 
					
						
							|  |  |  |   }, selector); | 
					
						
							|  |  |  | } |