aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorJez Ng2012-10-20 23:17:30 -0400
committerJez Ng2012-10-20 23:17:45 -0400
commitea73be17468b1bd02171c82e1fb21b1bc16ccd0e (patch)
tree223e4a3b56a410f599e19f81b38752ded86d44c6 /tests
parent2ed217b43663a553c78fa08a8a10152a43e93cf5 (diff)
downloadvimium-ea73be17468b1bd02171c82e1fb21b1bc16ccd0e.tar.bz2
Refactor handlerStack. Closes #657.
Previously, handlerStack was designed only for removal of the handler right at the top of the stack. However, some handlers sought to remove themselves when they were not at the top of the stack, creating confusion. The new handlerStack ensures that such removal can always be done safely.
Diffstat (limited to 'tests')
-rw-r--r--tests/dom_tests/dom_tests.coffee4
-rw-r--r--tests/dom_tests/dom_tests.html1
-rw-r--r--tests/unit_tests/handler_stack_test.coffee52
3 files changed, 55 insertions, 2 deletions
diff --git a/tests/dom_tests/dom_tests.coffee b/tests/dom_tests/dom_tests.coffee
index 3d981ee5..a0254acf 100644
--- a/tests/dom_tests/dom_tests.coffee
+++ b/tests/dom_tests/dom_tests.coffee
@@ -177,11 +177,11 @@ context "Input focus",
focusInput 1
assert.equal "first", document.activeElement.id
# deactivate the tabbing mode and its overlays
- handlerStack[handlerStack.length - 1].keydown mockKeyboardEvent("A")
+ handlerStack.bubbleEvent 'keydown', mockKeyboardEvent("A")
focusInput 100
assert.equal "third", document.activeElement.id
- handlerStack[handlerStack.length - 1].keydown mockKeyboardEvent("A")
+ handlerStack.bubbleEvent 'keydown', mockKeyboardEvent("A")
Tests.outputMethod = (args...) ->
newOutput = args.join "\n"
diff --git a/tests/dom_tests/dom_tests.html b/tests/dom_tests/dom_tests.html
index 1dc45782..c658235b 100644
--- a/tests/dom_tests/dom_tests.html
+++ b/tests/dom_tests/dom_tests.html
@@ -32,6 +32,7 @@
<script type="text/javascript" src="../../lib/utils.js"></script>
<script type="text/javascript" src="../../lib/keyboard_utils.js"></script>
<script type="text/javascript" src="../../lib/dom_utils.js"></script>
+ <script type="text/javascript" src="../../lib/handler_stack.js"></script>
<script type="text/javascript" src="../../lib/clipboard.js"></script>
<script type="text/javascript" src="../../content_scripts/link_hints.js"></script>
<script type="text/javascript" src="../../content_scripts/vomnibar.js"></script>
diff --git a/tests/unit_tests/handler_stack_test.coffee b/tests/unit_tests/handler_stack_test.coffee
new file mode 100644
index 00000000..0ed8f4c0
--- /dev/null
+++ b/tests/unit_tests/handler_stack_test.coffee
@@ -0,0 +1,52 @@
+require "./test_helper.js"
+extend(global, require "../../lib/handler_stack.js")
+
+context "handlerStack",
+ setup ->
+ stub global, "DomUtils", {}
+ stub DomUtils, "suppressEvent", ->
+ @handlerStack = new HandlerStack
+ @handler1Called = false
+ @handler2Called = false
+
+ should "bubble events", ->
+ @handlerStack.push { keydown: => @handler1Called = true }
+ @handlerStack.push { keydown: => @handler2Called = true }
+ @handlerStack.bubbleEvent 'keydown', {}
+ assert.isTrue @handler2Called
+ assert.isTrue @handler1Called
+
+ should "terminate bubbling on falsy return value", ->
+ @handlerStack.push { keydown: => @handler1Called = true }
+ @handlerStack.push { keydown: => @handler2Called = true; false }
+ @handlerStack.bubbleEvent 'keydown', {}
+ assert.isTrue @handler2Called
+ assert.isFalse @handler1Called
+
+ should "remove handlers correctly", ->
+ @handlerStack.push { keydown: => @handler1Called = true }
+ handlerId = @handlerStack.push { keydown: => @handler2Called = true }
+ @handlerStack.remove handlerId
+ @handlerStack.bubbleEvent 'keydown', {}
+ assert.isFalse @handler2Called
+ assert.isTrue @handler1Called
+
+ should "remove handlers correctly", ->
+ handlerId = @handlerStack.push { keydown: => @handler1Called = true }
+ @handlerStack.push { keydown: => @handler2Called = true }
+ @handlerStack.remove handlerId
+ @handlerStack.bubbleEvent 'keydown', {}
+ assert.isTrue @handler2Called
+ assert.isFalse @handler1Called
+
+ should "handle self-removing handlers correctly", ->
+ ctx = @
+ @handlerStack.push { keydown: => @handler1Called = true }
+ @handlerStack.push { keydown: ->
+ ctx.handler2Called = true
+ @remove()
+ }
+ @handlerStack.bubbleEvent 'keydown', {}
+ assert.isTrue @handler2Called
+ assert.isTrue @handler1Called
+ assert.equal @handlerStack.stack.length, 1