Skip to content

Commit 7a67273

Browse files
committed
chore: Add new tests for ephemeral focus.
1 parent f08551a commit 7a67273

File tree

3 files changed

+75
-17
lines changed

3 files changed

+75
-17
lines changed

test/webdriverio/test/actions_test.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
testFileLocations,
1616
testSetup,
1717
keyRight,
18+
sendKeyAndWait,
19+
getCurrentFocusedBlockId,
1820
} from './test_setup.js';
1921

2022
suite('Menus test', function () {
@@ -31,9 +33,7 @@ suite('Menus test', function () {
3133
// Navigate to draw_circle_1.
3234
await tabNavigateToWorkspace(this.browser);
3335
await focusOnBlock(this.browser, 'draw_circle_1');
34-
await this.browser.pause(PAUSE_TIME);
35-
await this.browser.keys([Key.Ctrl, Key.Return]);
36-
await this.browser.pause(PAUSE_TIME);
36+
await sendKeyAndWait(this.browser, [Key.Ctrl, Key.Return]);
3737
chai.assert.isTrue(
3838
await contextMenuExists(this.browser, 'Duplicate'),
3939
'The menu should be openable on a block',
@@ -48,8 +48,7 @@ suite('Menus test', function () {
4848
await moveToToolboxCategory(this.browser, 'Functions');
4949
// Move to flyout.
5050
await keyRight(this.browser);
51-
await this.browser.keys([Key.Ctrl, Key.Return]);
52-
await this.browser.pause(PAUSE_TIME);
51+
await sendKeyAndWait(this.browser, [Key.Ctrl, Key.Return]);
5352

5453
chai.assert.isTrue(
5554
await contextMenuExists(this.browser, 'Help'),
@@ -62,12 +61,28 @@ suite('Menus test', function () {
6261
await tabNavigateToWorkspace(this.browser);
6362
await focusOnBlock(this.browser, 'draw_circle_1');
6463
// Start moving the block
65-
await this.browser.keys('m');
66-
await this.browser.keys([Key.Ctrl, Key.Return]);
67-
await this.browser.pause(PAUSE_TIME);
64+
await sendKeyAndWait(this.browser, 'm');
65+
await sendKeyAndWait(this.browser, [Key.Ctrl, Key.Return]);
6866
chai.assert.isTrue(
6967
await contextMenuExists(this.browser, 'Duplicate', true),
7068
'The menu should not be openable during a move',
7169
);
7270
});
71+
72+
test('Closing menu restores focus to starting node', async function () {
73+
// Navigate to draw_circle_1.
74+
await tabNavigateToWorkspace(this.browser);
75+
await focusOnBlock(this.browser, 'draw_circle_1');
76+
// Open the context menu.
77+
await sendKeyAndWait(this.browser, [Key.Ctrl, Key.Return]);
78+
79+
// Close the context menu.
80+
await sendKeyAndWait(this.browser, Key.Escape);
81+
82+
// The previously focused now should now again be focused.
83+
chai.assert.strictEqual(
84+
await getCurrentFocusedBlockId(this.browser),
85+
'draw_circle_1',
86+
);
87+
});
7388
});

test/webdriverio/test/basic_test.ts

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import {
2121
keyRight,
2222
keyUp,
2323
keyDown,
24+
sendKeyAndWait,
25+
isEphemeralFocusActive,
2426
} from './test_setup.js';
2527
import {Key} from 'webdriverio';
2628

@@ -239,13 +241,11 @@ suite('Keyboard navigation on Blocks', function () {
239241
await tabNavigateToWorkspace(this.browser);
240242
await this.browser.pause(PAUSE_TIME);
241243
await focusOnBlock(this.browser, 'text_print_1');
242-
await this.browser.keys('m');
243-
await this.browser.pause(PAUSE_TIME);
244+
await sendKeyAndWait(this.browser, 'm');
244245

245246
chai.assert.isTrue(await isDragging(this.browser));
246247

247-
await this.browser.keys(Key.Tab);
248-
await this.browser.pause(PAUSE_TIME);
248+
await sendKeyAndWait(this.browser, Key.Tab);
249249

250250
chai.assert.isFalse(await isDragging(this.browser));
251251
});
@@ -354,8 +354,7 @@ suite('Keyboard navigation on Fields', function () {
354354
// Open a field editor dropdown
355355
await focusOnBlockField(this.browser, 'logic_boolean_1', 'BOOL');
356356
await this.browser.pause(PAUSE_TIME);
357-
await this.browser.keys(Key.Enter);
358-
await this.browser.pause(PAUSE_TIME);
357+
await sendKeyAndWait(this.browser, Key.Enter);
359358

360359
// Try to navigate to a different block
361360
await keyRight(this.browser);
@@ -368,13 +367,12 @@ suite('Keyboard navigation on Fields', function () {
368367
// Open colour picker
369368
await focusOnBlockField(this.browser, 'colour_picker_1', 'COLOUR');
370369
await this.browser.pause(PAUSE_TIME);
371-
await this.browser.keys(Key.Enter);
372-
await this.browser.pause(PAUSE_TIME);
370+
await sendKeyAndWait(this.browser, Key.Enter);
373371

374372
// Move right to pick a new colour.
375373
await keyRight(this.browser);
376374
// Enter to choose.
377-
await this.browser.keys(Key.Enter);
375+
await sendKeyAndWait(this.browser, Key.Enter);
378376

379377
// Focus seems to take longer than a single pause to settle.
380378
await this.browser.waitUntil(
@@ -385,4 +383,36 @@ suite('Keyboard navigation on Fields', function () {
385383
{timeout: 1000},
386384
);
387385
});
386+
387+
test('Exiting inline field editor should restore focus to field', async function () {
388+
// Select a block with an inline-editable field.
389+
await focusOnBlock(this.browser, 'p5_canvas_1');
390+
// Select the field.
391+
await keyRight(this.browser);
392+
// Open the field's editor.
393+
await sendKeyAndWait(this.browser, Key.Enter);
394+
395+
// Exit the editor.
396+
await sendKeyAndWait(this.browser, Key.Escape);
397+
398+
// The field should be focused without ephemeral focus.
399+
chai.assert.equal(await getFocusedFieldName(this.browser), 'WIDTH');
400+
chai.assert.isFalse(await isEphemeralFocusActive(this.browser));
401+
});
402+
403+
test('Exiting drop-down field editor should restore focus to field', async function () {
404+
// Select a block with a drop-down editable field.
405+
await focusOnBlock(this.browser, 'logic_boolean_1');
406+
// Select the field.
407+
await keyRight(this.browser);
408+
// Open the field's editor.
409+
await sendKeyAndWait(this.browser, Key.Enter);
410+
411+
// Exit the editor.
412+
await sendKeyAndWait(this.browser, Key.Escape);
413+
414+
// The field should be focused without ephemeral focus.
415+
chai.assert.equal(await getFocusedFieldName(this.browser), 'BOOL');
416+
chai.assert.isFalse(await isEphemeralFocusActive(this.browser));
417+
});
388418
});

test/webdriverio/test/test_setup.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,19 @@ export async function isDragging(
550550
});
551551
}
552552

553+
/**
554+
* Returns whether Blockly's FocusManager currently holds ephemeral focus.
555+
*
556+
* @param browser The active WebdriverIO Browser object.
557+
*/
558+
export async function isEphemeralFocusActive(
559+
browser: WebdriverIO.Browser,
560+
): Promise<boolean> {
561+
return await browser.execute(() =>
562+
Blockly.getFocusManager().ephemeralFocusTaken(),
563+
);
564+
}
565+
553566
/**
554567
* Returns the result of the specificied action precondition.
555568
*

0 commit comments

Comments
 (0)