|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import * as chai from 'chai'; |
| 8 | +import * as Blockly from 'blockly'; |
| 9 | +import { |
| 10 | + setCurrentCursorNodeById, |
| 11 | + testSetup, |
| 12 | + testFileLocations, |
| 13 | + PAUSE_TIME, |
| 14 | + focusWorkspace, |
| 15 | + getBlockElementById, |
| 16 | +} from './test_setup.js'; |
| 17 | +import {Key} from 'webdriverio'; |
| 18 | + |
| 19 | +const isKeyboardNavigating = function (browser: WebdriverIO.Browser) { |
| 20 | + return browser.execute(() => { |
| 21 | + return document.body.classList.contains('blocklyKeyboardNavigation'); |
| 22 | + }); |
| 23 | +}; |
| 24 | + |
| 25 | +suite( |
| 26 | + 'Keyboard navigation mode set on mouse or keyboard interaction', |
| 27 | + function () { |
| 28 | + // Setting timeout to unlimited as these tests take a longer time to run than most mocha tests |
| 29 | + this.timeout(0); |
| 30 | + |
| 31 | + // Setup Selenium for all of the tests |
| 32 | + suiteSetup(async function () { |
| 33 | + this.browser = await testSetup(testFileLocations.NAVIGATION_TEST_BLOCKS); |
| 34 | + }); |
| 35 | + |
| 36 | + setup(async function () { |
| 37 | + // Reset the keyboard navigation state between tests. |
| 38 | + await this.browser.execute(() => { |
| 39 | + Blockly.keyboardNavigationController.setIsActive(false); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + test('T to open toolbox enables keyboard mode', async function () { |
| 44 | + await focusWorkspace(this.browser); |
| 45 | + await this.browser.pause(PAUSE_TIME); |
| 46 | + await this.browser.keys('t'); |
| 47 | + await this.browser.pause(PAUSE_TIME); |
| 48 | + |
| 49 | + chai.assert.isTrue(await isKeyboardNavigating(this.browser)); |
| 50 | + }); |
| 51 | + |
| 52 | + test('M for move mode enables keyboard mode', async function () { |
| 53 | + await focusWorkspace(this.browser); |
| 54 | + await setCurrentCursorNodeById(this.browser, 'controls_if_2'); |
| 55 | + await this.browser.pause(PAUSE_TIME); |
| 56 | + await this.browser.keys('m'); |
| 57 | + |
| 58 | + chai.assert.isTrue(await isKeyboardNavigating(this.browser)); |
| 59 | + }); |
| 60 | + |
| 61 | + test('W for workspace cursor enables keyboard mode', async function () { |
| 62 | + await focusWorkspace(this.browser); |
| 63 | + await this.browser.pause(PAUSE_TIME); |
| 64 | + await this.browser.keys('w'); |
| 65 | + await this.browser.pause(PAUSE_TIME); |
| 66 | + |
| 67 | + chai.assert.isTrue(await isKeyboardNavigating(this.browser)); |
| 68 | + }); |
| 69 | + |
| 70 | + test('X to disconnect enables keyboard mode', async function () { |
| 71 | + await focusWorkspace(this.browser); |
| 72 | + await setCurrentCursorNodeById(this.browser, 'controls_if_2'); |
| 73 | + await this.browser.pause(PAUSE_TIME); |
| 74 | + await this.browser.keys('x'); |
| 75 | + await this.browser.pause(PAUSE_TIME); |
| 76 | + |
| 77 | + chai.assert.isTrue(await isKeyboardNavigating(this.browser)); |
| 78 | + }); |
| 79 | + |
| 80 | + test('Copy does not change keyboard mode state', async function () { |
| 81 | + await focusWorkspace(this.browser); |
| 82 | + |
| 83 | + // Make sure we're on a copyable block so that copy occurs |
| 84 | + await setCurrentCursorNodeById(this.browser, 'controls_if_2'); |
| 85 | + await this.browser.pause(PAUSE_TIME); |
| 86 | + await this.browser.keys(Key.Ctrl); |
| 87 | + await this.browser.keys('c'); |
| 88 | + await this.browser.keys(Key.Ctrl); // release ctrl key |
| 89 | + await this.browser.pause(PAUSE_TIME); |
| 90 | + |
| 91 | + chai.assert.isFalse(await isKeyboardNavigating(this.browser)); |
| 92 | + |
| 93 | + this.browser.execute(() => { |
| 94 | + Blockly.keyboardNavigationController.setIsActive(true); |
| 95 | + }); |
| 96 | + |
| 97 | + await this.browser.pause(PAUSE_TIME); |
| 98 | + await this.browser.keys(Key.Ctrl); |
| 99 | + await this.browser.keys('c'); |
| 100 | + await this.browser.keys(Key.Ctrl); // release ctrl key |
| 101 | + await this.browser.pause(PAUSE_TIME); |
| 102 | + |
| 103 | + chai.assert.isTrue(await isKeyboardNavigating(this.browser)); |
| 104 | + }); |
| 105 | + |
| 106 | + test('Delete does not change keyboard mode state', async function () { |
| 107 | + await focusWorkspace(this.browser); |
| 108 | + |
| 109 | + // Make sure we're on a deletable block so that delete occurs |
| 110 | + await setCurrentCursorNodeById(this.browser, 'controls_if_2'); |
| 111 | + await this.browser.pause(PAUSE_TIME); |
| 112 | + await this.browser.keys(Key.Backspace); |
| 113 | + await this.browser.pause(PAUSE_TIME); |
| 114 | + |
| 115 | + chai.assert.isFalse(await isKeyboardNavigating(this.browser)); |
| 116 | + |
| 117 | + this.browser.execute(() => { |
| 118 | + Blockly.keyboardNavigationController.setIsActive(true); |
| 119 | + }); |
| 120 | + |
| 121 | + // Focus a different deletable block |
| 122 | + await setCurrentCursorNodeById(this.browser, 'controls_if_1'); |
| 123 | + await this.browser.pause(PAUSE_TIME); |
| 124 | + await this.browser.keys(Key.Backspace); |
| 125 | + await this.browser.pause(PAUSE_TIME); |
| 126 | + |
| 127 | + chai.assert.isTrue(await isKeyboardNavigating(this.browser)); |
| 128 | + }); |
| 129 | + |
| 130 | + test('Right clicking a block disables keyboard mode', async function () { |
| 131 | + await focusWorkspace(this.browser); |
| 132 | + await this.browser.execute(() => { |
| 133 | + Blockly.keyboardNavigationController.setIsActive(true); |
| 134 | + }); |
| 135 | + |
| 136 | + await this.browser.pause(PAUSE_TIME); |
| 137 | + // Right click a block |
| 138 | + const element = await getBlockElementById( |
| 139 | + this.browser, |
| 140 | + 'simple_circle_1', |
| 141 | + ); |
| 142 | + await element.click({button: 'right'}); |
| 143 | + await this.browser.pause(PAUSE_TIME); |
| 144 | + |
| 145 | + chai.assert.isFalse(await isKeyboardNavigating(this.browser)); |
| 146 | + }); |
| 147 | + |
| 148 | + test('Dragging a block with mouse disables keyboard mode', async function () { |
| 149 | + await focusWorkspace(this.browser); |
| 150 | + |
| 151 | + await this.browser.execute(() => { |
| 152 | + Blockly.keyboardNavigationController.setIsActive(true); |
| 153 | + }); |
| 154 | + |
| 155 | + await this.browser.pause(PAUSE_TIME); |
| 156 | + // Drag a block |
| 157 | + const element = await getBlockElementById( |
| 158 | + this.browser, |
| 159 | + 'simple_circle_1', |
| 160 | + ); |
| 161 | + await element.dragAndDrop({x: 100, y: 100}); |
| 162 | + await this.browser.pause(PAUSE_TIME); |
| 163 | + |
| 164 | + chai.assert.isFalse(await isKeyboardNavigating(this.browser)); |
| 165 | + }); |
| 166 | + }, |
| 167 | +); |
0 commit comments