From 59fc644e1e0b53b64f5fa74e4a84a17e51ae50e7 Mon Sep 17 00:00:00 2001 From: Marco Bonardo Date: Thu, 25 Mar 2010 12:34:53 +0100 Subject: [PATCH] Bug 553489 - Part11: Test for cases where an icon should not be set. r=sdwilsh --- .../unit/test_doSetAndLoadFaviconForPage.js | 102 ++++----- ...est_doSetAndLoadFaviconForPage_failures.js | 209 ++++++++++++++++++ 2 files changed, 248 insertions(+), 63 deletions(-) create mode 100644 toolkit/components/places/tests/unit/test_doSetAndLoadFaviconForPage_failures.js diff --git a/toolkit/components/places/tests/unit/test_doSetAndLoadFaviconForPage.js b/toolkit/components/places/tests/unit/test_doSetAndLoadFaviconForPage.js index c827ca72233..5160cf0f60b 100644 --- a/toolkit/components/places/tests/unit/test_doSetAndLoadFaviconForPage.js +++ b/toolkit/components/places/tests/unit/test_doSetAndLoadFaviconForPage.js @@ -1,5 +1,43 @@ +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * vim:set ts=2 sw=2 sts=2 et: + * ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Places unit test code. + * + * The Initial Developer of the Original Code is the Mozilla Foundation. + * Portions created by the Initial Developer are Copyright (C) 2009 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Gavin Sharp (original author) + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + /* - * Tests for nsIFaviconService + * Tests for nsIFaviconService::SetAndLoadFaviconForPage() */ var iconsvc = Cc["@mozilla.org/browser/favicon-service;1"]. @@ -24,17 +62,6 @@ function checkAddSucceeded(pageURI, mimetype, data) { do_check_true(compareArrays(data, outData)); } -// TODO bug 527036: There is no way to know whether setting a favicon failed. -//function checkAddFailed(pageURI) { -// var savedFaviconURI = null; -// try { -// savedFaviconURI = iconsvc.getFaviconForPage(pageURI); -// } catch (ex) {} -// -// // ensure that the addition failed -// do_check_eq(savedFaviconURI, null); -//} - var favicons = [ { uri: iosvc.newFileURI(do_get_file("favicon-normal32.png")), @@ -57,18 +84,6 @@ var tests = [ } }, - // TODO bug 527036: There is no way to know whether setting a favicon failed. - //{ - // desc: "test setAndLoadFaviconForPage for about: URIs", - // pageURI: uri("about:test1"), - // go: function go2() { - // iconsvc.setAndLoadFaviconForPage(this.pageURI, favicons[0].uri, true); - // }, - // check: function check2() { - // checkAddFailed(this.pageURI); - // } - //}, - { desc: "test setAndLoadFaviconForPage for bookmarked about: URIs", pageURI: uri("about:test2"), @@ -84,26 +99,6 @@ var tests = [ } }, - // TODO bug 527036: There is no way to know whether setting a favicon failed. - //{ - // desc: "test setAndLoadFaviconForPage with history disabled", - // pageURI: uri("http://test2.bar/"), - // go: function go4() { - // // disable history - // var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch); - // prefs.setBoolPref("places.history.enabled", false); - // - // iconsvc.setAndLoadFaviconForPage(this.pageURI, favicons[0].uri, true); - // - // try { - // prefs.clearUserPref("places.history.enabled"); - // } catch (ex) {} - // }, - // check: function check4() { - // checkAddFailed(this.pageURI); - // } - //}, - { desc: "test setAndLoadFaviconForPage with history disabled for bookmarked URI", pageURI: uri("http://test3.bar/"), @@ -127,25 +122,6 @@ var tests = [ } }, - // TODO bug 527036: There is no way to know whether setting a favicon failed. - //{ - // desc: "test setAndLoadFaviconForPage in PB mode for non-bookmarked URI", - // pageURI: uri("http://test5.bar/"), - // favicon: favicons[0], - // go: function go7() { - // // enable PB - // var pb = Cc["@mozilla.org/privatebrowsing;1"]. - // getService(Ci.nsIPrivateBrowsingService); - // pb.privateBrowsingEnabled = true; - // - // iconsvc.setAndLoadFaviconForPage(this.pageURI, this.favicon.uri, true); - // - // pb.privateBrowsingEnabled = false; - // }, - // check: function check7() { - // checkAddFailed(this.pageURI); - // } - //}, ]; if ("@mozilla.org/privatebrowsing;1" in Cc) { diff --git a/toolkit/components/places/tests/unit/test_doSetAndLoadFaviconForPage_failures.js b/toolkit/components/places/tests/unit/test_doSetAndLoadFaviconForPage_failures.js new file mode 100644 index 00000000000..0d3d5bacf60 --- /dev/null +++ b/toolkit/components/places/tests/unit/test_doSetAndLoadFaviconForPage_failures.js @@ -0,0 +1,209 @@ +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * vim:set ts=2 sw=2 sts=2 et: + * ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is Places unit test code. + * + * The Initial Developer of the Original Code is the Mozilla Foundation. + * Portions created by the Initial Developer are Copyright (C) 2010 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Marco Bonardo (Original Author) + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +/* + * Tests failing cases of setAndLoadFaviconForPage + */ + +const PB_KEEP_SESSION_PREF = "browser.privatebrowsing.keep_current_session"; + +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); + +XPCOMUtils.defineLazyServiceGetter(this, "iconsvc", + "@mozilla.org/browser/favicon-service;1", + "nsIFaviconService"); +XPCOMUtils.defineLazyServiceGetter(this, "hs", + "@mozilla.org/browser/nav-history-service;1", + "nsINavHistoryService"); +XPCOMUtils.defineLazyServiceGetter(this, "pb", + "@mozilla.org/privatebrowsing;1", + "nsIPrivateBrowsingService"); +XPCOMUtils.defineLazyServiceGetter(this, "prefs", + "@mozilla.org/preferences-service;1", + "nsIPrefBranch"); + +let favicons = [ + { + uri: iosvc.newFileURI(do_get_file("favicon-normal16.png")), + data: readFileData(do_get_file("favicon-normal16.png")), + mimetype: "image/png", + size: 286 + }, + { + uri: iosvc.newFileURI(do_get_file("favicon-normal32.png")), + data: readFileData(do_get_file("favicon-normal32.png")), + mimetype: "image/png", + size: 344 + }, +]; + +let tests = [ + + { + desc: "test setAndLoadFaviconForPage for about: URIs", + pageURI: uri("about:test1"), + go: function go1() { + + iconsvc.setAndLoadFaviconForPage(this.pageURI, favicons[0].uri, true); + }, + clean: function clean1() {} + }, + + { + desc: "test setAndLoadFaviconForPage with history disabled", + pageURI: uri("http://test2.bar/"), + go: function go2() { + // Temporarily disable history. + prefs.setBoolPref("places.history.enabled", false); + + iconsvc.setAndLoadFaviconForPage(this.pageURI, favicons[0].uri, true); + }, + clean: function clean2() { + try { + prefs.clearUserPref("places.history.enabled"); + } catch (ex) {} + } + }, + + { + desc: "test setAndLoadFaviconForPage in PB mode for non-bookmarked URI", + pageURI: uri("http://test3.bar/"), + go: function go3() { + if (!("@mozilla.org/privatebrowsing;1" in Cc)) + return; + + // Enable PB mode. + prefs.setBoolPref(PB_KEEP_SESSION_PREF, true); + pb.privateBrowsingEnabled = true; + + iconsvc.setAndLoadFaviconForPage(this.pageURI, favicons[0].uri, true); + }, + clean: function clean3() { + if (!("@mozilla.org/privatebrowsing;1" in Cc)) + return; + + pb.privateBrowsingEnabled = false; + } + }, + + { // This is a valid icon set test, that will cause the closing notification. + desc: "test setAndLoadFaviconForPage for valid history uri", + pageURI: uri("http://test4.bar/"), + go: function go4() { + iconsvc.setAndLoadFaviconForPage(this.pageURI, favicons[1].uri, true); + }, + clean: function clean4() {} + }, + +]; + +let historyObserver = { + onBeginUpdateBatch: function() {}, + onEndUpdateBatch: function() {}, + onVisit: function() {}, + onTitleChanged: function() {}, + onBeforeDeleteURI: function() {}, + onDeleteURI: function() {}, + onClearHistory: function() {}, + onDeleteVisits: function() {}, + + onPageChanged: function historyObserver_onPageChanged(pageURI, what, value) { + if (what != Ci.nsINavHistoryObserver.ATTRIBUTE_FAVICON) + return; + + // dump tables, useful if the test fails. + dump_table("moz_places_temp"); + dump_table("moz_favicons"); + + // Ensure we have been called by the last test. + do_check_true(pageURI.equals(uri("http://test4.bar/"))); + + // Ensure there is only one entry in favicons table. + let stmt = DBConn().createStatement( + "SELECT url FROM moz_favicons" + ); + let c = 0; + try { + while (stmt.executeStep()) { + do_check_eq(stmt.row.url, favicons[1].uri.spec); + c++; + } + } + finally { + stmt.finalize(); + } + do_check_eq(c, 1); + + do_test_finished(); + }, + + QueryInterface: XPCOMUtils.generateQI([Ci.nsINavHistoryObserver]) +}; + +let currentTest = null; + +function run_test() { + do_test_pending(); + + // check that the favicon loaded correctly + do_check_eq(favicons[0].data.length, favicons[0].size); + do_check_eq(favicons[1].data.length, favicons[1].size); + + // Observe history for onPageChanged notification. + hs.addObserver(historyObserver, false); + + // We run all the tests, then we wait for an onPageChanged notification, + // ideally only the last test is successful, so it should be the only + // notification we get. Once we get it, test finishes. + runNextTest(); +}; + +function runNextTest() { + if (currentTest) { + currentTest.clean(); + } + + if (tests.length) { + currentTest = tests.shift(); + print(currentTest.desc); + currentTest.go(); + // Wait some time before calling the next test, this is needed to avoid + // invoking clean() too early. The first async step should run at least. + do_timeout(500, runNextTest); + } +}