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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/browser/browser_visituri.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,118 @@
     1.4 +/**
     1.5 + * Any copyright is dedicated to the Public Domain.
     1.6 + * http://creativecommons.org/publicdomain/zero/1.0/
     1.7 + */
     1.8 +
     1.9 +gBrowser.selectedTab = gBrowser.addTab();
    1.10 +
    1.11 +function finishAndCleanUp()
    1.12 +{
    1.13 +  gBrowser.removeCurrentTab();
    1.14 +  promiseClearHistory().then(finish);
    1.15 +}
    1.16 +
    1.17 +/**
    1.18 + * One-time observer callback.
    1.19 + */
    1.20 +function waitForObserve(name, callback)
    1.21 +{
    1.22 +  var observerService = Cc["@mozilla.org/observer-service;1"]
    1.23 +                        .getService(Ci.nsIObserverService);
    1.24 +  var observer = {
    1.25 +    QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
    1.26 +    observe: function(subject, topic, data) {
    1.27 +      observerService.removeObserver(observer, name);
    1.28 +      observer = null;
    1.29 +      callback(subject, topic, data);
    1.30 +    }
    1.31 +  };
    1.32 +
    1.33 +  observerService.addObserver(observer, name, false);
    1.34 +}
    1.35 +
    1.36 +/**
    1.37 + * One-time DOMContentLoaded callback.
    1.38 + */
    1.39 +function waitForLoad(callback)
    1.40 +{
    1.41 +  gBrowser.addEventListener("DOMContentLoaded", function() {
    1.42 +    gBrowser.removeEventListener("DOMContentLoaded", arguments.callee, true);
    1.43 +    callback();
    1.44 +  }, true);
    1.45 +}
    1.46 +
    1.47 +var conn = PlacesUtils.history.QueryInterface(Ci.nsPIPlacesDatabase).DBConnection;
    1.48 +
    1.49 +/**
    1.50 + * Gets a single column value from either the places or historyvisits table.
    1.51 + */
    1.52 +function getColumn(table, column, fromColumnName, fromColumnValue)
    1.53 +{
    1.54 +  let sql = "SELECT " + column + " " +
    1.55 +            "FROM " + table + " " +
    1.56 +            "WHERE " + fromColumnName + " = :val " +
    1.57 +            "LIMIT 1";
    1.58 +  let stmt = conn.createStatement(sql);
    1.59 +  try {
    1.60 +    stmt.params.val = fromColumnValue;
    1.61 +    ok(stmt.executeStep(), "Expect to get a row");
    1.62 +    return stmt.row[column];
    1.63 +  }
    1.64 +  finally {
    1.65 +    stmt.reset();
    1.66 +  }
    1.67 +}
    1.68 +
    1.69 +function test()
    1.70 +{
    1.71 +  // Make sure places visit chains are saved correctly with a redirect
    1.72 +  // transitions.
    1.73 +
    1.74 +  waitForExplicitFinish();
    1.75 +
    1.76 +  // Part 1: observe history events that fire when a visit occurs.
    1.77 +  // Make sure visits appear in order, and that the visit chain is correct.
    1.78 +  var expectedUrls = [
    1.79 +    "http://example.com/tests/toolkit/components/places/tests/browser/begin.html",
    1.80 +    "http://example.com/tests/toolkit/components/places/tests/browser/redirect_twice.sjs",
    1.81 +    "http://example.com/tests/toolkit/components/places/tests/browser/redirect_once.sjs",
    1.82 +    "http://example.com/tests/toolkit/components/places/tests/browser/final.html"
    1.83 +  ];
    1.84 +  var currentIndex = 0;
    1.85 +
    1.86 +  waitForObserve("uri-visit-saved", function(subject, topic, data) {
    1.87 +    var uri = subject.QueryInterface(Ci.nsIURI);
    1.88 +    var expected = expectedUrls[currentIndex];
    1.89 +    is(uri.spec, expected, "Saved URL visit " + uri.spec);
    1.90 +
    1.91 +    var placeId = getColumn("moz_places", "id", "url", uri.spec);
    1.92 +    var fromVisitId = getColumn("moz_historyvisits", "from_visit", "place_id", placeId);
    1.93 +
    1.94 +    if (currentIndex == 0) {
    1.95 +      is(fromVisitId, 0, "First visit has no from visit");
    1.96 +    }
    1.97 +    else {
    1.98 +      var lastVisitId = getColumn("moz_historyvisits", "place_id", "id", fromVisitId);
    1.99 +      var fromVisitUrl = getColumn("moz_places", "url", "id", lastVisitId);
   1.100 +      is(fromVisitUrl, expectedUrls[currentIndex - 1],
   1.101 +         "From visit was " + expectedUrls[currentIndex - 1]);
   1.102 +    }
   1.103 +
   1.104 +    currentIndex++;
   1.105 +    if (currentIndex < expectedUrls.length) {
   1.106 +      waitForObserve("uri-visit-saved", arguments.callee);
   1.107 +    }
   1.108 +    else {
   1.109 +      finishAndCleanUp();
   1.110 +    }
   1.111 +  });
   1.112 +
   1.113 +  // Load begin page, click link on page to record visits.
   1.114 +  content.location.href = "http://example.com/tests/toolkit/components/places/tests/browser/begin.html";
   1.115 +  waitForLoad(function() {
   1.116 +    EventUtils.sendMouseEvent({type: 'click'}, 'clickme', content.window);
   1.117 +    waitForLoad(function() {
   1.118 +      content.location.href = "about:blank";
   1.119 +    });
   1.120 +  });
   1.121 +}

mercurial