addon-sdk/source/test/addons/places/tests/test-places-favicon.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/addons/places/tests/test-places-favicon.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,188 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +'use strict';
     1.9 +
    1.10 +module.metadata = {
    1.11 +  'engines': {
    1.12 +    'Firefox': '*'
    1.13 +  }
    1.14 +};
    1.15 +
    1.16 +const { Cc, Ci, Cu } = require('chrome');
    1.17 +const { getFavicon } = require('sdk/places/favicon');
    1.18 +const tabs = require('sdk/tabs');
    1.19 +const open = tabs.open;
    1.20 +const port = 8099;
    1.21 +const host = 'http://localhost:' + port;
    1.22 +const { onFaviconChange, serve, binFavicon } = require('../favicon-helpers');
    1.23 +const { once } = require('sdk/system/events');
    1.24 +const { defer } = require('sdk/core/promise');
    1.25 +const { resetPlaces } = require('../places-helper');
    1.26 +const faviconService = Cc["@mozilla.org/browser/favicon-service;1"].
    1.27 +                         getService(Ci.nsIFaviconService);
    1.28 +
    1.29 +exports.testStringGetFaviconCallbackSuccess = function (assert, done) {
    1.30 +  let name = 'callbacksuccess'
    1.31 +  let srv = makeServer(name);
    1.32 +  let url = host + '/' + name + '.html';
    1.33 +  let favicon = host + '/' + name + '.ico';
    1.34 +  let tab;
    1.35 +
    1.36 +  onFaviconChange(url, function (faviconUrl) {
    1.37 +    getFavicon(url, function (url) {
    1.38 +      assert.equal(favicon, url, 'Callback returns correct favicon url');
    1.39 +      complete(tab, srv, done);
    1.40 +    });
    1.41 +  });
    1.42 +
    1.43 +  open({
    1.44 +    url: url,
    1.45 +    onOpen: function (newTab) tab = newTab,
    1.46 +    inBackground: true
    1.47 +  });
    1.48 +};
    1.49 +
    1.50 +exports.testStringGetFaviconCallbackFailure = function (assert, done) {
    1.51 +  let name = 'callbackfailure';
    1.52 +  let srv = makeServer(name);
    1.53 +  let url = host + '/' + name + '.html';
    1.54 +  let tab;
    1.55 +
    1.56 +  waitAndExpire(url).then(function () {
    1.57 +    getFavicon(url, function (url) {
    1.58 +      assert.equal(url, null, 'Callback returns null');
    1.59 +      complete(tab, srv, done);
    1.60 +    });
    1.61 +  });
    1.62 +
    1.63 +  open({
    1.64 +    url: url,
    1.65 +    onOpen: function (newTab) tab = newTab,
    1.66 +    inBackground: true
    1.67 +  });
    1.68 +};
    1.69 +
    1.70 +exports.testStringGetFaviconPromiseSuccess = function (assert, done) {
    1.71 +  let name = 'promisesuccess'
    1.72 +  let srv = makeServer(name);
    1.73 +  let url = host + '/' + name + '.html';
    1.74 +  let favicon = host + '/' + name + '.ico';
    1.75 +  let tab;
    1.76 +
    1.77 +  onFaviconChange(url, function (faviconUrl) {
    1.78 +    getFavicon(url).then(function (url) {
    1.79 +      assert.equal(url, favicon, 'Callback returns null');
    1.80 +    }, function (err) {
    1.81 +      assert.fail('Reject should not be called');
    1.82 +    }).then(complete.bind(null, tab, srv, done));
    1.83 +  });
    1.84 +
    1.85 +  open({
    1.86 +    url: url,
    1.87 +    onOpen: function (newTab) tab = newTab,
    1.88 +    inBackground: true
    1.89 +  });
    1.90 +};
    1.91 +
    1.92 +exports.testStringGetFaviconPromiseFailure = function (assert, done) {
    1.93 +  let name = 'promisefailure'
    1.94 +  let srv = makeServer(name);
    1.95 +  let url = host + '/' + name + '.html';
    1.96 +  let tab;
    1.97 +
    1.98 +  waitAndExpire(url).then(function () {
    1.99 +    getFavicon(url).then(invalidResolve(assert), validReject(assert, 'expired url'))
   1.100 +      .then(complete.bind(null, tab, srv, done));
   1.101 +  });
   1.102 +
   1.103 +  open({
   1.104 +    url: url,
   1.105 +    onOpen: function (newTab) tab = newTab,
   1.106 +    inBackground: true
   1.107 +  });
   1.108 +};
   1.109 +
   1.110 +exports.testTabsGetFaviconPromiseSuccess = function (assert, done) {
   1.111 +  let name = 'tabs-success'
   1.112 +  let srv = makeServer(name);
   1.113 +  let url = host + '/' + name + '.html';
   1.114 +  let favicon = host + '/' + name + '.ico';
   1.115 +  let tab;
   1.116 +
   1.117 +  onFaviconChange(url, function () {
   1.118 +    getFavicon(tab).then(function (url) {
   1.119 +      assert.equal(url, favicon, "getFavicon should return url for tab");
   1.120 +      complete(tab, srv, done);
   1.121 +    });
   1.122 +  });
   1.123 +
   1.124 +  open({
   1.125 +    url: url,
   1.126 +    onOpen: function (newTab) tab = newTab,
   1.127 +    inBackground: true
   1.128 +  });
   1.129 +};
   1.130 +
   1.131 +
   1.132 +exports.testTabsGetFaviconPromiseFailure = function (assert, done) {
   1.133 +  let name = 'tabs-failure'
   1.134 +  let srv = makeServer(name);
   1.135 +  let url = host + '/' + name + '.html';
   1.136 +  let tab;
   1.137 +
   1.138 +  waitAndExpire(url).then(function () {
   1.139 +    getFavicon(tab).then(invalidResolve(assert), validReject(assert, 'expired tab'))
   1.140 +      .then(complete.bind(null, tab, srv, done));
   1.141 +  });
   1.142 +
   1.143 +  open({
   1.144 +    url: url,
   1.145 +    onOpen: function (newTab) tab = newTab,
   1.146 +    inBackground: true
   1.147 +  });
   1.148 +};
   1.149 +
   1.150 +exports.testRejects = function (assert, done) {
   1.151 +  getFavicon({})
   1.152 +    .then(invalidResolve(assert), validReject(assert, 'Object'))
   1.153 +  .then(() => getFavicon(null))
   1.154 +    .then(invalidResolve(assert), validReject(assert, 'null'))
   1.155 +  .then(() => getFavicon(undefined))
   1.156 +    .then(invalidResolve(assert), validReject(assert, 'undefined'))
   1.157 +  .then(() => getFavicon([]))
   1.158 +    .then(invalidResolve(assert), validReject(assert, 'Array'))
   1.159 +    .catch(assert.fail).then(done);
   1.160 +};
   1.161 +
   1.162 +function invalidResolve (assert) {
   1.163 +  return function () assert.fail('Promise should not be resolved successfully');
   1.164 +}
   1.165 +
   1.166 +function validReject (assert, name) {
   1.167 +  return function () assert.pass(name + ' correctly rejected');
   1.168 +}
   1.169 +
   1.170 +function makeServer (name) {
   1.171 +  return serve({name: name, favicon: binFavicon, port: port, host: host});
   1.172 +}
   1.173 +
   1.174 +function waitAndExpire (url) {
   1.175 +  let deferred = defer();
   1.176 +  onFaviconChange(url, function (faviconUrl) {
   1.177 +    once('places-favicons-expired', function () {
   1.178 +      deferred.resolve();
   1.179 +    });
   1.180 +    faviconService.expireAllFavicons();
   1.181 +  });
   1.182 +  return deferred.promise;
   1.183 +}
   1.184 +
   1.185 +function complete(tab, srv, done) {
   1.186 +  tab.close(function () {
   1.187 +    resetPlaces(() => {
   1.188 +      srv.stop(done);
   1.189 +    });
   1.190 +  });
   1.191 +}

mercurial