| 1 |
// Copyright 2009 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.media.FlashObjectTest'); |
| 16 |
goog.setTestOnly('goog.ui.media.FlashObjectTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.DomHelper'); |
| 20 |
goog.require('goog.dom.TagName'); |
| 21 |
goog.require('goog.events'); |
| 22 |
goog.require('goog.events.Event'); |
| 23 |
goog.require('goog.events.EventType'); |
| 24 |
goog.require('goog.html.testing'); |
| 25 |
goog.require('goog.testing.MockControl'); |
| 26 |
goog.require('goog.testing.events'); |
| 27 |
goog.require('goog.testing.jsunit'); |
| 28 |
goog.require('goog.ui.media.FlashObject'); |
| 29 |
goog.require('goog.userAgent'); |
| 30 |
|
| 31 |
// Delay running the tests after page load. This test has some asynchronous |
| 32 |
// behavior that interacts with page load detection. |
| 33 |
goog.testing.jsunit.AUTO_RUN_DELAY_IN_MS = 500; |
| 34 |
|
| 35 |
var FLASH_URL = goog.html.testing.newTrustedResourceUrlForTest( |
| 36 |
'http://www.youtube.com/v/RbI7cCp0v6w&hl=en&fs=1'); |
| 37 |
var control = new goog.testing.MockControl(); |
| 38 |
var domHelper = control.createLooseMock(goog.dom.DomHelper); |
| 39 |
// TODO(user): mocking window.document throws exceptions in FF2. find out how |
| 40 |
// to mock it. |
| 41 |
var documentHelper = {body: control.createLooseMock(goog.dom.DomHelper)}; |
| 42 |
var element = goog.dom.createElement(goog.dom.TagName.DIV); |
| 43 |
|
| 44 |
function setUp() { |
| 45 |
control.$resetAll(); |
| 46 |
domHelper.getDocument().$returns(documentHelper).$anyTimes(); |
| 47 |
domHelper.createElement(goog.dom.TagName.DIV).$returns(element).$anyTimes(); |
| 48 |
documentHelper.body.appendChild(element).$anyTimes(); |
| 49 |
} |
| 50 |
|
| 51 |
function tearDown() { |
| 52 |
control.$verifyAll(); |
| 53 |
} |
| 54 |
|
| 55 |
function getFlashVarsFromElement(flash) { |
| 56 |
var el = flash.getFlashElement(); |
| 57 |
|
| 58 |
// This should work in everything except IE: |
| 59 |
if (el.hasAttribute && el.hasAttribute('flashvars')) |
| 60 |
return el.getAttribute('flashvars'); |
| 61 |
|
| 62 |
// For IE: find and return the value of the correct param element: |
| 63 |
el = el.firstChild; |
| 64 |
while (el) { |
| 65 |
if (el.name == 'FlashVars') { |
| 66 |
return el.value; |
| 67 |
} |
| 68 |
el = el.nextSibling; |
| 69 |
} |
| 70 |
return ''; |
| 71 |
} |
| 72 |
|
| 73 |
function testInstantiationAndRendering() { |
| 74 |
control.$replayAll(); |
| 75 |
|
| 76 |
var flash = new goog.ui.media.FlashObject(FLASH_URL, domHelper); |
| 77 |
flash.render(); |
| 78 |
flash.dispose(); |
| 79 |
} |
| 80 |
|
| 81 |
function testRenderedWithCorrectAttributes() { |
| 82 |
if (goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(11)) { |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
control.$replayAll(); |
| 87 |
|
| 88 |
var flash = new goog.ui.media.FlashObject(FLASH_URL, domHelper); |
| 89 |
flash.setAllowScriptAccess('allowScriptAccess'); |
| 90 |
flash.setBackgroundColor('backgroundColor'); |
| 91 |
flash.setId('id'); |
| 92 |
flash.setFlashVars({'k1': 'v1', 'k2': 'v2'}); |
| 93 |
flash.setWmode('wmode'); |
| 94 |
flash.render(); |
| 95 |
|
| 96 |
var el = flash.getFlashElement(); |
| 97 |
assertEquals('true', el.getAttribute('allowFullScreen')); |
| 98 |
assertEquals('all', el.getAttribute('allowNetworking')); |
| 99 |
assertEquals('allowScriptAccess', el.getAttribute('allowScriptAccess')); |
| 100 |
assertEquals( |
| 101 |
goog.ui.media.FlashObject.FLASH_CSS_CLASS, el.getAttribute('class')); |
| 102 |
assertEquals('k1=v1&k2=v2', el.getAttribute('FlashVars')); |
| 103 |
assertEquals('id', el.getAttribute('id')); |
| 104 |
assertEquals('id', el.getAttribute('name')); |
| 105 |
assertEquals( |
| 106 |
'https://www.macromedia.com/go/getflashplayer', |
| 107 |
el.getAttribute('pluginspage')); |
| 108 |
assertEquals('high', el.getAttribute('quality')); |
| 109 |
assertEquals('false', el.getAttribute('SeamlessTabbing')); |
| 110 |
assertEquals(FLASH_URL.getTypedStringValue(), el.getAttribute('src')); |
| 111 |
assertEquals('application/x-shockwave-flash', el.getAttribute('type')); |
| 112 |
assertEquals('wmode', el.getAttribute('wmode')); |
| 113 |
} |
| 114 |
|
| 115 |
function testRenderedWithCorrectAttributesOldIe() { |
| 116 |
if (!goog.userAgent.IE || goog.userAgent.isDocumentModeOrHigher(11)) { |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
control.$replayAll(); |
| 121 |
|
| 122 |
var flash = new goog.ui.media.FlashObject(FLASH_URL, domHelper); |
| 123 |
flash.setAllowScriptAccess('allowScriptAccess'); |
| 124 |
flash.setBackgroundColor('backgroundColor'); |
| 125 |
flash.setId('id'); |
| 126 |
flash.setFlashVars({'k1': 'v1', 'k2': 'v2'}); |
| 127 |
flash.setWmode('wmode'); |
| 128 |
flash.render(); |
| 129 |
|
| 130 |
var el = flash.getFlashElement(); |
| 131 |
assertEquals( |
| 132 |
'class', goog.ui.media.FlashObject.FLASH_CSS_CLASS, |
| 133 |
el.getAttribute('class')); |
| 134 |
assertEquals( |
| 135 |
'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000', el.getAttribute('classid')); |
| 136 |
assertEquals('id', 'id', el.getAttribute('id')); |
| 137 |
assertEquals('name', 'id', el.getAttribute('name')); |
| 138 |
|
| 139 |
assertContainsParam(el, 'allowFullScreen', 'true'); |
| 140 |
assertContainsParam(el, 'allowNetworking', 'all'); |
| 141 |
assertContainsParam(el, 'AllowScriptAccess', 'allowScriptAccess'); |
| 142 |
assertContainsParam(el, 'bgcolor', 'backgroundColor'); |
| 143 |
assertContainsParam(el, 'FlashVars', 'FlashVars'); |
| 144 |
assertContainsParam(el, 'movie', FLASH_URL); |
| 145 |
assertContainsParam(el, 'quality', 'high'); |
| 146 |
assertContainsParam(el, 'SeamlessTabbing', 'false'); |
| 147 |
assertContainsParam(el, 'wmode', 'wmode'); |
| 148 |
} |
| 149 |
|
| 150 |
function assertContainsParam(element, expectedName, expectedValue) { |
| 151 |
var failureMsg = 'Expected param with name \"' + expectedName + |
| 152 |
'\" and value \"' + expectedValue + '\". Not found in child nodes: ' + |
| 153 |
element.innerHTML; |
| 154 |
for (var i = 0; i < element.childNodes.length; i++) { |
| 155 |
var child = element.childNodes[i]; |
| 156 |
var name = child.getAttribute('name'); |
| 157 |
if (name === expectedName) { |
| 158 |
if (!child.getAttribute('value') === expectedValue) { |
| 159 |
fail(failureMsg); |
| 160 |
} |
| 161 |
return; |
| 162 |
} |
| 163 |
} |
| 164 |
fail(failureMsg); |
| 165 |
} |
| 166 |
|
| 167 |
function testSetFlashVar() { |
| 168 |
control.$replayAll(); |
| 169 |
|
| 170 |
var flash = new goog.ui.media.FlashObject(FLASH_URL, domHelper); |
| 171 |
|
| 172 |
assertTrue(flash.getFlashVars().isEmpty()); |
| 173 |
flash.setFlashVar('foo', 'bar'); |
| 174 |
flash.setFlashVar('hello', 'world'); |
| 175 |
assertFalse(flash.getFlashVars().isEmpty()); |
| 176 |
|
| 177 |
flash.render(); |
| 178 |
|
| 179 |
assertEquals('foo=bar&hello=world', getFlashVarsFromElement(flash)); |
| 180 |
flash.dispose(); |
| 181 |
} |
| 182 |
|
| 183 |
function testAddFlashVars() { |
| 184 |
control.$replayAll(); |
| 185 |
|
| 186 |
var flash = new goog.ui.media.FlashObject(FLASH_URL, domHelper); |
| 187 |
|
| 188 |
assertTrue(flash.getFlashVars().isEmpty()); |
| 189 |
flash.addFlashVars({'using': 'an', 'object': 'literal'}); |
| 190 |
assertFalse(flash.getFlashVars().isEmpty()); |
| 191 |
|
| 192 |
flash.render(); |
| 193 |
|
| 194 |
assertEquals('using=an&object=literal', getFlashVarsFromElement(flash)); |
| 195 |
flash.dispose(); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
/** |
| 200 |
* @deprecated Remove once setFlashVars is removed. |
| 201 |
*/ |
| 202 |
function testSetFlashVarsUsingFalseAsTheValue() { |
| 203 |
control.$replayAll(); |
| 204 |
|
| 205 |
var flash = new goog.ui.media.FlashObject(FLASH_URL, domHelper); |
| 206 |
|
| 207 |
assertTrue(flash.getFlashVars().isEmpty()); |
| 208 |
flash.setFlashVars('beEvil', false); |
| 209 |
assertFalse(flash.getFlashVars().isEmpty()); |
| 210 |
|
| 211 |
flash.render(); |
| 212 |
|
| 213 |
assertEquals('beEvil=false', getFlashVarsFromElement(flash)); |
| 214 |
flash.dispose(); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
/** |
| 219 |
* @deprecated Remove once setFlashVars is removed. |
| 220 |
*/ |
| 221 |
function testSetFlashVarsWithWrongArgument() { |
| 222 |
control.$replayAll(); |
| 223 |
|
| 224 |
assertThrows(function() { |
| 225 |
var flash = new goog.ui.media.FlashObject(FLASH_URL, domHelper); |
| 226 |
flash.setFlashVars('foo=bar'); |
| 227 |
flash.dispose(); |
| 228 |
}); |
| 229 |
} |
| 230 |
|
| 231 |
function testSetFlashVarUrlEncoding() { |
| 232 |
control.$replayAll(); |
| 233 |
|
| 234 |
var flash = new goog.ui.media.FlashObject(FLASH_URL, domHelper); |
| 235 |
flash.setFlashVar('foo', 'bar and some extra spaces'); |
| 236 |
flash.render(); |
| 237 |
assertEquals( |
| 238 |
'foo=bar%20and%20some%20extra%20spaces', getFlashVarsFromElement(flash)); |
| 239 |
flash.dispose(); |
| 240 |
} |
| 241 |
|
| 242 |
function testThrowsRequiredVersionOfFlashNotAvailable() { |
| 243 |
control.$replayAll(); |
| 244 |
|
| 245 |
var flash = new goog.ui.media.FlashObject(FLASH_URL, domHelper); |
| 246 |
flash.setRequiredVersion('999.999.999'); |
| 247 |
|
| 248 |
assertTrue(flash.hasRequiredVersion()); |
| 249 |
|
| 250 |
assertThrows(function() { flash.render(); }); |
| 251 |
|
| 252 |
flash.dispose(); |
| 253 |
} |
| 254 |
|
| 255 |
function testIsLoadedForIE() { |
| 256 |
control.$replayAll(); |
| 257 |
|
| 258 |
var flash = new goog.ui.media.FlashObject(FLASH_URL, domHelper); |
| 259 |
flash.render(); |
| 260 |
assertNotThrows('isLoaded() should not throw exception', function() { |
| 261 |
flash.isLoaded(); |
| 262 |
}); |
| 263 |
flash.dispose(); |
| 264 |
} |
| 265 |
|
| 266 |
function testIsLoadedAfterDispose() { |
| 267 |
control.$replayAll(); |
| 268 |
|
| 269 |
var flash = new goog.ui.media.FlashObject(FLASH_URL, domHelper); |
| 270 |
flash.render(); |
| 271 |
// TODO(user): find out a way to test the loadness of flash movies on |
| 272 |
// asynchronous tests. if debugger; is left here, the test pass. if removed |
| 273 |
// the test fails. that happens because flash needs some time to be |
| 274 |
// considered loaded, after flash.render() is called (like img.src i guess). |
| 275 |
// debugger; |
| 276 |
// assertTrue(flash.isLoaded()); |
| 277 |
flash.dispose(); |
| 278 |
assertFalse(flash.isLoaded()); |
| 279 |
} |
| 280 |
|
| 281 |
function testPropagatesEventsConsistently() { |
| 282 |
var event = control.createLooseMock(goog.events.Event); |
| 283 |
|
| 284 |
// we expect any event to have its propagation stopped. |
| 285 |
event.stopPropagation(); |
| 286 |
|
| 287 |
control.$replayAll(); |
| 288 |
|
| 289 |
var flash = new goog.ui.media.FlashObject(FLASH_URL, domHelper); |
| 290 |
flash.render(); |
| 291 |
event.target = flash.getElement(); |
| 292 |
event.type = goog.events.EventType.CLICK; |
| 293 |
goog.testing.events.fireBrowserEvent(event); |
| 294 |
flash.dispose(); |
| 295 |
} |
| 296 |
|
| 297 |
function testEventsGetsSinked() { |
| 298 |
var called = false; |
| 299 |
var flash = new goog.ui.media.FlashObject(FLASH_URL); |
| 300 |
var parent = goog.dom.createElement(goog.dom.TagName.DIV); |
| 301 |
flash.render(parent); |
| 302 |
|
| 303 |
goog.events.listen( |
| 304 |
parent, goog.events.EventType.CLICK, function(e) { called = true; }); |
| 305 |
|
| 306 |
assertFalse(called); |
| 307 |
|
| 308 |
goog.testing.events.fireClickSequence(flash.getElement()); |
| 309 |
|
| 310 |
assertFalse(called); |
| 311 |
flash.dispose(); |
| 312 |
} |