toolkit/components/places/tests/queries/test_sort-date-site-grouping.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_sort-date-site-grouping.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,225 @@
     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 +/* ***** BEGIN LICENSE BLOCK *****
     1.7 +  Any copyright is dedicated to the Public Domain.
     1.8 +  http://creativecommons.org/publicdomain/zero/1.0/
     1.9 + * ***** END LICENSE BLOCK ***** */
    1.10 +
    1.11 +// This test ensures that the date and site type of |place:| query maintains
    1.12 +// its quantifications correctly. Namely, it ensures that the date part of the
    1.13 +// query is not lost when the domain queries are made.
    1.14 +
    1.15 +// We specifically craft these entries so that if a by Date and Site sorting is
    1.16 +// applied, we find one domain in the today range, and two domains in the older
    1.17 +// than six months range.
    1.18 +// The correspondence between item in |testData| and date range is stored in
    1.19 +// leveledTestData.
    1.20 +let testData = [
    1.21 +  {
    1.22 +    isVisit: true,
    1.23 +    uri: "file:///directory/1",
    1.24 +    lastVisit: today,
    1.25 +    title: "test visit",
    1.26 +    isInQuery: true
    1.27 +  },
    1.28 +  {
    1.29 +    isVisit: true,
    1.30 +    uri: "http://example.com/1",
    1.31 +    lastVisit: today,
    1.32 +    title: "test visit",
    1.33 +    isInQuery: true
    1.34 +  },
    1.35 +  {
    1.36 +    isVisit: true,
    1.37 +    uri: "http://example.com/2",
    1.38 +    lastVisit: today,
    1.39 +    title: "test visit",
    1.40 +    isInQuery: true
    1.41 +  },
    1.42 +  {
    1.43 +    isVisit: true,
    1.44 +    uri: "file:///directory/2",
    1.45 +    lastVisit: olderthansixmonths,
    1.46 +    title: "test visit",
    1.47 +    isInQuery: true
    1.48 +  },
    1.49 +  {
    1.50 +    isVisit: true,
    1.51 +    uri: "http://example.com/3",
    1.52 +    lastVisit: olderthansixmonths,
    1.53 +    title: "test visit",
    1.54 +    isInQuery: true
    1.55 +  },
    1.56 +  {
    1.57 +    isVisit: true,
    1.58 +    uri: "http://example.com/4",
    1.59 +    lastVisit: olderthansixmonths,
    1.60 +    title: "test visit",
    1.61 +    isInQuery: true
    1.62 +  },
    1.63 +  {
    1.64 +    isVisit: true,
    1.65 +    uri: "http://example.net/1",
    1.66 +    lastVisit: olderthansixmonths + 1,
    1.67 +    title: "test visit",
    1.68 +    isInQuery: true
    1.69 +  }
    1.70 +];
    1.71 +let domainsInRange = [2, 3];
    1.72 +let leveledTestData = [// Today
    1.73 +                       [[0],    // Today, local files
    1.74 +                        [1,2]], // Today, example.com
    1.75 +                       // Older than six months
    1.76 +                       [[3],    // Older than six months, local files
    1.77 +                        [4,5],  // Older than six months, example.com
    1.78 +                        [6]     // Older than six months, example.net
    1.79 +                        ]];
    1.80 +
    1.81 +// This test data is meant for live updating. The |levels| property indicates
    1.82 +// date range index and then domain index.
    1.83 +let testDataAddedLater = [
    1.84 +  {
    1.85 +    isVisit: true,
    1.86 +    uri: "http://example.com/5",
    1.87 +    lastVisit: olderthansixmonths,
    1.88 +    title: "test visit",
    1.89 +    isInQuery: true,
    1.90 +    levels: [1,1]
    1.91 +  },
    1.92 +  {
    1.93 +    isVisit: true,
    1.94 +    uri: "http://example.com/6",
    1.95 +    lastVisit: olderthansixmonths,
    1.96 +    title: "test visit",
    1.97 +    isInQuery: true,
    1.98 +    levels: [1,1]
    1.99 +  },
   1.100 +  {
   1.101 +    isVisit: true,
   1.102 +    uri: "http://example.com/7",
   1.103 +    lastVisit: today,
   1.104 +    title: "test visit",
   1.105 +    isInQuery: true,
   1.106 +    levels: [0,1]
   1.107 +  },
   1.108 +  {
   1.109 +    isVisit: true,
   1.110 +    uri: "file:///directory/3",
   1.111 +    lastVisit: today,
   1.112 +    title: "test visit",
   1.113 +    isInQuery: true,
   1.114 +    levels: [0,0]
   1.115 +  }
   1.116 +];
   1.117 +
   1.118 +function run_test()
   1.119 +{
   1.120 +  run_next_test();
   1.121 +}
   1.122 +
   1.123 +add_task(function test_sort_date_site_grouping()
   1.124 +{
   1.125 +  yield task_populateDB(testData);
   1.126 +
   1.127 +  // On Linux, the (local files) folder is shown after sites unlike Mac/Windows.
   1.128 +  // Thus, we avoid running this test on Linux but this should be re-enabled
   1.129 +  // after bug 624024 is resolved.
   1.130 +  let isLinux = ("@mozilla.org/gnome-gconf-service;1" in Components.classes);
   1.131 +  if (isLinux)
   1.132 +    return;
   1.133 +
   1.134 +  // In this test, there are three levels of results:
   1.135 +  // 1st: Date queries. e.g., today, last week, or older than 6 months.
   1.136 +  // 2nd: Domain queries restricted to a date. e.g. mozilla.com today.
   1.137 +  // 3rd: Actual visits. e.g. mozilla.com/index.html today.
   1.138 +  //
   1.139 +  // We store all the third level result roots so that we can easily close all
   1.140 +  // containers and test live updating into specific results.
   1.141 +  let roots = [];
   1.142 +
   1.143 +  let query = PlacesUtils.history.getNewQuery();
   1.144 +  let options = PlacesUtils.history.getNewQueryOptions();
   1.145 +  options.resultType = Ci.nsINavHistoryQueryOptions.RESULTS_AS_DATE_SITE_QUERY;
   1.146 +
   1.147 +  let root = PlacesUtils.history.executeQuery(query, options).root;
   1.148 +  root.containerOpen = true;
   1.149 +
   1.150 +  // This corresponds to the number of date ranges.
   1.151 +  do_check_eq(root.childCount, leveledTestData.length);
   1.152 +
   1.153 +  // We pass off to |checkFirstLevel| to check the first level of results.
   1.154 +  for (let index = 0; index < leveledTestData.length; index++) {
   1.155 +    let node = root.getChild(index);
   1.156 +    checkFirstLevel(index, node, roots);
   1.157 +  }
   1.158 +
   1.159 +  // Test live updating.
   1.160 +  testDataAddedLater.forEach(function(visit) {
   1.161 +    yield task_populateDB([visit]);
   1.162 +    let oldLength = testData.length;
   1.163 +    let i = visit.levels[0];
   1.164 +    let j = visit.levels[1];
   1.165 +    testData.push(visit);
   1.166 +    leveledTestData[i][j].push(oldLength);
   1.167 +    compareArrayToResult(leveledTestData[i][j].
   1.168 +                         map(function(x) testData[x]), roots[i][j]);
   1.169 +  });
   1.170 +
   1.171 +  for (let i = 0; i < roots.length; i++) {
   1.172 +    for (let j = 0; j < roots[i].length; j++)
   1.173 +      roots[i][j].containerOpen = false;
   1.174 +  }
   1.175 +
   1.176 +  root.containerOpen = false;
   1.177 +});
   1.178 +
   1.179 +function checkFirstLevel(index, node, roots) {
   1.180 +    PlacesUtils.asContainer(node).containerOpen = true;
   1.181 +
   1.182 +    do_check_true(PlacesUtils.nodeIsDay(node));
   1.183 +    PlacesUtils.asQuery(node);
   1.184 +    let queries = node.getQueries();
   1.185 +    let options = node.queryOptions;
   1.186 +
   1.187 +    do_check_eq(queries.length, 1);
   1.188 +    let query = queries[0];
   1.189 +
   1.190 +    do_check_true(query.hasBeginTime && query.hasEndTime);
   1.191 +
   1.192 +    // Here we check the second level of results.
   1.193 +    let root = PlacesUtils.history.executeQuery(query, options).root;
   1.194 +    roots.push([]);
   1.195 +    root.containerOpen = true;
   1.196 +
   1.197 +    do_check_eq(root.childCount, leveledTestData[index].length);
   1.198 +    for (var secondIndex = 0; secondIndex < root.childCount; secondIndex++) {
   1.199 +      let child = PlacesUtils.asQuery(root.getChild(secondIndex));
   1.200 +      checkSecondLevel(index, secondIndex, child, roots);
   1.201 +    }
   1.202 +    root.containerOpen = false;
   1.203 +    node.containerOpen = false;
   1.204 +}
   1.205 +
   1.206 +function checkSecondLevel(index, secondIndex, child, roots) {
   1.207 +    let queries = child.getQueries();
   1.208 +    let options = child.queryOptions;
   1.209 +
   1.210 +    do_check_eq(queries.length, 1);
   1.211 +    let query = queries[0];
   1.212 +
   1.213 +    do_check_true(query.hasDomain);
   1.214 +    do_check_true(query.hasBeginTime && query.hasEndTime);
   1.215 +
   1.216 +    let root = PlacesUtils.history.executeQuery(query, options).root;
   1.217 +    // We should now have that roots[index][secondIndex] is set to the second
   1.218 +    // level's results root.
   1.219 +    roots[index].push(root);
   1.220 +
   1.221 +    // We pass off to compareArrayToResult to check the third level of
   1.222 +    // results.
   1.223 +    root.containerOpen = true;
   1.224 +    compareArrayToResult(leveledTestData[index][secondIndex].
   1.225 +                         map(function(x) testData[x]), root);
   1.226 +    // We close |root|'s container later so that we can test live
   1.227 +    // updates into it.
   1.228 +}

mercurial