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: // Cache actual visit_count value, filled by add_visit, used by check_results michael@0: let visit_count = 0; michael@0: michael@0: // Returns the Place ID corresponding to an added visit. michael@0: function task_add_visit(aURI, aVisitType) michael@0: { michael@0: // Add the visit asynchronously, and save its visit ID. michael@0: let deferUpdatePlaces = Promise.defer(); michael@0: PlacesUtils.asyncHistory.updatePlaces({ michael@0: uri: aURI, michael@0: visits: [{ transitionType: aVisitType, visitDate: Date.now() * 1000 }] michael@0: }, { michael@0: handleError: function TAV_handleError() { michael@0: deferUpdatePlaces.reject(new Error("Unexpected error in adding visit.")); michael@0: }, michael@0: handleResult: function (aPlaceInfo) { michael@0: this.visitId = aPlaceInfo.visits[0].visitId; michael@0: }, michael@0: handleCompletion: function TAV_handleCompletion() { michael@0: deferUpdatePlaces.resolve(this.visitId); michael@0: } michael@0: }); michael@0: let visitId = yield deferUpdatePlaces.promise; michael@0: michael@0: // Increase visit_count if applicable michael@0: if (aVisitType != 0 && michael@0: aVisitType != TRANSITION_EMBED && michael@0: aVisitType != TRANSITION_FRAMED_LINK && michael@0: aVisitType != TRANSITION_DOWNLOAD) { michael@0: visit_count ++; michael@0: } michael@0: michael@0: // Get the place id michael@0: if (visitId > 0) { michael@0: let sql = "SELECT place_id FROM moz_historyvisits WHERE id = ?1"; michael@0: let stmt = DBConn().createStatement(sql); michael@0: stmt.bindByIndex(0, visitId); michael@0: do_check_true(stmt.executeStep()); michael@0: let placeId = stmt.getInt64(0); michael@0: stmt.finalize(); michael@0: do_check_true(placeId > 0); michael@0: throw new Task.Result(placeId); michael@0: } michael@0: throw new Task.Result(0); michael@0: } michael@0: michael@0: /** michael@0: * Checks for results consistency, using visit_count as constraint michael@0: * @param aExpectedCount michael@0: * Number of history results we are expecting (excluded hidden ones) michael@0: * @param aExpectedCountWithHidden michael@0: * Number of history results we are expecting (included hidden ones) michael@0: */ michael@0: function check_results(aExpectedCount, aExpectedCountWithHidden) michael@0: { michael@0: let query = PlacesUtils.history.getNewQuery(); michael@0: // used to check visit_count michael@0: query.minVisits = visit_count; michael@0: query.maxVisits = visit_count; michael@0: let options = PlacesUtils.history.getNewQueryOptions(); michael@0: options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_HISTORY; michael@0: let root = PlacesUtils.history.executeQuery(query, options).root; michael@0: root.containerOpen = true; michael@0: // Children without hidden ones michael@0: do_check_eq(root.childCount, aExpectedCount); michael@0: root.containerOpen = false; michael@0: michael@0: // Execute again with includeHidden = true michael@0: // This will ensure visit_count is correct michael@0: options.includeHidden = true; michael@0: root = PlacesUtils.history.executeQuery(query, options).root; michael@0: root.containerOpen = true; michael@0: // Children with hidden ones michael@0: do_check_eq(root.childCount, aExpectedCountWithHidden); michael@0: root.containerOpen = false; michael@0: } michael@0: michael@0: // main michael@0: function run_test() michael@0: { michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_task(function test_execute() michael@0: { michael@0: const TEST_URI = uri("http://test.mozilla.org/"); michael@0: michael@0: // Add a visit that force hidden michael@0: yield task_add_visit(TEST_URI, TRANSITION_EMBED); michael@0: check_results(0, 0); michael@0: michael@0: let placeId = yield task_add_visit(TEST_URI, TRANSITION_FRAMED_LINK); michael@0: check_results(0, 1); michael@0: michael@0: // Add a visit that force unhide and check the place id. michael@0: // - We expect that the place gets hidden = 0 while retaining the same michael@0: // place id and a correct visit_count. michael@0: do_check_eq((yield task_add_visit(TEST_URI, TRANSITION_TYPED)), placeId); michael@0: check_results(1, 1); michael@0: michael@0: // Add a visit, check that hidden is not overwritten michael@0: // - We expect that the place has still hidden = 0, while retaining michael@0: // correct visit_count. michael@0: yield task_add_visit(TEST_URI, TRANSITION_EMBED); michael@0: check_results(1, 1); michael@0: });