michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const bmsvc = PlacesUtils.bookmarks; michael@0: const testFolderId = PlacesUtils.bookmarksMenuFolderId; michael@0: michael@0: // main michael@0: function run_test() { michael@0: var testURI = uri("http://foo.com"); michael@0: michael@0: /* michael@0: 1. Create a bookmark for a URI, with a keyword and post data. michael@0: 2. Create a bookmark for the same URI, with a different keyword and different post data. michael@0: 3. Confirm that our method for getting a URI+postdata retains bookmark affinity. michael@0: */ michael@0: var bm1 = bmsvc.insertBookmark(testFolderId, testURI, -1, "blah"); michael@0: bmsvc.setKeywordForBookmark(bm1, "foo"); michael@0: PlacesUtils.setPostDataForBookmark(bm1, "pdata1"); michael@0: var bm2 = bmsvc.insertBookmark(testFolderId, testURI, -1, "blah"); michael@0: bmsvc.setKeywordForBookmark(bm2, "bar"); michael@0: PlacesUtils.setPostDataForBookmark(bm2, "pdata2"); michael@0: michael@0: // check kw, pd for bookmark 1 michael@0: var url, postdata; michael@0: [url, postdata] = PlacesUtils.getURLAndPostDataForKeyword("foo"); michael@0: do_check_eq(testURI.spec, url); michael@0: do_check_eq(postdata, "pdata1"); michael@0: michael@0: // check kw, pd for bookmark 2 michael@0: [url, postdata] = PlacesUtils.getURLAndPostDataForKeyword("bar"); michael@0: do_check_eq(testURI.spec, url); michael@0: do_check_eq(postdata, "pdata2"); michael@0: michael@0: // cleanup michael@0: bmsvc.removeItem(bm1); michael@0: bmsvc.removeItem(bm2); michael@0: michael@0: /* michael@0: 1. Create two bookmarks with the same URI and keyword. michael@0: 2. Confirm that the most recently created one is returned for that keyword. michael@0: */ michael@0: var bm1 = bmsvc.insertBookmark(testFolderId, testURI, -1, "blah"); michael@0: bmsvc.setKeywordForBookmark(bm1, "foo"); michael@0: PlacesUtils.setPostDataForBookmark(bm1, "pdata1"); michael@0: var bm2 = bmsvc.insertBookmark(testFolderId, testURI, -1, "blah"); michael@0: bmsvc.setKeywordForBookmark(bm2, "foo"); michael@0: PlacesUtils.setPostDataForBookmark(bm2, "pdata2"); michael@0: michael@0: var bm1da = bmsvc.getItemDateAdded(bm1); michael@0: var bm1lm = bmsvc.getItemLastModified(bm1); michael@0: LOG("bm1 dateAdded: " + bm1da + ", lastModified: " + bm1lm); michael@0: var bm2da = bmsvc.getItemDateAdded(bm2); michael@0: var bm2lm = bmsvc.getItemLastModified(bm2); michael@0: LOG("bm2 dateAdded: " + bm2da + ", lastModified: " + bm2lm); michael@0: do_check_true(bm1da <= bm2da); michael@0: do_check_true(bm1lm <= bm2lm); michael@0: michael@0: [url, postdata] = PlacesUtils.getURLAndPostDataForKeyword("foo"); michael@0: do_check_eq(testURI.spec, url); michael@0: do_check_eq(postdata, "pdata2"); michael@0: michael@0: // cleanup michael@0: bmsvc.removeItem(bm1); michael@0: bmsvc.removeItem(bm2); michael@0: michael@0: /* michael@0: 1. Create two bookmarks with the same URI and keyword. michael@0: 2. Modify the first-created bookmark. michael@0: 3. Confirm that the most recently modified one is returned for that keyword. michael@0: */ michael@0: var bm1 = bmsvc.insertBookmark(testFolderId, testURI, -1, "blah"); michael@0: bmsvc.setKeywordForBookmark(bm1, "foo"); michael@0: PlacesUtils.setPostDataForBookmark(bm1, "pdata1"); michael@0: var bm2 = bmsvc.insertBookmark(testFolderId, testURI, -1, "blah"); michael@0: bmsvc.setKeywordForBookmark(bm2, "foo"); michael@0: PlacesUtils.setPostDataForBookmark(bm2, "pdata2"); michael@0: michael@0: // modify the older bookmark michael@0: bmsvc.setItemTitle(bm1, "change"); michael@0: michael@0: var bm1da = bmsvc.getItemDateAdded(bm1); michael@0: var bm1lm = bmsvc.getItemLastModified(bm1); michael@0: LOG("bm1 dateAdded: " + bm1da + ", lastModified: " + bm1lm); michael@0: var bm2da = bmsvc.getItemDateAdded(bm2); michael@0: var bm2lm = bmsvc.getItemLastModified(bm2); michael@0: LOG("bm2 dateAdded: " + bm2da + ", lastModified: " + bm2lm); michael@0: do_check_true(bm1da <= bm2da); michael@0: // the last modified for bm1 should be at least as big as bm2 michael@0: // but could be equal if the test runs faster than our PRNow() michael@0: // granularity michael@0: do_check_true(bm1lm >= bm2lm); michael@0: michael@0: // we need to ensure that bm1 last modified date is greater michael@0: // that the modified date of bm2, otherwise in case of a "tie" michael@0: // bm2 will win, as it has a bigger item id michael@0: if (bm1lm == bm2lm) michael@0: bmsvc.setItemLastModified(bm1, bm2lm + 1); michael@0: michael@0: [url, postdata] = PlacesUtils.getURLAndPostDataForKeyword("foo"); michael@0: do_check_eq(testURI.spec, url); michael@0: do_check_eq(postdata, "pdata1"); michael@0: michael@0: // cleanup michael@0: bmsvc.removeItem(bm1); michael@0: bmsvc.removeItem(bm2); michael@0: michael@0: /* michael@0: Test that id breaks ties: michael@0: 1. Create two bookmarks with the same URI and keyword, dateAdded and lastModified. michael@0: 2. Confirm that the most recently created one is returned for that keyword. michael@0: */ michael@0: var testDate = Date.now() * 1000; michael@0: var bm1 = bmsvc.insertBookmark(testFolderId, testURI, -1, "blah"); michael@0: bmsvc.setKeywordForBookmark(bm1, "foo"); michael@0: PlacesUtils.setPostDataForBookmark(bm1, "pdata1"); michael@0: bmsvc.setItemDateAdded(bm1, testDate); michael@0: bmsvc.setItemLastModified(bm1, testDate); michael@0: michael@0: var bm2 = bmsvc.insertBookmark(testFolderId, testURI, -1, "blah"); michael@0: bmsvc.setKeywordForBookmark(bm2, "foo"); michael@0: PlacesUtils.setPostDataForBookmark(bm2, "pdata2"); michael@0: bmsvc.setItemDateAdded(bm2, testDate); michael@0: bmsvc.setItemLastModified(bm2, testDate); michael@0: michael@0: var bm1da = bmsvc.getItemDateAdded(bm1, testDate); michael@0: var bm1lm = bmsvc.getItemLastModified(bm1); michael@0: LOG("bm1 dateAdded: " + bm1da + ", lastModified: " + bm1lm); michael@0: var bm2da = bmsvc.getItemDateAdded(bm2); michael@0: var bm2lm = bmsvc.getItemLastModified(bm2); michael@0: LOG("bm2 dateAdded: " + bm2da + ", lastModified: " + bm2lm); michael@0: michael@0: do_check_eq(bm1da, bm2da); michael@0: do_check_eq(bm1lm, bm2lm); michael@0: michael@0: michael@0: var ids = bmsvc.getBookmarkIdsForURI(testURI); michael@0: do_check_eq(ids[0], bm2); michael@0: do_check_eq(ids[1], bm1); michael@0: michael@0: [url, postdata] = PlacesUtils.getURLAndPostDataForKeyword("foo"); michael@0: do_check_eq(testURI.spec, url); michael@0: do_check_eq(postdata, "pdata2"); michael@0: michael@0: // cleanup michael@0: bmsvc.removeItem(bm1); michael@0: bmsvc.removeItem(bm2); michael@0: }