michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * What this is aimed to test: michael@0: * michael@0: * Expiration will obey to hardware spec, but user can set a custom maximum michael@0: * number of pages to retain, to restrict history, through michael@0: * "places.history.expiration.max_pages". michael@0: * This limit is used at next expiration run. michael@0: * If the pref is set to a number < 0 we will use the default value. michael@0: */ michael@0: michael@0: let hs = Cc["@mozilla.org/browser/nav-history-service;1"]. michael@0: getService(Ci.nsINavHistoryService); michael@0: michael@0: let tests = [ michael@0: michael@0: { desc: "Set max_pages to a negative value, with 1 page.", michael@0: maxPages: -1, michael@0: addPages: 1, michael@0: expectedNotifications: 0, // Will ignore and won't expire anything. michael@0: }, michael@0: michael@0: { desc: "Set max_pages to 0.", michael@0: maxPages: 0, michael@0: addPages: 1, michael@0: expectedNotifications: 1, michael@0: }, michael@0: michael@0: { desc: "Set max_pages to 0, with 2 pages.", michael@0: maxPages: 0, michael@0: addPages: 2, michael@0: expectedNotifications: 2, // Will expire everything. michael@0: }, michael@0: michael@0: // Notice if we are over limit we do a full step of expiration. So we ensure michael@0: // that we will expire if we are over the limit, but we don't ensure that we michael@0: // will expire exactly up to the limit. Thus in this case we expire michael@0: // everything. michael@0: { desc: "Set max_pages to 1 with 2 pages.", michael@0: maxPages: 1, michael@0: addPages: 2, michael@0: expectedNotifications: 2, // Will expire everything (in this case). michael@0: }, michael@0: michael@0: { desc: "Set max_pages to 10, with 9 pages.", michael@0: maxPages: 10, michael@0: addPages: 9, michael@0: expectedNotifications: 0, // We are at the limit, won't expire anything. michael@0: }, michael@0: michael@0: { desc: "Set max_pages to 10 with 10 pages.", michael@0: maxPages: 10, michael@0: addPages: 10, michael@0: expectedNotifications: 0, // We are below the limit, won't expire anything. michael@0: }, michael@0: ]; michael@0: michael@0: function run_test() { michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_task(function test_pref_maxpages() { michael@0: // The pref should not exist by default. michael@0: try { michael@0: getMaxPages(); michael@0: do_throw("interval pref should not exist by default"); michael@0: } michael@0: catch (ex) {} michael@0: michael@0: // Set interval to a large value so we don't expire on it. michael@0: setInterval(3600); // 1h michael@0: michael@0: for (let testIndex = 1; testIndex <= tests.length; testIndex++) { michael@0: let currentTest = tests[testIndex -1]; michael@0: print("\nTEST " + testIndex + ": " + currentTest.desc); michael@0: currentTest.receivedNotifications = 0; michael@0: michael@0: // Setup visits. michael@0: let now = getExpirablePRTime(); michael@0: for (let i = 0; i < currentTest.addPages; i++) { michael@0: let page = "http://" + testIndex + "." + i + ".mozilla.org/"; michael@0: yield promiseAddVisits({ uri: uri(page), visitDate: now++ }); michael@0: } michael@0: michael@0: // Observe history. michael@0: historyObserver = { michael@0: onBeginUpdateBatch: function PEX_onBeginUpdateBatch() {}, michael@0: onEndUpdateBatch: function PEX_onEndUpdateBatch() {}, michael@0: onClearHistory: function() {}, michael@0: onVisit: function() {}, michael@0: onTitleChanged: function() {}, michael@0: onDeleteURI: function(aURI) { michael@0: print("onDeleteURI " + aURI.spec); michael@0: currentTest.receivedNotifications++; michael@0: }, michael@0: onPageChanged: function() {}, michael@0: onDeleteVisits: function(aURI, aTime) { michael@0: print("onDeleteVisits " + aURI.spec + " " + aTime); michael@0: }, michael@0: }; michael@0: hs.addObserver(historyObserver, false); michael@0: michael@0: setMaxPages(currentTest.maxPages); michael@0: michael@0: // Expire now. michael@0: yield promiseForceExpirationStep(-1); michael@0: michael@0: hs.removeObserver(historyObserver, false); michael@0: michael@0: do_check_eq(currentTest.receivedNotifications, michael@0: currentTest.expectedNotifications); michael@0: michael@0: // Clean up. michael@0: yield promiseClearHistory(); michael@0: } michael@0: michael@0: clearMaxPages(); michael@0: yield promiseClearHistory(); michael@0: });