pagevisibilitymonitor_test.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2013 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.labs.dom.PageVisibilityMonitorTest');
  15. goog.setTestOnly('goog.labs.dom.PageVisibilityMonitorTest');
  16. goog.require('goog.events');
  17. goog.require('goog.functions');
  18. goog.require('goog.labs.dom.PageVisibilityMonitor');
  19. goog.require('goog.testing.PropertyReplacer');
  20. goog.require('goog.testing.events');
  21. goog.require('goog.testing.events.Event');
  22. goog.require('goog.testing.jsunit');
  23. goog.require('goog.testing.recordFunction');
  24. var stubs = new goog.testing.PropertyReplacer();
  25. var vh;
  26. function tearDown() {
  27. goog.dispose(vh);
  28. vh = null;
  29. stubs.reset();
  30. }
  31. function testConstructor() {
  32. vh = new goog.labs.dom.PageVisibilityMonitor();
  33. }
  34. function testNoVisibilitySupport() {
  35. stubs.set(
  36. goog.labs.dom.PageVisibilityMonitor.prototype, 'getBrowserEventType_',
  37. goog.functions.NULL);
  38. var listener = goog.testing.recordFunction();
  39. vh = new goog.labs.dom.PageVisibilityMonitor();
  40. goog.events.listen(vh, 'visibilitychange', listener);
  41. var e = new goog.testing.events.Event('visibilitychange');
  42. e.target = window.document;
  43. goog.testing.events.fireBrowserEvent(e);
  44. assertEquals(0, listener.getCallCount());
  45. }
  46. function testListener() {
  47. stubs.set(
  48. goog.labs.dom.PageVisibilityMonitor.prototype, 'getBrowserEventType_',
  49. goog.functions.constant('visibilitychange'));
  50. var listener = goog.testing.recordFunction();
  51. vh = new goog.labs.dom.PageVisibilityMonitor();
  52. goog.events.listen(vh, 'visibilitychange', listener);
  53. var e = new goog.testing.events.Event('visibilitychange');
  54. e.target = window.document;
  55. goog.testing.events.fireBrowserEvent(e);
  56. assertEquals(1, listener.getCallCount());
  57. }
  58. function testListenerForWebKit() {
  59. stubs.set(
  60. goog.labs.dom.PageVisibilityMonitor.prototype, 'getBrowserEventType_',
  61. goog.functions.constant('webkitvisibilitychange'));
  62. var listener = goog.testing.recordFunction();
  63. vh = new goog.labs.dom.PageVisibilityMonitor();
  64. goog.events.listen(vh, 'visibilitychange', listener);
  65. var e = new goog.testing.events.Event('webkitvisibilitychange');
  66. e.target = window.document;
  67. goog.testing.events.fireBrowserEvent(e);
  68. assertEquals(1, listener.getCallCount());
  69. }