toolkit/components/places/tests/chrome/test_favicon_annotations.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/chrome/test_favicon_annotations.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,162 @@
     1.4 +<?xml version="1.0"?>
     1.5 +<!-- This Source Code Form is subject to the terms of the Mozilla Public
     1.6 +   - License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 +   - file, You can obtain one at http://mozilla.org/MPL/2.0/.  -->
     1.8 +<!--
     1.9 + * This file tests the moz-anno protocol, which was added in Bug 316077 and how
    1.10 + * it loads favicons.
    1.11 +-->
    1.12 +
    1.13 +<window title="Favicon Annotation Protocol Test"
    1.14 +        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
    1.15 +        onload="test();">
    1.16 +
    1.17 +  <script type="application/javascript"
    1.18 +          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
    1.19 +  <script type="application/javascript"
    1.20 +          src="chrome://mochikit/content/tests/SimpleTest/WindowSnapshot.js"/>
    1.21 +  <script type="application/javascript"
    1.22 +          src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
    1.23 +
    1.24 +  <script type="application/javascript">
    1.25 +  <![CDATA[
    1.26 +
    1.27 +const Cc = Components.classes;
    1.28 +const Ci = Components.interfaces;
    1.29 +const Cr = Components.results;
    1.30 +
    1.31 +let fs = Cc["@mozilla.org/browser/favicon-service;1"].
    1.32 +         getService(Ci.nsIFaviconService);
    1.33 +
    1.34 +// Test descriptions that will be printed in the case of failure.
    1.35 +let testDescriptions = [
    1.36 +  "moz-anno URI with no data in the database loads default icon",
    1.37 +  "URI added to the database is properly loaded",
    1.38 +];
    1.39 +
    1.40 +// URIs to load (will be compared with expectedURIs of the same index).
    1.41 +let testURIs = [
    1.42 +  "http://mozilla.org/2009/made-up-favicon/places-rocks/",
    1.43 +  "http://mozilla.org/should-be-barney/",
    1.44 +];
    1.45 +
    1.46 +// URIs to load for expected results.
    1.47 +let expectedURIs = [
    1.48 +  fs.defaultFavicon.spec,
    1.49 +  "data:image/png,%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%00%10%00%00%00%10%08%06%00%00%00%1F%F3%FFa%00%00%00%04gAMA%00%00%AF%C87%05%8A%E9%00%00%00%19tEXtSoftware%00Adobe%20ImageReadyq%C9e%3C%00%00%01%D6IDATx%DAb%FC%FF%FF%3F%03%25%00%20%80%98%909%EF%DF%BFg%EF%EC%EC%FC%AD%AC%AC%FC%DF%95%91%F1%BF%89%89%C9%7F%20%FF%D7%EA%D5%AB%B7%DF%BBwO%16%9B%01%00%01%C4%00r%01%08%9F9s%C6%CD%D8%D8%F8%BF%0B%03%C3%FF3%40%BC%0A%88%EF%02q%1A%10%BB%40%F1%AAU%ABv%C1%D4%C30%40%00%81%89%993g%3E%06%1A%F6%3F%14%AA%11D%97%03%F1%7Fc%08%0D%E2%2B))%FD%17%04%89%A1%19%00%10%40%0C%D00%F8%0F3%00%C8%F8%BF%1B%E4%0Ac%88a%E5%60%17%19%FF%0F%0D%0D%05%1B%02v%D9%DD%BB%0A0%03%00%02%08%AC%B9%A3%A3%E3%17%03%D4v%90%01%EF%18%106%C3%0Cz%07%C5%BB%A1%DE%82y%07%20%80%A0%A6%08B%FCn%0C1%60%26%D4%20d%C3VA%C3%06%26%BE%0A%EA-%80%00%82%B9%E0%F7L4%0D%EF%90%F8%C6%60%2F%0A%82%BD%01%13%07%0700%D0%01%02%88%11%E4%02P%B41%DC%BB%C7%D0%014%0D%E8l%06W%20%06%BA%88%A1%1C%1AS%15%40%7C%16%CA6.%2Fgx%BFg%0F%83%CB%D9%B3%0C%7B%80%7C%80%00%02%BB%00%E8%9F%ED%20%1B%3A%A0%A6%9F%81%DA%DC%01%C5%B0%80%ED%80%FA%BF%BC%BC%FC%3F%83%12%90%9D%96%F6%1F%20%80%18%DE%BD%7B%C7%0E%8E%05AD%20%FEGr%A6%A0%A0%E0%7F%25P%80%02%9D%0F%D28%13%18%23%C6%C0%B0%02E%3D%C8%F5%00%01%04%8F%05P%A8%BA%40my%87%E4%12c%A8%8D%20%8B%D0%D3%00%08%03%04%10%9C%01R%E4%82d%3B%C8%A0%99%C6%90%90%C6%A5%19%84%01%02%08%9E%17%80%C9x%F7%7B%A0%DBVC%F9%A0%C0%5C%7D%16%2C%CE%00%F4%C6O%5C%99%09%20%800L%04y%A5%03%1A%95%A0%80%05%05%14.%DBA%18%20%80%18)%CD%CE%00%01%06%00%0C'%94%C7%C0k%C9%2C%00%00%00%00IEND%AEB%60%82",
    1.50 +];
    1.51 +
    1.52 +
    1.53 +/**
    1.54 + * The event listener placed on our test windows used to determine when it is
    1.55 + * safe to compare the two windows.
    1.56 + */
    1.57 +let _results = [];
    1.58 +function loadEventHandler()
    1.59 +{
    1.60 +  _results.push(snapshotWindow(window));
    1.61 +
    1.62 +  loadNextTest();
    1.63 +}
    1.64 +
    1.65 +/**
    1.66 + * This runs the comparison.
    1.67 + */
    1.68 +function compareResults(aIndex, aImage1, aImage2)
    1.69 +{
    1.70 +  let [correct, data1, data2] = compareSnapshots(aImage1, aImage2, true);
    1.71 +  SimpleTest.ok(correct,
    1.72 +                "Test '" + testDescriptions[aIndex] + "' matches expectations. " +
    1.73 +                "Data from window 1 is '" + data1 + "'. " +
    1.74 +                "Data from window 2 is '" + data2 + "'");
    1.75 +}
    1.76 +
    1.77 +/**
    1.78 + * Loads the next set of URIs to compare against.
    1.79 + */
    1.80 +let _counter = -1;
    1.81 +function loadNextTest()
    1.82 +{
    1.83 +  _counter++;
    1.84 +  // If we have no more tests, finish.
    1.85 +  if (_counter / 2 == testDescriptions.length) {
    1.86 +    for (let i = 0; i < _results.length; i = i + 2)
    1.87 +      compareResults(i / 2, _results[i], _results[i + 1]);
    1.88 +
    1.89 +    SimpleTest.finish();
    1.90 +    return;
    1.91 +  }
    1.92 +
    1.93 +  let nextURI = function() {
    1.94 +    let index = Math.floor(_counter / 2);
    1.95 +    if ((_counter % 2) == 0)
    1.96 +      return "moz-anno:favicon:" + testURIs[index];
    1.97 +    return expectedURIs[index];
    1.98 +  }
    1.99 +
   1.100 +  let img = document.getElementById("favicon");
   1.101 +  img.setAttribute("src", nextURI());
   1.102 +}
   1.103 +
   1.104 +function test()
   1.105 +{
   1.106 +  SimpleTest.waitForExplicitFinish();
   1.107 +  let db = Cc["@mozilla.org/browser/nav-history-service;1"].
   1.108 +           getService(Ci.nsPIPlacesDatabase).
   1.109 +           DBConnection;
   1.110 +
   1.111 +  // Empty any old favicons
   1.112 +  db.executeSimpleSQL("DELETE FROM moz_favicons");
   1.113 +
   1.114 +  let ios = Cc["@mozilla.org/network/io-service;1"].
   1.115 +            getService(Ci.nsIIOService);
   1.116 +  let uri = function(aSpec) {
   1.117 +    return ios.newURI(aSpec, null, null);
   1.118 +  };
   1.119 +
   1.120 +  let pageURI = uri("http://example.com/favicon_annotations");
   1.121 +  let history = Cc["@mozilla.org/browser/history;1"]
   1.122 +                  .getService(Ci.mozIAsyncHistory);
   1.123 +  history.updatePlaces(
   1.124 +    {
   1.125 +      uri: pageURI,
   1.126 +      visits: [{ transitionType: Ci.nsINavHistoryService.TRANSITION_TYPED,
   1.127 +                 visitDate: Date.now() * 1000
   1.128 +              }],
   1.129 +    },
   1.130 +    {
   1.131 +      handleError: function UP_handleError() {
   1.132 +        ok(false, "Unexpected error in adding visit.");
   1.133 +      },
   1.134 +      handleResult: function () {},
   1.135 +      handleCompletion: function UP_handleCompletion() {
   1.136 +        // Set the favicon data.  Note that the "moz-anno:" protocol requires
   1.137 +        // the favicon to be stored in the database, but the
   1.138 +        // replaceFaviconDataFromDataURL function will not save the favicon
   1.139 +        // unless it is associated with a page.  Thus, we must associate the
   1.140 +        // icon with a page explicitly in order for it to be visible through
   1.141 +        // the protocol.
   1.142 +        fs.replaceFaviconDataFromDataURL(uri(testURIs[1]), expectedURIs[1],
   1.143 +                                         (Date.now() + 60 * 60 * 24 * 1000) * 1000);
   1.144 +        fs.setAndFetchFaviconForPage(pageURI, uri(testURIs[1]), true,
   1.145 +                                     fs.FAVICON_LOAD_NON_PRIVATE);
   1.146 +
   1.147 +        // And start our test process.
   1.148 +        loadNextTest();
   1.149 +      }
   1.150 +    }
   1.151 +  );
   1.152 +
   1.153 +
   1.154 +}
   1.155 +
   1.156 +  ]]>
   1.157 +  </script>
   1.158 +
   1.159 +  <body xmlns="http://www.w3.org/1999/xhtml">
   1.160 +    <img id="favicon" onload="loadEventHandler();"/>
   1.161 +    <p id="display"></p>
   1.162 +    <div id="content" style="display:none;"></div>
   1.163 +    <pre id="test"></pre>
   1.164 +  </body>
   1.165 +</window>

mercurial