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

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 Cu.import("resource://gre/modules/BookmarkJSONUtils.jsm");
michael@0 6
michael@0 7 function run_test() {
michael@0 8 run_next_test();
michael@0 9 }
michael@0 10
michael@0 11 const LOAD_IN_SIDEBAR_ANNO = "bookmarkProperties/loadInSidebar";
michael@0 12 const DESCRIPTION_ANNO = "bookmarkProperties/description";
michael@0 13
michael@0 14 // An object representing the contents of bookmarks.json.
michael@0 15 let test_bookmarks = {
michael@0 16 menu: [
michael@0 17 { title: "Mozilla Firefox",
michael@0 18 children: [
michael@0 19 { title: "Help and Tutorials",
michael@0 20 url: "http://en-us.www.mozilla.com/en-US/firefox/help/",
michael@0 21 icon: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAHWSURBVHjaYvz//z8DJQAggJiQOe/fv2fv7Oz8rays/N+VkfG/iYnJfyD/1+rVq7ffu3dPFpsBAAHEAHIBCJ85c8bN2Nj4vwsDw/8zQLwKiO8CcRoQu0DxqlWrdsHUwzBAAIGJmTNnPgYa9j8UqhFElwPxf2MIDeIrKSn9FwSJoRkAEEAM0DD4DzMAyPi/G+QKY4hh5WAXGf8PDQ0FGwJ22d27CjADAAIIrLmjo+MXA9R2kAHvGBA2wwx6B8W7od6CeQcggKCmCEL8bgwxYCbUIGTDVkHDBia+CuotgACCueD3TDQN75D4xmAvCoK9ARMHBzAw0AECiBHkAlC0Mdy7x9ABNA3obAZXIAa6iKEcGlMVQHwWyjYuL2d4v2cPg8vZswx7gHyAAAK7AOif7SAbOqCmn4Ha3AHFsIDtgPq/vLz8P4MSkJ2W9h8ggBjevXvHDo4FQUQg/kdypqCg4H8lUIACnQ/SOBMYI8bAsAJFPcj1AAEEjwVQqLpAbXmH5BJjqI0gi9DTAAgDBBCcAVLkgmQ7yKCZxpCQxqUZhAECCJ4XgMl493ug21ZD+aDAXH0WLM4A9MZPXJkJIIAwTAR5pQMalaCABQUULttBGCCAGCnNzgABBgAMJ5THwGvJLAAAAABJRU5ErkJggg=="
michael@0 22 },
michael@0 23 { title: "Customize Firefox",
michael@0 24 url: "http://en-us.www.mozilla.com/en-US/firefox/customize/",
michael@0 25 icon: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAHWSURBVHjaYvz//z8DJQAggJiQOe/fv2fv7Oz8rays/N+VkfG/iYnJfyD/1+rVq7ffu3dPFpsBAAHEAHIBCJ85c8bN2Nj4vwsDw/8zQLwKiO8CcRoQu0DxqlWrdsHUwzBAAIGJmTNnPgYa9j8UqhFElwPxf2MIDeIrKSn9FwSJoRkAEEAM0DD4DzMAyPi/G+QKY4hh5WAXGf8PDQ0FGwJ22d27CjADAAIIrLmjo+MXA9R2kAHvGBA2wwx6B8W7od6CeQcggKCmCEL8bgwxYCbUIGTDVkHDBia+CuotgACCueD3TDQN75D4xmAvCoK9ARMHBzAw0AECiBHkAlC0Mdy7x9ABNA3obAZXIAa6iKEcGlMVQHwWyjYuL2d4v2cPg8vZswx7gHyAAAK7AOif7SAbOqCmn4Ha3AHFsIDtgPq/vLz8P4MSkJ2W9h8ggBjevXvHDo4FQUQg/kdypqCg4H8lUIACnQ/SOBMYI8bAsAJFPcj1AAEEjwVQqLpAbXmH5BJjqI0gi9DTAAgDBBCcAVLkgmQ7yKCZxpCQxqUZhAECCJ4XgMl493ug21ZD+aDAXH0WLM4A9MZPXJkJIIAwTAR5pQMalaCABQUULttBGCCAGCnNzgABBgAMJ5THwGvJLAAAAABJRU5ErkJggg=="
michael@0 26 },
michael@0 27 { title: "Get Involved",
michael@0 28 url: "http://en-us.www.mozilla.com/en-US/firefox/community/",
michael@0 29 icon: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAHWSURBVHjaYvz//z8DJQAggJiQOe/fv2fv7Oz8rays/N+VkfG/iYnJfyD/1+rVq7ffu3dPFpsBAAHEAHIBCJ85c8bN2Nj4vwsDw/8zQLwKiO8CcRoQu0DxqlWrdsHUwzBAAIGJmTNnPgYa9j8UqhFElwPxf2MIDeIrKSn9FwSJoRkAEEAM0DD4DzMAyPi/G+QKY4hh5WAXGf8PDQ0FGwJ22d27CjADAAIIrLmjo+MXA9R2kAHvGBA2wwx6B8W7od6CeQcggKCmCEL8bgwxYCbUIGTDVkHDBia+CuotgACCueD3TDQN75D4xmAvCoK9ARMHBzAw0AECiBHkAlC0Mdy7x9ABNA3obAZXIAa6iKEcGlMVQHwWyjYuL2d4v2cPg8vZswx7gHyAAAK7AOif7SAbOqCmn4Ha3AHFsIDtgPq/vLz8P4MSkJ2W9h8ggBjevXvHDo4FQUQg/kdypqCg4H8lUIACnQ/SOBMYI8bAsAJFPcj1AAEEjwVQqLpAbXmH5BJjqI0gi9DTAAgDBBCcAVLkgmQ7yKCZxpCQxqUZhAECCJ4XgMl493ug21ZD+aDAXH0WLM4A9MZPXJkJIIAwTAR5pQMalaCABQUULttBGCCAGCnNzgABBgAMJ5THwGvJLAAAAABJRU5ErkJggg=="
michael@0 30 },
michael@0 31 { title: "About Us",
michael@0 32 url: "http://en-us.www.mozilla.com/en-US/about/",
michael@0 33 icon: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAHWSURBVHjaYvz//z8DJQAggJiQOe/fv2fv7Oz8rays/N+VkfG/iYnJfyD/1+rVq7ffu3dPFpsBAAHEAHIBCJ85c8bN2Nj4vwsDw/8zQLwKiO8CcRoQu0DxqlWrdsHUwzBAAIGJmTNnPgYa9j8UqhFElwPxf2MIDeIrKSn9FwSJoRkAEEAM0DD4DzMAyPi/G+QKY4hh5WAXGf8PDQ0FGwJ22d27CjADAAIIrLmjo+MXA9R2kAHvGBA2wwx6B8W7od6CeQcggKCmCEL8bgwxYCbUIGTDVkHDBia+CuotgACCueD3TDQN75D4xmAvCoK9ARMHBzAw0AECiBHkAlC0Mdy7x9ABNA3obAZXIAa6iKEcGlMVQHwWyjYuL2d4v2cPg8vZswx7gHyAAAK7AOif7SAbOqCmn4Ha3AHFsIDtgPq/vLz8P4MSkJ2W9h8ggBjevXvHDo4FQUQg/kdypqCg4H8lUIACnQ/SOBMYI8bAsAJFPcj1AAEEjwVQqLpAbXmH5BJjqI0gi9DTAAgDBBCcAVLkgmQ7yKCZxpCQxqUZhAECCJ4XgMl493ug21ZD+aDAXH0WLM4A9MZPXJkJIIAwTAR5pQMalaCABQUULttBGCCAGCnNzgABBgAMJ5THwGvJLAAAAABJRU5ErkJggg=="
michael@0 34 }
michael@0 35 ]
michael@0 36 },
michael@0 37 {
michael@0 38 type: Ci.nsINavHistoryResultNode.RESULT_TYPE_SEPARATOR
michael@0 39 },
michael@0 40 { title: "test",
michael@0 41 description: "folder test comment",
michael@0 42 dateAdded: 1177541020000000,
michael@0 43 //lastModified: 1177541050000000,
michael@0 44 children: [
michael@0 45 { title: "test post keyword",
michael@0 46 description: "item description",
michael@0 47 dateAdded: 1177375336000000,
michael@0 48 //lastModified: 1177375423000000,
michael@0 49 keyword: "test",
michael@0 50 sidebar: true,
michael@0 51 postData: "hidden1%3Dbar&text1%3D%25s",
michael@0 52 charset: "ISO-8859-1"
michael@0 53 }
michael@0 54 ]
michael@0 55 }
michael@0 56 ],
michael@0 57 toolbar: [
michael@0 58 { title: "Getting Started",
michael@0 59 url: "http://en-us.www.mozilla.com/en-US/firefox/central/",
michael@0 60 icon: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAHWSURBVHjaYvz//z8DJQAggJiQOe/fv2fv7Oz8rays/N+VkfG/iYnJfyD/1+rVq7ffu3dPFpsBAAHEAHIBCJ85c8bN2Nj4vwsDw/8zQLwKiO8CcRoQu0DxqlWrdsHUwzBAAIGJmTNnPgYa9j8UqhFElwPxf2MIDeIrKSn9FwSJoRkAEEAM0DD4DzMAyPi/G+QKY4hh5WAXGf8PDQ0FGwJ22d27CjADAAIIrLmjo+MXA9R2kAHvGBA2wwx6B8W7od6CeQcggKCmCEL8bgwxYCbUIGTDVkHDBia+CuotgACCueD3TDQN75D4xmAvCoK9ARMHBzAw0AECiBHkAlC0Mdy7x9ABNA3obAZXIAa6iKEcGlMVQHwWyjYuL2d4v2cPg8vZswx7gHyAAAK7AOif7SAbOqCmn4Ha3AHFsIDtgPq/vLz8P4MSkJ2W9h8ggBjevXvHDo4FQUQg/kdypqCg4H8lUIACnQ/SOBMYI8bAsAJFPcj1AAEEjwVQqLpAbXmH5BJjqI0gi9DTAAgDBBCcAVLkgmQ7yKCZxpCQxqUZhAECCJ4XgMl493ug21ZD+aDAXH0WLM4A9MZPXJkJIIAwTAR5pQMalaCABQUULttBGCCAGCnNzgABBgAMJ5THwGvJLAAAAABJRU5ErkJggg=="
michael@0 61 },
michael@0 62 { title: "Latest Headlines",
michael@0 63 url: "http://en-us.fxfeeds.mozilla.com/en-US/firefox/livebookmarks/",
michael@0 64 feedUrl: "http://en-us.fxfeeds.mozilla.com/en-US/firefox/headlines.xml"
michael@0 65 }
michael@0 66 ],
michael@0 67 unfiled: [
michael@0 68 { title: "Example.tld",
michael@0 69 url: "http://example.tld/"
michael@0 70 }
michael@0 71 ]
michael@0 72 };
michael@0 73
michael@0 74 // Exported bookmarks file pointer.
michael@0 75 let bookmarksExportedFile;
michael@0 76
michael@0 77 add_task(function test_import_bookmarks() {
michael@0 78 let bookmarksFile = OS.Path.join(do_get_cwd().path, "bookmarks.json");
michael@0 79
michael@0 80 yield BookmarkJSONUtils.importFromFile(bookmarksFile, true);
michael@0 81 yield testImportedBookmarks();
michael@0 82 });
michael@0 83
michael@0 84 add_task(function test_export_bookmarks() {
michael@0 85 bookmarksExportedFile = OS.Path.join(OS.Constants.Path.profileDir,
michael@0 86 "bookmarks.exported.json");
michael@0 87 yield BookmarkJSONUtils.exportToFile(bookmarksExportedFile);
michael@0 88 });
michael@0 89
michael@0 90 add_task(function test_import_exported_bookmarks() {
michael@0 91 remove_all_bookmarks();
michael@0 92 yield BookmarkJSONUtils.importFromFile(bookmarksExportedFile, true);
michael@0 93 yield testImportedBookmarks();
michael@0 94 });
michael@0 95
michael@0 96 add_task(function test_import_ontop() {
michael@0 97 remove_all_bookmarks();
michael@0 98 yield BookmarkJSONUtils.importFromFile(bookmarksExportedFile, true);
michael@0 99 yield BookmarkJSONUtils.exportToFile(bookmarksExportedFile);
michael@0 100 yield BookmarkJSONUtils.importFromFile(bookmarksExportedFile, true);
michael@0 101 yield testImportedBookmarks();
michael@0 102 });
michael@0 103
michael@0 104 add_task(function test_clean() {
michael@0 105 remove_all_bookmarks();
michael@0 106 });
michael@0 107
michael@0 108 function testImportedBookmarks() {
michael@0 109 for (let group in test_bookmarks) {
michael@0 110 do_print("[testImportedBookmarks()] Checking group '" + group + "'");
michael@0 111
michael@0 112 let root;
michael@0 113 switch (group) {
michael@0 114 case "menu":
michael@0 115 root =
michael@0 116 PlacesUtils.getFolderContents(PlacesUtils.bookmarksMenuFolderId).root;
michael@0 117 break;
michael@0 118 case "toolbar":
michael@0 119 root =
michael@0 120 PlacesUtils.getFolderContents(PlacesUtils.toolbarFolderId).root;
michael@0 121 break;
michael@0 122 case "unfiled":
michael@0 123 root =
michael@0 124 PlacesUtils.getFolderContents(PlacesUtils.unfiledBookmarksFolderId).root;
michael@0 125 break;
michael@0 126 }
michael@0 127
michael@0 128 let items = test_bookmarks[group];
michael@0 129 do_check_eq(root.childCount, items.length);
michael@0 130
michael@0 131 for (let key in items) {
michael@0 132 yield checkItem(items[key], root.getChild(key));
michael@0 133 }
michael@0 134
michael@0 135 root.containerOpen = false;
michael@0 136 }
michael@0 137 }
michael@0 138
michael@0 139 function checkItem(aExpected, aNode) {
michael@0 140 let id = aNode.itemId;
michael@0 141
michael@0 142 return Task.spawn(function() {
michael@0 143 for (prop in aExpected) {
michael@0 144 switch (prop) {
michael@0 145 case "type":
michael@0 146 do_check_eq(aNode.type, aExpected.type);
michael@0 147 break;
michael@0 148 case "title":
michael@0 149 do_check_eq(aNode.title, aExpected.title);
michael@0 150 break;
michael@0 151 case "description":
michael@0 152 do_check_eq(PlacesUtils.annotations.getItemAnnotation(
michael@0 153 id, DESCRIPTION_ANNO), aExpected.description);
michael@0 154 break;
michael@0 155 case "dateAdded":
michael@0 156 do_check_eq(PlacesUtils.bookmarks.getItemDateAdded(id),
michael@0 157 aExpected.dateAdded);
michael@0 158 break;
michael@0 159 case "lastModified":
michael@0 160 do_check_eq(PlacesUtils.bookmarks.getItemLastModified(id),
michael@0 161 aExpected.lastModified);
michael@0 162 break;
michael@0 163 case "url":
michael@0 164 if (!("feedUrl" in aExpected))
michael@0 165 do_check_eq(aNode.uri, aExpected.url);
michael@0 166 break;
michael@0 167 case "icon":
michael@0 168 let (deferred = Promise.defer(), data) {
michael@0 169 PlacesUtils.favicons.getFaviconDataForPage(
michael@0 170 NetUtil.newURI(aExpected.url),
michael@0 171 function (aURI, aDataLen, aData, aMimeType) {
michael@0 172 deferred.resolve(aData);
michael@0 173 });
michael@0 174 data = yield deferred.promise;
michael@0 175 let base64Icon = "data:image/png;base64," +
michael@0 176 base64EncodeString(String.fromCharCode.apply(String, data));
michael@0 177 do_check_true(base64Icon == aExpected.icon);
michael@0 178 }
michael@0 179 break;
michael@0 180 case "keyword":
michael@0 181 break;
michael@0 182 case "sidebar":
michael@0 183 do_check_eq(PlacesUtils.annotations.itemHasAnnotation(
michael@0 184 id, LOAD_IN_SIDEBAR_ANNO), aExpected.sidebar);
michael@0 185 break;
michael@0 186 case "postData":
michael@0 187 do_check_eq(PlacesUtils.annotations.getItemAnnotation(
michael@0 188 id, PlacesUtils.POST_DATA_ANNO), aExpected.postData);
michael@0 189 break;
michael@0 190 case "charset":
michael@0 191 let testURI = NetUtil.newURI(aNode.uri);
michael@0 192 do_check_eq((yield PlacesUtils.getCharsetForURI(testURI)), aExpected.charset);
michael@0 193 break;
michael@0 194 case "feedUrl":
michael@0 195 let livemark = yield PlacesUtils.livemarks.getLivemark({ id: id });
michael@0 196 do_check_eq(livemark.siteURI.spec, aExpected.url);
michael@0 197 do_check_eq(livemark.feedURI.spec, aExpected.feedUrl);
michael@0 198 break;
michael@0 199 case "children":
michael@0 200 let folder = aNode.QueryInterface(Ci.nsINavHistoryContainerResultNode);
michael@0 201 do_check_eq(folder.hasChildren, aExpected.children.length > 0);
michael@0 202 folder.containerOpen = true;
michael@0 203 do_check_eq(folder.childCount, aExpected.children.length);
michael@0 204
michael@0 205 aExpected.children.forEach(function (item, index) checkItem(item, folder.getChild(index)));
michael@0 206
michael@0 207 folder.containerOpen = false;
michael@0 208 break;
michael@0 209 default:
michael@0 210 throw new Error("Unknown property");
michael@0 211 }
michael@0 212 }
michael@0 213 });
michael@0 214 }

mercurial