toolkit/components/places/tests/favicons/head_favicons.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/favicons/head_favicons.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,96 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     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 +const Ci = Components.interfaces;
    1.10 +const Cc = Components.classes;
    1.11 +const Cr = Components.results;
    1.12 +const Cu = Components.utils;
    1.13 +
    1.14 +Cu.import("resource://gre/modules/Services.jsm");
    1.15 +
    1.16 +// Import common head.
    1.17 +let (commonFile = do_get_file("../head_common.js", false)) {
    1.18 +  let uri = Services.io.newFileURI(commonFile);
    1.19 +  Services.scriptloader.loadSubScript(uri.spec, this);
    1.20 +}
    1.21 +
    1.22 +// Put any other stuff relative to this test folder below.
    1.23 +
    1.24 +
    1.25 +// This error icon must stay in sync with FAVICON_ERRORPAGE_URL in
    1.26 +// nsIFaviconService.idl, aboutCertError.xhtml and netError.xhtml.
    1.27 +const FAVICON_ERRORPAGE_URI =
    1.28 +  NetUtil.newURI("chrome://global/skin/icons/warning-16.png");
    1.29 +
    1.30 +/**
    1.31 + * Waits for the first OnPageChanged notification for ATTRIBUTE_FAVICON, and
    1.32 + * verifies that it matches the expected page URI and associated favicon URI.
    1.33 + *
    1.34 + * This function also double-checks the GUID parameter of the notification.
    1.35 + *
    1.36 + * @param aExpectedPageURI
    1.37 + *        nsIURI object of the page whose favicon should change.
    1.38 + * @param aExpectedFaviconURI
    1.39 + *        nsIURI object of the newly associated favicon.
    1.40 + * @param aCallback
    1.41 + *        This function is called after the check finished.
    1.42 + */
    1.43 +function waitForFaviconChanged(aExpectedPageURI, aExpectedFaviconURI,
    1.44 +                               aCallback) {
    1.45 +  let historyObserver = {
    1.46 +    __proto__: NavHistoryObserver.prototype,
    1.47 +    onPageChanged: function WFFC_onPageChanged(aURI, aWhat, aValue, aGUID) {
    1.48 +      if (aWhat != Ci.nsINavHistoryObserver.ATTRIBUTE_FAVICON) {
    1.49 +        return;
    1.50 +      }
    1.51 +      PlacesUtils.history.removeObserver(this);
    1.52 +
    1.53 +      do_check_true(aURI.equals(aExpectedPageURI));
    1.54 +      do_check_eq(aValue, aExpectedFaviconURI.spec);
    1.55 +      do_check_guid_for_uri(aURI, aGUID);
    1.56 +      aCallback();
    1.57 +    }
    1.58 +  };
    1.59 +  PlacesUtils.history.addObserver(historyObserver, false);
    1.60 +}
    1.61 +
    1.62 +/**
    1.63 + * Checks that the favicon for the given page matches the provided data.
    1.64 + *
    1.65 + * @param aPageURI
    1.66 + *        nsIURI object for the page to check.
    1.67 + * @param aExpectedMimeType
    1.68 + *        Expected MIME type of the icon, for example "image/png".
    1.69 + * @param aExpectedData
    1.70 + *        Expected icon data, expressed as an array of byte values.
    1.71 + * @param aCallback
    1.72 + *        This function is called after the check finished.
    1.73 + */
    1.74 +function checkFaviconDataForPage(aPageURI, aExpectedMimeType, aExpectedData,
    1.75 +                                 aCallback) {
    1.76 +  PlacesUtils.favicons.getFaviconDataForPage(aPageURI,
    1.77 +    function (aURI, aDataLen, aData, aMimeType) {
    1.78 +      do_check_eq(aExpectedMimeType, aMimeType);
    1.79 +      do_check_true(compareArrays(aExpectedData, aData));
    1.80 +      do_check_guid_for_uri(aPageURI);
    1.81 +      aCallback();
    1.82 +    });
    1.83 +}
    1.84 +
    1.85 +/**
    1.86 + * Checks that the given page has no associated favicon.
    1.87 + *
    1.88 + * @param aPageURI
    1.89 + *        nsIURI object for the page to check.
    1.90 + * @param aCallback
    1.91 + *        This function is called after the check finished.
    1.92 + */
    1.93 +function checkFaviconMissingForPage(aPageURI, aCallback) {
    1.94 +  PlacesUtils.favicons.getFaviconURLForPage(aPageURI,
    1.95 +    function (aURI, aDataLen, aData, aMimeType) {
    1.96 +      do_check_true(aURI === null);
    1.97 +      aCallback();
    1.98 +    });
    1.99 +}

mercurial