keyboard.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. var normalized_shortcuts = [
  2. 'ctrl-shift-m',
  3. 'alt-meta-p',
  4. ];
  5. var to_normalize = [
  6. ['shift-%', 'shift-5'],
  7. ['ShiFT-MeTa-CtRl-AlT-m', 'alt-ctrl-meta-shift-m'],
  8. ];
  9. var unshifted = "` 1 2 3 4 5 6 7 8 9 0 - = q w e r t y u i o p [ ] \\ a s d f g h j k l ; ' z x c v b n m , . /";
  10. // shifted = '~ ! @ # $ % ^ & * ( ) _ + Q W E R T Y U I O P { } | A S D F G H J K L : " Z X C V B N M < > ?';
  11. var ambiguous_expect = function(ch){
  12. // `-` is ambiguous in shortcut context as a separator, so map it to `minus`
  13. if(ch === '-'){
  14. return 'minus';
  15. }
  16. return ch;
  17. };
  18. casper.notebook_test(function () {
  19. var that = this;
  20. this.then(function () {
  21. this.each(unshifted.split(' '), function (self, item) {
  22. var result = this.evaluate(function (sc) {
  23. var e = IPython.keyboard.shortcut_to_event(sc);
  24. var sc2 = IPython.keyboard.event_to_shortcut(e);
  25. return sc2;
  26. }, item);
  27. this.test.assertEquals(result, ambiguous_expect(item), 'Shortcut to event roundtrip: '+item);
  28. });
  29. });
  30. this.then(function () {
  31. this.each(to_normalize, function (self, item) {
  32. var result = this.evaluate(function (pair) {
  33. return IPython.keyboard.normalize_shortcut(pair[0]);
  34. }, item);
  35. this.test.assertEquals(result, item[1], 'Normalize shortcut: '+item[0]);
  36. });
  37. });
  38. this.then(function () {
  39. this.each(normalized_shortcuts, function (self, item) {
  40. var result = this.evaluate(function (sc) {
  41. var e = IPython.keyboard.shortcut_to_event(sc);
  42. var sc2 = IPython.keyboard.event_to_shortcut(e);
  43. return sc2;
  44. }, item);
  45. this.test.assertEquals(result, item, 'Shortcut to event roundtrip: '+item);
  46. });
  47. });
  48. this.then(function(){
  49. var shortcuts_test = {
  50. 'i,e,e,e,e,e' : '[[5E]]',
  51. 'i,d,d,q,d' : '[[TEST1]]',
  52. 'i,d,d' : '[[TEST1 WILL BE SHADOWED]]',
  53. 'i,d,k' : '[[SHOULD SHADOW TEST2]]',
  54. 'i,d,k,r,q' : '[[TEST2 NOT SHADOWED]]',
  55. ';,up,down,up,down,left,right,left,right,b,a' : '[[KONAMI]]',
  56. 'ctrl-x,meta-c,meta-b,u,t,t,e,r,f,l,y' : '[[XKCD]]'
  57. };
  58. that.msgs = [];
  59. that.on('remote.message', function(msg) {
  60. that.msgs.push(msg);
  61. });
  62. that.evaluate(function (obj) {
  63. for(var k in obj){
  64. if ({}.hasOwnProperty.call(obj, k)) {
  65. IPython.keyboard_manager.command_shortcuts.add_shortcut(k, function(){console.log(obj[k]);});
  66. }
  67. }
  68. }, shortcuts_test);
  69. var longer_first = false;
  70. var longer_last = false;
  71. for(var m in that.msgs){
  72. if ({}.hasOwnProperty.call(that.msgs, m)) {
  73. longer_first = longer_first||(that.msgs[m].match(/you are overriting/)!= null);
  74. longer_last = longer_last ||(that.msgs[m].match(/will be shadowed/) != null);
  75. }
  76. }
  77. this.test.assert(longer_first, 'no warning if registering shorter shortut');
  78. this.test.assert(longer_last , 'no warning if registering longer shortut');
  79. });
  80. });