michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et: */ 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: const TEST_URI = NetUtil.newURI("http://mozilla.com/"); michael@0: const TEST_SUBDOMAIN_URI = NetUtil.newURI("http://foobar.mozilla.com/"); michael@0: michael@0: add_test(function test_addPage() michael@0: { michael@0: promiseAddVisits(TEST_URI).then(function () { michael@0: do_check_eq(1, PlacesUtils.history.hasHistoryEntries); michael@0: run_next_test(); michael@0: }); michael@0: }); michael@0: michael@0: add_test(function test_removePage() michael@0: { michael@0: PlacesUtils.bhistory.removePage(TEST_URI); michael@0: do_check_eq(0, PlacesUtils.history.hasHistoryEntries); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: add_test(function test_removePages() michael@0: { michael@0: let pages = []; michael@0: for (let i = 0; i < 8; i++) { michael@0: pages.push(NetUtil.newURI(TEST_URI.spec + i)); michael@0: } michael@0: michael@0: promiseAddVisits(pages.map(function (uri) ({ uri: uri }))).then(function () { michael@0: // Bookmarked item should not be removed from moz_places. michael@0: const ANNO_INDEX = 1; michael@0: const ANNO_NAME = "testAnno"; michael@0: const ANNO_VALUE = "foo"; michael@0: const BOOKMARK_INDEX = 2; michael@0: PlacesUtils.annotations.setPageAnnotation(pages[ANNO_INDEX], michael@0: ANNO_NAME, ANNO_VALUE, 0, michael@0: Ci.nsIAnnotationService.EXPIRE_NEVER); michael@0: PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId, michael@0: pages[BOOKMARK_INDEX], michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, michael@0: "test bookmark"); michael@0: PlacesUtils.annotations.setPageAnnotation(pages[BOOKMARK_INDEX], michael@0: ANNO_NAME, ANNO_VALUE, 0, michael@0: Ci.nsIAnnotationService.EXPIRE_NEVER); michael@0: michael@0: PlacesUtils.bhistory.removePages(pages, pages.length); michael@0: do_check_eq(0, PlacesUtils.history.hasHistoryEntries); michael@0: michael@0: // Check that the bookmark and its annotation still exist. michael@0: do_check_true(PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.unfiledBookmarksFolderId, 0) > 0); michael@0: do_check_eq(PlacesUtils.annotations.getPageAnnotation(pages[BOOKMARK_INDEX], ANNO_NAME), michael@0: ANNO_VALUE); michael@0: michael@0: // Check the annotation on the non-bookmarked page does not exist anymore. michael@0: try { michael@0: PlacesUtils.annotations.getPageAnnotation(pages[ANNO_INDEX], ANNO_NAME); michael@0: do_throw("did not expire expire_never anno on a not bookmarked item"); michael@0: } catch(ex) {} michael@0: michael@0: // Cleanup. michael@0: PlacesUtils.bookmarks.removeFolderChildren(PlacesUtils.unfiledBookmarksFolderId); michael@0: promiseClearHistory().then(run_next_test); michael@0: }); michael@0: }); michael@0: michael@0: add_test(function test_removePagesByTimeframe() michael@0: { michael@0: let visits = []; michael@0: let startDate = Date.now() * 1000; michael@0: for (let i = 0; i < 10; i++) { michael@0: visits.push({ michael@0: uri: NetUtil.newURI(TEST_URI.spec + i), michael@0: visitDate: startDate + i michael@0: }); michael@0: } michael@0: michael@0: promiseAddVisits(visits).then(function () { michael@0: // Delete all pages except the first and the last. michael@0: PlacesUtils.bhistory.removePagesByTimeframe(startDate + 1, startDate + 8); michael@0: michael@0: // Check that we have removed the correct pages. michael@0: for (let i = 0; i < 10; i++) { michael@0: do_check_eq(page_in_database(NetUtil.newURI(TEST_URI.spec + i)) == 0, michael@0: i > 0 && i < 9); michael@0: } michael@0: michael@0: // Clear remaining items and check that all pages have been removed. michael@0: PlacesUtils.bhistory.removePagesByTimeframe(startDate, startDate + 9); michael@0: do_check_eq(0, PlacesUtils.history.hasHistoryEntries); michael@0: run_next_test(); michael@0: }); michael@0: }); michael@0: michael@0: add_test(function test_removePagesFromHost() michael@0: { michael@0: promiseAddVisits(TEST_URI).then(function () { michael@0: PlacesUtils.bhistory.removePagesFromHost("mozilla.com", true); michael@0: do_check_eq(0, PlacesUtils.history.hasHistoryEntries); michael@0: run_next_test(); michael@0: }); michael@0: }); michael@0: michael@0: add_test(function test_removePagesFromHost_keepSubdomains() michael@0: { michael@0: promiseAddVisits([{ uri: TEST_URI }, { uri: TEST_SUBDOMAIN_URI }]).then(function () { michael@0: PlacesUtils.bhistory.removePagesFromHost("mozilla.com", false); michael@0: do_check_eq(1, PlacesUtils.history.hasHistoryEntries); michael@0: run_next_test(); michael@0: }); michael@0: }); michael@0: michael@0: add_test(function test_removeAllPages() michael@0: { michael@0: PlacesUtils.bhistory.removeAllPages(); michael@0: do_check_eq(0, PlacesUtils.history.hasHistoryEntries); michael@0: run_next_test(); michael@0: }); michael@0: michael@0: function run_test() michael@0: { michael@0: run_next_test(); michael@0: }