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

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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 const LOAD_IN_SIDEBAR_ANNO = "bookmarkProperties/loadInSidebar";
michael@0 8 const DESCRIPTION_ANNO = "bookmarkProperties/description";
michael@0 9 const POST_DATA_ANNO = "bookmarkProperties/POSTData";
michael@0 10
michael@0 11 do_check_eq(typeof PlacesUtils, "object");
michael@0 12
michael@0 13 // main
michael@0 14 function run_test() {
michael@0 15 do_test_pending();
michael@0 16
michael@0 17 /*
michael@0 18 HTML+FEATURES SUMMARY:
michael@0 19 - import legacy bookmarks
michael@0 20 - export as json, import, test (tests integrity of html > json)
michael@0 21 - export as html, import, test (tests integrity of json > html)
michael@0 22
michael@0 23 BACKUP/RESTORE SUMMARY:
michael@0 24 - create a bookmark in each root
michael@0 25 - tag multiple URIs with multiple tags
michael@0 26 - export as json, import, test
michael@0 27 */
michael@0 28
michael@0 29 // import the importer
michael@0 30 Cu.import("resource://gre/modules/BookmarkHTMLUtils.jsm");
michael@0 31
michael@0 32 // file pointer to legacy bookmarks file
michael@0 33 var bookmarksFileOld = OS.Path.join(do_get_cwd().path, "bookmarks.preplaces.html");
michael@0 34 // file pointer to a new places-exported json file
michael@0 35 var jsonFile = OS.Path.join(OS.Constants.Path.profileDir, "bookmarks.exported.json");
michael@0 36 Task.spawn(function () {
michael@0 37 // create bookmarks.exported.json
michael@0 38 if ((yield OS.File.exists(jsonFile)))
michael@0 39 yield OS.File.remove(jsonFile);
michael@0 40
michael@0 41 // Test importing a pre-Places canonical bookmarks file.
michael@0 42 // 1. import bookmarks.preplaces.html
michael@0 43 // Note: we do not empty the db before this import to catch bugs like 380999
michael@0 44 try {
michael@0 45 BookmarkHTMLUtils.importFromFile(bookmarksFileOld, true)
michael@0 46 .then(after_import, do_report_unexpected_exception);
michael@0 47 } catch(ex) { do_throw("couldn't import legacy bookmarks file: " + ex); }
michael@0 48 });
michael@0 49
michael@0 50 function after_import() {
michael@0 51 populate();
michael@0 52
michael@0 53 // 2. run the test-suite
michael@0 54 Task.spawn(function() {
michael@0 55 yield validate();
michael@0 56 yield promiseAsyncUpdates();
michael@0 57
michael@0 58 // Test exporting a Places canonical json file.
michael@0 59 // 1. export to bookmarks.exported.json
michael@0 60 try {
michael@0 61 yield BookmarkJSONUtils.exportToFile(jsonFile);
michael@0 62 } catch(ex) { do_throw("couldn't export to file: " + ex); }
michael@0 63 LOG("exported json");
michael@0 64
michael@0 65 // 2. empty bookmarks db
michael@0 66 // 3. import bookmarks.exported.json
michael@0 67 try {
michael@0 68 yield BookmarkJSONUtils.importFromFile(jsonFile, true);
michael@0 69 } catch(ex) { do_throw("couldn't import the exported file: " + ex); }
michael@0 70 LOG("imported json");
michael@0 71
michael@0 72 // 4. run the test-suite
michael@0 73 yield validate();
michael@0 74 LOG("validated import");
michael@0 75
michael@0 76 yield promiseAsyncUpdates();
michael@0 77 do_test_finished();
michael@0 78 });
michael@0 79 }
michael@0 80 }
michael@0 81
michael@0 82 var tagData = [
michael@0 83 { uri: uri("http://slint.us"), tags: ["indie", "kentucky", "music"] },
michael@0 84 { uri: uri("http://en.wikipedia.org/wiki/Diplodocus"), tags: ["dinosaur", "dj", "rad word"] }
michael@0 85 ];
michael@0 86
michael@0 87 var bookmarkData = [
michael@0 88 { uri: uri("http://slint.us"), title: "indie, kentucky, music" },
michael@0 89 { uri: uri("http://en.wikipedia.org/wiki/Diplodocus"), title: "dinosaur, dj, rad word" }
michael@0 90 ];
michael@0 91
michael@0 92 /*
michael@0 93 populate data in each folder
michael@0 94 (menu is populated via the html import)
michael@0 95 */
michael@0 96 function populate() {
michael@0 97 // add tags
michael@0 98 for each(let {uri: u, tags: t} in tagData)
michael@0 99 PlacesUtils.tagging.tagURI(u, t);
michael@0 100
michael@0 101 // add unfiled bookmarks
michael@0 102 for each(let {uri: u, title: t} in bookmarkData) {
michael@0 103 PlacesUtils.bookmarks.insertBookmark(PlacesUtils.bookmarks.unfiledBookmarksFolder,
michael@0 104 u, PlacesUtils.bookmarks.DEFAULT_INDEX, t);
michael@0 105 }
michael@0 106
michael@0 107 // add to the toolbar
michael@0 108 for each(let {uri: u, title: t} in bookmarkData) {
michael@0 109 PlacesUtils.bookmarks.insertBookmark(PlacesUtils.bookmarks.toolbarFolder,
michael@0 110 u, PlacesUtils.bookmarks.DEFAULT_INDEX, t);
michael@0 111 }
michael@0 112 }
michael@0 113
michael@0 114 function validate() {
michael@0 115 yield testCanonicalBookmarks();
michael@0 116 yield testToolbarFolder();
michael@0 117 testUnfiledBookmarks();
michael@0 118 testTags();
michael@0 119 }
michael@0 120
michael@0 121 // Tests a bookmarks datastore that has a set of bookmarks, etc
michael@0 122 // that flex each supported field and feature.
michael@0 123 function testCanonicalBookmarks() {
michael@0 124 // query to see if the deleted folder and items have been imported
michael@0 125 var query = PlacesUtils.history.getNewQuery();
michael@0 126 query.setFolders([PlacesUtils.bookmarksMenuFolderId], 1);
michael@0 127 var result = PlacesUtils.history.executeQuery(query, PlacesUtils.history.getNewQueryOptions());
michael@0 128 var rootNode = result.root;
michael@0 129 rootNode.containerOpen = true;
michael@0 130
michael@0 131 // Count expected bookmarks in the menu root.
michael@0 132 do_check_eq(rootNode.childCount, 3);
michael@0 133
michael@0 134 // check separator
michael@0 135 var testSeparator = rootNode.getChild(1);
michael@0 136 do_check_eq(testSeparator.type, testSeparator.RESULT_TYPE_SEPARATOR);
michael@0 137
michael@0 138 // get test folder
michael@0 139 var testFolder = rootNode.getChild(2);
michael@0 140 do_check_eq(testFolder.type, testFolder.RESULT_TYPE_FOLDER);
michael@0 141 do_check_eq(testFolder.title, "test");
michael@0 142
michael@0 143 /*
michael@0 144 // add date
michael@0 145 do_check_eq(PlacesUtils.bookmarks.getItemDateAdded(testFolder.itemId)/1000000, 1177541020);
michael@0 146 // last modified
michael@0 147 do_check_eq(PlacesUtils.bookmarks.getItemLastModified(testFolder.itemId)/1000000, 1177541050);
michael@0 148 */
michael@0 149
michael@0 150 testFolder = testFolder.QueryInterface(Ci.nsINavHistoryQueryResultNode);
michael@0 151 do_check_eq(testFolder.hasChildren, true);
michael@0 152 // folder description
michael@0 153 do_check_true(PlacesUtils.annotations.itemHasAnnotation(testFolder.itemId,
michael@0 154 DESCRIPTION_ANNO));
michael@0 155 do_check_eq("folder test comment",
michael@0 156 PlacesUtils.annotations.getItemAnnotation(testFolder.itemId, DESCRIPTION_ANNO));
michael@0 157 // open test folder, and test the children
michael@0 158 testFolder.containerOpen = true;
michael@0 159 var cc = testFolder.childCount;
michael@0 160 // XXX Bug 380468
michael@0 161 // do_check_eq(cc, 2);
michael@0 162 do_check_eq(cc, 1);
michael@0 163
michael@0 164 // test bookmark 1
michael@0 165 var testBookmark1 = testFolder.getChild(0);
michael@0 166 // url
michael@0 167 do_check_eq("http://test/post", testBookmark1.uri);
michael@0 168 // title
michael@0 169 do_check_eq("test post keyword", testBookmark1.title);
michael@0 170 // keyword
michael@0 171 do_check_eq("test", PlacesUtils.bookmarks.getKeywordForBookmark(testBookmark1.itemId));
michael@0 172 // sidebar
michael@0 173 do_check_true(PlacesUtils.annotations.itemHasAnnotation(testBookmark1.itemId,
michael@0 174 LOAD_IN_SIDEBAR_ANNO));
michael@0 175 /*
michael@0 176 // add date
michael@0 177 do_check_eq(testBookmark1.dateAdded/1000000, 1177375336);
michael@0 178
michael@0 179 // last modified
michael@0 180 do_check_eq(testBookmark1.lastModified/1000000, 1177375423);
michael@0 181 */
michael@0 182
michael@0 183 // post data
michael@0 184 do_check_true(PlacesUtils.annotations.itemHasAnnotation(testBookmark1.itemId, POST_DATA_ANNO));
michael@0 185 do_check_eq("hidden1%3Dbar&text1%3D%25s",
michael@0 186 PlacesUtils.annotations.getItemAnnotation(testBookmark1.itemId, POST_DATA_ANNO));
michael@0 187
michael@0 188 // last charset
michael@0 189 var testURI = PlacesUtils._uri(testBookmark1.uri);
michael@0 190 do_check_eq("ISO-8859-1", (yield PlacesUtils.getCharsetForURI(testURI)));
michael@0 191
michael@0 192 // description
michael@0 193 do_check_true(PlacesUtils.annotations.itemHasAnnotation(testBookmark1.itemId,
michael@0 194 DESCRIPTION_ANNO));
michael@0 195 do_check_eq("item description",
michael@0 196 PlacesUtils.annotations.getItemAnnotation(testBookmark1.itemId,
michael@0 197 DESCRIPTION_ANNO));
michael@0 198
michael@0 199 // clean up
michael@0 200 testFolder.containerOpen = false;
michael@0 201 rootNode.containerOpen = false;
michael@0 202 }
michael@0 203
michael@0 204 function testToolbarFolder() {
michael@0 205 var query = PlacesUtils.history.getNewQuery();
michael@0 206 query.setFolders([PlacesUtils.toolbarFolderId], 1);
michael@0 207 var result = PlacesUtils.history.executeQuery(query, PlacesUtils.history.getNewQueryOptions());
michael@0 208
michael@0 209 var toolbar = result.root;
michael@0 210 toolbar.containerOpen = true;
michael@0 211
michael@0 212 // child count (add 2 for pre-existing items)
michael@0 213 do_check_eq(toolbar.childCount, bookmarkData.length + 2);
michael@0 214
michael@0 215 // livemark
michael@0 216 var livemark = toolbar.getChild(1);
michael@0 217 // title
michael@0 218 do_check_eq("Latest Headlines", livemark.title);
michael@0 219
michael@0 220 let foundLivemark = yield PlacesUtils.livemarks.getLivemark({ id: livemark.itemId });
michael@0 221 do_check_eq("http://en-us.fxfeeds.mozilla.com/en-US/firefox/livebookmarks/",
michael@0 222 foundLivemark.siteURI.spec);
michael@0 223 do_check_eq("http://en-us.fxfeeds.mozilla.com/en-US/firefox/headlines.xml",
michael@0 224 foundLivemark.feedURI.spec);
michael@0 225
michael@0 226 // test added bookmark data
michael@0 227 var child = toolbar.getChild(2);
michael@0 228 do_check_eq(child.uri, bookmarkData[0].uri.spec);
michael@0 229 do_check_eq(child.title, bookmarkData[0].title);
michael@0 230 child = toolbar.getChild(3);
michael@0 231 do_check_eq(child.uri, bookmarkData[1].uri.spec);
michael@0 232 do_check_eq(child.title, bookmarkData[1].title);
michael@0 233
michael@0 234 toolbar.containerOpen = false;
michael@0 235 }
michael@0 236
michael@0 237 function testUnfiledBookmarks() {
michael@0 238 var query = PlacesUtils.history.getNewQuery();
michael@0 239 query.setFolders([PlacesUtils.unfiledBookmarksFolderId], 1);
michael@0 240 var result = PlacesUtils.history.executeQuery(query, PlacesUtils.history.getNewQueryOptions());
michael@0 241 var rootNode = result.root;
michael@0 242 rootNode.containerOpen = true;
michael@0 243 // child count (add 1 for pre-existing item)
michael@0 244 do_check_eq(rootNode.childCount, bookmarkData.length + 1);
michael@0 245 for (var i = 1; i < rootNode.childCount; i++) {
michael@0 246 var child = rootNode.getChild(i);
michael@0 247 dump(bookmarkData[i - 1].uri.spec + " == " + child.uri + "?\n");
michael@0 248 do_check_true(bookmarkData[i - 1].uri.equals(uri(child.uri)));
michael@0 249 do_check_eq(child.title, bookmarkData[i - 1].title);
michael@0 250 /* WTF
michael@0 251 if (child.tags)
michael@0 252 do_check_eq(child.tags, bookmarkData[i].title);
michael@0 253 */
michael@0 254 }
michael@0 255 rootNode.containerOpen = false;
michael@0 256 }
michael@0 257
michael@0 258 function testTags() {
michael@0 259 for each(let {uri: u, tags: t} in tagData) {
michael@0 260 var i = 0;
michael@0 261 dump("test tags for " + u.spec + ": " + t + "\n");
michael@0 262 var tt = PlacesUtils.tagging.getTagsForURI(u);
michael@0 263 dump("true tags for " + u.spec + ": " + tt + "\n");
michael@0 264 do_check_true(t.every(function(el) {
michael@0 265 i++;
michael@0 266 return tt.indexOf(el) > -1;
michael@0 267 }));
michael@0 268 do_check_eq(i, t.length);
michael@0 269 }
michael@0 270 }

mercurial