1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/places/tests/expiration/test_notifications_onDeleteVisits.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,135 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : 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 +/** 1.11 + * What this is aimed to test: 1.12 + * 1.13 + * Expiring only visits for a page, but not the full page, should fire an 1.14 + * onDeleteVisits notification. 1.15 + */ 1.16 + 1.17 +let hs = Cc["@mozilla.org/browser/nav-history-service;1"]. 1.18 + getService(Ci.nsINavHistoryService); 1.19 +let bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"]. 1.20 + getService(Ci.nsINavBookmarksService); 1.21 + 1.22 +let tests = [ 1.23 + 1.24 + { desc: "Add 1 bookmarked page.", 1.25 + addPages: 1, 1.26 + visitsPerPage: 1, 1.27 + addBookmarks: 1, 1.28 + limitExpiration: -1, 1.29 + expectedNotifications: 1, // Will expire visits for 1 page. 1.30 + }, 1.31 + 1.32 + { desc: "Add 2 pages, 1 bookmarked.", 1.33 + addPages: 2, 1.34 + visitsPerPage: 1, 1.35 + addBookmarks: 1, 1.36 + limitExpiration: -1, 1.37 + expectedNotifications: 1, // Will expire visits for 1 page. 1.38 + }, 1.39 + 1.40 + { desc: "Add 10 pages, none bookmarked.", 1.41 + addPages: 10, 1.42 + visitsPerPage: 1, 1.43 + addBookmarks: 0, 1.44 + limitExpiration: -1, 1.45 + expectedNotifications: 0, // Will expire only full pages. 1.46 + }, 1.47 + 1.48 + { desc: "Add 10 pages, all bookmarked.", 1.49 + addPages: 10, 1.50 + visitsPerPage: 1, 1.51 + addBookmarks: 10, 1.52 + limitExpiration: -1, 1.53 + expectedNotifications: 10, // Will expire visist for all pages. 1.54 + }, 1.55 + 1.56 + { desc: "Add 10 pages with lot of visits, none bookmarked.", 1.57 + addPages: 10, 1.58 + visitsPerPage: 10, 1.59 + addBookmarks: 0, 1.60 + limitExpiration: 10, 1.61 + expectedNotifications: 10, // Will expire 1 visist for each page, but won't 1.62 + }, // expire pages since they still have visits. 1.63 + 1.64 +]; 1.65 + 1.66 +function run_test() { 1.67 + run_next_test(); 1.68 +} 1.69 + 1.70 +add_task(function test_notifications_onDeleteVisits() { 1.71 + // Set interval to a large value so we don't expire on it. 1.72 + setInterval(3600); // 1h 1.73 + 1.74 + // Expire anything that is expirable. 1.75 + setMaxPages(0); 1.76 + 1.77 + for (let testIndex = 1; testIndex <= tests.length; testIndex++) { 1.78 + let currentTest = tests[testIndex -1]; 1.79 + print("\nTEST " + testIndex + ": " + currentTest.desc); 1.80 + currentTest.receivedNotifications = 0; 1.81 + 1.82 + // Setup visits. 1.83 + let now = getExpirablePRTime(); 1.84 + for (let j = 0; j < currentTest.visitsPerPage; j++) { 1.85 + for (let i = 0; i < currentTest.addPages; i++) { 1.86 + let page = "http://" + testIndex + "." + i + ".mozilla.org/"; 1.87 + yield promiseAddVisits({ uri: uri(page), visitDate: now++ }); 1.88 + } 1.89 + } 1.90 + 1.91 + // Setup bookmarks. 1.92 + currentTest.bookmarks = []; 1.93 + for (let i = 0; i < currentTest.addBookmarks; i++) { 1.94 + let page = "http://" + testIndex + "." + i + ".mozilla.org/"; 1.95 + bs.insertBookmark(bs.unfiledBookmarksFolder, uri(page), 1.96 + bs.DEFAULT_INDEX, null); 1.97 + currentTest.bookmarks.push(page); 1.98 + } 1.99 + 1.100 + // Observe history. 1.101 + historyObserver = { 1.102 + onBeginUpdateBatch: function PEX_onBeginUpdateBatch() {}, 1.103 + onEndUpdateBatch: function PEX_onEndUpdateBatch() {}, 1.104 + onClearHistory: function() {}, 1.105 + onVisit: function() {}, 1.106 + onTitleChanged: function() {}, 1.107 + onDeleteURI: function(aURI, aGUID, aReason) { 1.108 + // Check this uri was not bookmarked. 1.109 + do_check_eq(currentTest.bookmarks.indexOf(aURI.spec), -1); 1.110 + do_check_valid_places_guid(aGUID); 1.111 + do_check_eq(aReason, Ci.nsINavHistoryObserver.REASON_EXPIRED); 1.112 + }, 1.113 + onPageChanged: function() {}, 1.114 + onDeleteVisits: function(aURI, aTime, aGUID, aReason) { 1.115 + currentTest.receivedNotifications++; 1.116 + do_check_guid_for_uri(aURI, aGUID); 1.117 + do_check_eq(aReason, Ci.nsINavHistoryObserver.REASON_EXPIRED); 1.118 + }, 1.119 + }; 1.120 + hs.addObserver(historyObserver, false); 1.121 + 1.122 + // Expire now. 1.123 + yield promiseForceExpirationStep(currentTest.limitExpiration); 1.124 + 1.125 + hs.removeObserver(historyObserver, false); 1.126 + 1.127 + do_check_eq(currentTest.receivedNotifications, 1.128 + currentTest.expectedNotifications); 1.129 + 1.130 + // Clean up. 1.131 + bs.removeFolderChildren(bs.unfiledBookmarksFolder); 1.132 + yield promiseClearHistory(); 1.133 + } 1.134 + 1.135 + clearMaxPages(); 1.136 + bs.removeFolderChildren(bs.unfiledBookmarksFolder); 1.137 + yield promiseClearHistory(); 1.138 +});