| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
 | window.vimiumDomTestsAreRunning = true
# Install frontend event handlers.
installListeners()
HUD.init()
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
sendKeyboardEvent = (key) ->
  pageKeyboardEventCount = 0
  response = window.callPhantom
    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) ->
    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()
  normalMode = installModes()
  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
  normalMode # Return this.
# Tell Settings that it's been loaded.
Settings.isLoaded = true
# Shoulda.js doesn't support async code, so we try not to use any.
Utils.nextTick = (func) -> func()
#
# Retrieve the hint markers as an array object.
#
getHintMarkers = ->
  Array::slice.call document.getElementsByClassName("vimiumHintMarker"), 0
stubSettings = (key, value) -> stub Settings.cache, key, JSON.stringify value
HintCoordinator.sendMessage = (name, request = {}) -> HintCoordinator[name]? request; request
activateLinkHintsMode = -> HintCoordinator.activateMode HintCoordinator.getHintDescriptors()
#
# Generate tests that are common to both default and filtered
# link hinting modes.
#
createGeneralHintTests = (isFilteredMode) ->
  context "Link hints",
    setup ->
      initializeModeState()
      testContent = "<a>test</a>" + "<a>tress</a>"
      document.getElementById("test-div").innerHTML = testContent
      stubSettings "filterLinkHints", isFilteredMode
      stubSettings "linkHintCharacters", "ab"
      stubSettings "linkHintNumbers", "12"
    tearDown ->
      document.getElementById("test-div").innerHTML = ""
    should "create hints when activated, discard them when deactivated", ->
      linkHints = activateLinkHintsMode()
      assert.isFalse not linkHints.hintMarkerContainingDiv?
      linkHints.deactivateMode()
      assert.isTrue not linkHints.hintMarkerContainingDiv?
    should "position items correctly", ->
      assertStartPosition = (element1, element2) ->
        assert.equal element1.getClientRects()[0].left, element2.getClientRects()[0].left
        assert.equal element1.getClientRects()[0].top, element2.getClientRects()[0].top
      stub document.body, "style", "static"
      linkHints = activateLinkHintsMode()
      hintMarkers = getHintMarkers()
      assertStartPosition document.getElementsByTagName("a")[0], hintMarkers[0]
      assertStartPosition document.getElementsByTagName("a")[1], hintMarkers[1]
      linkHints.deactivateMode()
      stub document.body.style, "position", "relative"
      linkHints = activateLinkHintsMode()
      hintMarkers = getHintMarkers()
      assertStartPosition document.getElementsByTagName("a")[0], hintMarkers[0]
      assertStartPosition document.getElementsByTagName("a")[1], hintMarkers[1]
      linkHints.deactivateMode()
createGeneralHintTests false
createGeneralHintTests true
context "False positives in link-hint",
  setup ->
    testContent = '<span class="buttonWrapper">false positive<a>clickable</a></span>' + '<span class="buttonWrapper">clickable</span>'
    document.getElementById("test-div").innerHTML = testContent
    stubSettings "filterLinkHints", true
    stubSettings "linkHintNumbers", "12"
  tearDown ->
    document.getElementById("test-div").innerHTML = ""
  should "handle false positives", ->
    linkHints = activateLinkHintsMode()
    hintMarkers = getHintMarkers()
    linkHints.deactivateMode()
    assert.equal 2, hintMarkers.length
    for hintMarker in hintMarkers
      assert.equal "clickable", hintMarker.linkText
inputs = []
context "Test link hints for focusing input elements correctly",
  setup ->
    initializeModeState()
    testDiv = document.getElementById("test-div")
    testDiv.innerHTML = ""
    stubSettings "filterLinkHints", false
    stubSettings "linkHintCharacters", "ab"
    # Every HTML5 input type except for hidden. We should be able to activate all of them with link hints.
    #
    # TODO: Re-insert "color" into the inputTypes list when PhantomJS issue #13979 is fixed and integrated.
    # Ref: https://github.com/ariya/phantomjs/issues/13979, and Vimium #1944.
    inputTypes = ["button", "checkbox", "date", "datetime", "datetime-local", "email", "file",
      "image", "month", "number", "password", "radio", "range", "reset", "search", "submit", "tel", "text",
      "time", "url", "week"]
    for type in inputTypes
      input = document.createElement "input"
      input.type = type
      testDiv.appendChild input
      inputs.push input
  tearDown ->
    document.getElementById("test-div").innerHTML = ""
  should "Focus each input when its hint text is typed", ->
    for input in inputs
      input.scrollIntoView() # Ensure the element is visible so we create a link hint for it.
      activeListener = ensureCalled (event) ->
        input.blur() if event.type == "focus"
      input.addEventListener "focus", activeListener, false
      input.addEventListener "click", activeListener, false
      activateLinkHintsMode()
      [hint] = getHintMarkers().filter (hint) ->
        input == HintCoordinator.getLocalHintMarker(hint.hintDescriptor).element
      sendKeyboardEvent char for char in hint.hintString
      input.removeEventListener "focus", activeListener, false
      input.removeEventListener "click", activeListener, false
context "Test link hints for changing mode",
  setup ->
    initializeModeState()
    testDiv = document.getElementById("test-div")
    testDiv.innerHTML = "<a>link</a>"
    @linkHints = activateLinkHintsMode()
  tearDown ->
    document.getElementById("test-div").innerHTML = ""
    @linkHints.deactivateMode()
  should "change mode on shift", ->
    assert.equal "curr-tab", @linkHints.mode.name
    sendKeyboardEvent "shift-down"
    assert.equal "bg-tab", @linkHints.mode.name
    sendKeyboardEvent "shift-up"
    assert.equal "curr-tab", @linkHints.mode.name
  should "change mode on ctrl", ->
    assert.equal "curr-tab", @linkHints.mode.name
    sendKeyboardEvent "ctrl-down"
    assert.equal "fg-tab", @linkHints.mode.name
    sendKeyboardEvent "ctrl-up"
    assert.equal "curr-tab", @linkHints.mode.name
context "Alphabetical link hints",
  setup ->
    initializeModeState()
    stubSettings "filterLinkHints", false
    stubSettings "linkHintCharacters", "ab"
    # Three hints will trigger double hint chars.
    createLinks 3
    @linkHints = activateLinkHintsMode()
  tearDown ->
    @linkHints.deactivateMode()
    document.getElementById("test-div").innerHTML = ""
  should "label the hints correctly", ->
    hintMarkers = getHintMarkers()
    expectedHints = ["aa", "b", "ab"]
    for hint, i in expectedHints
      assert.equal hint, hintMarkers[i].hintString
  should "narrow the hints", ->
    hintMarkers = getHintMarkers()
    sendKeyboardEvent "A"
    assert.equal "none", hintMarkers[1].style.display
    assert.equal "", hintMarkers[0].style.display
  should "generate the correct number of alphabet hints", ->
    alphabetHints = new AlphabetHints
    for n in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      hintStrings = alphabetHints.hintStrings n
      assert.equal n, hintStrings.length
  should "generate non-overlapping alphabet hints", ->
    alphabetHints = new AlphabetHints
    for n in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
      hintStrings = alphabetHints.hintStrings n
      for h1 in hintStrings
        for h2 in hintStrings
          unless h1 == h2
            assert.isFalse 0 == h1.indexOf h2
context "Filtered link hints",
  # Note.  In all of these tests, the order of the elements returned by getHintMarkers() may be different from
  # the order they are listed in the test HTML content.  This is because LinkHints.activateMode() sorts the
  # elements.
  setup ->
    stubSettings "filterLinkHints", true
    stubSettings "linkHintNumbers", "0123456789"
  context "Text hints",
    setup ->
      initializeModeState()
      testContent = "<a>test</a>" + "<a>tress</a>" + "<a>trait</a>" + "<a>track<img alt='alt text'/></a>"
      document.getElementById("test-div").innerHTML = testContent
      @linkHints = activateLinkHintsMode()
    tearDown ->
      document.getElementById("test-div").innerHTML = ""
      @linkHints.deactivateMode()
    should "label the hints", ->
      hintMarkers = getHintMarkers()
      expectedMarkers = [1..4].map (m) -> m.toString()
      actualMarkers = [0...4].map (i) -> hintMarkers[i].textContent.toLowerCase()
      assert.equal expectedMarkers.length, actualMarkers.length
      for marker in expectedMarkers
        assert.isTrue marker in actualMarkers
    should "narrow the hints", ->
      hintMarkers = getHintMarkers()
      sendKeyboardEvent "T"
      sendKeyboardEvent "R"
      assert.equal "none", hintMarkers[0].style.display
      assert.equal "3", hintMarkers[1].hintString
      assert.equal "", hintMarkers[1].style.display
      sendKeyboardEvent "A"
      assert.equal "1", hintMarkers[3].hintString
  context "Image hints",
    setup ->
      initializeModeState()
      testContent = "<a><img alt='alt text'/></a><a><img alt='alt text' title='some title'/></a>
        <a><img title='some title'/></a>" + "<a><img src='' width='320px' height='100px'/></a>"
      document.getElementById("test-div").innerHTML = testContent
      @linkHints = activateLinkHintsMode()
    tearDown ->
      document.getElementById("test-div").innerHTML = ""
      @linkHints.deactivateMode()
    should "label the images", ->
      hintMarkers = getHintMarkers().map (marker) -> marker.textContent.toLowerCase()
      # We don't know the actual hint numbers which will be assigned, so we replace them with "N".
      hintMarkers = hintMarkers.map (str) -> str.replace /^[1-4]/, "N"
      assert.equal 4, hintMarkers.length
      assert.isTrue "N: alt text" in hintMarkers
      assert.isTrue "N: some title" in hintMarkers
      assert.isTrue "N: alt text" in hintMarkers
      assert.isTrue "N" in hintMarkers
  context "Input hints",
    setup ->
      initializeModeState()
      testContent = "<input type='text' value='some value'/><input type='password' value='some value'/>
        <textarea>some text</textarea><label for='test-input'/>a label</label>
        <input type='text' id='test-input' value='some value'/>
        <label for='test-input-2'/>a label: </label><input type='text' id='test-input-2' value='some value'/>"
      document.getElementById("test-div").innerHTML = testContent
      @linkHints = activateLinkHintsMode()
    tearDown ->
      document.getElementById("test-div").innerHTML = ""
      @linkHints.deactivateMode()
    should "label the input elements", ->
      hintMarkers = getHintMarkers()
      hintMarkers = getHintMarkers().map (marker) -> marker.textContent.toLowerCase()
      # We don't know the actual hint numbers which will be assigned, so we replace them with "N".
      hintMarkers = hintMarkers.map (str) -> str.replace /^[0-9]+/, "N"
      assert.equal 5, hintMarkers.length
      assert.isTrue "N" in hintMarkers
      assert.isTrue "N" in hintMarkers
      assert.isTrue "N: a label" in hintMarkers
      assert.isTrue "N: a label" in hintMarkers
      assert.isTrue "N" in hintMarkers
  context "Text hint scoring",
    setup ->
      initializeModeState()
      testContent = [
        {id: 0, text: "the xboy stood on the xburning deck"} # Noise.
        {id: 1, text: "the boy stood on the xburning deck"}  # Whole word (boy).
        {id: 2, text: "on the xboy stood the xburning deck"} # Start of text (on).
        {id: 3, text: "the xboy stood on the xburning deck"} # Noise.
        {id: 4, text: "the xboy stood on the xburning deck"} # Noise.
        {id: 5, text: "the xboy stood on the xburning"}      # Shortest text..
        {id: 6, text: "the xboy stood on the burning xdeck"} # Start of word (bu)
        {id: 7, text: "test abc one - longer"}               # For tab test - 2.
        {id: 8, text: "test abc one"}                        # For tab test - 1.
        {id: 9, text: "test abc one - longer still"}         # For tab test - 3.
      ].map(({id,text}) -> "<a id=\"#{id}\">#{text}</a>").join " "
      document.getElementById("test-div").innerHTML = testContent
      @linkHints = activateLinkHintsMode()
      @getActiveHintMarker = ->
        HintCoordinator.getLocalHintMarker(@linkHints.markerMatcher.activeHintMarker.hintDescriptor).element.id
    tearDown ->
      document.getElementById("test-div").innerHTML = ""
      @linkHints.deactivateMode()
    should "score start-of-word matches highly", ->
      sendKeyboardEvents "bu"
      assert.equal "6", @getActiveHintMarker()
    should "score start-of-text matches highly (br)", ->
      sendKeyboardEvents "on"
      assert.equal "2", @getActiveHintMarker()
    should "score whole-word matches highly", ->
      sendKeyboardEvents "boy"
      assert.equal "1", @getActiveHintMarker()
    should "score shorter texts more highly", ->
      sendKeyboardEvents "stood"
      assert.equal "5", @getActiveHintMarker()
    should "use tab to select the active hint", ->
      sendKeyboardEvents "abc"
      assert.equal "8", @getActiveHintMarker()
      sendKeyboardEvent "tab"
      assert.equal "7", @getActiveHintMarker()
      sendKeyboardEvent "tab"
      assert.equal "9", @getActiveHintMarker()
context "Input focus",
  setup ->
    initializeModeState()
    testContent = "<input type='text' id='first'/><input style='display:none;' id='second'/>
      <input type='password' id='third' value='some value'/>"
    document.getElementById("test-div").innerHTML = testContent
  tearDown ->
    document.getElementById("test-div").innerHTML = ""
  should "focus the first element", ->
    focusInput 1
    assert.equal "first", document.activeElement.id
  should "focus the nth element", ->
    focusInput 100
    assert.equal "third", document.activeElement.id
  should "activate insert mode on the first element", ->
    focusInput 1
    assert.isTrue InsertMode.permanentInstance.isActive()
  should "activate insert mode on the first element", ->
    focusInput 100
    assert.isTrue InsertMode.permanentInstance.isActive()
  should "activate the most recently-selected input if the count is 1", ->
    focusInput 3
    focusInput 1
    assert.equal "third", document.activeElement.id
  should "not trigger insert if there are no inputs", ->
    document.getElementById("test-div").innerHTML = ""
    focusInput 1
    assert.isFalse InsertMode.permanentInstance.isActive()
# TODO: these find prev/next link tests could be refactored into unit tests which invoke a function which has
# a tighter contract than goNext(), since they test minor aspects of goNext()'s link matching behavior, and we
# don't need to construct external state many times over just to test that.
# i.e. these tests should look something like:
# assert.equal(findLink(html("<a href=...">))[0].href, "first")
# These could then move outside of the dom_tests file.
context "Find prev / next links",
  setup ->
    initializeModeState()
    window.location.hash = ""
  should "find exact matches", ->
    document.getElementById("test-div").innerHTML = """
    <a href='#first'>nextcorrupted</a>
    <a href='#second'>next page</a>
    """
    stubSettings "nextPatterns", "next"
    goNext()
    assert.equal '#second', window.location.hash
  should "match against non-word patterns", ->
    document.getElementById("test-div").innerHTML = """
    <a href='#first'>>></a>
    """
    stubSettings "nextPatterns", ">>"
    goNext()
    assert.equal '#first', window.location.hash
  should "favor matches with fewer words", ->
    document.getElementById("test-div").innerHTML = """
    <a href='#first'>lorem ipsum next</a>
    <a href='#second'>next!</a>
    """
    stubSettings "nextPatterns", "next"
    goNext()
    assert.equal '#second', window.location.hash
  should "find link relation in header", ->
    document.getElementById("test-div").innerHTML = """
    <link rel='next' href='#first'>
    """
    goNext()
    assert.equal '#first', window.location.hash
  should "favor link relation to text matching", ->
    document.getElementById("test-div").innerHTML = """
    <link rel='next' href='#first'>
    <a href='#second'>next</a>
    """
    goNext()
    assert.equal '#first', window.location.hash
  should "match mixed case link relation", ->
    document.getElementById("test-div").innerHTML = """
    <link rel='Next' href='#first'>
    """
    goNext()
    assert.equal '#first', window.location.hash
createLinks = (n) ->
  for i in [0...n] by 1
    link = document.createElement("a")
    link.textContent = "test"
    document.getElementById("test-div").appendChild link
context "Key mapping",
  setup ->
    @normalMode = initializeModeState()
    @handlerCalled = false
    @handlerCalledCount = 0
    @normalMode.setCommandHandler ({count}) =>
      @handlerCalled = true
      @handlerCalledCount = count
  should "recognize first mapped key", ->
    assert.isTrue @normalMode.isMappedKey "m"
  should "recognize second mapped key", ->
    assert.isFalse @normalMode.isMappedKey "p"
    sendKeyboardEvent "z"
    assert.isTrue @normalMode.isMappedKey "p"
  should "recognize pass keys", ->
    assert.isTrue @normalMode.isPassKey "p"
  should "not mis-recognize pass keys", ->
    assert.isFalse @normalMode.isMappedKey "p"
    sendKeyboardEvent "z"
    assert.isTrue @normalMode.isMappedKey "p"
  should "recognize initial count keys", ->
    assert.isTrue @normalMode.isCountKey "1"
    assert.isTrue @normalMode.isCountKey "9"
  should "not recognize '0' as initial count key", ->
    assert.isFalse @normalMode.isCountKey "0"
  should "recognize subsequent count keys", ->
    sendKeyboardEvent "1"
    assert.isTrue @normalMode.isCountKey "0"
    assert.isTrue @normalMode.isCountKey "9"
  should "set and call command handler", ->
    sendKeyboardEvent "m"
    assert.isTrue @handlerCalled
    assert.equal 0, pageKeyboardEventCount
  should "not call command handler for pass keys", ->
    sendKeyboardEvent "p"
    assert.isFalse @handlerCalled
    assert.equal 3, pageKeyboardEventCount
  should "accept a count prefix with a single digit", ->
    sendKeyboardEvent "2"
    sendKeyboardEvent "m"
    assert.equal 2, @handlerCalledCount
    assert.equal 0, pageKeyboardEventCount
  should "accept a count prefix with multiple digits", ->
    sendKeyboardEvent "2"
    sendKeyboardEvent "0"
    sendKeyboardEvent "m"
    assert.equal 20, @handlerCalledCount
    assert.equal 0, pageKeyboardEventCount
  should "cancel a count prefix", ->
    sendKeyboardEvent "2"
    sendKeyboardEvent "z"
    sendKeyboardEvent "m"
    assert.equal 1, @handlerCalledCount
    assert.equal 0, pageKeyboardEventCount
  should "accept a count prefix for multi-key command mappings", ->
    sendKeyboardEvent "2"
    sendKeyboardEvent "z"
    sendKeyboardEvent "p"
    assert.equal 2, @handlerCalledCount
    assert.equal 0, pageKeyboardEventCount
  should "cancel a key prefix", ->
    sendKeyboardEvent "z"
    sendKeyboardEvent "m"
    assert.equal 1, @handlerCalledCount
    assert.equal 0, pageKeyboardEventCount
  should "cancel a count prefix after a prefix key", ->
    sendKeyboardEvent "2"
    sendKeyboardEvent "z"
    sendKeyboardEvent "m"
    assert.equal 1, @handlerCalledCount
    assert.equal 0, pageKeyboardEventCount
  should "cancel a prefix key on escape", ->
    sendKeyboardEvent "z"
    sendKeyboardEvent "escape"
    sendKeyboardEvent "p"
    assert.equal 0, @handlerCalledCount
  should "not handle escape on its own", ->
    sendKeyboardEvent "escape"
    assert.equal 2, pageKeyboardEventCount
context "Normal mode",
  setup ->
    initializeModeState()
  should "suppress mapped keys", ->
    sendKeyboardEvent "m"
    assert.equal 0, pageKeyboardEventCount
  should "not suppress unmapped keys", ->
    sendKeyboardEvent "u"
    assert.equal 3, pageKeyboardEventCount
  should "not suppress escape", ->
    sendKeyboardEvent "escape"
    assert.equal 2, pageKeyboardEventCount
  should "not suppress passKeys", ->
    sendKeyboardEvent "p"
    assert.equal 3, pageKeyboardEventCount
  should "suppress passKeys with a non-empty key state (a count)", ->
    sendKeyboardEvent "5"
    sendKeyboardEvent "p"
    assert.equal 0, pageKeyboardEventCount
  should "suppress passKeys with a non-empty key state (a key)", ->
    sendKeyboardEvent "z"
    sendKeyboardEvent "p"
    assert.equal 0, pageKeyboardEventCount
  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 1, 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()
    @insertMode = new InsertMode global: true
  should "not suppress mapped keys in insert mode", ->
    sendKeyboardEvent "m"
    assert.equal 3, pageKeyboardEventCount
  should "exit on escape", ->
    assert.isTrue @insertMode.modeIsActive
    sendKeyboardEvent "escape"
    assert.isFalse @insertMode.modeIsActive
  should "resume normal mode after leaving insert mode", ->
    @insertMode.exit()
    sendKeyboardEvent "m"
    assert.equal 0, pageKeyboardEventCount
context "Triggering insert mode",
  setup ->
    initializeModeState()
    testContent = "<input type='text' id='first'/>
      <input style='display:none;' id='second'/>
      <input type='password' id='third' value='some value'/>
      <p id='fourth' contenteditable='true'/>
      <p id='fifth'/>"
    document.getElementById("test-div").innerHTML = testContent
  tearDown ->
    document.activeElement?.blur()
    document.getElementById("test-div").innerHTML = ""
  should "trigger insert mode on focus of text input", ->
    assert.isFalse InsertMode.permanentInstance.isActive()
    document.getElementById("first").focus()
    assert.isTrue InsertMode.permanentInstance.isActive()
  should "trigger insert mode on focus of password input", ->
    assert.isFalse InsertMode.permanentInstance.isActive()
    document.getElementById("third").focus()
    assert.isTrue InsertMode.permanentInstance.isActive()
  should "trigger insert mode on focus of contentEditable elements", ->
    assert.isFalse InsertMode.permanentInstance.isActive()
    document.getElementById("fourth").focus()
    assert.isTrue InsertMode.permanentInstance.isActive()
  should "not trigger insert mode on other elements", ->
    assert.isFalse InsertMode.permanentInstance.isActive()
    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>
    """
    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()
  should "re-use an existing selection", ->
    assert.equal "I", getSelection()
    sendKeyboardEvents "ww"
    assert.equal "a", getSelection()
    sendKeyboardEvent "escape"
    new VisualMode
    assert.equal "a", getSelection()
  should "not move the selection on caret/visual mode toggle", ->
    sendKeyboardEvents "ww"
    assert.equal "a", getSelection()
    for key in "vcvcvc".split()
      sendKeyboardEvent key
      assert.equal "a", 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>
    """
    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()
    testContent = "<input type='text' id='first'/>
      <input style='display:none;' id='second'/>
      <input type='password' id='third' value='some value'/>"
    document.getElementById("test-div").innerHTML = testContent
  tearDown ->
    document.getElementById("test-div").innerHTML = ""
  should "not have duplicate singletons", ->
    count = 0
    class Test extends Mode
      constructor: -> count += 1; super singleton: "test"
      exit: -> count -= 1; super()
    assert.isTrue count == 0
    for [1..10]
      mode = new Test()
      assert.isTrue count == 1
    mode.exit()
    assert.isTrue count == 0
  should "exit on escape", ->
    test = new Mode exitOnEscape: true
    assert.isTrue test.modeIsActive
    sendKeyboardEvent "escape"
    assert.equal 0, pageKeyboardEventCount
    assert.isFalse test.modeIsActive
  should "not exit on escape if not enabled", ->
    test = new Mode exitOnEscape: false
    assert.isTrue test.modeIsActive
    sendKeyboardEvent "escape"
    assert.equal 2, pageKeyboardEventCount
    assert.isTrue test.modeIsActive
  should "exit on blur", ->
    element = document.getElementById("first")
    element.focus()
    test = new Mode exitOnBlur: element
    assert.isTrue test.modeIsActive
    element.blur()
    assert.isFalse test.modeIsActive
  should "not exit on blur if not enabled", ->
    element = document.getElementById("first")
    element.focus()
    test = new Mode exitOnBlur: false
    assert.isTrue test.modeIsActive
    element.blur()
    assert.isTrue test.modeIsActive
context "PostFindMode",
  setup ->
    initializeModeState()
    testContent = "<input type='text' id='first'/>"
    document.getElementById("test-div").innerHTML = testContent
    document.getElementById("first").focus()
    @postFindMode = new PostFindMode
  tearDown ->
    document.getElementById("test-div").innerHTML = ""
  should "be a singleton", ->
    assert.isTrue @postFindMode.modeIsActive
    new PostFindMode
    assert.isFalse @postFindMode.modeIsActive
  should "suppress unmapped printable keys", ->
    sendKeyboardEvent "m"
    assert.equal 0, pageKeyboardEventCount
  should "be deactivated on click events", ->
    handlerStack.bubbleEvent "click", target: document.activeElement
    assert.isFalse @postFindMode.modeIsActive
  should "enter insert mode on immediate escape", ->
    sendKeyboardEvent "escape"
    assert.equal 0, pageKeyboardEventCount
    assert.isFalse @postFindMode.modeIsActive
  should "not enter insert mode on subsequent escapes", ->
    sendKeyboardEvent "a"
    sendKeyboardEvent "escape"
    assert.isTrue @postFindMode.modeIsActive
 |