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