toolkit/components/places/tests/expiration/test_pref_maxpages.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/expiration/test_pref_maxpages.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,124 @@
     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 + * Expiration will obey to hardware spec, but user can set a custom maximum
    1.14 + * number of pages to retain, to restrict history, through
    1.15 + * "places.history.expiration.max_pages".
    1.16 + * This limit is used at next expiration run.
    1.17 + * If the pref is set to a number < 0 we will use the default value.
    1.18 + */
    1.19 +
    1.20 +let hs = Cc["@mozilla.org/browser/nav-history-service;1"].
    1.21 +         getService(Ci.nsINavHistoryService);
    1.22 +
    1.23 +let tests = [
    1.24 +
    1.25 +  { desc: "Set max_pages to a negative value, with 1 page.",
    1.26 +    maxPages: -1,
    1.27 +    addPages: 1,
    1.28 +    expectedNotifications: 0, // Will ignore and won't expire anything.
    1.29 +  },
    1.30 +
    1.31 +  { desc: "Set max_pages to 0.",
    1.32 +    maxPages: 0,
    1.33 +    addPages: 1,
    1.34 +    expectedNotifications: 1,
    1.35 +  },
    1.36 +
    1.37 +  { desc: "Set max_pages to 0, with 2 pages.",
    1.38 +    maxPages: 0,
    1.39 +    addPages: 2,
    1.40 +    expectedNotifications: 2, // Will expire everything.
    1.41 +  },
    1.42 +
    1.43 +  // Notice if we are over limit we do a full step of expiration.  So we ensure
    1.44 +  // that we will expire if we are over the limit, but we don't ensure that we
    1.45 +  // will expire exactly up to the limit.  Thus in this case we expire
    1.46 +  // everything.
    1.47 +  { desc: "Set max_pages to 1 with 2 pages.",
    1.48 +    maxPages: 1,
    1.49 +    addPages: 2,
    1.50 +    expectedNotifications: 2, // Will expire everything (in this case).
    1.51 +  },
    1.52 +
    1.53 +  { desc: "Set max_pages to 10, with 9 pages.",
    1.54 +    maxPages: 10,
    1.55 +    addPages: 9,
    1.56 +    expectedNotifications: 0, // We are at the limit, won't expire anything.
    1.57 +  },
    1.58 +
    1.59 +  { desc: "Set max_pages to 10 with 10 pages.",
    1.60 +    maxPages: 10,
    1.61 +    addPages: 10,
    1.62 +    expectedNotifications: 0, // We are below the limit, won't expire anything.
    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_pref_maxpages() {
    1.71 +  // The pref should not exist by default.
    1.72 +  try {
    1.73 +    getMaxPages();
    1.74 +    do_throw("interval pref should not exist by default");
    1.75 +  }
    1.76 +  catch (ex) {}
    1.77 +
    1.78 +  // Set interval to a large value so we don't expire on it.
    1.79 +  setInterval(3600); // 1h
    1.80 +
    1.81 +  for (let testIndex = 1; testIndex <= tests.length; testIndex++) {
    1.82 +    let currentTest = tests[testIndex -1];
    1.83 +    print("\nTEST " + testIndex + ": " + currentTest.desc);
    1.84 +    currentTest.receivedNotifications = 0;
    1.85 +
    1.86 +    // Setup visits.
    1.87 +    let now = getExpirablePRTime();
    1.88 +    for (let i = 0; i < currentTest.addPages; i++) {
    1.89 +      let page = "http://" + testIndex + "." + i + ".mozilla.org/";
    1.90 +      yield promiseAddVisits({ uri: uri(page), visitDate: now++ });
    1.91 +    }
    1.92 +
    1.93 +    // Observe history.
    1.94 +    historyObserver = {
    1.95 +      onBeginUpdateBatch: function PEX_onBeginUpdateBatch() {},
    1.96 +      onEndUpdateBatch: function PEX_onEndUpdateBatch() {},
    1.97 +      onClearHistory: function() {},
    1.98 +      onVisit: function() {},
    1.99 +      onTitleChanged: function() {},
   1.100 +      onDeleteURI: function(aURI) {
   1.101 +        print("onDeleteURI " + aURI.spec);
   1.102 +        currentTest.receivedNotifications++;
   1.103 +      },
   1.104 +      onPageChanged: function() {},
   1.105 +      onDeleteVisits: function(aURI, aTime) {
   1.106 +        print("onDeleteVisits " + aURI.spec + " " + aTime);
   1.107 +      },
   1.108 +    };
   1.109 +    hs.addObserver(historyObserver, false);
   1.110 +
   1.111 +    setMaxPages(currentTest.maxPages);
   1.112 +
   1.113 +    // Expire now.
   1.114 +    yield promiseForceExpirationStep(-1);
   1.115 +
   1.116 +    hs.removeObserver(historyObserver, false);
   1.117 +
   1.118 +    do_check_eq(currentTest.receivedNotifications,
   1.119 +                currentTest.expectedNotifications);
   1.120 +
   1.121 +    // Clean up.
   1.122 +    yield promiseClearHistory();
   1.123 +  }
   1.124 +
   1.125 +  clearMaxPages();
   1.126 +  yield promiseClearHistory();
   1.127 +});

mercurial