diff --git a/packages/blockly/tests/browser/test/focus_manager_test.mjs b/packages/blockly/tests/browser/test/focus_manager_test.mjs
index cd3c1567f7b..f7e5ef608e1 100644
--- a/packages/blockly/tests/browser/test/focus_manager_test.mjs
+++ b/packages/blockly/tests/browser/test/focus_manager_test.mjs
@@ -23,7 +23,7 @@
import * as chai from 'chai';
import {FIXTURE_HTML} from '../../mocha/test_helpers/dom_fixtures.js';
-import {focusableTreeImplFactory} from '../../mocha/test_helpers/focusable_tree.js';
+import {focusableTreeImplFactory} from '../../mocha/test_helpers/focusable_tree.ts';
import {testFileLocations, testSetup} from './test_setup.mjs';
/**
diff --git a/packages/blockly/tests/mocha/block_test.js b/packages/blockly/tests/mocha/block_test.js
index 65460e4337c..c54f3dbe569 100644
--- a/packages/blockly/tests/mocha/block_test.js
+++ b/packages/blockly/tests/mocha/block_test.js
@@ -12,7 +12,7 @@ import {EndRowInput} from '#core/inputs/end_row_input.js';
import {isCommentIcon} from '#core/interfaces/i_comment_icon.js';
import {Size} from '#core/utils/size.js';
import {assert} from 'chai';
-import {createRenderedBlock} from './test_helpers/block_definitions.js';
+import {createRenderedBlock} from './test_helpers/block_definitions.ts';
import {
createChangeListenerSpy,
createMockEvent,
diff --git a/packages/blockly/tests/mocha/blocks/lists_test.js b/packages/blockly/tests/mocha/blocks/lists_test.js
index 9fafbb54788..e627b84311f 100644
--- a/packages/blockly/tests/mocha/blocks/lists_test.js
+++ b/packages/blockly/tests/mocha/blocks/lists_test.js
@@ -6,7 +6,7 @@
import {ConnectionType} from '#core/connection_type.js';
import {assert} from 'chai';
-import {defineStatementBlock} from '../test_helpers/block_definitions.js';
+import {defineStatementBlock} from '../test_helpers/block_definitions.ts';
import {runSerializationTestSuite} from '../test_helpers/serialization.js';
import {
sharedTestSetup,
diff --git a/packages/blockly/tests/mocha/blocks/procedures_test.js b/packages/blockly/tests/mocha/blocks/procedures_test.js
index f654486f355..1382e4c99df 100644
--- a/packages/blockly/tests/mocha/blocks/procedures_test.js
+++ b/packages/blockly/tests/mocha/blocks/procedures_test.js
@@ -6,7 +6,7 @@
import * as Blockly from '#core/blockly.js';
import {assert} from 'chai';
-import {defineRowBlock} from '../test_helpers/block_definitions.js';
+import {defineRowBlock} from '../test_helpers/block_definitions.ts';
import {
assertCallBlockStructure,
assertDefBlockStructure,
diff --git a/packages/blockly/tests/mocha/blocks/variables_test.js b/packages/blockly/tests/mocha/blocks/variables_test.js
index dd3154ab392..92331cdfa87 100644
--- a/packages/blockly/tests/mocha/blocks/variables_test.js
+++ b/packages/blockly/tests/mocha/blocks/variables_test.js
@@ -294,7 +294,7 @@ suite('Variables', function () {
this.workspace
.getProcedureMap()
.add(
- new MockProcedureModel('test name')
+ new MockProcedureModel(this.workspace, 'test name')
.insertParameter(
new MockParameterModelWithVar('x', this.workspace),
0,
@@ -320,7 +320,7 @@ suite('Variables', function () {
this.workspace
.getProcedureMap()
.add(
- new MockProcedureModel('test name')
+ new MockProcedureModel(this.workspace, 'test name')
.insertParameter(
new MockParameterModelWithVar(
'definitely not x',
diff --git a/packages/blockly/tests/mocha/comment_deserialization_test.js b/packages/blockly/tests/mocha/comment_deserialization_test.js
index bd21698d563..ca01fa27ca5 100644
--- a/packages/blockly/tests/mocha/comment_deserialization_test.js
+++ b/packages/blockly/tests/mocha/comment_deserialization_test.js
@@ -10,7 +10,7 @@ import {
sharedTestSetup,
sharedTestTeardown,
} from './test_helpers/setup_teardown.js';
-import {simulateClick} from './test_helpers/user_input.js';
+import {simulateClick} from './test_helpers/user_input.ts';
suite('Comment Deserialization', function () {
setup(function () {
diff --git a/packages/blockly/tests/mocha/comment_view_test.js b/packages/blockly/tests/mocha/comment_view_test.ts
similarity index 67%
rename from packages/blockly/tests/mocha/comment_view_test.js
rename to packages/blockly/tests/mocha/comment_view_test.ts
index 6928d0f4406..ace2c3062af 100644
--- a/packages/blockly/tests/mocha/comment_view_test.js
+++ b/packages/blockly/tests/mocha/comment_view_test.ts
@@ -4,7 +4,9 @@
* SPDX-License-Identifier: Apache-2.0
*/
+import * as Blockly from '#core/blockly.js';
import {assert} from 'chai';
+import sinon from 'sinon';
import {
DEFAULT_INJECT_OPTIONS,
sharedTestSetup,
@@ -12,23 +14,29 @@ import {
} from './test_helpers/setup_teardown.js';
suite('Workspace comment', function () {
- setup(function () {
+ let workspace: Blockly.WorkspaceSvg;
+ let commentView: Blockly.comments.CommentView;
+
+ setup(function (this: Mocha.Context) {
sharedTestSetup.call(this);
- this.workspace = Blockly.inject('blocklyDiv', DEFAULT_INJECT_OPTIONS);
- this.commentView = new Blockly.comments.CommentView(this.workspace);
+ workspace = Blockly.inject('blocklyDiv', DEFAULT_INJECT_OPTIONS);
+ commentView = new Blockly.comments.CommentView(
+ workspace,
+ 'test-comment-id',
+ );
});
- teardown(function () {
- sharedTestTeardown.call(this);
+ teardown(function (this: Mocha.Context) {
+ sharedTestTeardown.call(this, workspace);
});
suite('Listeners', function () {
suite('Text change listeners', function () {
test('text change listeners are called when text is changed', function () {
const spy = sinon.spy();
- this.commentView.addTextChangeListener(spy);
+ commentView.addTextChangeListener(spy);
- this.commentView.setText('test');
+ commentView.setText('test');
assert.isTrue(spy.calledOnce, 'Expected the spy to be called once');
assert.isTrue(
@@ -39,15 +47,15 @@ suite('Workspace comment', function () {
test('text change listeners can remove themselves without skipping others', function () {
const fake1 = sinon.fake();
- const fake2 = sinon.fake(() =>
- this.commentView.removeTextChangeListener(fake2),
+ const fake2: sinon.SinonSpy = sinon.fake(() =>
+ commentView.removeTextChangeListener(fake2),
);
const fake3 = sinon.fake();
- this.commentView.addTextChangeListener(fake1);
- this.commentView.addTextChangeListener(fake2);
- this.commentView.addTextChangeListener(fake3);
+ commentView.addTextChangeListener(fake1);
+ commentView.addTextChangeListener(fake2);
+ commentView.addTextChangeListener(fake3);
- this.commentView.setText('test');
+ commentView.setText('test');
assert.isTrue(
fake1.calledOnce,
@@ -67,11 +75,11 @@ suite('Workspace comment', function () {
suite('Size change listeners', function () {
test('size change listeners are called when text is changed', function () {
const spy = sinon.spy();
- this.commentView.addSizeChangeListener(spy);
- const originalSize = this.commentView.getSize();
+ commentView.addSizeChangeListener(spy);
+ const originalSize = commentView.getSize();
const newSize = new Blockly.utils.Size(1337, 1337);
- this.commentView.setSize(newSize);
+ commentView.setSize(newSize);
assert.isTrue(spy.calledOnce, 'Expected the spy to be called once');
assert.isTrue(
@@ -82,16 +90,16 @@ suite('Workspace comment', function () {
test('size change listeners can remove themselves without skipping others', function () {
const fake1 = sinon.fake();
- const fake2 = sinon.fake(() =>
- this.commentView.removeSizeChangeListener(fake2),
+ const fake2: sinon.SinonSpy = sinon.fake(() =>
+ commentView.removeSizeChangeListener(fake2),
);
const fake3 = sinon.fake();
- this.commentView.addSizeChangeListener(fake1);
- this.commentView.addSizeChangeListener(fake2);
- this.commentView.addSizeChangeListener(fake3);
+ commentView.addSizeChangeListener(fake1);
+ commentView.addSizeChangeListener(fake2);
+ commentView.addSizeChangeListener(fake3);
const newSize = new Blockly.utils.Size(1337, 1337);
- this.commentView.setSize(newSize);
+ commentView.setSize(newSize);
assert.isTrue(
fake1.calledOnce,
@@ -111,9 +119,9 @@ suite('Workspace comment', function () {
suite('Collapse change listeners', function () {
test('collapse change listeners are called when text is changed', function () {
const spy = sinon.spy();
- this.commentView.addOnCollapseListener(spy);
+ commentView.addOnCollapseListener(spy);
- this.commentView.setCollapsed(true);
+ commentView.setCollapsed(true);
assert.isTrue(spy.calledOnce, 'Expected the spy to be called once');
assert.isTrue(
@@ -124,15 +132,15 @@ suite('Workspace comment', function () {
test('collapse change listeners can remove themselves without skipping others', function () {
const fake1 = sinon.fake();
- const fake2 = sinon.fake(() =>
- this.commentView.removeOnCollapseListener(fake2),
+ const fake2: sinon.SinonSpy = sinon.fake(() =>
+ commentView.removeOnCollapseListener(fake2),
);
const fake3 = sinon.fake();
- this.commentView.addOnCollapseListener(fake1);
- this.commentView.addOnCollapseListener(fake2);
- this.commentView.addOnCollapseListener(fake3);
+ commentView.addOnCollapseListener(fake1);
+ commentView.addOnCollapseListener(fake2);
+ commentView.addOnCollapseListener(fake3);
- this.commentView.setCollapsed(true);
+ commentView.setCollapsed(true);
assert.isTrue(
fake1.calledOnce,
@@ -152,24 +160,24 @@ suite('Workspace comment', function () {
suite('Dispose change listeners', function () {
test('dispose listeners are called when text is changed', function () {
const spy = sinon.spy();
- this.commentView.addDisposeListener(spy);
+ commentView.addDisposeListener(spy);
- this.commentView.dispose();
+ commentView.dispose();
assert.isTrue(spy.calledOnce, 'Expected the spy to be called once');
});
test('dispose listeners can remove themselves without skipping others', function () {
const fake1 = sinon.fake();
- const fake2 = sinon.fake(() =>
- this.commentView.removeDisposeListener(fake2),
+ const fake2: sinon.SinonSpy = sinon.fake(() =>
+ commentView.removeDisposeListener(fake2),
);
const fake3 = sinon.fake();
- this.commentView.addDisposeListener(fake1);
- this.commentView.addDisposeListener(fake2);
- this.commentView.addDisposeListener(fake3);
+ commentView.addDisposeListener(fake1);
+ commentView.addDisposeListener(fake2);
+ commentView.addDisposeListener(fake3);
- this.commentView.dispose();
+ commentView.dispose();
assert.isTrue(
fake1.calledOnce,
diff --git a/packages/blockly/tests/mocha/connection_checker_test.js b/packages/blockly/tests/mocha/connection_checker_test.js
deleted file mode 100644
index 6d74baf8b08..00000000000
--- a/packages/blockly/tests/mocha/connection_checker_test.js
+++ /dev/null
@@ -1,702 +0,0 @@
-/**
- * @license
- * Copyright 2020 Google LLC
- * SPDX-License-Identifier: Apache-2.0
- */
-
-import {ConnectionType} from '#core/connection_type.js';
-import {assert} from 'chai';
-import {
- DEFAULT_INJECT_OPTIONS,
- sharedTestSetup,
- sharedTestTeardown,
- workspaceTeardown,
-} from './test_helpers/setup_teardown.js';
-
-suite('Connection checker', function () {
- setup(function () {
- sharedTestSetup.call(this);
- });
- teardown(function () {
- sharedTestTeardown.call(this);
- });
- suiteSetup(function () {
- this.checker = new Blockly.ConnectionChecker();
- });
- suite('Safety checks', function () {
- function assertReasonHelper(checker, one, two, reason) {
- assert.equal(checker.canConnectWithReason(one, two), reason);
- // Order should not matter.
- assert.equal(checker.canConnectWithReason(two, one), reason);
- }
-
- test('Target Null', function () {
- const connection = new Blockly.Connection(
- {id: 'test'},
- ConnectionType.INPUT_VALUE,
- );
- assertReasonHelper(
- this.checker,
- connection,
- null,
- Blockly.Connection.REASON_TARGET_NULL,
- );
- });
- test('Target Self', function () {
- const block = {id: 'test', workspace: 1};
- const connection1 = new Blockly.Connection(
- block,
- ConnectionType.INPUT_VALUE,
- );
- const connection2 = new Blockly.Connection(
- block,
- ConnectionType.OUTPUT_VALUE,
- );
-
- assertReasonHelper(
- this.checker,
- connection1,
- connection2,
- Blockly.Connection.REASON_SELF_CONNECTION,
- );
- });
- test('Different Workspaces', function () {
- const connection1 = new Blockly.Connection(
- {id: 'test1', workspace: 1},
- ConnectionType.INPUT_VALUE,
- );
- const connection2 = new Blockly.Connection(
- {id: 'test2', workspace: 2},
- ConnectionType.OUTPUT_VALUE,
- );
-
- assertReasonHelper(
- this.checker,
- connection1,
- connection2,
- Blockly.Connection.REASON_DIFFERENT_WORKSPACES,
- );
- });
- suite('Types', function () {
- setup(function () {
- // We have to declare each separately so that the connections belong
- // on different blocks.
- const prevBlock = {id: 'test1', isShadow: function () {}};
- const nextBlock = {id: 'test2', isShadow: function () {}};
- const outBlock = {id: 'test3', isShadow: function () {}};
- const inBlock = {id: 'test4', isShadow: function () {}};
- this.previous = new Blockly.Connection(
- prevBlock,
- ConnectionType.PREVIOUS_STATEMENT,
- );
- this.next = new Blockly.Connection(
- nextBlock,
- ConnectionType.NEXT_STATEMENT,
- );
- this.output = new Blockly.Connection(
- outBlock,
- ConnectionType.OUTPUT_VALUE,
- );
- this.input = new Blockly.Connection(
- inBlock,
- ConnectionType.INPUT_VALUE,
- );
- });
- test('Previous, Next', function () {
- assertReasonHelper(
- this.checker,
- this.previous,
- this.next,
- Blockly.Connection.CAN_CONNECT,
- );
- });
- test('Previous, Output', function () {
- assertReasonHelper(
- this.checker,
- this.previous,
- this.output,
- Blockly.Connection.REASON_WRONG_TYPE,
- );
- });
- test('Previous, Input', function () {
- assertReasonHelper(
- this.checker,
- this.previous,
- this.input,
- Blockly.Connection.REASON_WRONG_TYPE,
- );
- });
- test('Next, Previous', function () {
- assertReasonHelper(
- this.checker,
- this.next,
- this.previous,
- Blockly.Connection.CAN_CONNECT,
- );
- });
- test('Next, Output', function () {
- assertReasonHelper(
- this.checker,
- this.next,
- this.output,
- Blockly.Connection.REASON_WRONG_TYPE,
- );
- });
- test('Next, Input', function () {
- assertReasonHelper(
- this.checker,
- this.next,
- this.input,
- Blockly.Connection.REASON_WRONG_TYPE,
- );
- });
- test('Output, Previous', function () {
- assertReasonHelper(
- this.checker,
- this.previous,
- this.output,
- Blockly.Connection.REASON_WRONG_TYPE,
- );
- });
- test('Output, Next', function () {
- assertReasonHelper(
- this.checker,
- this.output,
- this.next,
- Blockly.Connection.REASON_WRONG_TYPE,
- );
- });
- test('Output, Input', function () {
- assertReasonHelper(
- this.checker,
- this.output,
- this.input,
- Blockly.Connection.CAN_CONNECT,
- );
- });
- test('Input, Previous', function () {
- assertReasonHelper(
- this.checker,
- this.previous,
- this.input,
- Blockly.Connection.REASON_WRONG_TYPE,
- );
- });
- test('Input, Next', function () {
- assertReasonHelper(
- this.checker,
- this.input,
- this.next,
- Blockly.Connection.REASON_WRONG_TYPE,
- );
- });
- test('Input, Output', function () {
- assertReasonHelper(
- this.checker,
- this.input,
- this.output,
- Blockly.Connection.CAN_CONNECT,
- );
- });
- });
- suite('Shadows', function () {
- test('Previous Shadow', function () {
- const prevBlock = {
- id: 'test1',
- isShadow: function () {
- return true;
- },
- };
- const nextBlock = {
- id: 'test2',
- isShadow: function () {
- return false;
- },
- };
- const prev = new Blockly.Connection(
- prevBlock,
- ConnectionType.PREVIOUS_STATEMENT,
- );
- const next = new Blockly.Connection(
- nextBlock,
- ConnectionType.NEXT_STATEMENT,
- );
-
- assertReasonHelper(
- this.checker,
- prev,
- next,
- Blockly.Connection.CAN_CONNECT,
- );
- });
- test('Next Shadow', function () {
- const prevBlock = {
- id: 'test1',
- isShadow: function () {
- return false;
- },
- };
- const nextBlock = {
- id: 'test2',
- isShadow: function () {
- return true;
- },
- };
- const prev = new Blockly.Connection(
- prevBlock,
- ConnectionType.PREVIOUS_STATEMENT,
- );
- const next = new Blockly.Connection(
- nextBlock,
- ConnectionType.NEXT_STATEMENT,
- );
-
- assertReasonHelper(
- this.checker,
- prev,
- next,
- Blockly.Connection.REASON_SHADOW_PARENT,
- );
- });
- test('Prev and Next Shadow', function () {
- const prevBlock = {
- id: 'test1',
- isShadow: function () {
- return true;
- },
- };
- const nextBlock = {
- id: 'test2',
- isShadow: function () {
- return true;
- },
- };
- const prev = new Blockly.Connection(
- prevBlock,
- ConnectionType.PREVIOUS_STATEMENT,
- );
- const next = new Blockly.Connection(
- nextBlock,
- ConnectionType.NEXT_STATEMENT,
- );
-
- assertReasonHelper(
- this.checker,
- prev,
- next,
- Blockly.Connection.CAN_CONNECT,
- );
- });
- test('Output Shadow', function () {
- const outBlock = {
- id: 'test1',
- isShadow: function () {
- return true;
- },
- };
- const inBlock = {
- id: 'test2',
- isShadow: function () {
- return false;
- },
- };
- const outCon = new Blockly.Connection(
- outBlock,
- ConnectionType.OUTPUT_VALUE,
- );
- const inCon = new Blockly.Connection(
- inBlock,
- ConnectionType.INPUT_VALUE,
- );
-
- assertReasonHelper(
- this.checker,
- outCon,
- inCon,
- Blockly.Connection.CAN_CONNECT,
- );
- });
- test('Input Shadow', function () {
- const outBlock = {
- id: 'test1',
- isShadow: function () {
- return false;
- },
- };
- const inBlock = {
- id: 'test2',
- isShadow: function () {
- return true;
- },
- };
- const outCon = new Blockly.Connection(
- outBlock,
- ConnectionType.OUTPUT_VALUE,
- );
- const inCon = new Blockly.Connection(
- inBlock,
- ConnectionType.INPUT_VALUE,
- );
-
- assertReasonHelper(
- this.checker,
- outCon,
- inCon,
- Blockly.Connection.REASON_SHADOW_PARENT,
- );
- });
- test('Output and Input Shadow', function () {
- const outBlock = {
- id: 'test1',
- isShadow: function () {
- return true;
- },
- };
- const inBlock = {
- id: 'test2',
- isShadow: function () {
- return true;
- },
- };
- const outCon = new Blockly.Connection(
- outBlock,
- ConnectionType.OUTPUT_VALUE,
- );
- const inCon = new Blockly.Connection(
- inBlock,
- ConnectionType.INPUT_VALUE,
- );
-
- assertReasonHelper(
- this.checker,
- outCon,
- inCon,
- Blockly.Connection.CAN_CONNECT,
- );
- });
- });
- suite('Output and Previous', function () {
- /**
- * Update two connections to target each other.
- * @param {Connection} first The first connection to update.
- * @param {Connection} second The second connection to update.
- */
- const connectReciprocally = function (first, second) {
- if (!first || !second) {
- throw Error('Cannot connect null connections.');
- }
- first.targetConnection = second;
- second.targetConnection = first;
- };
- test('Output connected, adding previous', function () {
- const outBlock = {
- id: 'test1',
- isShadow: function () {},
- };
- const inBlock = {
- id: 'test2',
- isShadow: function () {},
- };
- const outCon = new Blockly.Connection(
- outBlock,
- ConnectionType.OUTPUT_VALUE,
- );
- const inCon = new Blockly.Connection(
- inBlock,
- ConnectionType.INPUT_VALUE,
- );
- outBlock.outputConnection = outCon;
- inBlock.inputConnection = inCon;
- connectReciprocally(inCon, outCon);
- const prevCon = new Blockly.Connection(
- outBlock,
- ConnectionType.PREVIOUS_STATEMENT,
- );
- const nextBlock = {
- id: 'test3',
- isShadow: function () {},
- };
- const nextCon = new Blockly.Connection(
- nextBlock,
- ConnectionType.NEXT_STATEMENT,
- );
-
- assertReasonHelper(
- this.checker,
- prevCon,
- nextCon,
- Blockly.Connection.REASON_PREVIOUS_AND_OUTPUT,
- );
- });
- test('Previous connected, adding output', function () {
- const prevBlock = {
- id: 'test1',
- isShadow: function () {},
- };
- const nextBlock = {
- id: 'test2',
- isShadow: function () {},
- };
- const prevCon = new Blockly.Connection(
- prevBlock,
- ConnectionType.PREVIOUS_STATEMENT,
- );
- const nextCon = new Blockly.Connection(
- nextBlock,
- ConnectionType.NEXT_STATEMENT,
- );
- prevBlock.previousConnection = prevCon;
- nextBlock.nextConnection = nextCon;
- connectReciprocally(prevCon, nextCon);
- const outCon = new Blockly.Connection(
- prevBlock,
- ConnectionType.OUTPUT_VALUE,
- );
- const inBlock = {
- id: 'test3',
- isShadow: function () {},
- };
- const inCon = new Blockly.Connection(
- inBlock,
- ConnectionType.INPUT_VALUE,
- );
-
- assertReasonHelper(
- this.checker,
- outCon,
- inCon,
- Blockly.Connection.REASON_PREVIOUS_AND_OUTPUT,
- );
- });
- });
- });
- suite('Check Types', function () {
- setup(function () {
- this.con1 = new Blockly.Connection(
- {id: 'test1'},
- ConnectionType.PREVIOUS_STATEMENT,
- );
- this.con2 = new Blockly.Connection(
- {id: 'test2'},
- ConnectionType.NEXT_STATEMENT,
- );
- });
- function assertCheckTypes(checker, one, two) {
- assert.isTrue(checker.doTypeChecks(one, two));
- // Order should not matter.
- assert.isTrue(checker.doTypeChecks(one, two));
- }
- test('No Types', function () {
- assertCheckTypes(this.checker, this.con1, this.con2);
- });
- test('Same Type', function () {
- this.con1.setCheck('type1');
- this.con2.setCheck('type1');
- assertCheckTypes(this.checker, this.con1, this.con2);
- });
- test('Same Types', function () {
- this.con1.setCheck(['type1', 'type2']);
- this.con2.setCheck(['type1', 'type2']);
- assertCheckTypes(this.checker, this.con1, this.con2);
- });
- test('Single Same Type', function () {
- this.con1.setCheck(['type1', 'type2']);
- this.con2.setCheck(['type1', 'type3']);
- assertCheckTypes(this.checker, this.con1, this.con2);
- });
- test('One Typed, One Promiscuous', function () {
- this.con1.setCheck('type1');
- assertCheckTypes(this.checker, this.con1, this.con2);
- });
- test('No Compatible Types', function () {
- this.con1.setCheck('type1');
- this.con2.setCheck('type2');
- assert.isFalse(this.checker.doTypeChecks(this.con1, this.con2));
- });
- });
- suite('Dragging Checks', function () {
- setup(function () {
- this.workspace = Blockly.inject('blocklyDiv', DEFAULT_INJECT_OPTIONS);
- });
-
- teardown(function () {
- workspaceTeardown.call(this, this.workspace);
- });
-
- suite('Stacks', function () {
- setup(function () {
- // Load in three blocks: A and B are connected (next/prev); B is unmovable.
- Blockly.Xml.domToWorkspace(
- Blockly.utils.xml
- .textToDom(`
-
-
-
-
-
-
-
- `),
- this.workspace,
- );
- this.blockA = this.workspace.getBlockById('A');
- this.blockB = this.workspace.getBlockById('B');
- this.blockC = this.workspace.getBlockById('C');
- this.checker = this.workspace.connectionChecker;
- });
-
- test('Connect a stack', function () {
- // block C is not connected to block A; both are movable.
- assert.isTrue(
- this.checker.doDragChecks(
- this.blockC.nextConnection,
- this.blockA.previousConnection,
- 9000,
- ),
- 'Should connect two compatible stack blocks',
- );
- });
-
- test('Connect to unmovable shadow block', function () {
- // Remove original test blocks.
- this.workspace.clear();
-
- // Add the same test blocks, but this time block B is a shadow block.
- Blockly.Xml.domToWorkspace(
- Blockly.utils.xml
- .textToDom(`
-
-
-
-
-
-
-
- `),
- this.workspace,
- );
- [this.blockA, this.blockB, this.blockC] =
- this.workspace.getAllBlocks(true);
-
- // Try to connect blockC into the input connection of blockA, replacing blockB.
- // This is allowed because shadow blocks can always be replaced, even though
- // they are unmovable.
- assert.isTrue(
- this.checker.doDragChecks(
- this.blockC.previousConnection,
- this.blockA.nextConnection,
- 9000,
- ),
- 'Should connect in place of a shadow block',
- );
- });
-
- test('Do not splice into unmovable stack', function () {
- // Try to connect blockC above blockB. It shouldn't work because B is not movable
- // and is already connected to A's nextConnection.
- assert.isFalse(
- this.checker.doDragChecks(
- this.blockC.previousConnection,
- this.blockA.nextConnection,
- 9000,
- ),
- 'Should not splice in a block above an unmovable block',
- );
- });
-
- test('Connect to bottom of unmovable stack', function () {
- // Try to connect blockC below blockB.
- // This is allowed even though B is not movable because it is on B's nextConnection.
- assert.isTrue(
- this.checker.doDragChecks(
- this.blockC.previousConnection,
- this.blockB.nextConnection,
- 9000,
- ),
- 'Should connect below an unmovable stack block',
- );
- });
-
- test('Connect to unconnected unmovable block', function () {
- this.blockB.previousConnection.disconnect();
- this.blockA.dispose();
-
- // Try to connect blockC above blockB.
- // This is allowed because we're not splicing into a stack.
- assert.isTrue(
- this.checker.doDragChecks(
- this.blockC.nextConnection,
- this.blockB.previousConnection,
- 9000,
- ),
- 'Should connect above an unconnected unmovable block',
- );
- });
- });
- suite('Rows', function () {
- setup(function () {
- // Load 3 blocks: A and B are connected (input/output); B is unmovable.
- Blockly.Xml.domToWorkspace(
- Blockly.utils.xml
- .textToDom(`
-
-
-
-
-
-
- `),
- this.workspace,
- );
- [this.blockA, this.blockB, this.blockC] =
- this.workspace.getAllBlocks(true);
- this.checker = this.workspace.connectionChecker;
- });
-
- test('Do not splice into unmovable block row', function () {
- // Try to connect C's output to A's input. Should fail because
- // A is already connected to B, which is unmovable.
- const inputConnection = this.blockA.inputList[0].connection;
- assert.isFalse(
- this.checker.doDragChecks(
- this.blockC.outputConnection,
- inputConnection,
- 9000,
- ),
- 'Should not splice in a block before an unmovable block',
- );
- });
-
- test('Connect to end of unmovable block', function () {
- // Make blockC unmovable
- this.blockC.setMovable(false);
- // Try to connect A's output to C's input. This is allowed.
- const inputConnection = this.blockC.inputList[0].connection;
- assert.isTrue(
- this.checker.doDragChecks(
- this.blockA.outputConnection,
- inputConnection,
- 9000,
- ),
- 'Should connect to end of unmovable block',
- );
- });
-
- test('Connect to unconnected unmovable block', function () {
- this.blockB.outputConnection.disconnect();
- this.blockA.dispose();
-
- // Try to connect C's input to B's output. Allowed because B is now unconnected.
- const inputConnection = this.blockC.inputList[0].connection;
- assert.isTrue(
- this.checker.doDragChecks(
- inputConnection,
- this.blockB.outputConnection,
- 9000,
- ),
- 'Should connect to unconnected unmovable block',
- );
- });
- });
- });
-});
diff --git a/packages/blockly/tests/mocha/connection_checker_test.ts b/packages/blockly/tests/mocha/connection_checker_test.ts
new file mode 100644
index 00000000000..0bef09b4fdd
--- /dev/null
+++ b/packages/blockly/tests/mocha/connection_checker_test.ts
@@ -0,0 +1,643 @@
+/**
+ * @license
+ * Copyright 2020 Google LLC
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+import * as Blockly from '#core/blockly.js';
+import {assert} from 'chai';
+import {
+ DEFAULT_INJECT_OPTIONS,
+ sharedTestSetup,
+ sharedTestTeardown,
+ workspaceTeardown,
+} from './test_helpers/setup_teardown.js';
+
+suite('Connection checker', function () {
+ let workspace: Blockly.Workspace;
+ let checker: Blockly.IConnectionChecker;
+
+ setup(function (this: Mocha.Context) {
+ sharedTestSetup.call(this);
+ workspace = new Blockly.Workspace();
+ });
+ teardown(function (this: Mocha.Context) {
+ sharedTestTeardown.call(this, workspace);
+ });
+ suiteSetup(function () {
+ checker = new Blockly.ConnectionChecker();
+ });
+ suite('Safety checks', function () {
+ function assertReasonHelper(
+ checker: Blockly.IConnectionChecker,
+ one: Blockly.Connection | null,
+ two: Blockly.Connection | null,
+ reason: number,
+ ) {
+ assert.equal(checker.canConnectWithReason(one, two, false), reason);
+ // Order should not matter.
+ assert.equal(checker.canConnectWithReason(two, one, false), reason);
+ }
+
+ test('Target Null', function () {
+ const block = workspace.newBlock('logic_negate');
+ block.initModel();
+
+ const inputConnection = block.getInput('BOOL')?.connection;
+ assert.exists(inputConnection);
+
+ assertReasonHelper(
+ checker,
+ inputConnection,
+ null,
+ Blockly.Connection.REASON_TARGET_NULL,
+ );
+ });
+ test('Target Self', function () {
+ const block = workspace.newBlock('logic_negate');
+ block.initModel();
+
+ const inputConnection = block.getInput('BOOL')?.connection;
+ assert.exists(inputConnection);
+
+ assertReasonHelper(
+ checker,
+ inputConnection,
+ block.outputConnection,
+ Blockly.Connection.REASON_SELF_CONNECTION,
+ );
+ });
+ test('Different Workspaces', function () {
+ const workspace2 = new Blockly.Workspace();
+ const block1 = workspace.newBlock('logic_negate');
+ const block2 = workspace2.newBlock('logic_negate');
+ block1.initModel();
+ block2.initModel();
+
+ const connection1 = block1.getInput('BOOL')?.connection;
+ assert.exists(connection1);
+ const connection2 = block2.outputConnection;
+
+ assertReasonHelper(
+ checker,
+ connection1,
+ connection2,
+ Blockly.Connection.REASON_DIFFERENT_WORKSPACES,
+ );
+ });
+ suite('Types', function () {
+ let previous: Blockly.Connection;
+ let next: Blockly.Connection;
+ let output: Blockly.Connection;
+ let input: Blockly.Connection;
+
+ setup(function () {
+ // We have to declare each separately so that the connections belong
+ // on different blocks.
+ const prevBlock = workspace.newBlock('text_print');
+ const nextBlock = workspace.newBlock('text_print');
+ const outBlock = workspace.newBlock('logic_negate');
+ const inBlock = workspace.newBlock('logic_negate');
+
+ prevBlock.initModel();
+ nextBlock.initModel();
+ outBlock.initModel();
+ inBlock.initModel();
+
+ const previousConnection = prevBlock.previousConnection;
+ assert.exists(previousConnection);
+ previous = previousConnection;
+
+ const nextConnection = nextBlock.nextConnection;
+ assert.exists(nextConnection);
+ next = nextConnection;
+
+ const outputConnection = outBlock.outputConnection;
+ assert.exists(outputConnection);
+ output = outputConnection;
+
+ const inputConnection = inBlock.getInput('BOOL')?.connection;
+ assert.exists(inputConnection);
+ input = inputConnection;
+ });
+ test('Previous, Next', function () {
+ assertReasonHelper(
+ checker,
+ previous,
+ next,
+ Blockly.Connection.CAN_CONNECT,
+ );
+ });
+ test('Previous, Output', function () {
+ assertReasonHelper(
+ checker,
+ previous,
+ output,
+ Blockly.Connection.REASON_WRONG_TYPE,
+ );
+ });
+ test('Previous, Input', function () {
+ assertReasonHelper(
+ checker,
+ previous,
+ input,
+ Blockly.Connection.REASON_WRONG_TYPE,
+ );
+ });
+ test('Next, Previous', function () {
+ assertReasonHelper(
+ checker,
+ next,
+ previous,
+ Blockly.Connection.CAN_CONNECT,
+ );
+ });
+ test('Next, Output', function () {
+ assertReasonHelper(
+ checker,
+ next,
+ output,
+ Blockly.Connection.REASON_WRONG_TYPE,
+ );
+ });
+ test('Next, Input', function () {
+ assertReasonHelper(
+ checker,
+ next,
+ input,
+ Blockly.Connection.REASON_WRONG_TYPE,
+ );
+ });
+ test('Output, Previous', function () {
+ assertReasonHelper(
+ checker,
+ previous,
+ output,
+ Blockly.Connection.REASON_WRONG_TYPE,
+ );
+ });
+ test('Output, Next', function () {
+ assertReasonHelper(
+ checker,
+ output,
+ next,
+ Blockly.Connection.REASON_WRONG_TYPE,
+ );
+ });
+ test('Output, Input', function () {
+ assertReasonHelper(
+ checker,
+ output,
+ input,
+ Blockly.Connection.CAN_CONNECT,
+ );
+ });
+ test('Input, Previous', function () {
+ assertReasonHelper(
+ checker,
+ previous,
+ input,
+ Blockly.Connection.REASON_WRONG_TYPE,
+ );
+ });
+ test('Input, Next', function () {
+ assertReasonHelper(
+ checker,
+ input,
+ next,
+ Blockly.Connection.REASON_WRONG_TYPE,
+ );
+ });
+ test('Input, Output', function () {
+ assertReasonHelper(
+ checker,
+ input,
+ output,
+ Blockly.Connection.CAN_CONNECT,
+ );
+ });
+ });
+ suite('Shadows', function () {
+ test('Previous Shadow', function () {
+ const prevBlock = workspace.newBlock('text_print');
+ prevBlock.initModel();
+ prevBlock.setShadow(true);
+ const nextBlock = workspace.newBlock('text_print');
+ nextBlock.initModel();
+
+ const prev = prevBlock.previousConnection;
+ const next = nextBlock.nextConnection;
+
+ assertReasonHelper(checker, prev, next, Blockly.Connection.CAN_CONNECT);
+ });
+ test('Next Shadow', function () {
+ const prevBlock = workspace.newBlock('text_print');
+ prevBlock.initModel();
+ const nextBlock = workspace.newBlock('text_print');
+ nextBlock.initModel();
+ nextBlock.setShadow(true);
+
+ const prev = prevBlock.previousConnection;
+ const next = nextBlock.nextConnection;
+
+ assertReasonHelper(
+ checker,
+ prev,
+ next,
+ Blockly.Connection.REASON_SHADOW_PARENT,
+ );
+ });
+ test('Prev and Next Shadow', function () {
+ const prevBlock = workspace.newBlock('text_print');
+ prevBlock.initModel();
+ prevBlock.setShadow(true);
+ const nextBlock = workspace.newBlock('text_print');
+ nextBlock.initModel();
+ nextBlock.setShadow(true);
+
+ const prev = prevBlock.previousConnection;
+ const next = nextBlock.nextConnection;
+
+ assertReasonHelper(checker, prev, next, Blockly.Connection.CAN_CONNECT);
+ });
+ test('Output Shadow', function () {
+ const outBlock = workspace.newBlock('logic_negate');
+ outBlock.initModel();
+ outBlock.setShadow(true);
+ const inBlock = workspace.newBlock('logic_negate');
+ inBlock.initModel();
+
+ const outCon = outBlock.outputConnection;
+ const inCon = inBlock.getInput('BOOL')?.connection;
+ assert.exists(inCon);
+
+ assertReasonHelper(
+ checker,
+ outCon,
+ inCon,
+ Blockly.Connection.CAN_CONNECT,
+ );
+ });
+ test('Input Shadow', function () {
+ const outBlock = workspace.newBlock('logic_negate');
+ outBlock.initModel();
+ const inBlock = workspace.newBlock('logic_negate');
+ inBlock.initModel();
+ inBlock.setShadow(true);
+
+ const outCon = outBlock.outputConnection;
+ const inCon = inBlock.getInput('BOOL')?.connection;
+ assert.exists(inCon);
+
+ assertReasonHelper(
+ checker,
+ outCon,
+ inCon,
+ Blockly.Connection.REASON_SHADOW_PARENT,
+ );
+ });
+ test('Output and Input Shadow', function () {
+ const outBlock = workspace.newBlock('logic_negate');
+ outBlock.initModel();
+ outBlock.setShadow(true);
+ const inBlock = workspace.newBlock('logic_negate');
+ inBlock.initModel();
+ inBlock.setShadow(true);
+
+ const outCon = outBlock.outputConnection;
+ const inCon = inBlock.getInput('BOOL')?.connection;
+ assert.exists(inCon);
+
+ assertReasonHelper(
+ checker,
+ outCon,
+ inCon,
+ Blockly.Connection.CAN_CONNECT,
+ );
+ });
+ });
+ suite('Output and Previous', function () {
+ /**
+ * Update two connections to target each other.
+ *
+ * @param first The first connection to update.
+ * @param second The second connection to update.
+ */
+ const connectReciprocally = function (
+ first: Blockly.Connection | null,
+ second: Blockly.Connection | null,
+ ) {
+ if (!first || !second) {
+ throw Error('Cannot connect null connections.');
+ }
+ first.targetConnection = second;
+ second.targetConnection = first;
+ };
+ test('Output connected, adding previous', function () {
+ const outBlock = workspace.newBlock('logic_negate');
+ outBlock.initModel();
+ const inBlock = workspace.newBlock('logic_negate');
+ inBlock.initModel();
+
+ const inCon = inBlock.getInput('BOOL')?.connection;
+ assert.exists(inCon);
+ const outCon = outBlock.outputConnection;
+
+ connectReciprocally(inCon, outCon);
+
+ const prevCon = new Blockly.Connection(
+ outBlock,
+ Blockly.ConnectionType.PREVIOUS_STATEMENT,
+ );
+ const nextBlock = workspace.newBlock('text_print');
+ nextBlock.initModel();
+ const nextCon = nextBlock.nextConnection;
+
+ assertReasonHelper(
+ checker,
+ prevCon,
+ nextCon,
+ Blockly.Connection.REASON_PREVIOUS_AND_OUTPUT,
+ );
+ });
+ test('Previous connected, adding output', function () {
+ const prevBlock = workspace.newBlock('text_print');
+ prevBlock.initModel();
+ const nextBlock = workspace.newBlock('text_print');
+ nextBlock.initModel();
+
+ const prevCon = prevBlock.previousConnection;
+ const nextCon = nextBlock.nextConnection;
+ connectReciprocally(prevCon, nextCon);
+
+ const outCon = new Blockly.Connection(
+ prevBlock,
+ Blockly.ConnectionType.OUTPUT_VALUE,
+ );
+ const inBlock = workspace.newBlock('text_length');
+ inBlock.initModel();
+ const inCon = inBlock.getInput('VALUE')?.connection;
+ assert.exists(inCon);
+
+ assertReasonHelper(
+ checker,
+ outCon,
+ inCon,
+ Blockly.Connection.REASON_PREVIOUS_AND_OUTPUT,
+ );
+ });
+ });
+ });
+ suite('Check Types', function () {
+ let con1: Blockly.Connection;
+ let con2: Blockly.Connection;
+
+ setup(function () {
+ const prevBlock = workspace.newBlock('text_print');
+ prevBlock.initModel();
+
+ const nextBlock = workspace.newBlock('text_print');
+ nextBlock.initModel();
+
+ con1 = new Blockly.Connection(
+ prevBlock,
+ Blockly.ConnectionType.PREVIOUS_STATEMENT,
+ );
+ con2 = new Blockly.Connection(
+ nextBlock,
+ Blockly.ConnectionType.NEXT_STATEMENT,
+ );
+ });
+ function assertCheckTypes(
+ checker: Blockly.IConnectionChecker,
+ one: Blockly.Connection,
+ two: Blockly.Connection,
+ ) {
+ assert.isTrue(checker.doTypeChecks(one, two));
+ // Order should not matter.
+ assert.isTrue(checker.doTypeChecks(one, two));
+ }
+ test('No Types', function () {
+ assertCheckTypes(checker, con1, con2);
+ });
+ test('Same Type', function () {
+ con1.setCheck('type1');
+ con2.setCheck('type1');
+ assertCheckTypes(checker, con1, con2);
+ });
+ test('Same Types', function () {
+ con1.setCheck(['type1', 'type2']);
+ con2.setCheck(['type1', 'type2']);
+ assertCheckTypes(checker, con1, con2);
+ });
+ test('Single Same Type', function () {
+ con1.setCheck(['type1', 'type2']);
+ con2.setCheck(['type1', 'type3']);
+ assertCheckTypes(checker, con1, con2);
+ });
+ test('One Typed, One Promiscuous', function () {
+ con1.setCheck('type1');
+ assertCheckTypes(checker, con1, con2);
+ });
+ test('No Compatible Types', function () {
+ con1.setCheck('type1');
+ con2.setCheck('type2');
+ assert.isFalse(checker.doTypeChecks(con1, con2));
+ });
+ });
+ suite('Dragging Checks', function () {
+ let workspace: Blockly.WorkspaceSvg;
+ let blockA: Blockly.BlockSvg;
+ let blockB: Blockly.BlockSvg;
+ let blockC: Blockly.BlockSvg;
+
+ setup(function () {
+ workspace = Blockly.inject('blocklyDiv', DEFAULT_INJECT_OPTIONS);
+ });
+
+ teardown(function (this: Mocha.Context) {
+ workspaceTeardown.call(this, workspace);
+ });
+
+ suite('Stacks', function () {
+ setup(function () {
+ // Load in three blocks: A and B are connected (next/prev); B is unmovable.
+ Blockly.Xml.domToWorkspace(
+ Blockly.utils.xml
+ .textToDom(`
+
+
+
+
+
+
+
+ `),
+ workspace,
+ );
+ const blockA_ = workspace.getBlockById('A');
+ assert.exists(blockA_);
+ blockA = blockA_;
+ const blockB_ = workspace.getBlockById('B');
+ assert.exists(blockB_);
+ blockB = blockB_;
+ const blockC_ = workspace.getBlockById('C');
+ assert.exists(blockC_);
+ blockC = blockC_;
+ checker = workspace.connectionChecker;
+ });
+
+ test('Connect a stack', function () {
+ // block C is not connected to block A; both are movable.
+ const nextConnection = blockC.nextConnection;
+ assert.exists(nextConnection);
+ const previousConnection = blockA.previousConnection;
+ assert.exists(previousConnection);
+ assert.isTrue(
+ checker.doDragChecks(nextConnection, previousConnection, 9000),
+ 'Should connect two compatible stack blocks',
+ );
+ });
+
+ test('Connect to unmovable shadow block', function () {
+ // Remove original test blocks.
+ workspace.clear();
+
+ // Add the same test blocks, but this time block B is a shadow block.
+ Blockly.Xml.domToWorkspace(
+ Blockly.utils.xml
+ .textToDom(`
+
+
+
+
+
+
+
+ `),
+ workspace,
+ );
+ [blockA, blockB, blockC] = workspace.getAllBlocks(true);
+
+ const nextConnection = blockA.nextConnection;
+ assert.exists(nextConnection);
+ const previousConnection = blockC.previousConnection;
+ assert.exists(previousConnection);
+
+ // Try to connect blockC into the input connection of blockA, replacing blockB.
+ // This is allowed because shadow blocks can always be replaced, even though
+ // they are unmovable.
+ assert.isTrue(
+ checker.doDragChecks(previousConnection, nextConnection, 9000),
+ 'Should connect in place of a shadow block',
+ );
+ });
+
+ test('Do not splice into unmovable stack', function () {
+ const nextConnection = blockA.nextConnection;
+ assert.exists(nextConnection);
+ const previousConnection = blockC.previousConnection;
+ assert.exists(previousConnection);
+
+ // Try to connect blockC above blockB. It shouldn't work because B is not movable
+ // and is already connected to A's nextConnection.
+ assert.isFalse(
+ checker.doDragChecks(previousConnection, nextConnection, 9000),
+ 'Should not splice in a block above an unmovable block',
+ );
+ });
+
+ test('Connect to bottom of unmovable stack', function () {
+ const nextConnection = blockB.nextConnection;
+ assert.exists(nextConnection);
+ const previousConnection = blockC.previousConnection;
+ assert.exists(previousConnection);
+
+ // Try to connect blockC below blockB.
+ // This is allowed even though B is not movable because it is on B's nextConnection.
+ assert.isTrue(
+ checker.doDragChecks(previousConnection, nextConnection, 9000),
+ 'Should connect below an unmovable stack block',
+ );
+ });
+
+ test('Connect to unconnected unmovable block', function () {
+ blockB.previousConnection?.disconnect();
+ blockA.dispose();
+
+ const nextConnection = blockC.nextConnection;
+ assert.exists(nextConnection);
+ const previousConnection = blockB.previousConnection;
+ assert.exists(previousConnection);
+
+ // Try to connect blockC above blockB.
+ // This is allowed because we're not splicing into a stack.
+ assert.isTrue(
+ checker.doDragChecks(nextConnection, previousConnection, 9000),
+ 'Should connect above an unconnected unmovable block',
+ );
+ });
+ });
+ suite('Rows', function () {
+ setup(function () {
+ // Load 3 blocks: A and B are connected (input/output); B is unmovable.
+ Blockly.Xml.domToWorkspace(
+ Blockly.utils.xml
+ .textToDom(`
+
+
+
+
+
+
+ `),
+ workspace,
+ );
+ [blockA, blockB, blockC] = workspace.getAllBlocks(true);
+ checker = workspace.connectionChecker;
+ });
+
+ test('Do not splice into unmovable block row', function () {
+ // Try to connect C's output to A's input. Should fail because
+ // A is already connected to B, which is unmovable.
+ const inputConnection = blockA.inputList[0].connection;
+ assert.instanceOf(inputConnection, Blockly.RenderedConnection);
+ const outputConnection = blockC.outputConnection;
+ assert.exists(outputConnection);
+ assert.isFalse(
+ checker.doDragChecks(outputConnection, inputConnection, 9000),
+ 'Should not splice in a block before an unmovable block',
+ );
+ });
+
+ test('Connect to end of unmovable block', function () {
+ // Make blockC unmovable
+ blockC.setMovable(false);
+ // Try to connect A's output to C's input. This is allowed.
+ const inputConnection = blockC.inputList[0].connection;
+ assert.instanceOf(inputConnection, Blockly.RenderedConnection);
+ const outputConnection = blockA.outputConnection;
+ assert.exists(outputConnection);
+ assert.isTrue(
+ checker.doDragChecks(outputConnection, inputConnection, 9000),
+ 'Should connect to end of unmovable block',
+ );
+ });
+
+ test('Connect to unconnected unmovable block', function () {
+ blockB.outputConnection?.disconnect();
+ blockA.dispose();
+
+ // Try to connect C's input to B's output. Allowed because B is now unconnected.
+ const inputConnection = blockC.inputList[0].connection;
+ assert.instanceOf(inputConnection, Blockly.RenderedConnection);
+ const outputConnection = blockB.outputConnection;
+ assert.exists(outputConnection);
+ assert.isTrue(
+ checker.doDragChecks(inputConnection, outputConnection, 9000),
+ 'Should connect to unconnected unmovable block',
+ );
+ });
+ });
+ });
+});
diff --git a/packages/blockly/tests/mocha/connection_test.js b/packages/blockly/tests/mocha/connection_test.js
index 6d749dd21c7..4c5ed25df4f 100644
--- a/packages/blockly/tests/mocha/connection_test.js
+++ b/packages/blockly/tests/mocha/connection_test.js
@@ -9,7 +9,7 @@ import {
defineRowBlock,
defineStackBlock,
defineStatementBlock,
-} from './test_helpers/block_definitions.js';
+} from './test_helpers/block_definitions.ts';
import {
createGenUidStubWithReturns,
DEFAULT_INJECT_OPTIONS,
diff --git a/packages/blockly/tests/mocha/contextmenu_test.js b/packages/blockly/tests/mocha/contextmenu_test.js
index 6dd7c359b15..ff369a3ce88 100644
--- a/packages/blockly/tests/mocha/contextmenu_test.js
+++ b/packages/blockly/tests/mocha/contextmenu_test.js
@@ -10,7 +10,7 @@ import {assert} from 'chai';
import {
defineRowBlock,
defineStackBlock,
-} from './test_helpers/block_definitions.js';
+} from './test_helpers/block_definitions.ts';
import {
DEFAULT_INJECT_OPTIONS,
sharedTestSetup,
diff --git a/packages/blockly/tests/mocha/dialog_test.js b/packages/blockly/tests/mocha/dialog_test.ts
similarity index 78%
rename from packages/blockly/tests/mocha/dialog_test.js
rename to packages/blockly/tests/mocha/dialog_test.ts
index 3bc8dc5d7b8..63a55296598 100644
--- a/packages/blockly/tests/mocha/dialog_test.js
+++ b/packages/blockly/tests/mocha/dialog_test.ts
@@ -4,7 +4,9 @@
* SPDX-License-Identifier: Apache-2.0
*/
+import * as Blockly from '#core/blockly.js';
import {assert} from 'chai';
+import sinon from 'sinon';
import {
DEFAULT_INJECT_OPTIONS,
sharedTestSetup,
@@ -12,13 +14,15 @@ import {
} from './test_helpers/setup_teardown.js';
suite('Dialog utilities', function () {
- setup(function () {
+ let workspace: Blockly.WorkspaceSvg;
+
+ setup(function (this: Mocha.Context) {
sharedTestSetup.call(this);
- this.workspace = Blockly.inject('blocklyDiv', DEFAULT_INJECT_OPTIONS);
+ workspace = Blockly.inject('blocklyDiv', DEFAULT_INJECT_OPTIONS);
});
- teardown(function () {
- sharedTestTeardown.call(this);
+ teardown(function (this: Mocha.Context) {
+ sharedTestTeardown.call(this, workspace);
Blockly.dialog.setAlert();
Blockly.dialog.setPrompt();
Blockly.dialog.setConfirm();
@@ -32,8 +36,9 @@ suite('Dialog utilities', function () {
const message = 'test';
Blockly.dialog.alert(message, callback);
const dialog = document.querySelector('dialog');
+ assert.isNotNull(dialog);
assert.include(dialog.textContent, 'test');
- dialog.querySelector('.blocklyDialogConfirmButton').click();
+ dialog.querySelector('.blocklyDialogConfirmButton')?.click();
});
test('support setting a custom alert handler', function () {
@@ -48,7 +53,7 @@ suite('Dialog utilities', function () {
test('do not call the built in alert if a custom alert handler is set', function () {
const alert = sinon.spy();
Blockly.dialog.setAlert(alert);
- Blockly.dialog.alert(test);
+ Blockly.dialog.alert('test');
const dialog = document.querySelector('dialog');
assert.isNull(dialog);
});
@@ -60,8 +65,9 @@ suite('Dialog utilities', function () {
const message = 'test';
Blockly.dialog.confirm(message, callback);
const dialog = document.querySelector('dialog');
+ assert.isNotNull(dialog);
assert.include(dialog.textContent, 'test');
- dialog.querySelector('.blocklyDialogCancelButton').click();
+ dialog.querySelector('.blocklyDialogCancelButton')?.click();
});
test('support setting a custom confirm handler', function () {
@@ -84,14 +90,14 @@ suite('Dialog utilities', function () {
});
test('invokes the provided callback with the confirmation response', function (done) {
- const callback = (result) => {
+ const callback = (result: boolean) => {
assert.isTrue(result);
done();
};
const message = 'test';
Blockly.dialog.confirm(message, callback);
const dialog = document.querySelector('dialog');
- dialog.querySelector('.blocklyDialogConfirmButton').click();
+ dialog?.querySelector('.blocklyDialogConfirmButton')?.click();
});
test('use the built in prompt by default', function (done) {
@@ -102,10 +108,11 @@ suite('Dialog utilities', function () {
const defaultValue = 'default';
Blockly.dialog.prompt(message, defaultValue, callback);
const dialog = document.querySelector('dialog');
+ assert.isNotNull(dialog);
assert.include(dialog.textContent, 'test');
const input = dialog.querySelector('input');
- assert.equal(input.value, 'default');
- dialog.querySelector('.blocklyDialogCancelButton').click();
+ assert.equal(input?.value, 'default');
+ dialog.querySelector('.blocklyDialogCancelButton')?.click();
});
test('support setting a custom prompt handler', function () {
@@ -130,7 +137,7 @@ suite('Dialog utilities', function () {
});
test('invokes the provided callback with the prompt response', function (done) {
- const callback = (response) => {
+ const callback = (response: string | null) => {
assert.equal(response, 'something');
done();
};
@@ -138,18 +145,18 @@ suite('Dialog utilities', function () {
const defaultValue = 'default';
Blockly.dialog.prompt(message, defaultValue, callback);
const dialog = document.querySelector('dialog');
+ assert.isNotNull(dialog);
assert.include(dialog.textContent, 'test');
const input = dialog.querySelector('input');
+ assert.isNotNull(input);
input.value = 'something';
- dialog.querySelector('.blocklyDialogConfirmButton').click();
+ dialog.querySelector('.blocklyDialogConfirmButton')?.click();
});
test('use the built-in toast by default', function () {
const message = 'test toast';
- Blockly.dialog.toast(this.workspace, {message});
- const toast = this.workspace
- .getInjectionDiv()
- .querySelector('.blocklyToast');
+ Blockly.dialog.toast(workspace, {message});
+ const toast = workspace.getInjectionDiv().querySelector('.blocklyToast');
assert.isNotNull(toast);
assert.equal(toast.textContent, message);
});
@@ -159,8 +166,8 @@ suite('Dialog utilities', function () {
Blockly.dialog.setToast(toast);
const message = 'test toast';
const options = {message};
- Blockly.dialog.toast(this.workspace, options);
- assert.isTrue(toast.calledWith(this.workspace, options));
+ Blockly.dialog.toast(workspace, options);
+ assert.isTrue(toast.calledWith(workspace, options));
});
test('do not use the built-in toast if a custom toast handler is set', function () {
@@ -169,7 +176,7 @@ suite('Dialog utilities', function () {
const toast = sinon.spy();
Blockly.dialog.setToast(toast);
const message = 'test toast';
- Blockly.dialog.toast(this.workspace, {message});
+ Blockly.dialog.toast(workspace, {message});
assert.isFalse(builtInToast.called);
builtInToast.restore();
diff --git a/packages/blockly/tests/mocha/event_block_change_test.ts b/packages/blockly/tests/mocha/event_block_change_test.ts
index 6923fc98187..1376905551f 100644
--- a/packages/blockly/tests/mocha/event_block_change_test.ts
+++ b/packages/blockly/tests/mocha/event_block_change_test.ts
@@ -6,7 +6,7 @@
import * as Blockly from '#core/blockly.js';
import {assert} from 'chai';
-import {defineMutatorBlocks} from './test_helpers/block_definitions.js';
+import {defineMutatorBlocks} from './test_helpers/block_definitions.ts';
import {
sharedTestSetup,
sharedTestTeardown,
diff --git a/packages/blockly/tests/mocha/event_block_create_test.ts b/packages/blockly/tests/mocha/event_block_create_test.ts
index 7e0520791ea..a2dc0b3bc1c 100644
--- a/packages/blockly/tests/mocha/event_block_create_test.ts
+++ b/packages/blockly/tests/mocha/event_block_create_test.ts
@@ -7,7 +7,7 @@
import * as Blockly from '#core/blockly.js';
import {assert} from 'chai';
import type sinon from 'sinon';
-import {defineRowBlock} from './test_helpers/block_definitions.js';
+import {defineRowBlock} from './test_helpers/block_definitions.ts';
import {assertEventFired} from './test_helpers/events.js';
import {
sharedTestSetup,
diff --git a/packages/blockly/tests/mocha/event_block_delete_test.ts b/packages/blockly/tests/mocha/event_block_delete_test.ts
index e800f35112b..03572d5e4a3 100644
--- a/packages/blockly/tests/mocha/event_block_delete_test.ts
+++ b/packages/blockly/tests/mocha/event_block_delete_test.ts
@@ -7,7 +7,7 @@
import * as Blockly from '#core/blockly.js';
import {assert} from 'chai';
import sinon from 'sinon';
-import {defineRowBlock} from './test_helpers/block_definitions.js';
+import {defineRowBlock} from './test_helpers/block_definitions.ts';
import {
sharedTestSetup,
sharedTestTeardown,
diff --git a/packages/blockly/tests/mocha/event_block_drag_test.ts b/packages/blockly/tests/mocha/event_block_drag_test.ts
index 54c424b5393..f51906b27b1 100644
--- a/packages/blockly/tests/mocha/event_block_drag_test.ts
+++ b/packages/blockly/tests/mocha/event_block_drag_test.ts
@@ -6,7 +6,7 @@
import * as Blockly from '#core/blockly.js';
import {assert} from 'chai';
-import {defineRowBlock} from './test_helpers/block_definitions.js';
+import {defineRowBlock} from './test_helpers/block_definitions.ts';
import {
sharedTestSetup,
sharedTestTeardown,
diff --git a/packages/blockly/tests/mocha/event_block_move_test.ts b/packages/blockly/tests/mocha/event_block_move_test.ts
index b6f1c09ea6a..5c87c6ebe7d 100644
--- a/packages/blockly/tests/mocha/event_block_move_test.ts
+++ b/packages/blockly/tests/mocha/event_block_move_test.ts
@@ -6,7 +6,7 @@
import * as Blockly from '#core/blockly.js';
import {assert} from 'chai';
-import {defineRowBlock} from './test_helpers/block_definitions.js';
+import {defineRowBlock} from './test_helpers/block_definitions.ts';
import {
sharedTestSetup,
sharedTestTeardown,
diff --git a/packages/blockly/tests/mocha/event_bubble_open_test.ts b/packages/blockly/tests/mocha/event_bubble_open_test.ts
index 59b6f8b3ad4..290c4559c3d 100644
--- a/packages/blockly/tests/mocha/event_bubble_open_test.ts
+++ b/packages/blockly/tests/mocha/event_bubble_open_test.ts
@@ -6,7 +6,7 @@
import * as Blockly from '#core/blockly.js';
import {assert} from 'chai';
-import {defineMutatorBlocks} from './test_helpers/block_definitions.js';
+import {defineMutatorBlocks} from './test_helpers/block_definitions.ts';
import {
DEFAULT_INJECT_OPTIONS,
sharedTestSetup,
diff --git a/packages/blockly/tests/mocha/event_click_test.ts b/packages/blockly/tests/mocha/event_click_test.ts
index facfa284eda..26e16f7dcf7 100644
--- a/packages/blockly/tests/mocha/event_click_test.ts
+++ b/packages/blockly/tests/mocha/event_click_test.ts
@@ -6,7 +6,7 @@
import * as Blockly from '#core/blockly.js';
import {assert} from 'chai';
-import {defineRowBlock} from './test_helpers/block_definitions.js';
+import {defineRowBlock} from './test_helpers/block_definitions.ts';
import {
sharedTestSetup,
sharedTestTeardown,
diff --git a/packages/blockly/tests/mocha/event_selected_test.ts b/packages/blockly/tests/mocha/event_selected_test.ts
index 2d7f8133a0b..6327a175078 100644
--- a/packages/blockly/tests/mocha/event_selected_test.ts
+++ b/packages/blockly/tests/mocha/event_selected_test.ts
@@ -7,7 +7,7 @@
import * as Blockly from '#core/blockly.js';
import {assert} from 'chai';
import type sinon from 'sinon';
-import {defineRowBlock} from './test_helpers/block_definitions.js';
+import {defineRowBlock} from './test_helpers/block_definitions.ts';
import {createChangeListenerSpy} from './test_helpers/events.js';
import {
DEFAULT_INJECT_OPTIONS,
diff --git a/packages/blockly/tests/mocha/field_checkbox_test.js b/packages/blockly/tests/mocha/field_checkbox_test.js
index 781c10a0c93..f422eb468f7 100644
--- a/packages/blockly/tests/mocha/field_checkbox_test.js
+++ b/packages/blockly/tests/mocha/field_checkbox_test.js
@@ -6,7 +6,7 @@
import * as Blockly from '#core/blockly.js';
import {assert} from 'chai';
-import {defineRowBlock} from './test_helpers/block_definitions.js';
+import {defineRowBlock} from './test_helpers/block_definitions.ts';
import {
assertFieldValue,
runConstructorSuiteTests,
diff --git a/packages/blockly/tests/mocha/field_dropdown_test.js b/packages/blockly/tests/mocha/field_dropdown_test.js
index 3cc3fe19d50..f3938fdff02 100644
--- a/packages/blockly/tests/mocha/field_dropdown_test.js
+++ b/packages/blockly/tests/mocha/field_dropdown_test.js
@@ -9,7 +9,7 @@ import {assert} from 'chai';
import {
createTestBlock,
defineRowBlock,
-} from './test_helpers/block_definitions.js';
+} from './test_helpers/block_definitions.ts';
import {
assertFieldValue,
runConstructorSuiteTests,
diff --git a/packages/blockly/tests/mocha/field_label_serializable_test.js b/packages/blockly/tests/mocha/field_label_serializable_test.js
index 3cb6a622be4..71acfa35ff3 100644
--- a/packages/blockly/tests/mocha/field_label_serializable_test.js
+++ b/packages/blockly/tests/mocha/field_label_serializable_test.js
@@ -9,7 +9,7 @@ import {assert} from 'chai';
import {
createTestBlock,
defineRowBlock,
-} from './test_helpers/block_definitions.js';
+} from './test_helpers/block_definitions.ts';
import {
assertFieldValue,
runConstructorSuiteTests,
diff --git a/packages/blockly/tests/mocha/field_label_test.js b/packages/blockly/tests/mocha/field_label_test.js
index 7ff62490413..29d026126ad 100644
--- a/packages/blockly/tests/mocha/field_label_test.js
+++ b/packages/blockly/tests/mocha/field_label_test.js
@@ -6,7 +6,7 @@
import * as Blockly from '#core/blockly.js';
import {assert} from 'chai';
-import {createTestBlock} from './test_helpers/block_definitions.js';
+import {createTestBlock} from './test_helpers/block_definitions.ts';
import {
assertFieldValue,
runConstructorSuiteTests,
diff --git a/packages/blockly/tests/mocha/field_number_test.js b/packages/blockly/tests/mocha/field_number_test.js
index 70275ff7519..8b273c3d5f7 100644
--- a/packages/blockly/tests/mocha/field_number_test.js
+++ b/packages/blockly/tests/mocha/field_number_test.js
@@ -6,7 +6,7 @@
import * as Blockly from '#core/blockly.js';
import {assert} from 'chai';
-import {defineRowBlock} from './test_helpers/block_definitions.js';
+import {defineRowBlock} from './test_helpers/block_definitions.ts';
import {runTestCases} from './test_helpers/common.js';
import {
assertFieldValue,
diff --git a/packages/blockly/tests/mocha/field_textinput_test.js b/packages/blockly/tests/mocha/field_textinput_test.js
index 612d5b0d913..ae645d4c3be 100644
--- a/packages/blockly/tests/mocha/field_textinput_test.js
+++ b/packages/blockly/tests/mocha/field_textinput_test.js
@@ -9,7 +9,7 @@ import {assert} from 'chai';
import {
createTestBlock,
defineRowBlock,
-} from './test_helpers/block_definitions.js';
+} from './test_helpers/block_definitions.ts';
import {
assertFieldValue,
runConstructorSuiteTests,
diff --git a/packages/blockly/tests/mocha/field_variable_test.js b/packages/blockly/tests/mocha/field_variable_test.js
index 31009de1cfc..bfe60766726 100644
--- a/packages/blockly/tests/mocha/field_variable_test.js
+++ b/packages/blockly/tests/mocha/field_variable_test.js
@@ -9,7 +9,7 @@ import {assert} from 'chai';
import {
createTestBlock,
defineRowBlock,
-} from './test_helpers/block_definitions.js';
+} from './test_helpers/block_definitions.ts';
import {
assertFieldValue,
runConstructorSuiteTests,
diff --git a/packages/blockly/tests/mocha/flyout_test.js b/packages/blockly/tests/mocha/flyout_test.js
index 977f0ad6820..d9f9e75e29d 100644
--- a/packages/blockly/tests/mocha/flyout_test.js
+++ b/packages/blockly/tests/mocha/flyout_test.js
@@ -15,7 +15,7 @@ import {
getProperSimpleJson,
getSimpleJson,
getXmlArray,
-} from './test_helpers/toolbox_definitions.js';
+} from './test_helpers/toolbox_definitions.ts';
suite('Flyout', function () {
setup(function () {
diff --git a/packages/blockly/tests/mocha/focus_manager_test.js b/packages/blockly/tests/mocha/focus_manager_test.js
deleted file mode 100644
index 032e7c35d65..00000000000
--- a/packages/blockly/tests/mocha/focus_manager_test.js
+++ /dev/null
@@ -1,5819 +0,0 @@
-/**
- * @license
- * Copyright 2025 Google LLC
- * SPDX-License-Identifier: Apache-2.0
- */
-
-import {FocusManager, getFocusManager} from '#core/focus_manager.js';
-import {assert} from 'chai';
-import {FocusableTreeImpl} from './test_helpers/focusable_tree.js';
-import {
- sharedTestSetup,
- sharedTestTeardown,
-} from './test_helpers/setup_teardown.js';
-
-suite('FocusManager', function () {
- const ACTIVE_FOCUS_NODE_CSS_SELECTOR = `.${FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME}`;
- const PASSIVE_FOCUS_NODE_CSS_SELECTOR = `.${FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME}`;
-
- setup(function () {
- sharedTestSetup.call(this);
- this.focusManager = getFocusManager();
-
- this.allFocusableTrees = [];
- this.allFocusableNodes = [];
- this.createFocusableTree = function (rootElementId, nestedTrees) {
- const tree = new FocusableTreeImpl(
- document.getElementById(rootElementId),
- nestedTrees || [],
- );
- this.allFocusableTrees.push(tree);
- return tree;
- };
- this.createFocusableNode = function (tree, elementId) {
- const node = tree.addNode(document.getElementById(elementId));
- this.allFocusableNodes.push(node);
- return node;
- };
-
- this.testFocusableTree1 = this.createFocusableTree('testFocusableTree1');
- this.testFocusableTree1Node1 = this.createFocusableNode(
- this.testFocusableTree1,
- 'testFocusableTree1.node1',
- );
- this.testFocusableTree1Node1Child1 = this.createFocusableNode(
- this.testFocusableTree1,
- 'testFocusableTree1.node1.child1',
- );
- this.testFocusableTree1Node2 = this.createFocusableNode(
- this.testFocusableTree1,
- 'testFocusableTree1.node2',
- );
- this.testFocusableNestedTree4 = this.createFocusableTree(
- 'testFocusableNestedTree4',
- );
- this.testFocusableNestedTree4Node1 = this.createFocusableNode(
- this.testFocusableNestedTree4,
- 'testFocusableNestedTree4.node1',
- );
- this.testFocusableNestedTree5 = this.createFocusableTree(
- 'testFocusableNestedTree5',
- );
- this.testFocusableNestedTree5Node1 = this.createFocusableNode(
- this.testFocusableNestedTree5,
- 'testFocusableNestedTree5.node1',
- );
- this.testFocusableTree2 = this.createFocusableTree('testFocusableTree2', [
- this.testFocusableNestedTree4,
- this.testFocusableNestedTree5,
- ]);
- this.testFocusableTree2Node1 = this.createFocusableNode(
- this.testFocusableTree2,
- 'testFocusableTree2.node1',
- );
-
- this.testFocusableGroup1 = this.createFocusableTree('testFocusableGroup1');
- this.testFocusableGroup1Node1 = this.createFocusableNode(
- this.testFocusableGroup1,
- 'testFocusableGroup1.node1',
- );
- this.testFocusableGroup1Node1Child1 = this.createFocusableNode(
- this.testFocusableGroup1,
- 'testFocusableGroup1.node1.child1',
- );
- this.testFocusableGroup1Node2 = this.createFocusableNode(
- this.testFocusableGroup1,
- 'testFocusableGroup1.node2',
- );
- this.testFocusableNestedGroup4 = this.createFocusableTree(
- 'testFocusableNestedGroup4',
- );
- this.testFocusableNestedGroup4Node1 = this.createFocusableNode(
- this.testFocusableNestedGroup4,
- 'testFocusableNestedGroup4.node1',
- );
- this.testFocusableGroup2 = this.createFocusableTree('testFocusableGroup2', [
- this.testFocusableNestedGroup4,
- ]);
- this.testFocusableGroup2Node1 = this.createFocusableNode(
- this.testFocusableGroup2,
- 'testFocusableGroup2.node1',
- );
- });
-
- teardown(function () {
- sharedTestTeardown.call(this);
-
- // Ensure all node CSS styles are reset so that state isn't leaked between tests.
- const activeElems = document.querySelectorAll(
- ACTIVE_FOCUS_NODE_CSS_SELECTOR,
- );
- const passiveElems = document.querySelectorAll(
- PASSIVE_FOCUS_NODE_CSS_SELECTOR,
- );
- for (const elem of activeElems) {
- elem.classList.remove(FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME);
- }
- for (const elem of passiveElems) {
- elem.classList.remove(FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME);
- }
-
- // Ensure any set tab indexes are properly reset between tests.
- for (const tree of this.allFocusableTrees) {
- tree
- .getRootFocusableNode()
- .getFocusableElement()
- .removeAttribute('tabindex');
- }
- for (const node of this.allFocusableNodes) {
- node.getFocusableElement().removeAttribute('tabindex');
- }
- this.allFocusableTrees = [];
- this.allFocusableNodes = [];
-
- // Reset the current active element.
- document.body.focus();
- });
-
- assert.includesClass = function (classList, className) {
- assert.isTrue(
- classList.contains(className),
- 'Expected class list to include: ' + className,
- );
- };
-
- assert.notIncludesClass = function (classList, className) {
- assert.isFalse(
- classList.contains(className),
- 'Expected class list to not include: ' + className,
- );
- };
-
- /* Basic lifecycle tests. */
-
- suite('registerTree()', function () {
- test('once does not throw', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
-
- // The test should pass due to no exception being thrown.
- });
-
- test('twice for same tree throws error', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
-
- const errorMsgRegex =
- /Attempted to re-register already registered tree.+?/;
- assert.throws(
- () => this.focusManager.registerTree(this.testFocusableTree1),
- errorMsgRegex,
- );
- });
-
- test('twice with different trees does not throw', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableGroup1);
-
- // The test shouldn't throw since two different trees were registered.
- });
-
- test('register after an unregister does not throw', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- this.focusManager.registerTree(this.testFocusableTree1);
-
- // The second register should not fail since the tree was previously unregistered.
- });
-
- test('for tree with missing ID throws error', function () {
- const rootNode = this.testFocusableTree1.getRootFocusableNode();
- const rootElem = rootNode.getFocusableElement();
- const oldId = rootElem.id;
- rootElem.removeAttribute('id');
-
- const errorMsgRegex =
- /Attempting to register a tree with a root element that has an invalid ID.+?/;
- assert.throws(
- () => this.focusManager.registerTree(this.testFocusableTree1),
- errorMsgRegex,
- );
- // Restore the ID for other tests.
- rootElem.id = oldId;
- });
-
- test('for tree with null ID throws error', function () {
- const rootNode = this.testFocusableTree1.getRootFocusableNode();
- const rootElem = rootNode.getFocusableElement();
- const oldId = rootElem.id;
- rootElem.setAttribute('id', null);
-
- const errorMsgRegex =
- /Attempting to register a tree with a root element that has an invalid ID.+?/;
- assert.throws(
- () => this.focusManager.registerTree(this.testFocusableTree1),
- errorMsgRegex,
- );
- // Restore the ID for other tests.
- rootElem.id = oldId;
- });
-
- test('for tree with empty throws error', function () {
- const rootNode = this.testFocusableTree1.getRootFocusableNode();
- const rootElem = rootNode.getFocusableElement();
- const oldId = rootElem.id;
- rootElem.setAttribute('id', '');
-
- const errorMsgRegex =
- /Attempting to register a tree with a root element that has an invalid ID.+?/;
- assert.throws(
- () => this.focusManager.registerTree(this.testFocusableTree1),
- errorMsgRegex,
- );
- // Restore the ID for other tests.
- rootElem.id = oldId;
- });
-
- test('for unmanaged tree does not overwrite tab index', function () {
- this.focusManager.registerTree(this.testFocusableTree1, false);
-
- const rootNode = this.testFocusableTree1.getRootFocusableNode();
- const rootElem = rootNode.getFocusableElement();
- assert.isNull(rootElem.getAttribute('tabindex'));
- });
-
- test('for unmanaged tree with custom tab index does not overwrite tab index', function () {
- const rootNode = this.testFocusableTree1.getRootFocusableNode();
- const rootElem = rootNode.getFocusableElement();
- rootElem.tabIndex = -1;
-
- this.focusManager.registerTree(this.testFocusableTree1, false);
-
- // The custom tab index shouldn't be overwritten for an unmanaged tree.
- assert.strictEqual(rootElem.getAttribute('tabindex'), '-1');
- });
-
- test('for managed tree overwrites root tab index to be tab navigable', function () {
- this.focusManager.registerTree(this.testFocusableTree1, true);
-
- const rootNode = this.testFocusableTree1.getRootFocusableNode();
- const rootElem = rootNode.getFocusableElement();
- assert.strictEqual(rootElem.getAttribute('tabindex'), '0');
- });
-
- test('for managed tree with custom tab index overwrites root tab index to be tab navigable', function () {
- const rootNode = this.testFocusableTree1.getRootFocusableNode();
- const rootElem = rootNode.getFocusableElement();
- rootElem.tabIndex = -1;
-
- this.focusManager.registerTree(this.testFocusableTree1, true);
-
- // A custom tab index should be overwritten for a managed tree.
- assert.strictEqual(rootElem.getAttribute('tabindex'), '0');
- });
- });
-
- suite('unregisterTree()', function () {
- test('for not yet registered tree throws', function () {
- const errorMsgRegex = /Attempted to unregister not registered tree.+?/;
- assert.throws(
- () => this.focusManager.unregisterTree(this.testFocusableTree1),
- errorMsgRegex,
- );
- });
-
- test('for registered tree does not throw', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- // Unregistering a registered tree should not fail.
- });
-
- test('twice for registered tree throws', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- const errorMsgRegex = /Attempted to unregister not registered tree.+?/;
- assert.throws(
- () => this.focusManager.unregisterTree(this.testFocusableTree1),
- errorMsgRegex,
- );
- });
-
- test('for unmanaged tree with custom tab index does not change tab index', function () {
- this.focusManager.registerTree(this.testFocusableTree1, false);
- const rootNode = this.testFocusableTree1.getRootFocusableNode();
- const rootElem = rootNode.getFocusableElement();
- rootElem.tabIndex = -1;
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- // Unregistering an unmanaged tree shouldn't change its tab index.
- assert.strictEqual(rootElem.getAttribute('tabindex'), '-1');
- });
-
- test('for managed tree removes tab index', function () {
- this.focusManager.registerTree(this.testFocusableTree1, true);
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- // Unregistering a managed tree should remove its tab index.
- const rootNode = this.testFocusableTree1.getRootFocusableNode();
- const rootElem = rootNode.getFocusableElement();
- assert.isNull(rootElem.getAttribute('tabindex'));
- });
-
- test('for managed tree with custom tab index removes tab index', function () {
- this.focusManager.registerTree(this.testFocusableTree1, true);
- const rootNode = this.testFocusableTree1.getRootFocusableNode();
- const rootElem = rootNode.getFocusableElement();
- rootElem.tabIndex = -1;
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- // Unregistering a managed tree should remove its tab index.
- assert.isNull(rootElem.getAttribute('tabindex'));
- });
- });
-
- suite('isRegistered()', function () {
- test('for not registered tree returns false', function () {
- const isRegistered = this.focusManager.isRegistered(
- this.testFocusableTree1,
- );
-
- assert.isFalse(isRegistered);
- });
-
- test('for registered tree returns true', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
-
- const isRegistered = this.focusManager.isRegistered(
- this.testFocusableTree1,
- );
-
- assert.isTrue(isRegistered);
- });
-
- test('for unregistered tree returns false', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- const isRegistered = this.focusManager.isRegistered(
- this.testFocusableTree1,
- );
-
- assert.isFalse(isRegistered);
- });
-
- test('for re-registered tree returns true', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.unregisterTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree1);
-
- const isRegistered = this.focusManager.isRegistered(
- this.testFocusableTree1,
- );
-
- assert.isTrue(isRegistered);
- });
-
- test('for unregistered tree with other registered tree returns false', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- const isRegistered = this.focusManager.isRegistered(
- this.testFocusableTree1,
- );
-
- assert.isFalse(isRegistered);
- });
- });
-
- suite('getFocusedTree()', function () {
- test('by default returns null', function () {
- const focusedTree = this.focusManager.getFocusedTree();
-
- assert.isNull(focusedTree);
- });
- });
-
- suite('getFocusedNode()', function () {
- test('by default returns null', function () {
- const focusedNode = this.focusManager.getFocusedNode();
-
- assert.isNull(focusedNode);
- });
-
- test('after focusing unfocusable node returns null', function () {
- this.testFocusableTree1Node1.canBeFocused = () => false;
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- const focusedNode = this.focusManager.getFocusedNode();
-
- // Unfocusable nodes should not be focused.
- assert.isNull(focusedNode);
- });
- });
-
- suite('getPreviouslyFocusedNode()', function () {
- test('by default returns null', function () {
- assert.isNull(this.focusManager.getPreviouslyFocusedNode());
- });
-
- test('after losing focus to untracked element returns prior node', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- document.body.focus();
-
- assert.isNull(this.focusManager.getFocusedNode());
- assert.strictEqual(
- this.focusManager.getPreviouslyFocusedNode(),
- this.testFocusableTree1Node1,
- );
- });
- });
-
- suite('focusTree()', function () {
- test('for not registered tree throws', function () {
- const errorMsgRegex = /Attempted to focus unregistered tree.+?/;
- assert.throws(
- () => this.focusManager.focusTree(this.testFocusableTree1),
- errorMsgRegex,
- );
- });
-
- test('for unregistered tree throws', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- const errorMsgRegex = /Attempted to focus unregistered tree.+?/;
- assert.throws(
- () => this.focusManager.focusTree(this.testFocusableTree1),
- errorMsgRegex,
- );
- });
- });
-
- test('unfocused node does not have a tab index by default', function () {
- const elem = this.testFocusableTree1Node1.getFocusableElement();
-
- // This is slightly testing the test setup, but it acts as a precondition sanity test for the
- // other tab index tests below. Important: 'getAttribute' is used here since direct access to
- // 'tabIndex' can default the value returned even when the tab index isn't set.
- assert.isNull(elem.getAttribute('tabindex'));
- });
-
- suite('focusNode()', function () {
- test('for not registered node throws', function () {
- const errorMsgRegex = /Attempted to focus unregistered node.+?/;
- assert.throws(
- () => this.focusManager.focusNode(this.testFocusableTree1Node1),
- errorMsgRegex,
- );
- });
-
- test('for unregistered node throws', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- const errorMsgRegex = /Attempted to focus unregistered node.+?/;
- assert.throws(
- () => this.focusManager.focusNode(this.testFocusableTree1Node1),
- errorMsgRegex,
- );
- });
-
- test('focuses element', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
-
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- const nodeElem = this.testFocusableTree1Node1.getFocusableElement();
- assert.strictEqual(document.activeElement, nodeElem);
- });
-
- test('fires focusin event', function () {
- let focusCount = 0;
- const focusListener = () => focusCount++;
- document.addEventListener('focusin', focusListener);
- this.focusManager.registerTree(this.testFocusableTree1);
-
- this.focusManager.focusNode(this.testFocusableTree1Node1);
- document.removeEventListener('focusin', focusListener);
-
- // There should be exactly 1 focus event fired from focusNode().
- assert.strictEqual(focusCount, 1);
- });
-
- test('for orphaned node returns tree root by default', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.testFocusableTree1.removeNode(this.testFocusableTree1Node1);
-
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- // Focusing an invalid node should fall back to the tree root when it has no restoration
- // fallback node.
- const currentNode = this.focusManager.getFocusedNode();
- const treeRoot = this.testFocusableTree1.getRootFocusableNode();
- assert.strictEqual(currentNode, treeRoot);
- });
-
- test('for orphaned node returns specified fallback node', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.testFocusableTree1.fallbackNode = this.testFocusableTree1Node2;
- this.testFocusableTree1.removeNode(this.testFocusableTree1Node1);
-
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- // Focusing an invalid node should fall back to the restored fallback.
- const currentNode = this.focusManager.getFocusedNode();
- assert.strictEqual(currentNode, this.testFocusableTree1Node2);
- });
-
- test('restores focus when element quietly loses focus', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
- // Remove the FocusManager's listeners to simulate not receiving a focus
- // event when focus is lost. This can happen in Firefox and Safari when an
- // element is removed and then re-added to the DOM. This is a contrived
- // setup to achieve the same outcome on all browsers. For context, see:
- // https://github.com/google/blockly-keyboard-experimentation/issues/87.
- for (const registeredListener of this.globalDocumentEventListeners) {
- const eventType = registeredListener.type;
- const eventListener = registeredListener.listener;
- document.removeEventListener(eventType, eventListener);
- }
- document.body.focus();
-
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- const currentNode = this.focusManager.getFocusedNode();
- const currentElem = currentNode?.getFocusableElement();
- assert.strictEqual(currentNode, this.testFocusableTree1Node1);
- assert.strictEqual(document.activeElement, currentElem);
- });
-
- test('restores focus when element and new node focused', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
- // Remove the FocusManager's listeners to simulate not receiving a focus
- // event when focus is lost. This can happen in Firefox and Safari when an
- // element is removed and then re-added to the DOM. This is a contrived
- // setup to achieve the same outcome on all browsers. For context, see:
- // https://github.com/google/blockly-keyboard-experimentation/issues/87.
- for (const registeredListener of this.globalDocumentEventListeners) {
- const eventType = registeredListener.type;
- const eventListener = registeredListener.listener;
- document.removeEventListener(eventType, eventListener);
- }
- document.body.focus();
-
- this.focusManager.focusNode(this.testFocusableTree1Node2);
-
- const currentNode = this.focusManager.getFocusedNode();
- const currentElem = currentNode?.getFocusableElement();
- assert.strictEqual(currentNode, this.testFocusableTree1Node2);
- assert.strictEqual(document.activeElement, currentElem);
- });
-
- test('for unfocused node calls onNodeFocus once', function () {
- sinon.spy(this.testFocusableTree1Node1, 'onNodeFocus');
- this.focusManager.registerTree(this.testFocusableTree1);
-
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- assert.strictEqual(this.testFocusableTree1Node1.onNodeFocus.callCount, 1);
- });
-
- test('for previously focused node calls onNodeBlur once', function () {
- sinon.spy(this.testFocusableTree1Node1, 'onNodeBlur');
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.focusNode(this.testFocusableTree1Node2);
-
- assert.strictEqual(this.testFocusableTree1Node1.onNodeBlur.callCount, 1);
- });
-
- test('for unfocused tree calls onTreeFocus once', function () {
- sinon.spy(this.testFocusableTree1, 'onTreeFocus');
- this.focusManager.registerTree(this.testFocusableTree1);
-
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- assert.strictEqual(this.testFocusableTree1.onTreeFocus.callCount, 1);
- });
-
- test('for previously focused tree calls onTreeBlur once', function () {
- sinon.spy(this.testFocusableTree1, 'onTreeBlur');
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.focusNode(this.testFocusableTree2Node1);
-
- assert.strictEqual(this.testFocusableTree1.onTreeBlur.callCount, 1);
- });
-
- test('for same node twice calls onNodeFocus once', function () {
- sinon.spy(this.testFocusableTree1Node1, 'onNodeFocus');
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- // Call focus for the same node a second time.
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- // Despite two calls to focus the node should only focus once.
- assert.strictEqual(this.testFocusableTree1Node1.onNodeFocus.callCount, 1);
- });
-
- test('for unfocusable node does not call onNodeFocus', function () {
- sinon.spy(this.testFocusableTree1Node1, 'onNodeFocus');
- this.testFocusableTree1Node1.canBeFocused = () => false;
- this.focusManager.registerTree(this.testFocusableTree1);
-
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- // Unfocusable nodes should not be focused, nor have their callbacks called.
- assert.strictEqual(this.testFocusableTree1Node1.onNodeFocus.callCount, 0);
- });
-
- test('for unfocused node overwrites tab index', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
-
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- // Focusing an element should overwrite its tab index.
- const elem = this.testFocusableTree1Node1.getFocusableElement();
- assert.strictEqual(elem.getAttribute('tabindex'), '-1');
- });
-
- test('for previously focused node keeps new tab index', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.focusNode(this.testFocusableTree1Node2);
-
- // The previously focused element should retain its tab index.
- const elem = this.testFocusableTree1Node1.getFocusableElement();
- assert.strictEqual(elem.getAttribute('tabindex'), '-1');
- });
-
- test('for node with custom tab index does not change tab index', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- const elem = this.testFocusableTree1Node1.getFocusableElement();
- elem.tabIndex = 0;
-
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- // If the node already has a tab index set then it should retain that index.
- assert.strictEqual(elem.getAttribute('tabindex'), '0');
- });
-
- suite('for unmanaged tree', function () {
- test('focused root overwrites tab index', function () {
- this.focusManager.registerTree(this.testFocusableTree1, false);
- const rootNode = this.testFocusableTree1.getRootFocusableNode();
-
- this.focusManager.focusNode(rootNode);
-
- // Focusing an unmanaged tree's root should overwrite its tab index.
- const rootElem = rootNode.getFocusableElement();
- assert.strictEqual(rootElem.getAttribute('tabindex'), '-1');
- });
-
- test('focused root with custom tab index does not change tab index', function () {
- this.focusManager.registerTree(this.testFocusableTree1, false);
- const rootNode = this.testFocusableTree1.getRootFocusableNode();
- const rootElem = rootNode.getFocusableElement();
- rootElem.tabIndex = 0;
-
- this.focusManager.focusNode(rootNode);
-
- // If the node already has a tab index set then it should retain that index.
- assert.strictEqual(rootElem.getAttribute('tabindex'), '0');
- });
-
- test('focused node in a tree after unmanaged was focused should keep previous root unchanged', function () {
- this.focusManager.registerTree(this.testFocusableTree1, false);
- this.focusManager.registerTree(this.testFocusableTree2, false);
- const rootNode = this.testFocusableTree1.getRootFocusableNode();
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.focusNode(this.testFocusableTree2Node1);
-
- // Focusing a different tree shouldn't change the root of the previous tree if it's unmanaged.
- const rootElem = rootNode.getFocusableElement();
- assert.isNull(rootElem.getAttribute('tabindex'));
- });
-
- test('focused node in a tree after unmanaged was root focused should make previous root tab navigable', function () {
- this.focusManager.registerTree(this.testFocusableTree1, false);
- this.focusManager.registerTree(this.testFocusableTree2, false);
- const rootNode = this.testFocusableTree1.getRootFocusableNode();
- this.focusManager.focusTree(this.testFocusableTree1);
-
- this.focusManager.focusNode(this.testFocusableTree2Node1);
-
- // The previous tree's root should be kept unchanged (since it was managed).
- const rootElem = rootNode.getFocusableElement();
- assert.strictEqual(rootElem.getAttribute('tabindex'), '-1');
- });
- });
-
- suite('for managed tree', function () {
- test('for unfocused node in managed tree overwrites tab index', function () {
- this.focusManager.registerTree(this.testFocusableTree1, true);
-
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- // Focusing an element should overwrite its tab index.
- const elem = this.testFocusableTree1Node1.getFocusableElement();
- assert.strictEqual(elem.getAttribute('tabindex'), '-1');
- });
-
- test('for previously focused node in managed tree keeps new tab index', function () {
- this.focusManager.registerTree(this.testFocusableTree1, true);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.focusNode(this.testFocusableTree1Node2);
-
- // The previously focused element should retain its tab index.
- const elem = this.testFocusableTree1Node1.getFocusableElement();
- assert.strictEqual(elem.getAttribute('tabindex'), '-1');
- });
-
- test('focused root makes root non-tab navigable', function () {
- this.focusManager.registerTree(this.testFocusableTree1, true);
- const rootNode = this.testFocusableTree1.getRootFocusableNode();
-
- this.focusManager.focusNode(rootNode);
-
- // Focusing the root in a managed tree should make it non-tab navigable.
- const rootElem = rootNode.getFocusableElement();
- assert.strictEqual(rootElem.getAttribute('tabindex'), '-1');
- });
-
- test('focused root with custom tab index should overwrite tab index', function () {
- this.focusManager.registerTree(this.testFocusableTree1, true);
- const rootNode = this.testFocusableTree1.getRootFocusableNode();
- const rootElem = rootNode.getFocusableElement();
- rootElem.tabIndex = 0;
-
- this.focusManager.focusNode(rootNode);
-
- // Custom tab indexes are overwritten for the root in a managed tree.
- assert.strictEqual(rootElem.getAttribute('tabindex'), '-1');
- });
-
- test('focused node tree root makes root non-tab navigable', function () {
- this.focusManager.registerTree(this.testFocusableTree1, true);
- const rootNode = this.testFocusableTree1.getRootFocusableNode();
-
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- // Focusing a node of a managed tree should make the root non-tab navigable.
- const rootElem = rootNode.getFocusableElement();
- assert.strictEqual(rootElem.getAttribute('tabindex'), '-1');
- });
-
- test('focused node root with custom tab index should overwrite tab index', function () {
- this.focusManager.registerTree(this.testFocusableTree1, true);
- const rootNode = this.testFocusableTree1.getRootFocusableNode();
- const rootElem = rootNode.getFocusableElement();
- rootElem.tabIndex = 0;
-
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- // Custom tab indexes are overwritten for the root in a managed tree even when a tree's node
- // is focused.
- assert.strictEqual(rootElem.getAttribute('tabindex'), '-1');
- });
-
- test('focused node in a tree after managed was focused should make previous root tab navigable', function () {
- this.focusManager.registerTree(this.testFocusableTree1, true);
- this.focusManager.registerTree(this.testFocusableTree2, false);
- const rootNode = this.testFocusableTree1.getRootFocusableNode();
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.focusNode(this.testFocusableTree2Node1);
-
- // Focusing a different tree shouldn't after a managed tree should make the managed tree tab
- // navigable.
- const rootElem = rootNode.getFocusableElement();
- assert.strictEqual(rootElem.getAttribute('tabindex'), '0');
- });
-
- test('focused node in a tree after managed was root focused should make previous root tab navigable', function () {
- this.focusManager.registerTree(this.testFocusableTree1, true);
- this.focusManager.registerTree(this.testFocusableTree2, false);
- const rootNode = this.testFocusableTree1.getRootFocusableNode();
- this.focusManager.focusTree(this.testFocusableTree1);
-
- this.focusManager.focusNode(this.testFocusableTree2Node1);
-
- // Focusing a different tree shouldn't after a managed tree should make the managed tree tab
- // navigable.
- const rootElem = rootNode.getFocusableElement();
- assert.strictEqual(rootElem.getAttribute('tabindex'), '0');
- });
- });
- });
-
- suite('getFocusManager()', function () {
- test('returns non-null manager', function () {
- const manager = getFocusManager();
-
- assert.isNotNull(manager);
- });
-
- test('returns the exact same instance in subsequent calls', function () {
- const manager1 = getFocusManager();
- const manager2 = getFocusManager();
-
- assert.strictEqual(manager2, manager1);
- });
- });
-
- /* Focus tests for HTML trees. */
-
- suite('focus*() switching in HTML tree', function () {
- suite('getFocusedTree()', function () {
- test('registered tree focusTree()ed no prev focus returns tree', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
-
- this.focusManager.focusTree(this.testFocusableTree1);
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableTree1,
- );
- });
-
- test('registered tree focusTree()ed prev node focused returns tree', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.focusTree(this.testFocusableTree1);
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableTree1,
- );
- });
-
- test('registered tree focusTree()ed diff tree prev focused returns new tree', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusTree(this.testFocusableTree1);
-
- this.focusManager.focusTree(this.testFocusableTree2);
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableTree2,
- );
- });
-
- test('registered tree focusTree()ed diff tree node prev focused returns new tree', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.focusTree(this.testFocusableTree2);
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableTree2,
- );
- });
-
- test('registered root focusNode()ed no prev focus returns tree', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
-
- this.focusManager.focusNode(
- this.testFocusableTree1.getRootFocusableNode(),
- );
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableTree1,
- );
- });
-
- test("registered node focusNode()ed no prev focus returns node's tree", function () {
- this.focusManager.registerTree(this.testFocusableTree1);
-
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableTree1,
- );
- });
-
- test("registered subnode focusNode()ed no prev focus returns node's tree", function () {
- this.focusManager.registerTree(this.testFocusableTree1);
-
- this.focusManager.focusNode(this.testFocusableTree1Node1Child1);
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableTree1,
- );
- });
-
- test('registered node focusNode()ed after prev node focus returns same tree', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.focusNode(this.testFocusableTree1Node2);
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableTree1,
- );
- });
-
- test("registered node focusNode()ed after prev node focus diff tree returns new node's tree", function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.focusNode(this.testFocusableTree2Node1);
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableTree2,
- );
- });
-
- test("registered tree root focusNode()ed after prev node focus diff tree returns new node's tree", function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.focusNode(
- this.testFocusableTree2.getRootFocusableNode(),
- );
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableTree2,
- );
- });
-
- test('unregistered tree focusTree()ed with no prev focus returns null', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.focusTree(this.testFocusableTree1);
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- assert.isNull(this.focusManager.getFocusedTree());
- });
-
- test('unregistered tree focusNode()ed with no prev focus returns null', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- assert.isNull(this.focusManager.getFocusedTree());
- });
-
- test('unregistered tree focusNode()ed with prev node prior focused returns null', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusNode(this.testFocusableTree2Node1);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- // Since the more recent tree was removed, there's no tree currently focused.
- assert.isNull(this.focusManager.getFocusedTree());
- });
-
- test('unregistered tree focusNode()ed with prev node recently focused returns new tree', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
- this.focusManager.focusNode(this.testFocusableTree2Node1);
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- // Since the most recent tree still exists, it still has focus.
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableTree2,
- );
- });
-
- test('nested tree focusTree()ed with no prev focus returns nested tree', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableNestedTree4);
-
- this.focusManager.focusTree(this.testFocusableNestedTree4);
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableNestedTree4,
- );
- });
-
- test('nested tree node focusNode()ed with no prev focus returns nested tree', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableNestedTree4);
-
- this.focusManager.focusNode(this.testFocusableNestedTree4Node1);
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableNestedTree4,
- );
- });
-
- test('nested tree node focusNode()ed after parent focused returns nested tree', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableNestedTree4);
- this.focusManager.focusNode(this.testFocusableTree2Node1);
-
- this.focusManager.focusNode(this.testFocusableNestedTree4Node1);
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableNestedTree4,
- );
- });
- });
- suite('getFocusedNode()', function () {
- test('registered tree focusTree()ed no prev focus returns root node', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
-
- this.focusManager.focusTree(this.testFocusableTree1);
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree1.getRootFocusableNode(),
- );
- });
-
- test('registered tree focusTree()ed prev node focused returns original node', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.focusTree(this.testFocusableTree1);
-
- // The original node retains focus since the tree already holds focus (per focusTree's
- // contract).
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree1Node1,
- );
- });
-
- test('registered tree focusTree()ed diff tree prev focused returns new root node', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusTree(this.testFocusableTree1);
-
- this.focusManager.focusTree(this.testFocusableTree2);
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree2.getRootFocusableNode(),
- );
- });
-
- test('registered tree focusTree()ed diff tree node prev focused returns new root node', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.focusTree(this.testFocusableTree2);
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree2.getRootFocusableNode(),
- );
- });
-
- test('registered root focusNode()ed no prev focus returns root node', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
-
- this.focusManager.focusNode(
- this.testFocusableTree1.getRootFocusableNode(),
- );
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree1.getRootFocusableNode(),
- );
- });
-
- test('registered node focusNode()ed no prev focus returns node', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
-
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree1Node1,
- );
- });
-
- test('registered subnode focusNode()ed no prev focus returns subnode', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
-
- this.focusManager.focusNode(this.testFocusableTree1Node1Child1);
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree1Node1Child1,
- );
- });
-
- test('registered node focusNode()ed after prev node focus returns new node', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.focusNode(this.testFocusableTree1Node2);
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree1Node2,
- );
- });
-
- test('registered node focusNode()ed after prev node focus diff tree returns new node', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.focusNode(this.testFocusableTree2Node1);
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree2Node1,
- );
- });
-
- test('registered tree root focusNode()ed after prev node focus diff tree returns new root', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.focusNode(
- this.testFocusableTree2.getRootFocusableNode(),
- );
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree2.getRootFocusableNode(),
- );
- });
-
- test('unregistered tree focusTree()ed with no prev focus returns null', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.focusTree(this.testFocusableTree1);
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- assert.isNull(this.focusManager.getFocusedNode());
- });
-
- test('unregistered tree focusNode()ed with no prev focus returns null', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- assert.isNull(this.focusManager.getFocusedNode());
- });
-
- test('unregistered tree focusNode()ed with prev node prior focused returns null', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusNode(this.testFocusableTree2Node1);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- // Since the more recent tree was removed, there's no tree currently focused.
- assert.isNull(this.focusManager.getFocusedNode());
- });
-
- test('unregistered tree focusNode()ed with prev node recently focused returns new node', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
- this.focusManager.focusNode(this.testFocusableTree2Node1);
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- // Since the most recent tree still exists, it still has focus.
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree2Node1,
- );
- });
-
- test('nested tree focusTree()ed with no prev focus returns nested root', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableNestedTree4);
-
- this.focusManager.focusTree(this.testFocusableNestedTree4);
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableNestedTree4.getRootFocusableNode(),
- );
- });
-
- test('nested tree node focusNode()ed with no prev focus returns focused node', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableNestedTree4);
-
- this.focusManager.focusNode(this.testFocusableNestedTree4Node1);
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableNestedTree4Node1,
- );
- });
-
- test('nested tree node focusNode()ed after parent focused returns focused node', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableNestedTree4);
- this.focusManager.focusNode(this.testFocusableTree2Node1);
-
- this.focusManager.focusNode(this.testFocusableNestedTree4Node1);
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableNestedTree4Node1,
- );
- });
- });
- suite('CSS classes', function () {
- test('registered tree focusTree()ed no prev focus root elem has active property', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
-
- this.focusManager.focusTree(this.testFocusableTree1);
-
- const rootElem = this.testFocusableTree1
- .getRootFocusableNode()
- .getFocusableElement();
- assert.includesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered tree focusTree()ed prev node focused original elem has active property', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.focusTree(this.testFocusableTree1);
-
- // The original node retains active focus since the tree already holds focus (per
- // focusTree's contract).
- const nodeElem = this.testFocusableTree1Node1.getFocusableElement();
- assert.includesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered tree focusTree()ed diff tree prev focused new root elem has active property', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusTree(this.testFocusableTree1);
-
- this.focusManager.focusTree(this.testFocusableTree2);
-
- const rootElem = this.testFocusableTree2
- .getRootFocusableNode()
- .getFocusableElement();
- assert.includesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered tree focusTree()ed diff tree node prev focused new root elem has active property', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.focusTree(this.testFocusableTree2);
-
- const rootElem = this.testFocusableTree2
- .getRootFocusableNode()
- .getFocusableElement();
- assert.includesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered root focusNode()ed no prev focus returns root elem has active property', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
-
- this.focusManager.focusNode(
- this.testFocusableTree1.getRootFocusableNode(),
- );
-
- const rootElem = this.testFocusableTree1
- .getRootFocusableNode()
- .getFocusableElement();
- assert.includesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered node focusNode()ed no prev focus node elem has active property', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
-
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- const nodeElem = this.testFocusableTree1Node1.getFocusableElement();
- assert.includesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered node focusNode()ed after prev node focus same tree old node elem has no focus property', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.focusNode(this.testFocusableTree1Node2);
-
- const prevNodeElem = this.testFocusableTree1Node1.getFocusableElement();
- assert.notIncludesClass(
- prevNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- prevNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered node focusNode()ed after prev node focus same tree new node elem has active property', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.focusNode(this.testFocusableTree1Node2);
-
- const newNodeElem = this.testFocusableTree1Node2.getFocusableElement();
- assert.includesClass(
- newNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- newNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered node focusNode()ed after prev node focus diff tree old node elem has passive property', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.focusNode(this.testFocusableTree2Node1);
-
- const prevNodeElem = this.testFocusableTree1Node1.getFocusableElement();
- assert.notIncludesClass(
- prevNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- prevNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered node focusNode()ed after prev node focus diff tree new node elem has active property', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.focusNode(this.testFocusableTree2Node1);
-
- const newNodeElem = this.testFocusableTree2Node1.getFocusableElement();
- assert.includesClass(
- newNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- newNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered tree root focusNode()ed after prev node focus diff tree new root has active property', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.focusNode(
- this.testFocusableTree2.getRootFocusableNode(),
- );
-
- const rootElem = this.testFocusableTree2
- .getRootFocusableNode()
- .getFocusableElement();
- assert.includesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('unregistered tree focusTree()ed with no prev focus removes focus', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.focusTree(this.testFocusableTree1);
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- // Since the tree was unregistered it no longer has focus indicators.
- const rootElem = this.testFocusableTree1
- .getRootFocusableNode()
- .getFocusableElement();
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('unregistered tree focusNode()ed with no prev focus removes focus', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- // Since the tree was unregistered it no longer has focus indicators.
- const nodeElem = this.testFocusableTree1Node1.getFocusableElement();
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('unregistered tree focusNode()ed with prev node prior removes focus from removed tree', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusNode(this.testFocusableTree2Node1);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- // Since the tree was unregistered it no longer has focus indicators. However, the old node
- // should still have passive indication.
- const otherNodeElem =
- this.testFocusableTree2Node1.getFocusableElement();
- const removedNodeElem =
- this.testFocusableTree1Node1.getFocusableElement();
- assert.includesClass(
- otherNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- otherNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- removedNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- removedNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('unregistered tree focusNode()ed with prev node recently removes focus from removed tree', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
- this.focusManager.focusNode(this.testFocusableTree2Node1);
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- // Since the tree was unregistered it no longer has focus indicators. However, the new node
- // should still have active indication.
- const otherNodeElem =
- this.testFocusableTree2Node1.getFocusableElement();
- const removedNodeElem =
- this.testFocusableTree1Node1.getFocusableElement();
- assert.includesClass(
- otherNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- otherNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- removedNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- removedNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('focusNode() multiple nodes in same tree with switches ensure passive focus has gone', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
- this.focusManager.focusNode(this.testFocusableTree2Node1);
-
- this.focusManager.focusNode(this.testFocusableTree1Node2);
-
- // When switching back to the first tree, ensure the original passive node is no longer
- // passive now that the new node is active.
- const node1 = this.testFocusableTree1Node1.getFocusableElement();
- const node2 = this.testFocusableTree1Node2.getFocusableElement();
- assert.notIncludesClass(
- node1.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- node2.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered tree focusTree()ed other tree node passively focused tree node now has active property', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
- this.focusManager.focusNode(this.testFocusableTree2Node1);
-
- this.focusManager.focusTree(this.testFocusableTree1);
-
- // The original node in the tree should be moved from passive to active focus per the
- // contract of focusTree). Also, the root of the tree should have no focus indication.
- const nodeElem = this.testFocusableTree1Node1.getFocusableElement();
- const rootElem = this.testFocusableTree1
- .getRootFocusableNode()
- .getFocusableElement();
- assert.includesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('focus on root, node in diff tree, then node in first tree; root should have focus gone', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusTree(this.testFocusableTree1);
- this.focusManager.focusNode(this.testFocusableTree2Node1);
-
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- const nodeElem = this.testFocusableTree1Node1.getFocusableElement();
- const rootElem = this.testFocusableTree1
- .getRootFocusableNode()
- .getFocusableElement();
- assert.includesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('nested tree focusTree()ed with no prev root has active focus', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableNestedTree4);
-
- this.focusManager.focusTree(this.testFocusableNestedTree4);
-
- const rootElem = this.testFocusableNestedTree4
- .getRootFocusableNode()
- .getFocusableElement();
- assert.includesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('nested tree node focusNode()ed with no prev focus node has active focus', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableNestedTree4);
-
- this.focusManager.focusNode(this.testFocusableNestedTree4Node1);
-
- const nodeElem =
- this.testFocusableNestedTree4Node1.getFocusableElement();
- assert.includesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('nested tree node focusNode()ed after parent focused prev has passive node has active', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableNestedTree4);
- this.focusManager.focusNode(this.testFocusableTree2Node1);
-
- this.focusManager.focusNode(this.testFocusableNestedTree4Node1);
-
- const prevNodeElem = this.testFocusableTree2Node1.getFocusableElement();
- const currNodeElem =
- this.testFocusableNestedTree4Node1.getFocusableElement();
- assert.notIncludesClass(
- prevNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- prevNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- currNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- currNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
- });
- });
-
- suite('DOM focus() switching in HTML tree', function () {
- suite('getFocusedTree()', function () {
- test('registered root focus()ed no prev focus returns tree', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- document.getElementById('testFocusableTree1').tabIndex = -1;
-
- document.getElementById('testFocusableTree1').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableTree1,
- );
- });
-
- test("registered node focus()ed no prev focus returns node's tree", function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
-
- document.getElementById('testFocusableTree1.node1').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableTree1,
- );
- });
-
- test("registered subnode focus()ed no prev focus returns node's tree", function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- document.getElementById('testFocusableTree1.node1.child1').tabIndex =
- -1;
-
- document.getElementById('testFocusableTree1.node1.child1').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableTree1,
- );
- });
-
- test('registered node focus()ed after prev node focus returns same tree', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node2').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').focus();
-
- document.getElementById('testFocusableTree1.node2').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableTree1,
- );
- });
-
- test("registered node focus()ed after prev node focus diff tree returns new node's tree", function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree2.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').focus();
-
- document.getElementById('testFocusableTree2.node1').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableTree2,
- );
- });
-
- test("registered tree root focus()ed after prev node focus diff tree returns new node's tree", function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree2').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').focus();
-
- document.getElementById('testFocusableTree2').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableTree2,
- );
- });
-
- test("non-registered node subelement focus()ed returns node's tree", function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- document.getElementById(
- 'testFocusableTree1.node2.unregisteredChild1',
- ).tabIndex = -1;
-
- document
- .getElementById('testFocusableTree1.node2.unregisteredChild1')
- .focus();
-
- // The tree of the unregistered child element should take focus.
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableTree1,
- );
- });
-
- test('non-registered tree focus()ed returns null', function () {
- document.getElementById('testUnregisteredFocusableTree3').tabIndex = -1;
-
- document.getElementById('testUnregisteredFocusableTree3').focus();
-
- assert.isNull(this.focusManager.getFocusedTree());
- });
-
- test('non-registered tree node focus()ed returns null', function () {
- document.getElementById(
- 'testUnregisteredFocusableTree3.node1',
- ).tabIndex = -1;
-
- document.getElementById('testUnregisteredFocusableTree3.node1').focus();
-
- assert.isNull(this.focusManager.getFocusedTree());
- });
-
- test('non-registered tree node focus()ed after registered node focused returns null', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById(
- 'testUnregisteredFocusableTree3.node1',
- ).tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').focus();
-
- document.getElementById('testUnregisteredFocusableTree3.node1').focus();
-
- assert.isNull(this.focusManager.getFocusedTree());
- });
-
- test('unfocusable element focus()ed after registered node focused returns original tree', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').focus();
-
- document.getElementById('testUnfocusableElement').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableTree1,
- );
- });
-
- test('unregistered tree focus()ed with no prev focus returns null', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- document.getElementById('testFocusableTree1').tabIndex = -1;
- document.getElementById('testFocusableTree1').focus();
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- assert.isNull(this.focusManager.getFocusedTree());
- });
-
- test('unregistered tree focus()ed with no prev focus returns null', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').focus();
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- assert.isNull(this.focusManager.getFocusedTree());
- });
-
- test('unregistered tree focus()ed with prev node prior focused returns null', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- document.getElementById('testFocusableTree2.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree2.node1').focus();
- document.getElementById('testFocusableTree1.node1').focus();
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- // Since the more recent tree was removed, there's no tree currently focused.
- assert.isNull(this.focusManager.getFocusedTree());
- });
-
- test('unregistered tree focus()ed with prev node recently focused returns new tree', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree2.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').focus();
- document.getElementById('testFocusableTree2.node1').focus();
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- // Since the most recent tree still exists, it still has focus.
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableTree2,
- );
- });
-
- test('unregistered tree focus()ed with prev node after unregistering returns null', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree2.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').focus();
- document.getElementById('testFocusableTree2.node1').focus();
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- document.getElementById('testFocusableTree1.node1').focus();
-
- // Attempting to focus a now removed tree should result in nothing being
- // focused since the removed tree can have DOM focus, but that focus is
- // ignored by FocusManager.
- assert.isNull(this.focusManager.getFocusedTree());
- });
-
- test('nested tree focusTree()ed with no prev focus returns nested tree', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableNestedTree4);
- document.getElementById('testFocusableNestedTree4').tabIndex = -1;
-
- document.getElementById('testFocusableNestedTree4').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableNestedTree4,
- );
- });
-
- test('nested tree node focusNode()ed with no prev focus returns nested tree', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableNestedTree4);
- document.getElementById('testFocusableNestedTree4.node1').tabIndex = -1;
-
- document.getElementById('testFocusableNestedTree4.node1').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableNestedTree4,
- );
- });
-
- test('nested tree node focusNode()ed after parent focused returns nested tree', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableNestedTree4);
- document.getElementById('testFocusableTree2.node1').tabIndex = -1;
- document.getElementById('testFocusableNestedTree4.node1').tabIndex = -1;
- document.getElementById('testFocusableTree2.node1').focus();
-
- document.getElementById('testFocusableNestedTree4.node1').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableNestedTree4,
- );
- });
- });
- suite('getFocusedNode()', function () {
- test('registered root focus()ed no prev focus returns root node', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- document.getElementById('testFocusableTree1').tabIndex = -1;
-
- document.getElementById('testFocusableTree1').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree1.getRootFocusableNode(),
- );
- });
-
- test('registered node focus()ed no prev focus returns node', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
-
- document.getElementById('testFocusableTree1.node1').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree1Node1,
- );
- });
-
- test('registered subnode focus()ed no prev focus returns subnode', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- document.getElementById('testFocusableTree1.node1.child1').tabIndex =
- -1;
-
- document.getElementById('testFocusableTree1.node1.child1').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree1Node1Child1,
- );
- });
-
- test('registered node focus()ed after prev node focus returns new node', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node2').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').focus();
-
- document.getElementById('testFocusableTree1.node2').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree1Node2,
- );
- });
-
- test('registered node focus()ed after prev node focus diff tree returns new node', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree2.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').focus();
-
- document.getElementById('testFocusableTree2.node1').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree2Node1,
- );
- });
-
- test('registered tree root focus()ed after prev node focus diff tree returns new root', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree2').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').focus();
-
- document.getElementById('testFocusableTree2').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree2.getRootFocusableNode(),
- );
- });
-
- test('non-registered node subelement focus()ed returns nearest node', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- document.getElementById(
- 'testFocusableTree1.node2.unregisteredChild1',
- ).tabIndex = -1;
-
- document
- .getElementById('testFocusableTree1.node2.unregisteredChild1')
- .focus();
-
- // The nearest node of the unregistered child element should take focus.
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree1Node2,
- );
- });
-
- test('non-registered tree focus()ed returns null', function () {
- document.getElementById('testUnregisteredFocusableTree3').tabIndex = -1;
-
- document.getElementById('testUnregisteredFocusableTree3').focus();
-
- assert.isNull(this.focusManager.getFocusedNode());
- });
-
- test('non-registered tree node focus()ed returns null', function () {
- document.getElementById(
- 'testUnregisteredFocusableTree3.node1',
- ).tabIndex = -1;
-
- document.getElementById('testUnregisteredFocusableTree3.node1').focus();
-
- assert.isNull(this.focusManager.getFocusedNode());
- });
-
- test('non-registered tree node focus()ed after registered node focused returns null', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById(
- 'testUnregisteredFocusableTree3.node1',
- ).tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').focus();
-
- document.getElementById('testUnregisteredFocusableTree3.node1').focus();
-
- assert.isNull(this.focusManager.getFocusedNode());
- });
-
- test('unfocusable element focus()ed after registered node focused returns original node', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').focus();
-
- document.getElementById('testUnfocusableElement').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree1Node1,
- );
- });
-
- test('unregistered tree focus()ed with no prev focus returns null', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- document.getElementById('testFocusableTree1').tabIndex = -1;
- document.getElementById('testFocusableTree1').focus();
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- assert.isNull(this.focusManager.getFocusedNode());
- });
-
- test('unregistered tree focus()ed with no prev focus returns null', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').focus();
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- assert.isNull(this.focusManager.getFocusedNode());
- });
-
- test('unregistered tree focus()ed with prev node prior focused returns null', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- document.getElementById('testFocusableTree2.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree2.node1').focus();
- document.getElementById('testFocusableTree1.node1').focus();
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- // Since the more recent tree was removed, there's no tree currently focused.
- assert.isNull(this.focusManager.getFocusedNode());
- });
-
- test('unregistered tree focus()ed with prev node recently focused returns new node', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree2.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').focus();
- document.getElementById('testFocusableTree2.node1').focus();
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- // Since the most recent tree still exists, it still has focus.
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree2Node1,
- );
- });
-
- test('unregistered tree focus()ed with prev node after unregistering returns null', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree2.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').focus();
- document.getElementById('testFocusableTree2.node1').focus();
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- document.getElementById('testFocusableTree1.node1').focus();
-
- // Attempting to focus a now removed tree should result in nothing being
- // focused since the removed tree can have DOM focus, but that focus is
- // ignored by FocusManager.
- assert.isNull(this.focusManager.getFocusedNode());
- });
-
- test('nested tree focus()ed with no prev focus returns nested root', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableNestedTree4);
- document.getElementById('testFocusableNestedTree4').tabIndex = -1;
-
- document.getElementById('testFocusableNestedTree4').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableNestedTree4.getRootFocusableNode(),
- );
- });
-
- test('nested tree node focus()ed with no prev focus returns focused node', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableNestedTree4);
- document.getElementById('testFocusableNestedTree4.node1').tabIndex = -1;
-
- document.getElementById('testFocusableNestedTree4.node1').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableNestedTree4Node1,
- );
- });
-
- test('nested tree node focus()ed after parent focused returns focused node', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableNestedTree4);
- document.getElementById('testFocusableTree2.node1').tabIndex = -1;
- document.getElementById('testFocusableNestedTree4.node1').tabIndex = -1;
- document.getElementById('testFocusableTree2.node1').focus();
-
- document.getElementById('testFocusableNestedTree4.node1').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableNestedTree4Node1,
- );
- });
-
- test('after focus() after trying to focusNode() an unfocusable node updates returns focus()ed node', function () {
- this.testFocusableTree1Node1.canBeFocused = () => false;
- document.getElementById('testFocusableTree1.node2').tabIndex = -1;
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- document.getElementById('testFocusableTree1.node2').focus();
-
- // focus()ing a new node should overwrite a failed attempt to focusNode() an unfocusable
- // node. This verifies that DOM focus syncing is properly reenabled by FocusManager.
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree1Node2,
- );
- });
-
- test('after focus() after trying to focusNode() the same node twice returns focus()ed node', function () {
- document.getElementById('testFocusableTree1.node2').tabIndex = -1;
- this.focusManager.registerTree(this.testFocusableTree1);
- // Intentionally try to focus the same node twice.
- this.focusManager.focusNode(this.testFocusableTree1Node1);
- this.focusManager.focusNode(this.testFocusableTree1Node1);
-
- document.getElementById('testFocusableTree1.node2').focus();
-
- // focus()ing a new node should overwrite a failed attempt to focusNode() the same node
- // twice. This verifies that DOM focus syncing is properly reenabled by FocusManager.
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree1Node2,
- );
- });
- });
- suite('CSS classes', function () {
- test('registered root focus()ed no prev focus returns root elem has active property', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- document.getElementById('testFocusableTree1').tabIndex = -1;
-
- document.getElementById('testFocusableTree1').focus();
-
- const rootElem = this.testFocusableTree1
- .getRootFocusableNode()
- .getFocusableElement();
- assert.includesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered node focus()ed no prev focus node elem has active property', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
-
- document.getElementById('testFocusableTree1.node1').focus();
-
- const nodeElem = this.testFocusableTree1Node1.getFocusableElement();
- assert.includesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered node focus()ed after prev node focus same tree old node elem has no focus property', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node2').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').focus();
-
- document.getElementById('testFocusableTree1.node2').focus();
-
- const prevNodeElem = this.testFocusableTree1Node1.getFocusableElement();
- assert.notIncludesClass(
- prevNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- prevNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered node focus()ed after prev node focus same tree new node elem has active property', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node2').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').focus();
-
- document.getElementById('testFocusableTree1.node2').focus();
-
- const newNodeElem = this.testFocusableTree1Node2.getFocusableElement();
- assert.includesClass(
- newNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- newNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered node focus()ed after prev node focus diff tree old node elem has passive property', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree2.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').focus();
-
- document.getElementById('testFocusableTree2.node1').focus();
-
- const prevNodeElem = this.testFocusableTree1Node1.getFocusableElement();
- assert.notIncludesClass(
- prevNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- prevNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered node focus()ed after prev node focus diff tree new node elem has active property', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree2.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').focus();
-
- document.getElementById('testFocusableTree2.node1').focus();
-
- const newNodeElem = this.testFocusableTree2Node1.getFocusableElement();
- assert.includesClass(
- newNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- newNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered tree root focus()ed after prev node focus diff tree new root has active property', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree2').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').focus();
-
- document.getElementById('testFocusableTree2').focus();
-
- const rootElem = this.testFocusableTree2
- .getRootFocusableNode()
- .getFocusableElement();
- assert.includesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('non-registered node subelement focus()ed nearest node has active property', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- document.getElementById(
- 'testFocusableTree1.node2.unregisteredChild1',
- ).tabIndex = -1;
-
- document
- .getElementById('testFocusableTree1.node2.unregisteredChild1')
- .focus();
-
- // The nearest node of the unregistered child element should be actively focused.
- const nodeElem = this.testFocusableTree1Node2.getFocusableElement();
- assert.includesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('non-registered tree focus()ed has no focus', function () {
- document.getElementById('testUnregisteredFocusableTree3').tabIndex = -1;
-
- document.getElementById('testUnregisteredFocusableTree3').focus();
-
- assert.isNull(this.focusManager.getFocusedNode());
- const rootElem = document.getElementById(
- 'testUnregisteredFocusableTree3',
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('non-registered tree node focus()ed has no focus', function () {
- document.getElementById(
- 'testUnregisteredFocusableTree3.node1',
- ).tabIndex = -1;
-
- document.getElementById('testUnregisteredFocusableTree3.node1').focus();
-
- assert.isNull(this.focusManager.getFocusedNode());
- const nodeElem = document.getElementById(
- 'testUnregisteredFocusableTree3.node1',
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('unfocsable element focus()ed after registered node focused original node has active focus', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').focus();
-
- document.getElementById('testUnfocusableElement').focus();
-
- // The original node should be unchanged, and the unregistered node should not have any
- // focus indicators.
- const nodeElem = document.getElementById('testFocusableTree1.node1');
- const attemptedNewNodeElem = document.getElementById(
- 'testUnfocusableElement',
- );
- assert.includesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- attemptedNewNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- attemptedNewNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('unregistered tree focus()ed with no prev focus removes focus', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- document.getElementById('testFocusableTree1').tabIndex = -1;
- document.getElementById('testFocusableTree1').focus();
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- // Since the tree was unregistered it no longer has focus indicators.
- const rootElem = this.testFocusableTree1
- .getRootFocusableNode()
- .getFocusableElement();
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('unregistered tree focus()ed with no prev focus removes focus', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').focus();
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- // Since the tree was unregistered it no longer has focus indicators.
- const nodeElem = this.testFocusableTree1Node1.getFocusableElement();
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('unregistered tree focus()ed with prev node prior removes focus from removed tree', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- document.getElementById('testFocusableTree2.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree2.node1').focus();
- document.getElementById('testFocusableTree1.node1').focus();
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- // Since the tree was unregistered it no longer has focus indicators. However, the old node
- // should still have passive indication.
- const otherNodeElem =
- this.testFocusableTree2Node1.getFocusableElement();
- const removedNodeElem =
- this.testFocusableTree1Node1.getFocusableElement();
- assert.includesClass(
- otherNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- otherNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- removedNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- removedNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('unregistered tree focus()ed with prev node recently removes focus from removed tree', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree2.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').focus();
- document.getElementById('testFocusableTree2.node1').focus();
-
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- // Since the tree was unregistered it no longer has focus indicators. However, the new node
- // should still have active indication.
- const otherNodeElem =
- this.testFocusableTree2Node1.getFocusableElement();
- const removedNodeElem =
- this.testFocusableTree1Node1.getFocusableElement();
- assert.includesClass(
- otherNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- otherNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- removedNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- removedNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('unregistered tree focus()ed with prev node after unregistering removes active indicator', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree2.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').focus();
- document.getElementById('testFocusableTree2.node1').focus();
- this.focusManager.unregisterTree(this.testFocusableTree1);
-
- document.getElementById('testFocusableTree1.node1').focus();
-
- // Attempting to focus a now removed tree should remove active.
- const otherNodeElem =
- this.testFocusableTree2Node1.getFocusableElement();
- const removedNodeElem =
- this.testFocusableTree1Node1.getFocusableElement();
- assert.notIncludesClass(
- otherNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- otherNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- removedNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- removedNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('focus() multiple nodes in same tree with switches ensure passive focus has gone', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree2.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node2').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').focus();
- document.getElementById('testFocusableTree2.node1').focus();
-
- document.getElementById('testFocusableTree1.node2').focus();
-
- // When switching back to the first tree, ensure the original passive node is no longer
- // passive now that the new node is active.
- const node1 = this.testFocusableTree1Node1.getFocusableElement();
- const node2 = this.testFocusableTree1Node2.getFocusableElement();
- assert.notIncludesClass(
- node1.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- node2.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('focus on root, node in diff tree, then node in first tree; root should have focus gone', function () {
- this.focusManager.registerTree(this.testFocusableTree1);
- this.focusManager.registerTree(this.testFocusableTree2);
- document.getElementById('testFocusableTree1').tabIndex = -1;
- document.getElementById('testFocusableTree2.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1.node1').tabIndex = -1;
- document.getElementById('testFocusableTree1').focus();
- document.getElementById('testFocusableTree2.node1').focus();
-
- document.getElementById('testFocusableTree1.node1').focus();
-
- const nodeElem = this.testFocusableTree1Node1.getFocusableElement();
- const rootElem = this.testFocusableTree1
- .getRootFocusableNode()
- .getFocusableElement();
- assert.includesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('nested tree focus()ed with no prev root has active focus', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableNestedTree4);
- document.getElementById('testFocusableNestedTree4').tabIndex = -1;
-
- document.getElementById('testFocusableNestedTree4').focus();
-
- const rootElem = this.testFocusableNestedTree4
- .getRootFocusableNode()
- .getFocusableElement();
- assert.includesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('nested tree node focus()ed with no prev focus node has active focus', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableNestedTree4);
- document.getElementById('testFocusableNestedTree4.node1').tabIndex = -1;
-
- document.getElementById('testFocusableNestedTree4.node1').focus();
-
- const nodeElem =
- this.testFocusableNestedTree4Node1.getFocusableElement();
- assert.includesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('nested tree node focus()ed after parent focused prev has passive node has active', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableNestedTree4);
- document.getElementById('testFocusableTree2.node1').tabIndex = -1;
- document.getElementById('testFocusableNestedTree4.node1').tabIndex = -1;
- document.getElementById('testFocusableTree2.node1').focus();
-
- document.getElementById('testFocusableNestedTree4.node1').focus();
-
- const prevNodeElem = this.testFocusableTree2Node1.getFocusableElement();
- const currNodeElem =
- this.testFocusableNestedTree4Node1.getFocusableElement();
- assert.notIncludesClass(
- prevNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- prevNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- currNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- currNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
- });
- });
-
- /* Focus tests for SVG trees. */
-
- suite('focus*() switching in SVG tree', function () {
- suite('getFocusedTree()', function () {
- test('registered tree focusTree()ed no prev focus returns tree', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
-
- this.focusManager.focusTree(this.testFocusableGroup1);
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableGroup1,
- );
- });
-
- test('registered tree focusTree()ed prev node focused returns tree', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- this.focusManager.focusTree(this.testFocusableGroup1);
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableGroup1,
- );
- });
-
- test('registered tree focusTree()ed diff tree prev focused returns new tree', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusTree(this.testFocusableGroup1);
-
- this.focusManager.focusTree(this.testFocusableGroup2);
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableGroup2,
- );
- });
-
- test('registered tree focusTree()ed diff tree node prev focused returns new tree', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- this.focusManager.focusTree(this.testFocusableGroup2);
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableGroup2,
- );
- });
-
- test('registered root focusNode()ed no prev focus returns tree', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
-
- this.focusManager.focusNode(
- this.testFocusableGroup1.getRootFocusableNode(),
- );
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableGroup1,
- );
- });
-
- test("registered node focusNode()ed no prev focus returns node's tree", function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
-
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableGroup1,
- );
- });
-
- test("registered subnode focusNode()ed no prev focus returns node's tree", function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
-
- this.focusManager.focusNode(this.testFocusableGroup1Node1Child1);
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableGroup1,
- );
- });
-
- test('registered node focusNode()ed after prev node focus returns same tree', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- this.focusManager.focusNode(this.testFocusableGroup1Node2);
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableGroup1,
- );
- });
-
- test("registered node focusNode()ed after prev node focus diff tree returns new node's tree", function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- this.focusManager.focusNode(this.testFocusableGroup2Node1);
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableGroup2,
- );
- });
-
- test("registered tree root focusNode()ed after prev node focus diff tree returns new node's tree", function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- this.focusManager.focusNode(
- this.testFocusableGroup2.getRootFocusableNode(),
- );
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableGroup2,
- );
- });
-
- test('unregistered tree focusTree()ed with no prev focus returns null', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.focusTree(this.testFocusableGroup1);
-
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- assert.isNull(this.focusManager.getFocusedTree());
- });
-
- test('unregistered tree focusNode()ed with no prev focus returns null', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- assert.isNull(this.focusManager.getFocusedTree());
- });
-
- test('unregistered tree focusNode()ed with prev node prior focused returns null', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableGroup2Node1);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- // Since the more recent tree was removed, there's no tree currently focused.
- assert.isNull(this.focusManager.getFocusedTree());
- });
-
- test('unregistered tree focusNode()ed with prev node recently focused returns new tree', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
- this.focusManager.focusNode(this.testFocusableGroup2Node1);
-
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- // Since the most recent tree still exists, it still has focus.
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableGroup2,
- );
- });
-
- test('nested tree focusTree()ed with no prev focus returns nested tree', function () {
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.registerTree(this.testFocusableNestedGroup4);
-
- this.focusManager.focusTree(this.testFocusableNestedGroup4);
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableNestedGroup4,
- );
- });
-
- test('nested tree node focusNode()ed with no prev focus returns nested tree', function () {
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.registerTree(this.testFocusableNestedGroup4);
-
- this.focusManager.focusNode(this.testFocusableNestedGroup4Node1);
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableNestedGroup4,
- );
- });
-
- test('nested tree node focusNode()ed after parent focused returns nested tree', function () {
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.registerTree(this.testFocusableNestedGroup4);
- this.focusManager.focusNode(this.testFocusableGroup2Node1);
-
- this.focusManager.focusNode(this.testFocusableNestedGroup4Node1);
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableNestedGroup4,
- );
- });
- });
- suite('getFocusedNode()', function () {
- test('registered tree focusTree()ed no prev focus returns root node', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
-
- this.focusManager.focusTree(this.testFocusableGroup1);
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup1.getRootFocusableNode(),
- );
- });
-
- test('registered tree focusTree()ed prev node focused returns original node', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- this.focusManager.focusTree(this.testFocusableGroup1);
-
- // The original node retains focus since the tree already holds focus (per focusTree's
- // contract).
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup1Node1,
- );
- });
-
- test('registered tree focusTree()ed diff tree prev focused returns new root node', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusTree(this.testFocusableGroup1);
-
- this.focusManager.focusTree(this.testFocusableGroup2);
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup2.getRootFocusableNode(),
- );
- });
-
- test('registered tree focusTree()ed diff tree node prev focused returns new root node', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- this.focusManager.focusTree(this.testFocusableGroup2);
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup2.getRootFocusableNode(),
- );
- });
-
- test('registered root focusNode()ed no prev focus returns root node', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
-
- this.focusManager.focusNode(
- this.testFocusableGroup1.getRootFocusableNode(),
- );
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup1.getRootFocusableNode(),
- );
- });
-
- test('registered node focusNode()ed no prev focus returns node', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
-
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup1Node1,
- );
- });
-
- test('registered subnode focusNode()ed no prev focus returns subnode', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
-
- this.focusManager.focusNode(this.testFocusableGroup1Node1Child1);
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup1Node1Child1,
- );
- });
-
- test('registered node focusNode()ed after prev node focus returns new node', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- this.focusManager.focusNode(this.testFocusableGroup1Node2);
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup1Node2,
- );
- });
-
- test('registered node focusNode()ed after prev node focus diff tree returns new node', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- this.focusManager.focusNode(this.testFocusableGroup2Node1);
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup2Node1,
- );
- });
-
- test('registered tree root focusNode()ed after prev node focus diff tree returns new root', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- this.focusManager.focusNode(
- this.testFocusableGroup2.getRootFocusableNode(),
- );
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup2.getRootFocusableNode(),
- );
- });
-
- test('unregistered tree focusTree()ed with no prev focus returns null', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.focusTree(this.testFocusableGroup1);
-
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- assert.isNull(this.focusManager.getFocusedNode());
- });
-
- test('unregistered tree focusNode()ed with no prev focus returns null', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- assert.isNull(this.focusManager.getFocusedNode());
- });
-
- test('unregistered tree focusNode()ed with prev node prior focused returns null', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableGroup2Node1);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- // Since the more recent tree was removed, there's no tree currently focused.
- assert.isNull(this.focusManager.getFocusedNode());
- });
-
- test('unregistered tree focusNode()ed with prev node recently focused returns new node', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
- this.focusManager.focusNode(this.testFocusableGroup2Node1);
-
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- // Since the most recent tree still exists, it still has focus.
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup2Node1,
- );
- });
-
- test('nested tree focusTree()ed with no prev focus returns nested root', function () {
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.registerTree(this.testFocusableNestedGroup4);
-
- this.focusManager.focusTree(this.testFocusableNestedGroup4);
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableNestedGroup4.getRootFocusableNode(),
- );
- });
-
- test('nested tree node focusNode()ed with no prev focus returns focused node', function () {
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.registerTree(this.testFocusableNestedGroup4);
-
- this.focusManager.focusNode(this.testFocusableNestedGroup4Node1);
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableNestedGroup4Node1,
- );
- });
-
- test('nested tree node focusNode()ed after parent focused returns focused node', function () {
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.registerTree(this.testFocusableNestedGroup4);
- this.focusManager.focusNode(this.testFocusableGroup2Node1);
-
- this.focusManager.focusNode(this.testFocusableNestedGroup4Node1);
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableNestedGroup4Node1,
- );
- });
- });
- suite('CSS classes', function () {
- test('registered tree focusTree()ed no prev focus root elem has active property', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
-
- this.focusManager.focusTree(this.testFocusableGroup1);
-
- const rootElem = this.testFocusableGroup1
- .getRootFocusableNode()
- .getFocusableElement();
- assert.includesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered tree focusTree()ed prev node focused original elem has active property', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- this.focusManager.focusTree(this.testFocusableGroup1);
-
- // The original node retains active focus since the tree already holds focus (per
- // focusTree's contract).
- const nodeElem = this.testFocusableGroup1Node1.getFocusableElement();
- assert.includesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered tree focusTree()ed diff tree prev focused new root elem has active property', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusTree(this.testFocusableGroup1);
-
- this.focusManager.focusTree(this.testFocusableGroup2);
-
- const rootElem = this.testFocusableGroup2
- .getRootFocusableNode()
- .getFocusableElement();
- assert.includesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered tree focusTree()ed diff tree node prev focused new root elem has active property', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- this.focusManager.focusTree(this.testFocusableGroup2);
-
- const rootElem = this.testFocusableGroup2
- .getRootFocusableNode()
- .getFocusableElement();
- assert.includesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered root focusNode()ed no prev focus returns root elem has active property', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
-
- this.focusManager.focusNode(
- this.testFocusableGroup1.getRootFocusableNode(),
- );
-
- const rootElem = this.testFocusableGroup1
- .getRootFocusableNode()
- .getFocusableElement();
- assert.includesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered node focusNode()ed no prev focus node elem has active property', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
-
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- const nodeElem = this.testFocusableGroup1Node1.getFocusableElement();
- assert.includesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered node focusNode()ed after prev node focus same tree old node elem has no focus property', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- this.focusManager.focusNode(this.testFocusableGroup1Node2);
-
- const prevNodeElem =
- this.testFocusableGroup1Node1.getFocusableElement();
- assert.notIncludesClass(
- prevNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- prevNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered node focusNode()ed after prev node focus same tree new node elem has active property', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- this.focusManager.focusNode(this.testFocusableGroup1Node2);
-
- const newNodeElem = this.testFocusableGroup1Node2.getFocusableElement();
- assert.includesClass(
- newNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- newNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered node focusNode()ed after prev node focus diff tree old node elem has passive property', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- this.focusManager.focusNode(this.testFocusableGroup2Node1);
-
- const prevNodeElem =
- this.testFocusableGroup1Node1.getFocusableElement();
- assert.notIncludesClass(
- prevNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- prevNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered node focusNode()ed after prev node focus diff tree new node elem has active property', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- this.focusManager.focusNode(this.testFocusableGroup2Node1);
-
- const newNodeElem = this.testFocusableGroup2Node1.getFocusableElement();
- assert.includesClass(
- newNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- newNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered tree root focusNode()ed after prev node focus diff tree new root has active property', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- this.focusManager.focusNode(
- this.testFocusableGroup2.getRootFocusableNode(),
- );
-
- const rootElem = this.testFocusableGroup2
- .getRootFocusableNode()
- .getFocusableElement();
- assert.includesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('unregistered tree focusTree()ed with no prev focus removes focus', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.focusTree(this.testFocusableGroup1);
-
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- // Since the tree was unregistered it no longer has focus indicators.
- const rootElem = this.testFocusableGroup1
- .getRootFocusableNode()
- .getFocusableElement();
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('unregistered tree focusNode()ed with no prev focus removes focus', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- // Since the tree was unregistered it no longer has focus indicators.
- const nodeElem = this.testFocusableGroup1Node1.getFocusableElement();
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('unregistered tree focusNode()ed with prev node prior removes focus from removed tree', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableGroup2Node1);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- // Since the tree was unregistered it no longer has focus indicators. However, the old node
- // should still have passive indication.
- const otherNodeElem =
- this.testFocusableGroup2Node1.getFocusableElement();
- const removedNodeElem =
- this.testFocusableGroup1Node1.getFocusableElement();
- assert.includesClass(
- otherNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- otherNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- removedNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- removedNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('unregistered tree focusNode()ed with prev node recently removes focus from removed tree', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
- this.focusManager.focusNode(this.testFocusableGroup2Node1);
-
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- // Since the tree was unregistered it no longer has focus indicators. However, the new node
- // should still have active indication.
- const otherNodeElem =
- this.testFocusableGroup2Node1.getFocusableElement();
- const removedNodeElem =
- this.testFocusableGroup1Node1.getFocusableElement();
- assert.includesClass(
- otherNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- otherNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- removedNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- removedNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('focusNode() multiple nodes in same tree with switches ensure passive focus has gone', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
- this.focusManager.focusNode(this.testFocusableGroup2Node1);
-
- this.focusManager.focusNode(this.testFocusableGroup1Node2);
-
- // When switching back to the first tree, ensure the original passive node is no longer
- // passive now that the new node is active.
- const node1 = this.testFocusableGroup1Node1.getFocusableElement();
- const node2 = this.testFocusableGroup1Node2.getFocusableElement();
- assert.notIncludesClass(
- node1.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- node2.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered tree focusTree()ed other tree node passively focused tree node now has active property', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
- this.focusManager.focusNode(this.testFocusableGroup2Node1);
-
- this.focusManager.focusTree(this.testFocusableGroup1);
-
- // The original node in the tree should be moved from passive to active focus per the
- // contract of focusTree). Also, the root of the tree should have no focus indication.
- const nodeElem = this.testFocusableGroup1Node1.getFocusableElement();
- const rootElem = this.testFocusableGroup1
- .getRootFocusableNode()
- .getFocusableElement();
- assert.includesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('focus on root, node in diff tree, then node in first tree; root should have focus gone', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusTree(this.testFocusableGroup1);
- this.focusManager.focusNode(this.testFocusableGroup2Node1);
-
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- const nodeElem = this.testFocusableGroup1Node1.getFocusableElement();
- const rootElem = this.testFocusableGroup1
- .getRootFocusableNode()
- .getFocusableElement();
- assert.includesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('nested tree focusTree()ed with no prev root has active focus', function () {
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.registerTree(this.testFocusableNestedGroup4);
-
- this.focusManager.focusTree(this.testFocusableNestedGroup4);
-
- const rootElem = this.testFocusableNestedGroup4
- .getRootFocusableNode()
- .getFocusableElement();
- assert.includesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('nested tree node focusNode()ed with no prev focus node has active focus', function () {
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.registerTree(this.testFocusableNestedGroup4);
-
- this.focusManager.focusNode(this.testFocusableNestedGroup4Node1);
-
- const nodeElem =
- this.testFocusableNestedGroup4Node1.getFocusableElement();
- assert.includesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('nested tree node focusNode()ed after parent focused prev has passive node has active', function () {
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.registerTree(this.testFocusableNestedGroup4);
- this.focusManager.focusNode(this.testFocusableGroup2Node1);
-
- this.focusManager.focusNode(this.testFocusableNestedGroup4Node1);
-
- const prevNodeElem =
- this.testFocusableGroup2Node1.getFocusableElement();
- const currNodeElem =
- this.testFocusableNestedGroup4Node1.getFocusableElement();
- assert.notIncludesClass(
- prevNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- prevNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- currNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- currNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
- });
- });
-
- suite('DOM focus() switching in SVG tree', function () {
- suite('getFocusedTree()', function () {
- test('registered root focus()ed no prev focus returns tree', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- document.getElementById('testFocusableGroup1').tabIndex = -1;
-
- document.getElementById('testFocusableGroup1').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableGroup1,
- );
- });
-
- test("registered node focus()ed no prev focus returns node's tree", function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
-
- document.getElementById('testFocusableGroup1.node1').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableGroup1,
- );
- });
-
- test("registered subnode focus()ed no prev focus returns node's tree", function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- document.getElementById('testFocusableGroup1.node1.child1').tabIndex =
- -1;
-
- document.getElementById('testFocusableGroup1.node1.child1').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableGroup1,
- );
- });
-
- test('registered node focus()ed after prev node focus returns same tree', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node2').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').focus();
-
- document.getElementById('testFocusableGroup1.node2').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableGroup1,
- );
- });
-
- test("registered node focus()ed after prev node focus diff tree returns new node's tree", function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup2.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').focus();
-
- document.getElementById('testFocusableGroup2.node1').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableGroup2,
- );
- });
-
- test("registered tree root focus()ed after prev node focus diff tree returns new node's tree", function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup2').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').focus();
-
- document.getElementById('testFocusableGroup2').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableGroup2,
- );
- });
-
- test("non-registered node subelement focus()ed returns node's tree", function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- document.getElementById(
- 'testFocusableGroup1.node2.unregisteredChild1',
- ).tabIndex = -1;
-
- document
- .getElementById('testFocusableGroup1.node2.unregisteredChild1')
- .focus();
-
- // The tree of the unregistered child element should take focus.
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableGroup1,
- );
- });
-
- test('non-registered tree focus()ed returns null', function () {
- document.getElementById('testUnregisteredFocusableGroup3').tabIndex =
- -1;
-
- document.getElementById('testUnregisteredFocusableGroup3').focus();
-
- assert.isNull(this.focusManager.getFocusedTree());
- });
-
- test('non-registered tree node focus()ed returns null', function () {
- document.getElementById(
- 'testUnregisteredFocusableGroup3.node1',
- ).tabIndex = -1;
-
- document
- .getElementById('testUnregisteredFocusableGroup3.node1')
- .focus();
-
- assert.isNull(this.focusManager.getFocusedTree());
- });
-
- test('non-registered tree node focus()ed after registered node focused returns null', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById(
- 'testUnregisteredFocusableGroup3.node1',
- ).tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').focus();
-
- document
- .getElementById('testUnregisteredFocusableGroup3.node1')
- .focus();
-
- // Attempting to focus a now removed tree should result in nothing being
- // focused since the removed tree can have DOM focus, but that focus is
- // ignored by FocusManager.
- assert.isNull(this.focusManager.getFocusedTree());
- });
-
- test('unregistered tree focus()ed with no prev focus returns null', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- document.getElementById('testFocusableGroup1').tabIndex = -1;
- document.getElementById('testFocusableGroup1').focus();
-
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- assert.isNull(this.focusManager.getFocusedTree());
- });
-
- test('unregistered tree focus()ed with no prev focus returns null', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').focus();
-
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- assert.isNull(this.focusManager.getFocusedTree());
- });
-
- test('unregistered tree focus()ed with prev node prior focused returns null', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- document.getElementById('testFocusableGroup2.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup2.node1').focus();
- document.getElementById('testFocusableGroup1.node1').focus();
-
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- // Since the more recent tree was removed, there's no tree currently focused.
- assert.isNull(this.focusManager.getFocusedTree());
- });
-
- test('unregistered tree focus()ed with prev node recently focused returns new tree', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup2.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').focus();
- document.getElementById('testFocusableGroup2.node1').focus();
-
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- // Since the most recent tree still exists, it still has focus.
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableGroup2,
- );
- });
-
- test('unregistered tree focus()ed with prev node after unregistering returns null', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup2.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').focus();
- document.getElementById('testFocusableGroup2.node1').focus();
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- document.getElementById('testFocusableGroup1.node1').focus();
-
- // Attempting to focus a now removed tree should result in nothing being
- // focused since the removed tree can have DOM focus, but that focus is
- // ignored by FocusManager.
- assert.isNull(this.focusManager.getFocusedTree());
- });
-
- test('nested tree focusTree()ed with no prev focus returns nested tree', function () {
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.registerTree(this.testFocusableNestedGroup4);
- document.getElementById('testFocusableNestedGroup4').tabIndex = -1;
-
- document.getElementById('testFocusableNestedGroup4').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableNestedGroup4,
- );
- });
-
- test('nested tree node focusNode()ed with no prev focus returns nested tree', function () {
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.registerTree(this.testFocusableNestedGroup4);
- document.getElementById('testFocusableNestedGroup4.node1').tabIndex =
- -1;
-
- document.getElementById('testFocusableNestedGroup4.node1').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableNestedGroup4,
- );
- });
-
- test('nested tree node focusNode()ed after parent focused returns nested tree', function () {
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.registerTree(this.testFocusableNestedGroup4);
- document.getElementById('testFocusableGroup2.node1').tabIndex = -1;
- document.getElementById('testFocusableNestedGroup4.node1').tabIndex =
- -1;
- document.getElementById('testFocusableGroup2.node1').focus();
-
- document.getElementById('testFocusableNestedGroup4.node1').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableNestedGroup4,
- );
- });
- });
- suite('getFocusedNode()', function () {
- test('registered root focus()ed no prev focus returns root node', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- document.getElementById('testFocusableGroup1').tabIndex = -1;
-
- document.getElementById('testFocusableGroup1').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup1.getRootFocusableNode(),
- );
- });
-
- test('registered node focus()ed no prev focus returns node', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
-
- document.getElementById('testFocusableGroup1.node1').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup1Node1,
- );
- });
-
- test('registered subnode focus()ed no prev focus returns subnode', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- document.getElementById('testFocusableGroup1.node1.child1').tabIndex =
- -1;
-
- document.getElementById('testFocusableGroup1.node1.child1').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup1Node1Child1,
- );
- });
-
- test('registered node focus()ed after prev node focus returns new node', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node2').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').focus();
-
- document.getElementById('testFocusableGroup1.node2').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup1Node2,
- );
- });
-
- test('registered node focus()ed after prev node focus diff tree returns new node', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup2.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').focus();
-
- document.getElementById('testFocusableGroup2.node1').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup2Node1,
- );
- });
-
- test('registered tree root focus()ed after prev node focus diff tree returns new root', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup2').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').focus();
-
- document.getElementById('testFocusableGroup2').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup2.getRootFocusableNode(),
- );
- });
-
- test('non-registered node subelement focus()ed returns nearest node', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- document.getElementById(
- 'testFocusableGroup1.node2.unregisteredChild1',
- ).tabIndex = -1;
-
- document
- .getElementById('testFocusableGroup1.node2.unregisteredChild1')
- .focus();
-
- // The nearest node of the unregistered child element should take focus.
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup1Node2,
- );
- });
-
- test('non-registered tree focus()ed returns null', function () {
- document.getElementById('testUnregisteredFocusableGroup3').tabIndex =
- -1;
-
- document.getElementById('testUnregisteredFocusableGroup3').focus();
-
- assert.isNull(this.focusManager.getFocusedNode());
- });
-
- test('non-registered tree node focus()ed returns null', function () {
- document.getElementById(
- 'testUnregisteredFocusableGroup3.node1',
- ).tabIndex = -1;
-
- document
- .getElementById('testUnregisteredFocusableGroup3.node1')
- .focus();
-
- assert.isNull(this.focusManager.getFocusedNode());
- });
-
- test('non-registered tree node focus()ed after registered node focused returns null', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById(
- 'testUnregisteredFocusableGroup3.node1',
- ).tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').focus();
-
- document
- .getElementById('testUnregisteredFocusableGroup3.node1')
- .focus();
-
- assert.isNull(this.focusManager.getFocusedNode());
- });
-
- test('unfocusable element focus()ed after registered node focused returns original node', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').focus();
-
- document.getElementById('testUnfocusableElement').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup1Node1,
- );
- });
-
- test('unregistered tree focus()ed with no prev focus returns null', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- document.getElementById('testFocusableGroup1').tabIndex = -1;
- document.getElementById('testFocusableGroup1').focus();
-
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- assert.isNull(this.focusManager.getFocusedNode());
- });
-
- test('unregistered tree focus()ed with no prev focus returns null', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').focus();
-
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- assert.isNull(this.focusManager.getFocusedNode());
- });
-
- test('unregistered tree focus()ed with prev node prior focused returns null', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- document.getElementById('testFocusableGroup2.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup2.node1').focus();
- document.getElementById('testFocusableGroup1.node1').focus();
-
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- // Since the more recent tree was removed, there's no tree currently focused.
- assert.isNull(this.focusManager.getFocusedNode());
- });
-
- test('unregistered tree focus()ed with prev node recently focused returns new node', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup2.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').focus();
- document.getElementById('testFocusableGroup2.node1').focus();
-
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- // Since the most recent tree still exists, it still has focus.
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup2Node1,
- );
- });
-
- test('unregistered tree focus()ed with prev node after unregistering returns null', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup2.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').focus();
- document.getElementById('testFocusableGroup2.node1').focus();
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- document.getElementById('testFocusableGroup1.node1').focus();
-
- // Attempting to focus a now removed tree should result in nothing being
- // focused since the removed tree can have DOM focus, but that focus is
- // ignored by FocusManager.
- assert.isNull(this.focusManager.getFocusedNode());
- });
-
- test('nested tree focus()ed with no prev focus returns nested root', function () {
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.registerTree(this.testFocusableNestedGroup4);
- document.getElementById('testFocusableNestedGroup4').tabIndex = -1;
-
- document.getElementById('testFocusableNestedGroup4').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableNestedGroup4.getRootFocusableNode(),
- );
- });
-
- test('nested tree node focus()ed with no prev focus returns focused node', function () {
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.registerTree(this.testFocusableNestedGroup4);
- document.getElementById('testFocusableNestedGroup4.node1').tabIndex =
- -1;
-
- document.getElementById('testFocusableNestedGroup4.node1').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableNestedGroup4Node1,
- );
- });
-
- test('nested tree node focus()ed after parent focused returns focused node', function () {
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.registerTree(this.testFocusableNestedGroup4);
- document.getElementById('testFocusableGroup2.node1').tabIndex = -1;
- document.getElementById('testFocusableNestedGroup4.node1').tabIndex =
- -1;
- document.getElementById('testFocusableGroup2.node1').focus();
-
- document.getElementById('testFocusableNestedGroup4.node1').focus();
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableNestedGroup4Node1,
- );
- });
-
- test('after focus() after trying to focusNode() an unfocusable node updates returns focus()ed node', function () {
- this.testFocusableGroup1Node1.canBeFocused = () => false;
- document.getElementById('testFocusableGroup1.node2').tabIndex = -1;
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- document.getElementById('testFocusableGroup1.node2').focus();
-
- // focus()ing a new node should overwrite a failed attempt to focusNode() an unfocusable
- // node. This verifies that DOM focus syncing is properly reenabled by FocusManager.
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup1Node2,
- );
- });
-
- test('after focus() after trying to focusNode() the same node twice returns focus()ed node', function () {
- document.getElementById('testFocusableGroup1.node2').tabIndex = -1;
- this.focusManager.registerTree(this.testFocusableGroup1);
- // Intentionally try to focus the same node twice.
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
- this.focusManager.focusNode(this.testFocusableGroup1Node1);
-
- document.getElementById('testFocusableGroup1.node2').focus();
-
- // focus()ing a new node should overwrite a failed attempt to focusNode() the same node
- // twice. This verifies that DOM focus syncing is properly reenabled by FocusManager.
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup1Node2,
- );
- });
- });
- suite('CSS classes', function () {
- test('registered root focus()ed no prev focus returns root elem has active property', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- document.getElementById('testFocusableGroup1').tabIndex = -1;
-
- document.getElementById('testFocusableGroup1').focus();
-
- const rootElem = this.testFocusableGroup1
- .getRootFocusableNode()
- .getFocusableElement();
- assert.includesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered node focus()ed no prev focus node elem has active property', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
-
- document.getElementById('testFocusableGroup1.node1').focus();
-
- const nodeElem = this.testFocusableGroup1Node1.getFocusableElement();
- assert.includesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered node focus()ed after prev node focus same tree old node elem has no focus property', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node2').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').focus();
-
- document.getElementById('testFocusableGroup1.node2').focus();
-
- const prevNodeElem =
- this.testFocusableGroup1Node1.getFocusableElement();
- assert.notIncludesClass(
- prevNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- prevNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered node focus()ed after prev node focus same tree new node elem has active property', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node2').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').focus();
-
- document.getElementById('testFocusableGroup1.node2').focus();
-
- const newNodeElem = this.testFocusableGroup1Node2.getFocusableElement();
- assert.includesClass(
- newNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- newNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered node focus()ed after prev node focus diff tree old node elem has passive property', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup2.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').focus();
-
- document.getElementById('testFocusableGroup2.node1').focus();
-
- const prevNodeElem =
- this.testFocusableGroup1Node1.getFocusableElement();
- assert.notIncludesClass(
- prevNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- prevNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered node focus()ed after prev node focus diff tree new node elem has active property', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup2.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').focus();
-
- document.getElementById('testFocusableGroup2.node1').focus();
-
- const newNodeElem = this.testFocusableGroup2Node1.getFocusableElement();
- assert.includesClass(
- newNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- newNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('registered tree root focus()ed after prev node focus diff tree new root has active property', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup2').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').focus();
-
- document.getElementById('testFocusableGroup2').focus();
-
- const rootElem = this.testFocusableGroup2
- .getRootFocusableNode()
- .getFocusableElement();
- assert.includesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('non-registered node subelement focus()ed nearest node has active property', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- document.getElementById(
- 'testFocusableGroup1.node2.unregisteredChild1',
- ).tabIndex = -1;
-
- document
- .getElementById('testFocusableGroup1.node2.unregisteredChild1')
- .focus();
-
- // The nearest node of the unregistered child element should be actively focused.
- const nodeElem = this.testFocusableGroup1Node2.getFocusableElement();
- assert.includesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('non-registered tree focus()ed has no focus', function () {
- document.getElementById('testUnregisteredFocusableGroup3').tabIndex =
- -1;
-
- document.getElementById('testUnregisteredFocusableGroup3').focus();
-
- assert.isNull(this.focusManager.getFocusedNode());
- const rootElem = document.getElementById(
- 'testUnregisteredFocusableGroup3',
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('non-registered tree node focus()ed has no focus', function () {
- document.getElementById(
- 'testUnregisteredFocusableGroup3.node1',
- ).tabIndex = -1;
-
- document
- .getElementById('testUnregisteredFocusableGroup3.node1')
- .focus();
-
- assert.isNull(this.focusManager.getFocusedNode());
- const nodeElem = document.getElementById(
- 'testUnregisteredFocusableGroup3.node1',
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('unfocusable element focus()ed after registered node focused original node has active focus', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').focus();
-
- document.getElementById('testUnfocusableElement').focus();
-
- // The original node should be unchanged, and the unregistered node should not have any
- // focus indicators.
- const nodeElem = document.getElementById('testFocusableGroup1.node1');
- const attemptedNewNodeElem = document.getElementById(
- 'testUnfocusableElement',
- );
- assert.includesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- attemptedNewNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- attemptedNewNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('unregistered tree focus()ed with no prev focus removes focus', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- document.getElementById('testFocusableGroup1').tabIndex = -1;
- document.getElementById('testFocusableGroup1').focus();
-
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- // Since the tree was unregistered it no longer has focus indicators.
- const rootElem = this.testFocusableGroup1
- .getRootFocusableNode()
- .getFocusableElement();
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('unregistered tree focus()ed with no prev focus removes focus', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').focus();
-
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- // Since the tree was unregistered it no longer has focus indicators.
- const nodeElem = this.testFocusableGroup1Node1.getFocusableElement();
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('unregistered tree focus()ed with prev node prior removes focus from removed tree', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- document.getElementById('testFocusableGroup2.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup2.node1').focus();
- document.getElementById('testFocusableGroup1.node1').focus();
-
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- // Since the tree was unregistered it no longer has focus indicators. However, the old node
- // should still have passive indication.
- const otherNodeElem =
- this.testFocusableGroup2Node1.getFocusableElement();
- const removedNodeElem =
- this.testFocusableGroup1Node1.getFocusableElement();
- assert.includesClass(
- otherNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- otherNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- removedNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- removedNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('unregistered tree focus()ed with prev node recently removes focus from removed tree', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup2.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').focus();
- document.getElementById('testFocusableGroup2.node1').focus();
-
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- // Since the tree was unregistered it no longer has focus indicators. However, the new node
- // should still have active indication.
- const otherNodeElem =
- this.testFocusableGroup2Node1.getFocusableElement();
- const removedNodeElem =
- this.testFocusableGroup1Node1.getFocusableElement();
- assert.includesClass(
- otherNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- otherNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- removedNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- removedNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('unregistered tree focus()ed with prev node after unregistering removes active indicator', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup2.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').focus();
- document.getElementById('testFocusableGroup2.node1').focus();
- this.focusManager.unregisterTree(this.testFocusableGroup1);
-
- document.getElementById('testFocusableGroup1.node1').focus();
-
- // Attempting to focus a now removed tree should remove active.
- const otherNodeElem =
- this.testFocusableGroup2Node1.getFocusableElement();
- const removedNodeElem =
- this.testFocusableGroup1Node1.getFocusableElement();
- assert.notIncludesClass(
- otherNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- otherNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- removedNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- removedNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('focus() multiple nodes in same tree with switches ensure passive focus has gone', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup2.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node2').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').focus();
- document.getElementById('testFocusableGroup2.node1').focus();
-
- document.getElementById('testFocusableGroup1.node2').focus();
-
- // When switching back to the first tree, ensure the original passive node is no longer
- // passive now that the new node is active.
- const node1 = this.testFocusableGroup1Node1.getFocusableElement();
- const node2 = this.testFocusableGroup1Node2.getFocusableElement();
- assert.notIncludesClass(
- node1.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- node2.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('focus on root, node in diff tree, then node in first tree; root should have focus gone', function () {
- this.focusManager.registerTree(this.testFocusableGroup1);
- this.focusManager.registerTree(this.testFocusableGroup2);
- document.getElementById('testFocusableGroup1').tabIndex = -1;
- document.getElementById('testFocusableGroup2.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup1').focus();
- document.getElementById('testFocusableGroup2.node1').focus();
-
- document.getElementById('testFocusableGroup1.node1').focus();
-
- const nodeElem = this.testFocusableGroup1Node1.getFocusableElement();
- const rootElem = this.testFocusableGroup1
- .getRootFocusableNode()
- .getFocusableElement();
- assert.includesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('nested tree focus()ed with no prev root has active focus', function () {
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.registerTree(this.testFocusableNestedGroup4);
- document.getElementById('testFocusableNestedGroup4').tabIndex = -1;
-
- document.getElementById('testFocusableNestedGroup4').focus();
-
- const rootElem = this.testFocusableNestedGroup4
- .getRootFocusableNode()
- .getFocusableElement();
- assert.includesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('nested tree node focus()ed with no prev focus node has active focus', function () {
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.registerTree(this.testFocusableNestedGroup4);
- document.getElementById('testFocusableNestedGroup4.node1').tabIndex =
- -1;
-
- document.getElementById('testFocusableNestedGroup4.node1').focus();
-
- const nodeElem =
- this.testFocusableNestedGroup4Node1.getFocusableElement();
- assert.includesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('nested tree node focus()ed after parent focused prev has passive node has active', function () {
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.registerTree(this.testFocusableNestedGroup4);
- document.getElementById('testFocusableGroup2.node1').tabIndex = -1;
- document.getElementById('testFocusableNestedGroup4.node1').tabIndex =
- -1;
- document.getElementById('testFocusableGroup2.node1').focus();
-
- document.getElementById('testFocusableNestedGroup4.node1').focus();
-
- const prevNodeElem =
- this.testFocusableGroup2Node1.getFocusableElement();
- const currNodeElem =
- this.testFocusableNestedGroup4Node1.getFocusableElement();
- assert.notIncludesClass(
- prevNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- prevNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- currNodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- currNodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
- });
- });
-
- /* High-level focus/defocusing tests. */
- suite('Defocusing and refocusing', function () {
- test('Defocusing actively focused root HTML tree switches to passive highlight', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusTree(this.testFocusableTree2);
- document.getElementById('testUnregisteredFocusableTree3').tabIndex = -1;
-
- document.getElementById('testUnregisteredFocusableTree3').focus();
-
- const rootNode = this.testFocusableTree2.getRootFocusableNode();
- const rootElem = rootNode.getFocusableElement();
- assert.isNull(this.focusManager.getFocusedTree());
- assert.isNull(this.focusManager.getFocusedNode());
- assert.includesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('Defocusing actively focused HTML tree node switches to passive highlight', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusNode(this.testFocusableTree2Node1);
- document.getElementById('testUnregisteredFocusableTree3').tabIndex = -1;
-
- document.getElementById('testUnregisteredFocusableTree3').focus();
-
- const nodeElem = this.testFocusableTree2Node1.getFocusableElement();
- assert.isNull(this.focusManager.getFocusedTree());
- assert.isNull(this.focusManager.getFocusedNode());
- assert.includesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('Defocusing actively focused HTML subtree node switches to passive highlight', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableNestedTree4);
- this.focusManager.focusNode(this.testFocusableNestedTree4Node1);
- document.getElementById('testUnregisteredFocusableTree3').tabIndex = -1;
-
- document.getElementById('testUnregisteredFocusableTree3').focus();
-
- const nodeElem = this.testFocusableNestedTree4Node1.getFocusableElement();
- assert.isNull(this.focusManager.getFocusedTree());
- assert.isNull(this.focusManager.getFocusedNode());
- assert.includesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('Refocusing actively focused root HTML tree restores to active highlight', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusTree(this.testFocusableTree2);
- document.getElementById('testUnregisteredFocusableTree3').tabIndex = -1;
- document.getElementById('testFocusableTree2').tabIndex = -1;
- document.getElementById('testUnregisteredFocusableTree3').focus();
-
- document.getElementById('testFocusableTree2').focus();
-
- const rootNode = this.testFocusableTree2.getRootFocusableNode();
- const rootElem = rootNode.getFocusableElement();
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableTree2,
- );
- assert.strictEqual(this.focusManager.getFocusedNode(), rootNode);
- assert.notIncludesClass(
- rootElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('Refocusing actively focused HTML tree node restores to active highlight', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.focusNode(this.testFocusableTree2Node1);
- document.getElementById('testUnregisteredFocusableTree3').tabIndex = -1;
- document.getElementById('testFocusableTree2.node1').tabIndex = -1;
- document.getElementById('testUnregisteredFocusableTree3').focus();
-
- document.getElementById('testFocusableTree2.node1').focus();
-
- const nodeElem = this.testFocusableTree2Node1.getFocusableElement();
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableTree2,
- );
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree2Node1,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('Refocusing actively focused HTML subtree node restores to active highlight', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableNestedTree4);
- this.focusManager.focusNode(this.testFocusableNestedTree4Node1);
- document.getElementById('testUnregisteredFocusableTree3').tabIndex = -1;
- document.getElementById('testFocusableNestedTree4.node1').tabIndex = -1;
- document.getElementById('testUnregisteredFocusableTree3').focus();
-
- document.getElementById('testFocusableNestedTree4.node1').focus();
-
- const nodeElem = this.testFocusableNestedTree4Node1.getFocusableElement();
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableNestedTree4,
- );
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableNestedTree4Node1,
- );
- assert.notIncludesClass(
- nodeElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
- });
-
- /* Combined HTML/SVG tree focus tests. */
-
- suite('HTML/SVG focus tree switching', function () {
- suite('Focus HTML tree then SVG tree', function () {
- test('HTML focusTree()ed then SVG focusTree()ed correctly updates getFocusedTree() and indicators', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusTree(this.testFocusableTree2);
-
- this.focusManager.focusTree(this.testFocusableGroup2);
-
- const prevElem = this.testFocusableTree2
- .getRootFocusableNode()
- .getFocusableElement();
- const currElem = this.testFocusableGroup2
- .getRootFocusableNode()
- .getFocusableElement();
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableGroup2,
- );
- assert.strictEqual(document.activeElement, currElem);
- assert.includesClass(
- currElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- currElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- prevElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- prevElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('HTML focusTree()ed then SVG focusNode()ed correctly updates getFocusedNode() and indicators', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusTree(this.testFocusableTree2);
-
- this.focusManager.focusNode(this.testFocusableGroup2Node1);
-
- const prevElem = this.testFocusableTree2
- .getRootFocusableNode()
- .getFocusableElement();
- const currElem = this.testFocusableGroup2Node1.getFocusableElement();
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup2Node1,
- );
- assert.strictEqual(document.activeElement, currElem);
- assert.includesClass(
- currElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- currElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- prevElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- prevElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('HTML focusTree()ed then SVG DOM focus()ed correctly updates getFocusedNode() and indicators', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusTree(this.testFocusableTree2);
- document.getElementById('testFocusableGroup2.node1').tabIndex = -1;
-
- document.getElementById('testFocusableGroup2.node1').focus();
-
- const prevElem = this.testFocusableTree2
- .getRootFocusableNode()
- .getFocusableElement();
- const currElem = this.testFocusableGroup2Node1.getFocusableElement();
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup2Node1,
- );
- assert.strictEqual(document.activeElement, currElem);
- assert.includesClass(
- currElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- currElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- prevElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- prevElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('HTML focusNode()ed then SVG focusTree()ed correctly updates getFocusedTree() and indicators', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableTree2Node1);
-
- this.focusManager.focusTree(this.testFocusableGroup2);
-
- const prevElem = this.testFocusableTree2Node1.getFocusableElement();
- const currElem = this.testFocusableGroup2
- .getRootFocusableNode()
- .getFocusableElement();
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableGroup2,
- );
- assert.strictEqual(document.activeElement, currElem);
- assert.includesClass(
- currElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- currElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- prevElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- prevElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('HTML focusNode()ed then SVG focusNode()ed correctly updates getFocusedNode() and indicators', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableTree2Node1);
-
- this.focusManager.focusNode(this.testFocusableGroup2Node1);
-
- const prevElem = this.testFocusableTree2Node1.getFocusableElement();
- const currElem = this.testFocusableGroup2Node1.getFocusableElement();
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup2Node1,
- );
- assert.strictEqual(document.activeElement, currElem);
- assert.includesClass(
- currElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- currElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- prevElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- prevElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('HTML focusNode()ed then SVG DOM focus()ed correctly updates getFocusedNode() and indicators', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableTree2Node1);
- document.getElementById('testFocusableGroup2.node1').tabIndex = -1;
-
- document.getElementById('testFocusableGroup2.node1').focus();
-
- const prevElem = this.testFocusableTree2Node1.getFocusableElement();
- const currElem = this.testFocusableGroup2Node1.getFocusableElement();
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup2Node1,
- );
- assert.strictEqual(document.activeElement, currElem);
- assert.includesClass(
- currElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- currElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- prevElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- prevElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('HTML DOM focus()ed then SVG focusTree()ed correctly updates getFocusedTree() and indicators', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- document.getElementById('testFocusableTree2.node1').tabIndex = -1;
- document.getElementById('testFocusableTree2.node1').focus();
-
- this.focusManager.focusTree(this.testFocusableGroup2);
-
- const prevElem = this.testFocusableTree2Node1.getFocusableElement();
- const currElem = this.testFocusableGroup2
- .getRootFocusableNode()
- .getFocusableElement();
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableGroup2,
- );
- assert.strictEqual(document.activeElement, currElem);
- assert.includesClass(
- currElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- currElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- prevElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- prevElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('HTML DOM focus()ed then SVG focusNode()ed correctly updates getFocusedNode() and indicators', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- document.getElementById('testFocusableTree2.node1').tabIndex = -1;
- document.getElementById('testFocusableTree2.node1').focus();
-
- this.focusManager.focusNode(this.testFocusableGroup2Node1);
-
- const prevElem = this.testFocusableTree2Node1.getFocusableElement();
- const currElem = this.testFocusableGroup2Node1.getFocusableElement();
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup2Node1,
- );
- assert.strictEqual(document.activeElement, currElem);
- assert.includesClass(
- currElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- currElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- prevElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- prevElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('HTML DOM focus()ed then SVG DOM focus()ed correctly updates getFocusedNode() and indicators', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- document.getElementById('testFocusableTree2.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup2.node1').tabIndex = -1;
- document.getElementById('testFocusableTree2.node1').focus();
-
- document.getElementById('testFocusableGroup2.node1').focus();
-
- const prevElem = this.testFocusableTree2Node1.getFocusableElement();
- const currElem = this.testFocusableGroup2Node1.getFocusableElement();
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup2Node1,
- );
- assert.strictEqual(document.activeElement, currElem);
- assert.includesClass(
- currElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- currElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- prevElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- prevElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
- });
- suite('Focus SVG tree then HTML tree', function () {
- test('SVG focusTree()ed then HTML focusTree()ed correctly updates getFocusedTree() and indicators', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusTree(this.testFocusableGroup2);
-
- this.focusManager.focusTree(this.testFocusableTree2);
-
- const prevElem = this.testFocusableGroup2
- .getRootFocusableNode()
- .getFocusableElement();
- const currElem = this.testFocusableTree2
- .getRootFocusableNode()
- .getFocusableElement();
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableTree2,
- );
- assert.strictEqual(document.activeElement, currElem);
- assert.includesClass(
- currElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- currElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- prevElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- prevElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('SVG focusTree()ed then HTML focusNode()ed correctly updates getFocusedNode() and indicators', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusTree(this.testFocusableGroup2);
-
- this.focusManager.focusNode(this.testFocusableTree2Node1);
-
- const prevElem = this.testFocusableGroup2
- .getRootFocusableNode()
- .getFocusableElement();
- const currElem = this.testFocusableTree2Node1.getFocusableElement();
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree2Node1,
- );
- assert.strictEqual(document.activeElement, currElem);
- assert.includesClass(
- currElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- currElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- prevElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- prevElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('SVG focusTree()ed then HTML DOM focus()ed correctly updates getFocusedNode() and indicators', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusTree(this.testFocusableGroup2);
- document.getElementById('testFocusableTree2.node1').tabIndex = -1;
-
- document.getElementById('testFocusableTree2.node1').focus();
-
- const prevElem = this.testFocusableGroup2
- .getRootFocusableNode()
- .getFocusableElement();
- const currElem = this.testFocusableTree2Node1.getFocusableElement();
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree2Node1,
- );
- assert.strictEqual(document.activeElement, currElem);
- assert.includesClass(
- currElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- currElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- prevElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- prevElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('SVG focusNode()ed then HTML focusTree()ed correctly updates getFocusedTree() and indicators', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableGroup2Node1);
-
- this.focusManager.focusTree(this.testFocusableTree2);
-
- const prevElem = this.testFocusableGroup2Node1.getFocusableElement();
- const currElem = this.testFocusableTree2
- .getRootFocusableNode()
- .getFocusableElement();
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableTree2,
- );
- assert.strictEqual(document.activeElement, currElem);
- assert.includesClass(
- currElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- currElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- prevElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- prevElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('SVG focusNode()ed then HTML focusNode()ed correctly updates getFocusedNode() and indicators', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableGroup2Node1);
-
- this.focusManager.focusNode(this.testFocusableTree2Node1);
-
- const prevElem = this.testFocusableGroup2Node1.getFocusableElement();
- const currElem = this.testFocusableTree2Node1.getFocusableElement();
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree2Node1,
- );
- assert.strictEqual(document.activeElement, currElem);
- assert.includesClass(
- currElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- currElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- prevElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- prevElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('SVG focusNode()ed then HTML DOM focus()ed correctly updates getFocusedNode() and indicators', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableGroup2Node1);
- document.getElementById('testFocusableTree2.node1').tabIndex = -1;
-
- document.getElementById('testFocusableTree2.node1').focus();
-
- const prevElem = this.testFocusableGroup2Node1.getFocusableElement();
- const currElem = this.testFocusableTree2Node1.getFocusableElement();
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree2Node1,
- );
- assert.strictEqual(document.activeElement, currElem);
- assert.includesClass(
- currElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- currElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- prevElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- prevElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('SVG DOM focus()ed then HTML focusTree()ed correctly updates getFocusedTree() and indicators', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- document.getElementById('testFocusableGroup2.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup2.node1').focus();
-
- this.focusManager.focusTree(this.testFocusableTree2);
-
- const prevElem = this.testFocusableGroup2Node1.getFocusableElement();
- const currElem = this.testFocusableTree2
- .getRootFocusableNode()
- .getFocusableElement();
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableTree2,
- );
- assert.strictEqual(document.activeElement, currElem);
- assert.includesClass(
- currElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- currElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- prevElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- prevElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('SVG DOM focus()ed then HTML focusNode()ed correctly updates getFocusedNode() and indicators', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- document.getElementById('testFocusableGroup2.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup2.node1').focus();
-
- this.focusManager.focusNode(this.testFocusableTree2Node1);
-
- const prevElem = this.testFocusableGroup2Node1.getFocusableElement();
- const currElem = this.testFocusableTree2Node1.getFocusableElement();
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree2Node1,
- );
- assert.strictEqual(document.activeElement, currElem);
- assert.includesClass(
- currElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- currElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- prevElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- prevElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('SVG DOM focus()ed then HTML DOM focus()ed correctly updates getFocusedNode() and indicators', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- document.getElementById('testFocusableGroup2.node1').tabIndex = -1;
- document.getElementById('testFocusableTree2.node1').tabIndex = -1;
- document.getElementById('testFocusableGroup2.node1').focus();
-
- document.getElementById('testFocusableTree2.node1').focus();
-
- const prevElem = this.testFocusableGroup2Node1.getFocusableElement();
- const currElem = this.testFocusableTree2Node1.getFocusableElement();
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree2Node1,
- );
- assert.strictEqual(document.activeElement, currElem);
- assert.includesClass(
- currElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- currElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.notIncludesClass(
- prevElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.includesClass(
- prevElem.classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
- });
- });
-
- /* Ephemeral focus tests. */
-
- suite('takeEphemeralFocus()', function () {
- setup(function () {
- // Ensure ephemeral-specific elements are focusable.
- document.getElementById('nonTreeElementForEphemeralFocus').tabIndex = -1;
- document.getElementById('nonTreeGroupForEphemeralFocus').tabIndex = -1;
- });
- teardown(function () {
- // Ensure ephemeral-specific elements have their tab indexes reset for a clean state.
- document
- .getElementById('nonTreeElementForEphemeralFocus')
- .removeAttribute('tabindex');
- document
- .getElementById('nonTreeGroupForEphemeralFocus')
- .removeAttribute('tabindex');
- });
-
- test('with no focused node does not change states', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
-
- const ephemeralElement = document.getElementById(
- 'nonTreeElementForEphemeralFocus',
- );
- this.focusManager.takeEphemeralFocus(ephemeralElement);
-
- // Taking focus without an existing node having focus should change no focus indicators.
- const activeElems = Array.from(
- document.querySelectorAll(ACTIVE_FOCUS_NODE_CSS_SELECTOR),
- );
- const passiveElems = Array.from(
- document.querySelectorAll(PASSIVE_FOCUS_NODE_CSS_SELECTOR),
- );
- assert.isEmpty(activeElems);
- assert.isEmpty(passiveElems);
- });
-
- test('with focused node changes focused node to passive', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableTree2Node1);
-
- const ephemeralElement = document.getElementById(
- 'nonTreeElementForEphemeralFocus',
- );
- this.focusManager.takeEphemeralFocus(ephemeralElement);
-
- // Taking focus without an existing node having focus should change no focus indicators.
- const activeElems = Array.from(
- document.querySelectorAll(ACTIVE_FOCUS_NODE_CSS_SELECTOR),
- );
- const passiveElems = Array.from(
- document.querySelectorAll(PASSIVE_FOCUS_NODE_CSS_SELECTOR),
- );
- assert.isEmpty(activeElems);
- assert.strictEqual(passiveElems.length, 1);
- assert.includesClass(
- this.testFocusableTree2Node1.getFocusableElement().classList,
- FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- });
-
- test('focuses provided HTML element', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
-
- const ephemeralElement = document.getElementById(
- 'nonTreeElementForEphemeralFocus',
- );
- this.focusManager.takeEphemeralFocus(ephemeralElement);
-
- assert.strictEqual(document.activeElement, ephemeralElement);
- });
-
- test('focuses provided SVG element', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
-
- const ephemeralElement = document.getElementById(
- 'nonTreeGroupForEphemeralFocus',
- );
- this.focusManager.takeEphemeralFocus(ephemeralElement);
-
- assert.strictEqual(document.activeElement, ephemeralElement);
- });
-
- test('twice for without finishing previous throws error', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- const ephemeralGroupElement = document.getElementById(
- 'nonTreeGroupForEphemeralFocus',
- );
- const ephemeralDivElement = document.getElementById(
- 'nonTreeElementForEphemeralFocus',
- );
- this.focusManager.takeEphemeralFocus(ephemeralGroupElement);
-
- const errorMsgRegex =
- /Attempted to take ephemeral focus when it's already held+?/;
- assert.throws(
- () => this.focusManager.takeEphemeralFocus(ephemeralDivElement),
- errorMsgRegex,
- );
- });
-
- test('then focusTree() changes getFocusedTree() but not active state', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusTree(this.testFocusableTree2);
- const ephemeralElement = document.getElementById(
- 'nonTreeGroupForEphemeralFocus',
- );
- this.focusManager.takeEphemeralFocus(ephemeralElement);
-
- this.focusManager.focusTree(this.testFocusableGroup2);
-
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableGroup2,
- );
- const activeElems = Array.from(
- document.querySelectorAll(ACTIVE_FOCUS_NODE_CSS_SELECTOR),
- );
- assert.isEmpty(activeElems);
- });
-
- test('then focusNode() changes getFocusedNode() but not active state', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableTree2Node1);
- const ephemeralElement = document.getElementById(
- 'nonTreeGroupForEphemeralFocus',
- );
- this.focusManager.takeEphemeralFocus(ephemeralElement);
-
- this.focusManager.focusNode(this.testFocusableGroup2Node1);
-
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup2Node1,
- );
- const activeElems = Array.from(
- document.querySelectorAll(ACTIVE_FOCUS_NODE_CSS_SELECTOR),
- );
- assert.isEmpty(activeElems);
- });
-
- test('then DOM refocus changes getFocusedNode() but not active state', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableTree2Node1);
- document.getElementById('testFocusableGroup2.node1').tabIndex = -1;
- const ephemeralElement = document.getElementById(
- 'nonTreeGroupForEphemeralFocus',
- );
- this.focusManager.takeEphemeralFocus(ephemeralElement);
-
- // Force focus to change via the DOM.
- document.getElementById('testFocusableGroup2.node1').focus();
-
- // The focus() state change will affect getFocusedNode() but it will not cause the node to now
- // be active.
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup2Node1,
- );
- const activeElems = Array.from(
- document.querySelectorAll(ACTIVE_FOCUS_NODE_CSS_SELECTOR),
- );
- assert.isEmpty(activeElems);
- });
-
- test('then finish ephemeral callback with no node does not change indicators', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- const ephemeralElement = document.getElementById(
- 'nonTreeElementForEphemeralFocus',
- );
- const finishFocusCallback =
- this.focusManager.takeEphemeralFocus(ephemeralElement);
-
- finishFocusCallback();
-
- // Finishing ephemeral focus without a previously focused node should not change indicators.
- const activeElems = Array.from(
- document.querySelectorAll(ACTIVE_FOCUS_NODE_CSS_SELECTOR),
- );
- const passiveElems = Array.from(
- document.querySelectorAll(PASSIVE_FOCUS_NODE_CSS_SELECTOR),
- );
- assert.isEmpty(activeElems);
- assert.isEmpty(passiveElems);
- });
-
- test('again after finishing previous empheral focus should focus new element', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- const ephemeralGroupElement = document.getElementById(
- 'nonTreeGroupForEphemeralFocus',
- );
- const ephemeralDivElement = document.getElementById(
- 'nonTreeElementForEphemeralFocus',
- );
- const finishFocusCallback = this.focusManager.takeEphemeralFocus(
- ephemeralGroupElement,
- );
-
- finishFocusCallback();
- this.focusManager.takeEphemeralFocus(ephemeralDivElement);
-
- // An exception should not be thrown and the new element should receive focus.
- assert.strictEqual(document.activeElement, ephemeralDivElement);
- });
-
- test('calling ephemeral callback twice throws error', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- const ephemeralElement = document.getElementById(
- 'nonTreeElementForEphemeralFocus',
- );
- const finishFocusCallback =
- this.focusManager.takeEphemeralFocus(ephemeralElement);
- finishFocusCallback();
-
- const errorMsgRegex =
- /Attempted to finish ephemeral focus twice for element+?/;
- assert.throws(() => finishFocusCallback(), errorMsgRegex);
- });
-
- test('then finish ephemeral callback should restore focused node', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableTree2Node1);
- const ephemeralElement = document.getElementById(
- 'nonTreeGroupForEphemeralFocus',
- );
- const finishFocusCallback =
- this.focusManager.takeEphemeralFocus(ephemeralElement);
-
- finishFocusCallback();
-
- // The original focused node should be restored.
- const nodeElem = this.testFocusableTree2Node1.getFocusableElement();
- const activeElems = Array.from(
- document.querySelectorAll(ACTIVE_FOCUS_NODE_CSS_SELECTOR),
- );
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableTree2Node1,
- );
- assert.strictEqual(activeElems.length, 1);
- assert.includesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.strictEqual(document.activeElement, nodeElem);
- });
-
- test('then focusTree() and finish ephemeral callback correctly sets new active state', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusTree(this.testFocusableTree2);
- const ephemeralElement = document.getElementById(
- 'nonTreeGroupForEphemeralFocus',
- );
- const finishFocusCallback =
- this.focusManager.takeEphemeralFocus(ephemeralElement);
-
- this.focusManager.focusTree(this.testFocusableGroup2);
- finishFocusCallback();
-
- // The tree's root should now be the active element since focus changed between the start and
- // end of the ephemeral flow.
- const rootElem = this.testFocusableGroup2
- .getRootFocusableNode()
- .getFocusableElement();
- const activeElems = Array.from(
- document.querySelectorAll(ACTIVE_FOCUS_NODE_CSS_SELECTOR),
- );
- assert.strictEqual(
- this.focusManager.getFocusedTree(),
- this.testFocusableGroup2,
- );
- assert.strictEqual(activeElems.length, 1);
- assert.includesClass(
- rootElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.strictEqual(document.activeElement, rootElem);
- });
-
- test('then focusNode() and finish ephemeral callback correctly sets new active state', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableTree2Node1);
- const ephemeralElement = document.getElementById(
- 'nonTreeGroupForEphemeralFocus',
- );
- const finishFocusCallback =
- this.focusManager.takeEphemeralFocus(ephemeralElement);
-
- this.focusManager.focusNode(this.testFocusableGroup2Node1);
- finishFocusCallback();
-
- // The tree's root should now be the active element since focus changed between the start and
- // end of the ephemeral flow.
- const nodeElem = this.testFocusableGroup2Node1.getFocusableElement();
- const activeElems = Array.from(
- document.querySelectorAll(ACTIVE_FOCUS_NODE_CSS_SELECTOR),
- );
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup2Node1,
- );
- assert.strictEqual(activeElems.length, 1);
- assert.includesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.strictEqual(document.activeElement, nodeElem);
- });
-
- test('then DOM focus change and finish ephemeral callback correctly sets new active state', function () {
- this.focusManager.registerTree(this.testFocusableTree2);
- this.focusManager.registerTree(this.testFocusableGroup2);
- this.focusManager.focusNode(this.testFocusableTree2Node1);
- document.getElementById('testFocusableGroup2.node1').tabIndex = -1;
- const ephemeralElement = document.getElementById(
- 'nonTreeGroupForEphemeralFocus',
- );
- const finishFocusCallback =
- this.focusManager.takeEphemeralFocus(ephemeralElement);
-
- document.getElementById('testFocusableGroup2.node1').focus();
- finishFocusCallback();
-
- // The tree's root should now be the active element since focus changed between the start and
- // end of the ephemeral flow.
- const nodeElem = this.testFocusableGroup2Node1.getFocusableElement();
- const activeElems = Array.from(
- document.querySelectorAll(ACTIVE_FOCUS_NODE_CSS_SELECTOR),
- );
- assert.strictEqual(
- this.focusManager.getFocusedNode(),
- this.testFocusableGroup2Node1,
- );
- assert.strictEqual(activeElems.length, 1);
- assert.includesClass(
- nodeElem.classList,
- FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
- );
- assert.strictEqual(document.activeElement, nodeElem);
- });
- });
-});
diff --git a/packages/blockly/tests/mocha/focus_manager_test.ts b/packages/blockly/tests/mocha/focus_manager_test.ts
new file mode 100644
index 00000000000..e0c20f7b8f4
--- /dev/null
+++ b/packages/blockly/tests/mocha/focus_manager_test.ts
@@ -0,0 +1,6046 @@
+/**
+ * @license
+ * Copyright 2025 Google LLC
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+import * as Blockly from '#core/blockly.js';
+import {assert} from 'chai';
+import sinon from 'sinon';
+import {FocusableTreeImpl} from './test_helpers/focusable_tree.ts';
+import {
+ sharedTestSetup,
+ sharedTestTeardown,
+} from './test_helpers/setup_teardown.js';
+
+suite('FocusManager', function () {
+ const ACTIVE_FOCUS_NODE_CSS_SELECTOR = `.${Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME}`;
+ const PASSIVE_FOCUS_NODE_CSS_SELECTOR = `.${Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME}`;
+
+ let focusManager: Blockly.FocusManager;
+ let allFocusableTrees: FocusableTreeImpl[];
+ let allFocusableNodes: Blockly.IFocusableNode[];
+
+ function createFocusableTree(
+ rootElementId: string,
+ nestedTrees?: FocusableTreeImpl[],
+ ): InstanceType {
+ const node = document.getElementById(rootElementId);
+ assert.exists(node);
+ const tree = new FocusableTreeImpl(node, nestedTrees || []);
+ allFocusableTrees.push(tree);
+ return tree;
+ }
+
+ function createFocusableNode(tree: FocusableTreeImpl, elementId: string) {
+ const element = document.getElementById(elementId);
+ assert.exists(element);
+ const node = tree.addNode(element);
+ allFocusableNodes.push(node);
+ return node;
+ }
+
+ let testFocusableTree1: FocusableTreeImpl;
+ let testFocusableTree1Node1: Blockly.IFocusableNode;
+ let testFocusableTree1Node1Child1: Blockly.IFocusableNode;
+ let testFocusableTree1Node2: Blockly.IFocusableNode;
+
+ let testFocusableNestedTree4: FocusableTreeImpl;
+ let testFocusableNestedTree4Node1: Blockly.IFocusableNode;
+ let testFocusableNestedTree5: FocusableTreeImpl;
+
+ let testFocusableTree2: FocusableTreeImpl;
+ let testFocusableTree2Node1: Blockly.IFocusableNode;
+
+ let testFocusableGroup1: FocusableTreeImpl;
+ let testFocusableGroup1Node1: Blockly.IFocusableNode;
+ let testFocusableGroup1Node1Child1: Blockly.IFocusableNode;
+ let testFocusableGroup1Node2: Blockly.IFocusableNode;
+ let testFocusableNestedGroup4: FocusableTreeImpl;
+ let testFocusableNestedGroup4Node1: Blockly.IFocusableNode;
+ let testFocusableGroup2: FocusableTreeImpl;
+ let testFocusableGroup2Node1: Blockly.IFocusableNode;
+ let globalDocumentEventListeners: {type: string; listener: () => void}[];
+
+ setup(function (this: Mocha.Context) {
+ ({globalDocumentEventListeners} = sharedTestSetup.call(this));
+ focusManager = Blockly.getFocusManager();
+
+ allFocusableTrees = [];
+ allFocusableNodes = [];
+ testFocusableTree1 = createFocusableTree('testFocusableTree1');
+ testFocusableTree1Node1 = createFocusableNode(
+ testFocusableTree1,
+ 'testFocusableTree1.node1',
+ );
+ testFocusableTree1Node1Child1 = createFocusableNode(
+ testFocusableTree1,
+ 'testFocusableTree1.node1.child1',
+ );
+ testFocusableTree1Node2 = createFocusableNode(
+ testFocusableTree1,
+ 'testFocusableTree1.node2',
+ );
+ testFocusableNestedTree4 = createFocusableTree('testFocusableNestedTree4');
+ testFocusableNestedTree4Node1 = createFocusableNode(
+ testFocusableNestedTree4,
+ 'testFocusableNestedTree4.node1',
+ );
+ testFocusableNestedTree5 = createFocusableTree('testFocusableNestedTree5');
+ testFocusableTree2 = createFocusableTree('testFocusableTree2', [
+ testFocusableNestedTree4,
+ testFocusableNestedTree5,
+ ]);
+ testFocusableTree2Node1 = createFocusableNode(
+ testFocusableTree2,
+ 'testFocusableTree2.node1',
+ );
+
+ testFocusableGroup1 = createFocusableTree('testFocusableGroup1');
+ testFocusableGroup1Node1 = createFocusableNode(
+ testFocusableGroup1,
+ 'testFocusableGroup1.node1',
+ );
+ testFocusableGroup1Node1Child1 = createFocusableNode(
+ testFocusableGroup1,
+ 'testFocusableGroup1.node1.child1',
+ );
+ testFocusableGroup1Node2 = createFocusableNode(
+ testFocusableGroup1,
+ 'testFocusableGroup1.node2',
+ );
+ testFocusableNestedGroup4 = createFocusableTree(
+ 'testFocusableNestedGroup4',
+ );
+ testFocusableNestedGroup4Node1 = createFocusableNode(
+ testFocusableNestedGroup4,
+ 'testFocusableNestedGroup4.node1',
+ );
+ testFocusableGroup2 = createFocusableTree('testFocusableGroup2', [
+ testFocusableNestedGroup4,
+ ]);
+ testFocusableGroup2Node1 = createFocusableNode(
+ testFocusableGroup2,
+ 'testFocusableGroup2.node1',
+ );
+ });
+
+ teardown(function (this: Mocha.Context) {
+ sharedTestTeardown.call(this);
+
+ // Ensure all node CSS styles are reset so that state isn't leaked between tests.
+ const activeElems = document.querySelectorAll(
+ ACTIVE_FOCUS_NODE_CSS_SELECTOR,
+ );
+ const passiveElems = document.querySelectorAll(
+ PASSIVE_FOCUS_NODE_CSS_SELECTOR,
+ );
+ for (const elem of activeElems) {
+ elem.classList.remove(
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ }
+ for (const elem of passiveElems) {
+ elem.classList.remove(
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ }
+
+ // Ensure any set tab indexes are properly reset between tests.
+ for (const tree of allFocusableTrees) {
+ tree
+ .getRootFocusableNode()
+ .getFocusableElement()
+ .removeAttribute('tabindex');
+ }
+ for (const node of allFocusableNodes) {
+ node.getFocusableElement().removeAttribute('tabindex');
+ }
+ allFocusableTrees = [];
+ allFocusableNodes = [];
+
+ // Reset the current active element.
+ document.body.focus();
+ });
+
+ function assertIncludesClass(classList: DOMTokenList, className: string) {
+ assert.isTrue(
+ classList.contains(className),
+ 'Expected class list to include: ' + className,
+ );
+ }
+
+ function assertNotIncludesClass(classList: DOMTokenList, className: string) {
+ assert.isFalse(
+ classList.contains(className),
+ 'Expected class list to not include: ' + className,
+ );
+ }
+
+ /* Basic lifecycle tests. */
+
+ suite('registerTree()', function () {
+ test('once does not throw', function () {
+ focusManager.registerTree(testFocusableTree1);
+
+ // The test should pass due to no exception being thrown.
+ });
+
+ test('twice for same tree throws error', function () {
+ focusManager.registerTree(testFocusableTree1);
+
+ const errorMsgRegex =
+ /Attempted to re-register already registered tree.+?/;
+ assert.throws(
+ () => focusManager.registerTree(testFocusableTree1),
+ errorMsgRegex,
+ );
+ });
+
+ test('twice with different trees does not throw', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableGroup1);
+
+ // The test shouldn't throw since two different trees were registered.
+ });
+
+ test('register after an unregister does not throw', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.unregisterTree(testFocusableTree1);
+
+ focusManager.registerTree(testFocusableTree1);
+
+ // The second register should not fail since the tree was previously unregistered.
+ });
+
+ test('for tree with missing ID throws error', function () {
+ const rootNode = testFocusableTree1.getRootFocusableNode();
+ const rootElem = rootNode.getFocusableElement();
+ const oldId = rootElem.id;
+ rootElem.removeAttribute('id');
+
+ const errorMsgRegex =
+ /Attempting to register a tree with a root element that has an invalid ID.+?/;
+ assert.throws(
+ () => focusManager.registerTree(testFocusableTree1),
+ errorMsgRegex,
+ );
+ // Restore the ID for other tests.
+ rootElem.id = oldId;
+ });
+
+ test('for tree with null ID throws error', function () {
+ const rootNode = testFocusableTree1.getRootFocusableNode();
+ const rootElem = rootNode.getFocusableElement();
+ const oldId = rootElem.id;
+ rootElem.removeAttribute('id');
+
+ const errorMsgRegex =
+ /Attempting to register a tree with a root element that has an invalid ID.+?/;
+ assert.throws(
+ () => focusManager.registerTree(testFocusableTree1),
+ errorMsgRegex,
+ );
+ // Restore the ID for other tests.
+ rootElem.id = oldId;
+ });
+
+ test('for tree with empty throws error', function () {
+ const rootNode = testFocusableTree1.getRootFocusableNode();
+ const rootElem = rootNode.getFocusableElement();
+ const oldId = rootElem.id;
+ rootElem.setAttribute('id', '');
+
+ const errorMsgRegex =
+ /Attempting to register a tree with a root element that has an invalid ID.+?/;
+ assert.throws(
+ () => focusManager.registerTree(testFocusableTree1),
+ errorMsgRegex,
+ );
+ // Restore the ID for other tests.
+ rootElem.id = oldId;
+ });
+
+ test('for unmanaged tree does not overwrite tab index', function () {
+ focusManager.registerTree(testFocusableTree1, false);
+
+ const rootNode = testFocusableTree1.getRootFocusableNode();
+ const rootElem = rootNode.getFocusableElement();
+ assert.isNull(rootElem.getAttribute('tabindex'));
+ });
+
+ test('for unmanaged tree with custom tab index does not overwrite tab index', function () {
+ const rootNode = testFocusableTree1.getRootFocusableNode();
+ const rootElem = rootNode.getFocusableElement();
+ rootElem?.setAttribute('tabindex', '-1');
+
+ focusManager.registerTree(testFocusableTree1, false);
+
+ // The custom tab index shouldn't be overwritten for an unmanaged tree.
+ assert.strictEqual(rootElem.getAttribute('tabindex'), '-1');
+ });
+
+ test('for managed tree overwrites root tab index to be tab navigable', function () {
+ focusManager.registerTree(testFocusableTree1, true);
+
+ const rootNode = testFocusableTree1.getRootFocusableNode();
+ const rootElem = rootNode.getFocusableElement();
+ assert.strictEqual(rootElem.getAttribute('tabindex'), '0');
+ });
+
+ test('for managed tree with custom tab index overwrites root tab index to be tab navigable', function () {
+ const rootNode = testFocusableTree1.getRootFocusableNode();
+ const rootElem = rootNode.getFocusableElement();
+ rootElem?.setAttribute('tabindex', '-1');
+
+ focusManager.registerTree(testFocusableTree1, true);
+
+ // A custom tab index should be overwritten for a managed tree.
+ assert.strictEqual(rootElem.getAttribute('tabindex'), '0');
+ });
+ });
+
+ suite('unregisterTree()', function () {
+ test('for not yet registered tree throws', function () {
+ const errorMsgRegex = /Attempted to unregister not registered tree.+?/;
+ assert.throws(
+ () => focusManager.unregisterTree(testFocusableTree1),
+ errorMsgRegex,
+ );
+ });
+
+ test('for registered tree does not throw', function () {
+ focusManager.registerTree(testFocusableTree1);
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ // Unregistering a registered tree should not fail.
+ });
+
+ test('twice for registered tree throws', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.unregisterTree(testFocusableTree1);
+
+ const errorMsgRegex = /Attempted to unregister not registered tree.+?/;
+ assert.throws(
+ () => focusManager.unregisterTree(testFocusableTree1),
+ errorMsgRegex,
+ );
+ });
+
+ test('for unmanaged tree with custom tab index does not change tab index', function () {
+ focusManager.registerTree(testFocusableTree1, false);
+ const rootNode = testFocusableTree1.getRootFocusableNode();
+ const rootElem = rootNode.getFocusableElement();
+ rootElem?.setAttribute('tabindex', '-1');
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ // Unregistering an unmanaged tree shouldn't change its tab index.
+ assert.strictEqual(rootElem.getAttribute('tabindex'), '-1');
+ });
+
+ test('for managed tree removes tab index', function () {
+ focusManager.registerTree(testFocusableTree1, true);
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ // Unregistering a managed tree should remove its tab index.
+ const rootNode = testFocusableTree1.getRootFocusableNode();
+ const rootElem = rootNode.getFocusableElement();
+ assert.isNull(rootElem.getAttribute('tabindex'));
+ });
+
+ test('for managed tree with custom tab index removes tab index', function () {
+ focusManager.registerTree(testFocusableTree1, true);
+ const rootNode = testFocusableTree1.getRootFocusableNode();
+ const rootElem = rootNode.getFocusableElement();
+ rootElem?.setAttribute('tabindex', '-1');
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ // Unregistering a managed tree should remove its tab index.
+ assert.isNull(rootElem.getAttribute('tabindex'));
+ });
+ });
+
+ suite('isRegistered()', function () {
+ test('for not registered tree returns false', function () {
+ const isRegistered = focusManager.isRegistered(testFocusableTree1);
+
+ assert.isFalse(isRegistered);
+ });
+
+ test('for registered tree returns true', function () {
+ focusManager.registerTree(testFocusableTree1);
+
+ const isRegistered = focusManager.isRegistered(testFocusableTree1);
+
+ assert.isTrue(isRegistered);
+ });
+
+ test('for unregistered tree returns false', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.unregisterTree(testFocusableTree1);
+
+ const isRegistered = focusManager.isRegistered(testFocusableTree1);
+
+ assert.isFalse(isRegistered);
+ });
+
+ test('for re-registered tree returns true', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.unregisterTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree1);
+
+ const isRegistered = focusManager.isRegistered(testFocusableTree1);
+
+ assert.isTrue(isRegistered);
+ });
+
+ test('for unregistered tree with other registered tree returns false', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.unregisterTree(testFocusableTree1);
+
+ const isRegistered = focusManager.isRegistered(testFocusableTree1);
+
+ assert.isFalse(isRegistered);
+ });
+ });
+
+ suite('getFocusedTree()', function () {
+ test('by default returns null', function () {
+ const focusedTree = focusManager.getFocusedTree();
+
+ assert.isNull(focusedTree);
+ });
+ });
+
+ suite('getFocusedNode()', function () {
+ test('by default returns null', function () {
+ const focusedNode = focusManager.getFocusedNode();
+
+ assert.isNull(focusedNode);
+ });
+
+ test('after focusing unfocusable node returns null', function () {
+ testFocusableTree1Node1.canBeFocused = () => false;
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ const focusedNode = focusManager.getFocusedNode();
+
+ // Unfocusable nodes should not be focused.
+ assert.isNull(focusedNode);
+ });
+ });
+
+ suite('getPreviouslyFocusedNode()', function () {
+ test('by default returns null', function () {
+ assert.isNull(focusManager.getPreviouslyFocusedNode());
+ });
+
+ test('after losing focus to untracked element returns prior node', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ document.body.focus();
+
+ assert.isNull(focusManager.getFocusedNode());
+ assert.strictEqual(
+ focusManager.getPreviouslyFocusedNode(),
+ testFocusableTree1Node1,
+ );
+ });
+ });
+
+ suite('focusTree()', function () {
+ test('for not registered tree throws', function () {
+ const errorMsgRegex = /Attempted to focus unregistered tree.+?/;
+ assert.throws(
+ () => focusManager.focusTree(testFocusableTree1),
+ errorMsgRegex,
+ );
+ });
+
+ test('for unregistered tree throws', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.unregisterTree(testFocusableTree1);
+
+ const errorMsgRegex = /Attempted to focus unregistered tree.+?/;
+ assert.throws(
+ () => focusManager.focusTree(testFocusableTree1),
+ errorMsgRegex,
+ );
+ });
+ });
+
+ test('unfocused node does not have a tab index by default', function () {
+ const elem = testFocusableTree1Node1.getFocusableElement();
+
+ // This is slightly testing the test setup, but it acts as a precondition sanity test for the
+ // other tab index tests below. Important: 'getAttribute' is used here since direct access to
+ // 'tabIndex' can default the value returned even when the tab index isn't set.
+ assert.isNull(elem.getAttribute('tabindex'));
+ });
+
+ suite('focusNode()', function () {
+ test('for not registered node throws', function () {
+ const errorMsgRegex = /Attempted to focus unregistered node.+?/;
+ assert.throws(
+ () => focusManager.focusNode(testFocusableTree1Node1),
+ errorMsgRegex,
+ );
+ });
+
+ test('for unregistered node throws', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.unregisterTree(testFocusableTree1);
+
+ const errorMsgRegex = /Attempted to focus unregistered node.+?/;
+ assert.throws(
+ () => focusManager.focusNode(testFocusableTree1Node1),
+ errorMsgRegex,
+ );
+ });
+
+ test('focuses element', function () {
+ focusManager.registerTree(testFocusableTree1);
+
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ const nodeElem = testFocusableTree1Node1.getFocusableElement();
+ assert.strictEqual(document.activeElement, nodeElem);
+ });
+
+ test('fires focusin event', function () {
+ let focusCount = 0;
+ const focusListener = () => focusCount++;
+ document.addEventListener('focusin', focusListener);
+ focusManager.registerTree(testFocusableTree1);
+
+ focusManager.focusNode(testFocusableTree1Node1);
+ document.removeEventListener('focusin', focusListener);
+
+ // There should be exactly 1 focus event fired from focusNode().
+ assert.strictEqual(focusCount, 1);
+ });
+
+ test('for orphaned node returns tree root by default', function () {
+ focusManager.registerTree(testFocusableTree1);
+ testFocusableTree1.removeNode(testFocusableTree1Node1);
+
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ // Focusing an invalid node should fall back to the tree root when it has no restoration
+ // fallback node.
+ const currentNode = focusManager.getFocusedNode();
+ const treeRoot = testFocusableTree1.getRootFocusableNode();
+ assert.strictEqual(currentNode, treeRoot);
+ });
+
+ test('for orphaned node returns specified fallback node', function () {
+ focusManager.registerTree(testFocusableTree1);
+ testFocusableTree1.fallbackNode = testFocusableTree1Node2;
+ testFocusableTree1.removeNode(testFocusableTree1Node1);
+
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ // Focusing an invalid node should fall back to the restored fallback.
+ const currentNode = focusManager.getFocusedNode();
+ assert.strictEqual(currentNode, testFocusableTree1Node2);
+ });
+
+ test('restores focus when element quietly loses focus', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.focusNode(testFocusableTree1Node1);
+ // Remove the FocusManager's listeners to simulate not receiving a focus
+ // event when focus is lost. This can happen in Firefox and Safari when an
+ // element is removed and then re-added to the DOM. This is a contrived
+ // setup to achieve the same outcome on all browsers. For context, see:
+ // https://github.com/google/blockly-keyboard-experimentation/issues/87.
+ for (const registeredListener of globalDocumentEventListeners) {
+ const eventType = registeredListener.type;
+ const eventListener = registeredListener.listener;
+ document.removeEventListener(eventType, eventListener);
+ }
+ document.body.focus();
+
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ const currentNode = focusManager.getFocusedNode();
+ const currentElem = currentNode?.getFocusableElement();
+ assert.strictEqual(currentNode, testFocusableTree1Node1);
+ assert.strictEqual(document.activeElement, currentElem);
+ });
+
+ test('restores focus when element and new node focused', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.focusNode(testFocusableTree1Node1);
+ // Remove the FocusManager's listeners to simulate not receiving a focus
+ // event when focus is lost. This can happen in Firefox and Safari when an
+ // element is removed and then re-added to the DOM. This is a contrived
+ // setup to achieve the same outcome on all browsers. For context, see:
+ // https://github.com/google/blockly-keyboard-experimentation/issues/87.
+ for (const registeredListener of globalDocumentEventListeners) {
+ const eventType = registeredListener.type;
+ const eventListener = registeredListener.listener;
+ document.removeEventListener(eventType, eventListener);
+ }
+ document.body.focus();
+
+ focusManager.focusNode(testFocusableTree1Node2);
+
+ const currentNode = focusManager.getFocusedNode();
+ const currentElem = currentNode?.getFocusableElement();
+ assert.strictEqual(currentNode, testFocusableTree1Node2);
+ assert.strictEqual(document.activeElement, currentElem);
+ });
+
+ test('for unfocused node calls onNodeFocus once', function () {
+ const focusSpy = sinon.spy(testFocusableTree1Node1, 'onNodeFocus');
+ focusManager.registerTree(testFocusableTree1);
+
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ assert.strictEqual(focusSpy.callCount, 1);
+ });
+
+ test('for previously focused node calls onNodeBlur once', function () {
+ const blurSpy = sinon.spy(testFocusableTree1Node1, 'onNodeBlur');
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.focusNode(testFocusableTree1Node2);
+
+ assert.strictEqual(blurSpy.callCount, 1);
+ });
+
+ test('for unfocused tree calls onTreeFocus once', function () {
+ const treeFocusSpy = sinon.spy(testFocusableTree1, 'onTreeFocus');
+ focusManager.registerTree(testFocusableTree1);
+
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ assert.strictEqual(treeFocusSpy.callCount, 1);
+ });
+
+ test('for previously focused tree calls onTreeBlur once', function () {
+ const treeBlurSpy = sinon.spy(testFocusableTree1, 'onTreeBlur');
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.focusNode(testFocusableTree2Node1);
+
+ assert.strictEqual(treeBlurSpy.callCount, 1);
+ });
+
+ test('for same node twice calls onNodeFocus once', function () {
+ const nodeFocusSpy = sinon.spy(testFocusableTree1Node1, 'onNodeFocus');
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ // Call focus for the same node a second time.
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ // Despite two calls to focus the node should only focus once.
+ assert.strictEqual(nodeFocusSpy.callCount, 1);
+ });
+
+ test('for unfocusable node does not call onNodeFocus', function () {
+ const nodeFocusSpy = sinon.spy(testFocusableTree1Node1, 'onNodeFocus');
+ testFocusableTree1Node1.canBeFocused = () => false;
+ focusManager.registerTree(testFocusableTree1);
+
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ // Unfocusable nodes should not be focused, nor have their callbacks called.
+ assert.strictEqual(nodeFocusSpy.callCount, 0);
+ });
+
+ test('for unfocused node overwrites tab index', function () {
+ focusManager.registerTree(testFocusableTree1);
+
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ // Focusing an element should overwrite its tab index.
+ const elem = testFocusableTree1Node1.getFocusableElement();
+ assert.strictEqual(elem.getAttribute('tabindex'), '-1');
+ });
+
+ test('for previously focused node keeps new tab index', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.focusNode(testFocusableTree1Node2);
+
+ // The previously focused element should retain its tab index.
+ const elem = testFocusableTree1Node1.getFocusableElement();
+ assert.strictEqual(elem.getAttribute('tabindex'), '-1');
+ });
+
+ test('for node with custom tab index does not change tab index', function () {
+ focusManager.registerTree(testFocusableTree1);
+ const elem = testFocusableTree1Node1.getFocusableElement();
+ elem.tabIndex = 0;
+
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ // If the node already has a tab index set then it should retain that index.
+ assert.strictEqual(elem.getAttribute('tabindex'), '0');
+ });
+
+ suite('for unmanaged tree', function () {
+ test('focused root overwrites tab index', function () {
+ focusManager.registerTree(testFocusableTree1, false);
+ const rootNode = testFocusableTree1.getRootFocusableNode();
+
+ focusManager.focusNode(rootNode);
+
+ // Focusing an unmanaged tree's root should overwrite its tab index.
+ const rootElem = rootNode.getFocusableElement();
+ assert.strictEqual(rootElem.getAttribute('tabindex'), '-1');
+ });
+
+ test('focused root with custom tab index does not change tab index', function () {
+ focusManager.registerTree(testFocusableTree1, false);
+ const rootNode = testFocusableTree1.getRootFocusableNode();
+ const rootElem = rootNode.getFocusableElement();
+ rootElem.tabIndex = 0;
+
+ focusManager.focusNode(rootNode);
+
+ // If the node already has a tab index set then it should retain that index.
+ assert.strictEqual(rootElem.getAttribute('tabindex'), '0');
+ });
+
+ test('focused node in a tree after unmanaged was focused should keep previous root unchanged', function () {
+ focusManager.registerTree(testFocusableTree1, false);
+ focusManager.registerTree(testFocusableTree2, false);
+ const rootNode = testFocusableTree1.getRootFocusableNode();
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.focusNode(testFocusableTree2Node1);
+
+ // Focusing a different tree shouldn't change the root of the previous tree if it's unmanaged.
+ const rootElem = rootNode.getFocusableElement();
+ assert.isNull(rootElem.getAttribute('tabindex'));
+ });
+
+ test('focused node in a tree after unmanaged was root focused should make previous root tab navigable', function () {
+ focusManager.registerTree(testFocusableTree1, false);
+ focusManager.registerTree(testFocusableTree2, false);
+ const rootNode = testFocusableTree1.getRootFocusableNode();
+ focusManager.focusTree(testFocusableTree1);
+
+ focusManager.focusNode(testFocusableTree2Node1);
+
+ // The previous tree's root should be kept unchanged (since it was managed).
+ const rootElem = rootNode.getFocusableElement();
+ assert.strictEqual(rootElem.getAttribute('tabindex'), '-1');
+ });
+ });
+
+ suite('for managed tree', function () {
+ test('for unfocused node in managed tree overwrites tab index', function () {
+ focusManager.registerTree(testFocusableTree1, true);
+
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ // Focusing an element should overwrite its tab index.
+ const elem = testFocusableTree1Node1.getFocusableElement();
+ assert.strictEqual(elem.getAttribute('tabindex'), '-1');
+ });
+
+ test('for previously focused node in managed tree keeps new tab index', function () {
+ focusManager.registerTree(testFocusableTree1, true);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.focusNode(testFocusableTree1Node2);
+
+ // The previously focused element should retain its tab index.
+ const elem = testFocusableTree1Node1.getFocusableElement();
+ assert.strictEqual(elem.getAttribute('tabindex'), '-1');
+ });
+
+ test('focused root makes root non-tab navigable', function () {
+ focusManager.registerTree(testFocusableTree1, true);
+ const rootNode = testFocusableTree1.getRootFocusableNode();
+
+ focusManager.focusNode(rootNode);
+
+ // Focusing the root in a managed tree should make it non-tab navigable.
+ const rootElem = rootNode.getFocusableElement();
+ assert.strictEqual(rootElem.getAttribute('tabindex'), '-1');
+ });
+
+ test('focused root with custom tab index should overwrite tab index', function () {
+ focusManager.registerTree(testFocusableTree1, true);
+ const rootNode = testFocusableTree1.getRootFocusableNode();
+ const rootElem = rootNode.getFocusableElement();
+ rootElem.tabIndex = 0;
+
+ focusManager.focusNode(rootNode);
+
+ // Custom tab indexes are overwritten for the root in a managed tree.
+ assert.strictEqual(rootElem.getAttribute('tabindex'), '-1');
+ });
+
+ test('focused node tree root makes root non-tab navigable', function () {
+ focusManager.registerTree(testFocusableTree1, true);
+ const rootNode = testFocusableTree1.getRootFocusableNode();
+
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ // Focusing a node of a managed tree should make the root non-tab navigable.
+ const rootElem = rootNode.getFocusableElement();
+ assert.strictEqual(rootElem.getAttribute('tabindex'), '-1');
+ });
+
+ test('focused node root with custom tab index should overwrite tab index', function () {
+ focusManager.registerTree(testFocusableTree1, true);
+ const rootNode = testFocusableTree1.getRootFocusableNode();
+ const rootElem = rootNode.getFocusableElement();
+ rootElem.tabIndex = 0;
+
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ // Custom tab indexes are overwritten for the root in a managed tree even when a tree's node
+ // is focused.
+ assert.strictEqual(rootElem.getAttribute('tabindex'), '-1');
+ });
+
+ test('focused node in a tree after managed was focused should make previous root tab navigable', function () {
+ focusManager.registerTree(testFocusableTree1, true);
+ focusManager.registerTree(testFocusableTree2, false);
+ const rootNode = testFocusableTree1.getRootFocusableNode();
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.focusNode(testFocusableTree2Node1);
+
+ // Focusing a different tree shouldn't after a managed tree should make the managed tree tab
+ // navigable.
+ const rootElem = rootNode.getFocusableElement();
+ assert.strictEqual(rootElem.getAttribute('tabindex'), '0');
+ });
+
+ test('focused node in a tree after managed was root focused should make previous root tab navigable', function () {
+ focusManager.registerTree(testFocusableTree1, true);
+ focusManager.registerTree(testFocusableTree2, false);
+ const rootNode = testFocusableTree1.getRootFocusableNode();
+ focusManager.focusTree(testFocusableTree1);
+
+ focusManager.focusNode(testFocusableTree2Node1);
+
+ // Focusing a different tree shouldn't after a managed tree should make the managed tree tab
+ // navigable.
+ const rootElem = rootNode.getFocusableElement();
+ assert.strictEqual(rootElem.getAttribute('tabindex'), '0');
+ });
+ });
+ });
+
+ suite('getFocusManager()', function () {
+ test('returns non-null manager', function () {
+ const manager = Blockly.getFocusManager();
+
+ assert.isNotNull(manager);
+ });
+
+ test('returns the exact same instance in subsequent calls', function () {
+ const manager1 = Blockly.getFocusManager();
+ const manager2 = Blockly.getFocusManager();
+
+ assert.strictEqual(manager2, manager1);
+ });
+ });
+
+ /* Focus tests for HTML trees. */
+
+ suite('focus*() switching in HTML tree', function () {
+ suite('getFocusedTree()', function () {
+ test('registered tree focusTree()ed no prev focus returns tree', function () {
+ focusManager.registerTree(testFocusableTree1);
+
+ focusManager.focusTree(testFocusableTree1);
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableTree1);
+ });
+
+ test('registered tree focusTree()ed prev node focused returns tree', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.focusTree(testFocusableTree1);
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableTree1);
+ });
+
+ test('registered tree focusTree()ed diff tree prev focused returns new tree', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusTree(testFocusableTree1);
+
+ focusManager.focusTree(testFocusableTree2);
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableTree2);
+ });
+
+ test('registered tree focusTree()ed diff tree node prev focused returns new tree', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.focusTree(testFocusableTree2);
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableTree2);
+ });
+
+ test('registered root focusNode()ed no prev focus returns tree', function () {
+ focusManager.registerTree(testFocusableTree1);
+
+ focusManager.focusNode(testFocusableTree1.getRootFocusableNode());
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableTree1);
+ });
+
+ test("registered node focusNode()ed no prev focus returns node's tree", function () {
+ focusManager.registerTree(testFocusableTree1);
+
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableTree1);
+ });
+
+ test("registered subnode focusNode()ed no prev focus returns node's tree", function () {
+ focusManager.registerTree(testFocusableTree1);
+
+ focusManager.focusNode(testFocusableTree1Node1Child1);
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableTree1);
+ });
+
+ test('registered node focusNode()ed after prev node focus returns same tree', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.focusNode(testFocusableTree1Node2);
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableTree1);
+ });
+
+ test("registered node focusNode()ed after prev node focus diff tree returns new node's tree", function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.focusNode(testFocusableTree2Node1);
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableTree2);
+ });
+
+ test("registered tree root focusNode()ed after prev node focus diff tree returns new node's tree", function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.focusNode(testFocusableTree2.getRootFocusableNode());
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableTree2);
+ });
+
+ test('unregistered tree focusTree()ed with no prev focus returns null', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.focusTree(testFocusableTree1);
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ assert.isNull(focusManager.getFocusedTree());
+ });
+
+ test('unregistered tree focusNode()ed with no prev focus returns null', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ assert.isNull(focusManager.getFocusedTree());
+ });
+
+ test('unregistered tree focusNode()ed with prev node prior focused returns null', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusNode(testFocusableTree2Node1);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ // Since the more recent tree was removed, there's no tree currently focused.
+ assert.isNull(focusManager.getFocusedTree());
+ });
+
+ test('unregistered tree focusNode()ed with prev node recently focused returns new tree', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusNode(testFocusableTree1Node1);
+ focusManager.focusNode(testFocusableTree2Node1);
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ // Since the most recent tree still exists, it still has focus.
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableTree2);
+ });
+
+ test('nested tree focusTree()ed with no prev focus returns nested tree', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableNestedTree4);
+
+ focusManager.focusTree(testFocusableNestedTree4);
+
+ assert.strictEqual(
+ focusManager.getFocusedTree(),
+ testFocusableNestedTree4,
+ );
+ });
+
+ test('nested tree node focusNode()ed with no prev focus returns nested tree', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableNestedTree4);
+
+ focusManager.focusNode(testFocusableNestedTree4Node1);
+
+ assert.strictEqual(
+ focusManager.getFocusedTree(),
+ testFocusableNestedTree4,
+ );
+ });
+
+ test('nested tree node focusNode()ed after parent focused returns nested tree', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableNestedTree4);
+ focusManager.focusNode(testFocusableTree2Node1);
+
+ focusManager.focusNode(testFocusableNestedTree4Node1);
+
+ assert.strictEqual(
+ focusManager.getFocusedTree(),
+ testFocusableNestedTree4,
+ );
+ });
+ });
+ suite('getFocusedNode()', function () {
+ test('registered tree focusTree()ed no prev focus returns root node', function () {
+ focusManager.registerTree(testFocusableTree1);
+
+ focusManager.focusTree(testFocusableTree1);
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree1.getRootFocusableNode(),
+ );
+ });
+
+ test('registered tree focusTree()ed prev node focused returns original node', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.focusTree(testFocusableTree1);
+
+ // The original node retains focus since the tree already holds focus (per focusTree's
+ // contract).
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree1Node1,
+ );
+ });
+
+ test('registered tree focusTree()ed diff tree prev focused returns new root node', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusTree(testFocusableTree1);
+
+ focusManager.focusTree(testFocusableTree2);
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree2.getRootFocusableNode(),
+ );
+ });
+
+ test('registered tree focusTree()ed diff tree node prev focused returns new root node', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.focusTree(testFocusableTree2);
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree2.getRootFocusableNode(),
+ );
+ });
+
+ test('registered root focusNode()ed no prev focus returns root node', function () {
+ focusManager.registerTree(testFocusableTree1);
+
+ focusManager.focusNode(testFocusableTree1.getRootFocusableNode());
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree1.getRootFocusableNode(),
+ );
+ });
+
+ test('registered node focusNode()ed no prev focus returns node', function () {
+ focusManager.registerTree(testFocusableTree1);
+
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree1Node1,
+ );
+ });
+
+ test('registered subnode focusNode()ed no prev focus returns subnode', function () {
+ focusManager.registerTree(testFocusableTree1);
+
+ focusManager.focusNode(testFocusableTree1Node1Child1);
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree1Node1Child1,
+ );
+ });
+
+ test('registered node focusNode()ed after prev node focus returns new node', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.focusNode(testFocusableTree1Node2);
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree1Node2,
+ );
+ });
+
+ test('registered node focusNode()ed after prev node focus diff tree returns new node', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.focusNode(testFocusableTree2Node1);
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree2Node1,
+ );
+ });
+
+ test('registered tree root focusNode()ed after prev node focus diff tree returns new root', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.focusNode(testFocusableTree2.getRootFocusableNode());
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree2.getRootFocusableNode(),
+ );
+ });
+
+ test('unregistered tree focusTree()ed with no prev focus returns null', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.focusTree(testFocusableTree1);
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ assert.isNull(focusManager.getFocusedNode());
+ });
+
+ test('unregistered tree focusNode()ed with no prev focus returns null', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ assert.isNull(focusManager.getFocusedNode());
+ });
+
+ test('unregistered tree focusNode()ed with prev node prior focused returns null', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusNode(testFocusableTree2Node1);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ // Since the more recent tree was removed, there's no tree currently focused.
+ assert.isNull(focusManager.getFocusedNode());
+ });
+
+ test('unregistered tree focusNode()ed with prev node recently focused returns new node', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusNode(testFocusableTree1Node1);
+ focusManager.focusNode(testFocusableTree2Node1);
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ // Since the most recent tree still exists, it still has focus.
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree2Node1,
+ );
+ });
+
+ test('nested tree focusTree()ed with no prev focus returns nested root', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableNestedTree4);
+
+ focusManager.focusTree(testFocusableNestedTree4);
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableNestedTree4.getRootFocusableNode(),
+ );
+ });
+
+ test('nested tree node focusNode()ed with no prev focus returns focused node', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableNestedTree4);
+
+ focusManager.focusNode(testFocusableNestedTree4Node1);
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableNestedTree4Node1,
+ );
+ });
+
+ test('nested tree node focusNode()ed after parent focused returns focused node', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableNestedTree4);
+ focusManager.focusNode(testFocusableTree2Node1);
+
+ focusManager.focusNode(testFocusableNestedTree4Node1);
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableNestedTree4Node1,
+ );
+ });
+ });
+ suite('CSS classes', function () {
+ test('registered tree focusTree()ed no prev focus root elem has active property', function () {
+ focusManager.registerTree(testFocusableTree1);
+
+ focusManager.focusTree(testFocusableTree1);
+
+ const rootElem = testFocusableTree1
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered tree focusTree()ed prev node focused original elem has active property', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.focusTree(testFocusableTree1);
+
+ // The original node retains active focus since the tree already holds focus (per
+ // focusTree's contract).
+ const nodeElem = testFocusableTree1Node1.getFocusableElement();
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered tree focusTree()ed diff tree prev focused new root elem has active property', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusTree(testFocusableTree1);
+
+ focusManager.focusTree(testFocusableTree2);
+
+ const rootElem = testFocusableTree2
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered tree focusTree()ed diff tree node prev focused new root elem has active property', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.focusTree(testFocusableTree2);
+
+ const rootElem = testFocusableTree2
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered root focusNode()ed no prev focus returns root elem has active property', function () {
+ focusManager.registerTree(testFocusableTree1);
+
+ focusManager.focusNode(testFocusableTree1.getRootFocusableNode());
+
+ const rootElem = testFocusableTree1
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered node focusNode()ed no prev focus node elem has active property', function () {
+ focusManager.registerTree(testFocusableTree1);
+
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ const nodeElem = testFocusableTree1Node1.getFocusableElement();
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered node focusNode()ed after prev node focus same tree old node elem has no focus property', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.focusNode(testFocusableTree1Node2);
+
+ const prevNodeElem = testFocusableTree1Node1.getFocusableElement();
+ assertNotIncludesClass(
+ prevNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ prevNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered node focusNode()ed after prev node focus same tree new node elem has active property', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.focusNode(testFocusableTree1Node2);
+
+ const newNodeElem = testFocusableTree1Node2.getFocusableElement();
+ assertIncludesClass(
+ newNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ newNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered node focusNode()ed after prev node focus diff tree old node elem has passive property', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.focusNode(testFocusableTree2Node1);
+
+ const prevNodeElem = testFocusableTree1Node1.getFocusableElement();
+ assertNotIncludesClass(
+ prevNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ prevNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered node focusNode()ed after prev node focus diff tree new node elem has active property', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.focusNode(testFocusableTree2Node1);
+
+ const newNodeElem = testFocusableTree2Node1.getFocusableElement();
+ assertIncludesClass(
+ newNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ newNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered tree root focusNode()ed after prev node focus diff tree new root has active property', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.focusNode(testFocusableTree2.getRootFocusableNode());
+
+ const rootElem = testFocusableTree2
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('unregistered tree focusTree()ed with no prev focus removes focus', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.focusTree(testFocusableTree1);
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ // Since the tree was unregistered it no longer has focus indicators.
+ const rootElem = testFocusableTree1
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('unregistered tree focusNode()ed with no prev focus removes focus', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ // Since the tree was unregistered it no longer has focus indicators.
+ const nodeElem = testFocusableTree1Node1.getFocusableElement();
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('unregistered tree focusNode()ed with prev node prior removes focus from removed tree', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusNode(testFocusableTree2Node1);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ // Since the tree was unregistered it no longer has focus indicators. However, the old node
+ // should still have passive indication.
+ const otherNodeElem = testFocusableTree2Node1.getFocusableElement();
+ const removedNodeElem = testFocusableTree1Node1.getFocusableElement();
+ assertIncludesClass(
+ otherNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ otherNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ removedNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ removedNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('unregistered tree focusNode()ed with prev node recently removes focus from removed tree', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusNode(testFocusableTree1Node1);
+ focusManager.focusNode(testFocusableTree2Node1);
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ // Since the tree was unregistered it no longer has focus indicators. However, the new node
+ // should still have active indication.
+ const otherNodeElem = testFocusableTree2Node1.getFocusableElement();
+ const removedNodeElem = testFocusableTree1Node1.getFocusableElement();
+ assertIncludesClass(
+ otherNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ otherNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ removedNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ removedNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('focusNode() multiple nodes in same tree with switches ensure passive focus has gone', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusNode(testFocusableTree1Node1);
+ focusManager.focusNode(testFocusableTree2Node1);
+
+ focusManager.focusNode(testFocusableTree1Node2);
+
+ // When switching back to the first tree, ensure the original passive node is no longer
+ // passive now that the new node is active.
+ const node1 = testFocusableTree1Node1.getFocusableElement();
+ const node2 = testFocusableTree1Node2.getFocusableElement();
+ assertNotIncludesClass(
+ node1.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ node2.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered tree focusTree()ed other tree node passively focused tree node now has active property', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusNode(testFocusableTree1Node1);
+ focusManager.focusNode(testFocusableTree2Node1);
+
+ focusManager.focusTree(testFocusableTree1);
+
+ // The original node in the tree should be moved from passive to active focus per the
+ // contract of focusTree). Also, the root of the tree should have no focus indication.
+ const nodeElem = testFocusableTree1Node1.getFocusableElement();
+ const rootElem = testFocusableTree1
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('focus on root, node in diff tree, then node in first tree; root should have focus gone', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusTree(testFocusableTree1);
+ focusManager.focusNode(testFocusableTree2Node1);
+
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ const nodeElem = testFocusableTree1Node1.getFocusableElement();
+ const rootElem = testFocusableTree1
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('nested tree focusTree()ed with no prev root has active focus', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableNestedTree4);
+
+ focusManager.focusTree(testFocusableNestedTree4);
+
+ const rootElem = testFocusableNestedTree4
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('nested tree node focusNode()ed with no prev focus node has active focus', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableNestedTree4);
+
+ focusManager.focusNode(testFocusableNestedTree4Node1);
+
+ const nodeElem = testFocusableNestedTree4Node1.getFocusableElement();
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('nested tree node focusNode()ed after parent focused prev has passive node has active', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableNestedTree4);
+ focusManager.focusNode(testFocusableTree2Node1);
+
+ focusManager.focusNode(testFocusableNestedTree4Node1);
+
+ const prevNodeElem = testFocusableTree2Node1.getFocusableElement();
+ const currNodeElem =
+ testFocusableNestedTree4Node1.getFocusableElement();
+ assertNotIncludesClass(
+ prevNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ prevNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ currNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ currNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+ });
+ });
+
+ suite('DOM focus() switching in HTML tree', function () {
+ suite('getFocusedTree()', function () {
+ test('registered root focus()ed no prev focus returns tree', function () {
+ focusManager.registerTree(testFocusableTree1);
+ document
+ .getElementById('testFocusableTree1')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableTree1')?.focus();
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableTree1);
+ });
+
+ test("registered node focus()ed no prev focus returns node's tree", function () {
+ focusManager.registerTree(testFocusableTree1);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableTree1);
+ });
+
+ test("registered subnode focus()ed no prev focus returns node's tree", function () {
+ focusManager.registerTree(testFocusableTree1);
+ document
+ .getElementById('testFocusableTree1.node1.child1')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableTree1.node1.child1')?.focus();
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableTree1);
+ });
+
+ test('registered node focus()ed after prev node focus returns same tree', function () {
+ focusManager.registerTree(testFocusableTree1);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree1.node2')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ document.getElementById('testFocusableTree1.node2')?.focus();
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableTree1);
+ });
+
+ test("registered node focus()ed after prev node focus diff tree returns new node's tree", function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ document.getElementById('testFocusableTree2.node1')?.focus();
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableTree2);
+ });
+
+ test("registered tree root focus()ed after prev node focus diff tree returns new node's tree", function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree2')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ document.getElementById('testFocusableTree2')?.focus();
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableTree2);
+ });
+
+ test("non-registered node subelement focus()ed returns node's tree", function () {
+ focusManager.registerTree(testFocusableTree1);
+ document
+ .getElementById('testFocusableTree1.node2.unregisteredChild1')
+ ?.setAttribute('tabindex', '-1');
+
+ document
+ .getElementById('testFocusableTree1.node2.unregisteredChild1')
+ ?.focus();
+
+ // The tree of the unregistered child element should take focus.
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableTree1);
+ });
+
+ test('non-registered tree focus()ed returns null', function () {
+ document
+ .getElementById('testUnregisteredFocusableTree3')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testUnregisteredFocusableTree3')?.focus();
+
+ assert.isNull(focusManager.getFocusedTree());
+ });
+
+ test('non-registered tree node focus()ed returns null', function () {
+ document
+ .getElementById('testUnregisteredFocusableTree3.node1')
+ ?.setAttribute('tabindex', '-1');
+
+ document
+ .getElementById('testUnregisteredFocusableTree3.node1')
+ ?.focus();
+
+ assert.isNull(focusManager.getFocusedTree());
+ });
+
+ test('non-registered tree node focus()ed after registered node focused returns null', function () {
+ focusManager.registerTree(testFocusableTree1);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testUnregisteredFocusableTree3.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ document
+ .getElementById('testUnregisteredFocusableTree3.node1')
+ ?.focus();
+
+ assert.isNull(focusManager.getFocusedTree());
+ });
+
+ test('unfocusable element focus()ed after registered node focused returns original tree', function () {
+ focusManager.registerTree(testFocusableTree1);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ document.getElementById('testUnfocusableElement')?.focus();
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableTree1);
+ });
+
+ test('unregistered tree focus()ed with no prev focus returns null', function () {
+ focusManager.registerTree(testFocusableTree1);
+ document
+ .getElementById('testFocusableTree1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1')?.focus();
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ assert.isNull(focusManager.getFocusedTree());
+ });
+
+ test('unregistered tree focus()ed with no prev focus returns null', function () {
+ focusManager.registerTree(testFocusableTree1);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ assert.isNull(focusManager.getFocusedTree());
+ });
+
+ test('unregistered tree focus()ed with prev node prior focused returns null', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ document
+ .getElementById('testFocusableTree2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree2.node1')?.focus();
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ // Since the more recent tree was removed, there's no tree currently focused.
+ assert.isNull(focusManager.getFocusedTree());
+ });
+
+ test('unregistered tree focus()ed with prev node recently focused returns new tree', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1.node1')?.focus();
+ document.getElementById('testFocusableTree2.node1')?.focus();
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ // Since the most recent tree still exists, it still has focus.
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableTree2);
+ });
+
+ test('unregistered tree focus()ed with prev node after unregistering returns null', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1.node1')?.focus();
+ document.getElementById('testFocusableTree2.node1')?.focus();
+ focusManager.unregisterTree(testFocusableTree1);
+
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ // Attempting to focus a now removed tree should result in nothing being
+ // focused since the removed tree can have DOM focus, but that focus is
+ // ignored by FocusManager.
+ assert.isNull(focusManager.getFocusedTree());
+ });
+
+ test('nested tree focusTree()ed with no prev focus returns nested tree', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableNestedTree4);
+ document
+ .getElementById('testFocusableNestedTree4')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableNestedTree4')?.focus();
+
+ assert.strictEqual(
+ focusManager.getFocusedTree(),
+ testFocusableNestedTree4,
+ );
+ });
+
+ test('nested tree node focusNode()ed with no prev focus returns nested tree', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableNestedTree4);
+ document
+ .getElementById('testFocusableNestedTree4.node1')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableNestedTree4.node1')?.focus();
+
+ assert.strictEqual(
+ focusManager.getFocusedTree(),
+ testFocusableNestedTree4,
+ );
+ });
+
+ test('nested tree node focusNode()ed after parent focused returns nested tree', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableNestedTree4);
+ document
+ .getElementById('testFocusableTree2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableNestedTree4.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree2.node1')?.focus();
+
+ document.getElementById('testFocusableNestedTree4.node1')?.focus();
+
+ assert.strictEqual(
+ focusManager.getFocusedTree(),
+ testFocusableNestedTree4,
+ );
+ });
+ });
+ suite('getFocusedNode()', function () {
+ test('registered root focus()ed no prev focus returns root node', function () {
+ focusManager.registerTree(testFocusableTree1);
+ document
+ .getElementById('testFocusableTree1')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableTree1')?.focus();
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree1.getRootFocusableNode(),
+ );
+ });
+
+ test('registered node focus()ed no prev focus returns node', function () {
+ focusManager.registerTree(testFocusableTree1);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree1Node1,
+ );
+ });
+
+ test('registered subnode focus()ed no prev focus returns subnode', function () {
+ focusManager.registerTree(testFocusableTree1);
+ document
+ .getElementById('testFocusableTree1.node1.child1')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableTree1.node1.child1')?.focus();
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree1Node1Child1,
+ );
+ });
+
+ test('registered node focus()ed after prev node focus returns new node', function () {
+ focusManager.registerTree(testFocusableTree1);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree1.node2')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ document.getElementById('testFocusableTree1.node2')?.focus();
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree1Node2,
+ );
+ });
+
+ test('registered node focus()ed after prev node focus diff tree returns new node', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ document.getElementById('testFocusableTree2.node1')?.focus();
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree2Node1,
+ );
+ });
+
+ test('registered tree root focus()ed after prev node focus diff tree returns new root', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree2')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ document.getElementById('testFocusableTree2')?.focus();
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree2.getRootFocusableNode(),
+ );
+ });
+
+ test('non-registered node subelement focus()ed returns nearest node', function () {
+ focusManager.registerTree(testFocusableTree1);
+ document
+ .getElementById('testFocusableTree1.node2.unregisteredChild1')
+ ?.setAttribute('tabindex', '-1');
+
+ document
+ .getElementById('testFocusableTree1.node2.unregisteredChild1')
+ ?.focus();
+
+ // The nearest node of the unregistered child element should take focus.
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree1Node2,
+ );
+ });
+
+ test('non-registered tree focus()ed returns null', function () {
+ document
+ .getElementById('testUnregisteredFocusableTree3')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testUnregisteredFocusableTree3')?.focus();
+
+ assert.isNull(focusManager.getFocusedNode());
+ });
+
+ test('non-registered tree node focus()ed returns null', function () {
+ document
+ .getElementById('testUnregisteredFocusableTree3.node1')
+ ?.setAttribute('tabindex', '-1');
+
+ document
+ .getElementById('testUnregisteredFocusableTree3.node1')
+ ?.focus();
+
+ assert.isNull(focusManager.getFocusedNode());
+ });
+
+ test('non-registered tree node focus()ed after registered node focused returns null', function () {
+ focusManager.registerTree(testFocusableTree1);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testUnregisteredFocusableTree3.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ document
+ .getElementById('testUnregisteredFocusableTree3.node1')
+ ?.focus();
+
+ assert.isNull(focusManager.getFocusedNode());
+ });
+
+ test('unfocusable element focus()ed after registered node focused returns original node', function () {
+ focusManager.registerTree(testFocusableTree1);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ document.getElementById('testUnfocusableElement')?.focus();
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree1Node1,
+ );
+ });
+
+ test('unregistered tree focus()ed with no prev focus returns null', function () {
+ focusManager.registerTree(testFocusableTree1);
+ document
+ .getElementById('testFocusableTree1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1')?.focus();
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ assert.isNull(focusManager.getFocusedNode());
+ });
+
+ test('unregistered tree focus()ed with no prev focus returns null', function () {
+ focusManager.registerTree(testFocusableTree1);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ assert.isNull(focusManager.getFocusedNode());
+ });
+
+ test('unregistered tree focus()ed with prev node prior focused returns null', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ document
+ .getElementById('testFocusableTree2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree2.node1')?.focus();
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ // Since the more recent tree was removed, there's no tree currently focused.
+ assert.isNull(focusManager.getFocusedNode());
+ });
+
+ test('unregistered tree focus()ed with prev node recently focused returns new node', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1.node1')?.focus();
+ document.getElementById('testFocusableTree2.node1')?.focus();
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ // Since the most recent tree still exists, it still has focus.
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree2Node1,
+ );
+ });
+
+ test('unregistered tree focus()ed with prev node after unregistering returns null', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1.node1')?.focus();
+ document.getElementById('testFocusableTree2.node1')?.focus();
+ focusManager.unregisterTree(testFocusableTree1);
+
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ // Attempting to focus a now removed tree should result in nothing being
+ // focused since the removed tree can have DOM focus, but that focus is
+ // ignored by FocusManager.
+ assert.isNull(focusManager.getFocusedNode());
+ });
+
+ test('nested tree focus()ed with no prev focus returns nested root', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableNestedTree4);
+ document
+ .getElementById('testFocusableNestedTree4')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableNestedTree4')?.focus();
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableNestedTree4.getRootFocusableNode(),
+ );
+ });
+
+ test('nested tree node focus()ed with no prev focus returns focused node', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableNestedTree4);
+ document
+ .getElementById('testFocusableNestedTree4.node1')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableNestedTree4.node1')?.focus();
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableNestedTree4Node1,
+ );
+ });
+
+ test('nested tree node focus()ed after parent focused returns focused node', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableNestedTree4);
+ document
+ .getElementById('testFocusableTree2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableNestedTree4.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree2.node1')?.focus();
+
+ document.getElementById('testFocusableNestedTree4.node1')?.focus();
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableNestedTree4Node1,
+ );
+ });
+
+ test('after focus() after trying to focusNode() an unfocusable node updates returns focus()ed node', function () {
+ testFocusableTree1Node1.canBeFocused = () => false;
+ document
+ .getElementById('testFocusableTree1.node2')
+ ?.setAttribute('tabindex', '-1');
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ document.getElementById('testFocusableTree1.node2')?.focus();
+
+ // focus()ing a new node should overwrite a failed attempt to focusNode() an unfocusable
+ // node. This verifies that DOM focus syncing is properly reenabled by FocusManager.
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree1Node2,
+ );
+ });
+
+ test('after focus() after trying to focusNode() the same node twice returns focus()ed node', function () {
+ document
+ .getElementById('testFocusableTree1.node2')
+ ?.setAttribute('tabindex', '-1');
+ focusManager.registerTree(testFocusableTree1);
+ // Intentionally try to focus the same node twice.
+ focusManager.focusNode(testFocusableTree1Node1);
+ focusManager.focusNode(testFocusableTree1Node1);
+
+ document.getElementById('testFocusableTree1.node2')?.focus();
+
+ // focus()ing a new node should overwrite a failed attempt to focusNode() the same node
+ // twice. This verifies that DOM focus syncing is properly reenabled by FocusManager.
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree1Node2,
+ );
+ });
+ });
+ suite('CSS classes', function () {
+ test('registered root focus()ed no prev focus returns root elem has active property', function () {
+ focusManager.registerTree(testFocusableTree1);
+ document
+ .getElementById('testFocusableTree1')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableTree1')?.focus();
+
+ const rootElem = testFocusableTree1
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered node focus()ed no prev focus node elem has active property', function () {
+ focusManager.registerTree(testFocusableTree1);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ const nodeElem = testFocusableTree1Node1.getFocusableElement();
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered node focus()ed after prev node focus same tree old node elem has no focus property', function () {
+ focusManager.registerTree(testFocusableTree1);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree1.node2')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ document.getElementById('testFocusableTree1.node2')?.focus();
+
+ const prevNodeElem = testFocusableTree1Node1.getFocusableElement();
+ assertNotIncludesClass(
+ prevNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ prevNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered node focus()ed after prev node focus same tree new node elem has active property', function () {
+ focusManager.registerTree(testFocusableTree1);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree1.node2')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ document.getElementById('testFocusableTree1.node2')?.focus();
+
+ const newNodeElem = testFocusableTree1Node2.getFocusableElement();
+ assertIncludesClass(
+ newNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ newNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered node focus()ed after prev node focus diff tree old node elem has passive property', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ document.getElementById('testFocusableTree2.node1')?.focus();
+
+ const prevNodeElem = testFocusableTree1Node1.getFocusableElement();
+ assertNotIncludesClass(
+ prevNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ prevNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered node focus()ed after prev node focus diff tree new node elem has active property', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ document.getElementById('testFocusableTree2.node1')?.focus();
+
+ const newNodeElem = testFocusableTree2Node1.getFocusableElement();
+ assertIncludesClass(
+ newNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ newNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered tree root focus()ed after prev node focus diff tree new root has active property', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree2')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ document.getElementById('testFocusableTree2')?.focus();
+
+ const rootElem = testFocusableTree2
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('non-registered node subelement focus()ed nearest node has active property', function () {
+ focusManager.registerTree(testFocusableTree1);
+ document
+ .getElementById('testFocusableTree1.node2.unregisteredChild1')
+ ?.setAttribute('tabindex', '-1');
+
+ document
+ .getElementById('testFocusableTree1.node2.unregisteredChild1')
+ ?.focus();
+
+ // The nearest node of the unregistered child element should be actively focused.
+ const nodeElem = testFocusableTree1Node2.getFocusableElement();
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('non-registered tree focus()ed has no focus', function () {
+ document
+ .getElementById('testUnregisteredFocusableTree3')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testUnregisteredFocusableTree3')?.focus();
+
+ assert.isNull(focusManager.getFocusedNode());
+ const rootElem = document.getElementById(
+ 'testUnregisteredFocusableTree3',
+ );
+ assert.exists(rootElem);
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('non-registered tree node focus()ed has no focus', function () {
+ document
+ .getElementById('testUnregisteredFocusableTree3.node1')
+ ?.setAttribute('tabindex', '-1');
+
+ document
+ .getElementById('testUnregisteredFocusableTree3.node1')
+ ?.focus();
+
+ assert.isNull(focusManager.getFocusedNode());
+ const nodeElem = document.getElementById(
+ 'testUnregisteredFocusableTree3.node1',
+ );
+ assert.exists(nodeElem);
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('unfocsable element focus()ed after registered node focused original node has active focus', function () {
+ focusManager.registerTree(testFocusableTree1);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ document.getElementById('testUnfocusableElement')?.focus();
+
+ // The original node should be unchanged, and the unregistered node should not have any
+ // focus indicators.
+ const nodeElem = document.getElementById('testFocusableTree1.node1');
+ const attemptedNewNodeElem = document.getElementById(
+ 'testUnfocusableElement',
+ );
+ assert.exists(nodeElem);
+ assert.exists(attemptedNewNodeElem);
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ attemptedNewNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ attemptedNewNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('unregistered tree focus()ed with no prev focus removes focus', function () {
+ focusManager.registerTree(testFocusableTree1);
+ document
+ .getElementById('testFocusableTree1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1')?.focus();
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ // Since the tree was unregistered it no longer has focus indicators.
+ const rootElem = testFocusableTree1
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('unregistered tree focus()ed with no prev focus removes focus', function () {
+ focusManager.registerTree(testFocusableTree1);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ // Since the tree was unregistered it no longer has focus indicators.
+ const nodeElem = testFocusableTree1Node1.getFocusableElement();
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('unregistered tree focus()ed with prev node prior removes focus from removed tree', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ document
+ .getElementById('testFocusableTree2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree2.node1')?.focus();
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ // Since the tree was unregistered it no longer has focus indicators. However, the old node
+ // should still have passive indication.
+ const otherNodeElem = testFocusableTree2Node1.getFocusableElement();
+ const removedNodeElem = testFocusableTree1Node1.getFocusableElement();
+ assertIncludesClass(
+ otherNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ otherNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ removedNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ removedNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('unregistered tree focus()ed with prev node recently removes focus from removed tree', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1.node1')?.focus();
+ document.getElementById('testFocusableTree2.node1')?.focus();
+
+ focusManager.unregisterTree(testFocusableTree1);
+
+ // Since the tree was unregistered it no longer has focus indicators. However, the new node
+ // should still have active indication.
+ const otherNodeElem = testFocusableTree2Node1.getFocusableElement();
+ const removedNodeElem = testFocusableTree1Node1.getFocusableElement();
+ assertIncludesClass(
+ otherNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ otherNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ removedNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ removedNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('unregistered tree focus()ed with prev node after unregistering removes active indicator', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1.node1')?.focus();
+ document.getElementById('testFocusableTree2.node1')?.focus();
+ focusManager.unregisterTree(testFocusableTree1);
+
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ // Attempting to focus a now removed tree should remove active.
+ const otherNodeElem = testFocusableTree2Node1.getFocusableElement();
+ const removedNodeElem = testFocusableTree1Node1.getFocusableElement();
+ assertNotIncludesClass(
+ otherNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ otherNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ removedNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ removedNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('focus() multiple nodes in same tree with switches ensure passive focus has gone', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree1.node2')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1.node1')?.focus();
+ document.getElementById('testFocusableTree2.node1')?.focus();
+
+ document.getElementById('testFocusableTree1.node2')?.focus();
+
+ // When switching back to the first tree, ensure the original passive node is no longer
+ // passive now that the new node is active.
+ const node1 = testFocusableTree1Node1.getFocusableElement();
+ const node2 = testFocusableTree1Node2.getFocusableElement();
+ assertNotIncludesClass(
+ node1.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ node2.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('focus on root, node in diff tree, then node in first tree; root should have focus gone', function () {
+ focusManager.registerTree(testFocusableTree1);
+ focusManager.registerTree(testFocusableTree2);
+ document
+ .getElementById('testFocusableTree1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree1')?.focus();
+ document.getElementById('testFocusableTree2.node1')?.focus();
+
+ document.getElementById('testFocusableTree1.node1')?.focus();
+
+ const nodeElem = testFocusableTree1Node1.getFocusableElement();
+ const rootElem = testFocusableTree1
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('nested tree focus()ed with no prev root has active focus', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableNestedTree4);
+ document
+ .getElementById('testFocusableNestedTree4')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableNestedTree4')?.focus();
+
+ const rootElem = testFocusableNestedTree4
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('nested tree node focus()ed with no prev focus node has active focus', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableNestedTree4);
+ document
+ .getElementById('testFocusableNestedTree4.node1')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableNestedTree4.node1')?.focus();
+
+ const nodeElem = testFocusableNestedTree4Node1.getFocusableElement();
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('nested tree node focus()ed after parent focused prev has passive node has active', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableNestedTree4);
+ document
+ .getElementById('testFocusableTree2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableNestedTree4.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree2.node1')?.focus();
+
+ document.getElementById('testFocusableNestedTree4.node1')?.focus();
+
+ const prevNodeElem = testFocusableTree2Node1.getFocusableElement();
+ const currNodeElem =
+ testFocusableNestedTree4Node1.getFocusableElement();
+ assertNotIncludesClass(
+ prevNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ prevNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ currNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ currNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+ });
+ });
+
+ /* Focus tests for SVG trees. */
+
+ suite('focus*() switching in SVG tree', function () {
+ suite('getFocusedTree()', function () {
+ test('registered tree focusTree()ed no prev focus returns tree', function () {
+ focusManager.registerTree(testFocusableGroup1);
+
+ focusManager.focusTree(testFocusableGroup1);
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableGroup1);
+ });
+
+ test('registered tree focusTree()ed prev node focused returns tree', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ focusManager.focusTree(testFocusableGroup1);
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableGroup1);
+ });
+
+ test('registered tree focusTree()ed diff tree prev focused returns new tree', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusTree(testFocusableGroup1);
+
+ focusManager.focusTree(testFocusableGroup2);
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableGroup2);
+ });
+
+ test('registered tree focusTree()ed diff tree node prev focused returns new tree', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ focusManager.focusTree(testFocusableGroup2);
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableGroup2);
+ });
+
+ test('registered root focusNode()ed no prev focus returns tree', function () {
+ focusManager.registerTree(testFocusableGroup1);
+
+ focusManager.focusNode(testFocusableGroup1.getRootFocusableNode());
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableGroup1);
+ });
+
+ test("registered node focusNode()ed no prev focus returns node's tree", function () {
+ focusManager.registerTree(testFocusableGroup1);
+
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableGroup1);
+ });
+
+ test("registered subnode focusNode()ed no prev focus returns node's tree", function () {
+ focusManager.registerTree(testFocusableGroup1);
+
+ focusManager.focusNode(testFocusableGroup1Node1Child1);
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableGroup1);
+ });
+
+ test('registered node focusNode()ed after prev node focus returns same tree', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ focusManager.focusNode(testFocusableGroup1Node2);
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableGroup1);
+ });
+
+ test("registered node focusNode()ed after prev node focus diff tree returns new node's tree", function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ focusManager.focusNode(testFocusableGroup2Node1);
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableGroup2);
+ });
+
+ test("registered tree root focusNode()ed after prev node focus diff tree returns new node's tree", function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ focusManager.focusNode(testFocusableGroup2.getRootFocusableNode());
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableGroup2);
+ });
+
+ test('unregistered tree focusTree()ed with no prev focus returns null', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.focusTree(testFocusableGroup1);
+
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ assert.isNull(focusManager.getFocusedTree());
+ });
+
+ test('unregistered tree focusNode()ed with no prev focus returns null', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ assert.isNull(focusManager.getFocusedTree());
+ });
+
+ test('unregistered tree focusNode()ed with prev node prior focused returns null', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableGroup2Node1);
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ // Since the more recent tree was removed, there's no tree currently focused.
+ assert.isNull(focusManager.getFocusedTree());
+ });
+
+ test('unregistered tree focusNode()ed with prev node recently focused returns new tree', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableGroup1Node1);
+ focusManager.focusNode(testFocusableGroup2Node1);
+
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ // Since the most recent tree still exists, it still has focus.
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableGroup2);
+ });
+
+ test('nested tree focusTree()ed with no prev focus returns nested tree', function () {
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.registerTree(testFocusableNestedGroup4);
+
+ focusManager.focusTree(testFocusableNestedGroup4);
+
+ assert.strictEqual(
+ focusManager.getFocusedTree(),
+ testFocusableNestedGroup4,
+ );
+ });
+
+ test('nested tree node focusNode()ed with no prev focus returns nested tree', function () {
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.registerTree(testFocusableNestedGroup4);
+
+ focusManager.focusNode(testFocusableNestedGroup4Node1);
+
+ assert.strictEqual(
+ focusManager.getFocusedTree(),
+ testFocusableNestedGroup4,
+ );
+ });
+
+ test('nested tree node focusNode()ed after parent focused returns nested tree', function () {
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.registerTree(testFocusableNestedGroup4);
+ focusManager.focusNode(testFocusableGroup2Node1);
+
+ focusManager.focusNode(testFocusableNestedGroup4Node1);
+
+ assert.strictEqual(
+ focusManager.getFocusedTree(),
+ testFocusableNestedGroup4,
+ );
+ });
+ });
+ suite('getFocusedNode()', function () {
+ test('registered tree focusTree()ed no prev focus returns root node', function () {
+ focusManager.registerTree(testFocusableGroup1);
+
+ focusManager.focusTree(testFocusableGroup1);
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup1.getRootFocusableNode(),
+ );
+ });
+
+ test('registered tree focusTree()ed prev node focused returns original node', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ focusManager.focusTree(testFocusableGroup1);
+
+ // The original node retains focus since the tree already holds focus (per focusTree's
+ // contract).
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup1Node1,
+ );
+ });
+
+ test('registered tree focusTree()ed diff tree prev focused returns new root node', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusTree(testFocusableGroup1);
+
+ focusManager.focusTree(testFocusableGroup2);
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup2.getRootFocusableNode(),
+ );
+ });
+
+ test('registered tree focusTree()ed diff tree node prev focused returns new root node', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ focusManager.focusTree(testFocusableGroup2);
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup2.getRootFocusableNode(),
+ );
+ });
+
+ test('registered root focusNode()ed no prev focus returns root node', function () {
+ focusManager.registerTree(testFocusableGroup1);
+
+ focusManager.focusNode(testFocusableGroup1.getRootFocusableNode());
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup1.getRootFocusableNode(),
+ );
+ });
+
+ test('registered node focusNode()ed no prev focus returns node', function () {
+ focusManager.registerTree(testFocusableGroup1);
+
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup1Node1,
+ );
+ });
+
+ test('registered subnode focusNode()ed no prev focus returns subnode', function () {
+ focusManager.registerTree(testFocusableGroup1);
+
+ focusManager.focusNode(testFocusableGroup1Node1Child1);
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup1Node1Child1,
+ );
+ });
+
+ test('registered node focusNode()ed after prev node focus returns new node', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ focusManager.focusNode(testFocusableGroup1Node2);
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup1Node2,
+ );
+ });
+
+ test('registered node focusNode()ed after prev node focus diff tree returns new node', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ focusManager.focusNode(testFocusableGroup2Node1);
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup2Node1,
+ );
+ });
+
+ test('registered tree root focusNode()ed after prev node focus diff tree returns new root', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ focusManager.focusNode(testFocusableGroup2.getRootFocusableNode());
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup2.getRootFocusableNode(),
+ );
+ });
+
+ test('unregistered tree focusTree()ed with no prev focus returns null', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.focusTree(testFocusableGroup1);
+
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ assert.isNull(focusManager.getFocusedNode());
+ });
+
+ test('unregistered tree focusNode()ed with no prev focus returns null', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ assert.isNull(focusManager.getFocusedNode());
+ });
+
+ test('unregistered tree focusNode()ed with prev node prior focused returns null', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableGroup2Node1);
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ // Since the more recent tree was removed, there's no tree currently focused.
+ assert.isNull(focusManager.getFocusedNode());
+ });
+
+ test('unregistered tree focusNode()ed with prev node recently focused returns new node', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableGroup1Node1);
+ focusManager.focusNode(testFocusableGroup2Node1);
+
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ // Since the most recent tree still exists, it still has focus.
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup2Node1,
+ );
+ });
+
+ test('nested tree focusTree()ed with no prev focus returns nested root', function () {
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.registerTree(testFocusableNestedGroup4);
+
+ focusManager.focusTree(testFocusableNestedGroup4);
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableNestedGroup4.getRootFocusableNode(),
+ );
+ });
+
+ test('nested tree node focusNode()ed with no prev focus returns focused node', function () {
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.registerTree(testFocusableNestedGroup4);
+
+ focusManager.focusNode(testFocusableNestedGroup4Node1);
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableNestedGroup4Node1,
+ );
+ });
+
+ test('nested tree node focusNode()ed after parent focused returns focused node', function () {
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.registerTree(testFocusableNestedGroup4);
+ focusManager.focusNode(testFocusableGroup2Node1);
+
+ focusManager.focusNode(testFocusableNestedGroup4Node1);
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableNestedGroup4Node1,
+ );
+ });
+ });
+ suite('CSS classes', function () {
+ test('registered tree focusTree()ed no prev focus root elem has active property', function () {
+ focusManager.registerTree(testFocusableGroup1);
+
+ focusManager.focusTree(testFocusableGroup1);
+
+ const rootElem = testFocusableGroup1
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered tree focusTree()ed prev node focused original elem has active property', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ focusManager.focusTree(testFocusableGroup1);
+
+ // The original node retains active focus since the tree already holds focus (per
+ // focusTree's contract).
+ const nodeElem = testFocusableGroup1Node1.getFocusableElement();
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered tree focusTree()ed diff tree prev focused new root elem has active property', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusTree(testFocusableGroup1);
+
+ focusManager.focusTree(testFocusableGroup2);
+
+ const rootElem = testFocusableGroup2
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered tree focusTree()ed diff tree node prev focused new root elem has active property', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ focusManager.focusTree(testFocusableGroup2);
+
+ const rootElem = testFocusableGroup2
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered root focusNode()ed no prev focus returns root elem has active property', function () {
+ focusManager.registerTree(testFocusableGroup1);
+
+ focusManager.focusNode(testFocusableGroup1.getRootFocusableNode());
+
+ const rootElem = testFocusableGroup1
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered node focusNode()ed no prev focus node elem has active property', function () {
+ focusManager.registerTree(testFocusableGroup1);
+
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ const nodeElem = testFocusableGroup1Node1.getFocusableElement();
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered node focusNode()ed after prev node focus same tree old node elem has no focus property', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ focusManager.focusNode(testFocusableGroup1Node2);
+
+ const prevNodeElem = testFocusableGroup1Node1.getFocusableElement();
+ assertNotIncludesClass(
+ prevNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ prevNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered node focusNode()ed after prev node focus same tree new node elem has active property', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ focusManager.focusNode(testFocusableGroup1Node2);
+
+ const newNodeElem = testFocusableGroup1Node2.getFocusableElement();
+ assertIncludesClass(
+ newNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ newNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered node focusNode()ed after prev node focus diff tree old node elem has passive property', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ focusManager.focusNode(testFocusableGroup2Node1);
+
+ const prevNodeElem = testFocusableGroup1Node1.getFocusableElement();
+ assertNotIncludesClass(
+ prevNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ prevNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered node focusNode()ed after prev node focus diff tree new node elem has active property', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ focusManager.focusNode(testFocusableGroup2Node1);
+
+ const newNodeElem = testFocusableGroup2Node1.getFocusableElement();
+ assertIncludesClass(
+ newNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ newNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered tree root focusNode()ed after prev node focus diff tree new root has active property', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ focusManager.focusNode(testFocusableGroup2.getRootFocusableNode());
+
+ const rootElem = testFocusableGroup2
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('unregistered tree focusTree()ed with no prev focus removes focus', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.focusTree(testFocusableGroup1);
+
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ // Since the tree was unregistered it no longer has focus indicators.
+ const rootElem = testFocusableGroup1
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('unregistered tree focusNode()ed with no prev focus removes focus', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ // Since the tree was unregistered it no longer has focus indicators.
+ const nodeElem = testFocusableGroup1Node1.getFocusableElement();
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('unregistered tree focusNode()ed with prev node prior removes focus from removed tree', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableGroup2Node1);
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ // Since the tree was unregistered it no longer has focus indicators. However, the old node
+ // should still have passive indication.
+ const otherNodeElem = testFocusableGroup2Node1.getFocusableElement();
+ const removedNodeElem = testFocusableGroup1Node1.getFocusableElement();
+ assertIncludesClass(
+ otherNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ otherNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ removedNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ removedNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('unregistered tree focusNode()ed with prev node recently removes focus from removed tree', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableGroup1Node1);
+ focusManager.focusNode(testFocusableGroup2Node1);
+
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ // Since the tree was unregistered it no longer has focus indicators. However, the new node
+ // should still have active indication.
+ const otherNodeElem = testFocusableGroup2Node1.getFocusableElement();
+ const removedNodeElem = testFocusableGroup1Node1.getFocusableElement();
+ assertIncludesClass(
+ otherNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ otherNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ removedNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ removedNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('focusNode() multiple nodes in same tree with switches ensure passive focus has gone', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableGroup1Node1);
+ focusManager.focusNode(testFocusableGroup2Node1);
+
+ focusManager.focusNode(testFocusableGroup1Node2);
+
+ // When switching back to the first tree, ensure the original passive node is no longer
+ // passive now that the new node is active.
+ const node1 = testFocusableGroup1Node1.getFocusableElement();
+ const node2 = testFocusableGroup1Node2.getFocusableElement();
+ assertNotIncludesClass(
+ node1.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ node2.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered tree focusTree()ed other tree node passively focused tree node now has active property', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableGroup1Node1);
+ focusManager.focusNode(testFocusableGroup2Node1);
+
+ focusManager.focusTree(testFocusableGroup1);
+
+ // The original node in the tree should be moved from passive to active focus per the
+ // contract of focusTree). Also, the root of the tree should have no focus indication.
+ const nodeElem = testFocusableGroup1Node1.getFocusableElement();
+ const rootElem = testFocusableGroup1
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('focus on root, node in diff tree, then node in first tree; root should have focus gone', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusTree(testFocusableGroup1);
+ focusManager.focusNode(testFocusableGroup2Node1);
+
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ const nodeElem = testFocusableGroup1Node1.getFocusableElement();
+ const rootElem = testFocusableGroup1
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('nested tree focusTree()ed with no prev root has active focus', function () {
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.registerTree(testFocusableNestedGroup4);
+
+ focusManager.focusTree(testFocusableNestedGroup4);
+
+ const rootElem = testFocusableNestedGroup4
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('nested tree node focusNode()ed with no prev focus node has active focus', function () {
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.registerTree(testFocusableNestedGroup4);
+
+ focusManager.focusNode(testFocusableNestedGroup4Node1);
+
+ const nodeElem = testFocusableNestedGroup4Node1.getFocusableElement();
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('nested tree node focusNode()ed after parent focused prev has passive node has active', function () {
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.registerTree(testFocusableNestedGroup4);
+ focusManager.focusNode(testFocusableGroup2Node1);
+
+ focusManager.focusNode(testFocusableNestedGroup4Node1);
+
+ const prevNodeElem = testFocusableGroup2Node1.getFocusableElement();
+ const currNodeElem =
+ testFocusableNestedGroup4Node1.getFocusableElement();
+ assertNotIncludesClass(
+ prevNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ prevNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ currNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ currNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+ });
+ });
+
+ suite('DOM focus() switching in SVG tree', function () {
+ suite('getFocusedTree()', function () {
+ test('registered root focus()ed no prev focus returns tree', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ document
+ .getElementById('testFocusableGroup1')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableGroup1')?.focus();
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableGroup1);
+ });
+
+ test("registered node focus()ed no prev focus returns node's tree", function () {
+ focusManager.registerTree(testFocusableGroup1);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableGroup1);
+ });
+
+ test("registered subnode focus()ed no prev focus returns node's tree", function () {
+ focusManager.registerTree(testFocusableGroup1);
+ document
+ .getElementById('testFocusableGroup1.node1.child1')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableGroup1.node1.child1')?.focus();
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableGroup1);
+ });
+
+ test('registered node focus()ed after prev node focus returns same tree', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup1.node2')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ document.getElementById('testFocusableGroup1.node2')?.focus();
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableGroup1);
+ });
+
+ test("registered node focus()ed after prev node focus diff tree returns new node's tree", function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ document.getElementById('testFocusableGroup2.node1')?.focus();
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableGroup2);
+ });
+
+ test("registered tree root focus()ed after prev node focus diff tree returns new node's tree", function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup2')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ document.getElementById('testFocusableGroup2')?.focus();
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableGroup2);
+ });
+
+ test("non-registered node subelement focus()ed returns node's tree", function () {
+ focusManager.registerTree(testFocusableGroup1);
+ document
+ .getElementById('testFocusableGroup1.node2.unregisteredChild1')
+ ?.setAttribute('tabindex', '-1');
+
+ document
+ .getElementById('testFocusableGroup1.node2.unregisteredChild1')
+ ?.focus();
+
+ // The tree of the unregistered child element should take focus.
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableGroup1);
+ });
+
+ test('non-registered tree focus()ed returns null', function () {
+ document
+ .getElementById('testUnregisteredFocusableGroup3')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testUnregisteredFocusableGroup3')?.focus();
+
+ assert.isNull(focusManager.getFocusedTree());
+ });
+
+ test('non-registered tree node focus()ed returns null', function () {
+ document
+ .getElementById('testUnregisteredFocusableGroup3.node1')
+ ?.setAttribute('tabindex', '-1');
+
+ document
+ .getElementById('testUnregisteredFocusableGroup3.node1')
+ ?.focus();
+
+ assert.isNull(focusManager.getFocusedTree());
+ });
+
+ test('non-registered tree node focus()ed after registered node focused returns null', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testUnregisteredFocusableGroup3.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ document
+ .getElementById('testUnregisteredFocusableGroup3.node1')
+ ?.focus();
+
+ // Attempting to focus a now removed tree should result in nothing being
+ // focused since the removed tree can have DOM focus, but that focus is
+ // ignored by FocusManager.
+ assert.isNull(focusManager.getFocusedTree());
+ });
+
+ test('unregistered tree focus()ed with no prev focus returns null', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ document
+ .getElementById('testFocusableGroup1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1')?.focus();
+
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ assert.isNull(focusManager.getFocusedTree());
+ });
+
+ test('unregistered tree focus()ed with no prev focus returns null', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ assert.isNull(focusManager.getFocusedTree());
+ });
+
+ test('unregistered tree focus()ed with prev node prior focused returns null', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ document
+ .getElementById('testFocusableGroup2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup2.node1')?.focus();
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ // Since the more recent tree was removed, there's no tree currently focused.
+ assert.isNull(focusManager.getFocusedTree());
+ });
+
+ test('unregistered tree focus()ed with prev node recently focused returns new tree', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+ document.getElementById('testFocusableGroup2.node1')?.focus();
+
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ // Since the most recent tree still exists, it still has focus.
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableGroup2);
+ });
+
+ test('unregistered tree focus()ed with prev node after unregistering returns null', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+ document.getElementById('testFocusableGroup2.node1')?.focus();
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ // Attempting to focus a now removed tree should result in nothing being
+ // focused since the removed tree can have DOM focus, but that focus is
+ // ignored by FocusManager.
+ assert.isNull(focusManager.getFocusedTree());
+ });
+
+ test('nested tree focusTree()ed with no prev focus returns nested tree', function () {
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.registerTree(testFocusableNestedGroup4);
+ document
+ .getElementById('testFocusableNestedGroup4')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableNestedGroup4')?.focus();
+
+ assert.strictEqual(
+ focusManager.getFocusedTree(),
+ testFocusableNestedGroup4,
+ );
+ });
+
+ test('nested tree node focusNode()ed with no prev focus returns nested tree', function () {
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.registerTree(testFocusableNestedGroup4);
+ document
+ .getElementById('testFocusableNestedGroup4.node1')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableNestedGroup4.node1')?.focus();
+
+ assert.strictEqual(
+ focusManager.getFocusedTree(),
+ testFocusableNestedGroup4,
+ );
+ });
+
+ test('nested tree node focusNode()ed after parent focused returns nested tree', function () {
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.registerTree(testFocusableNestedGroup4);
+ document
+ .getElementById('testFocusableGroup2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableNestedGroup4.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup2.node1')?.focus();
+
+ document.getElementById('testFocusableNestedGroup4.node1')?.focus();
+
+ assert.strictEqual(
+ focusManager.getFocusedTree(),
+ testFocusableNestedGroup4,
+ );
+ });
+ });
+ suite('getFocusedNode()', function () {
+ test('registered root focus()ed no prev focus returns root node', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ document
+ .getElementById('testFocusableGroup1')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableGroup1')?.focus();
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup1.getRootFocusableNode(),
+ );
+ });
+
+ test('registered node focus()ed no prev focus returns node', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup1Node1,
+ );
+ });
+
+ test('registered subnode focus()ed no prev focus returns subnode', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ document
+ .getElementById('testFocusableGroup1.node1.child1')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableGroup1.node1.child1')?.focus();
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup1Node1Child1,
+ );
+ });
+
+ test('registered node focus()ed after prev node focus returns new node', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup1.node2')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ document.getElementById('testFocusableGroup1.node2')?.focus();
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup1Node2,
+ );
+ });
+
+ test('registered node focus()ed after prev node focus diff tree returns new node', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ document.getElementById('testFocusableGroup2.node1')?.focus();
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup2Node1,
+ );
+ });
+
+ test('registered tree root focus()ed after prev node focus diff tree returns new root', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup2')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ document.getElementById('testFocusableGroup2')?.focus();
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup2.getRootFocusableNode(),
+ );
+ });
+
+ test('non-registered node subelement focus()ed returns nearest node', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ document
+ .getElementById('testFocusableGroup1.node2.unregisteredChild1')
+ ?.setAttribute('tabindex', '-1');
+
+ document
+ .getElementById('testFocusableGroup1.node2.unregisteredChild1')
+ ?.focus();
+
+ // The nearest node of the unregistered child element should take focus.
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup1Node2,
+ );
+ });
+
+ test('non-registered tree focus()ed returns null', function () {
+ document
+ .getElementById('testUnregisteredFocusableGroup3')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testUnregisteredFocusableGroup3')?.focus();
+
+ assert.isNull(focusManager.getFocusedNode());
+ });
+
+ test('non-registered tree node focus()ed returns null', function () {
+ document
+ .getElementById('testUnregisteredFocusableGroup3.node1')
+ ?.setAttribute('tabindex', '-1');
+
+ document
+ .getElementById('testUnregisteredFocusableGroup3.node1')
+ ?.focus();
+
+ assert.isNull(focusManager.getFocusedNode());
+ });
+
+ test('non-registered tree node focus()ed after registered node focused returns null', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testUnregisteredFocusableGroup3.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ document
+ .getElementById('testUnregisteredFocusableGroup3.node1')
+ ?.focus();
+
+ assert.isNull(focusManager.getFocusedNode());
+ });
+
+ test('unfocusable element focus()ed after registered node focused returns original node', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ document.getElementById('testUnfocusableElement')?.focus();
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup1Node1,
+ );
+ });
+
+ test('unregistered tree focus()ed with no prev focus returns null', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ document
+ .getElementById('testFocusableGroup1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1')?.focus();
+
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ assert.isNull(focusManager.getFocusedNode());
+ });
+
+ test('unregistered tree focus()ed with no prev focus returns null', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ assert.isNull(focusManager.getFocusedNode());
+ });
+
+ test('unregistered tree focus()ed with prev node prior focused returns null', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ document
+ .getElementById('testFocusableGroup2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup2.node1')?.focus();
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ // Since the more recent tree was removed, there's no tree currently focused.
+ assert.isNull(focusManager.getFocusedNode());
+ });
+
+ test('unregistered tree focus()ed with prev node recently focused returns new node', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+ document.getElementById('testFocusableGroup2.node1')?.focus();
+
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ // Since the most recent tree still exists, it still has focus.
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup2Node1,
+ );
+ });
+
+ test('unregistered tree focus()ed with prev node after unregistering returns null', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+ document.getElementById('testFocusableGroup2.node1')?.focus();
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ // Attempting to focus a now removed tree should result in nothing being
+ // focused since the removed tree can have DOM focus, but that focus is
+ // ignored by FocusManager.
+ assert.isNull(focusManager.getFocusedNode());
+ });
+
+ test('nested tree focus()ed with no prev focus returns nested root', function () {
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.registerTree(testFocusableNestedGroup4);
+ document
+ .getElementById('testFocusableNestedGroup4')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableNestedGroup4')?.focus();
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableNestedGroup4.getRootFocusableNode(),
+ );
+ });
+
+ test('nested tree node focus()ed with no prev focus returns focused node', function () {
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.registerTree(testFocusableNestedGroup4);
+ document
+ .getElementById('testFocusableNestedGroup4.node1')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableNestedGroup4.node1')?.focus();
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableNestedGroup4Node1,
+ );
+ });
+
+ test('nested tree node focus()ed after parent focused returns focused node', function () {
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.registerTree(testFocusableNestedGroup4);
+ document
+ .getElementById('testFocusableGroup2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableNestedGroup4.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup2.node1')?.focus();
+
+ document.getElementById('testFocusableNestedGroup4.node1')?.focus();
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableNestedGroup4Node1,
+ );
+ });
+
+ test('after focus() after trying to focusNode() an unfocusable node updates returns focus()ed node', function () {
+ testFocusableGroup1Node1.canBeFocused = () => false;
+ document
+ .getElementById('testFocusableGroup1.node2')
+ ?.setAttribute('tabindex', '-1');
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ document.getElementById('testFocusableGroup1.node2')?.focus();
+
+ // focus()ing a new node should overwrite a failed attempt to focusNode() an unfocusable
+ // node. This verifies that DOM focus syncing is properly reenabled by FocusManager.
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup1Node2,
+ );
+ });
+
+ test('after focus() after trying to focusNode() the same node twice returns focus()ed node', function () {
+ document
+ .getElementById('testFocusableGroup1.node2')
+ ?.setAttribute('tabindex', '-1');
+ focusManager.registerTree(testFocusableGroup1);
+ // Intentionally try to focus the same node twice.
+ focusManager.focusNode(testFocusableGroup1Node1);
+ focusManager.focusNode(testFocusableGroup1Node1);
+
+ document.getElementById('testFocusableGroup1.node2')?.focus();
+
+ // focus()ing a new node should overwrite a failed attempt to focusNode() the same node
+ // twice. This verifies that DOM focus syncing is properly reenabled by FocusManager.
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup1Node2,
+ );
+ });
+ });
+ suite('CSS classes', function () {
+ test('registered root focus()ed no prev focus returns root elem has active property', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ document
+ .getElementById('testFocusableGroup1')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableGroup1')?.focus();
+
+ const rootElem = testFocusableGroup1
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered node focus()ed no prev focus node elem has active property', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ const nodeElem = testFocusableGroup1Node1.getFocusableElement();
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered node focus()ed after prev node focus same tree old node elem has no focus property', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup1.node2')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ document.getElementById('testFocusableGroup1.node2')?.focus();
+
+ const prevNodeElem = testFocusableGroup1Node1.getFocusableElement();
+ assertNotIncludesClass(
+ prevNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ prevNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered node focus()ed after prev node focus same tree new node elem has active property', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup1.node2')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ document.getElementById('testFocusableGroup1.node2')?.focus();
+
+ const newNodeElem = testFocusableGroup1Node2.getFocusableElement();
+ assertIncludesClass(
+ newNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ newNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered node focus()ed after prev node focus diff tree old node elem has passive property', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ document.getElementById('testFocusableGroup2.node1')?.focus();
+
+ const prevNodeElem = testFocusableGroup1Node1.getFocusableElement();
+ assertNotIncludesClass(
+ prevNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ prevNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered node focus()ed after prev node focus diff tree new node elem has active property', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ document.getElementById('testFocusableGroup2.node1')?.focus();
+
+ const newNodeElem = testFocusableGroup2Node1.getFocusableElement();
+ assertIncludesClass(
+ newNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ newNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('registered tree root focus()ed after prev node focus diff tree new root has active property', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup2')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ document.getElementById('testFocusableGroup2')?.focus();
+
+ const rootElem = testFocusableGroup2
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('non-registered node subelement focus()ed nearest node has active property', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ document
+ .getElementById('testFocusableGroup1.node2.unregisteredChild1')
+ ?.setAttribute('tabindex', '-1');
+
+ document
+ .getElementById('testFocusableGroup1.node2.unregisteredChild1')
+ ?.focus();
+
+ // The nearest node of the unregistered child element should be actively focused.
+ const nodeElem = testFocusableGroup1Node2.getFocusableElement();
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('non-registered tree focus()ed has no focus', function () {
+ document
+ .getElementById('testUnregisteredFocusableGroup3')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testUnregisteredFocusableGroup3')?.focus();
+
+ assert.isNull(focusManager.getFocusedNode());
+ const rootElem = document.getElementById(
+ 'testUnregisteredFocusableGroup3',
+ );
+ assert.exists(rootElem);
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('non-registered tree node focus()ed has no focus', function () {
+ document
+ .getElementById('testUnregisteredFocusableGroup3.node1')
+ ?.setAttribute('tabindex', '-1');
+
+ document
+ .getElementById('testUnregisteredFocusableGroup3.node1')
+ ?.focus();
+
+ assert.isNull(focusManager.getFocusedNode());
+ const nodeElem = document.getElementById(
+ 'testUnregisteredFocusableGroup3.node1',
+ );
+ assert.exists(nodeElem);
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('unfocusable element focus()ed after registered node focused original node has active focus', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ document.getElementById('testUnfocusableElement')?.focus();
+
+ // The original node should be unchanged, and the unregistered node should not have any
+ // focus indicators.
+ const nodeElem = document.getElementById('testFocusableGroup1.node1');
+ const attemptedNewNodeElem = document.getElementById(
+ 'testUnfocusableElement',
+ );
+ assert.exists(nodeElem);
+ assert.exists(attemptedNewNodeElem);
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ attemptedNewNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ attemptedNewNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('unregistered tree focus()ed with no prev focus removes focus', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ document
+ .getElementById('testFocusableGroup1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1')?.focus();
+
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ // Since the tree was unregistered it no longer has focus indicators.
+ const rootElem = testFocusableGroup1
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('unregistered tree focus()ed with no prev focus removes focus', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ // Since the tree was unregistered it no longer has focus indicators.
+ const nodeElem = testFocusableGroup1Node1.getFocusableElement();
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('unregistered tree focus()ed with prev node prior removes focus from removed tree', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ document
+ .getElementById('testFocusableGroup2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup2.node1')?.focus();
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ // Since the tree was unregistered it no longer has focus indicators. However, the old node
+ // should still have passive indication.
+ const otherNodeElem = testFocusableGroup2Node1.getFocusableElement();
+ const removedNodeElem = testFocusableGroup1Node1.getFocusableElement();
+ assertIncludesClass(
+ otherNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ otherNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ removedNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ removedNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('unregistered tree focus()ed with prev node recently removes focus from removed tree', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+ document.getElementById('testFocusableGroup2.node1')?.focus();
+
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ // Since the tree was unregistered it no longer has focus indicators. However, the new node
+ // should still have active indication.
+ const otherNodeElem = testFocusableGroup2Node1.getFocusableElement();
+ const removedNodeElem = testFocusableGroup1Node1.getFocusableElement();
+ assertIncludesClass(
+ otherNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ otherNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ removedNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ removedNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('unregistered tree focus()ed with prev node after unregistering removes active indicator', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+ document.getElementById('testFocusableGroup2.node1')?.focus();
+ focusManager.unregisterTree(testFocusableGroup1);
+
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ // Attempting to focus a now removed tree should remove active.
+ const otherNodeElem = testFocusableGroup2Node1.getFocusableElement();
+ const removedNodeElem = testFocusableGroup1Node1.getFocusableElement();
+ assertNotIncludesClass(
+ otherNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ otherNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ removedNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ removedNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('focus() multiple nodes in same tree with switches ensure passive focus has gone', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup1.node2')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+ document.getElementById('testFocusableGroup2.node1')?.focus();
+
+ document.getElementById('testFocusableGroup1.node2')?.focus();
+
+ // When switching back to the first tree, ensure the original passive node is no longer
+ // passive now that the new node is active.
+ const node1 = testFocusableGroup1Node1.getFocusableElement();
+ const node2 = testFocusableGroup1Node2.getFocusableElement();
+ assertNotIncludesClass(
+ node1.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ node2.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('focus on root, node in diff tree, then node in first tree; root should have focus gone', function () {
+ focusManager.registerTree(testFocusableGroup1);
+ focusManager.registerTree(testFocusableGroup2);
+ document
+ .getElementById('testFocusableGroup1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup1.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup1')?.focus();
+ document.getElementById('testFocusableGroup2.node1')?.focus();
+
+ document.getElementById('testFocusableGroup1.node1')?.focus();
+
+ const nodeElem = testFocusableGroup1Node1.getFocusableElement();
+ const rootElem = testFocusableGroup1
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('nested tree focus()ed with no prev root has active focus', function () {
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.registerTree(testFocusableNestedGroup4);
+ document
+ .getElementById('testFocusableNestedGroup4')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableNestedGroup4')?.focus();
+
+ const rootElem = testFocusableNestedGroup4
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assertIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('nested tree node focus()ed with no prev focus node has active focus', function () {
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.registerTree(testFocusableNestedGroup4);
+ document
+ .getElementById('testFocusableNestedGroup4.node1')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableNestedGroup4.node1')?.focus();
+
+ const nodeElem = testFocusableNestedGroup4Node1.getFocusableElement();
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('nested tree node focus()ed after parent focused prev has passive node has active', function () {
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.registerTree(testFocusableNestedGroup4);
+ document
+ .getElementById('testFocusableGroup2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableNestedGroup4.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup2.node1')?.focus();
+
+ document.getElementById('testFocusableNestedGroup4.node1')?.focus();
+
+ const prevNodeElem = testFocusableGroup2Node1.getFocusableElement();
+ const currNodeElem =
+ testFocusableNestedGroup4Node1.getFocusableElement();
+ assertNotIncludesClass(
+ prevNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ prevNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ currNodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ currNodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+ });
+ });
+
+ /* High-level focus/defocusing tests. */
+ suite('Defocusing and refocusing', function () {
+ test('Defocusing actively focused root HTML tree switches to passive highlight', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusTree(testFocusableTree2);
+ document
+ .getElementById('testUnregisteredFocusableTree3')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testUnregisteredFocusableTree3')?.focus();
+
+ const rootNode = testFocusableTree2.getRootFocusableNode();
+ const rootElem = rootNode.getFocusableElement();
+ assert.isNull(focusManager.getFocusedTree());
+ assert.isNull(focusManager.getFocusedNode());
+ assertIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('Defocusing actively focused HTML tree node switches to passive highlight', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusNode(testFocusableTree2Node1);
+ document
+ .getElementById('testUnregisteredFocusableTree3')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testUnregisteredFocusableTree3')?.focus();
+
+ const nodeElem = testFocusableTree2Node1.getFocusableElement();
+ assert.isNull(focusManager.getFocusedTree());
+ assert.isNull(focusManager.getFocusedNode());
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('Defocusing actively focused HTML subtree node switches to passive highlight', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableNestedTree4);
+ focusManager.focusNode(testFocusableNestedTree4Node1);
+ document
+ .getElementById('testUnregisteredFocusableTree3')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testUnregisteredFocusableTree3')?.focus();
+
+ const nodeElem = testFocusableNestedTree4Node1.getFocusableElement();
+ assert.isNull(focusManager.getFocusedTree());
+ assert.isNull(focusManager.getFocusedNode());
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('Refocusing actively focused root HTML tree restores to active highlight', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusTree(testFocusableTree2);
+ document
+ .getElementById('testUnregisteredFocusableTree3')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree2')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testUnregisteredFocusableTree3')?.focus();
+
+ document.getElementById('testFocusableTree2')?.focus();
+
+ const rootNode = testFocusableTree2.getRootFocusableNode();
+ const rootElem = rootNode.getFocusableElement();
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableTree2);
+ assert.strictEqual(focusManager.getFocusedNode(), rootNode);
+ assertNotIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('Refocusing actively focused HTML tree node restores to active highlight', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.focusNode(testFocusableTree2Node1);
+ document
+ .getElementById('testUnregisteredFocusableTree3')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testUnregisteredFocusableTree3')?.focus();
+
+ document.getElementById('testFocusableTree2.node1')?.focus();
+
+ const nodeElem = testFocusableTree2Node1.getFocusableElement();
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableTree2);
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree2Node1,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('Refocusing actively focused HTML subtree node restores to active highlight', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableNestedTree4);
+ focusManager.focusNode(testFocusableNestedTree4Node1);
+ document
+ .getElementById('testUnregisteredFocusableTree3')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableNestedTree4.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testUnregisteredFocusableTree3')?.focus();
+
+ document.getElementById('testFocusableNestedTree4.node1')?.focus();
+
+ const nodeElem = testFocusableNestedTree4Node1.getFocusableElement();
+ assert.strictEqual(
+ focusManager.getFocusedTree(),
+ testFocusableNestedTree4,
+ );
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableNestedTree4Node1,
+ );
+ assertNotIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+ });
+
+ /* Combined HTML/SVG tree focus tests. */
+
+ suite('HTML/SVG focus tree switching', function () {
+ suite('Focus HTML tree then SVG tree', function () {
+ test('HTML focusTree()ed then SVG focusTree()ed correctly updates getFocusedTree() and indicators', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusTree(testFocusableTree2);
+
+ focusManager.focusTree(testFocusableGroup2);
+
+ const prevElem = testFocusableTree2
+ .getRootFocusableNode()
+ .getFocusableElement();
+ const currElem = testFocusableGroup2
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableGroup2);
+ assert.strictEqual(document.activeElement, currElem);
+ assertIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('HTML focusTree()ed then SVG focusNode()ed correctly updates getFocusedNode() and indicators', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusTree(testFocusableTree2);
+
+ focusManager.focusNode(testFocusableGroup2Node1);
+
+ const prevElem = testFocusableTree2
+ .getRootFocusableNode()
+ .getFocusableElement();
+ const currElem = testFocusableGroup2Node1.getFocusableElement();
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup2Node1,
+ );
+ assert.strictEqual(document.activeElement, currElem);
+ assertIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('HTML focusTree()ed then SVG DOM focus()ed correctly updates getFocusedNode() and indicators', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusTree(testFocusableTree2);
+ document
+ .getElementById('testFocusableGroup2.node1')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableGroup2.node1')?.focus();
+
+ const prevElem = testFocusableTree2
+ .getRootFocusableNode()
+ .getFocusableElement();
+ const currElem = testFocusableGroup2Node1.getFocusableElement();
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup2Node1,
+ );
+ assert.strictEqual(document.activeElement, currElem);
+ assertIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('HTML focusNode()ed then SVG focusTree()ed correctly updates getFocusedTree() and indicators', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableTree2Node1);
+
+ focusManager.focusTree(testFocusableGroup2);
+
+ const prevElem = testFocusableTree2Node1.getFocusableElement();
+ const currElem = testFocusableGroup2
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableGroup2);
+ assert.strictEqual(document.activeElement, currElem);
+ assertIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('HTML focusNode()ed then SVG focusNode()ed correctly updates getFocusedNode() and indicators', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableTree2Node1);
+
+ focusManager.focusNode(testFocusableGroup2Node1);
+
+ const prevElem = testFocusableTree2Node1.getFocusableElement();
+ const currElem = testFocusableGroup2Node1.getFocusableElement();
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup2Node1,
+ );
+ assert.strictEqual(document.activeElement, currElem);
+ assertIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('HTML focusNode()ed then SVG DOM focus()ed correctly updates getFocusedNode() and indicators', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableTree2Node1);
+ document
+ .getElementById('testFocusableGroup2.node1')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableGroup2.node1')?.focus();
+
+ const prevElem = testFocusableTree2Node1.getFocusableElement();
+ const currElem = testFocusableGroup2Node1.getFocusableElement();
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup2Node1,
+ );
+ assert.strictEqual(document.activeElement, currElem);
+ assertIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('HTML DOM focus()ed then SVG focusTree()ed correctly updates getFocusedTree() and indicators', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ document
+ .getElementById('testFocusableTree2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree2.node1')?.focus();
+
+ focusManager.focusTree(testFocusableGroup2);
+
+ const prevElem = testFocusableTree2Node1.getFocusableElement();
+ const currElem = testFocusableGroup2
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableGroup2);
+ assert.strictEqual(document.activeElement, currElem);
+ assertIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('HTML DOM focus()ed then SVG focusNode()ed correctly updates getFocusedNode() and indicators', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ document
+ .getElementById('testFocusableTree2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree2.node1')?.focus();
+
+ focusManager.focusNode(testFocusableGroup2Node1);
+
+ const prevElem = testFocusableTree2Node1.getFocusableElement();
+ const currElem = testFocusableGroup2Node1.getFocusableElement();
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup2Node1,
+ );
+ assert.strictEqual(document.activeElement, currElem);
+ assertIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('HTML DOM focus()ed then SVG DOM focus()ed correctly updates getFocusedNode() and indicators', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ document
+ .getElementById('testFocusableTree2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableGroup2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableTree2.node1')?.focus();
+
+ document.getElementById('testFocusableGroup2.node1')?.focus();
+
+ const prevElem = testFocusableTree2Node1.getFocusableElement();
+ const currElem = testFocusableGroup2Node1.getFocusableElement();
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup2Node1,
+ );
+ assert.strictEqual(document.activeElement, currElem);
+ assertIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+ });
+ suite('Focus SVG tree then HTML tree', function () {
+ test('SVG focusTree()ed then HTML focusTree()ed correctly updates getFocusedTree() and indicators', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusTree(testFocusableGroup2);
+
+ focusManager.focusTree(testFocusableTree2);
+
+ const prevElem = testFocusableGroup2
+ .getRootFocusableNode()
+ .getFocusableElement();
+ const currElem = testFocusableTree2
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableTree2);
+ assert.strictEqual(document.activeElement, currElem);
+ assertIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('SVG focusTree()ed then HTML focusNode()ed correctly updates getFocusedNode() and indicators', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusTree(testFocusableGroup2);
+
+ focusManager.focusNode(testFocusableTree2Node1);
+
+ const prevElem = testFocusableGroup2
+ .getRootFocusableNode()
+ .getFocusableElement();
+ const currElem = testFocusableTree2Node1.getFocusableElement();
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree2Node1,
+ );
+ assert.strictEqual(document.activeElement, currElem);
+ assertIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('SVG focusTree()ed then HTML DOM focus()ed correctly updates getFocusedNode() and indicators', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusTree(testFocusableGroup2);
+ document
+ .getElementById('testFocusableTree2.node1')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableTree2.node1')?.focus();
+
+ const prevElem = testFocusableGroup2
+ .getRootFocusableNode()
+ .getFocusableElement();
+ const currElem = testFocusableTree2Node1.getFocusableElement();
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree2Node1,
+ );
+ assert.strictEqual(document.activeElement, currElem);
+ assertIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('SVG focusNode()ed then HTML focusTree()ed correctly updates getFocusedTree() and indicators', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableGroup2Node1);
+
+ focusManager.focusTree(testFocusableTree2);
+
+ const prevElem = testFocusableGroup2Node1.getFocusableElement();
+ const currElem = testFocusableTree2
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableTree2);
+ assert.strictEqual(document.activeElement, currElem);
+ assertIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('SVG focusNode()ed then HTML focusNode()ed correctly updates getFocusedNode() and indicators', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableGroup2Node1);
+
+ focusManager.focusNode(testFocusableTree2Node1);
+
+ const prevElem = testFocusableGroup2Node1.getFocusableElement();
+ const currElem = testFocusableTree2Node1.getFocusableElement();
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree2Node1,
+ );
+ assert.strictEqual(document.activeElement, currElem);
+ assertIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('SVG focusNode()ed then HTML DOM focus()ed correctly updates getFocusedNode() and indicators', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableGroup2Node1);
+ document
+ .getElementById('testFocusableTree2.node1')
+ ?.setAttribute('tabindex', '-1');
+
+ document.getElementById('testFocusableTree2.node1')?.focus();
+
+ const prevElem = testFocusableGroup2Node1.getFocusableElement();
+ const currElem = testFocusableTree2Node1.getFocusableElement();
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree2Node1,
+ );
+ assert.strictEqual(document.activeElement, currElem);
+ assertIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('SVG DOM focus()ed then HTML focusTree()ed correctly updates getFocusedTree() and indicators', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ document
+ .getElementById('testFocusableGroup2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup2.node1')?.focus();
+
+ focusManager.focusTree(testFocusableTree2);
+
+ const prevElem = testFocusableGroup2Node1.getFocusableElement();
+ const currElem = testFocusableTree2
+ .getRootFocusableNode()
+ .getFocusableElement();
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableTree2);
+ assert.strictEqual(document.activeElement, currElem);
+ assertIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('SVG DOM focus()ed then HTML focusNode()ed correctly updates getFocusedNode() and indicators', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ document
+ .getElementById('testFocusableGroup2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup2.node1')?.focus();
+
+ focusManager.focusNode(testFocusableTree2Node1);
+
+ const prevElem = testFocusableGroup2Node1.getFocusableElement();
+ const currElem = testFocusableTree2Node1.getFocusableElement();
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree2Node1,
+ );
+ assert.strictEqual(document.activeElement, currElem);
+ assertIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('SVG DOM focus()ed then HTML DOM focus()ed correctly updates getFocusedNode() and indicators', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ document
+ .getElementById('testFocusableGroup2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('testFocusableTree2.node1')
+ ?.setAttribute('tabindex', '-1');
+ document.getElementById('testFocusableGroup2.node1')?.focus();
+
+ document.getElementById('testFocusableTree2.node1')?.focus();
+
+ const prevElem = testFocusableGroup2Node1.getFocusableElement();
+ const currElem = testFocusableTree2Node1.getFocusableElement();
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree2Node1,
+ );
+ assert.strictEqual(document.activeElement, currElem);
+ assertIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ currElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertNotIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assertIncludesClass(
+ prevElem.classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+ });
+ });
+
+ /* Ephemeral focus tests. */
+
+ suite('takeEphemeralFocus()', function () {
+ setup(function () {
+ // Ensure ephemeral-specific elements are focusable.
+ document
+ .getElementById('nonTreeElementForEphemeralFocus')
+ ?.setAttribute('tabindex', '-1');
+ document
+ .getElementById('nonTreeGroupForEphemeralFocus')
+ ?.setAttribute('tabindex', '-1');
+ });
+ teardown(function () {
+ // Ensure ephemeral-specific elements have their tab indexes reset for a clean state.
+ document
+ .getElementById('nonTreeElementForEphemeralFocus')
+ ?.removeAttribute('tabindex');
+ document
+ .getElementById('nonTreeGroupForEphemeralFocus')
+ ?.removeAttribute('tabindex');
+ });
+
+ test('with no focused node does not change states', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+
+ const ephemeralElement = document.getElementById(
+ 'nonTreeElementForEphemeralFocus',
+ );
+ assert.exists(ephemeralElement);
+ focusManager.takeEphemeralFocus(ephemeralElement);
+
+ // Taking focus without an existing node having focus should change no focus indicators.
+ const activeElems = Array.from(
+ document.querySelectorAll(ACTIVE_FOCUS_NODE_CSS_SELECTOR),
+ );
+ const passiveElems = Array.from(
+ document.querySelectorAll(PASSIVE_FOCUS_NODE_CSS_SELECTOR),
+ );
+ assert.isEmpty(activeElems);
+ assert.isEmpty(passiveElems);
+ });
+
+ test('with focused node changes focused node to passive', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableTree2Node1);
+
+ const ephemeralElement = document.getElementById(
+ 'nonTreeElementForEphemeralFocus',
+ );
+ assert.exists(ephemeralElement);
+ focusManager.takeEphemeralFocus(ephemeralElement);
+
+ // Taking focus without an existing node having focus should change no focus indicators.
+ const activeElems = Array.from(
+ document.querySelectorAll(ACTIVE_FOCUS_NODE_CSS_SELECTOR),
+ );
+ const passiveElems = Array.from(
+ document.querySelectorAll(PASSIVE_FOCUS_NODE_CSS_SELECTOR),
+ );
+ assert.isEmpty(activeElems);
+ assert.strictEqual(passiveElems.length, 1);
+ assertIncludesClass(
+ testFocusableTree2Node1.getFocusableElement().classList,
+ Blockly.FocusManager.PASSIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ });
+
+ test('focuses provided HTML element', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+
+ const ephemeralElement = document.getElementById(
+ 'nonTreeElementForEphemeralFocus',
+ );
+ assert.exists(ephemeralElement);
+ focusManager.takeEphemeralFocus(ephemeralElement);
+
+ assert.strictEqual(document.activeElement, ephemeralElement);
+ });
+
+ test('focuses provided SVG element', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+
+ const ephemeralElement = document.getElementById(
+ 'nonTreeGroupForEphemeralFocus',
+ );
+ assert.exists(ephemeralElement);
+ focusManager.takeEphemeralFocus(ephemeralElement);
+
+ assert.strictEqual(document.activeElement, ephemeralElement);
+ });
+
+ test('twice for without finishing previous throws error', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ const ephemeralGroupElement = document.getElementById(
+ 'nonTreeGroupForEphemeralFocus',
+ );
+ const ephemeralDivElement = document.getElementById(
+ 'nonTreeElementForEphemeralFocus',
+ );
+ assert.exists(ephemeralGroupElement);
+ focusManager.takeEphemeralFocus(ephemeralGroupElement);
+
+ assert.exists(ephemeralDivElement);
+ const errorMsgRegex =
+ /Attempted to take ephemeral focus when it's already held+?/;
+ assert.throws(
+ () => focusManager.takeEphemeralFocus(ephemeralDivElement),
+ errorMsgRegex,
+ );
+ });
+
+ test('then focusTree() changes getFocusedTree() but not active state', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusTree(testFocusableTree2);
+ const ephemeralElement = document.getElementById(
+ 'nonTreeGroupForEphemeralFocus',
+ );
+ assert.exists(ephemeralElement);
+ focusManager.takeEphemeralFocus(ephemeralElement);
+
+ focusManager.focusTree(testFocusableGroup2);
+
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableGroup2);
+ const activeElems = Array.from(
+ document.querySelectorAll(ACTIVE_FOCUS_NODE_CSS_SELECTOR),
+ );
+ assert.isEmpty(activeElems);
+ });
+
+ test('then focusNode() changes getFocusedNode() but not active state', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableTree2Node1);
+ const ephemeralElement = document.getElementById(
+ 'nonTreeGroupForEphemeralFocus',
+ );
+ assert.exists(ephemeralElement);
+ focusManager.takeEphemeralFocus(ephemeralElement);
+
+ focusManager.focusNode(testFocusableGroup2Node1);
+
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup2Node1,
+ );
+ const activeElems = Array.from(
+ document.querySelectorAll(ACTIVE_FOCUS_NODE_CSS_SELECTOR),
+ );
+ assert.isEmpty(activeElems);
+ });
+
+ test('then DOM refocus changes getFocusedNode() but not active state', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableTree2Node1);
+ document
+ .getElementById('testFocusableGroup2.node1')
+ ?.setAttribute('tabindex', '-1');
+ const ephemeralElement = document.getElementById(
+ 'nonTreeGroupForEphemeralFocus',
+ );
+ assert.exists(ephemeralElement);
+ focusManager.takeEphemeralFocus(ephemeralElement);
+
+ // Force focus to change via the DOM.
+ document.getElementById('testFocusableGroup2.node1')?.focus();
+
+ // The focus() state change will affect getFocusedNode() but it will not cause the node to now
+ // be active.
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup2Node1,
+ );
+ const activeElems = Array.from(
+ document.querySelectorAll(ACTIVE_FOCUS_NODE_CSS_SELECTOR),
+ );
+ assert.isEmpty(activeElems);
+ });
+
+ test('then finish ephemeral callback with no node does not change indicators', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ const ephemeralElement = document.getElementById(
+ 'nonTreeElementForEphemeralFocus',
+ );
+ assert.exists(ephemeralElement);
+ const finishFocusCallback =
+ focusManager.takeEphemeralFocus(ephemeralElement);
+
+ finishFocusCallback();
+
+ // Finishing ephemeral focus without a previously focused node should not change indicators.
+ const activeElems = Array.from(
+ document.querySelectorAll(ACTIVE_FOCUS_NODE_CSS_SELECTOR),
+ );
+ const passiveElems = Array.from(
+ document.querySelectorAll(PASSIVE_FOCUS_NODE_CSS_SELECTOR),
+ );
+ assert.isEmpty(activeElems);
+ assert.isEmpty(passiveElems);
+ });
+
+ test('again after finishing previous empheral focus should focus new element', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ const ephemeralGroupElement = document.getElementById(
+ 'nonTreeGroupForEphemeralFocus',
+ );
+ const ephemeralDivElement = document.getElementById(
+ 'nonTreeElementForEphemeralFocus',
+ );
+ assert.exists(ephemeralGroupElement);
+ const finishFocusCallback = focusManager.takeEphemeralFocus(
+ ephemeralGroupElement,
+ );
+
+ finishFocusCallback();
+ assert.exists(ephemeralDivElement);
+ focusManager.takeEphemeralFocus(ephemeralDivElement);
+
+ // An exception should not be thrown and the new element should receive focus.
+ assert.strictEqual(document.activeElement, ephemeralDivElement);
+ });
+
+ test('calling ephemeral callback twice throws error', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ const ephemeralElement = document.getElementById(
+ 'nonTreeElementForEphemeralFocus',
+ );
+ assert.exists(ephemeralElement);
+ const finishFocusCallback =
+ focusManager.takeEphemeralFocus(ephemeralElement);
+ finishFocusCallback();
+
+ const errorMsgRegex =
+ /Attempted to finish ephemeral focus twice for element+?/;
+ assert.throws(() => finishFocusCallback(), errorMsgRegex);
+ });
+
+ test('then finish ephemeral callback should restore focused node', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableTree2Node1);
+ const ephemeralElement = document.getElementById(
+ 'nonTreeGroupForEphemeralFocus',
+ );
+ assert.exists(ephemeralElement);
+ const finishFocusCallback =
+ focusManager.takeEphemeralFocus(ephemeralElement);
+
+ finishFocusCallback();
+
+ // The original focused node should be restored.
+ const nodeElem = testFocusableTree2Node1.getFocusableElement();
+ const activeElems = Array.from(
+ document.querySelectorAll(ACTIVE_FOCUS_NODE_CSS_SELECTOR),
+ );
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableTree2Node1,
+ );
+ assert.strictEqual(activeElems.length, 1);
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assert.strictEqual(document.activeElement, nodeElem);
+ });
+
+ test('then focusTree() and finish ephemeral callback correctly sets new active state', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusTree(testFocusableTree2);
+ const ephemeralElement = document.getElementById(
+ 'nonTreeGroupForEphemeralFocus',
+ );
+ assert.exists(ephemeralElement);
+ const finishFocusCallback =
+ focusManager.takeEphemeralFocus(ephemeralElement);
+
+ focusManager.focusTree(testFocusableGroup2);
+ finishFocusCallback();
+
+ // The tree's root should now be the active element since focus changed between the start and
+ // end of the ephemeral flow.
+ const rootElem = testFocusableGroup2
+ .getRootFocusableNode()
+ .getFocusableElement();
+ const activeElems = Array.from(
+ document.querySelectorAll(ACTIVE_FOCUS_NODE_CSS_SELECTOR),
+ );
+ assert.strictEqual(focusManager.getFocusedTree(), testFocusableGroup2);
+ assert.strictEqual(activeElems.length, 1);
+ assertIncludesClass(
+ rootElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assert.strictEqual(document.activeElement, rootElem);
+ });
+
+ test('then focusNode() and finish ephemeral callback correctly sets new active state', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableTree2Node1);
+ const ephemeralElement = document.getElementById(
+ 'nonTreeGroupForEphemeralFocus',
+ );
+ assert.exists(ephemeralElement);
+ const finishFocusCallback =
+ focusManager.takeEphemeralFocus(ephemeralElement);
+
+ focusManager.focusNode(testFocusableGroup2Node1);
+ finishFocusCallback();
+
+ // The tree's root should now be the active element since focus changed between the start and
+ // end of the ephemeral flow.
+ const nodeElem = testFocusableGroup2Node1.getFocusableElement();
+ const activeElems = Array.from(
+ document.querySelectorAll(ACTIVE_FOCUS_NODE_CSS_SELECTOR),
+ );
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup2Node1,
+ );
+ assert.strictEqual(activeElems.length, 1);
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assert.strictEqual(document.activeElement, nodeElem);
+ });
+
+ test('then DOM focus change and finish ephemeral callback correctly sets new active state', function () {
+ focusManager.registerTree(testFocusableTree2);
+ focusManager.registerTree(testFocusableGroup2);
+ focusManager.focusNode(testFocusableTree2Node1);
+ document
+ .getElementById('testFocusableGroup2.node1')
+ ?.setAttribute('tabindex', '-1');
+ const ephemeralElement = document.getElementById(
+ 'nonTreeGroupForEphemeralFocus',
+ );
+ assert.exists(ephemeralElement);
+ const finishFocusCallback =
+ focusManager.takeEphemeralFocus(ephemeralElement);
+
+ document.getElementById('testFocusableGroup2.node1')?.focus();
+ finishFocusCallback();
+
+ // The tree's root should now be the active element since focus changed between the start and
+ // end of the ephemeral flow.
+ const nodeElem = testFocusableGroup2Node1.getFocusableElement();
+ const activeElems = Array.from(
+ document.querySelectorAll(ACTIVE_FOCUS_NODE_CSS_SELECTOR),
+ );
+ assert.strictEqual(
+ focusManager.getFocusedNode(),
+ testFocusableGroup2Node1,
+ );
+ assert.strictEqual(activeElems.length, 1);
+ assertIncludesClass(
+ nodeElem.classList,
+ Blockly.FocusManager.ACTIVE_FOCUS_NODE_CSS_CLASS_NAME,
+ );
+ assert.strictEqual(document.activeElement, nodeElem);
+ });
+ });
+});
diff --git a/packages/blockly/tests/mocha/focusable_tree_traverser_test.js b/packages/blockly/tests/mocha/focusable_tree_traverser_test.js
index e22c0ee8c7c..e0e751b6644 100644
--- a/packages/blockly/tests/mocha/focusable_tree_traverser_test.js
+++ b/packages/blockly/tests/mocha/focusable_tree_traverser_test.js
@@ -7,7 +7,7 @@
import {FocusManager} from '#core/focus_manager.js';
import {FocusableTreeTraverser} from '#core/utils/focusable_tree_traverser.js';
import {assert} from 'chai';
-import {FocusableTreeImpl} from './test_helpers/focusable_tree.js';
+import {FocusableTreeImpl} from './test_helpers/focusable_tree.ts';
import {
sharedTestSetup,
sharedTestTeardown,
diff --git a/packages/blockly/tests/mocha/gesture_test.js b/packages/blockly/tests/mocha/gesture_test.js
index f831d3568a6..1a284c18c89 100644
--- a/packages/blockly/tests/mocha/gesture_test.js
+++ b/packages/blockly/tests/mocha/gesture_test.js
@@ -6,15 +6,15 @@
import {EventType} from '#core/events/type.js';
import {assert} from 'chai';
-import {defineBasicBlockWithField} from './test_helpers/block_definitions.js';
+import {defineBasicBlockWithField} from './test_helpers/block_definitions.ts';
import {assertEventFired, assertEventNotFired} from './test_helpers/events.js';
import {
DEFAULT_INJECT_OPTIONS,
sharedTestSetup,
sharedTestTeardown,
} from './test_helpers/setup_teardown.js';
-import {getProperSimpleJson} from './test_helpers/toolbox_definitions.js';
-import {dispatchPointerEvent} from './test_helpers/user_input.js';
+import {getProperSimpleJson} from './test_helpers/toolbox_definitions.ts';
+import {dispatchPointerEvent} from './test_helpers/user_input.ts';
suite('Gesture', function () {
function testGestureIsFieldClick(block, isFieldClick, eventsFireStub) {
diff --git a/packages/blockly/tests/mocha/icon_test.js b/packages/blockly/tests/mocha/icon_test.js
index 67e45635c9a..704d343364c 100644
--- a/packages/blockly/tests/mocha/icon_test.js
+++ b/packages/blockly/tests/mocha/icon_test.js
@@ -6,7 +6,7 @@
import * as Blockly from '#core/blockly.js';
import {assert} from 'chai';
-import {defineEmptyBlock} from './test_helpers/block_definitions.js';
+import {defineEmptyBlock} from './test_helpers/block_definitions.ts';
import {MockIcon, MockSerializableIcon} from './test_helpers/icon_mocks.js';
import {
DEFAULT_INJECT_OPTIONS,
@@ -14,7 +14,7 @@ import {
sharedTestTeardown,
workspaceTeardown,
} from './test_helpers/setup_teardown.js';
-import {simulateClick} from './test_helpers/user_input.js';
+import {simulateClick} from './test_helpers/user_input.ts';
class TestIcon extends Blockly.icons.Icon {
showContextMenu(e) {
diff --git a/packages/blockly/tests/mocha/input_test.js b/packages/blockly/tests/mocha/input_test.js
index 02a0ca7ff0c..a56cb651dd4 100644
--- a/packages/blockly/tests/mocha/input_test.js
+++ b/packages/blockly/tests/mocha/input_test.js
@@ -5,7 +5,7 @@
*/
import {assert} from 'chai';
-import {createRenderedBlock} from './test_helpers/block_definitions.js';
+import {createRenderedBlock} from './test_helpers/block_definitions.ts';
import {
DEFAULT_INJECT_OPTIONS,
sharedTestSetup,
diff --git a/packages/blockly/tests/mocha/jso_deserialization_test.js b/packages/blockly/tests/mocha/jso_deserialization_test.ts
similarity index 77%
rename from packages/blockly/tests/mocha/jso_deserialization_test.js
rename to packages/blockly/tests/mocha/jso_deserialization_test.ts
index 5260922cbd9..50db1aa3a30 100644
--- a/packages/blockly/tests/mocha/jso_deserialization_test.js
+++ b/packages/blockly/tests/mocha/jso_deserialization_test.ts
@@ -4,8 +4,9 @@
* SPDX-License-Identifier: Apache-2.0
*/
-import {EventType} from '#core/events/type.js';
+import * as Blockly from '#core/blockly.js';
import {assert} from 'chai';
+import sinon from 'sinon';
import {assertEventFired} from './test_helpers/events.js';
import {
MockParameterModel,
@@ -17,14 +18,18 @@ import {
} from './test_helpers/setup_teardown.js';
suite('JSO Deserialization', function () {
- setup(function () {
- sharedTestSetup.call(this);
- this.sandbox = sinon.createSandbox();
- this.workspace = new Blockly.Workspace();
+ let workspace: Blockly.Workspace;
+ let sandbox: sinon.SinonSandbox;
+ let eventsFireStub: sinon.SinonStub;
+
+ setup(function (this: Mocha.Context) {
+ ({eventsFireStub} = sharedTestSetup.call(this));
+ sandbox = sinon.createSandbox();
+ workspace = new Blockly.Workspace();
});
- teardown(function () {
- this.sandbox.restore();
+ teardown(function (this: Mocha.Context) {
+ sandbox.restore();
sharedTestTeardown.call(this);
});
@@ -40,7 +45,7 @@ suite('JSO Deserialization', function () {
},
};
assert.throws(() => {
- Blockly.serialization.workspaces.load(state, this.workspace);
+ Blockly.serialization.workspaces.load(state, workspace);
});
assert.isTrue(
Blockly.Events.isEnabled(),
@@ -62,12 +67,12 @@ suite('JSO Deserialization', function () {
],
},
};
- Blockly.serialization.workspaces.load(state, this.workspace);
+ Blockly.serialization.workspaces.load(state, workspace);
assertEventFired(
- this.eventsFireStub,
+ eventsFireStub,
Blockly.Events.FinishedLoading,
- {type: EventType.FINISHED_LOADING},
- this.workspace.id,
+ {type: Blockly.Events.FINISHED_LOADING},
+ workspace.id,
);
});
@@ -85,12 +90,12 @@ suite('JSO Deserialization', function () {
},
};
Blockly.Events.setGroup('my group');
- Blockly.serialization.workspaces.load(state, this.workspace);
+ Blockly.serialization.workspaces.load(state, workspace);
assertEventFired(
- this.eventsFireStub,
+ eventsFireStub,
Blockly.Events.FinishedLoading,
- {'group': 'my group', 'type': EventType.FINISHED_LOADING},
- this.workspace.id,
+ {'group': 'my group', 'type': Blockly.Events.FINISHED_LOADING},
+ workspace.id,
);
});
@@ -118,8 +123,8 @@ suite('JSO Deserialization', function () {
],
},
};
- Blockly.serialization.workspaces.load(state, this.workspace);
- const calls = this.eventsFireStub.getCalls();
+ Blockly.serialization.workspaces.load(state, workspace);
+ const calls = eventsFireStub.getCalls();
const group = calls[0].args[0].group;
assert.isTrue(calls.every((call) => call.args[0].group == group));
});
@@ -135,18 +140,18 @@ suite('JSO Deserialization', function () {
},
],
};
- Blockly.serialization.workspaces.load(state, this.workspace);
+ Blockly.serialization.workspaces.load(state, workspace);
assertEventFired(
- this.eventsFireStub,
+ eventsFireStub,
Blockly.Events.VarCreate,
{
'varName': 'test',
'varId': 'testId',
'varType': '',
'recordUndo': false,
- 'type': EventType.VAR_CREATE,
+ 'type': Blockly.Events.VAR_CREATE,
},
- this.workspace.id,
+ workspace.id,
);
});
@@ -159,20 +164,20 @@ suite('JSO Deserialization', function () {
},
],
};
- Blockly.serialization.workspaces.load(state, this.workspace, {
+ Blockly.serialization.workspaces.load(state, workspace, {
recordUndo: true,
});
assertEventFired(
- this.eventsFireStub,
+ eventsFireStub,
Blockly.Events.VarCreate,
{
'varName': 'test',
'varId': 'testId',
'varType': '',
'recordUndo': true,
- 'type': EventType.VAR_CREATE,
+ 'type': Blockly.Events.VAR_CREATE,
},
- this.workspace.id,
+ workspace.id,
);
});
@@ -186,18 +191,18 @@ suite('JSO Deserialization', function () {
],
};
Blockly.Events.setGroup('my group');
- Blockly.serialization.workspaces.load(state, this.workspace);
+ Blockly.serialization.workspaces.load(state, workspace);
assertEventFired(
- this.eventsFireStub,
+ eventsFireStub,
Blockly.Events.VarCreate,
{
'varName': 'test',
'varId': 'testId',
'varType': '',
'group': 'my group',
- 'type': EventType.VAR_CREATE,
+ 'type': Blockly.Events.VAR_CREATE,
},
- this.workspace.id,
+ workspace.id,
);
});
@@ -214,8 +219,8 @@ suite('JSO Deserialization', function () {
},
],
};
- Blockly.serialization.workspaces.load(state, this.workspace);
- const calls = this.eventsFireStub.getCalls();
+ Blockly.serialization.workspaces.load(state, workspace);
+ const calls = eventsFireStub.getCalls();
const group = calls[0].args[0].group;
assert.isTrue(calls.every((call) => call.args[0].group == group));
});
@@ -244,8 +249,8 @@ suite('JSO Deserialization', function () {
],
},
};
- Blockly.serialization.workspaces.load(state, this.workspace);
- const calls = this.eventsFireStub.getCalls();
+ Blockly.serialization.workspaces.load(state, workspace);
+ const calls = eventsFireStub.getCalls();
const count = calls.reduce((acc, call) => {
if (call.args[0] instanceof Blockly.Events.VarCreate) {
return acc + 1;
@@ -254,15 +259,15 @@ suite('JSO Deserialization', function () {
}, 0);
assert.equal(count, 1);
assertEventFired(
- this.eventsFireStub,
+ eventsFireStub,
Blockly.Events.VarCreate,
{
'varName': 'test',
'varId': 'testId',
'varType': '',
- 'type': EventType.VAR_CREATE,
+ 'type': Blockly.Events.VAR_CREATE,
},
- this.workspace.id,
+ workspace.id,
);
});
});
@@ -282,12 +287,12 @@ suite('JSO Deserialization', function () {
],
},
};
- Blockly.serialization.workspaces.load(state, this.workspace);
+ Blockly.serialization.workspaces.load(state, workspace);
assertEventFired(
- this.eventsFireStub,
+ eventsFireStub,
Blockly.Events.BlockCreate,
- {'recordUndo': false, 'type': EventType.BLOCK_CREATE},
- this.workspace.id,
+ {'recordUndo': false, 'type': Blockly.Events.BLOCK_CREATE},
+ workspace.id,
'testId',
);
});
@@ -305,14 +310,14 @@ suite('JSO Deserialization', function () {
],
},
};
- Blockly.serialization.workspaces.load(state, this.workspace, {
+ Blockly.serialization.workspaces.load(state, workspace, {
'recordUndo': true,
});
assertEventFired(
- this.eventsFireStub,
+ eventsFireStub,
Blockly.Events.BlockCreate,
- {'recordUndo': true, 'type': EventType.BLOCK_CREATE},
- this.workspace.id,
+ {'recordUndo': true, 'type': Blockly.Events.BLOCK_CREATE},
+ workspace.id,
'testId',
);
});
@@ -331,12 +336,12 @@ suite('JSO Deserialization', function () {
},
};
Blockly.Events.setGroup('my group');
- Blockly.serialization.workspaces.load(state, this.workspace);
+ Blockly.serialization.workspaces.load(state, workspace);
assertEventFired(
- this.eventsFireStub,
+ eventsFireStub,
Blockly.Events.BlockCreate,
- {'group': 'my group', 'type': EventType.BLOCK_CREATE},
- this.workspace.id,
+ {'group': 'my group', 'type': Blockly.Events.BLOCK_CREATE},
+ workspace.id,
'testId',
);
});
@@ -360,8 +365,8 @@ suite('JSO Deserialization', function () {
],
},
};
- Blockly.serialization.workspaces.load(state, this.workspace);
- const calls = this.eventsFireStub.getCalls();
+ Blockly.serialization.workspaces.load(state, workspace);
+ const calls = eventsFireStub.getCalls();
const group = calls[0].args[0].group;
assert.isTrue(calls.every((call) => call.args[0].group == group));
});
@@ -393,12 +398,12 @@ suite('JSO Deserialization', function () {
],
},
};
- Blockly.serialization.workspaces.load(state, this.workspace);
+ Blockly.serialization.workspaces.load(state, workspace);
assertEventFired(
- this.eventsFireStub,
+ eventsFireStub,
Blockly.Events.BlockCreate,
- {type: EventType.BLOCK_CREATE},
- this.workspace.id,
+ {type: Blockly.Events.BLOCK_CREATE},
+ workspace.id,
'id1',
);
});
@@ -412,12 +417,12 @@ suite('JSO Deserialization', function () {
'x': 42,
'y': 42,
};
- Blockly.serialization.blocks.append(state, this.workspace);
+ Blockly.serialization.blocks.append(state, workspace);
assertEventFired(
- this.eventsFireStub,
+ eventsFireStub,
Blockly.Events.BlockCreate,
{'recordUndo': false},
- this.workspace.id,
+ workspace.id,
'testId',
);
});
@@ -429,14 +434,14 @@ suite('JSO Deserialization', function () {
'x': 42,
'y': 42,
};
- Blockly.serialization.blocks.append(state, this.workspace, {
+ Blockly.serialization.blocks.append(state, workspace, {
'recordUndo': true,
});
assertEventFired(
- this.eventsFireStub,
+ eventsFireStub,
Blockly.Events.BlockCreate,
- {'recordUndo': true, 'type': EventType.BLOCK_CREATE},
- this.workspace.id,
+ {'recordUndo': true, 'type': Blockly.Events.BLOCK_CREATE},
+ workspace.id,
'testId',
);
});
@@ -449,12 +454,12 @@ suite('JSO Deserialization', function () {
'y': 42,
};
Blockly.Events.setGroup('my group');
- Blockly.serialization.blocks.append(state, this.workspace);
+ Blockly.serialization.blocks.append(state, workspace);
assertEventFired(
- this.eventsFireStub,
+ eventsFireStub,
Blockly.Events.BlockCreate,
- {'group': 'my group', 'type': EventType.BLOCK_CREATE},
- this.workspace.id,
+ {'group': 'my group', 'type': Blockly.Events.BLOCK_CREATE},
+ workspace.id,
'testId',
);
});
@@ -463,13 +468,14 @@ suite('JSO Deserialization', function () {
});
suite('Exceptions', function () {
- setup(function () {
- this.assertThrows = function (state, error) {
- assert.throws(() => {
- Blockly.serialization.workspaces.load(state, this.workspace);
- }, error);
- };
- });
+ function assertThrows(
+ state: {[key: string]: any},
+ error: new (...args: any[]) => Error,
+ ) {
+ assert.throws(() => {
+ Blockly.serialization.workspaces.load(state, workspace);
+ }, error);
+ }
suite('Undefined block type', function () {
test('Name', function () {
@@ -482,7 +488,7 @@ suite('JSO Deserialization', function () {
],
},
};
- this.assertThrows(state, TypeError);
+ assertThrows(state, TypeError);
});
test('Casing', function () {
@@ -495,7 +501,7 @@ suite('JSO Deserialization', function () {
],
},
};
- this.assertThrows(state, TypeError);
+ assertThrows(state, TypeError);
});
});
@@ -517,10 +523,7 @@ suite('JSO Deserialization', function () {
],
},
};
- this.assertThrows(
- state,
- Blockly.serialization.exceptions.MissingConnection,
- );
+ assertThrows(state, Blockly.serialization.exceptions.MissingConnection);
});
test('Input casing', function () {
@@ -540,10 +543,7 @@ suite('JSO Deserialization', function () {
],
},
};
- this.assertThrows(
- state,
- Blockly.serialization.exceptions.MissingConnection,
- );
+ assertThrows(state, Blockly.serialization.exceptions.MissingConnection);
});
test('Next', function () {
@@ -561,10 +561,7 @@ suite('JSO Deserialization', function () {
],
},
};
- this.assertThrows(
- state,
- Blockly.serialization.exceptions.MissingConnection,
- );
+ assertThrows(state, Blockly.serialization.exceptions.MissingConnection);
});
test('Previous', function () {
@@ -582,10 +579,7 @@ suite('JSO Deserialization', function () {
],
},
};
- this.assertThrows(
- state,
- Blockly.serialization.exceptions.MissingConnection,
- );
+ assertThrows(state, Blockly.serialization.exceptions.MissingConnection);
});
test('Output', function () {
@@ -605,10 +599,7 @@ suite('JSO Deserialization', function () {
],
},
};
- this.assertThrows(
- state,
- Blockly.serialization.exceptions.MissingConnection,
- );
+ assertThrows(state, Blockly.serialization.exceptions.MissingConnection);
});
});
@@ -630,7 +621,7 @@ suite('JSO Deserialization', function () {
],
},
};
- this.assertThrows(
+ assertThrows(
state,
Blockly.serialization.exceptions.BadConnectionCheck,
);
@@ -662,10 +653,7 @@ suite('JSO Deserialization', function () {
],
},
};
- this.assertThrows(
- state,
- Blockly.serialization.exceptions.RealChildOfShadow,
- );
+ assertThrows(state, Blockly.serialization.exceptions.RealChildOfShadow);
});
test('Next', function () {
@@ -688,28 +676,27 @@ suite('JSO Deserialization', function () {
],
},
};
- this.assertThrows(
- state,
- Blockly.serialization.exceptions.RealChildOfShadow,
- );
+ assertThrows(state, Blockly.serialization.exceptions.RealChildOfShadow);
});
});
});
test('Priority', function () {
- const blocksSerializer = Blockly.registry.getClass(
+ const blocksSerializer = Blockly.registry.getObject(
Blockly.registry.Type.SERIALIZER,
'blocks',
);
- const variablesSerializer = Blockly.registry.getClass(
+ const variablesSerializer = Blockly.registry.getObject(
Blockly.registry.Type.SERIALIZER,
'variables',
);
+ assert.exists(blocksSerializer);
+ assert.exists(variablesSerializer);
Blockly.serialization.registry.unregister('blocks');
Blockly.serialization.registry.unregister('variables');
- const calls = [];
+ const calls: string[] = [];
const first = {
priority: 100,
@@ -736,7 +723,7 @@ suite('JSO Deserialization', function () {
Blockly.serialization.workspaces.load(
{'first': {}, 'third': {}, 'second': {}},
- this.workspace,
+ workspace,
);
Blockly.serialization.registry.unregister('first');
@@ -760,6 +747,7 @@ suite('JSO Deserialization', function () {
// Most of this is covered by our round-trip tests. But we need one test
// for old xml hooks.
test('Xml hooks', function () {
+ type TestBlock = Blockly.Block & {someProperty: string | null};
Blockly.Blocks['test_block'] = {
init: function () {},
@@ -769,7 +757,7 @@ suite('JSO Deserialization', function () {
return container;
},
- domToMutation: function (element) {
+ domToMutation: function (this: TestBlock, element: Element) {
this.someProperty = element.getAttribute('value');
},
};
@@ -779,28 +767,27 @@ suite('JSO Deserialization', function () {
'type': 'test_block',
'extraState': '',
},
- this.workspace,
+ workspace,
);
delete Blockly.Blocks['test_block'];
- assert.equal(block.someProperty, 'some value');
+ assert.equal((block as TestBlock).someProperty, 'some value');
});
});
suite('Procedures', function () {
+ let procedureSerializer: Blockly.serialization.procedures.ProcedureSerializer<
+ MockProcedureModel,
+ MockParameterModel
+ >;
+
setup(function () {
- this.procedureSerializer =
+ procedureSerializer =
new Blockly.serialization.procedures.ProcedureSerializer(
MockProcedureModel,
MockParameterModel,
);
- this.procedureMap = this.workspace.getProcedureMap();
- });
-
- teardown(function () {
- this.procedureSerializer = null;
- this.procedureMap = null;
});
test('load is called for the procedure model', function () {
@@ -808,11 +795,13 @@ suite('JSO Deserialization', function () {
{
'id': 'test',
'parameters': [],
+ 'name': 'foo',
+ 'returnTypes': [''],
},
];
- const spy = this.sandbox.spy(MockProcedureModel, 'loadState');
+ const spy = sandbox.spy(MockProcedureModel, 'loadState');
- this.procedureSerializer.load(state, this.workspace);
+ procedureSerializer.load(state, workspace);
assert.isTrue(
spy.calledOnce,
@@ -827,17 +816,21 @@ suite('JSO Deserialization', function () {
'parameters': [
{
'id': 'test1',
+ 'name': 'x',
},
{
'id': 'test2',
+ 'name': 'y',
},
],
+ 'name': 'foo',
+ 'returnTypes': [''],
},
];
- const spy = this.sandbox.spy(MockParameterModel, 'loadState');
+ const spy = sandbox.spy(MockParameterModel, 'loadState');
- this.procedureSerializer.load(state, this.workspace);
+ procedureSerializer.load(state, workspace);
assert.isTrue(
spy.calledTwice,
diff --git a/packages/blockly/tests/mocha/jso_serialization_test.js b/packages/blockly/tests/mocha/jso_serialization_test.js
index 4a59ccfdccb..52079cf8af6 100644
--- a/packages/blockly/tests/mocha/jso_serialization_test.js
+++ b/packages/blockly/tests/mocha/jso_serialization_test.js
@@ -10,7 +10,7 @@ import {
defineRowBlock,
defineStackBlock,
defineStatementBlock,
-} from './test_helpers/block_definitions.js';
+} from './test_helpers/block_definitions.ts';
import {
MockParameterModel,
MockProcedureModel,
diff --git a/packages/blockly/tests/mocha/keyboard_movement_test.js b/packages/blockly/tests/mocha/keyboard_movement_test.js
index 6fbcad7ba46..ba42b1c178f 100644
--- a/packages/blockly/tests/mocha/keyboard_movement_test.js
+++ b/packages/blockly/tests/mocha/keyboard_movement_test.js
@@ -17,7 +17,7 @@ import {
sharedTestTeardown,
workspaceTeardown,
} from './test_helpers/setup_teardown.js';
-import {createKeyDownEvent} from './test_helpers/user_input.js';
+import {createKeyDownEvent} from './test_helpers/user_input.ts';
suite('Keyboard-driven movement', function () {
setup(function () {
diff --git a/packages/blockly/tests/mocha/keyboard_navigation_test.ts b/packages/blockly/tests/mocha/keyboard_navigation_test.ts
index 86bfb0a0b4f..a78a01fe565 100644
--- a/packages/blockly/tests/mocha/keyboard_navigation_test.ts
+++ b/packages/blockly/tests/mocha/keyboard_navigation_test.ts
@@ -7,7 +7,7 @@
import * as Blockly from '#core/blockly.js';
import {assert} from 'chai';
import sinon from 'sinon';
-import {defineStackBlock} from './test_helpers/block_definitions.js';
+import {defineStackBlock} from './test_helpers/block_definitions.ts';
import {navigationTestBlocks} from './test_helpers/navigation_test_blocks.ts';
import {p5blocks} from './test_helpers/p5_blocks.ts';
import {
@@ -16,7 +16,7 @@ import {
sharedTestTeardown,
workspaceTeardown,
} from './test_helpers/setup_teardown.js';
-import {createKeyDownEvent} from './test_helpers/user_input.js';
+import {createKeyDownEvent} from './test_helpers/user_input.ts';
/**
* Dispatches a keydown event with the given keycode on the workspace injection
diff --git a/packages/blockly/tests/mocha/mutator_test.js b/packages/blockly/tests/mocha/mutator_test.js
index 3278af6b58b..b42a76b13a7 100644
--- a/packages/blockly/tests/mocha/mutator_test.js
+++ b/packages/blockly/tests/mocha/mutator_test.js
@@ -8,7 +8,7 @@ import {assert} from 'chai';
import {
createRenderedBlock,
defineMutatorBlocks,
-} from './test_helpers/block_definitions.js';
+} from './test_helpers/block_definitions.ts';
import {assertEventFired, assertEventNotFired} from './test_helpers/events.js';
import {
DEFAULT_INJECT_OPTIONS,
diff --git a/packages/blockly/tests/mocha/procedure_map_test.ts b/packages/blockly/tests/mocha/procedure_map_test.ts
index a7310afb7bd..41d74589326 100644
--- a/packages/blockly/tests/mocha/procedure_map_test.ts
+++ b/packages/blockly/tests/mocha/procedure_map_test.ts
@@ -27,7 +27,7 @@ suite('Procedure Map', function () {
suite('publishing', function () {
test('inserting a procedure tells it to start publishing', function () {
- const procedureModel = new MockProcedureModel();
+ const procedureModel = new MockProcedureModel(workspace);
const spy = sinon.spy(procedureModel, 'startPublishing');
workspace.getProcedureMap().set(procedureModel.getId(), procedureModel);
@@ -35,7 +35,7 @@ suite('Procedure Map', function () {
});
test('adding a procedure tells it to start publishing', function () {
- const procedureModel = new MockProcedureModel();
+ const procedureModel = new MockProcedureModel(workspace);
const spy = sinon.spy(procedureModel, 'startPublishing');
workspace.getProcedureMap().add(procedureModel);
@@ -43,7 +43,7 @@ suite('Procedure Map', function () {
});
test('deleting a procedure tells it to stop publishing', function () {
- const procedureModel = new MockProcedureModel();
+ const procedureModel = new MockProcedureModel(workspace);
const spy = sinon.spy(procedureModel, 'stopPublishing');
workspace.getProcedureMap().add(procedureModel);
diff --git a/packages/blockly/tests/mocha/render_management_test.js b/packages/blockly/tests/mocha/render_management_test.js
deleted file mode 100644
index 28a2ef4f8c2..00000000000
--- a/packages/blockly/tests/mocha/render_management_test.js
+++ /dev/null
@@ -1,213 +0,0 @@
-/**
- * @license
- * Copyright 2023 Google LLC
- * SPDX-License-Identifier: Apache-2.0
- */
-
-import {assert} from 'chai';
-import {
- DEFAULT_INJECT_OPTIONS,
- sharedTestSetup,
- sharedTestTeardown,
- workspaceTeardown,
-} from './test_helpers/setup_teardown.js';
-
-suite('Render Management', function () {
- setup(function () {
- this.clock = sharedTestSetup.call(this).clock;
- });
-
- teardown(function () {
- sharedTestTeardown.call(this);
- });
-
- suite('finish queued renders callback', function () {
- function createMockBlock() {
- return {
- hasRendered: false,
- renderEfficiently: function () {
- this.hasRendered = true;
- },
-
- // All of the APIs the render management system needs.
- getParent: () => null,
- getChildren: () => [],
- isDisposed: () => false,
- getRelativeToSurfaceXY: () => ({x: 0, y: 0}),
- updateComponentLocations: () => {},
- bumpNeighbours: () => {},
- initialized: true,
- workspace: {
- resizeContents: () => {},
- connectionDBList: [],
- },
- };
- }
-
- test('the queueRender promise is properly resolved after rendering', function () {
- const block = createMockBlock();
- const promise = Blockly.renderManagement.queueRender(block).then(() => {
- assert.isTrue(block.hasRendered, 'Expected block to be rendered');
- });
- this.clock.runAll();
- return promise;
- });
-
- test('the finish queued renders promise is properly resolved after rendering', function () {
- const block = createMockBlock();
- Blockly.renderManagement.queueRender(block);
- const promise = Blockly.renderManagement.finishQueuedRenders(() => {
- assert.isTrue(block.hasRendered, 'Expected block to be rendered');
- });
- this.clock.runAll();
- return promise;
- });
- });
-
- suite('triggering queued renders', function () {
- function createMockBlock(ws) {
- return {
- hasRendered: false,
- renderEfficiently: function () {
- this.hasRendered = true;
- },
-
- // All of the APIs the render management system needs.
- getParent: () => null,
- getChildren: () => [],
- isDisposed: () => false,
- getRelativeToSurfaceXY: () => ({x: 0, y: 0}),
- updateComponentLocations: () => {},
- bumpNeighbours: () => {},
- initialized: true,
- workspace: ws || createMockWorkspace(),
- };
- }
-
- function createMockWorkspace() {
- return {
- resizeContents: () => {},
- connectionDBList: [],
- };
- }
-
- test('triggering queued renders rerenders blocks', function () {
- const block = createMockBlock();
- Blockly.renderManagement.queueRender(block);
-
- Blockly.renderManagement.triggerQueuedRenders();
-
- assert.isTrue(block.hasRendered, 'Expected block to be rendered');
- });
-
- test('triggering queued renders rerenders blocks in all workspaces', function () {
- const workspace1 = createMockWorkspace();
- const workspace2 = createMockWorkspace();
- const block1 = createMockBlock(workspace1);
- const block2 = createMockBlock(workspace2);
- Blockly.renderManagement.queueRender(block1);
- Blockly.renderManagement.queueRender(block2);
-
- Blockly.renderManagement.triggerQueuedRenders();
-
- assert.isTrue(block1.hasRendered, 'Expected block1 to be rendered');
- assert.isTrue(block2.hasRendered, 'Expected block2 to be rendered');
- });
-
- test('triggering queued renders in one workspace does not rerender blocks in another workspace', function () {
- const workspace1 = createMockWorkspace();
- const workspace2 = createMockWorkspace();
- const block1 = createMockBlock(workspace1);
- const block2 = createMockBlock(workspace2);
- Blockly.renderManagement.queueRender(block1);
- Blockly.renderManagement.queueRender(block2);
-
- Blockly.renderManagement.triggerQueuedRenders(workspace1);
-
- assert.isTrue(block1.hasRendered, 'Expected block1 to be rendered');
- assert.isFalse(block2.hasRendered, 'Expected block2 to not be rendered');
- });
- });
-
- suite('Post-render bumpNeighbours', function () {
- setup(function () {
- this.workspace = Blockly.inject('blocklyDiv', DEFAULT_INJECT_OPTIONS);
-
- this.block = this.workspace.newBlock('controls_if');
- this.block.initSvg();
-
- const block2 = this.workspace.newBlock('controls_if');
- block2.initSvg();
- Blockly.renderManagement.triggerQueuedRenders();
-
- // Align the blocks' next and previous connections without connecting them
- // so that they'll need to bump one another.
- this.block.moveBy(
- block2.nextConnection.x - this.block.previousConnection.x,
- block2.nextConnection.y - this.block.previousConnection.y,
- ['test'],
- );
- });
-
- teardown(function () {
- workspaceTeardown.call(this, this.workspace);
- });
-
- test('does not record undo event when the render was queued with recordUndo disabled', function () {
- Blockly.Events.setRecordUndo(false);
- Blockly.renderManagement.queueRender(this.block);
- Blockly.Events.setRecordUndo(true);
-
- Blockly.renderManagement.triggerQueuedRenders();
-
- assert.isFalse(
- this.workspace.getUndoStack().some((item) => {
- return (
- item.type === 'move' &&
- item.reason[0] === 'bump' &&
- item.blockId === this.block.id
- );
- }),
- );
- });
-
- test('records undo event when the render was queued with recordUndo enabled', function () {
- Blockly.Events.setRecordUndo(true);
- Blockly.renderManagement.queueRender(this.block);
- Blockly.Events.setRecordUndo(false);
-
- Blockly.renderManagement.triggerQueuedRenders();
-
- assert.isTrue(
- this.workspace.getUndoStack().some((item) => {
- return (
- item.type === 'move' &&
- item.reason[0] === 'bump' &&
- item.blockId === this.block.id
- );
- }),
- );
- });
-
- test('associates bump undo events with the event group from when the render was queued', function () {
- Blockly.Events.setRecordUndo(true);
- Blockly.Events.setGroup('test-group');
- Blockly.renderManagement.queueRender(this.block);
- Blockly.Events.setGroup(false);
- Blockly.Events.setRecordUndo(false);
-
- Blockly.renderManagement.triggerQueuedRenders();
-
- assert.isTrue(
- this.workspace.getUndoStack().some((item) => {
- return (
- item.type === 'move' &&
- item.reason[0] === 'bump' &&
- item.blockId === this.block.id &&
- item.group === 'test-group'
- );
- }),
- );
- });
- });
-});
diff --git a/packages/blockly/tests/mocha/render_management_test.ts b/packages/blockly/tests/mocha/render_management_test.ts
new file mode 100644
index 00000000000..c2fd127d70f
--- /dev/null
+++ b/packages/blockly/tests/mocha/render_management_test.ts
@@ -0,0 +1,197 @@
+/**
+ * @license
+ * Copyright 2023 Google LLC
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+import * as Blockly from '#core/blockly.js';
+import {assert} from 'chai';
+import sinon from 'sinon';
+import {
+ DEFAULT_INJECT_OPTIONS,
+ sharedTestSetup,
+ sharedTestTeardown,
+ workspaceTeardown,
+} from './test_helpers/setup_teardown.js';
+
+suite('Render Management', function () {
+ let workspace: Blockly.WorkspaceSvg;
+ let clock: sinon.SinonFakeTimers;
+
+ setup(function (this: Mocha.Context) {
+ ({clock} = sharedTestSetup.call(this));
+ workspace = Blockly.inject('blocklyDiv', DEFAULT_INJECT_OPTIONS);
+ });
+
+ teardown(function (this: Mocha.Context) {
+ sharedTestTeardown.call(this, workspace);
+ });
+
+ suite('finish queued renders callback', function () {
+ test('the queueRender promise is properly resolved after rendering', function () {
+ const block = workspace.newBlock('controls_if');
+ block.initSvg();
+ const renderSpy = sinon.spy(block, 'renderEfficiently');
+ const promise = Blockly.renderManagement.queueRender(block).then(() => {
+ sinon.assert.calledOnce(renderSpy);
+ });
+ clock.runAll();
+ return promise;
+ });
+
+ test('the finish queued renders promise is properly resolved after rendering', function () {
+ const block = workspace.newBlock('controls_if');
+ block.initSvg();
+ const renderSpy = sinon.spy(block, 'renderEfficiently');
+ Blockly.renderManagement.queueRender(block);
+ const promise = Blockly.renderManagement
+ .finishQueuedRenders()
+ .then(() => {
+ sinon.assert.calledOnce(renderSpy);
+ });
+ clock.runAll();
+ return promise;
+ });
+ });
+
+ suite('triggering queued renders', function () {
+ test('triggering queued renders rerenders blocks', function () {
+ const block = workspace.newBlock('controls_if');
+ block.initSvg();
+ const renderSpy = sinon.spy(block, 'renderEfficiently');
+ Blockly.renderManagement.queueRender(block);
+
+ Blockly.renderManagement.triggerQueuedRenders();
+
+ sinon.assert.calledOnce(renderSpy);
+ });
+
+ test('triggering queued renders rerenders blocks in all workspaces', function () {
+ const div = document.createElement('div');
+ document.body.appendChild(div);
+ const workspace2 = Blockly.inject(div, DEFAULT_INJECT_OPTIONS);
+ const block1 = workspace.newBlock('controls_if');
+ block1.initSvg();
+ const renderSpy1 = sinon.spy(block1, 'renderEfficiently');
+ const block2 = workspace2.newBlock('controls_if');
+ block2.initSvg();
+ const renderSpy2 = sinon.spy(block2, 'renderEfficiently');
+
+ Blockly.renderManagement.queueRender(block1);
+ Blockly.renderManagement.queueRender(block2);
+
+ Blockly.renderManagement.triggerQueuedRenders();
+
+ sinon.assert.calledOnce(renderSpy1);
+ sinon.assert.calledOnce(renderSpy2);
+
+ workspace2.dispose();
+ });
+
+ test('triggering queued renders in one workspace does not rerender blocks in another workspace', function () {
+ const div = document.createElement('div');
+ document.body.appendChild(div);
+ const workspace2 = Blockly.inject(div, DEFAULT_INJECT_OPTIONS);
+ const block1 = workspace.newBlock('controls_if');
+ block1.initSvg();
+ const renderSpy1 = sinon.spy(block1, 'renderEfficiently');
+ const block2 = workspace2.newBlock('controls_if');
+ block2.initSvg();
+ const renderSpy2 = sinon.spy(block2, 'renderEfficiently');
+
+ Blockly.renderManagement.queueRender(block1);
+ Blockly.renderManagement.queueRender(block2);
+
+ Blockly.renderManagement.triggerQueuedRenders(workspace);
+
+ sinon.assert.calledOnce(renderSpy1);
+ sinon.assert.notCalled(renderSpy2);
+
+ workspace2.dispose();
+ });
+ });
+
+ suite('Post-render bumpNeighbours', function () {
+ let block: Blockly.BlockSvg;
+
+ setup(function () {
+ block = workspace.newBlock('controls_if');
+ block.initSvg();
+
+ const block2 = workspace.newBlock('controls_if');
+ block2.initSvg();
+ Blockly.renderManagement.triggerQueuedRenders();
+
+ // Align the blocks' next and previous connections without connecting them
+ // so that they'll need to bump one another.
+ assert.isNotNull(block2.nextConnection);
+ assert.isNotNull(block.previousConnection);
+ block.moveBy(
+ block2.nextConnection.x - block.previousConnection.x,
+ block2.nextConnection.y - block.previousConnection.y,
+ ['test'],
+ );
+ });
+
+ teardown(function (this: Mocha.Context) {
+ workspaceTeardown.call(this, workspace);
+ });
+
+ test('does not record undo event when the render was queued with recordUndo disabled', function () {
+ Blockly.Events.setRecordUndo(false);
+ Blockly.renderManagement.queueRender(block);
+ Blockly.Events.setRecordUndo(true);
+
+ Blockly.renderManagement.triggerQueuedRenders();
+
+ assert.isFalse(
+ workspace.getUndoStack().some((item) => {
+ return (
+ item instanceof Blockly.Events.BlockMove &&
+ item.reason?.[0] === 'bump' &&
+ item.blockId === block.id
+ );
+ }),
+ );
+ });
+
+ test('records undo event when the render was queued with recordUndo enabled', function () {
+ Blockly.Events.setRecordUndo(true);
+ Blockly.renderManagement.queueRender(block);
+ Blockly.Events.setRecordUndo(false);
+
+ Blockly.renderManagement.triggerQueuedRenders();
+
+ assert.isTrue(
+ workspace.getUndoStack().some((item) => {
+ return (
+ item instanceof Blockly.Events.BlockMove &&
+ item.reason?.[0] === 'bump' &&
+ item.blockId === block.id
+ );
+ }),
+ );
+ });
+
+ test('associates bump undo events with the event group from when the render was queued', function () {
+ Blockly.Events.setRecordUndo(true);
+ Blockly.Events.setGroup('test-group');
+ Blockly.renderManagement.queueRender(block);
+ Blockly.Events.setGroup(false);
+ Blockly.Events.setRecordUndo(false);
+
+ Blockly.renderManagement.triggerQueuedRenders();
+
+ assert.isTrue(
+ workspace.getUndoStack().some((item) => {
+ return (
+ item instanceof Blockly.Events.BlockMove &&
+ item.reason?.[0] === 'bump' &&
+ item.blockId === block.id &&
+ item.group === 'test-group'
+ );
+ }),
+ );
+ });
+ });
+});
diff --git a/packages/blockly/tests/mocha/shortcut_items_test.js b/packages/blockly/tests/mocha/shortcut_items_test.js
index 1b5d5d9ed8d..e0d9e47b1b9 100644
--- a/packages/blockly/tests/mocha/shortcut_items_test.js
+++ b/packages/blockly/tests/mocha/shortcut_items_test.js
@@ -9,13 +9,13 @@ import {assert} from 'chai';
import {
defineRowBlock,
defineStackBlock,
-} from './test_helpers/block_definitions.js';
+} from './test_helpers/block_definitions.ts';
import {
DEFAULT_INJECT_OPTIONS,
sharedTestSetup,
sharedTestTeardown,
} from './test_helpers/setup_teardown.js';
-import {createKeyDownEvent} from './test_helpers/user_input.js';
+import {createKeyDownEvent} from './test_helpers/user_input.ts';
suite('Keyboard Shortcut Items', function () {
setup(function () {
diff --git a/packages/blockly/tests/mocha/shortcut_registry_test.ts b/packages/blockly/tests/mocha/shortcut_registry_test.ts
index 615879d39e5..956e5ebddb8 100644
--- a/packages/blockly/tests/mocha/shortcut_registry_test.ts
+++ b/packages/blockly/tests/mocha/shortcut_registry_test.ts
@@ -13,7 +13,7 @@ import {
sharedTestTeardown,
workspaceTeardown,
} from './test_helpers/setup_teardown.js';
-import {createKeyDownEvent} from './test_helpers/user_input.js';
+import {createKeyDownEvent} from './test_helpers/user_input.ts';
suite('Keyboard Shortcut Registry Test', function () {
let registry: Blockly.ShortcutRegistry;
diff --git a/packages/blockly/tests/mocha/test_helpers/block_definitions.js b/packages/blockly/tests/mocha/test_helpers/block_definitions.ts
similarity index 70%
rename from packages/blockly/tests/mocha/test_helpers/block_definitions.js
rename to packages/blockly/tests/mocha/test_helpers/block_definitions.ts
index 26507b29cb8..cd00ba55800 100644
--- a/packages/blockly/tests/mocha/test_helpers/block_definitions.js
+++ b/packages/blockly/tests/mocha/test_helpers/block_definitions.ts
@@ -3,7 +3,11 @@
* Copyright 2019 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
+import * as Blockly from '#core/blockly.js';
+/**
+ * Registers a block with no content.
+ */
export function defineEmptyBlock(name = 'empty_block') {
Blockly.defineBlocksWithJsonArray([
{
@@ -13,6 +17,9 @@ export function defineEmptyBlock(name = 'empty_block') {
]);
}
+/**
+ * Registers an empty block with previous and next connections.
+ */
export function defineStackBlock(name = 'stack_block') {
Blockly.defineBlocksWithJsonArray([
{
@@ -24,6 +31,9 @@ export function defineStackBlock(name = 'stack_block') {
]);
}
+/**
+ * Registers a block with one input and an output connection.
+ */
export function defineRowBlock(name = 'row_block') {
Blockly.defineBlocksWithJsonArray([
{
@@ -40,6 +50,9 @@ export function defineRowBlock(name = 'row_block') {
]);
}
+/**
+ * Registers a block with an output connection and a next connection.
+ */
export function defineRowToStackBlock(name = 'row_to_stack_block') {
Blockly.defineBlocksWithJsonArray([
{
@@ -51,6 +64,9 @@ export function defineRowToStackBlock(name = 'row_to_stack_block') {
]);
}
+/**
+ * Registers a block with a next, previous and statement input connections.
+ */
export function defineStatementBlock(name = 'statement_block') {
Blockly.defineBlocksWithJsonArray([
{
@@ -71,6 +87,9 @@ export function defineStatementBlock(name = 'statement_block') {
]);
}
+/**
+ * Registers a value block with a field and output connection.
+ */
export function defineBasicBlockWithField(name = 'test_field_block') {
Blockly.defineBlocksWithJsonArray([
{
@@ -87,6 +106,14 @@ export function defineBasicBlockWithField(name = 'test_field_block') {
]);
}
+type MutatorBlock = Blockly.Block & {
+ hasInput: boolean;
+ updateShape: () => void;
+};
+
+/**
+ * Registers blocks that support mutations.
+ */
export function defineMutatorBlocks() {
Blockly.defineBlocksWithJsonArray([
{
@@ -112,30 +139,30 @@ export function defineMutatorBlocks() {
const xmlMutator = {
hasInput: false,
- mutationToDom: function () {
+ mutationToDom: function (this: MutatorBlock) {
const mutation = Blockly.utils.xml.createElement('mutation');
- mutation.setAttribute('hasInput', this.hasInput);
+ mutation.setAttribute('hasInput', `${this.hasInput}`);
return mutation;
},
- domToMutation: function (mutation) {
+ domToMutation: function (this: MutatorBlock, mutation: Element) {
this.hasInput = mutation.getAttribute('hasInput') == 'true';
this.updateShape();
},
- decompose: function (workspace) {
+ decompose: function (workspace: Blockly.WorkspaceSvg) {
const topBlock = workspace.newBlock('checkbox_block', 'check_block');
topBlock.initSvg();
topBlock.render();
return topBlock;
},
- compose: function (topBlock) {
+ compose: function (this: MutatorBlock, topBlock: Blockly.Block) {
this.hasInput = topBlock.getFieldValue('CHECK') == 'TRUE';
this.updateShape();
},
- updateShape: function () {
+ updateShape: function (this: MutatorBlock) {
if (this.hasInput && !this.getInput('INPUT')) {
this.appendValueInput('INPUT');
} else if (!this.hasInput && this.getInput('INPUT')) {
@@ -152,24 +179,24 @@ export function defineMutatorBlocks() {
return {hasInput: this.hasInput};
},
- loadExtraState: function (state) {
+ loadExtraState: function (this: MutatorBlock, state: {hasInput?: boolean}) {
this.hasInput = state.hasInput || false;
this.updateShape();
},
- decompose: function (workspace) {
+ decompose: function (workspace: Blockly.WorkspaceSvg) {
const topBlock = workspace.newBlock('checkbox_block', 'check_block');
topBlock.initSvg();
topBlock.render();
return topBlock;
},
- compose: function (topBlock) {
+ compose: function (this: MutatorBlock, topBlock: Blockly.Block) {
this.hasInput = topBlock.getFieldValue('CHECK') == 'TRUE';
this.updateShape();
},
- updateShape: function () {
+ updateShape: function (this: MutatorBlock) {
if (this.hasInput && !this.getInput('INPUT')) {
this.appendValueInput('INPUT');
} else if (!this.hasInput && this.getInput('INPUT')) {
@@ -180,6 +207,9 @@ export function defineMutatorBlocks() {
Blockly.Extensions.registerMutator('jso_mutator', jsoMutator);
}
+/**
+ * Creates a mock of a block. New code should use real blocks instead.
+ */
export function createTestBlock() {
return {
'id': 'test',
@@ -196,8 +226,18 @@ export function createTestBlock() {
};
}
-export function createRenderedBlock(workspaceSvg, type) {
- const block = workspaceSvg.newBlock(type);
+/**
+ * Initializes and renders a block of the given type on the given workspace.
+ *
+ * @param workspaceSvg The workspace to create the block on.
+ * @param type The type of block to create.
+ * @returns The newly created block.
+ */
+export function createRenderedBlock(
+ workspaceSvg: Blockly.WorkspaceSvg,
+ type: string,
+) {
+ const block = workspaceSvg.newBlock(type) as Blockly.BlockSvg;
block.initSvg();
block.render();
return block;
diff --git a/packages/blockly/tests/mocha/test_helpers/focusable_tree.js b/packages/blockly/tests/mocha/test_helpers/focusable_tree.ts
similarity index 62%
rename from packages/blockly/tests/mocha/test_helpers/focusable_tree.js
rename to packages/blockly/tests/mocha/test_helpers/focusable_tree.ts
index 9b76c74bdd9..77cc79a98b2 100644
--- a/packages/blockly/tests/mocha/test_helpers/focusable_tree.js
+++ b/packages/blockly/tests/mocha/test_helpers/focusable_tree.ts
@@ -3,6 +3,7 @@
* Copyright 2026 Raspberry Pi Foundation
* SPDX-License-Identifier: Apache-2.0
*/
+import * as Blockly from '#core/blockly.js';
/**
* @fileoverview A minimal IFocusableTree implementation shared by the focus
@@ -12,12 +13,18 @@
/**
* Builds the test IFocusableTree implementation.
*
- * @returns {!Function} The FocusableTreeImpl class.
+ * @returns The FocusableTreeImpl class.
*/
export function focusableTreeImplFactory() {
/** A focusable node wrapping a single element. */
- class FocusableNodeImpl {
- constructor(element, tree) {
+ class FocusableNodeImpl implements Blockly.IFocusableNode {
+ element: HTMLElement | SVGElement;
+ tree: Blockly.IFocusableTree;
+
+ constructor(
+ element: HTMLElement | SVGElement,
+ tree: Blockly.IFocusableTree,
+ ) {
this.element = element;
this.tree = tree;
}
@@ -45,21 +52,29 @@ export function focusableTreeImplFactory() {
* Tests can set `fallbackNode` to control getRestoredFocusableNode(), and
* override `canBeFocused` on an individual node to make it unfocusable.
*/
- class FocusableTreeImpl {
- constructor(rootElement, nestedTrees = []) {
+ class FocusableTreeImpl implements Blockly.IFocusableTree {
+ nestedTrees: Blockly.IFocusableTree[];
+ idToNodeMap: {[key: string]: Blockly.IFocusableNode};
+ rootNode: Blockly.IFocusableNode;
+ fallbackNode: Blockly.IFocusableNode | null;
+
+ constructor(
+ rootElement: HTMLElement | SVGElement,
+ nestedTrees: Blockly.IFocusableTree[] = [],
+ ) {
this.nestedTrees = nestedTrees;
this.idToNodeMap = {};
this.rootNode = this.addNode(rootElement);
this.fallbackNode = null;
}
- addNode(element) {
+ addNode(element: HTMLElement | SVGElement) {
const node = new FocusableNodeImpl(element, this);
this.idToNodeMap[element.id] = node;
return node;
}
- removeNode(node) {
+ removeNode(node: Blockly.IFocusableNode) {
delete this.idToNodeMap[node.getFocusableElement().id];
}
@@ -75,16 +90,21 @@ export function focusableTreeImplFactory() {
return this.nestedTrees;
}
- lookUpFocusableNode(id) {
+ lookUpFocusableNode(id: string) {
return this.idToNodeMap[id];
}
onTreeFocus() {}
onTreeBlur() {}
+
+ getNavigator() {
+ return new Blockly.Navigator();
+ }
}
return FocusableTreeImpl;
}
export const FocusableTreeImpl = focusableTreeImplFactory();
+export type FocusableTreeImpl = InstanceType;
diff --git a/packages/blockly/tests/mocha/test_helpers/procedures.ts b/packages/blockly/tests/mocha/test_helpers/procedures.ts
index 0a0b2308e47..96a6df0cad1 100644
--- a/packages/blockly/tests/mocha/test_helpers/procedures.ts
+++ b/packages/blockly/tests/mocha/test_helpers/procedures.ts
@@ -194,17 +194,18 @@ export function createProcCallBlock(
}
export class MockProcedureModel implements Blockly.procedures.IProcedureModel {
- private readonly id = Blockly.utils.idGenerator.genUid();
+ private id = Blockly.utils.idGenerator.genUid();
private name: string;
private readonly parameters: Blockly.procedures.IParameterModel[] = [];
private returnTypes: string[] | null = null;
private enabled = true;
- constructor(name = '') {
+ constructor(workspace: Blockly.Workspace, name = '', id?: string) {
this.name = name;
+ this.id = id ?? this.id;
}
- static loadState(_state: object, _workspace: Blockly.Workspace) {
- return new MockProcedureModel();
+ static loadState(_state: object, workspace: Blockly.Workspace) {
+ return new MockProcedureModel(workspace);
}
saveState() {
@@ -269,15 +270,16 @@ export class MockProcedureModel implements Blockly.procedures.IProcedureModel {
}
export class MockParameterModel implements Blockly.procedures.IParameterModel {
- private readonly id = Blockly.utils.idGenerator.genUid();
+ private id = Blockly.utils.idGenerator.genUid();
private types: string[] = [];
private name: string;
- constructor(name: string) {
+ constructor(workspace: Blockly.Workspace, name = '', id?: string) {
this.name = name;
+ this.id = id ?? this.id;
}
- static loadState(_state: object, _workspace: Blockly.Workspace) {
- return new MockParameterModel('test');
+ static loadState(_state: object, workspace: Blockly.Workspace) {
+ return new MockParameterModel(workspace, 'test');
}
setProcedureModel(_model: Blockly.procedures.IProcedureModel) {
@@ -315,7 +317,7 @@ export class MockParameterModelWithVar extends MockParameterModel {
private variable: Blockly.VariableModel;
constructor(name: string, workspace: Blockly.Workspace) {
- super(name);
+ super(workspace, name);
this.variable = new Blockly.VariableModel(workspace, name);
}
diff --git a/packages/blockly/tests/mocha/test_helpers/setup_teardown.js b/packages/blockly/tests/mocha/test_helpers/setup_teardown.js
index 1801b8278f6..33103ec0100 100644
--- a/packages/blockly/tests/mocha/test_helpers/setup_teardown.js
+++ b/packages/blockly/tests/mocha/test_helpers/setup_teardown.js
@@ -115,6 +115,7 @@ function wrapDefineBlocksWithJsonArrayWithCleanup_(sharedCleanupObj) {
* @typedef {{
* clock: import('sinon').SinonFakeTimers,
* eventsFireStub: (import('sinon').SinonStub),
+ * globalDocumentEventListeners: {type: string, listener: () => void}[],
* }} SharedTestState
*/
@@ -169,6 +170,7 @@ export function sharedTestSetup(options = {}) {
return {
clock: this.clock,
eventsFireStub: this.eventsFireStub,
+ globalDocumentEventListeners: this.globalDocumentEventListeners,
};
}
diff --git a/packages/blockly/tests/mocha/test_helpers/toolbox_definitions.js b/packages/blockly/tests/mocha/test_helpers/toolbox_definitions.ts
similarity index 81%
rename from packages/blockly/tests/mocha/test_helpers/toolbox_definitions.js
rename to packages/blockly/tests/mocha/test_helpers/toolbox_definitions.ts
index 409ffd5d585..3ac8684f0f8 100644
--- a/packages/blockly/tests/mocha/test_helpers/toolbox_definitions.js
+++ b/packages/blockly/tests/mocha/test_helpers/toolbox_definitions.ts
@@ -3,13 +3,13 @@
* Copyright 2020 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
-
+import * as Blockly from '#core/blockly.js';
import {DEFAULT_INJECT_OPTIONS} from './setup_teardown.js';
/**
* Get JSON for a toolbox that contains categories.
- * @return {Blockly.utils.toolbox.ToolboxJson} The array holding information
- * for a toolbox.
+ *
+ * @return The array holding information for a toolbox.
*/
export function getCategoryJSON() {
return {
@@ -49,9 +49,9 @@ export function getCategoryJSON() {
}
/**
- * Get JSON for a simple toolbox.
- * @return {Blockly.utils.toolbox.ToolboxJson} The array holding information
- * for a simple toolbox.
+ * Get JSON for a simple toolbox that uses a `blockxml` field in the JSON.
+ *
+ * @return The array holding information for a simple toolbox.
*/
export function getSimpleJson() {
return {
@@ -89,6 +89,11 @@ export function getSimpleJson() {
};
}
+/**
+ * Get JSON for a simple toolbox using a pure-JSON definition.
+ *
+ * @return A toolbox definition.
+ */
export function getProperSimpleJson() {
return {
'contents': [
@@ -136,8 +141,8 @@ export function getProperSimpleJson() {
/**
* Get JSON for a toolbox that contains categories that contain categories.
- * @return {Blockly.utils.toolbox.ToolboxJson} The array holding information
- * for a toolbox.
+ *
+ * @return The array holding information for a toolbox.
*/
export function getDeeplyNestedJSON() {
return {
@@ -190,7 +195,8 @@ export function getDeeplyNestedJSON() {
/**
* Get an array filled with xml elements.
- * @return {Array} Array holding xml elements for a toolbox.
+ *
+ * @return Array holding xml elements for a toolbox.
*/
export function getXmlArray() {
const block = Blockly.utils.xml.textToDom(
@@ -216,6 +222,9 @@ export function getXmlArray() {
return [block, separator, button, label];
}
+/**
+ * Injects a Blockly workspace and returns its toolbox.
+ */
export function getInjectedToolbox() {
/**
* Category: First
@@ -229,7 +238,7 @@ export function getInjectedToolbox() {
* Category: NestedCategory
* Category: NestedItemOne
*/
- const toolboxXml = document.getElementById('toolbox-test');
+ const toolboxXml = document.getElementById('toolbox-test') ?? undefined;
const workspace = Blockly.inject('blocklyDiv', {
...DEFAULT_INJECT_OPTIONS,
toolbox: toolboxXml,
@@ -237,16 +246,21 @@ export function getInjectedToolbox() {
return workspace.getToolbox();
}
+/**
+ * Returns a jankily-constructed toolbox. New tests should not use this.
+ */
export function getBasicToolbox() {
const workspace = new Blockly.WorkspaceSvg(new Blockly.Options({}));
const toolbox = new Blockly.Toolbox(workspace);
toolbox.HtmlDiv = document.createElement('div');
- toolbox.flyout = sinon.createStubInstance(Blockly.VerticalFlyout);
return toolbox;
}
-export function getCollapsibleItem(toolbox) {
- const contents = toolbox.contents.values();
+/**
+ * Returns the first collapsible item of the given toolbox.
+ */
+export function getCollapsibleItem(toolbox: Blockly.Toolbox) {
+ const contents = toolbox.getToolboxItems();
for (const item of contents) {
if (item.isCollapsible()) {
return item;
@@ -254,8 +268,11 @@ export function getCollapsibleItem(toolbox) {
}
}
-export function getNonCollapsibleItem(toolbox) {
- const contents = toolbox.contents.values();
+/**
+ * Returns the first non-collapsible item of the given toolbox.
+ */
+export function getNonCollapsibleItem(toolbox: Blockly.Toolbox) {
+ const contents = toolbox.getToolboxItems();
for (const item of contents) {
if (!item.isCollapsible()) {
return item;
@@ -263,10 +280,16 @@ export function getNonCollapsibleItem(toolbox) {
}
}
-export function getChildItem(toolbox) {
+/**
+ * Returns the toolbox item with ID `nestedCategory` of the given toolbox.
+ */
+export function getChildItem(toolbox: Blockly.Toolbox) {
return toolbox.getToolboxItemById('nestedCategory');
}
-export function getSeparator(toolbox) {
+/**
+ * Returns the toolbox item with ID `separator` of the given toolbox.
+ */
+export function getSeparator(toolbox: Blockly.Toolbox) {
return toolbox.getToolboxItemById('separator');
}
diff --git a/packages/blockly/tests/mocha/test_helpers/user_input.js b/packages/blockly/tests/mocha/test_helpers/user_input.js
deleted file mode 100644
index b342631c9c2..00000000000
--- a/packages/blockly/tests/mocha/test_helpers/user_input.js
+++ /dev/null
@@ -1,63 +0,0 @@
-/**
- * @license
- * Copyright 2019 Google LLC
- * SPDX-License-Identifier: Apache-2.0
- */
-
-import {KeyCodes} from '#core/utils/keycodes.js';
-
-/**
- * Triggers pointer event on target.
- * @param {!EventTarget} target The object receiving the event.
- * @param {string} type The type of mouse event (eg: mousedown, mouseup,
- * click).
- * @param {Object=} properties Properties to pass into event
- * constructor.
- */
-export function dispatchPointerEvent(target, type, properties) {
- const eventInitDict = {
- cancelable: true,
- bubbles: true,
- isPrimary: true,
- pressure: 0.5,
- clientX: 10,
- clientY: 10,
- };
- if (properties) {
- Object.assign(eventInitDict, properties);
- }
- const event = new PointerEvent(type, eventInitDict);
- target.dispatchEvent(event);
-}
-
-/**
- * Creates a key down event used for testing.
- * @param {number} keyCode The keycode for the event. Use Blockly.utils.KeyCodes enum.
- * @param {!Array=} modifiers A list of modifiers. Use Blockly.utils.KeyCodes enum.
- * @return {!KeyboardEvent} The mocked keydown event.
- */
-export function createKeyDownEvent(keyCode, modifiers) {
- const event = {
- keyCode: keyCode,
- bubbles: true,
- };
- if (modifiers && modifiers.length > 0) {
- event.altKey = modifiers.includes(KeyCodes.ALT);
- event.ctrlKey = modifiers.includes(KeyCodes.CTRL);
- event.metaKey = modifiers.includes(KeyCodes.META);
- event.shiftKey = modifiers.includes(KeyCodes.SHIFT);
- }
- return new KeyboardEvent('keydown', event);
-}
-
-/**
- * Simulates mouse click by triggering relevant mouse events.
- * @param {!EventTarget} target The object receiving the event.
- * @param {Object=} properties Properties to pass into event
- * constructor.
- */
-export function simulateClick(target, properties) {
- dispatchPointerEvent(target, 'pointerdown', properties);
- dispatchPointerEvent(target, 'pointerup', properties);
- dispatchPointerEvent(target, 'click', properties);
-}
diff --git a/packages/blockly/tests/mocha/test_helpers/user_input.ts b/packages/blockly/tests/mocha/test_helpers/user_input.ts
new file mode 100644
index 00000000000..8b2760fb7f8
--- /dev/null
+++ b/packages/blockly/tests/mocha/test_helpers/user_input.ts
@@ -0,0 +1,69 @@
+/**
+ * @license
+ * Copyright 2019 Google LLC
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+import * as Blockly from '#core/blockly.js';
+
+/**
+ * Triggers pointer event on target.
+ *
+ * @param target The object receiving the event.
+ * @param type The type of mouse event (eg: mousedown, mouseup, click).
+ * @param properties Properties to pass into event constructor.
+ */
+export function dispatchPointerEvent(
+ target: Element,
+ type: string,
+ properties?: {[key: string]: string},
+) {
+ const eventInitDict = {
+ cancelable: true,
+ bubbles: true,
+ isPrimary: true,
+ pressure: 0.5,
+ clientX: 10,
+ clientY: 10,
+ };
+ if (properties) {
+ Object.assign(eventInitDict, properties);
+ }
+ const event = new PointerEvent(type, eventInitDict);
+ target.dispatchEvent(event);
+}
+
+/**
+ * Creates a key down event used for testing.
+ *
+ * @param keyCode The keycode for the event. Use Blockly.utils.KeyCodes enum.
+ * @param modifiers A list of modifiers. Use Blockly.utils.KeyCodes enum.
+ * @return The mocked keydown event.
+ */
+export function createKeyDownEvent(keyCode: number, modifiers?: number[]) {
+ modifiers = modifiers ?? [];
+ const event = {
+ keyCode: keyCode,
+ bubbles: true,
+ altKey: modifiers.includes(Blockly.utils.KeyCodes.ALT),
+ ctrlKey: modifiers.includes(Blockly.utils.KeyCodes.CTRL),
+ metaKey: modifiers.includes(Blockly.utils.KeyCodes.META),
+ shiftKey: modifiers.includes(Blockly.utils.KeyCodes.SHIFT),
+ };
+ return new KeyboardEvent('keydown', event);
+}
+
+/**
+ * Simulates mouse click by triggering relevant mouse events.
+ *
+ * @param target The object receiving the event.
+ * @param properties Properties to pass into event constructor.
+ */
+export function simulateClick(
+ target: Element,
+ properties?: {[key: string]: string},
+) {
+ dispatchPointerEvent(target, 'pointerdown', properties);
+ dispatchPointerEvent(target, 'pointerup', properties);
+ dispatchPointerEvent(target, 'click', properties);
+}
diff --git a/packages/blockly/tests/mocha/toolbox_test.js b/packages/blockly/tests/mocha/toolbox_test.js
index d22b1e2a9f3..fd0a64482df 100644
--- a/packages/blockly/tests/mocha/toolbox_test.js
+++ b/packages/blockly/tests/mocha/toolbox_test.js
@@ -8,7 +8,7 @@ import {assert} from 'chai';
import {
defineBasicBlockWithField,
defineStackBlock,
-} from './test_helpers/block_definitions.js';
+} from './test_helpers/block_definitions.ts';
import {
sharedTestSetup,
sharedTestTeardown,
@@ -24,7 +24,7 @@ import {
getSeparator,
getSimpleJson,
getXmlArray,
-} from './test_helpers/toolbox_definitions.js';
+} from './test_helpers/toolbox_definitions.ts';
suite('Toolbox', function () {
setup(function () {
diff --git a/packages/blockly/tests/mocha/trashcan_test.ts b/packages/blockly/tests/mocha/trashcan_test.ts
index 34eb7fa92b6..ac5150cdd23 100644
--- a/packages/blockly/tests/mocha/trashcan_test.ts
+++ b/packages/blockly/tests/mocha/trashcan_test.ts
@@ -13,14 +13,14 @@ import {
defineRowBlock,
defineStackBlock,
defineStatementBlock,
-} from './test_helpers/block_definitions.js';
+} from './test_helpers/block_definitions.ts';
import {assertEventFired, assertEventNotFired} from './test_helpers/events.js';
import {
DEFAULT_INJECT_OPTIONS,
sharedTestSetup,
sharedTestTeardown,
} from './test_helpers/setup_teardown.js';
-import {simulateClick} from './test_helpers/user_input.js';
+import {simulateClick} from './test_helpers/user_input.ts';
suite('Trashcan', function () {
let workspace: Blockly.WorkspaceSvg;
diff --git a/packages/blockly/tests/mocha/workspace_svg_test.ts b/packages/blockly/tests/mocha/workspace_svg_test.ts
index 790eeaa4fb5..4e30b1a3261 100644
--- a/packages/blockly/tests/mocha/workspace_svg_test.ts
+++ b/packages/blockly/tests/mocha/workspace_svg_test.ts
@@ -7,7 +7,7 @@
import * as Blockly from '#core/blockly.js';
import {assert} from 'chai';
import sinon from 'sinon';
-import {defineStackBlock} from './test_helpers/block_definitions.js';
+import {defineStackBlock} from './test_helpers/block_definitions.ts';
import {
assertEventFired,
assertEventNotFired,
@@ -19,7 +19,7 @@ import {
sharedTestTeardown,
workspaceTeardown,
} from './test_helpers/setup_teardown.js';
-import {dispatchPointerEvent} from './test_helpers/user_input.js';
+import {dispatchPointerEvent} from './test_helpers/user_input.ts';
import {testAWorkspace} from './test_helpers/workspace.ts';
suite('WorkspaceSvg', function () {
diff --git a/packages/blockly/tests/mocha/zoom_controls_test.ts b/packages/blockly/tests/mocha/zoom_controls_test.ts
index 85bf81650d5..647dce72945 100644
--- a/packages/blockly/tests/mocha/zoom_controls_test.ts
+++ b/packages/blockly/tests/mocha/zoom_controls_test.ts
@@ -13,7 +13,7 @@ import {
sharedTestSetup,
sharedTestTeardown,
} from './test_helpers/setup_teardown.js';
-import {simulateClick} from './test_helpers/user_input.js';
+import {simulateClick} from './test_helpers/user_input.ts';
suite('Zoom Controls', function () {
let workspace: Blockly.WorkspaceSvg;