docshell/test/unit/test_nsIDownloadHistory.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/docshell/test/unit/test_nsIDownloadHistory.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,68 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const NS_DOWNLOADHISTORY_CID = "{2ee83680-2af0-4bcb-bfa0-c9705f6554f1}";
     1.9 +
    1.10 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
    1.11 +
    1.12 +XPCOMUtils.defineLazyGetter(this, "Services", function() {
    1.13 +  Components.utils.import("resource://gre/modules/Services.jsm");
    1.14 +  return Services;
    1.15 +});
    1.16 +
    1.17 +function testLinkVistedObserver()
    1.18 +{
    1.19 +  const NS_LINK_VISITED_EVENT_TOPIC = "link-visited";
    1.20 +  var ios = Cc["@mozilla.org/network/io-service;1"].
    1.21 +            getService(Ci.nsIIOService);
    1.22 +  var testURI = ios.newURI("http://google.com/", null, null);
    1.23 +
    1.24 +  var gh = Cc["@mozilla.org/browser/global-history;2"].
    1.25 +           getService(Ci.nsIGlobalHistory2);
    1.26 +  do_check_false(gh.isVisited(testURI));
    1.27 +
    1.28 +  var topicReceived = false;
    1.29 +  var obs = {
    1.30 +    observe: function tlvo_observe(aSubject, aTopic, aData)
    1.31 +    {
    1.32 +      if (NS_LINK_VISITED_EVENT_TOPIC == aTopic) {
    1.33 +        do_check_eq(testURI, aSubject);
    1.34 +        topicReceived = true;
    1.35 +      }
    1.36 +    }
    1.37 +  };
    1.38 +
    1.39 +  var os = Cc["@mozilla.org/observer-service;1"].
    1.40 +           getService(Ci.nsIObserverService);
    1.41 +  os.addObserver(obs, NS_LINK_VISITED_EVENT_TOPIC, false);
    1.42 +
    1.43 +  var dh = Components.classesByID[NS_DOWNLOADHISTORY_CID].
    1.44 +           getService(Ci.nsIDownloadHistory);
    1.45 +  dh.addDownload(testURI);
    1.46 +  do_check_true(topicReceived);
    1.47 +  do_check_true(gh.isVisited(testURI));
    1.48 +}
    1.49 +
    1.50 +var tests = [testLinkVistedObserver];
    1.51 +
    1.52 +function run_test()
    1.53 +{
    1.54 +  // Not everyone uses/defines an nsGlobalHistory* service. Especially if
    1.55 +  // MOZ_PLACES is not defined. If getService fails, then abort gracefully.
    1.56 +  try {
    1.57 +    Cc["@mozilla.org/browser/global-history;2"].
    1.58 +      getService(Ci.nsIGlobalHistory2);
    1.59 +  }
    1.60 +  catch (ex) {
    1.61 +    return;
    1.62 +  }
    1.63 +
    1.64 +  // Needed to properly setup and shutdown the profile.
    1.65 +  do_get_profile();
    1.66 +
    1.67 +  for (var i = 0; i < tests.length; i++)
    1.68 +    tests[i]();
    1.69 +
    1.70 +  cleanup();
    1.71 +}

mercurial