|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 function check_bookmark_keyword(aItemId, aKeyword) |
|
5 { |
|
6 let keyword = aKeyword ? aKeyword.toLowerCase() : null; |
|
7 do_check_eq(PlacesUtils.bookmarks.getKeywordForBookmark(aItemId), |
|
8 keyword); |
|
9 } |
|
10 |
|
11 function check_uri_keyword(aURI, aKeyword) |
|
12 { |
|
13 let keyword = aKeyword ? aKeyword.toLowerCase() : null; |
|
14 do_check_eq(PlacesUtils.bookmarks.getKeywordForURI(aURI), |
|
15 keyword); |
|
16 |
|
17 if (aKeyword) { |
|
18 // This API can't tell which uri the user wants, so it returns a random one. |
|
19 let re = /http:\/\/test[0-9]\.mozilla\.org/; |
|
20 let url = PlacesUtils.bookmarks.getURIForKeyword(aKeyword).spec; |
|
21 do_check_true(re.test(url)); |
|
22 // Check case insensitivity. |
|
23 url = PlacesUtils.bookmarks.getURIForKeyword(aKeyword.toUpperCase()).spec |
|
24 do_check_true(re.test(url)); |
|
25 } |
|
26 } |
|
27 |
|
28 function check_orphans() |
|
29 { |
|
30 stmt = DBConn().createStatement( |
|
31 "SELECT id FROM moz_keywords k WHERE NOT EXISTS (" |
|
32 + "SELECT id FROM moz_bookmarks WHERE keyword_id = k.id " |
|
33 + ")" |
|
34 ); |
|
35 try { |
|
36 do_check_false(stmt.executeStep()); |
|
37 } finally { |
|
38 stmt.finalize(); |
|
39 } |
|
40 |
|
41 print("Check there are no orphan database entries"); |
|
42 let stmt = DBConn().createStatement( |
|
43 "SELECT b.id FROM moz_bookmarks b " |
|
44 + "LEFT JOIN moz_keywords k ON b.keyword_id = k.id " |
|
45 + "WHERE keyword_id NOTNULL AND k.id ISNULL" |
|
46 ); |
|
47 try { |
|
48 do_check_false(stmt.executeStep()); |
|
49 } finally { |
|
50 stmt.finalize(); |
|
51 } |
|
52 } |
|
53 |
|
54 const URIS = [ |
|
55 uri("http://test1.mozilla.org/"), |
|
56 uri("http://test2.mozilla.org/"), |
|
57 ]; |
|
58 |
|
59 add_test(function test_addBookmarkWithKeyword() |
|
60 { |
|
61 check_uri_keyword(URIS[0], null); |
|
62 |
|
63 let itemId = |
|
64 PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId, |
|
65 URIS[0], |
|
66 PlacesUtils.bookmarks.DEFAULT_INDEX, |
|
67 "test"); |
|
68 PlacesUtils.bookmarks.setKeywordForBookmark(itemId, "keyword"); |
|
69 check_bookmark_keyword(itemId, "keyword"); |
|
70 check_uri_keyword(URIS[0], "keyword"); |
|
71 |
|
72 promiseAsyncUpdates().then(function() { |
|
73 check_orphans(); |
|
74 run_next_test(); |
|
75 }); |
|
76 }); |
|
77 |
|
78 add_test(function test_addBookmarkToURIHavingKeyword() |
|
79 { |
|
80 let itemId = |
|
81 PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId, |
|
82 URIS[0], |
|
83 PlacesUtils.bookmarks.DEFAULT_INDEX, |
|
84 "test"); |
|
85 // The uri has a keyword, but this specific bookmark has not. |
|
86 check_bookmark_keyword(itemId, null); |
|
87 check_uri_keyword(URIS[0], "keyword"); |
|
88 |
|
89 promiseAsyncUpdates().then(function() { |
|
90 check_orphans(); |
|
91 run_next_test(); |
|
92 }); |
|
93 }); |
|
94 |
|
95 add_test(function test_addSameKeywordToOtherURI() |
|
96 { |
|
97 let itemId = |
|
98 PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId, |
|
99 URIS[1], |
|
100 PlacesUtils.bookmarks.DEFAULT_INDEX, |
|
101 "test2"); |
|
102 check_bookmark_keyword(itemId, null); |
|
103 check_uri_keyword(URIS[1], null); |
|
104 |
|
105 PlacesUtils.bookmarks.setKeywordForBookmark(itemId, "kEyWoRd"); |
|
106 check_bookmark_keyword(itemId, "kEyWoRd"); |
|
107 check_uri_keyword(URIS[1], "kEyWoRd"); |
|
108 |
|
109 // Check case insensitivity. |
|
110 check_uri_keyword(URIS[0], "kEyWoRd"); |
|
111 check_bookmark_keyword(itemId, "keyword"); |
|
112 check_uri_keyword(URIS[1], "keyword"); |
|
113 check_uri_keyword(URIS[0], "keyword"); |
|
114 |
|
115 promiseAsyncUpdates().then(function() { |
|
116 check_orphans(); |
|
117 run_next_test(); |
|
118 }); |
|
119 }); |
|
120 |
|
121 add_test(function test_removeBookmarkWithKeyword() |
|
122 { |
|
123 let itemId = |
|
124 PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId, |
|
125 URIS[1], |
|
126 PlacesUtils.bookmarks.DEFAULT_INDEX, |
|
127 "test"); |
|
128 PlacesUtils.bookmarks.setKeywordForBookmark(itemId, "keyword"); |
|
129 check_bookmark_keyword(itemId, "keyword"); |
|
130 check_uri_keyword(URIS[1], "keyword"); |
|
131 |
|
132 // The keyword should not be removed from other bookmarks. |
|
133 PlacesUtils.bookmarks.removeItem(itemId); |
|
134 |
|
135 check_uri_keyword(URIS[1], "keyword"); |
|
136 check_uri_keyword(URIS[0], "keyword"); |
|
137 |
|
138 promiseAsyncUpdates().then(function() { |
|
139 check_orphans(); |
|
140 run_next_test(); |
|
141 }); |
|
142 }); |
|
143 |
|
144 add_test(function test_removeFolderWithKeywordedBookmarks() |
|
145 { |
|
146 // Keyword should be removed as well. |
|
147 PlacesUtils.bookmarks.removeFolderChildren(PlacesUtils.unfiledBookmarksFolderId); |
|
148 |
|
149 check_uri_keyword(URIS[1], null); |
|
150 check_uri_keyword(URIS[0], null); |
|
151 |
|
152 promiseAsyncUpdates().then(function() { |
|
153 check_orphans(); |
|
154 run_next_test(); |
|
155 }); |
|
156 }); |
|
157 |
|
158 function run_test() |
|
159 { |
|
160 run_next_test(); |
|
161 } |