eventtarget_via_googevents_test.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2012 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.EventTargetGoogEventsTest');
  15. goog.setTestOnly('goog.events.EventTargetGoogEventsTest');
  16. goog.require('goog.events');
  17. goog.require('goog.events.EventTarget');
  18. goog.require('goog.events.eventTargetTester');
  19. goog.require('goog.events.eventTargetTester.KeyType');
  20. goog.require('goog.events.eventTargetTester.UnlistenReturnType');
  21. goog.require('goog.testing');
  22. goog.require('goog.testing.jsunit');
  23. function setUp() {
  24. var newListenableFn = function() { return new goog.events.EventTarget(); };
  25. var unlistenByKeyFn = function(src, key) {
  26. return goog.events.unlistenByKey(key);
  27. };
  28. goog.events.eventTargetTester.setUp(
  29. newListenableFn, goog.events.listen, goog.events.unlisten,
  30. unlistenByKeyFn, goog.events.listenOnce, goog.events.dispatchEvent,
  31. goog.events.removeAll, goog.events.getListeners, goog.events.getListener,
  32. goog.events.hasListener, goog.events.eventTargetTester.KeyType.NUMBER,
  33. goog.events.eventTargetTester.UnlistenReturnType.BOOLEAN, true);
  34. }
  35. function tearDown() {
  36. goog.events.eventTargetTester.tearDown();
  37. }
  38. function testUnlistenProperCleanup() {
  39. goog.events.listen(eventTargets[0], EventType.A, listeners[0]);
  40. goog.events.unlisten(eventTargets[0], EventType.A, listeners[0]);
  41. goog.events.listen(eventTargets[0], EventType.A, listeners[0]);
  42. eventTargets[0].unlisten(EventType.A, listeners[0]);
  43. }
  44. function testUnlistenByKeyProperCleanup() {
  45. var keyNum = goog.events.listen(eventTargets[0], EventType.A, listeners[0]);
  46. goog.events.unlistenByKey(keyNum);
  47. }
  48. function testListenOnceProperCleanup() {
  49. goog.events.listenOnce(eventTargets[0], EventType.A, listeners[0]);
  50. eventTargets[0].dispatchEvent(EventType.A);
  51. }
  52. function testListenWithObject() {
  53. var obj = {};
  54. obj.handleEvent = goog.testing.recordFunction();
  55. goog.events.listen(eventTargets[0], EventType.A, obj);
  56. eventTargets[0].dispatchEvent(EventType.A);
  57. assertEquals(1, obj.handleEvent.getCallCount());
  58. }
  59. function testListenWithObjectHandleEventReturningFalse() {
  60. var obj = {};
  61. obj.handleEvent = function() { return false; };
  62. goog.events.listen(eventTargets[0], EventType.A, obj);
  63. assertFalse(eventTargets[0].dispatchEvent(EventType.A));
  64. }