michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: 'use strict'; michael@0: michael@0: module.metadata = { michael@0: 'engines': { michael@0: 'Firefox': '*' michael@0: } michael@0: }; michael@0: michael@0: const { Cc, Ci, Cu } = require('chrome'); michael@0: const { getFavicon } = require('sdk/places/favicon'); michael@0: const tabs = require('sdk/tabs'); michael@0: const open = tabs.open; michael@0: const port = 8099; michael@0: const host = 'http://localhost:' + port; michael@0: const { onFaviconChange, serve, binFavicon } = require('../favicon-helpers'); michael@0: const { once } = require('sdk/system/events'); michael@0: const { defer } = require('sdk/core/promise'); michael@0: const { resetPlaces } = require('../places-helper'); michael@0: const faviconService = Cc["@mozilla.org/browser/favicon-service;1"]. michael@0: getService(Ci.nsIFaviconService); michael@0: michael@0: exports.testStringGetFaviconCallbackSuccess = function (assert, done) { michael@0: let name = 'callbacksuccess' michael@0: let srv = makeServer(name); michael@0: let url = host + '/' + name + '.html'; michael@0: let favicon = host + '/' + name + '.ico'; michael@0: let tab; michael@0: michael@0: onFaviconChange(url, function (faviconUrl) { michael@0: getFavicon(url, function (url) { michael@0: assert.equal(favicon, url, 'Callback returns correct favicon url'); michael@0: complete(tab, srv, done); michael@0: }); michael@0: }); michael@0: michael@0: open({ michael@0: url: url, michael@0: onOpen: function (newTab) tab = newTab, michael@0: inBackground: true michael@0: }); michael@0: }; michael@0: michael@0: exports.testStringGetFaviconCallbackFailure = function (assert, done) { michael@0: let name = 'callbackfailure'; michael@0: let srv = makeServer(name); michael@0: let url = host + '/' + name + '.html'; michael@0: let tab; michael@0: michael@0: waitAndExpire(url).then(function () { michael@0: getFavicon(url, function (url) { michael@0: assert.equal(url, null, 'Callback returns null'); michael@0: complete(tab, srv, done); michael@0: }); michael@0: }); michael@0: michael@0: open({ michael@0: url: url, michael@0: onOpen: function (newTab) tab = newTab, michael@0: inBackground: true michael@0: }); michael@0: }; michael@0: michael@0: exports.testStringGetFaviconPromiseSuccess = function (assert, done) { michael@0: let name = 'promisesuccess' michael@0: let srv = makeServer(name); michael@0: let url = host + '/' + name + '.html'; michael@0: let favicon = host + '/' + name + '.ico'; michael@0: let tab; michael@0: michael@0: onFaviconChange(url, function (faviconUrl) { michael@0: getFavicon(url).then(function (url) { michael@0: assert.equal(url, favicon, 'Callback returns null'); michael@0: }, function (err) { michael@0: assert.fail('Reject should not be called'); michael@0: }).then(complete.bind(null, tab, srv, done)); michael@0: }); michael@0: michael@0: open({ michael@0: url: url, michael@0: onOpen: function (newTab) tab = newTab, michael@0: inBackground: true michael@0: }); michael@0: }; michael@0: michael@0: exports.testStringGetFaviconPromiseFailure = function (assert, done) { michael@0: let name = 'promisefailure' michael@0: let srv = makeServer(name); michael@0: let url = host + '/' + name + '.html'; michael@0: let tab; michael@0: michael@0: waitAndExpire(url).then(function () { michael@0: getFavicon(url).then(invalidResolve(assert), validReject(assert, 'expired url')) michael@0: .then(complete.bind(null, tab, srv, done)); michael@0: }); michael@0: michael@0: open({ michael@0: url: url, michael@0: onOpen: function (newTab) tab = newTab, michael@0: inBackground: true michael@0: }); michael@0: }; michael@0: michael@0: exports.testTabsGetFaviconPromiseSuccess = function (assert, done) { michael@0: let name = 'tabs-success' michael@0: let srv = makeServer(name); michael@0: let url = host + '/' + name + '.html'; michael@0: let favicon = host + '/' + name + '.ico'; michael@0: let tab; michael@0: michael@0: onFaviconChange(url, function () { michael@0: getFavicon(tab).then(function (url) { michael@0: assert.equal(url, favicon, "getFavicon should return url for tab"); michael@0: complete(tab, srv, done); michael@0: }); michael@0: }); michael@0: michael@0: open({ michael@0: url: url, michael@0: onOpen: function (newTab) tab = newTab, michael@0: inBackground: true michael@0: }); michael@0: }; michael@0: michael@0: michael@0: exports.testTabsGetFaviconPromiseFailure = function (assert, done) { michael@0: let name = 'tabs-failure' michael@0: let srv = makeServer(name); michael@0: let url = host + '/' + name + '.html'; michael@0: let tab; michael@0: michael@0: waitAndExpire(url).then(function () { michael@0: getFavicon(tab).then(invalidResolve(assert), validReject(assert, 'expired tab')) michael@0: .then(complete.bind(null, tab, srv, done)); michael@0: }); michael@0: michael@0: open({ michael@0: url: url, michael@0: onOpen: function (newTab) tab = newTab, michael@0: inBackground: true michael@0: }); michael@0: }; michael@0: michael@0: exports.testRejects = function (assert, done) { michael@0: getFavicon({}) michael@0: .then(invalidResolve(assert), validReject(assert, 'Object')) michael@0: .then(() => getFavicon(null)) michael@0: .then(invalidResolve(assert), validReject(assert, 'null')) michael@0: .then(() => getFavicon(undefined)) michael@0: .then(invalidResolve(assert), validReject(assert, 'undefined')) michael@0: .then(() => getFavicon([])) michael@0: .then(invalidResolve(assert), validReject(assert, 'Array')) michael@0: .catch(assert.fail).then(done); michael@0: }; michael@0: michael@0: function invalidResolve (assert) { michael@0: return function () assert.fail('Promise should not be resolved successfully'); michael@0: } michael@0: michael@0: function validReject (assert, name) { michael@0: return function () assert.pass(name + ' correctly rejected'); michael@0: } michael@0: michael@0: function makeServer (name) { michael@0: return serve({name: name, favicon: binFavicon, port: port, host: host}); michael@0: } michael@0: michael@0: function waitAndExpire (url) { michael@0: let deferred = defer(); michael@0: onFaviconChange(url, function (faviconUrl) { michael@0: once('places-favicons-expired', function () { michael@0: deferred.resolve(); michael@0: }); michael@0: faviconService.expireAllFavicons(); michael@0: }); michael@0: return deferred.promise; michael@0: } michael@0: michael@0: function complete(tab, srv, done) { michael@0: tab.close(function () { michael@0: resetPlaces(() => { michael@0: srv.stop(done); michael@0: }); michael@0: }); michael@0: }