From c117135962461238739f73f1726c68b10e5e2de5 Mon Sep 17 00:00:00 2001 From: Matt Hillsdon Date: Wed, 25 Nov 2026 19:21:52 +0000 Subject: [PATCH 1/2] fix: don't change icon bubbles after the block is disposed MutatorIcon and WarningIcon.setBubbleVisible wait for queued renders before opening or closing the bubble. Callers such as undoing a mutation change don't await it, so the block can be disposed in the meantime. The icon then built a bubble for the disposed block, or disposed the already-disposed mini workspace and fired a bubble_open event for it, which threw errors. Return after waiting if the block has been disposed; dispose() has already cleaned up the bubble. CommentIcon already does this via its rendered check. In the Node test run, the stray events were queued after teardown with their flush scheduled on a restored clock, so later procedure tests saw no events and failed intermittently. Part of #10724 --- packages/blockly/core/icons/mutator_icon.ts | 1 + packages/blockly/core/icons/warning_icon.ts | 1 + packages/blockly/tests/mocha/block_test.js | 12 ++++++ packages/blockly/tests/mocha/mutator_test.js | 42 ++++++++++++++++++++ 4 files changed, 56 insertions(+) diff --git a/packages/blockly/core/icons/mutator_icon.ts b/packages/blockly/core/icons/mutator_icon.ts index b6c2e6a975e..f00b408b3a2 100644 --- a/packages/blockly/core/icons/mutator_icon.ts +++ b/packages/blockly/core/icons/mutator_icon.ts @@ -170,6 +170,7 @@ export class MutatorIcon extends Icon implements IHasBubble { if (this.bubbleIsVisible() === visible) return; await renderManagement.finishQueuedRenders(); + if (this.sourceBlock.isDeadOrDying()) return; if (visible) { this.miniWorkspaceBubble = new MiniWorkspaceBubble( diff --git a/packages/blockly/core/icons/warning_icon.ts b/packages/blockly/core/icons/warning_icon.ts index d0cc9e25369..07a084457fd 100644 --- a/packages/blockly/core/icons/warning_icon.ts +++ b/packages/blockly/core/icons/warning_icon.ts @@ -176,6 +176,7 @@ export class WarningIcon extends Icon implements IHasBubble { if (this.bubbleIsVisible() === visible) return; await renderManagement.finishQueuedRenders(); + if (this.sourceBlock.isDeadOrDying()) return; if (visible) { this.textBubble = new TextBubble( diff --git a/packages/blockly/tests/mocha/block_test.js b/packages/blockly/tests/mocha/block_test.js index 65460e4337c..47214f0ef86 100644 --- a/packages/blockly/tests/mocha/block_test.js +++ b/packages/blockly/tests/mocha/block_test.js @@ -1954,6 +1954,18 @@ suite('Blocks', function () { assert.equal(label, 'Bubble'); }); }); + + test('disposing the block while the bubble is opening does not open it', async function () { + this.block.setWarningText('Warning'); + const icon = this.block.getIcon(Blockly.icons.WarningIcon.TYPE); + const opening = icon.setBubbleVisible(true); + this.block.dispose(); + // Run the frame that setBubbleVisible waits for. + this.clock.runAll(); + await opening; + + assert.isFalse(icon.bubbleIsVisible()); + }); }); suite('Warning icons and collapsing', function () { diff --git a/packages/blockly/tests/mocha/mutator_test.js b/packages/blockly/tests/mocha/mutator_test.js index 3278af6b58b..e2697866c9d 100644 --- a/packages/blockly/tests/mocha/mutator_test.js +++ b/packages/blockly/tests/mocha/mutator_test.js @@ -136,4 +136,46 @@ suite('Mutator', function () { assert.equal(label, 'Bubble'); }); }); + + suite('Disposing the block', function () { + setup(function () { + this.workspace = Blockly.inject('blocklyDiv', DEFAULT_INJECT_OPTIONS); + this.block = createRenderedBlock(this.workspace, 'controls_if'); + this.icon = this.block.getIcon(Blockly.icons.MutatorIcon.TYPE); + }); + + teardown(function () { + sharedTestTeardown.call(this); + }); + + test('while the bubble is opening does not open it', async function () { + const opening = this.icon.setBubbleVisible(true); + this.block.dispose(); + await opening; + + assert.isFalse(this.icon.bubbleIsVisible()); + assertEventNotFired( + this.eventsFireStub, + Blockly.Events.BubbleOpen, + {}, + this.workspace.id, + this.block.id, + ); + }); + + test('while the bubble is closing does not fire a close event', async function () { + await this.icon.setBubbleVisible(true); + const closing = this.icon.setBubbleVisible(false); + this.block.dispose(); + await closing; + + assertEventNotFired( + this.eventsFireStub, + Blockly.Events.BubbleOpen, + {isOpen: false}, + this.workspace.id, + this.block.id, + ); + }); + }); }); From 8e8082131c8c347343dd161fdbab110d91171500 Mon Sep 17 00:00:00 2001 From: Matt Hillsdon Date: Thu, 26 Nov 2026 10:42:15 +0000 Subject: [PATCH 2/2] test: clear focus before restoring real timers in teardown JSDom runs all of a window's animation frames from one setInterval, created with whatever setInterval is global at the time and reused until no frames are pending. Since jsdom 31, while any element has focus, attribute and style changes also start that interval, to check the element is still focusable (jsdom/jsdom#4434). focus_manager_test.js focused the body after sharedTestTeardown had restored the real timers, and nothing reset that, so every later test in the worker started the interval when its teardown changed the DOM after the restore. Later tests' frames then ran on a real interval their fake clock couldn't control. Blockly fires events after a frame, so those tests saw events late or not at all, depending on load and which files shared a worker. Clear focus in sharedTestTeardown while the fake clock is still installed, and drop the body focus from focus_manager_test.js's teardown, which the blur now covers. Part of #10724 --- packages/blockly/tests/mocha/focus_manager_test.js | 3 --- .../blockly/tests/mocha/test_helpers/setup_teardown.js | 8 ++++++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/blockly/tests/mocha/focus_manager_test.js b/packages/blockly/tests/mocha/focus_manager_test.js index 032e7c35d65..e335265b95e 100644 --- a/packages/blockly/tests/mocha/focus_manager_test.js +++ b/packages/blockly/tests/mocha/focus_manager_test.js @@ -130,9 +130,6 @@ suite('FocusManager', function () { } this.allFocusableTrees = []; this.allFocusableNodes = []; - - // Reset the current active element. - document.body.focus(); }); assert.includesClass = function (classList, className) { diff --git a/packages/blockly/tests/mocha/test_helpers/setup_teardown.js b/packages/blockly/tests/mocha/test_helpers/setup_teardown.js index 1801b8278f6..181fcc8fed1 100644 --- a/packages/blockly/tests/mocha/test_helpers/setup_teardown.js +++ b/packages/blockly/tests/mocha/test_helpers/setup_teardown.js @@ -227,6 +227,14 @@ export function sharedTestTeardown(workspace = this.workspace) { ); } + // Clear focus while the fake clock is still installed. While an element + // has focus, JSDom starts its animation frame interval on any DOM change, + // using whatever setInterval is current. If that happens after the + // restore below, later tests' animation frames end up on a real timer + // their fake clock cannot run, so their events arrive late. + document.activeElement?.blur?.(); + this.clock.runAll(); + // Restore all stubbed methods. this.sharedSetupSandbox_.restore(); sinon.restore();