| 1 |
// Copyright 2011 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.GoogleVideoTest'); |
| 16 |
goog.setTestOnly('goog.ui.media.GoogleVideoTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.TagName'); |
| 20 |
goog.require('goog.testing.jsunit'); |
| 21 |
goog.require('goog.ui.media.FlashObject'); |
| 22 |
goog.require('goog.ui.media.GoogleVideo'); |
| 23 |
goog.require('goog.ui.media.GoogleVideoModel'); |
| 24 |
goog.require('goog.ui.media.Media'); |
| 25 |
var video; |
| 26 |
var control; |
| 27 |
var VIDEO_URL_PREFIX = 'https://video.google.com/videoplay?docid='; |
| 28 |
var VIDEO_ID = '7582902000166025817'; |
| 29 |
var VIDEO_URL = VIDEO_URL_PREFIX + VIDEO_ID; |
| 30 |
var parent = goog.dom.createElement(goog.dom.TagName.DIV); |
| 31 |
|
| 32 |
function setUp() { |
| 33 |
video = new goog.ui.media.GoogleVideo(); |
| 34 |
var model = new goog.ui.media.GoogleVideoModel(VIDEO_ID, 'video caption'); |
| 35 |
control = new goog.ui.media.Media(model, video); |
| 36 |
control.setSelected(true); |
| 37 |
} |
| 38 |
|
| 39 |
function tearDown() { |
| 40 |
control.dispose(); |
| 41 |
} |
| 42 |
|
| 43 |
function testBasicRendering() { |
| 44 |
control.render(parent); |
| 45 |
var el = goog.dom.getElementsByTagNameAndClass( |
| 46 |
goog.dom.TagName.DIV, goog.ui.media.GoogleVideo.CSS_CLASS, parent); |
| 47 |
assertEquals(1, el.length); |
| 48 |
assertEquals(VIDEO_URL, control.getDataModel().getUrl()); |
| 49 |
} |
| 50 |
|
| 51 |
function testParsingUrl() { |
| 52 |
assertExtractsCorrectly(VIDEO_ID, VIDEO_URL); |
| 53 |
// Test a url with # at the end. |
| 54 |
assertExtractsCorrectly(VIDEO_ID, VIDEO_URL + '#'); |
| 55 |
// Test a url with a negative docid. |
| 56 |
assertExtractsCorrectly('-123', VIDEO_URL_PREFIX + '-123'); |
| 57 |
// Test a url with two docids. The valid one is the second. |
| 58 |
assertExtractsCorrectly('123', VIDEO_URL + '#docid=123'); |
| 59 |
|
| 60 |
var invalidUrl = 'http://invalidUrl/filename.doc'; |
| 61 |
var e = assertThrows('parser expects a well formed URL', function() { |
| 62 |
goog.ui.media.GoogleVideoModel.newInstance(invalidUrl); |
| 63 |
}); |
| 64 |
assertEquals( |
| 65 |
'failed to parse video id from GoogleVideo url: ' + invalidUrl, |
| 66 |
e.message); |
| 67 |
} |
| 68 |
|
| 69 |
function testBuildingUrl() { |
| 70 |
assertEquals(VIDEO_URL, goog.ui.media.GoogleVideoModel.buildUrl(VIDEO_ID)); |
| 71 |
} |
| 72 |
|
| 73 |
function testCreatingModel() { |
| 74 |
var model = new goog.ui.media.GoogleVideoModel(VIDEO_ID); |
| 75 |
assertEquals(VIDEO_ID, model.getVideoId()); |
| 76 |
assertEquals(VIDEO_URL, model.getUrl()); |
| 77 |
assertUndefined(model.getCaption()); |
| 78 |
} |
| 79 |
|
| 80 |
function testCreatingDomOnInitialState() { |
| 81 |
control.render(parent); |
| 82 |
var caption = goog.dom.getElementsByTagNameAndClass( |
| 83 |
goog.dom.TagName.DIV, goog.ui.media.GoogleVideo.CSS_CLASS + '-caption', |
| 84 |
parent); |
| 85 |
assertEquals(1, caption.length); |
| 86 |
|
| 87 |
var flash = goog.dom.getElementsByTagNameAndClass( |
| 88 |
goog.dom.TagName.DIV, goog.ui.media.FlashObject.CSS_CLASS, parent); |
| 89 |
assertEquals(1, flash.length); |
| 90 |
} |
| 91 |
|
| 92 |
function assertExtractsCorrectly(expectedVideoId, url) { |
| 93 |
var model = goog.ui.media.GoogleVideoModel.newInstance(url); |
| 94 |
assertEquals('Video id for ' + url, expectedVideoId, model.getVideoId()); |
| 95 |
} |