diff options
| author | Stephen Blott | 2016-03-25 10:14:39 +0000 |
|---|---|---|
| committer | Stephen Blott | 2016-03-25 10:14:41 +0000 |
| commit | 326d6c486db97764123fdbeb68212624b9941da2 (patch) | |
| tree | f7df2da8bb8f225508afd034fcc5c7262364eb33 | |
| parent | a6fc63636c4e815ae128a8a15e7cf13ea028b297 (diff) | |
| download | vimium-326d6c486db97764123fdbeb68212624b9941da2.tar.bz2 | |
Add tests for visual mode.
The coverage here is far from completem but we do catch the basics.
| -rw-r--r-- | content_scripts/mode_visual.coffee | 4 | ||||
| -rw-r--r-- | tests/dom_tests/dom_tests.coffee | 153 |
2 files changed, 152 insertions, 5 deletions
diff --git a/content_scripts/mode_visual.coffee b/content_scripts/mode_visual.coffee index c2d1037b..cacf8aa3 100644 --- a/content_scripts/mode_visual.coffee +++ b/content_scripts/mode_visual.coffee @@ -250,6 +250,10 @@ class VisualMode extends KeyHandlerMode unless @name == "caret" if @selection.type in [ "Caret", "Range" ] selectionRect = @selection.getRangeAt(0).getBoundingClientRect() + # Only for the tests. We don't have getBoundingClientRect() in the tests, so we patch in a default + # rect here. + # TODO(smblott) Is there a way to stub this properly? + selectionRect ||= {top: 0, bottom: 0, left: 0, right: 0, width: 0, height: 0} selectionRect = Rect.intersect selectionRect, Rect.create 0, 0, window.innerWidth, window.innerHeight if selectionRect.height >= 0 and selectionRect.width >= 0 # The selection is visible in the current viewport. diff --git a/tests/dom_tests/dom_tests.coffee b/tests/dom_tests/dom_tests.coffee index 979500f5..d67b6c5d 100644 --- a/tests/dom_tests/dom_tests.coffee +++ b/tests/dom_tests/dom_tests.coffee @@ -7,6 +7,9 @@ Frame.registerFrameId chromeFrameId: 0 installListener = (element, event, callback) -> element.addEventListener event, (-> callback.apply(this, arguments)), true +getSelection = -> + window.getSelection().toString() + # A count of the number of keyboard events received by the page (for the most recently-sent keystroke). E.g., # we expect 3 if the keystroke is passed through (keydown, keypress, keyup), and 0 if it is suppressed. pageKeyboardEventCount = 0 @@ -17,6 +20,9 @@ sendKeyboardEvent = (key) -> request: "keyboard" key: key +sendKeyboardEvents = (keys) -> + sendKeyboardEvent ch for ch in keys.split() + # These listeners receive events after the main frontend listeners, and do not receive suppressed events. for type in [ "keydown", "keypress", "keyup" ] installListener window, type, (event) -> @@ -326,23 +332,23 @@ context "Filtered link hints", @linkHints.deactivateMode() should "score start-of-word matches highly", -> - sendKeyboardEvent ch for ch in "bu".split() + sendKeyboardEvents "bu" assert.equal "6", @getActiveHintMarker() should "score start-of-text matches highly (br)", -> - sendKeyboardEvent ch for ch in "on".split() + sendKeyboardEvents "on" assert.equal "2", @getActiveHintMarker() should "score whole-word matches highly", -> - sendKeyboardEvent ch for ch in "boy".split() + sendKeyboardEvents "boy" assert.equal "1", @getActiveHintMarker() should "score shorter texts more highly", -> - sendKeyboardEvent ch for ch in "stood".split() + sendKeyboardEvents "stood" assert.equal "5", @getActiveHintMarker() should "use tab to select the active hint", -> - sendKeyboardEvent ch for ch in "abc".split() + sendKeyboardEvents "abc" assert.equal "8", @getActiveHintMarker() sendKeyboardEvent "tab" assert.equal "7", @getActiveHintMarker() @@ -728,6 +734,143 @@ context "Triggering insert mode", document.getElementById("fifth").focus() assert.isFalse InsertMode.permanentInstance.isActive() +context "Caret mode", + setup -> + document.getElementById("test-div").innerHTML = """ + <p><pre> + It is an ancient Mariner, + And he stoppeth one of three. + By thy long grey beard and glittering eye, + Now wherefore stopp'st thou me? + </pre></p> + <p><pre> + The Bridegroom's doors are opened wide, + And I am next of kin; + The guests are met, the feast is set: + May'st hear the merry din. + </pre></p> + """ + initializeModeState() + @initialVisualMode = new VisualMode + + tearDown -> + document.getElementById("test-div").innerHTML = "" + + should "enter caret mode", -> + assert.isFalse @initialVisualMode.modeIsActive + assert.equal "I", getSelection() + + should "exit caret mode on escape", -> + sendKeyboardEvent "escape" + assert.equal "", getSelection() + + should "move caret with l and h", -> + assert.equal "I", getSelection() + sendKeyboardEvent "l" + assert.equal "t", getSelection() + sendKeyboardEvent "h" + assert.equal "I", getSelection() + + should "move caret with w and b", -> + assert.equal "I", getSelection() + sendKeyboardEvent "w" + assert.equal "i", getSelection() + sendKeyboardEvent "b" + assert.equal "I", getSelection() + + should "move caret with e", -> + assert.equal "I", getSelection() + sendKeyboardEvent "e" + assert.equal " ", getSelection() + sendKeyboardEvent "e" + assert.equal " ", getSelection() + + should "move caret with j and k", -> + assert.equal "I", getSelection() + sendKeyboardEvent "j" + assert.equal "A", getSelection() + sendKeyboardEvent "k" + assert.equal "I", getSelection() + +context "Visual mode", + setup -> + document.getElementById("test-div").innerHTML = """ + <p><pre> + It is an ancient Mariner, + And he stoppeth one of three. + By thy long grey beard and glittering eye, + Now wherefore stopp'st thou me? + </pre></p> + <p><pre> + The Bridegroom's doors are opened wide, + And I am next of kin; + The guests are met, the feast is set: + May'st hear the merry din. + </pre></p> + """ + initializeModeState() + @initialVisualMode = new VisualMode + sendKeyboardEvent "w" + sendKeyboardEvent "w" + # We should now be at the "a" of "an". + sendKeyboardEvent "v" + + tearDown -> + document.getElementById("test-div").innerHTML = "" + + should "select word with e", -> + assert.equal "a", getSelection() + sendKeyboardEvent "e" + assert.equal "an", getSelection() + sendKeyboardEvent "e" + assert.equal "an ancient", getSelection() + + should "select opposite end of the selection with o", -> + assert.equal "a", getSelection() + sendKeyboardEvent "e" + assert.equal "an", getSelection() + sendKeyboardEvent "e" + assert.equal "an ancient", getSelection() + sendKeyboardEvents "ow" + assert.equal "ancient", getSelection() + sendKeyboardEvents "oe" + assert.equal "ancient Mariner", getSelection() + + should "accept a count", -> + assert.equal "a", getSelection() + sendKeyboardEvents "2e" + assert.equal "an ancient", getSelection() + + should "select a word", -> + assert.equal "a", getSelection() + sendKeyboardEvents "aw" + assert.equal "an", getSelection() + + should "select a word with a count", -> + assert.equal "a", getSelection() + sendKeyboardEvents "2aw" + assert.equal "an ancient", getSelection() + + should "select a word with a count", -> + assert.equal "a", getSelection() + sendKeyboardEvents "2aw" + assert.equal "an ancient", getSelection() + + should "select to start of line", -> + assert.equal "a", getSelection() + sendKeyboardEvents "0" + assert.equal "It is", getSelection().trim() + + should "select to end of line", -> + assert.equal "a", getSelection() + sendKeyboardEvents "$" + assert.equal "an ancient Mariner,", getSelection() + + should "re-enter caret mode", -> + assert.equal "a", getSelection() + sendKeyboardEvents "cww" + assert.equal "M", getSelection() + context "Mode utilities", setup -> initializeModeState() |
