1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/places/tests/unit/test_history_sidebar.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,445 @@ 1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +// Get history service 1.11 +var hs = Cc["@mozilla.org/browser/nav-history-service;1"]. 1.12 + getService(Ci.nsINavHistoryService); 1.13 +var bh = hs.QueryInterface(Ci.nsIBrowserHistory); 1.14 +var bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"]. 1.15 + getService(Ci.nsINavBookmarksService); 1.16 +var ps = Cc["@mozilla.org/preferences-service;1"]. 1.17 + getService(Ci.nsIPrefBranch); 1.18 + 1.19 +/** 1.20 + * Adds a test URI visit to the database. 1.21 + * 1.22 + * @param aURI 1.23 + * The URI to add a visit for. 1.24 + * @param aTime 1.25 + * Reference "now" time. 1.26 + * @param aDayOffset 1.27 + * number of days to add, pass a negative value to subtract them. 1.28 + */ 1.29 +function task_add_normalized_visit(aURI, aTime, aDayOffset) { 1.30 + var dateObj = new Date(aTime); 1.31 + // Normalize to midnight 1.32 + dateObj.setHours(0); 1.33 + dateObj.setMinutes(0); 1.34 + dateObj.setSeconds(0); 1.35 + dateObj.setMilliseconds(0); 1.36 + // Days where DST changes should be taken in count. 1.37 + var previousDateObj = new Date(dateObj.getTime() + aDayOffset * 86400000); 1.38 + var DSTCorrection = (dateObj.getTimezoneOffset() - 1.39 + previousDateObj.getTimezoneOffset()) * 60 * 1000; 1.40 + // Substract aDayOffset 1.41 + var PRTimeWithOffset = (previousDateObj.getTime() - DSTCorrection) * 1000; 1.42 + var timeInMs = new Date(PRTimeWithOffset/1000); 1.43 + print("Adding visit to " + aURI.spec + " at " + timeInMs); 1.44 + yield promiseAddVisits({ 1.45 + uri: aURI, 1.46 + visitDate: PRTimeWithOffset 1.47 + }); 1.48 +} 1.49 + 1.50 +function days_for_x_months_ago(aNowObj, aMonths) { 1.51 + var oldTime = new Date(); 1.52 + // Set day before month, otherwise we could try to calculate 30 February, or 1.53 + // other nonexistent days. 1.54 + oldTime.setDate(1); 1.55 + oldTime.setMonth(aNowObj.getMonth() - aMonths); 1.56 + oldTime.setHours(0); 1.57 + oldTime.setMinutes(0); 1.58 + oldTime.setSeconds(0); 1.59 + // Stay larger for eventual timezone issues, add 2 days. 1.60 + return parseInt((aNowObj - oldTime) / (1000*60*60*24)) + 2; 1.61 +} 1.62 + 1.63 +var nowObj = new Date(); 1.64 +// This test relies on en-US locale 1.65 +// Offset is number of days 1.66 +var containers = [ 1.67 + { label: "Today" , offset: 0 , visible: true }, 1.68 + { label: "Yesterday" , offset: -1 , visible: true }, 1.69 + { label: "Last 7 days" , offset: -3 , visible: true }, 1.70 + { label: "This month" , offset: -8 , visible: nowObj.getDate() > 8 }, 1.71 + { label: "" , offset: -days_for_x_months_ago(nowObj, 0) , visible: true }, 1.72 + { label: "" , offset: -days_for_x_months_ago(nowObj, 1) , visible: true }, 1.73 + { label: "" , offset: -days_for_x_months_ago(nowObj, 2) , visible: true }, 1.74 + { label: "" , offset: -days_for_x_months_ago(nowObj, 3) , visible: true }, 1.75 + { label: "" , offset: -days_for_x_months_ago(nowObj, 4) , visible: true }, 1.76 + { label: "Older than 6 months" , offset: -days_for_x_months_ago(nowObj, 5) , visible: true }, 1.77 +]; 1.78 + 1.79 +var visibleContainers = containers.filter( 1.80 + function(aContainer) {return aContainer.visible}); 1.81 + 1.82 +/** 1.83 + * Asynchronous task that fills history and checks containers' labels. 1.84 + */ 1.85 +function task_fill_history() { 1.86 + print("\n\n*** TEST Fill History\n"); 1.87 + // We can't use "now" because our hardcoded offsets would be invalid for some 1.88 + // date. So we hardcode a date. 1.89 + for (var i = 0; i < containers.length; i++) { 1.90 + var container = containers[i]; 1.91 + var testURI = uri("http://mirror"+i+".mozilla.com/b"); 1.92 + yield task_add_normalized_visit(testURI, nowObj.getTime(), container.offset); 1.93 + var testURI = uri("http://mirror"+i+".mozilla.com/a"); 1.94 + yield task_add_normalized_visit(testURI, nowObj.getTime(), container.offset); 1.95 + var testURI = uri("http://mirror"+i+".google.com/b"); 1.96 + yield task_add_normalized_visit(testURI, nowObj.getTime(), container.offset); 1.97 + var testURI = uri("http://mirror"+i+".google.com/a"); 1.98 + yield task_add_normalized_visit(testURI, nowObj.getTime(), container.offset); 1.99 + // Bug 485703 - Hide date containers not containing additional entries 1.100 + // compared to previous ones. 1.101 + // Check after every new container is added. 1.102 + check_visit(container.offset); 1.103 + } 1.104 + 1.105 + var options = hs.getNewQueryOptions(); 1.106 + options.resultType = options.RESULTS_AS_DATE_SITE_QUERY; 1.107 + var query = hs.getNewQuery(); 1.108 + 1.109 + var result = hs.executeQuery(query, options); 1.110 + var root = result.root; 1.111 + root.containerOpen = true; 1.112 + 1.113 + var cc = root.childCount; 1.114 + print("Found containers:"); 1.115 + var previousLabels = []; 1.116 + for (var i = 0; i < cc; i++) { 1.117 + var container = visibleContainers[i]; 1.118 + var node = root.getChild(i); 1.119 + print(node.title); 1.120 + if (container.label) 1.121 + do_check_eq(node.title, container.label); 1.122 + // Check labels are not repeated. 1.123 + do_check_eq(previousLabels.indexOf(node.title), -1); 1.124 + previousLabels.push(node.title); 1.125 + } 1.126 + do_check_eq(cc, visibleContainers.length); 1.127 + root.containerOpen = false; 1.128 +} 1.129 + 1.130 +/** 1.131 + * Bug 485703 - Hide date containers not containing additional entries compared 1.132 + * to previous ones. 1.133 + */ 1.134 +function check_visit(aOffset) { 1.135 + var options = hs.getNewQueryOptions(); 1.136 + options.resultType = options.RESULTS_AS_DATE_SITE_QUERY; 1.137 + var query = hs.getNewQuery(); 1.138 + var result = hs.executeQuery(query, options); 1.139 + var root = result.root; 1.140 + root.containerOpen = true; 1.141 + var cc = root.childCount; 1.142 + 1.143 + var unexpected = []; 1.144 + switch (aOffset) { 1.145 + case 0: 1.146 + unexpected = ["Yesterday", "Last 7 days", "This month"]; 1.147 + break; 1.148 + case -1: 1.149 + unexpected = ["Last 7 days", "This month"]; 1.150 + break; 1.151 + case -3: 1.152 + unexpected = ["This month"]; 1.153 + break; 1.154 + default: 1.155 + // Other containers are tested later. 1.156 + } 1.157 + 1.158 + print("Found containers:"); 1.159 + for (var i = 0; i < cc; i++) { 1.160 + var node = root.getChild(i); 1.161 + print(node.title); 1.162 + do_check_eq(unexpected.indexOf(node.title), -1); 1.163 + } 1.164 + 1.165 + root.containerOpen = false; 1.166 +} 1.167 + 1.168 +/** 1.169 + * Queries history grouped by date and site, checking containers' labels and 1.170 + * children. 1.171 + */ 1.172 +function test_RESULTS_AS_DATE_SITE_QUERY() { 1.173 + print("\n\n*** TEST RESULTS_AS_DATE_SITE_QUERY\n"); 1.174 + var options = hs.getNewQueryOptions(); 1.175 + options.resultType = options.RESULTS_AS_DATE_SITE_QUERY; 1.176 + var query = hs.getNewQuery(); 1.177 + var result = hs.executeQuery(query, options); 1.178 + var root = result.root; 1.179 + root.containerOpen = true; 1.180 + 1.181 + // Check one of the days 1.182 + var dayNode = root.getChild(0) 1.183 + .QueryInterface(Ci.nsINavHistoryContainerResultNode); 1.184 + dayNode.containerOpen = true; 1.185 + do_check_eq(dayNode.childCount, 2); 1.186 + 1.187 + // Items should be sorted by host 1.188 + var site1 = dayNode.getChild(0) 1.189 + .QueryInterface(Ci.nsINavHistoryContainerResultNode); 1.190 + do_check_eq(site1.title, "mirror0.google.com"); 1.191 + 1.192 + var site2 = dayNode.getChild(1) 1.193 + .QueryInterface(Ci.nsINavHistoryContainerResultNode); 1.194 + do_check_eq(site2.title, "mirror0.mozilla.com"); 1.195 + 1.196 + site1.containerOpen = true; 1.197 + do_check_eq(site1.childCount, 2); 1.198 + 1.199 + // Inside of host sites are sorted by title 1.200 + var site1visit = site1.getChild(0); 1.201 + do_check_eq(site1visit.uri, "http://mirror0.google.com/a"); 1.202 + 1.203 + // Bug 473157: changing sorting mode should not affect the containers 1.204 + result.sortingMode = options.SORT_BY_TITLE_DESCENDING; 1.205 + 1.206 + // Check one of the days 1.207 + var dayNode = root.getChild(0) 1.208 + .QueryInterface(Ci.nsINavHistoryContainerResultNode); 1.209 + dayNode.containerOpen = true; 1.210 + do_check_eq(dayNode.childCount, 2); 1.211 + 1.212 + // Hosts are still sorted by title 1.213 + var site1 = dayNode.getChild(0) 1.214 + .QueryInterface(Ci.nsINavHistoryContainerResultNode); 1.215 + do_check_eq(site1.title, "mirror0.google.com"); 1.216 + 1.217 + var site2 = dayNode.getChild(1) 1.218 + .QueryInterface(Ci.nsINavHistoryContainerResultNode); 1.219 + do_check_eq(site2.title, "mirror0.mozilla.com"); 1.220 + 1.221 + site1.containerOpen = true; 1.222 + do_check_eq(site1.childCount, 2); 1.223 + 1.224 + // But URLs are now sorted by title descending 1.225 + var site1visit = site1.getChild(0); 1.226 + do_check_eq(site1visit.uri, "http://mirror0.google.com/b"); 1.227 + 1.228 + site1.containerOpen = false; 1.229 + dayNode.containerOpen = false; 1.230 + root.containerOpen = false; 1.231 +} 1.232 + 1.233 +/** 1.234 + * Queries history grouped by date, checking containers' labels and children. 1.235 + */ 1.236 +function test_RESULTS_AS_DATE_QUERY() { 1.237 + print("\n\n*** TEST RESULTS_AS_DATE_QUERY\n"); 1.238 + var options = hs.getNewQueryOptions(); 1.239 + options.resultType = options.RESULTS_AS_DATE_QUERY; 1.240 + var query = hs.getNewQuery(); 1.241 + var result = hs.executeQuery(query, options); 1.242 + var root = result.root; 1.243 + root.containerOpen = true; 1.244 + 1.245 + var cc = root.childCount; 1.246 + do_check_eq(cc, visibleContainers.length); 1.247 + print("Found containers:"); 1.248 + for (var i = 0; i < cc; i++) { 1.249 + var container = visibleContainers[i]; 1.250 + var node = root.getChild(i); 1.251 + print(node.title); 1.252 + if (container.label) 1.253 + do_check_eq(node.title, container.label); 1.254 + } 1.255 + 1.256 + // Check one of the days 1.257 + var dayNode = root.getChild(0) 1.258 + .QueryInterface(Ci.nsINavHistoryContainerResultNode); 1.259 + dayNode.containerOpen = true; 1.260 + do_check_eq(dayNode.childCount, 4); 1.261 + 1.262 + // Items should be sorted by title 1.263 + var visit1 = dayNode.getChild(0); 1.264 + do_check_eq(visit1.uri, "http://mirror0.google.com/a"); 1.265 + 1.266 + var visit2 = dayNode.getChild(3); 1.267 + do_check_eq(visit2.uri, "http://mirror0.mozilla.com/b"); 1.268 + 1.269 + // Bug 473157: changing sorting mode should not affect the containers 1.270 + result.sortingMode = options.SORT_BY_TITLE_DESCENDING; 1.271 + 1.272 + // Check one of the days 1.273 + var dayNode = root.getChild(0) 1.274 + .QueryInterface(Ci.nsINavHistoryContainerResultNode); 1.275 + dayNode.containerOpen = true; 1.276 + do_check_eq(dayNode.childCount, 4); 1.277 + 1.278 + // But URLs are now sorted by title descending 1.279 + var visit1 = dayNode.getChild(0); 1.280 + do_check_eq(visit1.uri, "http://mirror0.mozilla.com/b"); 1.281 + 1.282 + var visit2 = dayNode.getChild(3); 1.283 + do_check_eq(visit2.uri, "http://mirror0.google.com/a"); 1.284 + 1.285 + dayNode.containerOpen = false; 1.286 + root.containerOpen = false; 1.287 +} 1.288 + 1.289 +/** 1.290 + * Queries history grouped by site, checking containers' labels and children. 1.291 + */ 1.292 +function test_RESULTS_AS_SITE_QUERY() { 1.293 + print("\n\n*** TEST RESULTS_AS_SITE_QUERY\n"); 1.294 + // add a bookmark with a domain not in the set of visits in the db 1.295 + var itemId = bs.insertBookmark(bs.toolbarFolder, uri("http://foobar"), 1.296 + bs.DEFAULT_INDEX, ""); 1.297 + 1.298 + var options = hs.getNewQueryOptions(); 1.299 + options.resultType = options.RESULTS_AS_SITE_QUERY; 1.300 + options.sortingMode = options.SORT_BY_TITLE_ASCENDING; 1.301 + var query = hs.getNewQuery(); 1.302 + var result = hs.executeQuery(query, options); 1.303 + var root = result.root; 1.304 + root.containerOpen = true; 1.305 + do_check_eq(root.childCount, containers.length * 2); 1.306 + 1.307 +/* Expected results: 1.308 + "mirror0.google.com", 1.309 + "mirror0.mozilla.com", 1.310 + "mirror1.google.com", 1.311 + "mirror1.mozilla.com", 1.312 + "mirror2.google.com", 1.313 + "mirror2.mozilla.com", 1.314 + "mirror3.google.com", <== We check for this site (index 6) 1.315 + "mirror3.mozilla.com", 1.316 + "mirror4.google.com", 1.317 + "mirror4.mozilla.com", 1.318 + "mirror5.google.com", 1.319 + "mirror5.mozilla.com", 1.320 + ... 1.321 +*/ 1.322 + 1.323 + // Items should be sorted by host 1.324 + var siteNode = root.getChild(6) 1.325 + .QueryInterface(Ci.nsINavHistoryContainerResultNode); 1.326 + do_check_eq(siteNode.title, "mirror3.google.com"); 1.327 + 1.328 + siteNode.containerOpen = true; 1.329 + do_check_eq(siteNode.childCount, 2); 1.330 + 1.331 + // Inside of host sites are sorted by title 1.332 + var visitNode = siteNode.getChild(0); 1.333 + do_check_eq(visitNode.uri, "http://mirror3.google.com/a"); 1.334 + 1.335 + // Bug 473157: changing sorting mode should not affect the containers 1.336 + result.sortingMode = options.SORT_BY_TITLE_DESCENDING; 1.337 + var siteNode = root.getChild(6) 1.338 + .QueryInterface(Ci.nsINavHistoryContainerResultNode); 1.339 + do_check_eq(siteNode.title, "mirror3.google.com"); 1.340 + 1.341 + siteNode.containerOpen = true; 1.342 + do_check_eq(siteNode.childCount, 2); 1.343 + 1.344 + // But URLs are now sorted by title descending 1.345 + var visit = siteNode.getChild(0); 1.346 + do_check_eq(visit.uri, "http://mirror3.google.com/b"); 1.347 + 1.348 + siteNode.containerOpen = false; 1.349 + root.containerOpen = false; 1.350 + 1.351 + // Cleanup. 1.352 + bs.removeItem(itemId); 1.353 +} 1.354 + 1.355 +/** 1.356 + * Checks that queries grouped by date do liveupdate correctly. 1.357 + */ 1.358 +function task_test_date_liveupdate(aResultType) { 1.359 + var midnight = nowObj; 1.360 + midnight.setHours(0); 1.361 + midnight.setMinutes(0); 1.362 + midnight.setSeconds(0); 1.363 + midnight.setMilliseconds(0); 1.364 + 1.365 + // TEST 1. Test that the query correctly updates when it is root. 1.366 + var options = hs.getNewQueryOptions(); 1.367 + options.resultType = aResultType; 1.368 + var query = hs.getNewQuery(); 1.369 + var result = hs.executeQuery(query, options); 1.370 + var root = result.root; 1.371 + root.containerOpen = true; 1.372 + 1.373 + do_check_eq(root.childCount, visibleContainers.length); 1.374 + // Remove "Today". 1.375 + hs.removePagesByTimeframe(midnight.getTime() * 1000, Date.now() * 1000); 1.376 + do_check_eq(root.childCount, visibleContainers.length - 1); 1.377 + 1.378 + // Open "Last 7 days" container, this way we will have a container accepting 1.379 + // the new visit, but we should still add back "Today" container. 1.380 + var last7Days = root.getChild(1) 1.381 + .QueryInterface(Ci.nsINavHistoryContainerResultNode); 1.382 + last7Days.containerOpen = true; 1.383 + 1.384 + // Add a visit for "Today". This should add back the missing "Today" 1.385 + // container. 1.386 + yield task_add_normalized_visit(uri("http://www.mozilla.org/"), nowObj.getTime(), 0); 1.387 + do_check_eq(root.childCount, visibleContainers.length); 1.388 + 1.389 + last7Days.containerOpen = false; 1.390 + root.containerOpen = false; 1.391 + 1.392 + // TEST 2. Test that the query correctly updates even if it is not root. 1.393 + var itemId = bs.insertBookmark(bs.toolbarFolder, 1.394 + uri("place:type=" + aResultType), 1.395 + bs.DEFAULT_INDEX, ""); 1.396 + 1.397 + // Query toolbar and open our query container, then check again liveupdate. 1.398 + options = hs.getNewQueryOptions(); 1.399 + query = hs.getNewQuery(); 1.400 + query.setFolders([bs.toolbarFolder], 1); 1.401 + result = hs.executeQuery(query, options); 1.402 + root = result.root; 1.403 + root.containerOpen = true; 1.404 + do_check_eq(root.childCount, 1); 1.405 + var dateContainer = root.getChild(0).QueryInterface(Ci.nsINavHistoryContainerResultNode); 1.406 + dateContainer.containerOpen = true; 1.407 + 1.408 + do_check_eq(dateContainer.childCount, visibleContainers.length); 1.409 + // Remove "Today". 1.410 + hs.removePagesByTimeframe(midnight.getTime() * 1000, Date.now() * 1000); 1.411 + do_check_eq(dateContainer.childCount, visibleContainers.length - 1); 1.412 + // Add a visit for "Today". 1.413 + yield task_add_normalized_visit(uri("http://www.mozilla.org/"), nowObj.getTime(), 0); 1.414 + do_check_eq(dateContainer.childCount, visibleContainers.length); 1.415 + 1.416 + dateContainer.containerOpen = false; 1.417 + root.containerOpen = false; 1.418 + 1.419 + // Cleanup. 1.420 + bs.removeItem(itemId); 1.421 +} 1.422 + 1.423 +function run_test() 1.424 +{ 1.425 + run_next_test(); 1.426 +} 1.427 + 1.428 +add_task(function test_history_sidebar() 1.429 +{ 1.430 + // If we're dangerously close to a date change, just bail out. 1.431 + if (nowObj.getHours() == 23 && nowObj.getMinutes() >= 50) { 1.432 + return; 1.433 + } 1.434 + 1.435 + yield task_fill_history(); 1.436 + test_RESULTS_AS_DATE_SITE_QUERY(); 1.437 + test_RESULTS_AS_DATE_QUERY(); 1.438 + test_RESULTS_AS_SITE_QUERY(); 1.439 + 1.440 + yield task_test_date_liveupdate(Ci.nsINavHistoryQueryOptions.RESULTS_AS_DATE_SITE_QUERY); 1.441 + yield task_test_date_liveupdate(Ci.nsINavHistoryQueryOptions.RESULTS_AS_DATE_QUERY); 1.442 + 1.443 + // The remaining views are 1.444 + // RESULTS_AS_URI + SORT_BY_VISITCOUNT_DESCENDING 1.445 + // -> test_399266.js 1.446 + // RESULTS_AS_URI + SORT_BY_DATE_DESCENDING 1.447 + // -> test_385397.js 1.448 +});