toolkit/components/places/tests/unit/test_removeVisitsByTimeframe.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_removeVisitsByTimeframe.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,389 @@
     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 +const bmsvc = PlacesUtils.bookmarks;
    1.11 +const histsvc = PlacesUtils.history;
    1.12 +
    1.13 +const NOW = Date.now() * 1000;
    1.14 +const TEST_URL = "http://example.com/";
    1.15 +const TEST_URI = uri(TEST_URL);
    1.16 +const PLACE_URL = "place:queryType=0&sort=8&maxResults=10";
    1.17 +const PLACE_URI = uri(PLACE_URL);
    1.18 +
    1.19 +var tests = [
    1.20 +  {
    1.21 +    desc: "Remove some visits outside valid timeframe from an unbookmarked URI",
    1.22 +    run:   function () {
    1.23 +      print("Add 10 visits for the URI from way in the past.");
    1.24 +      let visits = [];
    1.25 +      for (let i = 0; i < 10; i++) {
    1.26 +        visits.push({ uri: TEST_URI, visitDate: NOW - 1000 - i });
    1.27 +      }
    1.28 +      promiseAddVisits(visits).then(this.continue_run.bind(this));
    1.29 +    },
    1.30 +    continue_run: function () {
    1.31 +      print("Remove visits using timerange outside the URI's visits.");
    1.32 +      histsvc.QueryInterface(Ci.nsIBrowserHistory).
    1.33 +        removeVisitsByTimeframe(NOW - 10, NOW);
    1.34 +
    1.35 +      print("URI should still exist in moz_places.");
    1.36 +      do_check_true(page_in_database(TEST_URL));
    1.37 +
    1.38 +      print("Run a history query and check that all visits still exist.");
    1.39 +      var query = histsvc.getNewQuery();
    1.40 +      var opts = histsvc.getNewQueryOptions();
    1.41 +      opts.resultType = opts.RESULTS_AS_VISIT;
    1.42 +      opts.sortingMode = opts.SORT_BY_DATE_DESCENDING;
    1.43 +      var resultRoot = histsvc.executeQuery(query, opts).root;
    1.44 +      resultRoot.containerOpen = true;
    1.45 +      do_check_eq(resultRoot.childCount, 10);
    1.46 +      for (let i = 0; i < resultRoot.childCount; i++) {
    1.47 +        var visitTime = resultRoot.getChild(i).time;
    1.48 +        do_check_eq(visitTime, NOW - 1000 - i);
    1.49 +      }
    1.50 +      resultRoot.containerOpen = false;
    1.51 +
    1.52 +      print("asyncHistory.isURIVisited should return true.");
    1.53 +      PlacesUtils.asyncHistory.isURIVisited(TEST_URI, function(aURI, aIsVisited) {
    1.54 +        do_check_true(aIsVisited);
    1.55 +
    1.56 +        promiseAsyncUpdates().then(function () {
    1.57 +          print("Frecency should be positive.")
    1.58 +          do_check_true(frecencyForUrl(TEST_URI) > 0);
    1.59 +          run_next_test();
    1.60 +        });
    1.61 +      });
    1.62 +    }
    1.63 +  },
    1.64 +
    1.65 +  {
    1.66 +    desc: "Remove some visits outside valid timeframe from a bookmarked URI",
    1.67 +    run:   function () {
    1.68 +      print("Add 10 visits for the URI from way in the past.");
    1.69 +      let visits = [];
    1.70 +      for (let i = 0; i < 10; i++) {
    1.71 +        visits.push({ uri: TEST_URI, visitDate: NOW - 1000 - i });
    1.72 +      }
    1.73 +      promiseAddVisits(visits).then(function () {
    1.74 +        print("Bookmark the URI.");
    1.75 +        bmsvc.insertBookmark(bmsvc.unfiledBookmarksFolder,
    1.76 +                             TEST_URI,
    1.77 +                             bmsvc.DEFAULT_INDEX,
    1.78 +                             "bookmark title");
    1.79 +
    1.80 +        promiseAsyncUpdates().then(this.continue_run.bind(this));
    1.81 +      }.bind(this));
    1.82 +    },
    1.83 +    continue_run: function () {
    1.84 +      print("Remove visits using timerange outside the URI's visits.");
    1.85 +      histsvc.QueryInterface(Ci.nsIBrowserHistory).
    1.86 +        removeVisitsByTimeframe(NOW - 10, NOW);
    1.87 +
    1.88 +      print("URI should still exist in moz_places.");
    1.89 +      do_check_true(page_in_database(TEST_URL));
    1.90 +
    1.91 +      print("Run a history query and check that all visits still exist.");
    1.92 +      var query = histsvc.getNewQuery();
    1.93 +      var opts = histsvc.getNewQueryOptions();
    1.94 +      opts.resultType = opts.RESULTS_AS_VISIT;
    1.95 +      opts.sortingMode = opts.SORT_BY_DATE_DESCENDING;
    1.96 +      var resultRoot = histsvc.executeQuery(query, opts).root;
    1.97 +      resultRoot.containerOpen = true;
    1.98 +      do_check_eq(resultRoot.childCount, 10);
    1.99 +      for (let i = 0; i < resultRoot.childCount; i++) {
   1.100 +        var visitTime = resultRoot.getChild(i).time;
   1.101 +        do_check_eq(visitTime, NOW - 1000 - i);
   1.102 +      }
   1.103 +      resultRoot.containerOpen = false;
   1.104 +
   1.105 +      print("asyncHistory.isURIVisited should return true.");
   1.106 +      PlacesUtils.asyncHistory.isURIVisited(TEST_URI, function(aURI, aIsVisited) {
   1.107 +        do_check_true(aIsVisited);
   1.108 +
   1.109 +        promiseAsyncUpdates().then(function () {
   1.110 +          print("Frecency should be positive.")
   1.111 +          do_check_true(frecencyForUrl(TEST_URI) > 0);
   1.112 +          run_next_test();
   1.113 +        });
   1.114 +      });
   1.115 +    }
   1.116 +  },
   1.117 +
   1.118 +  {
   1.119 +    desc: "Remove some visits from an unbookmarked URI",
   1.120 +    run:   function () {
   1.121 +      print("Add 10 visits for the URI from now to 9 usecs in the past.");
   1.122 +      let visits = [];
   1.123 +      for (let i = 0; i < 10; i++) {
   1.124 +        visits.push({ uri: TEST_URI, visitDate: NOW - i });
   1.125 +      }
   1.126 +      promiseAddVisits(visits).then(this.continue_run.bind(this));
   1.127 +    },
   1.128 +    continue_run: function () {
   1.129 +      print("Remove the 5 most recent visits.");
   1.130 +      histsvc.QueryInterface(Ci.nsIBrowserHistory).
   1.131 +        removeVisitsByTimeframe(NOW - 4, NOW);
   1.132 +
   1.133 +      print("URI should still exist in moz_places.");
   1.134 +      do_check_true(page_in_database(TEST_URL));
   1.135 +
   1.136 +      print("Run a history query and check that only the older 5 visits " +
   1.137 +            "still exist.");
   1.138 +      var query = histsvc.getNewQuery();
   1.139 +      var opts = histsvc.getNewQueryOptions();
   1.140 +      opts.resultType = opts.RESULTS_AS_VISIT;
   1.141 +      opts.sortingMode = opts.SORT_BY_DATE_DESCENDING;
   1.142 +      var resultRoot = histsvc.executeQuery(query, opts).root;
   1.143 +      resultRoot.containerOpen = true;
   1.144 +      do_check_eq(resultRoot.childCount, 5);
   1.145 +      for (let i = 0; i < resultRoot.childCount; i++) {
   1.146 +        var visitTime = resultRoot.getChild(i).time;
   1.147 +        do_check_eq(visitTime, NOW - i - 5);
   1.148 +      }
   1.149 +      resultRoot.containerOpen = false;
   1.150 +
   1.151 +      print("asyncHistory.isURIVisited should return true.");
   1.152 +      PlacesUtils.asyncHistory.isURIVisited(TEST_URI, function(aURI, aIsVisited) {
   1.153 +        do_check_true(aIsVisited);
   1.154 +
   1.155 +        promiseAsyncUpdates().then(function () {
   1.156 +          print("Frecency should be positive.")
   1.157 +          do_check_true(frecencyForUrl(TEST_URI) > 0);
   1.158 +          run_next_test();
   1.159 +        });
   1.160 +      });
   1.161 +    }
   1.162 +  },
   1.163 +
   1.164 +  {
   1.165 +    desc: "Remove some visits from a bookmarked URI",
   1.166 +    run:   function () {
   1.167 +      print("Add 10 visits for the URI from now to 9 usecs in the past.");
   1.168 +      let visits = [];
   1.169 +      for (let i = 0; i < 10; i++) {
   1.170 +        visits.push({ uri: TEST_URI, visitDate: NOW - i });
   1.171 +      }
   1.172 +      promiseAddVisits(visits).then(function () {
   1.173 +        print("Bookmark the URI.");
   1.174 +        bmsvc.insertBookmark(bmsvc.unfiledBookmarksFolder,
   1.175 +                             TEST_URI,
   1.176 +                             bmsvc.DEFAULT_INDEX,
   1.177 +                             "bookmark title");
   1.178 +        promiseAsyncUpdates().then(this.continue_run.bind(this));
   1.179 +      }.bind(this));
   1.180 +    },
   1.181 +    continue_run: function () {
   1.182 +      print("Remove the 5 most recent visits.");
   1.183 +      histsvc.QueryInterface(Ci.nsIBrowserHistory).
   1.184 +        removeVisitsByTimeframe(NOW - 4, NOW);
   1.185 +
   1.186 +      print("URI should still exist in moz_places.");
   1.187 +      do_check_true(page_in_database(TEST_URL));
   1.188 +
   1.189 +      print("Run a history query and check that only the older 5 visits " +
   1.190 +            "still exist.");
   1.191 +      var query = histsvc.getNewQuery();
   1.192 +      var opts = histsvc.getNewQueryOptions();
   1.193 +      opts.resultType = opts.RESULTS_AS_VISIT;
   1.194 +      opts.sortingMode = opts.SORT_BY_DATE_DESCENDING;
   1.195 +      var resultRoot = histsvc.executeQuery(query, opts).root;
   1.196 +      resultRoot.containerOpen = true;
   1.197 +      do_check_eq(resultRoot.childCount, 5);
   1.198 +      for (let i = 0; i < resultRoot.childCount; i++) {
   1.199 +        var visitTime = resultRoot.getChild(i).time;
   1.200 +        do_check_eq(visitTime, NOW - i - 5);
   1.201 +      }
   1.202 +      resultRoot.containerOpen = false;
   1.203 +
   1.204 +      print("asyncHistory.isURIVisited should return true.");
   1.205 +      PlacesUtils.asyncHistory.isURIVisited(TEST_URI, function(aURI, aIsVisited) {
   1.206 +        do_check_true(aIsVisited);
   1.207 +
   1.208 +        promiseAsyncUpdates().then(function () {
   1.209 +          print("Frecency should be positive.")
   1.210 +          do_check_true(frecencyForUrl(TEST_URI) > 0);
   1.211 +          run_next_test();
   1.212 +        });
   1.213 +      });
   1.214 +    }
   1.215 +  },
   1.216 +
   1.217 +  {
   1.218 +    desc: "Remove all visits from an unbookmarked URI",
   1.219 +    run:   function () {
   1.220 +      print("Add some visits for the URI.");
   1.221 +      let visits = [];
   1.222 +      for (let i = 0; i < 10; i++) {
   1.223 +        visits.push({ uri: TEST_URI, visitDate: NOW - i });
   1.224 +      }
   1.225 +      promiseAddVisits(visits).then(this.continue_run.bind(this));
   1.226 +    },
   1.227 +    continue_run: function () {
   1.228 +      print("Remove all visits.");
   1.229 +      histsvc.QueryInterface(Ci.nsIBrowserHistory).
   1.230 +        removeVisitsByTimeframe(NOW - 10, NOW);
   1.231 +
   1.232 +      print("URI should no longer exist in moz_places.");
   1.233 +      do_check_false(page_in_database(TEST_URL));
   1.234 +
   1.235 +      print("Run a history query and check that no visits exist.");
   1.236 +      var query = histsvc.getNewQuery();
   1.237 +      var opts = histsvc.getNewQueryOptions();
   1.238 +      opts.resultType = opts.RESULTS_AS_VISIT;
   1.239 +      opts.sortingMode = opts.SORT_BY_DATE_DESCENDING;
   1.240 +      var resultRoot = histsvc.executeQuery(query, opts).root;
   1.241 +      resultRoot.containerOpen = true;
   1.242 +      do_check_eq(resultRoot.childCount, 0);
   1.243 +      resultRoot.containerOpen = false;
   1.244 +
   1.245 +      print("asyncHistory.isURIVisited should return false.");
   1.246 +      PlacesUtils.asyncHistory.isURIVisited(TEST_URI, function(aURI, aIsVisited) {
   1.247 +        do_check_false(aIsVisited);
   1.248 +        run_next_test();
   1.249 +      });
   1.250 +    }
   1.251 +  },
   1.252 +
   1.253 +  {
   1.254 +    desc: "Remove all visits from an unbookmarked place: URI",
   1.255 +    run:   function () {
   1.256 +      print("Add some visits for the URI.");
   1.257 +      let visits = [];
   1.258 +      for (let i = 0; i < 10; i++) {
   1.259 +        visits.push({ uri: PLACE_URI, visitDate: NOW - i });
   1.260 +      }
   1.261 +      promiseAddVisits(visits).then(this.continue_run.bind(this));
   1.262 +    },
   1.263 +    continue_run: function () {
   1.264 +      print("Remove all visits.");
   1.265 +      histsvc.QueryInterface(Ci.nsIBrowserHistory).
   1.266 +        removeVisitsByTimeframe(NOW - 10, NOW);
   1.267 +
   1.268 +      print("URI should still exist in moz_places.");
   1.269 +      do_check_true(page_in_database(PLACE_URL));
   1.270 +
   1.271 +      print("Run a history query and check that no visits exist.");
   1.272 +      var query = histsvc.getNewQuery();
   1.273 +      var opts = histsvc.getNewQueryOptions();
   1.274 +      opts.resultType = opts.RESULTS_AS_VISIT;
   1.275 +      opts.sortingMode = opts.SORT_BY_DATE_DESCENDING;
   1.276 +      var resultRoot = histsvc.executeQuery(query, opts).root;
   1.277 +      resultRoot.containerOpen = true;
   1.278 +      do_check_eq(resultRoot.childCount, 0);
   1.279 +      resultRoot.containerOpen = false;
   1.280 +
   1.281 +      print("asyncHistory.isURIVisited should return false.");
   1.282 +      PlacesUtils.asyncHistory.isURIVisited(PLACE_URI, function(aURI, aIsVisited) {
   1.283 +        do_check_false(aIsVisited);
   1.284 +
   1.285 +        promiseAsyncUpdates().then(function () {
   1.286 +          print("Frecency should be zero.")
   1.287 +          do_check_eq(frecencyForUrl(PLACE_URL), 0);
   1.288 +          run_next_test();
   1.289 +        });
   1.290 +      });
   1.291 +    }
   1.292 +  },
   1.293 +
   1.294 +  {
   1.295 +    desc: "Remove all visits from a bookmarked URI",
   1.296 +    run:   function () {
   1.297 +      print("Add some visits for the URI.");
   1.298 +      let visits = [];
   1.299 +      for (let i = 0; i < 10; i++) {
   1.300 +        visits.push({ uri: TEST_URI, visitDate: NOW - i });
   1.301 +      }
   1.302 +      promiseAddVisits(visits).then(function () {
   1.303 +        print("Bookmark the URI.");
   1.304 +        bmsvc.insertBookmark(bmsvc.unfiledBookmarksFolder,
   1.305 +                             TEST_URI,
   1.306 +                             bmsvc.DEFAULT_INDEX,
   1.307 +                             "bookmark title");
   1.308 +        promiseAsyncUpdates().then(this.continue_run.bind(this));
   1.309 +      }.bind(this));
   1.310 +    },
   1.311 +    continue_run: function () {
   1.312 +      print("Remove all visits.");
   1.313 +      histsvc.QueryInterface(Ci.nsIBrowserHistory).
   1.314 +        removeVisitsByTimeframe(NOW - 10, NOW);
   1.315 +
   1.316 +      print("URI should still exist in moz_places.");
   1.317 +      do_check_true(page_in_database(TEST_URL));
   1.318 +
   1.319 +      print("Run a history query and check that no visits exist.");
   1.320 +      var query = histsvc.getNewQuery();
   1.321 +      var opts = histsvc.getNewQueryOptions();
   1.322 +      opts.resultType = opts.RESULTS_AS_VISIT;
   1.323 +      opts.sortingMode = opts.SORT_BY_DATE_DESCENDING;
   1.324 +      var resultRoot = histsvc.executeQuery(query, opts).root;
   1.325 +      resultRoot.containerOpen = true;
   1.326 +      do_check_eq(resultRoot.childCount, 0);
   1.327 +      resultRoot.containerOpen = false;
   1.328 +
   1.329 +      print("asyncHistory.isURIVisited should return false.");
   1.330 +      PlacesUtils.asyncHistory.isURIVisited(TEST_URI, function(aURI, aIsVisited) {
   1.331 +        do_check_false(aIsVisited);
   1.332 +
   1.333 +        print("nsINavBookmarksService.isBookmarked should return true.");
   1.334 +        do_check_true(bmsvc.isBookmarked(TEST_URI));
   1.335 +
   1.336 +        promiseAsyncUpdates().then(function () {
   1.337 +          print("Frecency should be negative.")
   1.338 +          do_check_true(frecencyForUrl(TEST_URI) < 0);
   1.339 +          run_next_test();
   1.340 +        });
   1.341 +      });
   1.342 +    }
   1.343 +  },
   1.344 +
   1.345 +  {
   1.346 +    desc: "Remove some visits from a zero frecency URI retains zero frecency",
   1.347 +    run: function () {
   1.348 +      do_log_info("Add some visits for the URI.");
   1.349 +      promiseAddVisits([{ uri: TEST_URI, transition: TRANSITION_FRAMED_LINK,
   1.350 +                          visitDate: (NOW - 86400000000) },
   1.351 +                        { uri: TEST_URI, transition: TRANSITION_FRAMED_LINK,
   1.352 +                          visitDate: NOW }]).then(
   1.353 +                       this.continue_run.bind(this));
   1.354 +    },
   1.355 +    continue_run: function () {
   1.356 +      do_log_info("Remove newer visit.");
   1.357 +      histsvc.QueryInterface(Ci.nsIBrowserHistory).
   1.358 +        removeVisitsByTimeframe(NOW - 10, NOW);
   1.359 +
   1.360 +      promiseAsyncUpdates().then(function() {
   1.361 +        do_log_info("URI should still exist in moz_places.");
   1.362 +        do_check_true(page_in_database(TEST_URL));
   1.363 +        do_log_info("Frecency should be zero.")
   1.364 +        do_check_eq(frecencyForUrl(TEST_URI), 0);
   1.365 +        run_next_test();
   1.366 +      });
   1.367 +    }
   1.368 +  }
   1.369 +];
   1.370 +
   1.371 +///////////////////////////////////////////////////////////////////////////////
   1.372 +
   1.373 +function run_test()
   1.374 +{
   1.375 +  do_test_pending();
   1.376 +  run_next_test();
   1.377 +}
   1.378 +
   1.379 +function run_next_test() {
   1.380 +  if (tests.length) {
   1.381 +    let test = tests.shift();
   1.382 +    print("\n ***Test: " + test.desc);
   1.383 +    promiseClearHistory().then(function() {
   1.384 +      remove_all_bookmarks();
   1.385 +      DBConn().executeSimpleSQL("DELETE FROM moz_places");
   1.386 +      test.run.call(test);
   1.387 +    });
   1.388 +  }
   1.389 +  else {
   1.390 +    do_test_finished();
   1.391 +  }
   1.392 +}

mercurial