flashobject_test.js 9.3 KB

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