image_test.js 2.2 KB

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