|
| 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 | + focusOnBlock, |
| 11 | + testSetup, |
| 12 | + testFileLocations, |
| 13 | + PAUSE_TIME, |
| 14 | + getBlockElementById, |
| 15 | + tabNavigateToWorkspace, |
| 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(async function () { |
| 32 | + // Reload the page between tests |
| 33 | + this.browser = await testSetup(testFileLocations.NAVIGATION_TEST_BLOCKS); |
| 34 | + |
| 35 | + await this.browser.pause(PAUSE_TIME); |
| 36 | + |
| 37 | + // Reset the keyboard navigation state between tests. |
| 38 | + await this.browser.execute(() => { |
| 39 | + Blockly.keyboardNavigationController.setIsActive(false); |
| 40 | + }); |
| 41 | + |
| 42 | + // Start with the workspace focused. |
| 43 | + await tabNavigateToWorkspace(this.browser); |
| 44 | + }); |
| 45 | + |
| 46 | + test('T to open toolbox enables keyboard mode', async function () { |
| 47 | + await this.browser.pause(PAUSE_TIME); |
| 48 | + await this.browser.keys('t'); |
| 49 | + await this.browser.pause(PAUSE_TIME); |
| 50 | + |
| 51 | + chai.assert.isTrue(await isKeyboardNavigating(this.browser)); |
| 52 | + }); |
| 53 | + |
| 54 | + test('M for move mode enables keyboard mode', async function () { |
| 55 | + await focusOnBlock(this.browser, 'controls_if_2'); |
| 56 | + await this.browser.pause(PAUSE_TIME); |
| 57 | + await this.browser.keys('m'); |
| 58 | + |
| 59 | + chai.assert.isTrue(await isKeyboardNavigating(this.browser)); |
| 60 | + }); |
| 61 | + |
| 62 | + test('W for workspace cursor enables keyboard mode', async function () { |
| 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 focusOnBlock(this.browser, 'controls_if_2'); |
| 72 | + await this.browser.pause(PAUSE_TIME); |
| 73 | + await this.browser.keys('x'); |
| 74 | + await this.browser.pause(PAUSE_TIME); |
| 75 | + |
| 76 | + chai.assert.isTrue(await isKeyboardNavigating(this.browser)); |
| 77 | + }); |
| 78 | + |
| 79 | + test('Copy does not change keyboard mode state', async function () { |
| 80 | + // Make sure we're on a copyable block so that copy occurs |
| 81 | + await focusOnBlock(this.browser, 'controls_if_2'); |
| 82 | + await this.browser.pause(PAUSE_TIME); |
| 83 | + await this.browser.keys(Key.Ctrl); |
| 84 | + await this.browser.keys('c'); |
| 85 | + await this.browser.keys(Key.Ctrl); // release ctrl key |
| 86 | + await this.browser.pause(PAUSE_TIME); |
| 87 | + |
| 88 | + chai.assert.isFalse(await isKeyboardNavigating(this.browser)); |
| 89 | + |
| 90 | + this.browser.execute(() => { |
| 91 | + Blockly.keyboardNavigationController.setIsActive(true); |
| 92 | + }); |
| 93 | + |
| 94 | + await this.browser.pause(PAUSE_TIME); |
| 95 | + await this.browser.keys(Key.Ctrl); |
| 96 | + await this.browser.keys('c'); |
| 97 | + await this.browser.keys(Key.Ctrl); // release ctrl key |
| 98 | + await this.browser.pause(PAUSE_TIME); |
| 99 | + |
| 100 | + chai.assert.isTrue(await isKeyboardNavigating(this.browser)); |
| 101 | + }); |
| 102 | + |
| 103 | + test('Delete does not change keyboard mode state', async function () { |
| 104 | + // Make sure we're on a deletable block so that delete occurs |
| 105 | + await focusOnBlock(this.browser, 'controls_if_2'); |
| 106 | + await this.browser.pause(PAUSE_TIME); |
| 107 | + await this.browser.keys(Key.Backspace); |
| 108 | + await this.browser.pause(PAUSE_TIME); |
| 109 | + |
| 110 | + chai.assert.isFalse(await isKeyboardNavigating(this.browser)); |
| 111 | + |
| 112 | + this.browser.execute(() => { |
| 113 | + Blockly.keyboardNavigationController.setIsActive(true); |
| 114 | + }); |
| 115 | + |
| 116 | + // Focus a different deletable block |
| 117 | + await focusOnBlock(this.browser, 'controls_if_1'); |
| 118 | + await this.browser.pause(PAUSE_TIME); |
| 119 | + await this.browser.keys(Key.Backspace); |
| 120 | + await this.browser.pause(PAUSE_TIME); |
| 121 | + |
| 122 | + chai.assert.isTrue(await isKeyboardNavigating(this.browser)); |
| 123 | + }); |
| 124 | + |
| 125 | + test('Right clicking a block disables keyboard mode', async function () { |
| 126 | + await this.browser.execute(() => { |
| 127 | + Blockly.keyboardNavigationController.setIsActive(true); |
| 128 | + }); |
| 129 | + |
| 130 | + await this.browser.pause(PAUSE_TIME); |
| 131 | + // Right click a block |
| 132 | + const element = await getBlockElementById(this.browser, 'controls_if_1'); |
| 133 | + await element.click({button: 'right'}); |
| 134 | + await this.browser.pause(PAUSE_TIME); |
| 135 | + |
| 136 | + chai.assert.isFalse(await isKeyboardNavigating(this.browser)); |
| 137 | + }); |
| 138 | + |
| 139 | + test('Dragging a block with mouse disables keyboard mode', async function () { |
| 140 | + await this.browser.execute(() => { |
| 141 | + Blockly.keyboardNavigationController.setIsActive(true); |
| 142 | + }); |
| 143 | + |
| 144 | + await this.browser.pause(PAUSE_TIME); |
| 145 | + // Drag a block |
| 146 | + const element = await getBlockElementById(this.browser, 'controls_if_1'); |
| 147 | + await element.dragAndDrop({x: 10, y: 10}); |
| 148 | + await this.browser.pause(PAUSE_TIME); |
| 149 | + |
| 150 | + chai.assert.isFalse(await isKeyboardNavigating(this.browser)); |
| 151 | + }); |
| 152 | + }, |
| 153 | +); |
0 commit comments