toolkit/components/places/tests/unit/test_454977.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 // Cache actual visit_count value, filled by add_visit, used by check_results
michael@0 8 let visit_count = 0;
michael@0 9
michael@0 10 // Returns the Place ID corresponding to an added visit.
michael@0 11 function task_add_visit(aURI, aVisitType)
michael@0 12 {
michael@0 13 // Add the visit asynchronously, and save its visit ID.
michael@0 14 let deferUpdatePlaces = Promise.defer();
michael@0 15 PlacesUtils.asyncHistory.updatePlaces({
michael@0 16 uri: aURI,
michael@0 17 visits: [{ transitionType: aVisitType, visitDate: Date.now() * 1000 }]
michael@0 18 }, {
michael@0 19 handleError: function TAV_handleError() {
michael@0 20 deferUpdatePlaces.reject(new Error("Unexpected error in adding visit."));
michael@0 21 },
michael@0 22 handleResult: function (aPlaceInfo) {
michael@0 23 this.visitId = aPlaceInfo.visits[0].visitId;
michael@0 24 },
michael@0 25 handleCompletion: function TAV_handleCompletion() {
michael@0 26 deferUpdatePlaces.resolve(this.visitId);
michael@0 27 }
michael@0 28 });
michael@0 29 let visitId = yield deferUpdatePlaces.promise;
michael@0 30
michael@0 31 // Increase visit_count if applicable
michael@0 32 if (aVisitType != 0 &&
michael@0 33 aVisitType != TRANSITION_EMBED &&
michael@0 34 aVisitType != TRANSITION_FRAMED_LINK &&
michael@0 35 aVisitType != TRANSITION_DOWNLOAD) {
michael@0 36 visit_count ++;
michael@0 37 }
michael@0 38
michael@0 39 // Get the place id
michael@0 40 if (visitId > 0) {
michael@0 41 let sql = "SELECT place_id FROM moz_historyvisits WHERE id = ?1";
michael@0 42 let stmt = DBConn().createStatement(sql);
michael@0 43 stmt.bindByIndex(0, visitId);
michael@0 44 do_check_true(stmt.executeStep());
michael@0 45 let placeId = stmt.getInt64(0);
michael@0 46 stmt.finalize();
michael@0 47 do_check_true(placeId > 0);
michael@0 48 throw new Task.Result(placeId);
michael@0 49 }
michael@0 50 throw new Task.Result(0);
michael@0 51 }
michael@0 52
michael@0 53 /**
michael@0 54 * Checks for results consistency, using visit_count as constraint
michael@0 55 * @param aExpectedCount
michael@0 56 * Number of history results we are expecting (excluded hidden ones)
michael@0 57 * @param aExpectedCountWithHidden
michael@0 58 * Number of history results we are expecting (included hidden ones)
michael@0 59 */
michael@0 60 function check_results(aExpectedCount, aExpectedCountWithHidden)
michael@0 61 {
michael@0 62 let query = PlacesUtils.history.getNewQuery();
michael@0 63 // used to check visit_count
michael@0 64 query.minVisits = visit_count;
michael@0 65 query.maxVisits = visit_count;
michael@0 66 let options = PlacesUtils.history.getNewQueryOptions();
michael@0 67 options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_HISTORY;
michael@0 68 let root = PlacesUtils.history.executeQuery(query, options).root;
michael@0 69 root.containerOpen = true;
michael@0 70 // Children without hidden ones
michael@0 71 do_check_eq(root.childCount, aExpectedCount);
michael@0 72 root.containerOpen = false;
michael@0 73
michael@0 74 // Execute again with includeHidden = true
michael@0 75 // This will ensure visit_count is correct
michael@0 76 options.includeHidden = true;
michael@0 77 root = PlacesUtils.history.executeQuery(query, options).root;
michael@0 78 root.containerOpen = true;
michael@0 79 // Children with hidden ones
michael@0 80 do_check_eq(root.childCount, aExpectedCountWithHidden);
michael@0 81 root.containerOpen = false;
michael@0 82 }
michael@0 83
michael@0 84 // main
michael@0 85 function run_test()
michael@0 86 {
michael@0 87 run_next_test();
michael@0 88 }
michael@0 89
michael@0 90 add_task(function test_execute()
michael@0 91 {
michael@0 92 const TEST_URI = uri("http://test.mozilla.org/");
michael@0 93
michael@0 94 // Add a visit that force hidden
michael@0 95 yield task_add_visit(TEST_URI, TRANSITION_EMBED);
michael@0 96 check_results(0, 0);
michael@0 97
michael@0 98 let placeId = yield task_add_visit(TEST_URI, TRANSITION_FRAMED_LINK);
michael@0 99 check_results(0, 1);
michael@0 100
michael@0 101 // Add a visit that force unhide and check the place id.
michael@0 102 // - We expect that the place gets hidden = 0 while retaining the same
michael@0 103 // place id and a correct visit_count.
michael@0 104 do_check_eq((yield task_add_visit(TEST_URI, TRANSITION_TYPED)), placeId);
michael@0 105 check_results(1, 1);
michael@0 106
michael@0 107 // Add a visit, check that hidden is not overwritten
michael@0 108 // - We expect that the place has still hidden = 0, while retaining
michael@0 109 // correct visit_count.
michael@0 110 yield task_add_visit(TEST_URI, TRANSITION_EMBED);
michael@0 111 check_results(1, 1);
michael@0 112 });

mercurial