mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-03-02 14:30:43 +00:00
Bug 520165 - Part13: New expiration tests, r=mano
This commit is contained in:
parent
ed084077d7
commit
80955263c8
@ -48,6 +48,7 @@ MODULE = test_places
|
||||
|
||||
XPCSHELL_TESTS = \
|
||||
autocomplete \
|
||||
expiration \
|
||||
sync \
|
||||
bookmarks \
|
||||
queries \
|
||||
|
369
toolkit/components/places/tests/expiration/head_expiration.js
Normal file
369
toolkit/components/places/tests/expiration/head_expiration.js
Normal file
@ -0,0 +1,369 @@
|
||||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
||||
* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Places Unit Tests.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Mozilla Foundation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2009
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Marco Bonardo <mak77@bonardo.net> (Original Author)
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
const NS_APP_USER_PROFILE_50_DIR = "ProfD";
|
||||
const NS_APP_HISTORY_50_FILE = "UHist";
|
||||
|
||||
const Ci = Components.interfaces;
|
||||
const Cc = Components.classes;
|
||||
const Cr = Components.results;
|
||||
|
||||
function LOG(aMsg) {
|
||||
aMsg = ("*** PLACES TESTS: " + aMsg);
|
||||
Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).
|
||||
logStringMessage(aMsg);
|
||||
print(aMsg);
|
||||
}
|
||||
|
||||
// If there's no location registered for the profile direcotry, register one now.
|
||||
var dirSvc = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
|
||||
var profileDir = null;
|
||||
try {
|
||||
profileDir = dirSvc.get(NS_APP_USER_PROFILE_50_DIR, Ci.nsIFile);
|
||||
} catch (e) {}
|
||||
if (!profileDir) {
|
||||
// Register our own provider for the profile directory.
|
||||
// It will simply return the current directory.
|
||||
var provider = {
|
||||
getFile: function(prop, persistent) {
|
||||
persistent.value = true;
|
||||
if (prop == NS_APP_USER_PROFILE_50_DIR)
|
||||
return dirSvc.get("CurProcD", Ci.nsIFile);
|
||||
if (prop == NS_APP_HISTORY_50_FILE) {
|
||||
var histFile = dirSvc.get("CurProcD", Ci.nsIFile);
|
||||
histFile.append("history.dat");
|
||||
return histFile;
|
||||
}
|
||||
throw Cr.NS_ERROR_FAILURE;
|
||||
},
|
||||
QueryInterface: function(iid) {
|
||||
if (iid.equals(Ci.nsIDirectoryServiceProvider) ||
|
||||
iid.equals(Ci.nsISupports)) {
|
||||
return this;
|
||||
}
|
||||
throw Cr.NS_ERROR_NO_INTERFACE;
|
||||
}
|
||||
};
|
||||
dirSvc.QueryInterface(Ci.nsIDirectoryService).registerProvider(provider);
|
||||
}
|
||||
|
||||
var iosvc = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
|
||||
|
||||
function uri(spec) {
|
||||
return iosvc.newURI(spec, null, null);
|
||||
}
|
||||
|
||||
// Delete a previously created sqlite file
|
||||
function clearDB() {
|
||||
try {
|
||||
var file = dirSvc.get('ProfD', Ci.nsIFile);
|
||||
file.append("places.sqlite");
|
||||
if (file.exists())
|
||||
file.remove(false);
|
||||
} catch(ex) { dump("Exception: " + ex); }
|
||||
}
|
||||
clearDB();
|
||||
|
||||
/**
|
||||
* Dumps the rows of a table out to the console.
|
||||
*
|
||||
* @param aName
|
||||
* The name of the table or view to output.
|
||||
*/
|
||||
function dump_table(aName)
|
||||
{
|
||||
let db = DBConn()
|
||||
let stmt = db.createStatement("SELECT * FROM " + aName);
|
||||
|
||||
dump("\n*** Printing data from " + aName + ":\n");
|
||||
let count = 0;
|
||||
while (stmt.executeStep()) {
|
||||
let columns = stmt.numEntries;
|
||||
|
||||
if (count == 0) {
|
||||
// Print the column names.
|
||||
for (let i = 0; i < columns; i++)
|
||||
dump(stmt.getColumnName(i) + "\t");
|
||||
dump("\n");
|
||||
}
|
||||
|
||||
// Print the row.
|
||||
for (let i = 0; i < columns; i++) {
|
||||
switch (stmt.getTypeOfIndex(i)) {
|
||||
case Ci.mozIStorageValueArray.VALUE_TYPE_NULL:
|
||||
dump("NULL\t");
|
||||
break;
|
||||
case Ci.mozIStorageValueArray.VALUE_TYPE_INTEGER:
|
||||
dump(stmt.getInt64(i) + "\t");
|
||||
break;
|
||||
case Ci.mozIStorageValueArray.VALUE_TYPE_FLOAT:
|
||||
dump(stmt.getDouble(i) + "\t");
|
||||
break;
|
||||
case Ci.mozIStorageValueArray.VALUE_TYPE_TEXT:
|
||||
dump(stmt.getString(i) + "\t");
|
||||
break;
|
||||
}
|
||||
}
|
||||
dump("\n");
|
||||
|
||||
count++;
|
||||
}
|
||||
dump("*** There were a total of " + count + " rows of data.\n\n");
|
||||
|
||||
stmt.reset();
|
||||
stmt.finalize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Function tests to see if the place associated with the bookmark with id
|
||||
* aBookmarkId has the uri aExpectedURI. The event will call do_test_finished()
|
||||
* if aFinish is true.
|
||||
*
|
||||
* @param aBookmarkId
|
||||
* The bookmark to check against.
|
||||
* @param aExpectedURI
|
||||
* The URI we expect to be in moz_places.
|
||||
* @param aExpected
|
||||
* Indicates if we expect to get a result or not.
|
||||
* @param [optional] aFinish
|
||||
* Indicates if the test should be completed or not.
|
||||
*/
|
||||
function new_test_bookmark_uri_event(aBookmarkId, aExpectedURI, aExpected, aFinish)
|
||||
{
|
||||
let db = DBConn();
|
||||
let stmt = db.createStatement(
|
||||
"SELECT moz_places.url " +
|
||||
"FROM moz_bookmarks INNER JOIN moz_places " +
|
||||
"ON moz_bookmarks.fk = moz_places.id " +
|
||||
"WHERE moz_bookmarks.id = ?1"
|
||||
);
|
||||
stmt.bindInt64Parameter(0, aBookmarkId);
|
||||
|
||||
if (aExpected) {
|
||||
do_check_true(stmt.executeStep());
|
||||
do_check_eq(stmt.getUTF8String(0), aExpectedURI);
|
||||
}
|
||||
else {
|
||||
do_check_false(stmt.executeStep());
|
||||
}
|
||||
stmt.reset();
|
||||
stmt.finalize();
|
||||
|
||||
if (aFinish)
|
||||
do_test_finished();
|
||||
}
|
||||
|
||||
/**
|
||||
* Function tests to see if the place associated with the visit with id aVisitId
|
||||
* has the uri aExpectedURI. The event will call do_test_finished() if aFinish is
|
||||
* true.
|
||||
*
|
||||
* @param aVisitId
|
||||
* The visit to check against.
|
||||
* @param aExpectedURI
|
||||
* The URI we expect to be in moz_places.
|
||||
* @param aExpected
|
||||
* Indicates if we expect to get a result or not.
|
||||
* @param [optional] aFinish
|
||||
* Indicates if the test should be completed or not.
|
||||
*/
|
||||
function new_test_visit_uri_event(aVisitId, aExpectedURI, aExpected, aFinish)
|
||||
{
|
||||
let db = DBConn();
|
||||
let stmt = db.createStatement(
|
||||
"SELECT moz_places.url " +
|
||||
"FROM moz_historyvisits INNER JOIN moz_places " +
|
||||
"ON moz_historyvisits.place_id = moz_places.id " +
|
||||
"WHERE moz_historyvisits.id = ?1"
|
||||
);
|
||||
stmt.bindInt64Parameter(0, aVisitId);
|
||||
|
||||
if (aExpected) {
|
||||
do_check_true(stmt.executeStep());
|
||||
do_check_eq(stmt.getUTF8String(0), aExpectedURI);
|
||||
}
|
||||
else {
|
||||
do_check_false(stmt.executeStep());
|
||||
}
|
||||
stmt.reset();
|
||||
stmt.finalize();
|
||||
|
||||
if (aFinish)
|
||||
do_test_finished();
|
||||
}
|
||||
|
||||
/**
|
||||
* Function gets current database connection, if the connection has been closed
|
||||
* it will try to reconnect to the places.sqlite database.
|
||||
*/
|
||||
function DBConn()
|
||||
{
|
||||
let db = Cc["@mozilla.org/browser/nav-history-service;1"].
|
||||
getService(Ci.nsPIPlacesDatabase).
|
||||
DBConnection;
|
||||
if (db.connectionReady)
|
||||
return db;
|
||||
|
||||
// open a new connection if needed
|
||||
let file = dirSvc.get('ProfD', Ci.nsIFile);
|
||||
file.append("places.sqlite");
|
||||
let storageService = Cc["@mozilla.org/storage/service;1"].
|
||||
getService(Ci.mozIStorageService);
|
||||
|
||||
try {
|
||||
return dbConn = storageService.openDatabase(file);
|
||||
}
|
||||
catch(ex) {}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flushes any events in the event loop of the main thread.
|
||||
*/
|
||||
function flush_main_thread_events()
|
||||
{
|
||||
let tm = Cc["@mozilla.org/thread-manager;1"].getService(Ci.nsIThreadManager);
|
||||
while (tm.mainThread.hasPendingEvents())
|
||||
tm.mainThread.processNextEvent(false);
|
||||
}
|
||||
|
||||
// Simulates a Places shutdown.
|
||||
function shutdownPlaces()
|
||||
{
|
||||
const TOPIC_XPCOM_SHUTDOWN = "xpcom-shutdown";
|
||||
let hs = Cc["@mozilla.org/browser/nav-history-service;1"].
|
||||
getService(Ci.nsIObserver);
|
||||
hs.observe(null, TOPIC_XPCOM_SHUTDOWN, null);
|
||||
let sync = Cc["@mozilla.org/places/sync;1"].getService(Ci.nsIObserver);
|
||||
sync.observe(null, TOPIC_XPCOM_SHUTDOWN, null);
|
||||
let expire = Cc["@mozilla.org/places/expiration;1"].getService(Ci.nsIObserver);
|
||||
expire.observe(null, TOPIC_XPCOM_SHUTDOWN, null);
|
||||
}
|
||||
|
||||
// Simulates an expiration at shutdown.
|
||||
function shutdownExpiration()
|
||||
{
|
||||
const TOPIC_XPCOM_SHUTDOWN = "xpcom-shutdown";
|
||||
let expire = Cc["@mozilla.org/places/expiration;1"].getService(Ci.nsIObserver);
|
||||
expire.observe(null, TOPIC_XPCOM_SHUTDOWN, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Causes expiration component to start, otherwise it would wait for the first
|
||||
* history notification.
|
||||
*/
|
||||
function force_expiration_start() {
|
||||
Cc["@mozilla.org/places/expiration;1"].getService(Ci.nsISupports);
|
||||
}
|
||||
|
||||
/**
|
||||
* Forces an expiration run.
|
||||
*/
|
||||
function force_expiration_step(aLimit) {
|
||||
if (!aLimit)
|
||||
aLimit = -1; // No limit.
|
||||
const TOPIC_DEBUG_START_EXPIRATION = "places-debug-start-expiration";
|
||||
let expire = Cc["@mozilla.org/places/expiration;1"].getService(Ci.nsIObserver);
|
||||
expire.observe(null, TOPIC_DEBUG_START_EXPIRATION, aLimit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears history invoking callback when done.
|
||||
*/
|
||||
function waitForClearHistory(aCallback) {
|
||||
let os = Cc["@mozilla.org/observer-service;1"].
|
||||
getService(Ci.nsIObserverService);
|
||||
let observer = {
|
||||
observe: function(aSubject, aTopic, aData) {
|
||||
os.removeObserver(this, TOPIC_EXPIRATION_FINISHED);
|
||||
aCallback();
|
||||
}
|
||||
};
|
||||
os.addObserver(observer, TOPIC_EXPIRATION_FINISHED, false);
|
||||
|
||||
let hs = Cc["@mozilla.org/browser/nav-history-service;1"].
|
||||
getService(Ci.nsINavHistoryService);
|
||||
hs.QueryInterface(Ci.nsIBrowserHistory).removeAllPages();
|
||||
}
|
||||
|
||||
/**
|
||||
* Expiration preferences helpers.
|
||||
*/
|
||||
let prefs = Cc["@mozilla.org/preferences-service;1"].
|
||||
getService(Ci.nsIPrefBranch);
|
||||
|
||||
function setInterval(aNewInterval) {
|
||||
prefs.setIntPref("places.history.expiration.interval_seconds", aNewInterval);
|
||||
}
|
||||
function getInterval() {
|
||||
return prefs.getIntPref("places.history.expiration.interval_seconds");
|
||||
}
|
||||
function clearInterval() {
|
||||
try {
|
||||
prefs.clearUserPref("places.history.expiration.interval_seconds");
|
||||
}
|
||||
catch(ex) {}
|
||||
}
|
||||
|
||||
function setMaxPages(aNewMaxPages) {
|
||||
prefs.setIntPref("places.history.expiration.max_pages", aNewMaxPages);
|
||||
}
|
||||
function getMaxPages() {
|
||||
return prefs.getIntPref("places.history.expiration.max_pages");
|
||||
}
|
||||
function clearMaxPages() {
|
||||
try {
|
||||
prefs.clearUserPref("places.history.expiration.max_pages");
|
||||
}
|
||||
catch(ex) {}
|
||||
}
|
||||
|
||||
function setHistoryEnabled(aHistoryEnabled) {
|
||||
prefs.setBoolPref("places.history.enabled", aHistoryEnabled);
|
||||
}
|
||||
function getHistoryEnabled() {
|
||||
return prefs.getBoolPref("places.history.enabled");
|
||||
}
|
||||
function clearHistoryEnabled() {
|
||||
try {
|
||||
prefs.clearUserPref("places.history.enabled");
|
||||
}
|
||||
catch(ex) {}
|
||||
}
|
@ -0,0 +1,138 @@
|
||||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
||||
* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Places Unit Tests.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Mozilla Foundation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2009
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Marco Bonardo <mak77@bonardo.net> (Original Author)
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
/**
|
||||
* What this is aimed to test:
|
||||
*
|
||||
* EXPIRE_WITH_HISTORY annotations should be expired when a page has no more
|
||||
* visits, even if the page still exists in the database.
|
||||
* This expiration policy is only valid for page annotations.
|
||||
*/
|
||||
|
||||
const TOPIC_EXPIRATION_FINISHED = "places-expiration-finished";
|
||||
|
||||
let os = Cc["@mozilla.org/observer-service;1"].
|
||||
getService(Ci.nsIObserverService);
|
||||
let hs = Cc["@mozilla.org/browser/nav-history-service;1"].
|
||||
getService(Ci.nsINavHistoryService);
|
||||
let bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
|
||||
getService(Ci.nsINavBookmarksService);
|
||||
let as = Cc["@mozilla.org/browser/annotation-service;1"].
|
||||
getService(Ci.nsIAnnotationService);
|
||||
|
||||
function run_test() {
|
||||
// Set interval to a large value so we don't expire on it.
|
||||
setInterval(3600); // 1h
|
||||
|
||||
// Expire all expirable pages.
|
||||
setMaxPages(0);
|
||||
|
||||
// Add some visited page and a couple expire with history annotations for each.
|
||||
let now = Date.now() * 1000;
|
||||
for (let i = 0; i < 5; i++) {
|
||||
let pageURI = uri("http://page_anno." + i + ".mozilla.org/");
|
||||
hs.addVisit(pageURI, now++, null, hs.TRANSITION_TYPED, false, 0);
|
||||
as.setPageAnnotation(pageURI, "page_expire1", "test", 0, as.EXPIRE_WITH_HISTORY);
|
||||
as.setPageAnnotation(pageURI, "page_expire2", "test", 0, as.EXPIRE_WITH_HISTORY);
|
||||
}
|
||||
|
||||
let pages = as.getPagesWithAnnotation("page_expire1");
|
||||
do_check_eq(pages.length, 5);
|
||||
pages = as.getPagesWithAnnotation("page_expire2");
|
||||
do_check_eq(pages.length, 5);
|
||||
|
||||
// Add some bookmarked page and a couple session annotations for each.
|
||||
for (let i = 0; i < 5; i++) {
|
||||
let pageURI = uri("http://item_anno." + i + ".mozilla.org/");
|
||||
// We also add a visit before bookmarking.
|
||||
hs.addVisit(pageURI, now++, null, hs.TRANSITION_TYPED, false, 0);
|
||||
let id = bs.insertBookmark(bs.unfiledBookmarksFolder, pageURI,
|
||||
bs.DEFAULT_INDEX, null);
|
||||
// Notice we use page annotations here, items annotations can't use this
|
||||
// kind of expiration policy.
|
||||
as.setPageAnnotation(pageURI, "item_persist1", "test", 0, as.EXPIRE_WITH_HISTORY);
|
||||
as.setPageAnnotation(pageURI, "item_persist2", "test", 0, as.EXPIRE_WITH_HISTORY);
|
||||
}
|
||||
|
||||
let items = as.getPagesWithAnnotation("item_persist1");
|
||||
do_check_eq(items.length, 5);
|
||||
items = as.getPagesWithAnnotation("item_persist2");
|
||||
do_check_eq(items.length, 5);
|
||||
|
||||
// Add other visited page and a couple expire with history annotations for each.
|
||||
// We won't expire these visits, so the annotations should survive.
|
||||
for (let i = 0; i < 5; i++) {
|
||||
let pageURI = uri("http://persist_page_anno." + i + ".mozilla.org/");
|
||||
hs.addVisit(pageURI, now++, null, hs.TRANSITION_TYPED, false, 0);
|
||||
as.setPageAnnotation(pageURI, "page_persist1", "test", 0, as.EXPIRE_WITH_HISTORY);
|
||||
as.setPageAnnotation(pageURI, "page_persist2", "test", 0, as.EXPIRE_WITH_HISTORY);
|
||||
}
|
||||
|
||||
pages = as.getPagesWithAnnotation("page_persist1");
|
||||
do_check_eq(pages.length, 5);
|
||||
pages = as.getPagesWithAnnotation("page_persist2");
|
||||
do_check_eq(pages.length, 5);
|
||||
|
||||
// Observe expirations.
|
||||
observer = {
|
||||
observe: function(aSubject, aTopic, aData) {
|
||||
os.removeObserver(observer, TOPIC_EXPIRATION_FINISHED);
|
||||
|
||||
let pages = as.getPagesWithAnnotation("page_expire1");
|
||||
do_check_eq(pages.length, 0);
|
||||
pages = as.getPagesWithAnnotation("page_expire2");
|
||||
do_check_eq(pages.length, 0);
|
||||
let items = as.getItemsWithAnnotation("item_persist1");
|
||||
do_check_eq(items.length, 0);
|
||||
items = as.getItemsWithAnnotation("item_persist2");
|
||||
do_check_eq(items.length, 0);
|
||||
pages = as.getPagesWithAnnotation("page_persist1");
|
||||
do_check_eq(pages.length, 5);
|
||||
pages = as.getPagesWithAnnotation("page_persist2");
|
||||
do_check_eq(pages.length, 5);
|
||||
|
||||
do_test_finished();
|
||||
}
|
||||
};
|
||||
os.addObserver(observer, TOPIC_EXPIRATION_FINISHED, false);
|
||||
|
||||
// Expire all visits for the first 5 pages and the bookmarks.
|
||||
force_expiration_step(10);
|
||||
do_test_pending();
|
||||
}
|
@ -0,0 +1,139 @@
|
||||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
||||
* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Places Unit Tests.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Mozilla Foundation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2009
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Marco Bonardo <mak77@bonardo.net> (Original Author)
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
/**
|
||||
* What this is aimed to test:
|
||||
*
|
||||
* EXPIRE_NEVER annotations should be expired when a page is removed from the
|
||||
* database.
|
||||
* If the annotation is a page annotation this will happen when the page is
|
||||
* expired, namely when the page has no visits and is not bookmarked.
|
||||
* Otherwise if it's an item annotation the annotation will be expired when
|
||||
* the item is removed, thus expiration won't handle this case at all.
|
||||
*/
|
||||
|
||||
const TOPIC_EXPIRATION_FINISHED = "places-expiration-finished";
|
||||
|
||||
let os = Cc["@mozilla.org/observer-service;1"].
|
||||
getService(Ci.nsIObserverService);
|
||||
let hs = Cc["@mozilla.org/browser/nav-history-service;1"].
|
||||
getService(Ci.nsINavHistoryService);
|
||||
let bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
|
||||
getService(Ci.nsINavBookmarksService);
|
||||
let as = Cc["@mozilla.org/browser/annotation-service;1"].
|
||||
getService(Ci.nsIAnnotationService);
|
||||
|
||||
function run_test() {
|
||||
// Set interval to a large value so we don't expire on it.
|
||||
setInterval(3600); // 1h
|
||||
|
||||
// Expire all expirable pages.
|
||||
setMaxPages(0);
|
||||
|
||||
// Add some visited page and a couple expire never annotations for each.
|
||||
let now = Date.now() * 1000;
|
||||
for (let i = 0; i < 5; i++) {
|
||||
let pageURI = uri("http://page_anno." + i + ".mozilla.org/");
|
||||
hs.addVisit(pageURI, now++, null, hs.TRANSITION_TYPED, false, 0);
|
||||
as.setPageAnnotation(pageURI, "page_expire1", "test", 0, as.EXPIRE_NEVER);
|
||||
as.setPageAnnotation(pageURI, "page_expire2", "test", 0, as.EXPIRE_NEVER);
|
||||
}
|
||||
|
||||
let pages = as.getPagesWithAnnotation("page_expire1");
|
||||
do_check_eq(pages.length, 5);
|
||||
pages = as.getPagesWithAnnotation("page_expire2");
|
||||
do_check_eq(pages.length, 5);
|
||||
|
||||
// Add some bookmarked page and a couple expire never annotations for each.
|
||||
for (let i = 0; i < 5; i++) {
|
||||
let pageURI = uri("http://item_anno." + i + ".mozilla.org/");
|
||||
// We also add a visit before bookmarking.
|
||||
hs.addVisit(pageURI, now++, null, hs.TRANSITION_TYPED, false, 0);
|
||||
let id = bs.insertBookmark(bs.unfiledBookmarksFolder, pageURI,
|
||||
bs.DEFAULT_INDEX, null);
|
||||
as.setItemAnnotation(id, "item_persist1", "test", 0, as.EXPIRE_NEVER);
|
||||
as.setItemAnnotation(id, "item_persist2", "test", 0, as.EXPIRE_NEVER);
|
||||
}
|
||||
|
||||
let items = as.getItemsWithAnnotation("item_persist1");
|
||||
do_check_eq(items.length, 5);
|
||||
items = as.getItemsWithAnnotation("item_persist2");
|
||||
do_check_eq(items.length, 5);
|
||||
|
||||
// Add other visited page and a couple expire never annotations for each.
|
||||
// We won't expire these visits, so the annotations should survive.
|
||||
for (let i = 0; i < 5; i++) {
|
||||
let pageURI = uri("http://persist_page_anno." + i + ".mozilla.org/");
|
||||
hs.addVisit(pageURI, now++, null, hs.TRANSITION_TYPED, false, 0);
|
||||
as.setPageAnnotation(pageURI, "page_persist1", "test", 0, as.EXPIRE_NEVER);
|
||||
as.setPageAnnotation(pageURI, "page_persist2", "test", 0, as.EXPIRE_NEVER);
|
||||
}
|
||||
|
||||
pages = as.getPagesWithAnnotation("page_persist1");
|
||||
do_check_eq(pages.length, 5);
|
||||
pages = as.getPagesWithAnnotation("page_persist2");
|
||||
do_check_eq(pages.length, 5);
|
||||
|
||||
// Observe expirations.
|
||||
observer = {
|
||||
observe: function(aSubject, aTopic, aData) {
|
||||
os.removeObserver(observer, TOPIC_EXPIRATION_FINISHED);
|
||||
|
||||
let pages = as.getPagesWithAnnotation("page_expire1");
|
||||
do_check_eq(pages.length, 0);
|
||||
pages = as.getPagesWithAnnotation("page_expire2");
|
||||
do_check_eq(pages.length, 0);
|
||||
let items = as.getItemsWithAnnotation("item_persist1");
|
||||
do_check_eq(items.length, 5);
|
||||
items = as.getItemsWithAnnotation("item_persist2");
|
||||
do_check_eq(items.length, 5);
|
||||
pages = as.getPagesWithAnnotation("page_persist1");
|
||||
do_check_eq(pages.length, 5);
|
||||
pages = as.getPagesWithAnnotation("page_persist2");
|
||||
do_check_eq(pages.length, 5);
|
||||
|
||||
do_test_finished();
|
||||
}
|
||||
};
|
||||
os.addObserver(observer, TOPIC_EXPIRATION_FINISHED, false);
|
||||
|
||||
// Expire all visits for the first 5 pages and the bookmarks.
|
||||
force_expiration_step(10);
|
||||
do_test_pending();
|
||||
}
|
@ -0,0 +1,213 @@
|
||||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
||||
* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Places Unit Tests.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Mozilla Foundation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2009
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Marco Bonardo <mak77@bonardo.net> (Original Author)
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
/**
|
||||
* What this is aimed to test:
|
||||
*
|
||||
* Annotations can be set with a timed expiration policy.
|
||||
* Supported policies are:
|
||||
* - EXPIRE_DAYS: annotation would be expired after 7 days
|
||||
* - EXPIRE_WEEKS: annotation would be expired after 30 days
|
||||
* - EXPIRE_MONTHS: annotation would be expired after 180 days
|
||||
*/
|
||||
|
||||
const TOPIC_EXPIRATION_FINISHED = "places-expiration-finished";
|
||||
|
||||
let os = Cc["@mozilla.org/observer-service;1"].
|
||||
getService(Ci.nsIObserverService);
|
||||
let hs = Cc["@mozilla.org/browser/nav-history-service;1"].
|
||||
getService(Ci.nsINavHistoryService);
|
||||
let bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
|
||||
getService(Ci.nsINavBookmarksService);
|
||||
let as = Cc["@mozilla.org/browser/annotation-service;1"].
|
||||
getService(Ci.nsIAnnotationService);
|
||||
|
||||
/**
|
||||
* Creates an aged annotation.
|
||||
*
|
||||
* @param aIdentifier Either a page url or an item id.
|
||||
* @param aIdentifier Name of the annotation.
|
||||
* @param aValue Value for the annotation.
|
||||
* @param aExpirePolicy Expiration policy of the annotation.
|
||||
* @param aAgeInDays Age in days of the annotation.
|
||||
* @param [optional] aLastModifiedAgeInDays Age in days of the annotation, for lastModified.
|
||||
*/
|
||||
let now = Date.now();
|
||||
function add_old_anno(aIdentifier, aName, aValue, aExpirePolicy,
|
||||
aAgeInDays, aLastModifiedAgeInDays) {
|
||||
let expireDate = (now - (aAgeInDays * 86400 * 1000)) * 1000;
|
||||
let lastModifiedDate = 0;
|
||||
if (aLastModifiedAgeInDays)
|
||||
lastModifiedDate = (now - (aLastModifiedAgeInDays * 86400 * 1000)) * 1000;
|
||||
|
||||
let dbConn = DBConn();
|
||||
let sql;
|
||||
if (typeof(aIdentifier) == "number") {
|
||||
// Item annotation.
|
||||
as.setItemAnnotation(aIdentifier, aName, aValue, 0, aExpirePolicy);
|
||||
// Update dateAdded for the last added annotation.
|
||||
sql = "UPDATE moz_items_annos SET dateAdded = :expire_date, lastModified = :last_modified " +
|
||||
"WHERE id = (SELECT id FROM moz_items_annos " +
|
||||
"WHERE item_id = :id " +
|
||||
"ORDER BY dateAdded DESC LIMIT 1)";
|
||||
}
|
||||
else if (aIdentifier instanceof Ci.nsIURI){
|
||||
// Page annotation.
|
||||
as.setPageAnnotation(aIdentifier, aName, aValue, 0, aExpirePolicy);
|
||||
// Update dateAdded for the last added annotation.
|
||||
sql = "UPDATE moz_annos SET dateAdded = :expire_date, lastModified = :last_modified " +
|
||||
"WHERE id = (SELECT a.id FROM moz_annos a " +
|
||||
"LEFT JOIN moz_places_view h on h.id = a.place_id " +
|
||||
"WHERE h.url = :id " +
|
||||
"ORDER BY a.dateAdded DESC LIMIT 1)";
|
||||
}
|
||||
else
|
||||
do_throw("Wrong identifier type");
|
||||
|
||||
let stmt = dbConn.createStatement(sql);
|
||||
stmt.params.id = (typeof(aIdentifier) == "number") ? aIdentifier
|
||||
: aIdentifier.spec;
|
||||
stmt.params.expire_date = expireDate;
|
||||
stmt.params.last_modified = lastModifiedDate;
|
||||
try {
|
||||
stmt.executeStep();
|
||||
}
|
||||
finally {
|
||||
stmt.finalize();
|
||||
}
|
||||
}
|
||||
|
||||
function run_test() {
|
||||
// Set interval to a large value so we don't expire on it.
|
||||
setInterval(3600); // 1h
|
||||
|
||||
// Expire all expirable pages.
|
||||
setMaxPages(0);
|
||||
|
||||
// Add some bookmarked page and timed annotations for each.
|
||||
for (let i = 0; i < 5; i++) {
|
||||
let pageURI = uri("http://item_anno." + i + ".mozilla.org/");
|
||||
hs.addVisit(pageURI, now++, null, hs.TRANSITION_TYPED, false, 0);
|
||||
let id = bs.insertBookmark(bs.unfiledBookmarksFolder, pageURI,
|
||||
bs.DEFAULT_INDEX, null);
|
||||
// Add a 6 days old anno.
|
||||
add_old_anno(id, "persist_days", "test", as.EXPIRE_DAYS, 6);
|
||||
// Add a 8 days old anno, modified 5 days ago.
|
||||
add_old_anno(id, "persist_lm_days", "test", as.EXPIRE_DAYS, 8, 6);
|
||||
// Add a 8 days old anno.
|
||||
add_old_anno(id, "expire_days", "test", as.EXPIRE_DAYS, 8);
|
||||
|
||||
// Add a 29 days old anno.
|
||||
add_old_anno(id, "persist_weeks", "test", as.EXPIRE_WEEKS, 29);
|
||||
// Add a 31 days old anno, modified 29 days ago.
|
||||
add_old_anno(id, "persist_lm_weeks", "test", as.EXPIRE_WEEKS, 31, 29);
|
||||
// Add a 31 days old anno.
|
||||
add_old_anno(id, "expire_weeks", "test", as.EXPIRE_WEEKS, 31);
|
||||
|
||||
// Add a 179 days old anno.
|
||||
add_old_anno(id, "persist_months", "test", as.EXPIRE_MONTHS, 179);
|
||||
// Add a 181 days old anno, modified 179 days ago.
|
||||
add_old_anno(id, "persist_lm_months", "test", as.EXPIRE_MONTHS, 181, 179);
|
||||
// Add a 181 days old anno.
|
||||
add_old_anno(id, "expire_months", "test", as.EXPIRE_MONTHS, 181);
|
||||
}
|
||||
|
||||
// Add some visited page and timed annotations for each.
|
||||
let now = Date.now() * 1000;
|
||||
for (let i = 0; i < 5; i++) {
|
||||
let pageURI = uri("http://page_anno." + i + ".mozilla.org/");
|
||||
hs.addVisit(pageURI, now++, null, hs.TRANSITION_TYPED, false, 0);
|
||||
// Add a 6 days old anno.
|
||||
add_old_anno(pageURI, "persist_days", "test", as.EXPIRE_DAYS, 6);
|
||||
// Add a 8 days old anno, modified 5 days ago.
|
||||
add_old_anno(pageURI, "persist_lm_days", "test", as.EXPIRE_DAYS, 8, 6);
|
||||
// Add a 8 days old anno.
|
||||
add_old_anno(pageURI, "expire_days", "test", as.EXPIRE_DAYS, 8);
|
||||
|
||||
// Add a 29 days old anno.
|
||||
add_old_anno(pageURI, "persist_weeks", "test", as.EXPIRE_WEEKS, 29);
|
||||
// Add a 31 days old anno, modified 29 days ago.
|
||||
add_old_anno(pageURI, "persist_lm_weeks", "test", as.EXPIRE_WEEKS, 31, 29);
|
||||
// Add a 31 days old anno.
|
||||
add_old_anno(pageURI, "expire_weeks", "test", as.EXPIRE_WEEKS, 31);
|
||||
|
||||
// Add a 179 days old anno.
|
||||
add_old_anno(pageURI, "persist_months", "test", as.EXPIRE_MONTHS, 179);
|
||||
// Add a 181 days old anno, modified 179 days ago.
|
||||
add_old_anno(pageURI, "persist_lm_months", "test", as.EXPIRE_MONTHS, 181, 179);
|
||||
// Add a 181 days old anno.
|
||||
add_old_anno(pageURI, "expire_months", "test", as.EXPIRE_MONTHS, 181);
|
||||
}
|
||||
|
||||
// Observe expirations.
|
||||
observer = {
|
||||
observe: function(aSubject, aTopic, aData) {
|
||||
os.removeObserver(observer, TOPIC_EXPIRATION_FINISHED);
|
||||
|
||||
["expire_days", "expire_weeks", "expire_months"].forEach(function(aAnno) {
|
||||
let pages = as.getPagesWithAnnotation(aAnno);
|
||||
do_check_eq(pages.length, 0);
|
||||
});
|
||||
|
||||
["expire_days", "expire_weeks", "expire_months"].forEach(function(aAnno) {
|
||||
let items = as.getItemsWithAnnotation(aAnno);
|
||||
do_check_eq(items.length, 0);
|
||||
});
|
||||
|
||||
["persist_days", "persist_lm_days", "persist_weeks", "persist_lm_weeks",
|
||||
"persist_months", "persist_lm_months"].forEach(function(aAnno) {
|
||||
let pages = as.getPagesWithAnnotation(aAnno);
|
||||
do_check_eq(pages.length, 5);
|
||||
});
|
||||
|
||||
["persist_days", "persist_lm_days", "persist_weeks", "persist_lm_weeks",
|
||||
"persist_months", "persist_lm_months"].forEach(function(aAnno) {
|
||||
let items = as.getItemsWithAnnotation(aAnno);
|
||||
do_check_eq(items.length, 5);
|
||||
});
|
||||
|
||||
do_test_finished();
|
||||
}
|
||||
};
|
||||
os.addObserver(observer, TOPIC_EXPIRATION_FINISHED, false);
|
||||
|
||||
// Expire all visits for the bookmarks.
|
||||
force_expiration_step(5);
|
||||
do_test_pending();
|
||||
}
|
@ -0,0 +1,110 @@
|
||||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
||||
* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Places Unit Tests.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Mozilla Foundation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2009
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Marco Bonardo <mak77@bonardo.net> (Original Author)
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
/**
|
||||
* What this is aimed to test:
|
||||
*
|
||||
* Session annotations should be expired when browsing session ends.
|
||||
*/
|
||||
|
||||
const TOPIC_EXPIRATION_FINISHED = "places-expiration-finished";
|
||||
|
||||
let os = Cc["@mozilla.org/observer-service;1"].
|
||||
getService(Ci.nsIObserverService);
|
||||
let hs = Cc["@mozilla.org/browser/nav-history-service;1"].
|
||||
getService(Ci.nsINavHistoryService);
|
||||
let bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
|
||||
getService(Ci.nsINavBookmarksService);
|
||||
let as = Cc["@mozilla.org/browser/annotation-service;1"].
|
||||
getService(Ci.nsIAnnotationService);
|
||||
|
||||
function run_test() {
|
||||
// Set interval to a large value so we don't expire on it.
|
||||
setInterval(3600); // 1h
|
||||
|
||||
// Add some visited page and a couple session annotations for each.
|
||||
let now = Date.now() * 1000;
|
||||
for (let i = 0; i < 10; i++) {
|
||||
let pageURI = uri("http://session_page_anno." + i + ".mozilla.org/");
|
||||
hs.addVisit(pageURI, now++, null, hs.TRANSITION_TYPED, false, 0);
|
||||
as.setPageAnnotation(pageURI, "test1", "test", 0, as.EXPIRE_SESSION);
|
||||
as.setPageAnnotation(pageURI, "test2", "test", 0, as.EXPIRE_SESSION);
|
||||
}
|
||||
|
||||
// Add some bookmarked page and a couple session annotations for each.
|
||||
for (let i = 0; i < 10; i++) {
|
||||
let pageURI = uri("http://session_item_anno." + i + ".mozilla.org/");
|
||||
let id = bs.insertBookmark(bs.unfiledBookmarksFolder, pageURI,
|
||||
bs.DEFAULT_INDEX, null);
|
||||
as.setItemAnnotation(id, "test1", "test", 0, as.EXPIRE_SESSION);
|
||||
as.setItemAnnotation(id, "test2", "test", 0, as.EXPIRE_SESSION);
|
||||
}
|
||||
|
||||
|
||||
let pages = as.getPagesWithAnnotation("test1");
|
||||
do_check_eq(pages.length, 10);
|
||||
pages = as.getPagesWithAnnotation("test2");
|
||||
do_check_eq(pages.length, 10);
|
||||
let items = as.getItemsWithAnnotation("test1");
|
||||
do_check_eq(items.length, 10);
|
||||
items = as.getItemsWithAnnotation("test2");
|
||||
do_check_eq(items.length, 10);
|
||||
|
||||
// Observe expirations.
|
||||
observer = {
|
||||
observe: function(aSubject, aTopic, aData) {
|
||||
os.removeObserver(observer, TOPIC_EXPIRATION_FINISHED);
|
||||
|
||||
let pages = as.getPagesWithAnnotation("test1");
|
||||
do_check_eq(pages.length, 0);
|
||||
pages = as.getPagesWithAnnotation("test2");
|
||||
do_check_eq(pages.length, 0);
|
||||
let items = as.getItemsWithAnnotation("test1");
|
||||
do_check_eq(items.length, 0);
|
||||
items = as.getItemsWithAnnotation("test2");
|
||||
do_check_eq(items.length, 0);
|
||||
|
||||
do_test_finished();
|
||||
}
|
||||
};
|
||||
os.addObserver(observer, TOPIC_EXPIRATION_FINISHED, false);
|
||||
|
||||
shutdownExpiration();
|
||||
do_test_pending();
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
||||
* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Places Unit Tests.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Mozilla Foundation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2009
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Marco Bonardo <mak77@bonardo.net> (Original Author)
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
/**
|
||||
* What this is aimed to test:
|
||||
*
|
||||
* Ensure that History (through category cache) notifies us just once.
|
||||
*/
|
||||
|
||||
const TOPIC_EXPIRATION_FINISHED = "places-expiration-finished";
|
||||
|
||||
let os = Cc["@mozilla.org/observer-service;1"].
|
||||
getService(Ci.nsIObserverService);
|
||||
let hs = Cc["@mozilla.org/browser/nav-history-service;1"].
|
||||
getService(Ci.nsINavHistoryService);
|
||||
|
||||
let gObserver = {
|
||||
notifications: 0,
|
||||
observe: function(aSubject, aTopic, aData) {
|
||||
this.notifications++;
|
||||
}
|
||||
};
|
||||
os.addObserver(gObserver, TOPIC_EXPIRATION_FINISHED, false);
|
||||
|
||||
function run_test() {
|
||||
// Set interval to a large value so we don't expire on it.
|
||||
setInterval(3600); // 1h
|
||||
|
||||
hs.QueryInterface(Ci.nsIBrowserHistory).removeAllPages();
|
||||
|
||||
do_timeout(2000, check_result);
|
||||
do_test_pending();
|
||||
}
|
||||
|
||||
function check_result() {
|
||||
os.removeObserver(gObserver, TOPIC_EXPIRATION_FINISHED);
|
||||
do_check_eq(gObserver.notifications, 1);
|
||||
do_test_finished();
|
||||
}
|
@ -0,0 +1,168 @@
|
||||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
||||
* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Places Unit Tests.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Mozilla Foundation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2009
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Marco Bonardo <mak77@bonardo.net> (Original Author)
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
/**
|
||||
* What this is aimed to test:
|
||||
*
|
||||
* Expiring a full page should fire an onDeleteURI notification.
|
||||
*/
|
||||
|
||||
const TOPIC_EXPIRATION_FINISHED = "places-expiration-finished";
|
||||
|
||||
let os = Cc["@mozilla.org/observer-service;1"].
|
||||
getService(Ci.nsIObserverService);
|
||||
let hs = Cc["@mozilla.org/browser/nav-history-service;1"].
|
||||
getService(Ci.nsINavHistoryService);
|
||||
let bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
|
||||
getService(Ci.nsINavBookmarksService);
|
||||
|
||||
let gTests = [
|
||||
|
||||
{ desc: "Add 1 bookmarked page.",
|
||||
addPages: 1,
|
||||
addBookmarks: 1,
|
||||
expectedNotifications: 0, // No expirable pages.
|
||||
},
|
||||
|
||||
{ desc: "Add 2 pages, 1 bookmarked.",
|
||||
addPages: 2,
|
||||
addBookmarks: 1,
|
||||
expectedNotifications: 1, // Only one expirable page.
|
||||
},
|
||||
|
||||
{ desc: "Add 10 pages, none bookmarked.",
|
||||
addPages: 10,
|
||||
addBookmarks: 0,
|
||||
expectedNotifications: 10, // Will expire everything.
|
||||
},
|
||||
|
||||
{ desc: "Add 10 pages, all bookmarked.",
|
||||
addPages: 10,
|
||||
addBookmarks: 10,
|
||||
expectedNotifications: 0, // No expirable pages.
|
||||
},
|
||||
|
||||
];
|
||||
|
||||
let gCurrentTest;
|
||||
let gTestIndex = 0;
|
||||
|
||||
function run_test() {
|
||||
// Set interval to a large value so we don't expire on it.
|
||||
setInterval(3600); // 1h
|
||||
|
||||
// Expire anything that is expirable.
|
||||
setMaxPages(0);
|
||||
|
||||
do_test_pending();
|
||||
run_next_test();
|
||||
}
|
||||
|
||||
function run_next_test() {
|
||||
if (gTests.length) {
|
||||
gCurrentTest = gTests.shift();
|
||||
gTestIndex++;
|
||||
print("\nTEST " + gTestIndex + ": " + gCurrentTest.desc);
|
||||
gCurrentTest.receivedNotifications = 0;
|
||||
|
||||
// Setup visits.
|
||||
let now = Date.now() * 1000;
|
||||
for (let i = 0; i < gCurrentTest.addPages; i++) {
|
||||
let page = "http://" + gTestIndex + "." + i + ".mozilla.org/";
|
||||
hs.addVisit(uri(page), now++, null, hs.TRANSITION_TYPED, false, 0);
|
||||
}
|
||||
|
||||
// Setup bookmarks.
|
||||
gCurrentTest.bookmarks = [];
|
||||
for (let i = 0; i < gCurrentTest.addBookmarks; i++) {
|
||||
let page = "http://" + gTestIndex + "." + i + ".mozilla.org/";
|
||||
bs.insertBookmark(bs.unfiledBookmarksFolder, uri(page),
|
||||
bs.DEFAULT_INDEX, null);
|
||||
gCurrentTest.bookmarks.push(page);
|
||||
}
|
||||
|
||||
// Observe history.
|
||||
historyObserver = {
|
||||
onBeginUpdateBatch: function PEX_onBeginUpdateBatch() {},
|
||||
onEndUpdateBatch: function PEX_onEndUpdateBatch() {},
|
||||
onClearHistory: function() {},
|
||||
onVisit: function() {},
|
||||
onTitleChanged: function() {},
|
||||
onBeforeDeleteURI: function() {},
|
||||
onDeleteURI: function(aURI) {
|
||||
gCurrentTest.receivedNotifications++;
|
||||
// Check this uri was not bookmarked.
|
||||
do_check_eq(gCurrentTest.bookmarks.indexOf(aURI.spec), -1);
|
||||
},
|
||||
onPageChanged: function() {},
|
||||
onDeleteVisits: function(aURI, aTime) { },
|
||||
};
|
||||
hs.addObserver(historyObserver, false);
|
||||
|
||||
// Observe expirations.
|
||||
observer = {
|
||||
observe: function(aSubject, aTopic, aData) {
|
||||
os.removeObserver(observer, TOPIC_EXPIRATION_FINISHED);
|
||||
hs.removeObserver(historyObserver, false);
|
||||
|
||||
// This test finished.
|
||||
check_result();
|
||||
}
|
||||
};
|
||||
os.addObserver(observer, TOPIC_EXPIRATION_FINISHED, false);
|
||||
|
||||
// Expire now, observers will check results.
|
||||
force_expiration_step();
|
||||
}
|
||||
else {
|
||||
clearMaxPages();
|
||||
bs.removeFolderChildren(bs.unfiledBookmarksFolder);
|
||||
waitForClearHistory(do_test_finished);
|
||||
}
|
||||
}
|
||||
|
||||
function check_result() {
|
||||
|
||||
do_check_eq(gCurrentTest.receivedNotifications,
|
||||
gCurrentTest.expectedNotifications);
|
||||
|
||||
// Clean up.
|
||||
bs.removeFolderChildren(bs.unfiledBookmarksFolder);
|
||||
waitForClearHistory(run_next_test);
|
||||
}
|
@ -0,0 +1,188 @@
|
||||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
||||
* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Places Unit Tests.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Mozilla Foundation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2009
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Marco Bonardo <mak77@bonardo.net> (Original Author)
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
/**
|
||||
* What this is aimed to test:
|
||||
*
|
||||
* Expiring only visits for a page, but not the full page, should fire an
|
||||
* onDeleteVisits notification.
|
||||
*/
|
||||
|
||||
const TOPIC_EXPIRATION_FINISHED = "places-expiration-finished";
|
||||
|
||||
let os = Cc["@mozilla.org/observer-service;1"].
|
||||
getService(Ci.nsIObserverService);
|
||||
let hs = Cc["@mozilla.org/browser/nav-history-service;1"].
|
||||
getService(Ci.nsINavHistoryService);
|
||||
let bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
|
||||
getService(Ci.nsINavBookmarksService);
|
||||
|
||||
let gTests = [
|
||||
|
||||
{ desc: "Add 1 bookmarked page.",
|
||||
addPages: 1,
|
||||
visitsPerPage: 1,
|
||||
addBookmarks: 1,
|
||||
limitExpiration: -1,
|
||||
expectedNotifications: 1, // Will expire visits for 1 page.
|
||||
},
|
||||
|
||||
{ desc: "Add 2 pages, 1 bookmarked.",
|
||||
addPages: 2,
|
||||
visitsPerPage: 1,
|
||||
addBookmarks: 1,
|
||||
limitExpiration: -1,
|
||||
expectedNotifications: 1, // Will expire visits for 1 page.
|
||||
},
|
||||
|
||||
{ desc: "Add 10 pages, none bookmarked.",
|
||||
addPages: 10,
|
||||
visitsPerPage: 1,
|
||||
addBookmarks: 0,
|
||||
limitExpiration: -1,
|
||||
expectedNotifications: 0, // Will expire only full pages.
|
||||
},
|
||||
|
||||
{ desc: "Add 10 pages, all bookmarked.",
|
||||
addPages: 10,
|
||||
visitsPerPage: 1,
|
||||
addBookmarks: 10,
|
||||
limitExpiration: -1,
|
||||
expectedNotifications: 10, // Will expire visist for all pages.
|
||||
},
|
||||
|
||||
{ desc: "Add 10 pages with lot of visits, none bookmarked.",
|
||||
addPages: 10,
|
||||
visitsPerPage: 10,
|
||||
addBookmarks: 0,
|
||||
limitExpiration: 10,
|
||||
expectedNotifications: 10, // Will expire 1 visist for each page, but won't
|
||||
}, // expire pages since they still have visits.
|
||||
|
||||
];
|
||||
|
||||
let gCurrentTest;
|
||||
let gTestIndex = 0;
|
||||
|
||||
function run_test() {
|
||||
// Set interval to a large value so we don't expire on it.
|
||||
setInterval(3600); // 1h
|
||||
|
||||
// Expire anything that is expirable.
|
||||
setMaxPages(0);
|
||||
|
||||
do_test_pending();
|
||||
run_next_test();
|
||||
}
|
||||
|
||||
function run_next_test() {
|
||||
if (gTests.length) {
|
||||
gCurrentTest = gTests.shift();
|
||||
gTestIndex++;
|
||||
print("\nTEST " + gTestIndex + ": " + gCurrentTest.desc);
|
||||
gCurrentTest.receivedNotifications = 0;
|
||||
|
||||
// Setup visits.
|
||||
let now = Date.now() * 1000;
|
||||
for (let j = 0; j < gCurrentTest.visitsPerPage; j++) {
|
||||
for (let i = 0; i < gCurrentTest.addPages; i++) {
|
||||
let page = "http://" + gTestIndex + "." + i + ".mozilla.org/";
|
||||
hs.addVisit(uri(page), now++, null, hs.TRANSITION_TYPED, false, 0);
|
||||
}
|
||||
}
|
||||
|
||||
// Setup bookmarks.
|
||||
gCurrentTest.bookmarks = [];
|
||||
for (let i = 0; i < gCurrentTest.addBookmarks; i++) {
|
||||
let page = "http://" + gTestIndex + "." + i + ".mozilla.org/";
|
||||
bs.insertBookmark(bs.unfiledBookmarksFolder, uri(page),
|
||||
bs.DEFAULT_INDEX, null);
|
||||
gCurrentTest.bookmarks.push(page);
|
||||
}
|
||||
|
||||
// Observe history.
|
||||
historyObserver = {
|
||||
onBeginUpdateBatch: function PEX_onBeginUpdateBatch() {},
|
||||
onEndUpdateBatch: function PEX_onEndUpdateBatch() {},
|
||||
onClearHistory: function() {},
|
||||
onVisit: function() {},
|
||||
onTitleChanged: function() {},
|
||||
onBeforeDeleteURI: function() {},
|
||||
onDeleteURI: function(aURI) {
|
||||
// Check this uri was not bookmarked.
|
||||
do_check_eq(gCurrentTest.bookmarks.indexOf(aURI.spec), -1);
|
||||
},
|
||||
onPageChanged: function() {},
|
||||
onDeleteVisits: function(aURI, aTime) {
|
||||
gCurrentTest.receivedNotifications++;
|
||||
},
|
||||
};
|
||||
hs.addObserver(historyObserver, false);
|
||||
|
||||
// Observe expirations.
|
||||
observer = {
|
||||
observe: function(aSubject, aTopic, aData) {
|
||||
os.removeObserver(observer, TOPIC_EXPIRATION_FINISHED);
|
||||
hs.removeObserver(historyObserver, false);
|
||||
|
||||
// This test finished.
|
||||
check_result();
|
||||
}
|
||||
};
|
||||
os.addObserver(observer, TOPIC_EXPIRATION_FINISHED, false);
|
||||
|
||||
// Expire now, observers will check results.
|
||||
force_expiration_step(gCurrentTest.limitExpiration);
|
||||
}
|
||||
else {
|
||||
clearMaxPages();
|
||||
bs.removeFolderChildren(bs.unfiledBookmarksFolder);
|
||||
waitForClearHistory(do_test_finished);
|
||||
}
|
||||
}
|
||||
|
||||
function check_result() {
|
||||
|
||||
do_check_eq(gCurrentTest.receivedNotifications,
|
||||
gCurrentTest.expectedNotifications);
|
||||
|
||||
// Clean up.
|
||||
bs.removeFolderChildren(bs.unfiledBookmarksFolder);
|
||||
waitForClearHistory(run_next_test);
|
||||
}
|
126
toolkit/components/places/tests/expiration/test_pref_interval.js
Normal file
126
toolkit/components/places/tests/expiration/test_pref_interval.js
Normal file
@ -0,0 +1,126 @@
|
||||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
||||
* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Places Unit Tests.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Mozilla Foundation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2009
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Marco Bonardo <mak77@bonardo.net> (Original Author)
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
/**
|
||||
* What this is aimed to test:
|
||||
*
|
||||
* Expiration relies on an interval, that is user-preffable setting
|
||||
* "places.history.expiration.interval_seconds".
|
||||
* On pref change it will stop current interval timer and fire a new one,
|
||||
* that will obey the new value.
|
||||
* If the pref is set to a number <= 0 we will use the default value.
|
||||
*/
|
||||
|
||||
const TOPIC_EXPIRATION_FINISHED = "places-expiration-finished";
|
||||
const MAX_WAIT_SECONDS = 3;
|
||||
|
||||
let os = Cc["@mozilla.org/observer-service;1"].
|
||||
getService(Ci.nsIObserverService);
|
||||
|
||||
let gTests = [
|
||||
|
||||
// This test should be the first, so the interval won't be influenced by
|
||||
// status of history.
|
||||
{ desc: "Set interval to 1s.",
|
||||
interval: 1,
|
||||
expectedNotification: true,
|
||||
},
|
||||
|
||||
{ desc: "Set interval to a negative value.",
|
||||
interval: -1,
|
||||
expectedNotification: false, // Will ignore.
|
||||
},
|
||||
|
||||
{ desc: "Set interval to 0.",
|
||||
interval: 0,
|
||||
expectedNotification: false, // Will ignore.
|
||||
},
|
||||
|
||||
{ desc: "Set interval to a large value.",
|
||||
interval: 100,
|
||||
expectedNotification: false, // Won't wait so long.
|
||||
},
|
||||
];
|
||||
|
||||
let gCurrentTest;
|
||||
|
||||
function run_test() {
|
||||
// The pref should not exist by default.
|
||||
try {
|
||||
getInterval();
|
||||
do_throw("interval pref should not exist by default");
|
||||
}
|
||||
catch (ex) {}
|
||||
|
||||
// Force the component, so it will start observing preferences.
|
||||
force_expiration_start();
|
||||
|
||||
run_next_test();
|
||||
do_test_pending();
|
||||
}
|
||||
|
||||
function run_next_test() {
|
||||
if (gTests.length) {
|
||||
gCurrentTest = gTests.shift();
|
||||
print(gCurrentTest.desc);
|
||||
gCurrentTest.receivedNotification = false;
|
||||
gCurrentTest.observer = {
|
||||
observe: function(aSubject, aTopic, aData) {
|
||||
gCurrentTest.receivedNotification = true;
|
||||
}
|
||||
};
|
||||
os.addObserver(gCurrentTest.observer, TOPIC_EXPIRATION_FINISHED, false);
|
||||
setInterval(gCurrentTest.interval);
|
||||
let waitSeconds = Math.min(MAX_WAIT_SECONDS, gCurrentTest.interval + 1);
|
||||
do_timeout(waitSeconds * 1000, check_result);
|
||||
}
|
||||
else {
|
||||
clearInterval();
|
||||
do_test_finished();
|
||||
}
|
||||
}
|
||||
|
||||
function check_result() {
|
||||
os.removeObserver(gCurrentTest.observer, TOPIC_EXPIRATION_FINISHED);
|
||||
|
||||
do_check_eq(gCurrentTest.receivedNotification,
|
||||
gCurrentTest.expectedNotification);
|
||||
|
||||
run_next_test();
|
||||
}
|
180
toolkit/components/places/tests/expiration/test_pref_maxpages.js
Normal file
180
toolkit/components/places/tests/expiration/test_pref_maxpages.js
Normal file
@ -0,0 +1,180 @@
|
||||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
||||
* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Places Unit Tests.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Mozilla Foundation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2009
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Marco Bonardo <mak77@bonardo.net> (Original Author)
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
/**
|
||||
* What this is aimed to test:
|
||||
*
|
||||
* Expiration will obey to hardware spec, but user can set a custom maximum
|
||||
* number of pages to retain, to restrict history, through
|
||||
* "places.history.expiration.max_pages".
|
||||
* This limit is used at next expiration run.
|
||||
* If the pref is set to a number < 0 we will use the default value.
|
||||
*/
|
||||
|
||||
const TOPIC_EXPIRATION_FINISHED = "places-expiration-finished";
|
||||
|
||||
let os = Cc["@mozilla.org/observer-service;1"].
|
||||
getService(Ci.nsIObserverService);
|
||||
let hs = Cc["@mozilla.org/browser/nav-history-service;1"].
|
||||
getService(Ci.nsINavHistoryService);
|
||||
|
||||
let gTests = [
|
||||
|
||||
{ desc: "Set max_pages to a negative value, with 1 page.",
|
||||
maxPages: -1,
|
||||
addPages: 1,
|
||||
expectedNotifications: 0, // Will ignore and won't expire anything.
|
||||
},
|
||||
|
||||
{ desc: "Set max_pages to 0.",
|
||||
maxPages: 0,
|
||||
addPages: 1,
|
||||
expectedNotifications: 1,
|
||||
},
|
||||
|
||||
{ desc: "Set max_pages to 0, with 2 pages.",
|
||||
maxPages: 0,
|
||||
addPages: 2,
|
||||
expectedNotifications: 2, // Will expire everything.
|
||||
},
|
||||
|
||||
// Notice if we are over limit we do a full step of expiration. So we ensure
|
||||
// that we will expire if we are over the limit, but we don't ensure that we
|
||||
// will expire exactly up to the limit. Thus in this case we expire
|
||||
// everything.
|
||||
{ desc: "Set max_pages to 1 with 2 pages.",
|
||||
maxPages: 1,
|
||||
addPages: 2,
|
||||
expectedNotifications: 2, // Will expire everything (in this case).
|
||||
},
|
||||
|
||||
{ desc: "Set max_pages to 10, with 9 pages.",
|
||||
maxPages: 10,
|
||||
addPages: 9,
|
||||
expectedNotifications: 0, // We are at the limit, won't expire anything.
|
||||
},
|
||||
|
||||
{ desc: "Set max_pages to 10 with 10 pages.",
|
||||
maxPages: 10,
|
||||
addPages: 10,
|
||||
expectedNotifications: 0, // We are below the limit, won't expire anything.
|
||||
},
|
||||
];
|
||||
|
||||
let gCurrentTest;
|
||||
let gTestIndex = 0;
|
||||
|
||||
function run_test() {
|
||||
// The pref should not exist by default.
|
||||
try {
|
||||
getMaxPages();
|
||||
do_throw("interval pref should not exist by default");
|
||||
}
|
||||
catch (ex) {}
|
||||
|
||||
// Set interval to a large value so we don't expire on it.
|
||||
setInterval(3600); // 1h
|
||||
|
||||
do_test_pending();
|
||||
run_next_test();
|
||||
}
|
||||
|
||||
function run_next_test() {
|
||||
if (gTests.length) {
|
||||
gCurrentTest = gTests.shift();
|
||||
gTestIndex++;
|
||||
print("\nTEST " + gTestIndex + ": " + gCurrentTest.desc);
|
||||
gCurrentTest.receivedNotifications = 0;
|
||||
|
||||
// Setup visits.
|
||||
let now = Date.now() * 1000;
|
||||
for (let i = 0; i < gCurrentTest.addPages; i++) {
|
||||
hs.addVisit(uri("http://" + gTestIndex + "." + i + ".mozilla.org/"), now++, null,
|
||||
hs.TRANSITION_TYPED, false, 0);
|
||||
}
|
||||
|
||||
// Observe history.
|
||||
historyObserver = {
|
||||
onBeginUpdateBatch: function PEX_onBeginUpdateBatch() {},
|
||||
onEndUpdateBatch: function PEX_onEndUpdateBatch() {},
|
||||
onClearHistory: function() {},
|
||||
onVisit: function() {},
|
||||
onTitleChanged: function() {},
|
||||
onBeforeDeleteURI: function() {},
|
||||
onDeleteURI: function(aURI) {
|
||||
print("onDeleteURI " + aURI.spec);
|
||||
gCurrentTest.receivedNotifications++;
|
||||
},
|
||||
onPageChanged: function() {},
|
||||
onDeleteVisits: function(aURI, aTime) {
|
||||
print("onDeleteVisits " + aURI.spec + " " + aTime);
|
||||
},
|
||||
};
|
||||
hs.addObserver(historyObserver, false);
|
||||
|
||||
// Observe expirations.
|
||||
observer = {
|
||||
observe: function(aSubject, aTopic, aData) {
|
||||
os.removeObserver(observer, TOPIC_EXPIRATION_FINISHED);
|
||||
hs.removeObserver(historyObserver, false);
|
||||
|
||||
// This test finished.
|
||||
check_result();
|
||||
}
|
||||
};
|
||||
os.addObserver(observer, TOPIC_EXPIRATION_FINISHED, false);
|
||||
|
||||
setMaxPages(gCurrentTest.maxPages);
|
||||
// Expire now, observers will check results.
|
||||
force_expiration_step();
|
||||
}
|
||||
else {
|
||||
clearMaxPages();
|
||||
waitForClearHistory(do_test_finished);
|
||||
}
|
||||
}
|
||||
|
||||
function check_result() {
|
||||
|
||||
do_check_eq(gCurrentTest.receivedNotifications,
|
||||
gCurrentTest.expectedNotifications);
|
||||
|
||||
// Clean up.
|
||||
waitForClearHistory(run_next_test);
|
||||
}
|
@ -0,0 +1,196 @@
|
||||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
||||
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
||||
* ***** BEGIN LICENSE BLOCK *****
|
||||
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
||||
*
|
||||
* The contents of this file are subject to the Mozilla Public License Version
|
||||
* 1.1 (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
* http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing rights and limitations under the
|
||||
* License.
|
||||
*
|
||||
* The Original Code is Places Unit Tests.
|
||||
*
|
||||
* The Initial Developer of the Original Code is
|
||||
* Mozilla Foundation.
|
||||
* Portions created by the Initial Developer are Copyright (C) 2009
|
||||
* the Initial Developer. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Marco Bonardo <mak77@bonardo.net> (Original Author)
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the terms of
|
||||
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
||||
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
||||
* in which case the provisions of the GPL or the LGPL are applicable instead
|
||||
* of those above. If you wish to allow use of your version of this file only
|
||||
* under the terms of either the GPL or the LGPL, and not to allow others to
|
||||
* use your version of this file under the terms of the MPL, indicate your
|
||||
* decision by deleting the provisions above and replace them with the notice
|
||||
* and other provisions required by the GPL or the LGPL. If you do not delete
|
||||
* the provisions above, a recipient may use your version of this file under
|
||||
* the terms of any one of the MPL, the GPL or the LGPL.
|
||||
*
|
||||
* ***** END LICENSE BLOCK ***** */
|
||||
|
||||
/**
|
||||
* What this is aimed to test:
|
||||
*
|
||||
* bh.removeAllPages should expire everything but bookmarked pages and valid
|
||||
* annos.
|
||||
*/
|
||||
|
||||
const TOPIC_EXPIRATION_FINISHED = "places-expiration-finished";
|
||||
|
||||
let os = Cc["@mozilla.org/observer-service;1"].
|
||||
getService(Ci.nsIObserverService);
|
||||
let hs = Cc["@mozilla.org/browser/nav-history-service;1"].
|
||||
getService(Ci.nsINavHistoryService);
|
||||
let bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
|
||||
getService(Ci.nsINavBookmarksService);
|
||||
let as = Cc["@mozilla.org/browser/annotation-service;1"].
|
||||
getService(Ci.nsIAnnotationService);
|
||||
|
||||
/**
|
||||
* Creates an aged annotation.
|
||||
*
|
||||
* @param aIdentifier Either a page url or an item id.
|
||||
* @param aIdentifier Name of the annotation.
|
||||
* @param aValue Value for the annotation.
|
||||
* @param aExpirePolicy Expiration policy of the annotation.
|
||||
* @param aAgeInDays Age in days of the annotation.
|
||||
* @param [optional] aLastModifiedAgeInDays Age in days of the annotation, for lastModified.
|
||||
*/
|
||||
let now = Date.now();
|
||||
function add_old_anno(aIdentifier, aName, aValue, aExpirePolicy,
|
||||
aAgeInDays, aLastModifiedAgeInDays) {
|
||||
let expireDate = (now - (aAgeInDays * 86400 * 1000)) * 1000;
|
||||
let lastModifiedDate = 0;
|
||||
if (aLastModifiedAgeInDays)
|
||||
lastModifiedDate = (now - (aLastModifiedAgeInDays * 86400 * 1000)) * 1000;
|
||||
|
||||
let dbConn = DBConn();
|
||||
let sql;
|
||||
if (typeof(aIdentifier) == "number") {
|
||||
// Item annotation.
|
||||
as.setItemAnnotation(aIdentifier, aName, aValue, 0, aExpirePolicy);
|
||||
// Update dateAdded for the last added annotation.
|
||||
sql = "UPDATE moz_items_annos SET dateAdded = :expire_date, lastModified = :last_modified " +
|
||||
"WHERE id = (SELECT id FROM moz_items_annos " +
|
||||
"WHERE item_id = :id " +
|
||||
"ORDER BY dateAdded DESC LIMIT 1)";
|
||||
}
|
||||
else if (aIdentifier instanceof Ci.nsIURI){
|
||||
// Page annotation.
|
||||
as.setPageAnnotation(aIdentifier, aName, aValue, 0, aExpirePolicy);
|
||||
// Update dateAdded for the last added annotation.
|
||||
sql = "UPDATE moz_annos SET dateAdded = :expire_date, lastModified = :last_modified " +
|
||||
"WHERE id = (SELECT a.id FROM moz_annos a " +
|
||||
"LEFT JOIN moz_places_view h on h.id = a.place_id " +
|
||||
"WHERE h.url = :id " +
|
||||
"ORDER BY a.dateAdded DESC LIMIT 1)";
|
||||
}
|
||||
else
|
||||
do_throw("Wrong identifier type");
|
||||
|
||||
let stmt = dbConn.createStatement(sql);
|
||||
stmt.params.id = (typeof(aIdentifier) == "number") ? aIdentifier
|
||||
: aIdentifier.spec;
|
||||
stmt.params.expire_date = expireDate;
|
||||
stmt.params.last_modified = lastModifiedDate;
|
||||
try {
|
||||
stmt.executeStep();
|
||||
}
|
||||
finally {
|
||||
stmt.finalize();
|
||||
}
|
||||
}
|
||||
|
||||
function run_test() {
|
||||
// Set interval to a large value so we don't expire on it.
|
||||
setInterval(3600); // 1h
|
||||
|
||||
// Expire all expirable pages.
|
||||
setMaxPages(0);
|
||||
|
||||
// Add some bookmarked page with visit and annotations.
|
||||
for (let i = 0; i < 5; i++) {
|
||||
let pageURI = uri("http://item_anno." + i + ".mozilla.org/");
|
||||
// This visit will be expired.
|
||||
hs.addVisit(pageURI, now++, null, hs.TRANSITION_TYPED, false, 0);
|
||||
let id = bs.insertBookmark(bs.unfiledBookmarksFolder, pageURI,
|
||||
bs.DEFAULT_INDEX, null);
|
||||
// Will persist because it's an EXPIRE_NEVER item anno.
|
||||
as.setItemAnnotation(id, "persist", "test", 0, as.EXPIRE_NEVER);
|
||||
// Will persist because the page is bookmarked.
|
||||
as.setPageAnnotation(pageURI, "persist", "test", 0, as.EXPIRE_NEVER);
|
||||
// All EXPIRE_SESSION annotations are expected to expire on clear history.
|
||||
as.setItemAnnotation(id, "expire_session", "test", 0, as.EXPIRE_SESSION);
|
||||
as.setPageAnnotation(pageURI, "expire_session", "test", 0, as.EXPIRE_SESSION);
|
||||
// Annotations with timed policy will expire regardless bookmarked status.
|
||||
add_old_anno(id, "expire_days", "test", as.EXPIRE_DAYS, 8);
|
||||
add_old_anno(id, "expire_weeks", "test", as.EXPIRE_WEEKS, 31);
|
||||
add_old_anno(id, "expire_months", "test", as.EXPIRE_MONTHS, 181);
|
||||
add_old_anno(pageURI, "expire_days", "test", as.EXPIRE_DAYS, 8);
|
||||
add_old_anno(pageURI, "expire_weeks", "test", as.EXPIRE_WEEKS, 31);
|
||||
add_old_anno(pageURI, "expire_months", "test", as.EXPIRE_MONTHS, 181);
|
||||
}
|
||||
|
||||
// Add some visited page and annotations for each.
|
||||
let now = Date.now() * 1000;
|
||||
for (let i = 0; i < 5; i++) {
|
||||
// All page annotations related to these expired pages are expected to
|
||||
// expire as well.
|
||||
let pageURI = uri("http://page_anno." + i + ".mozilla.org/");
|
||||
hs.addVisit(pageURI, now++, null, hs.TRANSITION_TYPED, false, 0);
|
||||
as.setPageAnnotation(pageURI, "expire", "test", 0, as.EXPIRE_NEVER);
|
||||
as.setPageAnnotation(pageURI, "expire_session", "test", 0, as.EXPIRE_SESSION);
|
||||
add_old_anno(pageURI, "expire_days", "test", as.EXPIRE_DAYS, 8);
|
||||
add_old_anno(pageURI, "expire_weeks", "test", as.EXPIRE_WEEKS, 31);
|
||||
add_old_anno(pageURI, "expire_months", "test", as.EXPIRE_MONTHS, 181);
|
||||
}
|
||||
|
||||
// Observe expirations.
|
||||
observer = {
|
||||
observe: function(aSubject, aTopic, aData) {
|
||||
os.removeObserver(observer, TOPIC_EXPIRATION_FINISHED);
|
||||
|
||||
["expire_days", "expire_weeks", "expire_months", "expire_session",
|
||||
"expire"].forEach(function(aAnno) {
|
||||
let pages = as.getPagesWithAnnotation(aAnno);
|
||||
do_check_eq(pages.length, 0);
|
||||
});
|
||||
|
||||
["expire_days", "expire_weeks", "expire_months", "expire_session",
|
||||
"expire"].forEach(function(aAnno) {
|
||||
let items = as.getItemsWithAnnotation(aAnno);
|
||||
do_check_eq(items.length, 0);
|
||||
});
|
||||
|
||||
["persist"].forEach(function(aAnno) {
|
||||
let pages = as.getPagesWithAnnotation(aAnno);
|
||||
do_check_eq(pages.length, 5);
|
||||
});
|
||||
|
||||
["persist"].forEach(function(aAnno) {
|
||||
let items = as.getItemsWithAnnotation(aAnno);
|
||||
do_check_eq(items.length, 5);
|
||||
items.forEach(function(aItemId) {
|
||||
// Check item exists.
|
||||
bs.getItemIndex(aItemId);
|
||||
});
|
||||
});
|
||||
|
||||
do_test_finished();
|
||||
}
|
||||
};
|
||||
os.addObserver(observer, TOPIC_EXPIRATION_FINISHED, false);
|
||||
|
||||
// Expire all visits for the bookmarks.
|
||||
hs.QueryInterface(Ci.nsIBrowserHistory).removeAllPages();
|
||||
do_test_pending();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user