| 1 |
// Copyright 2008 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.events.ActionHandlerTest'); |
| 16 |
goog.setTestOnly('goog.events.ActionHandlerTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.events'); |
| 20 |
goog.require('goog.events.ActionHandler'); |
| 21 |
goog.require('goog.testing.events'); |
| 22 |
goog.require('goog.testing.jsunit'); |
| 23 |
|
| 24 |
var actionHandler; |
| 25 |
function setUp() { |
| 26 |
actionHandler = |
| 27 |
new goog.events.ActionHandler(goog.dom.getElement('actionDiv')); |
| 28 |
} |
| 29 |
function tearDown() { |
| 30 |
actionHandler.dispose(); |
| 31 |
} |
| 32 |
|
| 33 |
// Tests to see that both the BEFOREACTION and ACTION events are fired |
| 34 |
function testActionHandlerWithBeforeActionHandler() { |
| 35 |
var actionEventFired = false; |
| 36 |
var beforeActionFired = false; |
| 37 |
goog.events.listen( |
| 38 |
actionHandler, goog.events.ActionHandler.EventType.ACTION, |
| 39 |
function(e) { actionEventFired = true; }); |
| 40 |
goog.events.listen( |
| 41 |
actionHandler, goog.events.ActionHandler.EventType.BEFOREACTION, |
| 42 |
function(e) { beforeActionFired = true; }); |
| 43 |
goog.testing.events.fireClickSequence(goog.dom.getElement('actionDiv')); |
| 44 |
assertTrue('BEFOREACTION event was not fired', beforeActionFired); |
| 45 |
assertTrue('ACTION event was not fired', actionEventFired); |
| 46 |
} |
| 47 |
|
| 48 |
// Tests to see that the ACTION event is fired, even if there is no |
| 49 |
// BEFOREACTION handler. |
| 50 |
function testActionHandlerWithoutBeforeActionHandler() { |
| 51 |
var actionEventFired = false; |
| 52 |
goog.events.listen( |
| 53 |
actionHandler, goog.events.ActionHandler.EventType.ACTION, |
| 54 |
function(e) { actionEventFired = true; }); |
| 55 |
goog.testing.events.fireClickSequence(goog.dom.getElement('actionDiv')); |
| 56 |
assertTrue('ACTION event was not fired', actionEventFired); |
| 57 |
} |
| 58 |
|
| 59 |
// If the BEFOREACTION listener swallows the event, it should cancel the |
| 60 |
// ACTION event. |
| 61 |
function testBeforeActionCancel() { |
| 62 |
var actionEventFired = false; |
| 63 |
var beforeActionFired = false; |
| 64 |
goog.events.listen( |
| 65 |
actionHandler, goog.events.ActionHandler.EventType.ACTION, |
| 66 |
function(e) { actionEvent = e; }); |
| 67 |
goog.events.listen( |
| 68 |
actionHandler, goog.events.ActionHandler.EventType.BEFOREACTION, |
| 69 |
function(e) { |
| 70 |
beforeActionFired = true; |
| 71 |
e.preventDefault(); |
| 72 |
}); |
| 73 |
goog.testing.events.fireClickSequence(goog.dom.getElement('actionDiv')); |
| 74 |
assertTrue(beforeActionFired); |
| 75 |
assertFalse(actionEventFired); |
| 76 |
} |