toolkit/components/places/tests/unit/test_history.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/unit/test_history.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,184 @@
     1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et: */
     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 +// Get history services
    1.11 +var histsvc = Cc["@mozilla.org/browser/nav-history-service;1"].
    1.12 +              getService(Ci.nsINavHistoryService);
    1.13 +
    1.14 +/**
    1.15 + * Checks to see that a URI is in the database.
    1.16 + *
    1.17 + * @param aURI
    1.18 + *        The URI to check.
    1.19 + * @returns true if the URI is in the DB, false otherwise.
    1.20 + */
    1.21 +function uri_in_db(aURI) {
    1.22 +  var options = histsvc.getNewQueryOptions();
    1.23 +  options.maxResults = 1;
    1.24 +  options.resultType = options.RESULTS_AS_URI
    1.25 +  var query = histsvc.getNewQuery();
    1.26 +  query.uri = aURI;
    1.27 +  var result = histsvc.executeQuery(query, options);
    1.28 +  var root = result.root;
    1.29 +  root.containerOpen = true;
    1.30 +  var cc = root.childCount;
    1.31 +  root.containerOpen = false;
    1.32 +  return (cc == 1);
    1.33 +}
    1.34 +
    1.35 +// main
    1.36 +function run_test()
    1.37 +{
    1.38 +  run_next_test();
    1.39 +}
    1.40 +
    1.41 +add_task(function test_execute()
    1.42 +{
    1.43 +  // we have a new profile, so we should have imported bookmarks
    1.44 +  do_check_eq(histsvc.databaseStatus, histsvc.DATABASE_STATUS_CREATE);
    1.45 +
    1.46 +  // add a visit
    1.47 +  var testURI = uri("http://mozilla.com");
    1.48 +  yield promiseAddVisits(testURI);
    1.49 +
    1.50 +  // now query for the visit, setting sorting and limit such that
    1.51 +  // we should retrieve only the visit we just added
    1.52 +  var options = histsvc.getNewQueryOptions();
    1.53 +  options.sortingMode = options.SORT_BY_DATE_DESCENDING;
    1.54 +  options.maxResults = 1;
    1.55 +  // TODO: using full visit crashes in xpcshell test
    1.56 +  //options.resultType = options.RESULTS_AS_FULL_VISIT;
    1.57 +  options.resultType = options.RESULTS_AS_VISIT;
    1.58 +  var query = histsvc.getNewQuery();
    1.59 +  var result = histsvc.executeQuery(query, options);
    1.60 +  var root = result.root;
    1.61 +  root.containerOpen = true;
    1.62 +  var cc = root.childCount;
    1.63 +  for (var i=0; i < cc; ++i) {
    1.64 +    var node = root.getChild(i);
    1.65 +    // test node properties in RESULTS_AS_VISIT
    1.66 +    do_check_eq(node.uri, testURI.spec);
    1.67 +    do_check_eq(node.type, Ci.nsINavHistoryResultNode.RESULT_TYPE_URI);
    1.68 +    // TODO: change query type to RESULTS_AS_FULL_VISIT and test this
    1.69 +    //do_check_eq(node.transitionType, histsvc.TRANSITION_TYPED);
    1.70 +  }
    1.71 +  root.containerOpen = false;
    1.72 +
    1.73 +  // add another visit for the same URI, and a third visit for a different URI
    1.74 +  var testURI2 = uri("http://google.com/");
    1.75 +  yield promiseAddVisits(testURI);
    1.76 +  yield promiseAddVisits(testURI2);
    1.77 +
    1.78 +  options.maxResults = 5;
    1.79 +  options.resultType = options.RESULTS_AS_URI;
    1.80 +
    1.81 +  // test minVisits
    1.82 +  query.minVisits = 0;
    1.83 +  result = histsvc.executeQuery(query, options);
    1.84 +  result.root.containerOpen = true;
    1.85 +  do_check_eq(result.root.childCount, 2);
    1.86 +  result.root.containerOpen = false;
    1.87 +  query.minVisits = 1;
    1.88 +  result = histsvc.executeQuery(query, options);
    1.89 +  result.root.containerOpen = true;
    1.90 +  do_check_eq(result.root.childCount, 2);
    1.91 +  result.root.containerOpen = false;
    1.92 +  query.minVisits = 2;
    1.93 +  result = histsvc.executeQuery(query, options);
    1.94 +  result.root.containerOpen = true;
    1.95 +  do_check_eq(result.root.childCount, 1);
    1.96 +  query.minVisits = 3;
    1.97 +  result.root.containerOpen = false;
    1.98 +  result = histsvc.executeQuery(query, options);
    1.99 +  result.root.containerOpen = true;
   1.100 +  do_check_eq(result.root.childCount, 0);
   1.101 +  result.root.containerOpen = false;
   1.102 +
   1.103 +  // test maxVisits
   1.104 +  query.minVisits = -1;
   1.105 +  query.maxVisits = -1;
   1.106 +  result = histsvc.executeQuery(query, options);
   1.107 +  result.root.containerOpen = true;
   1.108 +  do_check_eq(result.root.childCount, 2);
   1.109 +  result.root.containerOpen = false;
   1.110 +  query.maxVisits = 0;
   1.111 +  result = histsvc.executeQuery(query, options);
   1.112 +  result.root.containerOpen = true;
   1.113 +  do_check_eq(result.root.childCount, 0);
   1.114 +  result.root.containerOpen = false;
   1.115 +  query.maxVisits = 1;
   1.116 +  result = histsvc.executeQuery(query, options);
   1.117 +  result.root.containerOpen = true;
   1.118 +  do_check_eq(result.root.childCount, 1);
   1.119 +  result.root.containerOpen = false;
   1.120 +  query.maxVisits = 2;
   1.121 +  result = histsvc.executeQuery(query, options);
   1.122 +  result.root.containerOpen = true;
   1.123 +  do_check_eq(result.root.childCount, 2);
   1.124 +  result.root.containerOpen = false;
   1.125 +  query.maxVisits = 3;
   1.126 +  result = histsvc.executeQuery(query, options);
   1.127 +  result.root.containerOpen = true;
   1.128 +  do_check_eq(result.root.childCount, 2);
   1.129 +  result.root.containerOpen = false;
   1.130 +  
   1.131 +  // test annotation-based queries
   1.132 +  var annos = Cc["@mozilla.org/browser/annotation-service;1"].
   1.133 +              getService(Ci.nsIAnnotationService);
   1.134 +  annos.setPageAnnotation(uri("http://mozilla.com/"), "testAnno", 0, 0,
   1.135 +                          Ci.nsIAnnotationService.EXPIRE_NEVER);
   1.136 +  query.annotation = "testAnno";
   1.137 +  result = histsvc.executeQuery(query, options);
   1.138 +  result.root.containerOpen = true;
   1.139 +  do_check_eq(result.root.childCount, 1);
   1.140 +  do_check_eq(result.root.getChild(0).uri, "http://mozilla.com/");
   1.141 +  result.root.containerOpen = false;
   1.142 +
   1.143 +  // test annotationIsNot
   1.144 +  query.annotationIsNot = true;
   1.145 +  result = histsvc.executeQuery(query, options);
   1.146 +  result.root.containerOpen = true;
   1.147 +  do_check_eq(result.root.childCount, 1);
   1.148 +  do_check_eq(result.root.getChild(0).uri, "http://google.com/");
   1.149 +  result.root.containerOpen = false;
   1.150 +
   1.151 +  // By default history is enabled.
   1.152 +  do_check_true(!histsvc.historyDisabled);
   1.153 +
   1.154 +  // test getPageTitle
   1.155 +  yield promiseAddVisits({ uri: uri("http://example.com"), title: "title" });
   1.156 +  let placeInfo = yield PlacesUtils.promisePlaceInfo(uri("http://example.com"));
   1.157 +  do_check_eq(placeInfo.title, "title");
   1.158 +
   1.159 +  // query for the visit
   1.160 +  do_check_true(uri_in_db(testURI));
   1.161 +
   1.162 +  // test for schema changes in bug 373239
   1.163 +  // get direct db connection
   1.164 +  var db = histsvc.QueryInterface(Ci.nsPIPlacesDatabase).DBConnection;
   1.165 +  var q = "SELECT id FROM moz_bookmarks";
   1.166 +  var statement;
   1.167 +  try {
   1.168 +     statement = db.createStatement(q);
   1.169 +  } catch(ex) {
   1.170 +    do_throw("bookmarks table does not have id field, schema is too old!");
   1.171 +  }
   1.172 +  finally {
   1.173 +    statement.finalize();
   1.174 +  }
   1.175 +
   1.176 +  // bug 394741 - regressed history text searches
   1.177 +  yield promiseAddVisits(uri("http://mozilla.com"));
   1.178 +  var options = histsvc.getNewQueryOptions();
   1.179 +  //options.resultType = options.RESULTS_AS_VISIT;
   1.180 +  var query = histsvc.getNewQuery();
   1.181 +  query.searchTerms = "moz";
   1.182 +  var result = histsvc.executeQuery(query, options);
   1.183 +  var root = result.root;
   1.184 +  root.containerOpen = true;
   1.185 +  do_check_true(root.childCount > 0);
   1.186 +  root.containerOpen = false;
   1.187 +});

mercurial