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: var testData = []; michael@0: var now = Date.now() * 1000; michael@0: function createTestData() { michael@0: function generateVisits(aPage) { michael@0: for (var i = 0; i < aPage.visitCount; i++) { michael@0: testData.push({ isInQuery: aPage.inQuery, michael@0: isVisit: true, michael@0: title: aPage.title, michael@0: uri: aPage.uri, michael@0: lastVisit: now++, michael@0: isTag: aPage.tags && aPage.tags.length > 0, michael@0: tagArray: aPage.tags }); michael@0: } michael@0: } michael@0: michael@0: var pages = [ michael@0: { uri: "http://foo.com/", title: "amo", tags: ["moz"], visitCount: 3, inQuery: true }, michael@0: { uri: "http://moilla.com/", title: "bMoz", tags: ["bugzilla"], visitCount: 5, inQuery: true }, michael@0: { uri: "http://foo.mail.com/changeme1.html", title: "c Moz", visitCount: 7, inQuery: true }, michael@0: { uri: "http://foo.mail.com/changeme2.html", tags: ["moz"], title: "", visitCount: 1, inQuery: false }, michael@0: { uri: "http://foo.mail.com/changeme3.html", title: "zydeco", visitCount: 5, inQuery: false }, michael@0: ]; michael@0: pages.forEach(generateVisits); michael@0: } michael@0: michael@0: /** michael@0: * This test will test Queries that use relative search terms and URI options michael@0: */ michael@0: function run_test() michael@0: { michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_task(function test_results_as_visit() michael@0: { michael@0: createTestData(); michael@0: yield task_populateDB(testData); michael@0: var query = PlacesUtils.history.getNewQuery(); michael@0: query.searchTerms = "moz"; michael@0: query.minVisits = 2; michael@0: michael@0: // Options michael@0: var options = PlacesUtils.history.getNewQueryOptions(); michael@0: options.sortingMode = options.SORT_BY_VISITCOUNT_ASCENDING; michael@0: options.resultType = options.RESULTS_AS_VISIT; michael@0: michael@0: // Results michael@0: var result = PlacesUtils.history.executeQuery(query, options); michael@0: var root = result.root; michael@0: root.containerOpen = true; michael@0: michael@0: LOG("Number of items in result set: " + root.childCount); michael@0: for(var i=0; i < root.childCount; ++i) { michael@0: LOG("result: " + root.getChild(i).uri + " Title: " + root.getChild(i).title); michael@0: } michael@0: michael@0: // Check our inital result set michael@0: compareArrayToResult(testData, root); michael@0: michael@0: // If that passes, check liveupdate michael@0: // Add to the query set michael@0: LOG("Adding item to query") michael@0: var tmp = []; michael@0: for (var i=0; i < 2; i++) { michael@0: tmp.push({ isVisit: true, michael@0: uri: "http://foo.com/added.html", michael@0: title: "ab moz" }); michael@0: } michael@0: yield task_populateDB(tmp); michael@0: for (var i=0; i < 2; i++) michael@0: do_check_eq(root.getChild(i).title, "ab moz"); michael@0: michael@0: // Update an existing URI michael@0: LOG("Updating Item"); michael@0: var change2 = [{ isVisit: true, michael@0: title: "moz", michael@0: uri: "http://foo.mail.com/changeme2.html" }]; michael@0: yield task_populateDB(change2); michael@0: do_check_true(isInResult(change2, root)); michael@0: michael@0: // Update some visits - add one and take one out of query set, and simply michael@0: // change one so that it still applies to the query. michael@0: LOG("Updating More Items"); michael@0: var change3 = [{ isVisit: true, michael@0: lastVisit: now++, michael@0: uri: "http://foo.mail.com/changeme1.html", michael@0: title: "foo"}, michael@0: { isVisit: true, michael@0: lastVisit: now++, michael@0: uri: "http://foo.mail.com/changeme3.html", michael@0: title: "moz", michael@0: isTag: true, michael@0: tagArray: ["foo", "moz"] }]; michael@0: yield task_populateDB(change3); michael@0: do_check_false(isInResult({uri: "http://foo.mail.com/changeme1.html"}, root)); michael@0: do_check_true(isInResult({uri: "http://foo.mail.com/changeme3.html"}, root)); michael@0: michael@0: // And now, delete one michael@0: LOG("Delete item outside of batch"); michael@0: var change4 = [{ isVisit: true, michael@0: lastVisit: now++, michael@0: uri: "http://moilla.com/", michael@0: title: "mo,z" }]; michael@0: yield task_populateDB(change4); michael@0: do_check_false(isInResult(change4, root)); michael@0: michael@0: root.containerOpen = false; michael@0: });