diff options
| -rw-r--r-- | content_scripts/mode_key_handler.coffee | 2 | ||||
| -rw-r--r-- | content_scripts/vimium_frontend.coffee | 6 | ||||
| -rw-r--r-- | tests/dom_tests/dom_tests.coffee | 107 |
3 files changed, 111 insertions, 4 deletions
diff --git a/content_scripts/mode_key_handler.coffee b/content_scripts/mode_key_handler.coffee index 9946dddf..f2c4a745 100644 --- a/content_scripts/mode_key_handler.coffee +++ b/content_scripts/mode_key_handler.coffee @@ -15,6 +15,8 @@ class KeyHandlerMode extends Mode keydownEvents: {} setKeyMapping: (@keyMapping) -> @reset() setPassKeys: (@passKeys) -> @reset() + # Only for tests. + setCommandHandler: (@commandHandler) -> # Reset the key state, optionally retaining the count provided. reset: (@countPrefix = 0) -> diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee index 972911c5..2e57ee1a 100644 --- a/content_scripts/vimium_frontend.coffee +++ b/content_scripts/vimium_frontend.coffee @@ -126,11 +126,11 @@ class NormalMode extends KeyHandlerMode else Utils.invokeCommandString registryEntry.command for i in [0...count] -# Only exported for tests; also, "args..." is only for the tests. -window.initializeModes = (args...) -> +# Only exported for tests. +window.initializeModes = -> # Install the permanent modes. The permanently-installed insert mode tracks focus/blur events, and # activates/deactivates itself accordingly. normalMode is exported only for the tests. - window.normalMode = new NormalMode args... + window.normalMode = new NormalMode new InsertMode permanent: true Scroller.init() diff --git a/tests/dom_tests/dom_tests.coffee b/tests/dom_tests/dom_tests.coffee index c2c34fa8..ba0ee9d2 100644 --- a/tests/dom_tests/dom_tests.coffee +++ b/tests/dom_tests/dom_tests.coffee @@ -20,12 +20,22 @@ for type in [ "keydown", "keypress", "keyup" ] installListener window, type, (event) -> pageKeyboardEventCount += 1 +commandName = commandCount = null + # Some tests have side effects on the handler stack and the active mode, so these are reset on setup. initializeModeState = -> Mode.reset() handlerStack.reset() - initializeModes keyMapping: {m: {}, p: {}, z: {p: {}}} + initializeModes() normalMode.setPassKeys "p" + normalMode.setKeyMapping + m: options: {}, command: "m" # A mapped key. + p: options: {}, command: "p" # A pass key. + z: + p: options: {}, command: "zp" # Not a pass key. + normalMode.setCommandHandler ({command, count}) -> + [commandName, commandCount] = [command.command, count] + commandName = commandCount = null # Tell Settings that it's been loaded. Settings.isLoaded = true @@ -378,6 +388,101 @@ context "Normal mode", sendKeyboardEvent "p" assert.equal pageKeyboardEventCount, 0 + should "invoke commands for mapped keys", -> + sendKeyboardEvent "m" + assert.equal "m", commandName + + should "invoke commands for mapped keys with a mapped prefix", -> + sendKeyboardEvent "z" + sendKeyboardEvent "m" + assert.equal "m", commandName + + should "invoke commands for mapped keys with an unmapped prefix", -> + sendKeyboardEvent "a" + sendKeyboardEvent "m" + assert.equal "m", commandName + + should "not invoke commands for pass keys", -> + sendKeyboardEvent "p" + assert.equal null, commandName + + should "not invoke commands for pass keys with an unmapped prefix", -> + sendKeyboardEvent "a" + sendKeyboardEvent "p" + assert.equal null, commandName + + should "invoke commands for pass keys with a count", -> + sendKeyboardEvent "1" + sendKeyboardEvent "p" + assert.equal "p", commandName + + should "invoke commands for pass keys with a key queue", -> + sendKeyboardEvent "z" + sendKeyboardEvent "p" + assert.equal "zp", commandName + + should "default to a count of 1", -> + sendKeyboardEvent "m" + assert.equal 1, commandCount + + should "accept count prefixes of length 1", -> + sendKeyboardEvent "2" + sendKeyboardEvent "m" + assert.equal 2, commandCount + + should "accept count prefixes of length 2", -> + sendKeyboardEvent "12" + sendKeyboardEvent "m" + assert.equal 12, commandCount + + should "get the correct count for mixed inputs (single key)", -> + sendKeyboardEvent "2" + sendKeyboardEvent "z" + sendKeyboardEvent "m" + assert.equal 1, commandCount + + should "get the correct count for mixed inputs (multi key)", -> + sendKeyboardEvent "2" + sendKeyboardEvent "z" + sendKeyboardEvent "p" + assert.equal 2, commandCount + + should "get the correct count for mixed inputs (multi key, duplicates)", -> + sendKeyboardEvent "2" + sendKeyboardEvent "z" + sendKeyboardEvent "z" + sendKeyboardEvent "p" + assert.equal 2, commandCount + + should "get the correct count for mixed inputs (with leading mapped keys)", -> + sendKeyboardEvent "z" + sendKeyboardEvent "2" + sendKeyboardEvent "m" + assert.equal 2, commandCount + + should "get the correct count for mixed inputs (with leading unmapped keys)", -> + sendKeyboardEvent "a" + sendKeyboardEvent "2" + sendKeyboardEvent "m" + assert.equal 2, commandCount + + should "not get a count after unmapped keys", -> + sendKeyboardEvent "2" + sendKeyboardEvent "a" + sendKeyboardEvent "m" + assert.equal 1, commandCount + + should "get the correct count after unmapped keys", -> + sendKeyboardEvent "2" + sendKeyboardEvent "a" + sendKeyboardEvent "3" + sendKeyboardEvent "m" + assert.equal 3, commandCount + + should "not handle unmapped keys", -> + sendKeyboardEvent "u" + assert.equal null, commandCount + context "Insert mode", setup -> initializeModeState() |
