@@ -183,6 +183,57 @@ suite('Toolbox', function () {
183183 } ) ;
184184 } ) ;
185185
186+ suite ( 'focus management' , function ( ) {
187+ setup ( function ( ) {
188+ this . toolbox = getInjectedToolbox ( ) ;
189+ } ) ;
190+ teardown ( function ( ) {
191+ this . toolbox . dispose ( ) ;
192+ } ) ;
193+
194+ test ( 'Losing focus hides autoclosing flyout' , function ( ) {
195+ // Focus the toolbox and select a category to open the flyout.
196+ const target = this . toolbox . HtmlDiv . querySelector (
197+ '.blocklyToolboxCategory' ,
198+ ) ;
199+ Blockly . getFocusManager ( ) . focusNode ( this . toolbox ) ;
200+ target . dispatchEvent (
201+ new PointerEvent ( 'pointerdown' , {
202+ target,
203+ bubbles : true ,
204+ } ) ,
205+ ) ;
206+ assert . isTrue ( this . toolbox . getFlyout ( ) . isVisible ( ) ) ;
207+
208+ // Focus the workspace to trigger the toolbox to close the flyout.
209+ Blockly . getFocusManager ( ) . focusNode ( this . toolbox . getWorkspace ( ) ) ;
210+ assert . isFalse ( this . toolbox . getFlyout ( ) . isVisible ( ) ) ;
211+ } ) ;
212+
213+ test ( 'Losing focus does not hide non-autoclosing flyout' , function ( ) {
214+ // Make the toolbox's flyout non-autoclosing.
215+ this . toolbox . getFlyout ( ) . setAutoClose ( false ) ;
216+
217+ // Focus the toolbox and select a category to open the flyout.
218+ const target = this . toolbox . HtmlDiv . querySelector (
219+ '.blocklyToolboxCategory' ,
220+ ) ;
221+ Blockly . getFocusManager ( ) . focusNode ( this . toolbox ) ;
222+ target . dispatchEvent (
223+ new PointerEvent ( 'pointerdown' , {
224+ target,
225+ bubbles : true ,
226+ } ) ,
227+ ) ;
228+ assert . isTrue ( this . toolbox . getFlyout ( ) . isVisible ( ) ) ;
229+
230+ // Focus the workspace; this should *not* trigger the toolbox to close the
231+ // flyout, which should remain visible.
232+ Blockly . getFocusManager ( ) . focusNode ( this . toolbox . getWorkspace ( ) ) ;
233+ assert . isTrue ( this . toolbox . getFlyout ( ) . isVisible ( ) ) ;
234+ } ) ;
235+ } ) ;
236+
186237 suite ( 'onClick_' , function ( ) {
187238 setup ( function ( ) {
188239 this . toolbox = getInjectedToolbox ( ) ;
0 commit comments