test-places-favicon.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. 'use strict';
  5. module.metadata = {
  6. 'engines': {
  7. 'Firefox': '*'
  8. }
  9. };
  10. const { Cc, Ci, Cu } = require('chrome');
  11. const { getFavicon } = require('sdk/places/favicon');
  12. const tabs = require('sdk/tabs');
  13. const open = tabs.open;
  14. const port = 8099;
  15. const host = 'http://localhost:' + port;
  16. const { onFaviconChange, serve, binFavicon } = require('../favicon-helpers');
  17. const { once } = require('sdk/system/events');
  18. const { defer } = require('sdk/core/promise');
  19. const { resetPlaces } = require('../places-helper');
  20. const faviconService = Cc["@mozilla.org/browser/favicon-service;1"].
  21. getService(Ci.nsIFaviconService);
  22. exports.testStringGetFaviconCallbackSuccess = function (assert, done) {
  23. let name = 'callbacksuccess'
  24. let srv = makeServer(name);
  25. let url = host + '/' + name + '.html';
  26. let favicon = host + '/' + name + '.ico';
  27. let tab;
  28. onFaviconChange(url, function (faviconUrl) {
  29. getFavicon(url, function (url) {
  30. assert.equal(favicon, url, 'Callback returns correct favicon url');
  31. complete(tab, srv, done);
  32. });
  33. });
  34. open({
  35. url: url,
  36. onOpen: function (newTab) tab = newTab,
  37. inBackground: true
  38. });
  39. };
  40. exports.testStringGetFaviconCallbackFailure = function (assert, done) {
  41. let name = 'callbackfailure';
  42. let srv = makeServer(name);
  43. let url = host + '/' + name + '.html';
  44. let tab;
  45. waitAndExpire(url).then(function () {
  46. getFavicon(url, function (url) {
  47. assert.equal(url, null, 'Callback returns null');
  48. complete(tab, srv, done);
  49. });
  50. });
  51. open({
  52. url: url,
  53. onOpen: function (newTab) tab = newTab,
  54. inBackground: true
  55. });
  56. };
  57. exports.testStringGetFaviconPromiseSuccess = function (assert, done) {
  58. let name = 'promisesuccess'
  59. let srv = makeServer(name);
  60. let url = host + '/' + name + '.html';
  61. let favicon = host + '/' + name + '.ico';
  62. let tab;
  63. onFaviconChange(url, function (faviconUrl) {
  64. getFavicon(url).then(function (url) {
  65. assert.equal(url, favicon, 'Callback returns null');
  66. }, function (err) {
  67. assert.fail('Reject should not be called');
  68. }).then(complete.bind(null, tab, srv, done));
  69. });
  70. open({
  71. url: url,
  72. onOpen: function (newTab) tab = newTab,
  73. inBackground: true
  74. });
  75. };
  76. exports.testStringGetFaviconPromiseFailure = function (assert, done) {
  77. let name = 'promisefailure'
  78. let srv = makeServer(name);
  79. let url = host + '/' + name + '.html';
  80. let tab;
  81. waitAndExpire(url).then(function () {
  82. getFavicon(url).then(invalidResolve(assert), validReject(assert, 'expired url'))
  83. .then(complete.bind(null, tab, srv, done));
  84. });
  85. open({
  86. url: url,
  87. onOpen: function (newTab) tab = newTab,
  88. inBackground: true
  89. });
  90. };
  91. exports.testTabsGetFaviconPromiseSuccess = function (assert, done) {
  92. let name = 'tabs-success'
  93. let srv = makeServer(name);
  94. let url = host + '/' + name + '.html';
  95. let favicon = host + '/' + name + '.ico';
  96. let tab;
  97. onFaviconChange(url, function () {
  98. getFavicon(tab).then(function (url) {
  99. assert.equal(url, favicon, "getFavicon should return url for tab");
  100. complete(tab, srv, done);
  101. });
  102. });
  103. open({
  104. url: url,
  105. onOpen: function (newTab) tab = newTab,
  106. inBackground: true
  107. });
  108. };
  109. exports.testTabsGetFaviconPromiseFailure = function (assert, done) {
  110. let name = 'tabs-failure'
  111. let srv = makeServer(name);
  112. let url = host + '/' + name + '.html';
  113. let tab;
  114. waitAndExpire(url).then(function () {
  115. getFavicon(tab).then(invalidResolve(assert), validReject(assert, 'expired tab'))
  116. .then(complete.bind(null, tab, srv, done));
  117. });
  118. open({
  119. url: url,
  120. onOpen: function (newTab) tab = newTab,
  121. inBackground: true
  122. });
  123. };
  124. exports.testRejects = function (assert, done) {
  125. getFavicon({})
  126. .then(invalidResolve(assert), validReject(assert, 'Object'))
  127. .then(getFavicon(null))
  128. .then(invalidResolve(assert), validReject(assert, 'null'))
  129. .then(getFavicon(undefined))
  130. .then(invalidResolve(assert), validReject(assert, 'undefined'))
  131. .then(getFavicon([]))
  132. .then(invalidResolve(assert), validReject(assert, 'Array'))
  133. .then(done);
  134. };
  135. function invalidResolve (assert) {
  136. return function () assert.fail('Promise should not be resolved successfully');
  137. }
  138. function validReject (assert, name) {
  139. return function () assert.pass(name + ' correctly rejected');
  140. }
  141. function makeServer (name) {
  142. return serve({name: name, favicon: binFavicon, port: port, host: host});
  143. }
  144. function waitAndExpire (url) {
  145. let deferred = defer();
  146. onFaviconChange(url, function (faviconUrl) {
  147. once('places-favicons-expired', function () {
  148. deferred.resolve();
  149. });
  150. faviconService.expireAllFavicons();
  151. });
  152. return deferred.promise;
  153. }
  154. function complete(tab, srv, done) {
  155. tab.close(function () {
  156. resetPlaces(() => {
  157. srv.stop(done);
  158. });
  159. });
  160. }