image_loader_client_unittest.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. import {reportPromise} from 'chrome-extension://hhaomjibdihmijegdhdafkllkbggdgoj/common/js/test_error_reporting.js';
  5. import {assertFalse, assertTrue} from 'chrome://webui-test/chai_assert.js';
  6. import {ImageLoaderClient} from './image_loader_client.js';
  7. import {LoadImageRequest, LoadImageResponse, LoadImageResponseStatus} from './load_image_request.js';
  8. /** @suppress {const|checkTypes} */
  9. export function setUp() {
  10. chrome.metricsPrivate = {
  11. MetricTypeType:
  12. {HISTOGRAM_LOG: 'histogram-log', HISTOGRAM_LINEAR: 'histogram-linear'},
  13. recordPercentage: function() {},
  14. recordValue: function() {},
  15. };
  16. chrome.i18n = {
  17. getMessage: function() {},
  18. };
  19. }
  20. /**
  21. * Lets the client to load URL and returns the local cache (not caches in the
  22. * image loader extension) is used or not.
  23. *
  24. * @param {ImageLoaderClient} client
  25. * @param {string} url URL
  26. * @param {boolean} cache Whether to request caching on the request.
  27. * @return {Promise<boolean>} True if the local cache is used.
  28. */
  29. function loadAndCheckCacheUsed(client, url, cache) {
  30. let cacheUsed = true;
  31. /** @suppress {accessControls} */
  32. ImageLoaderClient.sendMessage_ = function(message, callback) {
  33. cacheUsed = false;
  34. if (callback) {
  35. callback(new LoadImageResponse(
  36. LoadImageResponseStatus.SUCCESS, message.taskId || -1,
  37. {width: 100, height: 100, ifd: null, data: 'ImageData'}));
  38. }
  39. };
  40. const request = LoadImageRequest.createForUrl(url);
  41. request.cache = cache;
  42. return new Promise(function(fulfill) {
  43. client.load(request, function() {
  44. fulfill(cacheUsed);
  45. });
  46. });
  47. }
  48. export function testCache(callback) {
  49. const client = new ImageLoaderClient();
  50. reportPromise(
  51. loadAndCheckCacheUsed(client, 'http://example.com/image.jpg', true)
  52. .then(function(cacheUsed) {
  53. assertFalse(cacheUsed);
  54. return loadAndCheckCacheUsed(
  55. client, 'http://example.com/image.jpg', true);
  56. })
  57. .then(function(cacheUsed) {
  58. assertTrue(cacheUsed);
  59. }),
  60. callback);
  61. }
  62. export function testNoCache(callback) {
  63. const client = new ImageLoaderClient();
  64. reportPromise(
  65. loadAndCheckCacheUsed(client, 'http://example.com/image.jpg', false)
  66. .then(function(cacheUsed) {
  67. assertFalse(cacheUsed);
  68. return loadAndCheckCacheUsed(
  69. client, 'http://example.com/image.jpg', false);
  70. })
  71. .then(function(cacheUsed) {
  72. assertFalse(cacheUsed);
  73. }),
  74. callback);
  75. }
  76. export function testDataURLCache(callback) {
  77. const client = new ImageLoaderClient();
  78. reportPromise(
  79. loadAndCheckCacheUsed(client, 'data:URI', true)
  80. .then(function(cacheUsed) {
  81. assertFalse(cacheUsed);
  82. return loadAndCheckCacheUsed(client, 'data:URI', true);
  83. })
  84. .then(function(cacheUsed) {
  85. assertFalse(cacheUsed);
  86. }),
  87. callback);
  88. }