toolkit/components/places/tests/bookmarks/test_keywords.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/bookmarks/test_keywords.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,161 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +function check_bookmark_keyword(aItemId, aKeyword)
     1.8 +{
     1.9 +  let keyword = aKeyword ? aKeyword.toLowerCase() : null;
    1.10 +  do_check_eq(PlacesUtils.bookmarks.getKeywordForBookmark(aItemId),
    1.11 +              keyword);
    1.12 +}
    1.13 +
    1.14 +function check_uri_keyword(aURI, aKeyword)
    1.15 +{
    1.16 +  let keyword = aKeyword ? aKeyword.toLowerCase() : null;
    1.17 +  do_check_eq(PlacesUtils.bookmarks.getKeywordForURI(aURI),
    1.18 +              keyword);
    1.19 +
    1.20 +  if (aKeyword) {
    1.21 +    // This API can't tell which uri the user wants, so it returns a random one.
    1.22 +    let re = /http:\/\/test[0-9]\.mozilla\.org/;
    1.23 +    let url = PlacesUtils.bookmarks.getURIForKeyword(aKeyword).spec;
    1.24 +    do_check_true(re.test(url));
    1.25 +    // Check case insensitivity.
    1.26 +    url = PlacesUtils.bookmarks.getURIForKeyword(aKeyword.toUpperCase()).spec
    1.27 +    do_check_true(re.test(url));
    1.28 +  }
    1.29 +}
    1.30 +
    1.31 +function check_orphans()
    1.32 +{
    1.33 +  stmt = DBConn().createStatement(
    1.34 +    "SELECT id FROM moz_keywords k WHERE NOT EXISTS ("
    1.35 +  +    "SELECT id FROM moz_bookmarks WHERE keyword_id = k.id "
    1.36 +  + ")"
    1.37 +  );
    1.38 +  try {
    1.39 +    do_check_false(stmt.executeStep());
    1.40 +  } finally {
    1.41 +    stmt.finalize();
    1.42 +  }
    1.43 +
    1.44 +  print("Check there are no orphan database entries");
    1.45 +  let stmt = DBConn().createStatement(
    1.46 +    "SELECT b.id FROM moz_bookmarks b "
    1.47 +  + "LEFT JOIN moz_keywords k ON b.keyword_id = k.id "
    1.48 +  + "WHERE keyword_id NOTNULL AND k.id ISNULL"
    1.49 +  );
    1.50 +  try {
    1.51 +    do_check_false(stmt.executeStep());
    1.52 +  } finally {
    1.53 +    stmt.finalize();
    1.54 +  }
    1.55 +}
    1.56 +
    1.57 +const URIS = [
    1.58 +  uri("http://test1.mozilla.org/"),
    1.59 +  uri("http://test2.mozilla.org/"),
    1.60 +];
    1.61 +
    1.62 +add_test(function test_addBookmarkWithKeyword()
    1.63 +{
    1.64 +  check_uri_keyword(URIS[0], null);
    1.65 +
    1.66 +  let itemId =
    1.67 +    PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId,
    1.68 +                                         URIS[0],
    1.69 +                                         PlacesUtils.bookmarks.DEFAULT_INDEX,
    1.70 +                                         "test");
    1.71 +  PlacesUtils.bookmarks.setKeywordForBookmark(itemId, "keyword");
    1.72 +  check_bookmark_keyword(itemId, "keyword");
    1.73 +  check_uri_keyword(URIS[0], "keyword");
    1.74 +
    1.75 +  promiseAsyncUpdates().then(function() {
    1.76 +    check_orphans();
    1.77 +    run_next_test();
    1.78 +  });
    1.79 +});
    1.80 +
    1.81 +add_test(function test_addBookmarkToURIHavingKeyword()
    1.82 +{
    1.83 +  let itemId =
    1.84 +    PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId,
    1.85 +                                         URIS[0],
    1.86 +                                         PlacesUtils.bookmarks.DEFAULT_INDEX,
    1.87 +                                         "test");
    1.88 +  // The uri has a keyword, but this specific bookmark has not.
    1.89 +  check_bookmark_keyword(itemId, null);
    1.90 +  check_uri_keyword(URIS[0], "keyword");
    1.91 +
    1.92 +  promiseAsyncUpdates().then(function() {
    1.93 +    check_orphans();
    1.94 +    run_next_test();
    1.95 +  });
    1.96 +});
    1.97 +
    1.98 +add_test(function test_addSameKeywordToOtherURI()
    1.99 +{
   1.100 +  let itemId =
   1.101 +    PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId,
   1.102 +                                         URIS[1],
   1.103 +                                         PlacesUtils.bookmarks.DEFAULT_INDEX,
   1.104 +                                         "test2");
   1.105 +  check_bookmark_keyword(itemId, null);
   1.106 +  check_uri_keyword(URIS[1], null);
   1.107 +
   1.108 +  PlacesUtils.bookmarks.setKeywordForBookmark(itemId, "kEyWoRd");
   1.109 +  check_bookmark_keyword(itemId, "kEyWoRd");
   1.110 +  check_uri_keyword(URIS[1], "kEyWoRd");
   1.111 +
   1.112 +  // Check case insensitivity.
   1.113 +  check_uri_keyword(URIS[0], "kEyWoRd");
   1.114 +  check_bookmark_keyword(itemId, "keyword");
   1.115 +  check_uri_keyword(URIS[1], "keyword");
   1.116 +  check_uri_keyword(URIS[0], "keyword");
   1.117 +
   1.118 +  promiseAsyncUpdates().then(function() {
   1.119 +    check_orphans();
   1.120 +    run_next_test();
   1.121 +  });
   1.122 +});
   1.123 +
   1.124 +add_test(function test_removeBookmarkWithKeyword()
   1.125 +{
   1.126 +  let itemId =
   1.127 +    PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId,
   1.128 +                                         URIS[1],
   1.129 +                                         PlacesUtils.bookmarks.DEFAULT_INDEX,
   1.130 +                                         "test");
   1.131 +  PlacesUtils.bookmarks.setKeywordForBookmark(itemId, "keyword");
   1.132 +  check_bookmark_keyword(itemId, "keyword");
   1.133 +  check_uri_keyword(URIS[1], "keyword");
   1.134 +
   1.135 +  // The keyword should not be removed from other bookmarks.
   1.136 +  PlacesUtils.bookmarks.removeItem(itemId);
   1.137 +
   1.138 +  check_uri_keyword(URIS[1], "keyword");
   1.139 +  check_uri_keyword(URIS[0], "keyword");
   1.140 +
   1.141 +  promiseAsyncUpdates().then(function() {
   1.142 +    check_orphans();
   1.143 +    run_next_test();
   1.144 +  });
   1.145 +});
   1.146 +
   1.147 +add_test(function test_removeFolderWithKeywordedBookmarks()
   1.148 +{
   1.149 +  // Keyword should be removed as well.
   1.150 +  PlacesUtils.bookmarks.removeFolderChildren(PlacesUtils.unfiledBookmarksFolderId);
   1.151 +
   1.152 +  check_uri_keyword(URIS[1], null);
   1.153 +  check_uri_keyword(URIS[0], null);
   1.154 +
   1.155 +  promiseAsyncUpdates().then(function() {
   1.156 +    check_orphans();
   1.157 +    run_next_test();
   1.158 +  });
   1.159 +});
   1.160 +
   1.161 +function run_test()
   1.162 +{
   1.163 +  run_next_test();
   1.164 +}

mercurial