Skip to content

Commit

Permalink
changes to get unit tests passing, see #1570
Browse files Browse the repository at this point in the history
  • Loading branch information
jessegreenberg committed Apr 25, 2024
1 parent 014658a commit b2506ab
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
7 changes: 6 additions & 1 deletion js/input/Hotkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @author Jonathan Olson <[email protected]>
*/

import { EnglishKey, hotkeyManager, scenery } from '../imports.js';
import { EnglishKey, EnglishStringToCodeMap, hotkeyManager, scenery } from '../imports.js';
import optionize from '../../../phet-core/js/optionize.js';
import EnabledComponent, { EnabledComponentOptions } from '../../../axon/js/EnabledComponent.js';
import TProperty from '../../../axon/js/TProperty.js';
Expand Down Expand Up @@ -158,6 +158,11 @@ export default class Hotkey extends EnabledComponent {

this.keys = _.uniq( [ this.key, ...this.modifierKeys ] );

// Make sure that every key has an entry in the EnglishStringToCodeMap
for ( const key of this.keys ) {
assert && assert( EnglishStringToCodeMap[ key ], `No codes for this key exists, do you need to add it to EnglishStringToCodeMap?: ${key}` );
}

// Create a timer to handle the optional fire-on-hold feature.
if ( this.fireOnHold && this.fireOnHoldTiming === 'custom' ) {
this.fireOnHoldTimer = new CallbackTimer( {
Expand Down
13 changes: 12 additions & 1 deletion js/listeners/KeyboardListenerTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,16 @@ QUnit.test( 'KeyboardListener Tests', assert => {
bogusListener.dispose();
}, Error, 'Constructor should catch providing bad keys at runtime' );

const a = new Node( { tagName: 'div' } );
const a = new Node( { tagName: 'div', focusable: true } );
rootNode.addChild( a );
a.addInputListener( listener );

const domElementA = a.pdomInstances[ 0 ].peer!.primarySibling!;
assert.ok( domElementA, 'pdom element needed' );

// Hotkey uses the focused Trail to determine if it should fire, so we need to focus the element
a.focus();

triggerKeydownEvent( domElementA, KeyboardUtils.KEY_TAB );
assert.ok( !callbackFired, 'should not fire on tab' );
triggerKeyupEvent( domElementA, KeyboardUtils.KEY_TAB );
Expand Down Expand Up @@ -103,7 +106,15 @@ QUnit.test( 'KeyboardListener Tests', assert => {
}
}
} );

a.addInputListener( listenerWithOverlappingKeys );

// TODO: https://github.com/phetsims/scenery/issues/1570, This is a workaround for a problem with hotkeyManager
// where it doesn't update the availableHotkeys when new input listeners are added. We can manually update by
// triggering focus changes.
a.blur();
a.focus();

triggerKeydownEvent( domElementA, KeyboardUtils.KEY_P, true );
assert.ok( !pFired, 'p should not fire because control key is down' );
assert.ok( ctrlPFired, 'ctrl P should have fired' );
Expand Down

0 comments on commit b2506ab

Please sign in to comment.