actionhandler_test.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. goog.provide('goog.events.ActionHandlerTest');
  15. goog.setTestOnly('goog.events.ActionHandlerTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.events');
  18. goog.require('goog.events.ActionHandler');
  19. goog.require('goog.testing.events');
  20. goog.require('goog.testing.jsunit');
  21. var actionHandler;
  22. function setUp() {
  23. actionHandler =
  24. new goog.events.ActionHandler(goog.dom.getElement('actionDiv'));
  25. }
  26. function tearDown() {
  27. actionHandler.dispose();
  28. }
  29. // Tests to see that both the BEFOREACTION and ACTION events are fired
  30. function testActionHandlerWithBeforeActionHandler() {
  31. var actionEventFired = false;
  32. var beforeActionFired = false;
  33. goog.events.listen(
  34. actionHandler, goog.events.ActionHandler.EventType.ACTION,
  35. function(e) { actionEventFired = true; });
  36. goog.events.listen(
  37. actionHandler, goog.events.ActionHandler.EventType.BEFOREACTION,
  38. function(e) { beforeActionFired = true; });
  39. goog.testing.events.fireClickSequence(goog.dom.getElement('actionDiv'));
  40. assertTrue('BEFOREACTION event was not fired', beforeActionFired);
  41. assertTrue('ACTION event was not fired', actionEventFired);
  42. }
  43. // Tests to see that the ACTION event is fired, even if there is no
  44. // BEFOREACTION handler.
  45. function testActionHandlerWithoutBeforeActionHandler() {
  46. var actionEventFired = false;
  47. goog.events.listen(
  48. actionHandler, goog.events.ActionHandler.EventType.ACTION,
  49. function(e) { actionEventFired = true; });
  50. goog.testing.events.fireClickSequence(goog.dom.getElement('actionDiv'));
  51. assertTrue('ACTION event was not fired', actionEventFired);
  52. }
  53. // If the BEFOREACTION listener swallows the event, it should cancel the
  54. // ACTION event.
  55. function testBeforeActionCancel() {
  56. var actionEventFired = false;
  57. var beforeActionFired = false;
  58. goog.events.listen(
  59. actionHandler, goog.events.ActionHandler.EventType.ACTION,
  60. function(e) { actionEvent = e; });
  61. goog.events.listen(
  62. actionHandler, goog.events.ActionHandler.EventType.BEFOREACTION,
  63. function(e) {
  64. beforeActionFired = true;
  65. e.preventDefault();
  66. });
  67. goog.testing.events.fireClickSequence(goog.dom.getElement('actionDiv'));
  68. assertTrue(beforeActionFired);
  69. assertFalse(actionEventFired);
  70. }