Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : |
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 | /** |
michael@0 | 8 | * What this is aimed to test: |
michael@0 | 9 | * |
michael@0 | 10 | * Expiration will obey to hardware spec, but user can set a custom maximum |
michael@0 | 11 | * number of pages to retain, to restrict history, through |
michael@0 | 12 | * "places.history.expiration.max_pages". |
michael@0 | 13 | * This limit is used at next expiration run. |
michael@0 | 14 | * If the pref is set to a number < 0 we will use the default value. |
michael@0 | 15 | */ |
michael@0 | 16 | |
michael@0 | 17 | let hs = Cc["@mozilla.org/browser/nav-history-service;1"]. |
michael@0 | 18 | getService(Ci.nsINavHistoryService); |
michael@0 | 19 | |
michael@0 | 20 | let tests = [ |
michael@0 | 21 | |
michael@0 | 22 | { desc: "Set max_pages to a negative value, with 1 page.", |
michael@0 | 23 | maxPages: -1, |
michael@0 | 24 | addPages: 1, |
michael@0 | 25 | expectedNotifications: 0, // Will ignore and won't expire anything. |
michael@0 | 26 | }, |
michael@0 | 27 | |
michael@0 | 28 | { desc: "Set max_pages to 0.", |
michael@0 | 29 | maxPages: 0, |
michael@0 | 30 | addPages: 1, |
michael@0 | 31 | expectedNotifications: 1, |
michael@0 | 32 | }, |
michael@0 | 33 | |
michael@0 | 34 | { desc: "Set max_pages to 0, with 2 pages.", |
michael@0 | 35 | maxPages: 0, |
michael@0 | 36 | addPages: 2, |
michael@0 | 37 | expectedNotifications: 2, // Will expire everything. |
michael@0 | 38 | }, |
michael@0 | 39 | |
michael@0 | 40 | // Notice if we are over limit we do a full step of expiration. So we ensure |
michael@0 | 41 | // that we will expire if we are over the limit, but we don't ensure that we |
michael@0 | 42 | // will expire exactly up to the limit. Thus in this case we expire |
michael@0 | 43 | // everything. |
michael@0 | 44 | { desc: "Set max_pages to 1 with 2 pages.", |
michael@0 | 45 | maxPages: 1, |
michael@0 | 46 | addPages: 2, |
michael@0 | 47 | expectedNotifications: 2, // Will expire everything (in this case). |
michael@0 | 48 | }, |
michael@0 | 49 | |
michael@0 | 50 | { desc: "Set max_pages to 10, with 9 pages.", |
michael@0 | 51 | maxPages: 10, |
michael@0 | 52 | addPages: 9, |
michael@0 | 53 | expectedNotifications: 0, // We are at the limit, won't expire anything. |
michael@0 | 54 | }, |
michael@0 | 55 | |
michael@0 | 56 | { desc: "Set max_pages to 10 with 10 pages.", |
michael@0 | 57 | maxPages: 10, |
michael@0 | 58 | addPages: 10, |
michael@0 | 59 | expectedNotifications: 0, // We are below the limit, won't expire anything. |
michael@0 | 60 | }, |
michael@0 | 61 | ]; |
michael@0 | 62 | |
michael@0 | 63 | function run_test() { |
michael@0 | 64 | run_next_test(); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | add_task(function test_pref_maxpages() { |
michael@0 | 68 | // The pref should not exist by default. |
michael@0 | 69 | try { |
michael@0 | 70 | getMaxPages(); |
michael@0 | 71 | do_throw("interval pref should not exist by default"); |
michael@0 | 72 | } |
michael@0 | 73 | catch (ex) {} |
michael@0 | 74 | |
michael@0 | 75 | // Set interval to a large value so we don't expire on it. |
michael@0 | 76 | setInterval(3600); // 1h |
michael@0 | 77 | |
michael@0 | 78 | for (let testIndex = 1; testIndex <= tests.length; testIndex++) { |
michael@0 | 79 | let currentTest = tests[testIndex -1]; |
michael@0 | 80 | print("\nTEST " + testIndex + ": " + currentTest.desc); |
michael@0 | 81 | currentTest.receivedNotifications = 0; |
michael@0 | 82 | |
michael@0 | 83 | // Setup visits. |
michael@0 | 84 | let now = getExpirablePRTime(); |
michael@0 | 85 | for (let i = 0; i < currentTest.addPages; i++) { |
michael@0 | 86 | let page = "http://" + testIndex + "." + i + ".mozilla.org/"; |
michael@0 | 87 | yield promiseAddVisits({ uri: uri(page), visitDate: now++ }); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | // Observe history. |
michael@0 | 91 | historyObserver = { |
michael@0 | 92 | onBeginUpdateBatch: function PEX_onBeginUpdateBatch() {}, |
michael@0 | 93 | onEndUpdateBatch: function PEX_onEndUpdateBatch() {}, |
michael@0 | 94 | onClearHistory: function() {}, |
michael@0 | 95 | onVisit: function() {}, |
michael@0 | 96 | onTitleChanged: function() {}, |
michael@0 | 97 | onDeleteURI: function(aURI) { |
michael@0 | 98 | print("onDeleteURI " + aURI.spec); |
michael@0 | 99 | currentTest.receivedNotifications++; |
michael@0 | 100 | }, |
michael@0 | 101 | onPageChanged: function() {}, |
michael@0 | 102 | onDeleteVisits: function(aURI, aTime) { |
michael@0 | 103 | print("onDeleteVisits " + aURI.spec + " " + aTime); |
michael@0 | 104 | }, |
michael@0 | 105 | }; |
michael@0 | 106 | hs.addObserver(historyObserver, false); |
michael@0 | 107 | |
michael@0 | 108 | setMaxPages(currentTest.maxPages); |
michael@0 | 109 | |
michael@0 | 110 | // Expire now. |
michael@0 | 111 | yield promiseForceExpirationStep(-1); |
michael@0 | 112 | |
michael@0 | 113 | hs.removeObserver(historyObserver, false); |
michael@0 | 114 | |
michael@0 | 115 | do_check_eq(currentTest.receivedNotifications, |
michael@0 | 116 | currentTest.expectedNotifications); |
michael@0 | 117 | |
michael@0 | 118 | // Clean up. |
michael@0 | 119 | yield promiseClearHistory(); |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | clearMaxPages(); |
michael@0 | 123 | yield promiseClearHistory(); |
michael@0 | 124 | }); |