toolkit/components/places/tests/browser/browser_visituri.js

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

mercurial