toolkit/components/places/tests/unit/test_418643_removeFolderChildren.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 // Get services.
michael@0 8 try {
michael@0 9 var histSvc = Cc["@mozilla.org/browser/nav-history-service;1"].
michael@0 10 getService(Ci.nsINavHistoryService);
michael@0 11 var bmSvc = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
michael@0 12 getService(Ci.nsINavBookmarksService);
michael@0 13 var annoSvc = Cc["@mozilla.org/browser/annotation-service;1"]
michael@0 14 .getService(Ci.nsIAnnotationService);
michael@0 15 } catch(ex) {
michael@0 16 do_throw("Could not get services\n");
michael@0 17 }
michael@0 18
michael@0 19 var validAnnoName = "validAnno";
michael@0 20 var validItemName = "validItem";
michael@0 21 var deletedAnnoName = "deletedAnno";
michael@0 22 var deletedItemName = "deletedItem";
michael@0 23 var bookmarkedURI = uri("http://www.mozilla.org/");
michael@0 24 // set lastModified to the past to prevent VM timing bugs
michael@0 25 var pastDate = Date.now() * 1000 - 1;
michael@0 26 var deletedBookmarkIds = [];
michael@0 27
michael@0 28 // bookmarks observer
michael@0 29 var observer = {
michael@0 30 // cached ordered array of notified items
michael@0 31 _onItemRemovedItemIds: [],
michael@0 32 onItemRemoved: function(aItemId, aParentId, aIndex) {
michael@0 33 // We should first get notifications for children, then for their parent
michael@0 34 do_check_eq(this._onItemRemovedItemIds.indexOf(aParentId), -1);
michael@0 35 // Ensure we are not wrongly removing 1 level up
michael@0 36 do_check_neq(aParentId, bmSvc.toolbarFolder);
michael@0 37 // Removed item must be one of those we have manually deleted
michael@0 38 do_check_neq(deletedBookmarkIds.indexOf(aItemId), -1);
michael@0 39 this._onItemRemovedItemIds.push(aItemId);
michael@0 40 },
michael@0 41
michael@0 42 QueryInterface: function(aIID) {
michael@0 43 if (aIID.equals(Ci.nsINavBookmarkObserver) ||
michael@0 44 aIID.equals(Ci.nsISupports)) {
michael@0 45 return this;
michael@0 46 }
michael@0 47 throw Cr.NS_ERROR_NO_INTERFACE;
michael@0 48 }
michael@0 49 };
michael@0 50
michael@0 51 bmSvc.addObserver(observer, false);
michael@0 52
michael@0 53 function add_bookmarks() {
michael@0 54 // This is the folder we will cleanup
michael@0 55 var validFolderId = bmSvc.createFolder(bmSvc.toolbarFolder,
michael@0 56 validItemName,
michael@0 57 bmSvc.DEFAULT_INDEX);
michael@0 58 annoSvc.setItemAnnotation(validFolderId, validAnnoName,
michael@0 59 "annotation", 0,
michael@0 60 annoSvc.EXPIRE_NEVER);
michael@0 61 bmSvc.setItemLastModified(validFolderId, pastDate);
michael@0 62
michael@0 63 // This bookmark should not be deleted
michael@0 64 var validItemId = bmSvc.insertBookmark(bmSvc.toolbarFolder,
michael@0 65 bookmarkedURI,
michael@0 66 bmSvc.DEFAULT_INDEX,
michael@0 67 validItemName);
michael@0 68 annoSvc.setItemAnnotation(validItemId, validAnnoName,
michael@0 69 "annotation", 0, annoSvc.EXPIRE_NEVER);
michael@0 70
michael@0 71 // The following contents should be deleted
michael@0 72 var deletedItemId = bmSvc.insertBookmark(validFolderId,
michael@0 73 bookmarkedURI,
michael@0 74 bmSvc.DEFAULT_INDEX,
michael@0 75 deletedItemName);
michael@0 76 annoSvc.setItemAnnotation(deletedItemId, deletedAnnoName,
michael@0 77 "annotation", 0, annoSvc.EXPIRE_NEVER);
michael@0 78 deletedBookmarkIds.push(deletedItemId);
michael@0 79
michael@0 80 var internalFolderId = bmSvc.createFolder(validFolderId,
michael@0 81 deletedItemName,
michael@0 82 bmSvc.DEFAULT_INDEX);
michael@0 83 annoSvc.setItemAnnotation(internalFolderId, deletedAnnoName,
michael@0 84 "annotation", 0, annoSvc.EXPIRE_NEVER);
michael@0 85 deletedBookmarkIds.push(internalFolderId);
michael@0 86
michael@0 87 deletedItemId = bmSvc.insertBookmark(internalFolderId,
michael@0 88 bookmarkedURI,
michael@0 89 bmSvc.DEFAULT_INDEX,
michael@0 90 deletedItemName);
michael@0 91 annoSvc.setItemAnnotation(deletedItemId, deletedAnnoName,
michael@0 92 "annotation", 0, annoSvc.EXPIRE_NEVER);
michael@0 93 deletedBookmarkIds.push(deletedItemId);
michael@0 94
michael@0 95 return validFolderId;
michael@0 96 }
michael@0 97
michael@0 98 function check_bookmarks(aFolderId) {
michael@0 99 // check that we still have valid bookmarks
michael@0 100 var bookmarks = bmSvc.getBookmarkIdsForURI(bookmarkedURI);
michael@0 101 for(var i = 0; i < bookmarks.length; i++) {
michael@0 102 do_check_eq(bmSvc.getItemTitle(bookmarks[i]), validItemName);
michael@0 103 do_check_true(annoSvc.itemHasAnnotation(bookmarks[i],validAnnoName));
michael@0 104 }
michael@0 105
michael@0 106 // check that folder exists and has still its annotation
michael@0 107 do_check_eq(bmSvc.getItemTitle(aFolderId), validItemName);
michael@0 108 do_check_true(annoSvc.itemHasAnnotation(aFolderId, validAnnoName));
michael@0 109
michael@0 110 // check that folder is empty
michael@0 111 var options = histSvc.getNewQueryOptions();
michael@0 112 var query = histSvc.getNewQuery();
michael@0 113 query.setFolders([aFolderId], 1);
michael@0 114 var result = histSvc.executeQuery(query, options);
michael@0 115 var root = result.root;
michael@0 116 root.containerOpen = true;
michael@0 117 do_check_eq(root.childCount, 0);
michael@0 118 root.containerOpen = false;
michael@0 119
michael@0 120 // test that lastModified got updated
michael@0 121 do_check_true(pastDate < bmSvc.getItemLastModified(aFolderId));
michael@0 122
michael@0 123 // test that all children have been deleted, we use annos for that
michael@0 124 var deletedItems = annoSvc.getItemsWithAnnotation(deletedAnnoName);
michael@0 125 do_check_eq(deletedItems.length, 0);
michael@0 126
michael@0 127 // test that observer has been called for (and only for) deleted items
michael@0 128 do_check_eq(observer._onItemRemovedItemIds.length, deletedBookmarkIds.length);
michael@0 129
michael@0 130 // Sanity check: all roots should be intact
michael@0 131 do_check_eq(bmSvc.getFolderIdForItem(bmSvc.placesRoot), 0);
michael@0 132 do_check_eq(bmSvc.getFolderIdForItem(bmSvc.bookmarksMenuFolder), bmSvc.placesRoot);
michael@0 133 do_check_eq(bmSvc.getFolderIdForItem(bmSvc.tagsFolder), bmSvc.placesRoot);
michael@0 134 do_check_eq(bmSvc.getFolderIdForItem(bmSvc.unfiledBookmarksFolder), bmSvc.placesRoot);
michael@0 135 do_check_eq(bmSvc.getFolderIdForItem(bmSvc.toolbarFolder), bmSvc.placesRoot);
michael@0 136 }
michael@0 137
michael@0 138 // main
michael@0 139 function run_test() {
michael@0 140 var folderId = add_bookmarks();
michael@0 141 bmSvc.removeFolderChildren(folderId);
michael@0 142 check_bookmarks(folderId);
michael@0 143 }

mercurial