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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* vim:set ts=2 sw=2 sts=2 et: */
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 // Get history services
     8 var histsvc = Cc["@mozilla.org/browser/nav-history-service;1"].
     9               getService(Ci.nsINavHistoryService);
    11 /**
    12  * Checks to see that a URI is in the database.
    13  *
    14  * @param aURI
    15  *        The URI to check.
    16  * @returns true if the URI is in the DB, false otherwise.
    17  */
    18 function uri_in_db(aURI) {
    19   var options = histsvc.getNewQueryOptions();
    20   options.maxResults = 1;
    21   options.resultType = options.RESULTS_AS_URI
    22   var query = histsvc.getNewQuery();
    23   query.uri = aURI;
    24   var result = histsvc.executeQuery(query, options);
    25   var root = result.root;
    26   root.containerOpen = true;
    27   var cc = root.childCount;
    28   root.containerOpen = false;
    29   return (cc == 1);
    30 }
    32 // main
    33 function run_test()
    34 {
    35   run_next_test();
    36 }
    38 add_task(function test_execute()
    39 {
    40   // we have a new profile, so we should have imported bookmarks
    41   do_check_eq(histsvc.databaseStatus, histsvc.DATABASE_STATUS_CREATE);
    43   // add a visit
    44   var testURI = uri("http://mozilla.com");
    45   yield promiseAddVisits(testURI);
    47   // now query for the visit, setting sorting and limit such that
    48   // we should retrieve only the visit we just added
    49   var options = histsvc.getNewQueryOptions();
    50   options.sortingMode = options.SORT_BY_DATE_DESCENDING;
    51   options.maxResults = 1;
    52   // TODO: using full visit crashes in xpcshell test
    53   //options.resultType = options.RESULTS_AS_FULL_VISIT;
    54   options.resultType = options.RESULTS_AS_VISIT;
    55   var query = histsvc.getNewQuery();
    56   var result = histsvc.executeQuery(query, options);
    57   var root = result.root;
    58   root.containerOpen = true;
    59   var cc = root.childCount;
    60   for (var i=0; i < cc; ++i) {
    61     var node = root.getChild(i);
    62     // test node properties in RESULTS_AS_VISIT
    63     do_check_eq(node.uri, testURI.spec);
    64     do_check_eq(node.type, Ci.nsINavHistoryResultNode.RESULT_TYPE_URI);
    65     // TODO: change query type to RESULTS_AS_FULL_VISIT and test this
    66     //do_check_eq(node.transitionType, histsvc.TRANSITION_TYPED);
    67   }
    68   root.containerOpen = false;
    70   // add another visit for the same URI, and a third visit for a different URI
    71   var testURI2 = uri("http://google.com/");
    72   yield promiseAddVisits(testURI);
    73   yield promiseAddVisits(testURI2);
    75   options.maxResults = 5;
    76   options.resultType = options.RESULTS_AS_URI;
    78   // test minVisits
    79   query.minVisits = 0;
    80   result = histsvc.executeQuery(query, options);
    81   result.root.containerOpen = true;
    82   do_check_eq(result.root.childCount, 2);
    83   result.root.containerOpen = false;
    84   query.minVisits = 1;
    85   result = histsvc.executeQuery(query, options);
    86   result.root.containerOpen = true;
    87   do_check_eq(result.root.childCount, 2);
    88   result.root.containerOpen = false;
    89   query.minVisits = 2;
    90   result = histsvc.executeQuery(query, options);
    91   result.root.containerOpen = true;
    92   do_check_eq(result.root.childCount, 1);
    93   query.minVisits = 3;
    94   result.root.containerOpen = false;
    95   result = histsvc.executeQuery(query, options);
    96   result.root.containerOpen = true;
    97   do_check_eq(result.root.childCount, 0);
    98   result.root.containerOpen = false;
   100   // test maxVisits
   101   query.minVisits = -1;
   102   query.maxVisits = -1;
   103   result = histsvc.executeQuery(query, options);
   104   result.root.containerOpen = true;
   105   do_check_eq(result.root.childCount, 2);
   106   result.root.containerOpen = false;
   107   query.maxVisits = 0;
   108   result = histsvc.executeQuery(query, options);
   109   result.root.containerOpen = true;
   110   do_check_eq(result.root.childCount, 0);
   111   result.root.containerOpen = false;
   112   query.maxVisits = 1;
   113   result = histsvc.executeQuery(query, options);
   114   result.root.containerOpen = true;
   115   do_check_eq(result.root.childCount, 1);
   116   result.root.containerOpen = false;
   117   query.maxVisits = 2;
   118   result = histsvc.executeQuery(query, options);
   119   result.root.containerOpen = true;
   120   do_check_eq(result.root.childCount, 2);
   121   result.root.containerOpen = false;
   122   query.maxVisits = 3;
   123   result = histsvc.executeQuery(query, options);
   124   result.root.containerOpen = true;
   125   do_check_eq(result.root.childCount, 2);
   126   result.root.containerOpen = false;
   128   // test annotation-based queries
   129   var annos = Cc["@mozilla.org/browser/annotation-service;1"].
   130               getService(Ci.nsIAnnotationService);
   131   annos.setPageAnnotation(uri("http://mozilla.com/"), "testAnno", 0, 0,
   132                           Ci.nsIAnnotationService.EXPIRE_NEVER);
   133   query.annotation = "testAnno";
   134   result = histsvc.executeQuery(query, options);
   135   result.root.containerOpen = true;
   136   do_check_eq(result.root.childCount, 1);
   137   do_check_eq(result.root.getChild(0).uri, "http://mozilla.com/");
   138   result.root.containerOpen = false;
   140   // test annotationIsNot
   141   query.annotationIsNot = true;
   142   result = histsvc.executeQuery(query, options);
   143   result.root.containerOpen = true;
   144   do_check_eq(result.root.childCount, 1);
   145   do_check_eq(result.root.getChild(0).uri, "http://google.com/");
   146   result.root.containerOpen = false;
   148   // By default history is enabled.
   149   do_check_true(!histsvc.historyDisabled);
   151   // test getPageTitle
   152   yield promiseAddVisits({ uri: uri("http://example.com"), title: "title" });
   153   let placeInfo = yield PlacesUtils.promisePlaceInfo(uri("http://example.com"));
   154   do_check_eq(placeInfo.title, "title");
   156   // query for the visit
   157   do_check_true(uri_in_db(testURI));
   159   // test for schema changes in bug 373239
   160   // get direct db connection
   161   var db = histsvc.QueryInterface(Ci.nsPIPlacesDatabase).DBConnection;
   162   var q = "SELECT id FROM moz_bookmarks";
   163   var statement;
   164   try {
   165      statement = db.createStatement(q);
   166   } catch(ex) {
   167     do_throw("bookmarks table does not have id field, schema is too old!");
   168   }
   169   finally {
   170     statement.finalize();
   171   }
   173   // bug 394741 - regressed history text searches
   174   yield promiseAddVisits(uri("http://mozilla.com"));
   175   var options = histsvc.getNewQueryOptions();
   176   //options.resultType = options.RESULTS_AS_VISIT;
   177   var query = histsvc.getNewQuery();
   178   query.searchTerms = "moz";
   179   var result = histsvc.executeQuery(query, options);
   180   var root = result.root;
   181   root.containerOpen = true;
   182   do_check_true(root.childCount > 0);
   183   root.containerOpen = false;
   184 });

mercurial