Bug 484263 - Most visited, recently bookmarked and recent tags folder re-added when they are upgraded, r=dietrich

This commit is contained in:
Marco Bonardo 2009-03-26 13:58:32 +01:00
parent c4e7adf2be
commit 90ce4bf6ac
3 changed files with 254 additions and 7 deletions

View File

@ -808,6 +808,12 @@ BrowserGlue.prototype = {
},
ensurePlacesDefaultQueriesInitialized: function() {
// This is actual version of the smart bookmarks, must be increased every
// time smart bookmarks change.
// When adding a new smart bookmark below, its newInVersion property must
// be set to the version it has been added in, we will compare its value
// to users' smartBookmarksVersion and add new smart bookmarks without
// recreating old deleted ones.
const SMART_BOOKMARKS_VERSION = 2;
const SMART_BOOKMARKS_ANNO = "Places/SmartBookmark";
const SMART_BOOKMARKS_PREF = "browser.places.smartBookmarksVersion";
@ -820,7 +826,7 @@ BrowserGlue.prototype = {
var smartBookmarksCurrentVersion = 0;
try {
smartBookmarksCurrentVersion = this._prefs.getIntPref(SMART_BOOKMARKS_PREF);
} catch(ex) {}
} catch(ex) { /* no version set, new profile */ }
// bail out if we don't have to create or update Smart Bookmarks
if (smartBookmarksCurrentVersion == -1 ||
@ -858,7 +864,8 @@ BrowserGlue.prototype = {
Ci.nsINavHistoryQueryOptions.SORT_BY_VISITCOUNT_DESCENDING +
"&maxResults=" + MAX_RESULTS),
parent: bmsvc.toolbarFolder,
position: bookmarksToolbarIndex++};
position: bookmarksToolbarIndex++,
newInVersion: 1 };
smartBookmarks.push(smart);
// RECENTLY BOOKMARKED
@ -876,7 +883,8 @@ BrowserGlue.prototype = {
"&maxResults=" + MAX_RESULTS +
"&excludeQueries=1"),
parent: bmsvc.bookmarksMenuFolder,
position: bookmarksMenuIndex++};
position: bookmarksMenuIndex++,
newInVersion: 1 };
smartBookmarks.push(smart);
// RECENT TAGS
@ -890,15 +898,19 @@ BrowserGlue.prototype = {
Ci.nsINavHistoryQueryOptions.SORT_BY_LASTMODIFIED_DESCENDING +
"&maxResults=" + MAX_RESULTS),
parent: bmsvc.bookmarksMenuFolder,
position: bookmarksMenuIndex++};
position: bookmarksMenuIndex++,
newInVersion: 1 };
smartBookmarks.push(smart);
var smartBookmarkItemIds = annosvc.getItemsWithAnnotation(SMART_BOOKMARKS_ANNO, {});
// set current itemId, parent and position if Smart Bookmark exists
// Set current itemId, parent and position if Smart Bookmark exists,
// we will use these informations to create the new version at the same
// position.
for each(var itemId in smartBookmarkItemIds) {
var queryId = annosvc.getItemAnnotation(itemId, SMART_BOOKMARKS_ANNO);
for (var i = 0; i < smartBookmarks.length; i++){
if (smartBookmarks[i].queryId == queryId) {
smartBookmarks[i].found = true;
smartBookmarks[i].itemId = itemId;
smartBookmarks[i].parent = bmsvc.getFolderIdForItem(itemId);
smartBookmarks[i].position = bmsvc.getItemIndex(itemId);
@ -916,6 +928,14 @@ BrowserGlue.prototype = {
// create smart bookmarks
for each(var smartBookmark in smartBookmarks) {
// We update or create only changed or new smart bookmarks.
// Also we respect user choices, so we won't try to create a smart
// bookmark if it has been removed.
if (smartBookmarksCurrentVersion > 0 &&
smartBookmark.newInVersion <= smartBookmarksCurrentVersion &&
!smartBookmark.found)
continue;
smartBookmark.itemId = bmsvc.insertBookmark(smartBookmark.parent,
smartBookmark.uri,
smartBookmark.position,
@ -927,7 +947,8 @@ BrowserGlue.prototype = {
// If we are creating all Smart Bookmarks from ground up, add a
// separator below them in the bookmarks menu.
if (smartBookmarkItemIds.length == 0)
if (smartBookmarksCurrentVersion == 0 &&
smartBookmarkItemIds.length == 0)
bmsvc.insertSeparator(bmsvc.bookmarksMenuFolder, bookmarksMenuIndex);
}
};

View File

@ -171,8 +171,11 @@ let gTestDir = do_get_cwd();
const FILENAME_BOOKMARKS_HTML = "bookmarks.html";
let backup_date = new Date().toLocaleFormat("%Y-%m-%d");
const FILENAME_BOOKMARKS_JSON = "bookmarks-" + backup_date + ".json";
// Number of smart bookmarks we have on the toolbar
// Smart bookmarks constants
const SMART_BOOKMARKS_VERSION = 2;
const SMART_BOOKMARKS_ON_TOOLBAR = 1;
const SMART_BOOKMARKS_ON_MENU = 2;
/**
* Creates a bookmarks.html file in the profile folder from a given source file.

View File

@ -0,0 +1,223 @@
/* -*- 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 Mozilla Corp.
* Portions created by the Initial Developer are Copyright (C) 2009
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Marco Bonardo <mak77@bonardo.net>
*
* 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 that nsBrowserGlue is correctly interpreting the preferences settable
* by the user or by other components.
*/
// Initialize browserGlue.
var bg = Cc["@mozilla.org/browser/browserglue;1"].
getService(Ci.nsIBrowserGlue);
// Initialize Places.
var hs = Cc["@mozilla.org/browser/nav-history-service;1"].
getService(Ci.nsINavHistoryService);
var bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
getService(Ci.nsINavBookmarksService);
// Get other services.
var ps = Cc["@mozilla.org/preferences-service;1"].
getService(Ci.nsIPrefBranch);
var os = Cc["@mozilla.org/observer-service;1"].
getService(Ci.nsIObserverService);
var as = Cc["@mozilla.org/browser/annotation-service;1"].
getService(Ci.nsIAnnotationService);
const PREF_SMART_BOOKMARKS_VERSION = "browser.places.smartBookmarksVersion";
const SMART_BOOKMARKS_ANNO = "Places/SmartBookmark";
const TOPIC_PLACES_INIT_COMPLETE = "places-init-complete";
var tests = [];
//------------------------------------------------------------------------------
tests.push({
description: "All smart bookmarks are created if smart bookmarks version is 0.",
exec: function() {
// Sanity check: we should not have any bookmark on the toolbar.
do_check_eq(bs.getIdForItemAt(bs.toolbarFolder, 0), -1);
// Sanity check: we should not have any bookmark on the menu.
do_check_eq(bs.getIdForItemAt(bs.bookmarksMenuFolder, 0), -1);
// Set preferences.
ps.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 0);
// Force nsBrowserGlue::_initPlaces().
os.notifyObservers(null, TOPIC_PLACES_INIT_COMPLETE, null);
// Count items on toolbar.
do_check_eq(countFolderChildren(bs.toolbarFolder), SMART_BOOKMARKS_ON_TOOLBAR);
// Count items on menu (+1 for the separator).
do_check_eq(countFolderChildren(bs.bookmarksMenuFolder), SMART_BOOKMARKS_ON_MENU + 1);
// Check version has been updated.
do_check_eq(ps.getIntPref(PREF_SMART_BOOKMARKS_VERSION), SMART_BOOKMARKS_VERSION);
next_test();
}
});
//------------------------------------------------------------------------------
tests.push({
description: "An existing smart bookmark is replaced when version changes.",
exec: function() {
// Sanity check: we have a smart bookmark on the toolbar.
var itemId = bs.getIdForItemAt(bs.toolbarFolder, 0);
do_check_neq(itemId, -1);
do_check_true(as.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO));
// Change its title.
bs.setItemTitle(itemId, "new title");
do_check_eq(bs.getItemTitle(itemId), "new title");
// Set preferences.
ps.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 1);
// Force nsBrowserGlue::_initPlaces().
os.notifyObservers(null, TOPIC_PLACES_INIT_COMPLETE, null);
// Count items on toolbar.
do_check_eq(countFolderChildren(bs.toolbarFolder), SMART_BOOKMARKS_ON_TOOLBAR);
// Count items on menu (+1 for the separator).
do_check_eq(countFolderChildren(bs.bookmarksMenuFolder), SMART_BOOKMARKS_ON_MENU + 1);
// check smart bookmark has been replaced, itemId has changed.
itemId = bs.getIdForItemAt(bs.toolbarFolder, 0);
do_check_neq(itemId, -1);
do_check_neq(bs.getItemTitle(itemId), "new title");
do_check_true(as.itemHasAnnotation(itemId, SMART_BOOKMARKS_ANNO));
// Check version has been updated.
do_check_eq(ps.getIntPref(PREF_SMART_BOOKMARKS_VERSION), SMART_BOOKMARKS_VERSION);
next_test();
}
});
//------------------------------------------------------------------------------
tests.push({
description: "An explicitly removed smart bookmark should not be recreated.",
exec: function() {
// Set preferences.
ps.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 1);
// Remove toolbar's smart bookmarks
bs.removeFolderChildren(bs.toolbarFolder);
// Force nsBrowserGlue::_initPlaces().
os.notifyObservers(null, TOPIC_PLACES_INIT_COMPLETE, null);
// Count items on toolbar, we should not have recreated the smart bookmark.
do_check_eq(countFolderChildren(bs.toolbarFolder), 0);
// Count items on menu (+1 for the separator).
do_check_eq(countFolderChildren(bs.bookmarksMenuFolder), SMART_BOOKMARKS_ON_MENU + 1);
// Check version has been updated.
do_check_eq(ps.getIntPref(PREF_SMART_BOOKMARKS_VERSION), SMART_BOOKMARKS_VERSION);
next_test();
}
});
//------------------------------------------------------------------------------
tests.push({
description: "Even if a smart bookmark has been removed recreate it if version is 0.",
exec: function() {
// Set preferences.
ps.setIntPref(PREF_SMART_BOOKMARKS_VERSION, 0);
// Force nsBrowserGlue::_initPlaces().
os.notifyObservers(null, TOPIC_PLACES_INIT_COMPLETE, null);
// Count items on toolbar, we should not have recreated the smart bookmark.
do_check_eq(countFolderChildren(bs.toolbarFolder), SMART_BOOKMARKS_ON_TOOLBAR);
// Count items on menu (+1 for the separator).
do_check_eq(countFolderChildren(bs.bookmarksMenuFolder), SMART_BOOKMARKS_ON_MENU + 1);
// Check version has been updated.
do_check_eq(ps.getIntPref(PREF_SMART_BOOKMARKS_VERSION), SMART_BOOKMARKS_VERSION);
finish_test();
}
});
//------------------------------------------------------------------------------
function countFolderChildren(aFolderItemId) {
var query = hs.getNewQuery();
query.setFolders([aFolderItemId], 1);
var options = hs.getNewQueryOptions();
var rootNode = hs.executeQuery(query, options).root;
rootNode.containerOpen = true;
var cc = rootNode.childCount;
rootNode.containerOpen = false;
return cc;
}
function finish_test() {
// Clean up database from all bookmarks.
remove_all_bookmarks();
// Simulate application closing to remove the idle observer and avoid leaks.
os.notifyObservers(null, "quit-application-granted", null);
do_test_finished();
}
var testIndex = 0;
function next_test() {
// Simulate application closing to remove the idle observer and avoid leaks.
os.notifyObservers(null, "quit-application-granted", null);
// nsBrowserGlue stops observing topics after first notification,
// so we add back the observer to test additional runs.
os.addObserver(bg, TOPIC_PLACES_INIT_COMPLETE, false);
// Execute next test.
var test = tests.shift();
dump("\nTEST " + (++testIndex) + ": " + test.description);
test.exec();
}
function run_test() {
// Clean up database from all bookmarks.
remove_all_bookmarks();
// Kick-off tests.
do_test_pending();
next_test();
}