toolkit/components/places/tests/unit/test_pageGuid_bookmarkGuid.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_pageGuid_bookmarkGuid.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,180 @@
     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 +function run_test() {
    1.14 +  run_next_test();
    1.15 +}
    1.16 +
    1.17 +add_task(function test_addBookmarksAndCheckGuids() {
    1.18 +  let folder = bmsvc.createFolder(bmsvc.placesRoot, "test folder", bmsvc.DEFAULT_INDEX);
    1.19 +  let b1 = bmsvc.insertBookmark(folder, uri("http://test1.com/"),
    1.20 +                                bmsvc.DEFAULT_INDEX, "1 title");
    1.21 +  let b2 = bmsvc.insertBookmark(folder, uri("http://test2.com/"),
    1.22 +                                bmsvc.DEFAULT_INDEX, "2 title");
    1.23 +  let b3 = bmsvc.insertBookmark(folder, uri("http://test3.com/"),
    1.24 +                                bmsvc.DEFAULT_INDEX, "3 title");
    1.25 +  let s1 = bmsvc.insertSeparator(folder, bmsvc.DEFAULT_INDEX);
    1.26 +  let f1 = bmsvc.createFolder(folder, "test folder 2", bmsvc.DEFAULT_INDEX);
    1.27 +
    1.28 +  let root = PlacesUtils.getFolderContents(folder).root;
    1.29 +  do_check_eq(root.childCount, 5);
    1.30 +
    1.31 +  // check bookmark guids
    1.32 +  let bookmarkGuidZero = root.getChild(0).bookmarkGuid;
    1.33 +  do_check_eq(bookmarkGuidZero.length, 12);
    1.34 +  // bookmarks have bookmark guids
    1.35 +  do_check_eq(root.getChild(1).bookmarkGuid.length, 12);
    1.36 +  do_check_eq(root.getChild(2).bookmarkGuid.length, 12);
    1.37 +  // separator has bookmark guid
    1.38 +  do_check_eq(root.getChild(3).bookmarkGuid.length, 12);
    1.39 +  // folder has bookmark guid
    1.40 +  do_check_eq(root.getChild(4).bookmarkGuid.length, 12);
    1.41 +  // all bookmark guids are different.
    1.42 +  do_check_neq(bookmarkGuidZero, root.getChild(1).bookmarkGuid);
    1.43 +  do_check_neq(root.getChild(1).bookmarkGuid, root.getChild(2).bookmarkGuid);
    1.44 +  do_check_neq(root.getChild(2).bookmarkGuid, root.getChild(3).bookmarkGuid);
    1.45 +  do_check_neq(root.getChild(3).bookmarkGuid, root.getChild(4).bookmarkGuid);
    1.46 +
    1.47 +  // check page guids
    1.48 +  let pageGuidZero = root.getChild(0).pageGuid;
    1.49 +  do_check_eq(pageGuidZero.length, 12);
    1.50 +  // bookmarks have page guids
    1.51 +  do_check_eq(root.getChild(1).pageGuid.length, 12);
    1.52 +  do_check_eq(root.getChild(2).pageGuid.length, 12);
    1.53 +  // folder and separator don't have page guids
    1.54 +  do_check_eq(root.getChild(3).pageGuid, "");
    1.55 +  do_check_eq(root.getChild(4).pageGuid, "");
    1.56 +
    1.57 +  do_check_neq(pageGuidZero, root.getChild(1).pageGuid);
    1.58 +  do_check_neq(root.getChild(1).pageGuid, root.getChild(2).pageGuid);
    1.59 +
    1.60 +  root.containerOpen = false;
    1.61 +
    1.62 +  remove_all_bookmarks();
    1.63 +});
    1.64 +
    1.65 +add_task(function test_updateBookmarksAndCheckGuids() {
    1.66 +  let folder = bmsvc.createFolder(bmsvc.placesRoot, "test folder", bmsvc.DEFAULT_INDEX);
    1.67 +  let b1 = bmsvc.insertBookmark(folder, uri("http://test1.com/"),
    1.68 +                                bmsvc.DEFAULT_INDEX, "1 title");
    1.69 +  let f1 = bmsvc.createFolder(folder, "test folder 2", bmsvc.DEFAULT_INDEX);
    1.70 +
    1.71 +  let root = PlacesUtils.getFolderContents(folder).root;
    1.72 +  do_check_eq(root.childCount, 2);
    1.73 +
    1.74 +  // ensure the bookmark and page guids remain the same after modifing other property.
    1.75 +  let bookmarkGuidZero = root.getChild(0).bookmarkGuid;
    1.76 +  let pageGuidZero = root.getChild(0).pageGuid;
    1.77 +  bmsvc.setItemTitle(b1, "1 title mod");
    1.78 +  do_check_eq(root.getChild(0).title, "1 title mod");
    1.79 +  do_check_eq(root.getChild(0).bookmarkGuid, bookmarkGuidZero);
    1.80 +  do_check_eq(root.getChild(0).pageGuid, pageGuidZero);
    1.81 +
    1.82 +  let bookmarkGuidOne = root.getChild(1).bookmarkGuid;
    1.83 +  let pageGuidOne = root.getChild(1).pageGuid;
    1.84 +  bmsvc.setItemTitle(f1, "test foolder 234");
    1.85 +  do_check_eq(root.getChild(1).title, "test foolder 234");
    1.86 +  do_check_eq(root.getChild(1).bookmarkGuid, bookmarkGuidOne);
    1.87 +  do_check_eq(root.getChild(1).pageGuid, pageGuidOne);
    1.88 +
    1.89 +  root.containerOpen = false;
    1.90 +
    1.91 +  remove_all_bookmarks();
    1.92 +});
    1.93 +
    1.94 +add_task(function test_addVisitAndCheckGuid() {
    1.95 +  // add a visit and test page guid and non-existing bookmark guids.
    1.96 +  let now = Date.now() * 1000;
    1.97 +  let sourceURI = uri("http://test4.com/");
    1.98 +  yield promiseAddVisits({ uri: sourceURI });
    1.99 +  do_check_eq(bmsvc.getBookmarkedURIFor(sourceURI), null);
   1.100 +
   1.101 +  let options = histsvc.getNewQueryOptions();
   1.102 +  let query = histsvc.getNewQuery();
   1.103 +  query.uri = sourceURI;
   1.104 +  let root = histsvc.executeQuery(query, options).root;
   1.105 +  root.containerOpen = true;
   1.106 +  do_check_eq(root.childCount, 1);
   1.107 +
   1.108 +  do_check_valid_places_guid(root.getChild(0).pageGuid);
   1.109 +  do_check_eq(root.getChild(0).bookmarkGuid, "");
   1.110 +  root.containerOpen = false;
   1.111 +
   1.112 +  yield promiseClearHistory();
   1.113 +});
   1.114 +
   1.115 +add_task(function test_addItemsWithInvalidGUIDsFails() {
   1.116 +  const INVALID_GUID = "XYZ";
   1.117 +  try {
   1.118 +    bmsvc.createFolder(bmsvc.placesRoot, "XYZ folder",
   1.119 +                       bmsvc.DEFAULT_INDEX, INVALID_GUID);
   1.120 +    do_throw("Adding a folder with an invalid guid should fail");
   1.121 +  }
   1.122 +  catch(ex) { }
   1.123 +
   1.124 +  let folder = bmsvc.createFolder(bmsvc.placesRoot, "test folder",
   1.125 +                                  bmsvc.DEFAULT_INDEX);
   1.126 +  try {
   1.127 +    bmsvc.insertBookmark(folder, uri("http://test.tld"), bmsvc.DEFAULT_INDEX,
   1.128 +                         "title", INVALID_GUID);
   1.129 +    do_throw("Adding a bookmark with an invalid guid should fail");
   1.130 +  }
   1.131 +  catch(ex) { }
   1.132 +
   1.133 +  try {
   1.134 +    bmsvc.insertSeparator(folder, bmsvc.DEFAULT_INDEX, INVALID_GUID);
   1.135 +    do_throw("Adding a separator with an invalid guid should fail");
   1.136 +  }
   1.137 +  catch(ex) { }
   1.138 +
   1.139 +  remove_all_bookmarks();
   1.140 +});
   1.141 +
   1.142 +add_task(function test_addItemsWithGUIDs() {
   1.143 +  const FOLDER_GUID     = "FOLDER--GUID";
   1.144 +  const BOOKMARK_GUID   = "BM------GUID";
   1.145 +  const SEPARATOR_GUID  = "SEP-----GUID";
   1.146 +
   1.147 +  let folder = bmsvc.createFolder(bmsvc.placesRoot, "test folder",
   1.148 +                                  bmsvc.DEFAULT_INDEX, FOLDER_GUID);
   1.149 +  bmsvc.insertBookmark(folder, uri("http://test1.com/"), bmsvc.DEFAULT_INDEX,
   1.150 +                       "1 title", BOOKMARK_GUID);
   1.151 +  bmsvc.insertSeparator(folder, bmsvc.DEFAULT_INDEX, SEPARATOR_GUID);
   1.152 +
   1.153 +  let root = PlacesUtils.getFolderContents(folder).root;
   1.154 +  do_check_eq(root.childCount, 2);
   1.155 +  do_check_eq(root.bookmarkGuid, FOLDER_GUID);
   1.156 +  do_check_eq(root.getChild(0).bookmarkGuid, BOOKMARK_GUID);
   1.157 +  do_check_eq(root.getChild(1).bookmarkGuid, SEPARATOR_GUID);
   1.158 +
   1.159 +  root.containerOpen = false;
   1.160 +  remove_all_bookmarks();
   1.161 +});
   1.162 +
   1.163 +add_task(function test_emptyGUIDIgnored() {
   1.164 +  let folder = bmsvc.createFolder(bmsvc.placesRoot, "test folder",
   1.165 +                                  bmsvc.DEFAULT_INDEX, "");
   1.166 +  do_check_valid_places_guid(PlacesUtils.getFolderContents(folder)
   1.167 +                                        .root.bookmarkGuid);
   1.168 +  remove_all_bookmarks();
   1.169 +});
   1.170 +
   1.171 +add_task(function test_usingSameGUIDFails() {
   1.172 +  const GUID = "XYZXYZXYZXYZ";
   1.173 +  bmsvc.createFolder(bmsvc.placesRoot, "test folder",
   1.174 +                     bmsvc.DEFAULT_INDEX, GUID);
   1.175 +  try {
   1.176 +    bmsvc.createFolder(bmsvc.placesRoot, "test folder 2",
   1.177 +                       bmsvc.DEFAULT_INDEX, GUID);
   1.178 +    do_throw("Using the same guid twice should fail");
   1.179 +  }
   1.180 +  catch(ex) { }
   1.181 +
   1.182 +  remove_all_bookmarks();
   1.183 +});

mercurial