test_398914.js
Go to the documentation of this file.
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et: */
3 /* ***** BEGIN LICENSE BLOCK *****
4  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is mozilla.com code.
17  *
18  * The Initial Developer of the Original Code is Mozilla Corp.
19  * Portions created by the Initial Developer are Copyright (C) 2007
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  * Dietrich Ayala <dietrich@mozilla.com>
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38 
39 Components.utils.import("resource://gre/modules/utils.js");
40 
41 const bmsvc = PlacesUtils.bookmarks;
42 const testFolderId = PlacesUtils.bookmarksMenuFolderId;
43 
44 // main
45 function run_test() {
46  var testURI = uri("http://foo.com");
47 
48  /*
49  1. Create a bookmark for a URI, with a keyword and post data.
50  2. Create a bookmark for the same URI, with a different keyword and different post data.
51  3. Confirm that our method for getting a URI+postdata retains bookmark affinity.
52  */
53  var bm1 = bmsvc.insertBookmark(testFolderId, testURI, -1, "blah");
54  bmsvc.setKeywordForBookmark(bm1, "foo");
55  PlacesUtils.setPostDataForBookmark(bm1, "pdata1");
56  var bm2 = bmsvc.insertBookmark(testFolderId, testURI, -1, "blah");
57  bmsvc.setKeywordForBookmark(bm2, "bar");
58  PlacesUtils.setPostDataForBookmark(bm2, "pdata2");
59 
60  // check kw, pd for bookmark 1
61  var url, postdata;
62  [url, postdata] = PlacesUtils.getURLAndPostDataForKeyword("foo");
63  do_check_eq(testURI.spec, url);
64  do_check_eq(postdata, "pdata1");
65 
66  // check kw, pd for bookmark 2
67  [url, postdata] = PlacesUtils.getURLAndPostDataForKeyword("bar");
68  do_check_eq(testURI.spec, url);
69  do_check_eq(postdata, "pdata2");
70 
71  // cleanup
72  bmsvc.removeItem(bm1);
73  bmsvc.removeItem(bm2);
74 
75  /*
76  1. Create two bookmarks with the same URI and keyword.
77  2. Confirm that the most recently created one is returned for that keyword.
78  */
79  var bm1 = bmsvc.insertBookmark(testFolderId, testURI, -1, "blah");
80  bmsvc.setKeywordForBookmark(bm1, "foo");
81  PlacesUtils.setPostDataForBookmark(bm1, "pdata1");
82  var bm2 = bmsvc.insertBookmark(testFolderId, testURI, -1, "blah");
83  bmsvc.setKeywordForBookmark(bm2, "foo");
84  PlacesUtils.setPostDataForBookmark(bm2, "pdata2");
85 
86  var bm1da = bmsvc.getItemDateAdded(bm1);
87  var bm1lm = bmsvc.getItemLastModified(bm1);
88  LOG("bm1 dateAdded: " + bm1da + ", lastModified: " + bm1lm);
89  var bm2da = bmsvc.getItemDateAdded(bm2);
90  var bm2lm = bmsvc.getItemLastModified(bm2);
91  LOG("bm2 dateAdded: " + bm2da + ", lastModified: " + bm2lm);
92  do_check_true(bm1da <= bm2da);
93  do_check_true(bm1lm <= bm2lm);
94 
95  [url, postdata] = PlacesUtils.getURLAndPostDataForKeyword("foo");
96  do_check_eq(testURI.spec, url);
97  do_check_eq(postdata, "pdata2");
98 
99  // cleanup
100  bmsvc.removeItem(bm1);
101  bmsvc.removeItem(bm2);
102 
103  /*
104  1. Create two bookmarks with the same URI and keyword.
105  2. Modify the first-created bookmark.
106  3. Confirm that the most recently modified one is returned for that keyword.
107  */
108  var bm1 = bmsvc.insertBookmark(testFolderId, testURI, -1, "blah");
109  bmsvc.setKeywordForBookmark(bm1, "foo");
110  PlacesUtils.setPostDataForBookmark(bm1, "pdata1");
111  var bm2 = bmsvc.insertBookmark(testFolderId, testURI, -1, "blah");
112  bmsvc.setKeywordForBookmark(bm2, "foo");
113  PlacesUtils.setPostDataForBookmark(bm2, "pdata2");
114 
115  // modify the older bookmark
116  bmsvc.setItemTitle(bm1, "change");
117 
118  var bm1da = bmsvc.getItemDateAdded(bm1);
119  var bm1lm = bmsvc.getItemLastModified(bm1);
120  LOG("bm1 dateAdded: " + bm1da + ", lastModified: " + bm1lm);
121  var bm2da = bmsvc.getItemDateAdded(bm2);
122  var bm2lm = bmsvc.getItemLastModified(bm2);
123  LOG("bm2 dateAdded: " + bm2da + ", lastModified: " + bm2lm);
124  do_check_true(bm1da <= bm2da);
125  // the last modified for bm1 should be at least as big as bm2
126  // but could be equal if the test runs faster than our PRNow()
127  // granularity
128  do_check_true(bm1lm >= bm2lm);
129 
130  // we need to ensure that bm1 last modified date is greater
131  // that the modified date of bm2, otherwise in case of a "tie"
132  // bm2 will win, as it has a bigger item id
133  if (bm1lm == bm2lm)
134  bmsvc.setItemLastModified(bm1, bm2lm + 1);
135 
136  [url, postdata] = PlacesUtils.getURLAndPostDataForKeyword("foo");
137  do_check_eq(testURI.spec, url);
138  do_check_eq(postdata, "pdata1");
139 
140  // cleanup
141  bmsvc.removeItem(bm1);
142  bmsvc.removeItem(bm2);
143 
144  /*
145  Test that id breaks ties:
146  1. Create two bookmarks with the same URI and keyword, dateAdded and lastModified.
147  2. Confirm that the most recently created one is returned for that keyword.
148  */
149  var testDate = Date.now() * 1000;
150  var bm1 = bmsvc.insertBookmark(testFolderId, testURI, -1, "blah");
151  bmsvc.setKeywordForBookmark(bm1, "foo");
152  PlacesUtils.setPostDataForBookmark(bm1, "pdata1");
153  bmsvc.setItemDateAdded(bm1, testDate);
154  bmsvc.setItemLastModified(bm1, testDate);
155 
156  var bm2 = bmsvc.insertBookmark(testFolderId, testURI, -1, "blah");
157  bmsvc.setKeywordForBookmark(bm2, "foo");
158  PlacesUtils.setPostDataForBookmark(bm2, "pdata2");
159  bmsvc.setItemDateAdded(bm2, testDate);
160  bmsvc.setItemLastModified(bm2, testDate);
161 
162  var bm1da = bmsvc.getItemDateAdded(bm1, testDate);
163  var bm1lm = bmsvc.getItemLastModified(bm1);
164  LOG("bm1 dateAdded: " + bm1da + ", lastModified: " + bm1lm);
165  var bm2da = bmsvc.getItemDateAdded(bm2);
166  var bm2lm = bmsvc.getItemLastModified(bm2);
167  LOG("bm2 dateAdded: " + bm2da + ", lastModified: " + bm2lm);
168 
169  do_check_eq(bm1da, bm2da);
170  do_check_eq(bm1lm, bm2lm);
171 
172 
173  var ids = bmsvc.getBookmarkIdsForURI(testURI, {});
174  do_check_eq(ids[0], bm2);
175  do_check_eq(ids[1], bm1);
176 
177  [url, postdata] = PlacesUtils.getURLAndPostDataForKeyword("foo");
178  do_check_eq(testURI.spec, url);
179  do_check_eq(postdata, "pdata2");
180 
181  // cleanup
182  bmsvc.removeItem(bm1);
183  bmsvc.removeItem(bm2);
184 }
do_check_eq(typeof PlacesUtils,"object")
const bmsvc
Definition: test_398914.js:41
#define LOG(args)
const testFolderId
Definition: test_398914.js:42
var postdata
function run_test()
Definition: test_398914.js:45
function url(spec)
var uri
Definition: FeedWriter.js:1135