youtube_test.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.YoutubeTest');
  15. goog.setTestOnly('goog.ui.media.YoutubeTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.TagName');
  18. goog.require('goog.testing.jsunit');
  19. goog.require('goog.ui.media.FlashObject');
  20. goog.require('goog.ui.media.Youtube');
  21. goog.require('goog.ui.media.YoutubeModel');
  22. var youtube;
  23. var control;
  24. var YOUTUBE_VIDEO_ID = 'dMH0bHeiRNg';
  25. var YOUTUBE_URL = 'http://www.youtube.com/watch?v=' + YOUTUBE_VIDEO_ID;
  26. var parent = goog.dom.createElement(goog.dom.TagName.DIV);
  27. function setUp() {
  28. var model =
  29. new goog.ui.media.YoutubeModel(YOUTUBE_VIDEO_ID, 'evolution of dance');
  30. control = goog.ui.media.Youtube.newControl(model);
  31. }
  32. function tearDown() {
  33. control.dispose();
  34. }
  35. function testBasicRendering() {
  36. control.render(parent);
  37. var el = goog.dom.getElementsByTagNameAndClass(
  38. goog.dom.TagName.DIV, goog.ui.media.Youtube.CSS_CLASS, parent);
  39. assertEquals(1, el.length);
  40. assertEquals(YOUTUBE_URL, control.getDataModel().getUrl());
  41. }
  42. function testParsingUrl() {
  43. // a simple link
  44. assertExtractsCorrectly(
  45. 'uddeBVmKTqE', 'http://www.youtube.com/watch?v=uddeBVmKTqE');
  46. // a simple mobile link
  47. assertExtractsCorrectly(
  48. 'uddeBVmKTqE', 'http://m.youtube.com/watch?v=uddeBVmKTqE');
  49. // a secure mobile link
  50. assertExtractsCorrectly(
  51. 'uddeBVmKTqE', 'https://m.youtube.com/watch?v=uddeBVmKTqE');
  52. // a simple short link
  53. assertExtractsCorrectly('uddeBVmKTqE', 'http://youtu.be/uddeBVmKTqE');
  54. // a secure short link
  55. assertExtractsCorrectly('uddeBVmKTqE', 'https://youtu.be/uddeBVmKTqE');
  56. // a secure short link with a CGI parameter
  57. assertExtractsCorrectly(
  58. 'uddeBVmKTqE', 'https://youtu.be/uddeBVmKTqE?feature=channel');
  59. // a channel link
  60. assertExtractsCorrectly(
  61. '4Pb9e1uu3EQ',
  62. 'http://www.youtube.com/watch?v=4Pb9e1uu3EQ&feature=channel');
  63. // a UK link
  64. assertExtractsCorrectly(
  65. 'xqWXO87TlH4',
  66. 'http://uk.youtube.com/watch?gl=GB&hl=en-GB&v=xqWXO87TlH4');
  67. // an India link
  68. assertExtractsCorrectly(
  69. '10FKWOn4qGA',
  70. 'http://www.youtube.com/watch?gl=IN&hl=en-GB&v=10FKWOn4qGA');
  71. // an ad
  72. assertExtractsCorrectly(
  73. 'wk1_kDJhyBk',
  74. 'http://www.youtube.com/watch?v=wk1_kDJhyBk&feature=yva-video-display');
  75. // a related video
  76. assertExtractsCorrectly(
  77. '7qL2PuLF0SI',
  78. 'http://www.youtube.com/watch?v=7qL2PuLF0SI&feature=related');
  79. // with a timestamp
  80. assertExtractsCorrectly(
  81. 'siJZXtsdfsf', 'http://www.youtube.com/watch?v=siJZXtsdfsf#t=2m59s');
  82. // with a timestamp and multiple hash params
  83. assertExtractsCorrectly(
  84. 'siJZXtabdef',
  85. 'http://www.youtube.com/watch?v=siJZXtabdef#t=1m59s&videos=foo');
  86. // with a timestamp, multiple regular and hash params
  87. assertExtractsCorrectly(
  88. 'siJZXtabxyz',
  89. 'http://www.youtube.com/watch?foo=bar&v=siJZXtabxyz&x=y#t=1m30s' +
  90. '&videos=bar');
  91. // only hash params
  92. assertExtractsCorrectly(
  93. 'MWBpQoPwT3U',
  94. 'http://www.youtube.com/watch#!playnext=1&playnext_from=TL' +
  95. '&videos=RX1XPmgerGo&v=MWBpQoPwT3U');
  96. // only hash params
  97. assertExtractsCorrectly(
  98. 'MWBpQoPwT3V',
  99. 'http://www.youtube.com/watch#!playnext=1&playnext_from=TL' +
  100. '&videos=RX1XPmgerGp&v=MWBpQoPwT3V&foo=bar');
  101. assertExtractsCorrectly(
  102. 'jqxENMKaeCU',
  103. 'http://www.youtube.com/watch#!v=jqxENMKaeCU&feature=related');
  104. // Lots of query params, some of them w/ numbers, one of them before the
  105. // video ID
  106. assertExtractsCorrectly(
  107. 'qbce2yN81mE',
  108. 'http://www.youtube.com/watch?usg=AFQjCNFf90T3fekgdVBmPp-Wgya5_CTSaw' +
  109. '&v=qbce2yN81mE&source=video&vgc=rss');
  110. assertExtractsCorrectly(
  111. 'Lc-8onVA5Jk', 'http://www.youtube.com/watch?v=Lc-8onVA5Jk&feature=dir');
  112. // Last character in the video ID is '-' (a non-word but valid character)
  113. // and the video ID is the last query parameter
  114. assertExtractsCorrectly(
  115. 'Lc-8onV5Jk-', 'http://www.youtube.com/watch?v=Lc-8onV5Jk-');
  116. var invalidUrls = [
  117. 'http://invalidUrl/watch?v=dMH0bHeiRNg',
  118. 'http://www$youtube.com/watch?v=dMH0bHeiRNg',
  119. 'http://www.youtube$com/watch?v=dMH0bHeiRNg',
  120. 'http://w_w.youtube.com/watch?v=dMH0bHeiRNg'
  121. ];
  122. for (var i = 0, j = invalidUrls.length; i < j; ++i) {
  123. var e = assertThrows('parser expects a well formed URL', function() {
  124. goog.ui.media.YoutubeModel.newInstance(invalidUrls[i]);
  125. });
  126. assertEquals(
  127. 'failed to parse video id from youtube url: ' + invalidUrls[i],
  128. e.message);
  129. }
  130. }
  131. function testBuildingUrl() {
  132. assertEquals(
  133. YOUTUBE_URL, goog.ui.media.YoutubeModel.buildUrl(YOUTUBE_VIDEO_ID));
  134. }
  135. function testCreatingModel() {
  136. var model = new goog.ui.media.YoutubeModel(YOUTUBE_VIDEO_ID);
  137. assertEquals(YOUTUBE_VIDEO_ID, model.getVideoId());
  138. assertEquals(YOUTUBE_URL, model.getUrl());
  139. assertUndefined(model.getCaption());
  140. }
  141. function testCreatingDomOnInitialState() {
  142. control.render(parent);
  143. var preview = goog.dom.getElementsByTagNameAndClass(
  144. goog.dom.TagName.IMG, goog.ui.media.Youtube.CSS_CLASS + '-thumbnail0',
  145. parent);
  146. assertEquals(1, preview.length);
  147. var caption = goog.dom.getElementsByTagNameAndClass(
  148. goog.dom.TagName.DIV, goog.ui.media.Youtube.CSS_CLASS + '-caption',
  149. parent);
  150. assertEquals(1, caption.length);
  151. var flash = goog.dom.getElementsByTagNameAndClass(
  152. goog.dom.TagName.DIV, goog.ui.media.FlashObject.CSS_CLASS);
  153. assertEquals(0, flash.length);
  154. }
  155. function testCreatingDomOnSelectedState() {
  156. control.render(parent);
  157. control.setSelected(true);
  158. var preview = goog.dom.getElementsByTagNameAndClass(
  159. goog.dom.TagName.IMG, goog.ui.media.Youtube.CSS_CLASS + '-preview',
  160. parent);
  161. assertEquals(0, preview.length);
  162. var caption = goog.dom.getElementsByTagNameAndClass(
  163. goog.dom.TagName.DIV, goog.ui.media.Youtube.CSS_CLASS + '-caption',
  164. parent);
  165. assertEquals(1, caption.length);
  166. var flash = goog.dom.getElementsByTagNameAndClass(
  167. goog.dom.TagName.DIV, goog.ui.media.FlashObject.CSS_CLASS, parent);
  168. assertEquals(1, flash.length);
  169. }
  170. function testSettingSelectedStateAfterRender() {
  171. control.render(parent);
  172. control.setSelected(true);
  173. var preview = goog.dom.getElementsByTagNameAndClass(
  174. goog.dom.TagName.IMG, goog.ui.media.Youtube.CSS_CLASS + '-preview',
  175. parent);
  176. assertEquals(0, preview.length);
  177. var caption = goog.dom.getElementsByTagNameAndClass(
  178. goog.dom.TagName.DIV, goog.ui.media.Youtube.CSS_CLASS + '-caption',
  179. parent);
  180. assertEquals(1, caption.length);
  181. var flash = goog.dom.getElementsByTagNameAndClass(
  182. goog.dom.TagName.DIV, goog.ui.media.FlashObject.CSS_CLASS, parent);
  183. assertEquals(1, flash.length);
  184. control.setSelected(false);
  185. var preview = goog.dom.getElementsByTagNameAndClass(
  186. goog.dom.TagName.IMG, goog.ui.media.Youtube.CSS_CLASS + '-thumbnail0',
  187. parent);
  188. assertEquals(1, preview.length);
  189. var caption = goog.dom.getElementsByTagNameAndClass(
  190. goog.dom.TagName.DIV, goog.ui.media.Youtube.CSS_CLASS + '-caption',
  191. parent);
  192. assertEquals(1, caption.length);
  193. // setting select as false doesn't actually remove the flash movie from
  194. // the DOM tree, which means that setting selected to true won't actually
  195. // restart the movie. TODO(user): fix this.
  196. var flash = goog.dom.getElementsByTagNameAndClass(
  197. goog.dom.TagName.DIV, goog.ui.media.FlashObject.CSS_CLASS, parent);
  198. assertEquals(1, flash.length);
  199. control.setSelected(true);
  200. var flash = goog.dom.getElementsByTagNameAndClass(
  201. goog.dom.TagName.DIV, goog.ui.media.FlashObject.CSS_CLASS, parent);
  202. assertEquals(1, flash.length);
  203. }
  204. function testUrlMatcher() {
  205. var matcher = goog.ui.media.YoutubeModel.MATCHER_;
  206. assertTrue(matcher.test('http://www.youtube.com/watch?v=55D-ybnYQSs'));
  207. assertTrue(matcher.test('https://youtube.com/watch?v=55D-ybnYQSs'));
  208. assertTrue(
  209. matcher.test('https://youtube.com/watch?blarg=blop&v=55D-ybnYQSs'));
  210. assertTrue(matcher.test('http://www.youtube.com/watch?v=55D-ybnYQSs#wee'));
  211. assertFalse(matcher.test('http://www.cnn.com/watch?v=55D-ybnYQSs#wee'));
  212. assertFalse(matcher.test('ftp://www.youtube.com/watch?v=55D-ybnYQSs#wee'));
  213. }
  214. function assertExtractsCorrectly(expectedVideoId, url) {
  215. var youtube = goog.ui.media.YoutubeModel.newInstance(url);
  216. assertEquals('videoid for ' + url, expectedVideoId, youtube.getVideoId());
  217. }