|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import * as Blockly from 'blockly'; |
| 8 | +import * as chai from 'chai'; |
| 9 | +import {Key} from 'webdriverio'; |
| 10 | +import { |
| 11 | + keyDown, |
| 12 | + keyRight, |
| 13 | + PAUSE_TIME, |
| 14 | + tabNavigateToWorkspace, |
| 15 | + testFileLocations, |
| 16 | + testSetup, |
| 17 | +} from './test_setup.js'; |
| 18 | + |
| 19 | +suite('Scrolling into view', function () { |
| 20 | + // Setting timeout to unlimited as these tests take longer time to run |
| 21 | + this.timeout(0); |
| 22 | + |
| 23 | + // Clear the workspace and load start blocks |
| 24 | + setup(async function () { |
| 25 | + this.browser = await testSetup(testFileLocations.BASE); |
| 26 | + // Predictable small window size for scrolling. |
| 27 | + this.browser.setWindowSize(800, 600); |
| 28 | + await this.browser.pause(PAUSE_TIME); |
| 29 | + }); |
| 30 | + |
| 31 | + test('Insert scrolls new block into view', async function () { |
| 32 | + await tabNavigateToWorkspace(this.browser); |
| 33 | + |
| 34 | + // Separate the two top-level blocks by moving p5_draw_1 further down. |
| 35 | + await keyDown(this.browser, 3); |
| 36 | + await this.browser.keys('m'); |
| 37 | + await this.browser.keys([Key.Alt, ...new Array(25).fill(Key.ArrowDown)]); |
| 38 | + await this.browser.keys(Key.Enter); |
| 39 | + // Scroll back up, leaving cursor on the draw block out of the viewport. |
| 40 | + await this.browser.execute(() => { |
| 41 | + const workspace = Blockly.getMainWorkspace() as Blockly.WorkspaceSvg; |
| 42 | + workspace.scrollBoundsIntoView( |
| 43 | + ( |
| 44 | + workspace.getTopBlocks(true)[0] as Blockly.BlockSvg |
| 45 | + ).getBoundingRectangleWithoutChildren(), |
| 46 | + ); |
| 47 | + }); |
| 48 | + |
| 49 | + // Insert and confirm the test block which should be scrolled into view. |
| 50 | + await this.browser.keys('t'); |
| 51 | + await keyRight(this.browser); |
| 52 | + await this.browser.keys(Key.Enter); |
| 53 | + await this.browser.keys(Key.Enter); |
| 54 | + |
| 55 | + // Assert new block has been scrolled into the viewport. |
| 56 | + await this.browser.pause(PAUSE_TIME); |
| 57 | + const inViewport = await this.browser.execute(() => { |
| 58 | + const workspace = Blockly.getMainWorkspace() as Blockly.WorkspaceSvg; |
| 59 | + const block = workspace.getBlocksByType( |
| 60 | + 'controls_if', |
| 61 | + )[0] as Blockly.BlockSvg; |
| 62 | + const blockBounds = block.getBoundingRectangleWithoutChildren(); |
| 63 | + const rawViewport = workspace.getMetricsManager().getViewMetrics(true); |
| 64 | + const viewport = new Blockly.utils.Rect( |
| 65 | + rawViewport.top, |
| 66 | + rawViewport.top + rawViewport.height, |
| 67 | + rawViewport.left, |
| 68 | + rawViewport.left + rawViewport.width, |
| 69 | + ); |
| 70 | + return viewport.contains(blockBounds.left, blockBounds.top); |
| 71 | + }); |
| 72 | + chai.assert.isTrue(inViewport); |
| 73 | + }); |
| 74 | +}); |
0 commit comments