toolkit/components/places/tests/browser/browser_favicon_setAndFetchFaviconForPage.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/browser/browser_favicon_setAndFetchFaviconForPage.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,145 @@
     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 +// This file tests the normal operation of setAndFetchFaviconForPage.
     1.9 +function test() {
    1.10 +  // Initialization
    1.11 +  waitForExplicitFinish();
    1.12 +  let windowsToClose = [];
    1.13 +  let testURI = "https://www.mozilla.org/en-US/";
    1.14 +  let favIconLocation =
    1.15 +    "http://example.org/tests/toolkit/components/places/tests/browser/favicon-normal32.png";
    1.16 +  let favIconURI = NetUtil.newURI(favIconLocation);
    1.17 +  let favIconMimeType= "image/png";
    1.18 +  let pageURI;
    1.19 +  let favIconData;
    1.20 +
    1.21 +  function testOnWindow(aOptions, aCallback) {
    1.22 +    whenNewWindowLoaded(aOptions, function(aWin) {
    1.23 +      windowsToClose.push(aWin);
    1.24 +      executeSoon(function() aCallback(aWin));
    1.25 +    });
    1.26 +  };
    1.27 +
    1.28 +  // This function is called after calling finish() on the test.
    1.29 +  registerCleanupFunction(function() {
    1.30 +    windowsToClose.forEach(function(aWin) {
    1.31 +      aWin.close();
    1.32 +    });
    1.33 +  });
    1.34 +
    1.35 +  function getIconFile(aCallback) {
    1.36 +    NetUtil.asyncFetch(favIconLocation, function(inputStream, status) {
    1.37 +      if (!Components.isSuccessCode(status)) {
    1.38 +        ok(false, "Could not get the icon file");
    1.39 +        // Handle error.
    1.40 +        return;
    1.41 +      }
    1.42 +
    1.43 +      // Check the returned size versus the expected size.
    1.44 +      let size = inputStream.available();
    1.45 +      favIconData = NetUtil.readInputStreamToString(inputStream, size);
    1.46 +      is(size, favIconData.length, "Check correct icon size");
    1.47 +      // Check that the favicon loaded correctly before starting the actual tests.
    1.48 +      is(favIconData.length, 344, "Check correct icon length (344)");
    1.49 +
    1.50 +      if (aCallback) {
    1.51 +        aCallback();
    1.52 +      } else {
    1.53 +        finish();
    1.54 +      }
    1.55 +    });
    1.56 +  }
    1.57 +
    1.58 +  function testNormal(aWindow, aCallback) {
    1.59 +    pageURI = NetUtil.newURI("http://example.com/normal");
    1.60 +    waitForFaviconChanged(pageURI, favIconURI, aWindow,
    1.61 +      function testNormalCallback() {
    1.62 +        checkFaviconDataForPage(pageURI, favIconMimeType, favIconData, aWindow,
    1.63 +          aCallback);
    1.64 +      }
    1.65 +    );
    1.66 +
    1.67 +    addVisits({uri: pageURI, transition: TRANSITION_TYPED}, aWindow,
    1.68 +      function () {
    1.69 +        aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage(pageURI,
    1.70 +          favIconURI, true, aWindow.PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE);
    1.71 +      }
    1.72 +    );
    1.73 +  }
    1.74 +
    1.75 +  function testAboutURIBookmarked(aWindow, aCallback) {
    1.76 +    pageURI = NetUtil.newURI("about:testAboutURI_bookmarked");
    1.77 +    waitForFaviconChanged(pageURI, favIconURI, aWindow,
    1.78 +      function testAboutURIBookmarkedCallback() {
    1.79 +        checkFaviconDataForPage(pageURI, favIconMimeType, favIconData, aWindow,
    1.80 +          aCallback);
    1.81 +      }
    1.82 +    );
    1.83 +
    1.84 +    aWindow.PlacesUtils.bookmarks.insertBookmark(
    1.85 +      aWindow.PlacesUtils.unfiledBookmarksFolderId, pageURI,
    1.86 +      aWindow.PlacesUtils.bookmarks.DEFAULT_INDEX, pageURI.spec);
    1.87 +    aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage(pageURI, favIconURI,
    1.88 +      true, aWindow.PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE);
    1.89 +  }
    1.90 +
    1.91 +  function testPrivateBrowsingBookmarked(aWindow, aCallback) {
    1.92 +    pageURI = NetUtil.newURI("http://example.com/privateBrowsing_bookmarked");
    1.93 +    waitForFaviconChanged(pageURI, favIconURI, aWindow,
    1.94 +      function testPrivateBrowsingBookmarkedCallback() {
    1.95 +        checkFaviconDataForPage(pageURI, favIconMimeType, favIconData, aWindow,
    1.96 +          aCallback);
    1.97 +      }
    1.98 +    );
    1.99 +
   1.100 +    aWindow.PlacesUtils.bookmarks.insertBookmark(
   1.101 +      aWindow.PlacesUtils.unfiledBookmarksFolderId, pageURI,
   1.102 +      aWindow.PlacesUtils.bookmarks.DEFAULT_INDEX, pageURI.spec);
   1.103 +    aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage(pageURI, favIconURI,
   1.104 +      true, aWindow.PlacesUtils.favicons.FAVICON_LOAD_PRIVATE);
   1.105 +  }
   1.106 +
   1.107 +  function testDisabledHistoryBookmarked(aWindow, aCallback) {
   1.108 +    pageURI = NetUtil.newURI("http://example.com/disabledHistory_bookmarked");
   1.109 +    waitForFaviconChanged(pageURI, favIconURI, aWindow,
   1.110 +      function testDisabledHistoryBookmarkedCallback() {
   1.111 +        checkFaviconDataForPage(pageURI, favIconMimeType, favIconData, aWindow,
   1.112 +          aCallback);
   1.113 +      }
   1.114 +    );
   1.115 +
   1.116 +    // Disable history while changing the favicon.
   1.117 +    aWindow.Services.prefs.setBoolPref("places.history.enabled", false);
   1.118 +
   1.119 +    aWindow.PlacesUtils.bookmarks.insertBookmark(
   1.120 +      aWindow.PlacesUtils.unfiledBookmarksFolderId, pageURI,
   1.121 +      aWindow.PlacesUtils.bookmarks.DEFAULT_INDEX, pageURI.spec);
   1.122 +    aWindow.PlacesUtils.favicons.setAndFetchFaviconForPage(pageURI, favIconURI,
   1.123 +      true, aWindow.PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE);
   1.124 +
   1.125 +    // The setAndFetchFaviconForPage function calls CanAddURI synchronously, thus
   1.126 +    // we can set the preference back to true immediately.  We don't clear the
   1.127 +    // preference because not all products enable Places by default.
   1.128 +    aWindow.Services.prefs.setBoolPref("places.history.enabled", true);
   1.129 +  }
   1.130 +
   1.131 +  getIconFile(function () {
   1.132 +    testOnWindow({}, function(aWin) {
   1.133 +      testNormal(aWin, function () {
   1.134 +        testOnWindow({}, function(aWin) {
   1.135 +          testAboutURIBookmarked(aWin, function () {
   1.136 +            testOnWindow({private: true}, function(aWin) {
   1.137 +              testPrivateBrowsingBookmarked(aWin, function () {
   1.138 +                testOnWindow({}, function(aWin) {
   1.139 +                  testDisabledHistoryBookmarked(aWin, finish);
   1.140 +                });
   1.141 +              });
   1.142 +            });
   1.143 +          });
   1.144 +        });
   1.145 +      });
   1.146 +    });
   1.147 +  });
   1.148 +}

mercurial