toolkit/components/places/tests/browser/browser_visituri.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 /**
michael@0 2 * Any copyright is dedicated to the Public Domain.
michael@0 3 * http://creativecommons.org/publicdomain/zero/1.0/
michael@0 4 */
michael@0 5
michael@0 6 gBrowser.selectedTab = gBrowser.addTab();
michael@0 7
michael@0 8 function finishAndCleanUp()
michael@0 9 {
michael@0 10 gBrowser.removeCurrentTab();
michael@0 11 promiseClearHistory().then(finish);
michael@0 12 }
michael@0 13
michael@0 14 /**
michael@0 15 * One-time observer callback.
michael@0 16 */
michael@0 17 function waitForObserve(name, callback)
michael@0 18 {
michael@0 19 var observerService = Cc["@mozilla.org/observer-service;1"]
michael@0 20 .getService(Ci.nsIObserverService);
michael@0 21 var observer = {
michael@0 22 QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
michael@0 23 observe: function(subject, topic, data) {
michael@0 24 observerService.removeObserver(observer, name);
michael@0 25 observer = null;
michael@0 26 callback(subject, topic, data);
michael@0 27 }
michael@0 28 };
michael@0 29
michael@0 30 observerService.addObserver(observer, name, false);
michael@0 31 }
michael@0 32
michael@0 33 /**
michael@0 34 * One-time DOMContentLoaded callback.
michael@0 35 */
michael@0 36 function waitForLoad(callback)
michael@0 37 {
michael@0 38 gBrowser.addEventListener("DOMContentLoaded", function() {
michael@0 39 gBrowser.removeEventListener("DOMContentLoaded", arguments.callee, true);
michael@0 40 callback();
michael@0 41 }, true);
michael@0 42 }
michael@0 43
michael@0 44 var conn = PlacesUtils.history.QueryInterface(Ci.nsPIPlacesDatabase).DBConnection;
michael@0 45
michael@0 46 /**
michael@0 47 * Gets a single column value from either the places or historyvisits table.
michael@0 48 */
michael@0 49 function getColumn(table, column, fromColumnName, fromColumnValue)
michael@0 50 {
michael@0 51 let sql = "SELECT " + column + " " +
michael@0 52 "FROM " + table + " " +
michael@0 53 "WHERE " + fromColumnName + " = :val " +
michael@0 54 "LIMIT 1";
michael@0 55 let stmt = conn.createStatement(sql);
michael@0 56 try {
michael@0 57 stmt.params.val = fromColumnValue;
michael@0 58 ok(stmt.executeStep(), "Expect to get a row");
michael@0 59 return stmt.row[column];
michael@0 60 }
michael@0 61 finally {
michael@0 62 stmt.reset();
michael@0 63 }
michael@0 64 }
michael@0 65
michael@0 66 function test()
michael@0 67 {
michael@0 68 // Make sure places visit chains are saved correctly with a redirect
michael@0 69 // transitions.
michael@0 70
michael@0 71 waitForExplicitFinish();
michael@0 72
michael@0 73 // Part 1: observe history events that fire when a visit occurs.
michael@0 74 // Make sure visits appear in order, and that the visit chain is correct.
michael@0 75 var expectedUrls = [
michael@0 76 "http://example.com/tests/toolkit/components/places/tests/browser/begin.html",
michael@0 77 "http://example.com/tests/toolkit/components/places/tests/browser/redirect_twice.sjs",
michael@0 78 "http://example.com/tests/toolkit/components/places/tests/browser/redirect_once.sjs",
michael@0 79 "http://example.com/tests/toolkit/components/places/tests/browser/final.html"
michael@0 80 ];
michael@0 81 var currentIndex = 0;
michael@0 82
michael@0 83 waitForObserve("uri-visit-saved", function(subject, topic, data) {
michael@0 84 var uri = subject.QueryInterface(Ci.nsIURI);
michael@0 85 var expected = expectedUrls[currentIndex];
michael@0 86 is(uri.spec, expected, "Saved URL visit " + uri.spec);
michael@0 87
michael@0 88 var placeId = getColumn("moz_places", "id", "url", uri.spec);
michael@0 89 var fromVisitId = getColumn("moz_historyvisits", "from_visit", "place_id", placeId);
michael@0 90
michael@0 91 if (currentIndex == 0) {
michael@0 92 is(fromVisitId, 0, "First visit has no from visit");
michael@0 93 }
michael@0 94 else {
michael@0 95 var lastVisitId = getColumn("moz_historyvisits", "place_id", "id", fromVisitId);
michael@0 96 var fromVisitUrl = getColumn("moz_places", "url", "id", lastVisitId);
michael@0 97 is(fromVisitUrl, expectedUrls[currentIndex - 1],
michael@0 98 "From visit was " + expectedUrls[currentIndex - 1]);
michael@0 99 }
michael@0 100
michael@0 101 currentIndex++;
michael@0 102 if (currentIndex < expectedUrls.length) {
michael@0 103 waitForObserve("uri-visit-saved", arguments.callee);
michael@0 104 }
michael@0 105 else {
michael@0 106 finishAndCleanUp();
michael@0 107 }
michael@0 108 });
michael@0 109
michael@0 110 // Load begin page, click link on page to record visits.
michael@0 111 content.location.href = "http://example.com/tests/toolkit/components/places/tests/browser/begin.html";
michael@0 112 waitForLoad(function() {
michael@0 113 EventUtils.sendMouseEvent({type: 'click'}, 'clickme', content.window);
michael@0 114 waitForLoad(function() {
michael@0 115 content.location.href = "about:blank";
michael@0 116 });
michael@0 117 });
michael@0 118 }

mercurial