| 1 |
// Copyright 2012 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 |
/** |
| 16 |
* @fileoverview Unit tests for goog.labs.net.Image. |
| 17 |
* |
| 18 |
* @author [email protected] (Nathan Naze) |
| 19 |
*/ |
| 20 |
|
| 21 |
goog.provide('goog.labs.net.imageTest'); |
| 22 |
|
| 23 |
goog.require('goog.labs.net.image'); |
| 24 |
goog.require('goog.string'); |
| 25 |
goog.require('goog.testing.TestCase'); |
| 26 |
goog.require('goog.testing.jsunit'); |
| 27 |
goog.require('goog.testing.recordFunction'); |
| 28 |
|
| 29 |
goog.setTestOnly('goog.labs.net.imageTest'); |
| 30 |
|
| 31 |
|
| 32 |
function setUpPage() { |
| 33 |
goog.testing.TestCase.getActiveTestCase().promiseTimeout = 10000; // 10s |
| 34 |
} |
| 35 |
|
| 36 |
function testValidImage() { |
| 37 |
var url = 'testdata/cleardot.gif'; |
| 38 |
|
| 39 |
return goog.labs.net.image.load(url).then(function(value) { |
| 40 |
assertEquals('IMG', value.tagName); |
| 41 |
assertTrue(goog.string.endsWith(value.src, url)); |
| 42 |
}); |
| 43 |
} |
| 44 |
|
| 45 |
function testInvalidImage() { |
| 46 |
var url = 'testdata/invalid.gif'; // This file does not exist. |
| 47 |
|
| 48 |
return goog.labs.net.image.load(url).then( |
| 49 |
function() { fail('Invalid image should not resolve'); }, |
| 50 |
function(errResult) { assertNull(errResult); }); |
| 51 |
} |
| 52 |
|
| 53 |
function testImageFactory() { |
| 54 |
var returnedImage = new Image(); |
| 55 |
var factory = function() { return returnedImage; }; |
| 56 |
var countedFactory = goog.testing.recordFunction(factory); |
| 57 |
|
| 58 |
var url = 'testdata/cleardot.gif'; |
| 59 |
|
| 60 |
return goog.labs.net.image.load(url, countedFactory).then(function(value) { |
| 61 |
assertEquals(returnedImage, value); |
| 62 |
assertEquals(1, countedFactory.getCallCount()); |
| 63 |
}); |
| 64 |
} |
| 65 |
|
| 66 |
function testExistingImage() { |
| 67 |
var image = new Image(); |
| 68 |
|
| 69 |
var url = 'testdata/cleardot.gif'; |
| 70 |
|
| 71 |
return goog.labs.net.image.load(url, image).then(function(value) { |
| 72 |
assertEquals(image, value); |
| 73 |
}); |
| 74 |
} |