1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/places/tests/unit/test_454977.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,112 @@ 1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +// Cache actual visit_count value, filled by add_visit, used by check_results 1.11 +let visit_count = 0; 1.12 + 1.13 +// Returns the Place ID corresponding to an added visit. 1.14 +function task_add_visit(aURI, aVisitType) 1.15 +{ 1.16 + // Add the visit asynchronously, and save its visit ID. 1.17 + let deferUpdatePlaces = Promise.defer(); 1.18 + PlacesUtils.asyncHistory.updatePlaces({ 1.19 + uri: aURI, 1.20 + visits: [{ transitionType: aVisitType, visitDate: Date.now() * 1000 }] 1.21 + }, { 1.22 + handleError: function TAV_handleError() { 1.23 + deferUpdatePlaces.reject(new Error("Unexpected error in adding visit.")); 1.24 + }, 1.25 + handleResult: function (aPlaceInfo) { 1.26 + this.visitId = aPlaceInfo.visits[0].visitId; 1.27 + }, 1.28 + handleCompletion: function TAV_handleCompletion() { 1.29 + deferUpdatePlaces.resolve(this.visitId); 1.30 + } 1.31 + }); 1.32 + let visitId = yield deferUpdatePlaces.promise; 1.33 + 1.34 + // Increase visit_count if applicable 1.35 + if (aVisitType != 0 && 1.36 + aVisitType != TRANSITION_EMBED && 1.37 + aVisitType != TRANSITION_FRAMED_LINK && 1.38 + aVisitType != TRANSITION_DOWNLOAD) { 1.39 + visit_count ++; 1.40 + } 1.41 + 1.42 + // Get the place id 1.43 + if (visitId > 0) { 1.44 + let sql = "SELECT place_id FROM moz_historyvisits WHERE id = ?1"; 1.45 + let stmt = DBConn().createStatement(sql); 1.46 + stmt.bindByIndex(0, visitId); 1.47 + do_check_true(stmt.executeStep()); 1.48 + let placeId = stmt.getInt64(0); 1.49 + stmt.finalize(); 1.50 + do_check_true(placeId > 0); 1.51 + throw new Task.Result(placeId); 1.52 + } 1.53 + throw new Task.Result(0); 1.54 +} 1.55 + 1.56 +/** 1.57 + * Checks for results consistency, using visit_count as constraint 1.58 + * @param aExpectedCount 1.59 + * Number of history results we are expecting (excluded hidden ones) 1.60 + * @param aExpectedCountWithHidden 1.61 + * Number of history results we are expecting (included hidden ones) 1.62 + */ 1.63 +function check_results(aExpectedCount, aExpectedCountWithHidden) 1.64 +{ 1.65 + let query = PlacesUtils.history.getNewQuery(); 1.66 + // used to check visit_count 1.67 + query.minVisits = visit_count; 1.68 + query.maxVisits = visit_count; 1.69 + let options = PlacesUtils.history.getNewQueryOptions(); 1.70 + options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_HISTORY; 1.71 + let root = PlacesUtils.history.executeQuery(query, options).root; 1.72 + root.containerOpen = true; 1.73 + // Children without hidden ones 1.74 + do_check_eq(root.childCount, aExpectedCount); 1.75 + root.containerOpen = false; 1.76 + 1.77 + // Execute again with includeHidden = true 1.78 + // This will ensure visit_count is correct 1.79 + options.includeHidden = true; 1.80 + root = PlacesUtils.history.executeQuery(query, options).root; 1.81 + root.containerOpen = true; 1.82 + // Children with hidden ones 1.83 + do_check_eq(root.childCount, aExpectedCountWithHidden); 1.84 + root.containerOpen = false; 1.85 +} 1.86 + 1.87 +// main 1.88 +function run_test() 1.89 +{ 1.90 + run_next_test(); 1.91 +} 1.92 + 1.93 +add_task(function test_execute() 1.94 +{ 1.95 + const TEST_URI = uri("http://test.mozilla.org/"); 1.96 + 1.97 + // Add a visit that force hidden 1.98 + yield task_add_visit(TEST_URI, TRANSITION_EMBED); 1.99 + check_results(0, 0); 1.100 + 1.101 + let placeId = yield task_add_visit(TEST_URI, TRANSITION_FRAMED_LINK); 1.102 + check_results(0, 1); 1.103 + 1.104 + // Add a visit that force unhide and check the place id. 1.105 + // - We expect that the place gets hidden = 0 while retaining the same 1.106 + // place id and a correct visit_count. 1.107 + do_check_eq((yield task_add_visit(TEST_URI, TRANSITION_TYPED)), placeId); 1.108 + check_results(1, 1); 1.109 + 1.110 + // Add a visit, check that hidden is not overwritten 1.111 + // - We expect that the place has still hidden = 0, while retaining 1.112 + // correct visit_count. 1.113 + yield task_add_visit(TEST_URI, TRANSITION_EMBED); 1.114 + check_results(1, 1); 1.115 +});