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: // Get history services michael@0: var histsvc = Cc["@mozilla.org/browser/nav-history-service;1"]. michael@0: getService(Ci.nsINavHistoryService); michael@0: michael@0: /** michael@0: * Checks to see that a URI is in the database. michael@0: * michael@0: * @param aURI michael@0: * The URI to check. michael@0: * @returns true if the URI is in the DB, false otherwise. michael@0: */ michael@0: function uri_in_db(aURI) { michael@0: var options = histsvc.getNewQueryOptions(); michael@0: options.maxResults = 1; michael@0: options.resultType = options.RESULTS_AS_URI michael@0: var query = histsvc.getNewQuery(); michael@0: query.uri = aURI; michael@0: var result = histsvc.executeQuery(query, options); michael@0: var root = result.root; michael@0: root.containerOpen = true; michael@0: var cc = root.childCount; michael@0: root.containerOpen = false; michael@0: return (cc == 1); michael@0: } michael@0: michael@0: // main michael@0: function run_test() michael@0: { michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_task(function test_execute() michael@0: { michael@0: // we have a new profile, so we should have imported bookmarks michael@0: do_check_eq(histsvc.databaseStatus, histsvc.DATABASE_STATUS_CREATE); michael@0: michael@0: // add a visit michael@0: var testURI = uri("http://mozilla.com"); michael@0: yield promiseAddVisits(testURI); michael@0: michael@0: // now query for the visit, setting sorting and limit such that michael@0: // we should retrieve only the visit we just added michael@0: var options = histsvc.getNewQueryOptions(); michael@0: options.sortingMode = options.SORT_BY_DATE_DESCENDING; michael@0: options.maxResults = 1; michael@0: // TODO: using full visit crashes in xpcshell test michael@0: //options.resultType = options.RESULTS_AS_FULL_VISIT; michael@0: options.resultType = options.RESULTS_AS_VISIT; michael@0: var query = histsvc.getNewQuery(); michael@0: var result = histsvc.executeQuery(query, options); michael@0: var root = result.root; michael@0: root.containerOpen = true; michael@0: var cc = root.childCount; michael@0: for (var i=0; i < cc; ++i) { michael@0: var node = root.getChild(i); michael@0: // test node properties in RESULTS_AS_VISIT michael@0: do_check_eq(node.uri, testURI.spec); michael@0: do_check_eq(node.type, Ci.nsINavHistoryResultNode.RESULT_TYPE_URI); michael@0: // TODO: change query type to RESULTS_AS_FULL_VISIT and test this michael@0: //do_check_eq(node.transitionType, histsvc.TRANSITION_TYPED); michael@0: } michael@0: root.containerOpen = false; michael@0: michael@0: // add another visit for the same URI, and a third visit for a different URI michael@0: var testURI2 = uri("http://google.com/"); michael@0: yield promiseAddVisits(testURI); michael@0: yield promiseAddVisits(testURI2); michael@0: michael@0: options.maxResults = 5; michael@0: options.resultType = options.RESULTS_AS_URI; michael@0: michael@0: // test minVisits michael@0: query.minVisits = 0; michael@0: result = histsvc.executeQuery(query, options); michael@0: result.root.containerOpen = true; michael@0: do_check_eq(result.root.childCount, 2); michael@0: result.root.containerOpen = false; michael@0: query.minVisits = 1; michael@0: result = histsvc.executeQuery(query, options); michael@0: result.root.containerOpen = true; michael@0: do_check_eq(result.root.childCount, 2); michael@0: result.root.containerOpen = false; michael@0: query.minVisits = 2; michael@0: result = histsvc.executeQuery(query, options); michael@0: result.root.containerOpen = true; michael@0: do_check_eq(result.root.childCount, 1); michael@0: query.minVisits = 3; michael@0: result.root.containerOpen = false; michael@0: result = histsvc.executeQuery(query, options); michael@0: result.root.containerOpen = true; michael@0: do_check_eq(result.root.childCount, 0); michael@0: result.root.containerOpen = false; michael@0: michael@0: // test maxVisits michael@0: query.minVisits = -1; michael@0: query.maxVisits = -1; michael@0: result = histsvc.executeQuery(query, options); michael@0: result.root.containerOpen = true; michael@0: do_check_eq(result.root.childCount, 2); michael@0: result.root.containerOpen = false; michael@0: query.maxVisits = 0; michael@0: result = histsvc.executeQuery(query, options); michael@0: result.root.containerOpen = true; michael@0: do_check_eq(result.root.childCount, 0); michael@0: result.root.containerOpen = false; michael@0: query.maxVisits = 1; michael@0: result = histsvc.executeQuery(query, options); michael@0: result.root.containerOpen = true; michael@0: do_check_eq(result.root.childCount, 1); michael@0: result.root.containerOpen = false; michael@0: query.maxVisits = 2; michael@0: result = histsvc.executeQuery(query, options); michael@0: result.root.containerOpen = true; michael@0: do_check_eq(result.root.childCount, 2); michael@0: result.root.containerOpen = false; michael@0: query.maxVisits = 3; michael@0: result = histsvc.executeQuery(query, options); michael@0: result.root.containerOpen = true; michael@0: do_check_eq(result.root.childCount, 2); michael@0: result.root.containerOpen = false; michael@0: michael@0: // test annotation-based queries michael@0: var annos = Cc["@mozilla.org/browser/annotation-service;1"]. michael@0: getService(Ci.nsIAnnotationService); michael@0: annos.setPageAnnotation(uri("http://mozilla.com/"), "testAnno", 0, 0, michael@0: Ci.nsIAnnotationService.EXPIRE_NEVER); michael@0: query.annotation = "testAnno"; michael@0: result = histsvc.executeQuery(query, options); michael@0: result.root.containerOpen = true; michael@0: do_check_eq(result.root.childCount, 1); michael@0: do_check_eq(result.root.getChild(0).uri, "http://mozilla.com/"); michael@0: result.root.containerOpen = false; michael@0: michael@0: // test annotationIsNot michael@0: query.annotationIsNot = true; michael@0: result = histsvc.executeQuery(query, options); michael@0: result.root.containerOpen = true; michael@0: do_check_eq(result.root.childCount, 1); michael@0: do_check_eq(result.root.getChild(0).uri, "http://google.com/"); michael@0: result.root.containerOpen = false; michael@0: michael@0: // By default history is enabled. michael@0: do_check_true(!histsvc.historyDisabled); michael@0: michael@0: // test getPageTitle michael@0: yield promiseAddVisits({ uri: uri("http://example.com"), title: "title" }); michael@0: let placeInfo = yield PlacesUtils.promisePlaceInfo(uri("http://example.com")); michael@0: do_check_eq(placeInfo.title, "title"); michael@0: michael@0: // query for the visit michael@0: do_check_true(uri_in_db(testURI)); michael@0: michael@0: // test for schema changes in bug 373239 michael@0: // get direct db connection michael@0: var db = histsvc.QueryInterface(Ci.nsPIPlacesDatabase).DBConnection; michael@0: var q = "SELECT id FROM moz_bookmarks"; michael@0: var statement; michael@0: try { michael@0: statement = db.createStatement(q); michael@0: } catch(ex) { michael@0: do_throw("bookmarks table does not have id field, schema is too old!"); michael@0: } michael@0: finally { michael@0: statement.finalize(); michael@0: } michael@0: michael@0: // bug 394741 - regressed history text searches michael@0: yield promiseAddVisits(uri("http://mozilla.com")); michael@0: var options = histsvc.getNewQueryOptions(); michael@0: //options.resultType = options.RESULTS_AS_VISIT; michael@0: var query = histsvc.getNewQuery(); michael@0: query.searchTerms = "moz"; michael@0: var result = histsvc.executeQuery(query, options); michael@0: var root = result.root; michael@0: root.containerOpen = true; michael@0: do_check_true(root.childCount > 0); michael@0: root.containerOpen = false; michael@0: });