| 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.ui.IframeMaskTest'); |
| 16 |
goog.setTestOnly('goog.ui.IframeMaskTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.TagName'); |
| 20 |
goog.require('goog.dom.iframe'); |
| 21 |
goog.require('goog.structs.Pool'); |
| 22 |
goog.require('goog.style'); |
| 23 |
goog.require('goog.testing.MockClock'); |
| 24 |
goog.require('goog.testing.StrictMock'); |
| 25 |
goog.require('goog.testing.jsunit'); |
| 26 |
goog.require('goog.ui.IframeMask'); |
| 27 |
goog.require('goog.ui.Popup'); |
| 28 |
goog.require('goog.ui.PopupBase'); |
| 29 |
goog.require('goog.userAgent'); |
| 30 |
|
| 31 |
var iframeMask; |
| 32 |
var mockClock; |
| 33 |
|
| 34 |
function setUp() { |
| 35 |
goog.dom.getElement('sandbox').innerHTML = '<div id="popup"></div>'; |
| 36 |
mockClock = new goog.testing.MockClock(true); |
| 37 |
|
| 38 |
iframeMask = new goog.ui.IframeMask(); |
| 39 |
} |
| 40 |
|
| 41 |
function tearDown() { |
| 42 |
iframeMask.dispose(); |
| 43 |
mockClock.dispose(); |
| 44 |
|
| 45 |
assertNoIframes(); |
| 46 |
} |
| 47 |
|
| 48 |
function findOneAndOnlyIframe() { |
| 49 |
var iframes = goog.dom.getElementsByTagName(goog.dom.TagName.IFRAME); |
| 50 |
assertEquals( |
| 51 |
'There should be exactly 1 iframe in the document', 1, iframes.length); |
| 52 |
return iframes[0]; |
| 53 |
} |
| 54 |
|
| 55 |
function assertNoIframes() { |
| 56 |
assertEquals( |
| 57 |
'Expected no iframes in the document', 0, |
| 58 |
goog.dom.getElementsByTagNameAndClass(goog.dom.TagName.IFRAME).length); |
| 59 |
} |
| 60 |
|
| 61 |
function testApplyFullScreenMask() { |
| 62 |
iframeMask.applyMask(); |
| 63 |
|
| 64 |
var iframe = findOneAndOnlyIframe(); |
| 65 |
assertEquals('block', iframe.style.display); |
| 66 |
assertEquals('absolute', iframe.style.position); |
| 67 |
|
| 68 |
// coerce zindex to a string |
| 69 |
assertEquals('1', iframe.style.zIndex + ''); |
| 70 |
|
| 71 |
iframeMask.hideMask(); |
| 72 |
assertEquals('none', iframe.style.display); |
| 73 |
} |
| 74 |
|
| 75 |
function testApplyOpacity() { |
| 76 |
iframeMask.setOpacity(0.3); |
| 77 |
iframeMask.applyMask(); |
| 78 |
|
| 79 |
if (goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(9)) { |
| 80 |
assertContains( |
| 81 |
'Expected opactity to be set in the CSS style', '30', |
| 82 |
findOneAndOnlyIframe().style.cssText); |
| 83 |
} else { |
| 84 |
assertContains( |
| 85 |
'Expected opactity to be set in the CSS style', '0.3', |
| 86 |
findOneAndOnlyIframe().style.cssText); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
function testApplyZIndex() { |
| 91 |
iframeMask.setZIndex(5); |
| 92 |
iframeMask.applyMask(); |
| 93 |
|
| 94 |
// coerce zindex to a string |
| 95 |
assertEquals('5', findOneAndOnlyIframe().style.zIndex + ''); |
| 96 |
} |
| 97 |
|
| 98 |
function testSnapElement() { |
| 99 |
iframeMask.setSnapElement(goog.dom.getElement('popup')); |
| 100 |
iframeMask.applyMask(); |
| 101 |
|
| 102 |
var iframe = findOneAndOnlyIframe(); |
| 103 |
var bounds = goog.style.getBounds(iframe); |
| 104 |
assertEquals(100, bounds.left); |
| 105 |
assertEquals(900, bounds.top); |
| 106 |
assertEquals(300, bounds.width); |
| 107 |
assertEquals(400, bounds.height); |
| 108 |
|
| 109 |
iframeMask.setSnapElement(document.documentElement); |
| 110 |
|
| 111 |
// Make sure that snapping to a different element changes the bounds. |
| 112 |
assertNotEquals( |
| 113 |
'Snap element not updated', 400, goog.style.getBounds(iframe).height); |
| 114 |
} |
| 115 |
|
| 116 |
function testAttachToPopup() { |
| 117 |
var popup = new goog.ui.Popup(goog.dom.getElement('popup')); |
| 118 |
iframeMask.listenOnTarget( |
| 119 |
popup, goog.ui.PopupBase.EventType.SHOW, goog.ui.PopupBase.EventType.HIDE, |
| 120 |
goog.dom.getElement('popup')); |
| 121 |
|
| 122 |
assertNoIframes(); |
| 123 |
popup.setVisible(true); |
| 124 |
assertNoIframes(); |
| 125 |
|
| 126 |
// Tick because the showing of the iframe mask happens asynchronously. |
| 127 |
// (Otherwise the handling of the mousedown can take so long that a bounce |
| 128 |
// occurs). |
| 129 |
mockClock.tick(1); |
| 130 |
|
| 131 |
var iframe = findOneAndOnlyIframe(); |
| 132 |
var bounds = goog.style.getBounds(iframe); |
| 133 |
assertEquals(300, bounds.width); |
| 134 |
assertEquals(400, bounds.height); |
| 135 |
assertEquals('block', iframe.style.display); |
| 136 |
|
| 137 |
popup.setVisible(false); |
| 138 |
assertEquals('none', iframe.style.display); |
| 139 |
} |
| 140 |
|
| 141 |
function testQuickHidingPopup() { |
| 142 |
var popup = new goog.ui.Popup(goog.dom.getElement('popup')); |
| 143 |
iframeMask.listenOnTarget( |
| 144 |
popup, goog.ui.PopupBase.EventType.SHOW, |
| 145 |
goog.ui.PopupBase.EventType.HIDE); |
| 146 |
|
| 147 |
assertNoIframes(); |
| 148 |
popup.setVisible(true); |
| 149 |
assertNoIframes(); |
| 150 |
popup.setVisible(false); |
| 151 |
assertNoIframes(); |
| 152 |
|
| 153 |
// Tick because the showing of the iframe mask happens asynchronously. |
| 154 |
// (Otherwise the handling of the mousedown can take so long that a bounce |
| 155 |
// occurs). |
| 156 |
mockClock.tick(1); |
| 157 |
assertNoIframes(); |
| 158 |
} |
| 159 |
|
| 160 |
function testRemoveHandlers() { |
| 161 |
var popup = new goog.ui.Popup(goog.dom.getElement('popup')); |
| 162 |
iframeMask.listenOnTarget( |
| 163 |
popup, goog.ui.PopupBase.EventType.SHOW, |
| 164 |
goog.ui.PopupBase.EventType.HIDE); |
| 165 |
iframeMask.removeHandlers(); |
| 166 |
popup.setVisible(true); |
| 167 |
|
| 168 |
// Tick because the showing of the iframe mask happens asynchronously. |
| 169 |
// (Otherwise the handling of the mousedown can take so long that a bounce |
| 170 |
// occurs). |
| 171 |
mockClock.tick(1); |
| 172 |
assertNoIframes(); |
| 173 |
} |
| 174 |
|
| 175 |
function testIframePool() { |
| 176 |
var iframe = goog.dom.iframe.createBlank(goog.dom.getDomHelper()); |
| 177 |
var mockPool = new goog.testing.StrictMock(goog.structs.Pool); |
| 178 |
mockPool.getObject(); |
| 179 |
mockPool.$returns(iframe); |
| 180 |
|
| 181 |
mockPool.$replay(); |
| 182 |
|
| 183 |
iframeMask.dispose(); |
| 184 |
|
| 185 |
// Create a new iframe mask with a pool, and verify that it checks |
| 186 |
// its iframe out of the pool instead of creating one. |
| 187 |
iframeMask = new goog.ui.IframeMask(null, mockPool); |
| 188 |
iframeMask.applyMask(); |
| 189 |
mockPool.$verify(); |
| 190 |
findOneAndOnlyIframe(); |
| 191 |
|
| 192 |
mockPool.$reset(); |
| 193 |
|
| 194 |
mockPool.releaseObject(iframe); |
| 195 |
mockPool.$replay(); |
| 196 |
|
| 197 |
// When the iframe mask has a pool, the pool is responsible for |
| 198 |
// removing the iframe from the DOM. |
| 199 |
iframeMask.hideMask(); |
| 200 |
mockPool.$verify(); |
| 201 |
findOneAndOnlyIframe(); |
| 202 |
|
| 203 |
// And showing the iframe again should check it out of the pool again. |
| 204 |
mockPool.$reset(); |
| 205 |
mockPool.getObject(); |
| 206 |
mockPool.$returns(iframe); |
| 207 |
mockPool.$replay(); |
| 208 |
|
| 209 |
iframeMask.applyMask(); |
| 210 |
mockPool.$verify(); |
| 211 |
|
| 212 |
// When the test is over, the iframe mask should be disposed. Make sure |
| 213 |
// that the pool removes the iframe from the page. |
| 214 |
mockPool.$reset(); |
| 215 |
mockPool.releaseObject(iframe); |
| 216 |
mockPool.$does(function() { goog.dom.removeNode(iframe); }); |
| 217 |
mockPool.$replay(); |
| 218 |
} |