toolkit/components/places/tests/queries/test_results-as-visit.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/queries/test_results-as-visit.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,113 @@
     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 +var testData = [];
    1.10 +var now = Date.now() * 1000;
    1.11 +function createTestData() {
    1.12 +  function generateVisits(aPage) {
    1.13 +    for (var i = 0; i < aPage.visitCount; i++) {
    1.14 +      testData.push({ isInQuery: aPage.inQuery,
    1.15 +                      isVisit: true,
    1.16 +                      title: aPage.title,
    1.17 +                      uri: aPage.uri,
    1.18 +                      lastVisit: now++,
    1.19 +                      isTag: aPage.tags && aPage.tags.length > 0,
    1.20 +                      tagArray: aPage.tags });
    1.21 +    }
    1.22 +  }
    1.23 +  
    1.24 +  var pages = [
    1.25 +    { uri: "http://foo.com/", title: "amo", tags: ["moz"], visitCount: 3, inQuery: true },
    1.26 +    { uri: "http://moilla.com/", title: "bMoz", tags: ["bugzilla"], visitCount: 5, inQuery: true },
    1.27 +    { uri: "http://foo.mail.com/changeme1.html", title: "c Moz", visitCount: 7, inQuery: true },
    1.28 +    { uri: "http://foo.mail.com/changeme2.html", tags: ["moz"], title: "", visitCount: 1, inQuery: false },
    1.29 +    { uri: "http://foo.mail.com/changeme3.html", title: "zydeco", visitCount: 5, inQuery: false },
    1.30 +  ];
    1.31 +  pages.forEach(generateVisits);
    1.32 +}
    1.33 +
    1.34 +/**
    1.35 + * This test will test Queries that use relative search terms and URI options
    1.36 + */
    1.37 +function run_test()
    1.38 +{
    1.39 +  run_next_test();
    1.40 +}
    1.41 +
    1.42 +add_task(function test_results_as_visit()
    1.43 +{
    1.44 +   createTestData();
    1.45 +   yield task_populateDB(testData);
    1.46 +   var query = PlacesUtils.history.getNewQuery();
    1.47 +   query.searchTerms = "moz";
    1.48 +   query.minVisits = 2;
    1.49 +
    1.50 +   // Options
    1.51 +   var options = PlacesUtils.history.getNewQueryOptions();
    1.52 +   options.sortingMode = options.SORT_BY_VISITCOUNT_ASCENDING;
    1.53 +   options.resultType = options.RESULTS_AS_VISIT;
    1.54 +
    1.55 +   // Results
    1.56 +   var result = PlacesUtils.history.executeQuery(query, options);
    1.57 +   var root = result.root;
    1.58 +   root.containerOpen = true;
    1.59 +
    1.60 +   LOG("Number of items in result set: " + root.childCount);
    1.61 +   for(var i=0; i < root.childCount; ++i) {
    1.62 +     LOG("result: " + root.getChild(i).uri + " Title: " + root.getChild(i).title);
    1.63 +   }
    1.64 +
    1.65 +   // Check our inital result set
    1.66 +   compareArrayToResult(testData, root);
    1.67 +
    1.68 +   // If that passes, check liveupdate
    1.69 +   // Add to the query set
    1.70 +   LOG("Adding item to query")
    1.71 +   var tmp = [];
    1.72 +   for (var i=0; i < 2; i++) {
    1.73 +     tmp.push({ isVisit: true,
    1.74 +                uri: "http://foo.com/added.html",
    1.75 +                title: "ab moz" });
    1.76 +   }
    1.77 +   yield task_populateDB(tmp);
    1.78 +   for (var i=0; i < 2; i++)
    1.79 +     do_check_eq(root.getChild(i).title, "ab moz");
    1.80 +
    1.81 +   // Update an existing URI
    1.82 +   LOG("Updating Item");
    1.83 +   var change2 = [{ isVisit: true,
    1.84 +                    title: "moz",
    1.85 +                    uri: "http://foo.mail.com/changeme2.html" }];
    1.86 +   yield task_populateDB(change2);
    1.87 +   do_check_true(isInResult(change2, root));
    1.88 +
    1.89 +   // Update some visits - add one and take one out of query set, and simply
    1.90 +   // change one so that it still applies to the query.
    1.91 +   LOG("Updating More Items");
    1.92 +   var change3 = [{ isVisit: true,
    1.93 +                    lastVisit: now++,
    1.94 +                    uri: "http://foo.mail.com/changeme1.html",
    1.95 +                    title: "foo"},
    1.96 +                  { isVisit: true,
    1.97 +                    lastVisit: now++,
    1.98 +                    uri: "http://foo.mail.com/changeme3.html",
    1.99 +                    title: "moz",
   1.100 +                    isTag: true,
   1.101 +                    tagArray: ["foo", "moz"] }];
   1.102 +   yield task_populateDB(change3);
   1.103 +   do_check_false(isInResult({uri: "http://foo.mail.com/changeme1.html"}, root));
   1.104 +   do_check_true(isInResult({uri: "http://foo.mail.com/changeme3.html"}, root));
   1.105 +
   1.106 +   // And now, delete one
   1.107 +   LOG("Delete item outside of batch");
   1.108 +   var change4 = [{ isVisit: true,
   1.109 +                    lastVisit: now++,
   1.110 +                    uri: "http://moilla.com/",
   1.111 +                    title: "mo,z" }];
   1.112 +   yield task_populateDB(change4);
   1.113 +   do_check_false(isInResult(change4, root));
   1.114 +
   1.115 +   root.containerOpen = false;
   1.116 +});

mercurial