Initial commit for changing key mappings (#2787)

* Added ability to easily change key mappings
This commit is contained in:
alschmiedt
2019-08-13 13:37:01 -07:00
committed by GitHub
parent 4758b4aa55
commit 74fa3bb71a
11 changed files with 494 additions and 226 deletions

View File

@@ -42,6 +42,7 @@
<script src="field_number_test.js"></script>
<script src="field_textinput_test.js"></script>
<script src="field_variable_test.js"></script>
<script src="key_map_test.js"></script>
<script src="navigation_modify_test.js"></script>
<script src="navigation_test.js"></script>
<script src="procedures_test.js"></script>

View File

@@ -0,0 +1,66 @@
suite('Key Map Tests', function() {
setup(function() {
Blockly.user.keyMap.setKeyMap(Blockly.user.keyMap.createDefaultKeyMap());
});
test('Test adding a new action to key map', function() {
var newAction = new Blockly.Action('test_action', 'test', function(){
return "test";
});
Blockly.user.keyMap.setActionForKey('65', newAction);
assertEquals(Blockly.user.keyMap.map_['65'].name, 'test_action');
});
test('Test giving an old action a new key', function() {
Blockly.user.keyMap.setActionForKey(goog.events.KeyCodes.F, Blockly.Navigation.ACTION_PREVIOUS);
assertEquals(Blockly.user.keyMap.map_[goog.events.KeyCodes.W], undefined);
assertEquals(Blockly.user.keyMap.map_[goog.events.KeyCodes.F],
Blockly.Navigation.ACTION_PREVIOUS);
});
test('Test get key by action defined', function() {
var key = Blockly.user.keyMap.getKeyByAction(Blockly.Navigation.ACTION_PREVIOUS);
assertEquals(key, goog.events.KeyCodes.W);
});
test('Test get key by action undefined', function() {
var key = Blockly.user.keyMap.getKeyByAction(new Blockly.Action('something'));
assertEquals(key, undefined);
});
test('Test set key map', function() {
var testKeyMap = Blockly.user.keyMap.createDefaultKeyMap();
testKeyMap['randomKey'] = new Blockly.Action('test','',null);
Blockly.user.keyMap.setKeyMap(testKeyMap);
assertEquals(Blockly.user.keyMap.map_['randomKey'].name, 'test');
});
test('Test get key map returns a clone', function() {
var keyMap = Blockly.user.keyMap.getKeyMap();
keyMap['randomKey'] = new Blockly.Action('test', '', null);
assertEquals(Blockly.user.keyMap.map_['randomKey'], undefined);
});
test('Test serialize key code with modifiers', function() {
var mockEvent = {
getModifierState: function(){
return true;
},
keyCode: 65
};
var serializedKey = Blockly.user.keyMap.serializeKeyEvent(mockEvent);
assertEquals(serializedKey, 'ShiftControlAltMeta65');
});
test('Test serialize key code without modifiers', function() {
var mockEvent = {
getModifierState: function(){
return false;
},
keyCode: 65
};
var serializedKey = Blockly.user.keyMap.serializeKeyEvent(mockEvent);
assertEquals(serializedKey, '65');
});
teardown(function() {});
});

View File

@@ -8,25 +8,26 @@ suite('Navigation', function() {
}]);
var toolbox = document.getElementById('toolbox-minimal');
this.mockEvent = {
getModifierState: function() {
return false;
}
};
this.workspace = Blockly.inject('blocklyDiv', {toolbox: toolbox});
});
test('Focuses workspace from flyout (e)', function() {
Blockly.Navigation.currentState_ = Blockly.Navigation.STATE_FLYOUT;
var mockEvent = {
keyCode: goog.events.KeyCodes.E
};
chai.assert.isTrue(Blockly.Navigation.navigate(mockEvent));
this.mockEvent.keyCode = goog.events.KeyCodes.E;
chai.assert.isTrue(Blockly.Navigation.onKeyPress(this.mockEvent));
chai.assert.equal(Blockly.Navigation.currentState_,
Blockly.Navigation.STATE_WS);
});
test('Focuses workspace from flyout (escape)', function() {
Blockly.Navigation.currentState_ = Blockly.Navigation.STATE_FLYOUT;
var mockEvent = {
keyCode: goog.events.KeyCodes.ESC
};
chai.assert.isTrue(Blockly.Navigation.navigate(mockEvent));
this.mockEvent.keyCode = goog.events.KeyCodes.ESC;
chai.assert.isTrue(Blockly.Navigation.onKeyPress(this.mockEvent));
chai.assert.equal(Blockly.Navigation.currentState_,
Blockly.Navigation.STATE_WS);
});
@@ -53,7 +54,11 @@ suite('Navigation', function() {
var toolbox = document.getElementById('toolbox-categories');
this.workspace = Blockly.inject('blocklyDiv', {toolbox: toolbox});
Blockly.Navigation.focusToolbox();
this.mockEvent = {
getModifierState: function() {
return false;
}
};
this.firstCategory_ = this.workspace.getToolbox().tree_.firstChild_;
this.secondCategory_ = this.firstCategory_.getNextShownNode();
@@ -66,9 +71,8 @@ suite('Navigation', function() {
});
test('Next', function() {
chai.assert.isTrue(Blockly.Navigation.navigate({
keyCode: goog.events.KeyCodes.S
}));
this.mockEvent.keyCode = goog.events.KeyCodes.S;
chai.assert.isTrue(Blockly.Navigation.onKeyPress(this.mockEvent));
chai.assert.equal(Blockly.Navigation.currentState_,
Blockly.Navigation.STATE_TOOLBOX);
chai.assert.equal(Blockly.Navigation.currentCategory_,
@@ -78,10 +82,9 @@ suite('Navigation', function() {
// Should be a no-op.
test('Next at end', function() {
Blockly.Navigation.nextCategory();
this.mockEvent.keyCode = goog.events.KeyCodes.S;
var startCategory = Blockly.Navigation.currentCategory_;
chai.assert.isTrue(Blockly.Navigation.navigate({
keyCode: goog.events.KeyCodes.S
}));
chai.assert.isTrue(Blockly.Navigation.onKeyPress(this.mockEvent));
chai.assert.equal(Blockly.Navigation.currentState_,
Blockly.Navigation.STATE_TOOLBOX);
chai.assert.equal(Blockly.Navigation.currentCategory_,
@@ -91,11 +94,10 @@ suite('Navigation', function() {
test('Previous', function() {
// Go forward one so that we can go back one:
Blockly.Navigation.nextCategory();
this.mockEvent.keyCode = goog.events.KeyCodes.W;
chai.assert.equal(Blockly.Navigation.currentCategory_,
this.secondCategory_);
chai.assert.isTrue(Blockly.Navigation.navigate({
keyCode: goog.events.KeyCodes.W
}));
chai.assert.isTrue(Blockly.Navigation.onKeyPress(this.mockEvent));
chai.assert.equal(Blockly.Navigation.currentState_,
Blockly.Navigation.STATE_TOOLBOX);
chai.assert.equal(Blockly.Navigation.currentCategory_,
@@ -105,9 +107,8 @@ suite('Navigation', function() {
// Should be a no-op.
test('Previous at start', function() {
var startCategory = Blockly.Navigation.currentCategory_;
chai.assert.isTrue(Blockly.Navigation.navigate({
keyCode: goog.events.KeyCodes.W
}));
this.mockEvent.keyCode = goog.events.KeyCodes.W;
chai.assert.isTrue(Blockly.Navigation.onKeyPress(this.mockEvent));
chai.assert.equal(Blockly.Navigation.currentState_,
Blockly.Navigation.STATE_TOOLBOX);
chai.assert.equal(Blockly.Navigation.currentCategory_,
@@ -115,9 +116,8 @@ suite('Navigation', function() {
});
test('Out', function() {
chai.assert.isTrue(Blockly.Navigation.navigate({
keyCode: goog.events.KeyCodes.A
}));
this.mockEvent.keyCode = goog.events.KeyCodes.A;
chai.assert.isTrue(Blockly.Navigation.onKeyPress(this.mockEvent));
// TODO (fenichel/aschmiedt): Decide whether out should go to the
// workspace.
chai.assert.equal(Blockly.Navigation.currentState_,
@@ -125,9 +125,8 @@ suite('Navigation', function() {
});
test('Go to flyout', function() {
chai.assert.isTrue(Blockly.Navigation.navigate({
keyCode: goog.events.KeyCodes.D
}));
this.mockEvent.keyCode = goog.events.KeyCodes.D;
chai.assert.isTrue(Blockly.Navigation.onKeyPress(this.mockEvent));
chai.assert.equal(Blockly.Navigation.currentState_,
Blockly.Navigation.STATE_FLYOUT);
@@ -136,19 +135,15 @@ suite('Navigation', function() {
test('Focuses workspace from toolbox (e)', function() {
Blockly.Navigation.currentState_ = Blockly.Navigation.STATE_TOOLBOX;
var mockEvent = {
keyCode: goog.events.KeyCodes.E
};
chai.assert.isTrue(Blockly.Navigation.navigate(mockEvent));
this.mockEvent.keyCode = goog.events.KeyCodes.E;
chai.assert.isTrue(Blockly.Navigation.onKeyPress(this.mockEvent));
chai.assert.equal(Blockly.Navigation.currentState_,
Blockly.Navigation.STATE_WS);
});
test('Focuses workspace from toolbox (escape)', function() {
Blockly.Navigation.currentState_ = Blockly.Navigation.STATE_TOOLBOX;
var mockEvent = {
keyCode: goog.events.KeyCodes.ESC
};
chai.assert.isTrue(Blockly.Navigation.navigate(mockEvent));
this.mockEvent.keyCode = goog.events.KeyCodes.E;
chai.assert.isTrue(Blockly.Navigation.onKeyPress(this.mockEvent));
chai.assert.equal(Blockly.Navigation.currentState_,
Blockly.Navigation.STATE_WS);
});