From ea73be17468b1bd02171c82e1fb21b1bc16ccd0e Mon Sep 17 00:00:00 2001
From: Jez Ng
Date: Sat, 20 Oct 2012 23:17:30 -0400
Subject: 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.
---
 tests/dom_tests/dom_tests.coffee           |  4 +--
 tests/dom_tests/dom_tests.html             |  1 +
 tests/unit_tests/handler_stack_test.coffee | 52 ++++++++++++++++++++++++++++++
 3 files changed, 55 insertions(+), 2 deletions(-)
 create mode 100644 tests/unit_tests/handler_stack_test.coffee
(limited to 'tests')
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 @@
     
     
     
+    
     
     
     
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
-- 
cgit v1.2.3