toolkit/components/places/tests/unit/test_history_sidebar.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 // Get history service
michael@0 8 var hs = Cc["@mozilla.org/browser/nav-history-service;1"].
michael@0 9 getService(Ci.nsINavHistoryService);
michael@0 10 var bh = hs.QueryInterface(Ci.nsIBrowserHistory);
michael@0 11 var bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
michael@0 12 getService(Ci.nsINavBookmarksService);
michael@0 13 var ps = Cc["@mozilla.org/preferences-service;1"].
michael@0 14 getService(Ci.nsIPrefBranch);
michael@0 15
michael@0 16 /**
michael@0 17 * Adds a test URI visit to the database.
michael@0 18 *
michael@0 19 * @param aURI
michael@0 20 * The URI to add a visit for.
michael@0 21 * @param aTime
michael@0 22 * Reference "now" time.
michael@0 23 * @param aDayOffset
michael@0 24 * number of days to add, pass a negative value to subtract them.
michael@0 25 */
michael@0 26 function task_add_normalized_visit(aURI, aTime, aDayOffset) {
michael@0 27 var dateObj = new Date(aTime);
michael@0 28 // Normalize to midnight
michael@0 29 dateObj.setHours(0);
michael@0 30 dateObj.setMinutes(0);
michael@0 31 dateObj.setSeconds(0);
michael@0 32 dateObj.setMilliseconds(0);
michael@0 33 // Days where DST changes should be taken in count.
michael@0 34 var previousDateObj = new Date(dateObj.getTime() + aDayOffset * 86400000);
michael@0 35 var DSTCorrection = (dateObj.getTimezoneOffset() -
michael@0 36 previousDateObj.getTimezoneOffset()) * 60 * 1000;
michael@0 37 // Substract aDayOffset
michael@0 38 var PRTimeWithOffset = (previousDateObj.getTime() - DSTCorrection) * 1000;
michael@0 39 var timeInMs = new Date(PRTimeWithOffset/1000);
michael@0 40 print("Adding visit to " + aURI.spec + " at " + timeInMs);
michael@0 41 yield promiseAddVisits({
michael@0 42 uri: aURI,
michael@0 43 visitDate: PRTimeWithOffset
michael@0 44 });
michael@0 45 }
michael@0 46
michael@0 47 function days_for_x_months_ago(aNowObj, aMonths) {
michael@0 48 var oldTime = new Date();
michael@0 49 // Set day before month, otherwise we could try to calculate 30 February, or
michael@0 50 // other nonexistent days.
michael@0 51 oldTime.setDate(1);
michael@0 52 oldTime.setMonth(aNowObj.getMonth() - aMonths);
michael@0 53 oldTime.setHours(0);
michael@0 54 oldTime.setMinutes(0);
michael@0 55 oldTime.setSeconds(0);
michael@0 56 // Stay larger for eventual timezone issues, add 2 days.
michael@0 57 return parseInt((aNowObj - oldTime) / (1000*60*60*24)) + 2;
michael@0 58 }
michael@0 59
michael@0 60 var nowObj = new Date();
michael@0 61 // This test relies on en-US locale
michael@0 62 // Offset is number of days
michael@0 63 var containers = [
michael@0 64 { label: "Today" , offset: 0 , visible: true },
michael@0 65 { label: "Yesterday" , offset: -1 , visible: true },
michael@0 66 { label: "Last 7 days" , offset: -3 , visible: true },
michael@0 67 { label: "This month" , offset: -8 , visible: nowObj.getDate() > 8 },
michael@0 68 { label: "" , offset: -days_for_x_months_ago(nowObj, 0) , visible: true },
michael@0 69 { label: "" , offset: -days_for_x_months_ago(nowObj, 1) , visible: true },
michael@0 70 { label: "" , offset: -days_for_x_months_ago(nowObj, 2) , visible: true },
michael@0 71 { label: "" , offset: -days_for_x_months_ago(nowObj, 3) , visible: true },
michael@0 72 { label: "" , offset: -days_for_x_months_ago(nowObj, 4) , visible: true },
michael@0 73 { label: "Older than 6 months" , offset: -days_for_x_months_ago(nowObj, 5) , visible: true },
michael@0 74 ];
michael@0 75
michael@0 76 var visibleContainers = containers.filter(
michael@0 77 function(aContainer) {return aContainer.visible});
michael@0 78
michael@0 79 /**
michael@0 80 * Asynchronous task that fills history and checks containers' labels.
michael@0 81 */
michael@0 82 function task_fill_history() {
michael@0 83 print("\n\n*** TEST Fill History\n");
michael@0 84 // We can't use "now" because our hardcoded offsets would be invalid for some
michael@0 85 // date. So we hardcode a date.
michael@0 86 for (var i = 0; i < containers.length; i++) {
michael@0 87 var container = containers[i];
michael@0 88 var testURI = uri("http://mirror"+i+".mozilla.com/b");
michael@0 89 yield task_add_normalized_visit(testURI, nowObj.getTime(), container.offset);
michael@0 90 var testURI = uri("http://mirror"+i+".mozilla.com/a");
michael@0 91 yield task_add_normalized_visit(testURI, nowObj.getTime(), container.offset);
michael@0 92 var testURI = uri("http://mirror"+i+".google.com/b");
michael@0 93 yield task_add_normalized_visit(testURI, nowObj.getTime(), container.offset);
michael@0 94 var testURI = uri("http://mirror"+i+".google.com/a");
michael@0 95 yield task_add_normalized_visit(testURI, nowObj.getTime(), container.offset);
michael@0 96 // Bug 485703 - Hide date containers not containing additional entries
michael@0 97 // compared to previous ones.
michael@0 98 // Check after every new container is added.
michael@0 99 check_visit(container.offset);
michael@0 100 }
michael@0 101
michael@0 102 var options = hs.getNewQueryOptions();
michael@0 103 options.resultType = options.RESULTS_AS_DATE_SITE_QUERY;
michael@0 104 var query = hs.getNewQuery();
michael@0 105
michael@0 106 var result = hs.executeQuery(query, options);
michael@0 107 var root = result.root;
michael@0 108 root.containerOpen = true;
michael@0 109
michael@0 110 var cc = root.childCount;
michael@0 111 print("Found containers:");
michael@0 112 var previousLabels = [];
michael@0 113 for (var i = 0; i < cc; i++) {
michael@0 114 var container = visibleContainers[i];
michael@0 115 var node = root.getChild(i);
michael@0 116 print(node.title);
michael@0 117 if (container.label)
michael@0 118 do_check_eq(node.title, container.label);
michael@0 119 // Check labels are not repeated.
michael@0 120 do_check_eq(previousLabels.indexOf(node.title), -1);
michael@0 121 previousLabels.push(node.title);
michael@0 122 }
michael@0 123 do_check_eq(cc, visibleContainers.length);
michael@0 124 root.containerOpen = false;
michael@0 125 }
michael@0 126
michael@0 127 /**
michael@0 128 * Bug 485703 - Hide date containers not containing additional entries compared
michael@0 129 * to previous ones.
michael@0 130 */
michael@0 131 function check_visit(aOffset) {
michael@0 132 var options = hs.getNewQueryOptions();
michael@0 133 options.resultType = options.RESULTS_AS_DATE_SITE_QUERY;
michael@0 134 var query = hs.getNewQuery();
michael@0 135 var result = hs.executeQuery(query, options);
michael@0 136 var root = result.root;
michael@0 137 root.containerOpen = true;
michael@0 138 var cc = root.childCount;
michael@0 139
michael@0 140 var unexpected = [];
michael@0 141 switch (aOffset) {
michael@0 142 case 0:
michael@0 143 unexpected = ["Yesterday", "Last 7 days", "This month"];
michael@0 144 break;
michael@0 145 case -1:
michael@0 146 unexpected = ["Last 7 days", "This month"];
michael@0 147 break;
michael@0 148 case -3:
michael@0 149 unexpected = ["This month"];
michael@0 150 break;
michael@0 151 default:
michael@0 152 // Other containers are tested later.
michael@0 153 }
michael@0 154
michael@0 155 print("Found containers:");
michael@0 156 for (var i = 0; i < cc; i++) {
michael@0 157 var node = root.getChild(i);
michael@0 158 print(node.title);
michael@0 159 do_check_eq(unexpected.indexOf(node.title), -1);
michael@0 160 }
michael@0 161
michael@0 162 root.containerOpen = false;
michael@0 163 }
michael@0 164
michael@0 165 /**
michael@0 166 * Queries history grouped by date and site, checking containers' labels and
michael@0 167 * children.
michael@0 168 */
michael@0 169 function test_RESULTS_AS_DATE_SITE_QUERY() {
michael@0 170 print("\n\n*** TEST RESULTS_AS_DATE_SITE_QUERY\n");
michael@0 171 var options = hs.getNewQueryOptions();
michael@0 172 options.resultType = options.RESULTS_AS_DATE_SITE_QUERY;
michael@0 173 var query = hs.getNewQuery();
michael@0 174 var result = hs.executeQuery(query, options);
michael@0 175 var root = result.root;
michael@0 176 root.containerOpen = true;
michael@0 177
michael@0 178 // Check one of the days
michael@0 179 var dayNode = root.getChild(0)
michael@0 180 .QueryInterface(Ci.nsINavHistoryContainerResultNode);
michael@0 181 dayNode.containerOpen = true;
michael@0 182 do_check_eq(dayNode.childCount, 2);
michael@0 183
michael@0 184 // Items should be sorted by host
michael@0 185 var site1 = dayNode.getChild(0)
michael@0 186 .QueryInterface(Ci.nsINavHistoryContainerResultNode);
michael@0 187 do_check_eq(site1.title, "mirror0.google.com");
michael@0 188
michael@0 189 var site2 = dayNode.getChild(1)
michael@0 190 .QueryInterface(Ci.nsINavHistoryContainerResultNode);
michael@0 191 do_check_eq(site2.title, "mirror0.mozilla.com");
michael@0 192
michael@0 193 site1.containerOpen = true;
michael@0 194 do_check_eq(site1.childCount, 2);
michael@0 195
michael@0 196 // Inside of host sites are sorted by title
michael@0 197 var site1visit = site1.getChild(0);
michael@0 198 do_check_eq(site1visit.uri, "http://mirror0.google.com/a");
michael@0 199
michael@0 200 // Bug 473157: changing sorting mode should not affect the containers
michael@0 201 result.sortingMode = options.SORT_BY_TITLE_DESCENDING;
michael@0 202
michael@0 203 // Check one of the days
michael@0 204 var dayNode = root.getChild(0)
michael@0 205 .QueryInterface(Ci.nsINavHistoryContainerResultNode);
michael@0 206 dayNode.containerOpen = true;
michael@0 207 do_check_eq(dayNode.childCount, 2);
michael@0 208
michael@0 209 // Hosts are still sorted by title
michael@0 210 var site1 = dayNode.getChild(0)
michael@0 211 .QueryInterface(Ci.nsINavHistoryContainerResultNode);
michael@0 212 do_check_eq(site1.title, "mirror0.google.com");
michael@0 213
michael@0 214 var site2 = dayNode.getChild(1)
michael@0 215 .QueryInterface(Ci.nsINavHistoryContainerResultNode);
michael@0 216 do_check_eq(site2.title, "mirror0.mozilla.com");
michael@0 217
michael@0 218 site1.containerOpen = true;
michael@0 219 do_check_eq(site1.childCount, 2);
michael@0 220
michael@0 221 // But URLs are now sorted by title descending
michael@0 222 var site1visit = site1.getChild(0);
michael@0 223 do_check_eq(site1visit.uri, "http://mirror0.google.com/b");
michael@0 224
michael@0 225 site1.containerOpen = false;
michael@0 226 dayNode.containerOpen = false;
michael@0 227 root.containerOpen = false;
michael@0 228 }
michael@0 229
michael@0 230 /**
michael@0 231 * Queries history grouped by date, checking containers' labels and children.
michael@0 232 */
michael@0 233 function test_RESULTS_AS_DATE_QUERY() {
michael@0 234 print("\n\n*** TEST RESULTS_AS_DATE_QUERY\n");
michael@0 235 var options = hs.getNewQueryOptions();
michael@0 236 options.resultType = options.RESULTS_AS_DATE_QUERY;
michael@0 237 var query = hs.getNewQuery();
michael@0 238 var result = hs.executeQuery(query, options);
michael@0 239 var root = result.root;
michael@0 240 root.containerOpen = true;
michael@0 241
michael@0 242 var cc = root.childCount;
michael@0 243 do_check_eq(cc, visibleContainers.length);
michael@0 244 print("Found containers:");
michael@0 245 for (var i = 0; i < cc; i++) {
michael@0 246 var container = visibleContainers[i];
michael@0 247 var node = root.getChild(i);
michael@0 248 print(node.title);
michael@0 249 if (container.label)
michael@0 250 do_check_eq(node.title, container.label);
michael@0 251 }
michael@0 252
michael@0 253 // Check one of the days
michael@0 254 var dayNode = root.getChild(0)
michael@0 255 .QueryInterface(Ci.nsINavHistoryContainerResultNode);
michael@0 256 dayNode.containerOpen = true;
michael@0 257 do_check_eq(dayNode.childCount, 4);
michael@0 258
michael@0 259 // Items should be sorted by title
michael@0 260 var visit1 = dayNode.getChild(0);
michael@0 261 do_check_eq(visit1.uri, "http://mirror0.google.com/a");
michael@0 262
michael@0 263 var visit2 = dayNode.getChild(3);
michael@0 264 do_check_eq(visit2.uri, "http://mirror0.mozilla.com/b");
michael@0 265
michael@0 266 // Bug 473157: changing sorting mode should not affect the containers
michael@0 267 result.sortingMode = options.SORT_BY_TITLE_DESCENDING;
michael@0 268
michael@0 269 // Check one of the days
michael@0 270 var dayNode = root.getChild(0)
michael@0 271 .QueryInterface(Ci.nsINavHistoryContainerResultNode);
michael@0 272 dayNode.containerOpen = true;
michael@0 273 do_check_eq(dayNode.childCount, 4);
michael@0 274
michael@0 275 // But URLs are now sorted by title descending
michael@0 276 var visit1 = dayNode.getChild(0);
michael@0 277 do_check_eq(visit1.uri, "http://mirror0.mozilla.com/b");
michael@0 278
michael@0 279 var visit2 = dayNode.getChild(3);
michael@0 280 do_check_eq(visit2.uri, "http://mirror0.google.com/a");
michael@0 281
michael@0 282 dayNode.containerOpen = false;
michael@0 283 root.containerOpen = false;
michael@0 284 }
michael@0 285
michael@0 286 /**
michael@0 287 * Queries history grouped by site, checking containers' labels and children.
michael@0 288 */
michael@0 289 function test_RESULTS_AS_SITE_QUERY() {
michael@0 290 print("\n\n*** TEST RESULTS_AS_SITE_QUERY\n");
michael@0 291 // add a bookmark with a domain not in the set of visits in the db
michael@0 292 var itemId = bs.insertBookmark(bs.toolbarFolder, uri("http://foobar"),
michael@0 293 bs.DEFAULT_INDEX, "");
michael@0 294
michael@0 295 var options = hs.getNewQueryOptions();
michael@0 296 options.resultType = options.RESULTS_AS_SITE_QUERY;
michael@0 297 options.sortingMode = options.SORT_BY_TITLE_ASCENDING;
michael@0 298 var query = hs.getNewQuery();
michael@0 299 var result = hs.executeQuery(query, options);
michael@0 300 var root = result.root;
michael@0 301 root.containerOpen = true;
michael@0 302 do_check_eq(root.childCount, containers.length * 2);
michael@0 303
michael@0 304 /* Expected results:
michael@0 305 "mirror0.google.com",
michael@0 306 "mirror0.mozilla.com",
michael@0 307 "mirror1.google.com",
michael@0 308 "mirror1.mozilla.com",
michael@0 309 "mirror2.google.com",
michael@0 310 "mirror2.mozilla.com",
michael@0 311 "mirror3.google.com", <== We check for this site (index 6)
michael@0 312 "mirror3.mozilla.com",
michael@0 313 "mirror4.google.com",
michael@0 314 "mirror4.mozilla.com",
michael@0 315 "mirror5.google.com",
michael@0 316 "mirror5.mozilla.com",
michael@0 317 ...
michael@0 318 */
michael@0 319
michael@0 320 // Items should be sorted by host
michael@0 321 var siteNode = root.getChild(6)
michael@0 322 .QueryInterface(Ci.nsINavHistoryContainerResultNode);
michael@0 323 do_check_eq(siteNode.title, "mirror3.google.com");
michael@0 324
michael@0 325 siteNode.containerOpen = true;
michael@0 326 do_check_eq(siteNode.childCount, 2);
michael@0 327
michael@0 328 // Inside of host sites are sorted by title
michael@0 329 var visitNode = siteNode.getChild(0);
michael@0 330 do_check_eq(visitNode.uri, "http://mirror3.google.com/a");
michael@0 331
michael@0 332 // Bug 473157: changing sorting mode should not affect the containers
michael@0 333 result.sortingMode = options.SORT_BY_TITLE_DESCENDING;
michael@0 334 var siteNode = root.getChild(6)
michael@0 335 .QueryInterface(Ci.nsINavHistoryContainerResultNode);
michael@0 336 do_check_eq(siteNode.title, "mirror3.google.com");
michael@0 337
michael@0 338 siteNode.containerOpen = true;
michael@0 339 do_check_eq(siteNode.childCount, 2);
michael@0 340
michael@0 341 // But URLs are now sorted by title descending
michael@0 342 var visit = siteNode.getChild(0);
michael@0 343 do_check_eq(visit.uri, "http://mirror3.google.com/b");
michael@0 344
michael@0 345 siteNode.containerOpen = false;
michael@0 346 root.containerOpen = false;
michael@0 347
michael@0 348 // Cleanup.
michael@0 349 bs.removeItem(itemId);
michael@0 350 }
michael@0 351
michael@0 352 /**
michael@0 353 * Checks that queries grouped by date do liveupdate correctly.
michael@0 354 */
michael@0 355 function task_test_date_liveupdate(aResultType) {
michael@0 356 var midnight = nowObj;
michael@0 357 midnight.setHours(0);
michael@0 358 midnight.setMinutes(0);
michael@0 359 midnight.setSeconds(0);
michael@0 360 midnight.setMilliseconds(0);
michael@0 361
michael@0 362 // TEST 1. Test that the query correctly updates when it is root.
michael@0 363 var options = hs.getNewQueryOptions();
michael@0 364 options.resultType = aResultType;
michael@0 365 var query = hs.getNewQuery();
michael@0 366 var result = hs.executeQuery(query, options);
michael@0 367 var root = result.root;
michael@0 368 root.containerOpen = true;
michael@0 369
michael@0 370 do_check_eq(root.childCount, visibleContainers.length);
michael@0 371 // Remove "Today".
michael@0 372 hs.removePagesByTimeframe(midnight.getTime() * 1000, Date.now() * 1000);
michael@0 373 do_check_eq(root.childCount, visibleContainers.length - 1);
michael@0 374
michael@0 375 // Open "Last 7 days" container, this way we will have a container accepting
michael@0 376 // the new visit, but we should still add back "Today" container.
michael@0 377 var last7Days = root.getChild(1)
michael@0 378 .QueryInterface(Ci.nsINavHistoryContainerResultNode);
michael@0 379 last7Days.containerOpen = true;
michael@0 380
michael@0 381 // Add a visit for "Today". This should add back the missing "Today"
michael@0 382 // container.
michael@0 383 yield task_add_normalized_visit(uri("http://www.mozilla.org/"), nowObj.getTime(), 0);
michael@0 384 do_check_eq(root.childCount, visibleContainers.length);
michael@0 385
michael@0 386 last7Days.containerOpen = false;
michael@0 387 root.containerOpen = false;
michael@0 388
michael@0 389 // TEST 2. Test that the query correctly updates even if it is not root.
michael@0 390 var itemId = bs.insertBookmark(bs.toolbarFolder,
michael@0 391 uri("place:type=" + aResultType),
michael@0 392 bs.DEFAULT_INDEX, "");
michael@0 393
michael@0 394 // Query toolbar and open our query container, then check again liveupdate.
michael@0 395 options = hs.getNewQueryOptions();
michael@0 396 query = hs.getNewQuery();
michael@0 397 query.setFolders([bs.toolbarFolder], 1);
michael@0 398 result = hs.executeQuery(query, options);
michael@0 399 root = result.root;
michael@0 400 root.containerOpen = true;
michael@0 401 do_check_eq(root.childCount, 1);
michael@0 402 var dateContainer = root.getChild(0).QueryInterface(Ci.nsINavHistoryContainerResultNode);
michael@0 403 dateContainer.containerOpen = true;
michael@0 404
michael@0 405 do_check_eq(dateContainer.childCount, visibleContainers.length);
michael@0 406 // Remove "Today".
michael@0 407 hs.removePagesByTimeframe(midnight.getTime() * 1000, Date.now() * 1000);
michael@0 408 do_check_eq(dateContainer.childCount, visibleContainers.length - 1);
michael@0 409 // Add a visit for "Today".
michael@0 410 yield task_add_normalized_visit(uri("http://www.mozilla.org/"), nowObj.getTime(), 0);
michael@0 411 do_check_eq(dateContainer.childCount, visibleContainers.length);
michael@0 412
michael@0 413 dateContainer.containerOpen = false;
michael@0 414 root.containerOpen = false;
michael@0 415
michael@0 416 // Cleanup.
michael@0 417 bs.removeItem(itemId);
michael@0 418 }
michael@0 419
michael@0 420 function run_test()
michael@0 421 {
michael@0 422 run_next_test();
michael@0 423 }
michael@0 424
michael@0 425 add_task(function test_history_sidebar()
michael@0 426 {
michael@0 427 // If we're dangerously close to a date change, just bail out.
michael@0 428 if (nowObj.getHours() == 23 && nowObj.getMinutes() >= 50) {
michael@0 429 return;
michael@0 430 }
michael@0 431
michael@0 432 yield task_fill_history();
michael@0 433 test_RESULTS_AS_DATE_SITE_QUERY();
michael@0 434 test_RESULTS_AS_DATE_QUERY();
michael@0 435 test_RESULTS_AS_SITE_QUERY();
michael@0 436
michael@0 437 yield task_test_date_liveupdate(Ci.nsINavHistoryQueryOptions.RESULTS_AS_DATE_SITE_QUERY);
michael@0 438 yield task_test_date_liveupdate(Ci.nsINavHistoryQueryOptions.RESULTS_AS_DATE_QUERY);
michael@0 439
michael@0 440 // The remaining views are
michael@0 441 // RESULTS_AS_URI + SORT_BY_VISITCOUNT_DESCENDING
michael@0 442 // -> test_399266.js
michael@0 443 // RESULTS_AS_URI + SORT_BY_DATE_DESCENDING
michael@0 444 // -> test_385397.js
michael@0 445 });

mercurial