toolkit/components/places/tests/unit/test_398914.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_398914.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,150 @@
     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 testFolderId = PlacesUtils.bookmarksMenuFolderId;
    1.12 +
    1.13 +// main
    1.14 +function run_test() {
    1.15 +  var testURI = uri("http://foo.com");
    1.16 +
    1.17 +  /*
    1.18 +  1. Create a bookmark for a URI, with a keyword and post data.
    1.19 +  2. Create a bookmark for the same URI, with a different keyword and different post data.
    1.20 +  3. Confirm that our method for getting a URI+postdata retains bookmark affinity.
    1.21 +  */
    1.22 +  var bm1 = bmsvc.insertBookmark(testFolderId, testURI, -1, "blah");
    1.23 +  bmsvc.setKeywordForBookmark(bm1, "foo");
    1.24 +  PlacesUtils.setPostDataForBookmark(bm1, "pdata1");
    1.25 +  var bm2 = bmsvc.insertBookmark(testFolderId, testURI, -1, "blah");
    1.26 +  bmsvc.setKeywordForBookmark(bm2, "bar");
    1.27 +  PlacesUtils.setPostDataForBookmark(bm2, "pdata2");
    1.28 +
    1.29 +  // check kw, pd for bookmark 1
    1.30 +  var url, postdata;
    1.31 +  [url, postdata] = PlacesUtils.getURLAndPostDataForKeyword("foo");
    1.32 +  do_check_eq(testURI.spec, url);
    1.33 +  do_check_eq(postdata, "pdata1");
    1.34 +
    1.35 +  // check kw, pd for bookmark 2
    1.36 +  [url, postdata] = PlacesUtils.getURLAndPostDataForKeyword("bar");
    1.37 +  do_check_eq(testURI.spec, url);
    1.38 +  do_check_eq(postdata, "pdata2");
    1.39 +
    1.40 +  // cleanup
    1.41 +  bmsvc.removeItem(bm1);
    1.42 +  bmsvc.removeItem(bm2);
    1.43 +
    1.44 +  /*
    1.45 +  1. Create two bookmarks with the same URI and keyword.
    1.46 +  2. Confirm that the most recently created one is returned for that keyword.
    1.47 +  */
    1.48 +  var bm1 = bmsvc.insertBookmark(testFolderId, testURI, -1, "blah");
    1.49 +  bmsvc.setKeywordForBookmark(bm1, "foo");
    1.50 +  PlacesUtils.setPostDataForBookmark(bm1, "pdata1");
    1.51 +  var bm2 = bmsvc.insertBookmark(testFolderId, testURI, -1, "blah");
    1.52 +  bmsvc.setKeywordForBookmark(bm2, "foo");
    1.53 +  PlacesUtils.setPostDataForBookmark(bm2, "pdata2");
    1.54 +
    1.55 +  var bm1da = bmsvc.getItemDateAdded(bm1);
    1.56 +  var bm1lm = bmsvc.getItemLastModified(bm1);
    1.57 +  LOG("bm1 dateAdded: " + bm1da + ", lastModified: " + bm1lm);
    1.58 +  var bm2da = bmsvc.getItemDateAdded(bm2);
    1.59 +  var bm2lm = bmsvc.getItemLastModified(bm2);
    1.60 +  LOG("bm2 dateAdded: " + bm2da + ", lastModified: " + bm2lm);
    1.61 +  do_check_true(bm1da <= bm2da);
    1.62 +  do_check_true(bm1lm <= bm2lm);
    1.63 +
    1.64 +  [url, postdata] = PlacesUtils.getURLAndPostDataForKeyword("foo");
    1.65 +  do_check_eq(testURI.spec, url);
    1.66 +  do_check_eq(postdata, "pdata2");
    1.67 +
    1.68 +  // cleanup
    1.69 +  bmsvc.removeItem(bm1);
    1.70 +  bmsvc.removeItem(bm2);
    1.71 +
    1.72 +  /*
    1.73 +  1. Create two bookmarks with the same URI and keyword.
    1.74 +  2. Modify the first-created bookmark.
    1.75 +  3. Confirm that the most recently modified one is returned for that keyword.
    1.76 +  */
    1.77 +  var bm1 = bmsvc.insertBookmark(testFolderId, testURI, -1, "blah");
    1.78 +  bmsvc.setKeywordForBookmark(bm1, "foo");
    1.79 +  PlacesUtils.setPostDataForBookmark(bm1, "pdata1");
    1.80 +  var bm2 = bmsvc.insertBookmark(testFolderId, testURI, -1, "blah");
    1.81 +  bmsvc.setKeywordForBookmark(bm2, "foo");
    1.82 +  PlacesUtils.setPostDataForBookmark(bm2, "pdata2");
    1.83 +
    1.84 +  // modify the older bookmark
    1.85 +  bmsvc.setItemTitle(bm1, "change");
    1.86 +
    1.87 +  var bm1da = bmsvc.getItemDateAdded(bm1);
    1.88 +  var bm1lm = bmsvc.getItemLastModified(bm1);
    1.89 +  LOG("bm1 dateAdded: " + bm1da + ", lastModified: " + bm1lm);
    1.90 +  var bm2da = bmsvc.getItemDateAdded(bm2);
    1.91 +  var bm2lm = bmsvc.getItemLastModified(bm2);
    1.92 +  LOG("bm2 dateAdded: " + bm2da + ", lastModified: " + bm2lm);
    1.93 +  do_check_true(bm1da <= bm2da);
    1.94 +  // the last modified for bm1 should be at least as big as bm2
    1.95 +  // but could be equal if the test runs faster than our PRNow()
    1.96 +  // granularity
    1.97 +  do_check_true(bm1lm >= bm2lm);
    1.98 +
    1.99 +  // we need to ensure that bm1 last modified date is greater
   1.100 +  // that the modified date of bm2, otherwise in case of a "tie"
   1.101 +  // bm2 will win, as it has a bigger item id
   1.102 +  if (bm1lm == bm2lm) 
   1.103 +    bmsvc.setItemLastModified(bm1, bm2lm + 1);
   1.104 +
   1.105 +  [url, postdata] = PlacesUtils.getURLAndPostDataForKeyword("foo");
   1.106 +  do_check_eq(testURI.spec, url);
   1.107 +  do_check_eq(postdata, "pdata1");
   1.108 +
   1.109 +  // cleanup
   1.110 +  bmsvc.removeItem(bm1);
   1.111 +  bmsvc.removeItem(bm2);
   1.112 +
   1.113 +  /*
   1.114 +  Test that id breaks ties:
   1.115 +  1. Create two bookmarks with the same URI and keyword, dateAdded and lastModified.
   1.116 +  2. Confirm that the most recently created one is returned for that keyword.
   1.117 +  */
   1.118 +  var testDate = Date.now() * 1000;
   1.119 +  var bm1 = bmsvc.insertBookmark(testFolderId, testURI, -1, "blah");
   1.120 +  bmsvc.setKeywordForBookmark(bm1, "foo");
   1.121 +  PlacesUtils.setPostDataForBookmark(bm1, "pdata1");
   1.122 +  bmsvc.setItemDateAdded(bm1, testDate);
   1.123 +  bmsvc.setItemLastModified(bm1, testDate);
   1.124 +
   1.125 +  var bm2 = bmsvc.insertBookmark(testFolderId, testURI, -1, "blah");
   1.126 +  bmsvc.setKeywordForBookmark(bm2, "foo");
   1.127 +  PlacesUtils.setPostDataForBookmark(bm2, "pdata2");
   1.128 +  bmsvc.setItemDateAdded(bm2, testDate);
   1.129 +  bmsvc.setItemLastModified(bm2, testDate);
   1.130 +
   1.131 +  var bm1da = bmsvc.getItemDateAdded(bm1, testDate);
   1.132 +  var bm1lm = bmsvc.getItemLastModified(bm1);
   1.133 +  LOG("bm1 dateAdded: " + bm1da + ", lastModified: " + bm1lm);
   1.134 +  var bm2da = bmsvc.getItemDateAdded(bm2);
   1.135 +  var bm2lm = bmsvc.getItemLastModified(bm2);
   1.136 +  LOG("bm2 dateAdded: " + bm2da + ", lastModified: " + bm2lm);
   1.137 +
   1.138 +  do_check_eq(bm1da, bm2da);
   1.139 +  do_check_eq(bm1lm, bm2lm);
   1.140 +
   1.141 +
   1.142 +  var ids = bmsvc.getBookmarkIdsForURI(testURI);
   1.143 +  do_check_eq(ids[0], bm2);
   1.144 +  do_check_eq(ids[1], bm1);
   1.145 +
   1.146 +  [url, postdata] = PlacesUtils.getURLAndPostDataForKeyword("foo");
   1.147 +  do_check_eq(testURI.spec, url);
   1.148 +  do_check_eq(postdata, "pdata2");
   1.149 +
   1.150 +  // cleanup
   1.151 +  bmsvc.removeItem(bm1);
   1.152 +  bmsvc.removeItem(bm2);
   1.153 +}

mercurial