2 * https://bugzilla.mozilla.org/show_bug.cgi?id=420605
6 waitForExplicitFinish();
8 var pageurl = "http://mochi.test:8888/browser/docshell/test/browser/file_bug420605.html";
9 var fragmenturl = "http://mochi.test:8888/browser/docshell/test/browser/file_bug420605.html#firefox";
11 var historyService = Cc["@mozilla.org/browser/nav-history-service;1"]
12 .getService(Ci.nsINavHistoryService);
14 /* Queries nsINavHistoryService and returns a single history entry
16 function getNavHistoryEntry(aURI) {
17 var options = historyService.getNewQueryOptions();
18 options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_HISTORY;
19 options.maxResults = 1;
21 var query = historyService.getNewQuery();
24 var result = historyService.executeQuery(query, options);
25 result.root.containerOpen = true;
27 if (!result.root.childCount) {
30 return result.root.getChild(0);
33 // We'll save the favicon URL of the orignal page here and check that the
34 // page with a hash has the same favicon.
37 // Control flow in this test is a bit complicated.
39 // When the page loads, onPageLoad (the DOMContentLoaded handler) and
40 // historyObserver::onPageChanged are both called, in some order. Once
41 // they've both run, we click a fragment link in the content page
42 // (clickLinkIfReady), which should trigger another onPageChanged event,
43 // this time for the fragment's URL.
45 var _clickLinkTimes = 0;
46 function clickLinkIfReady() {
48 if (_clickLinkTimes == 2) {
49 EventUtils.sendMouseEvent({type:'click'}, 'firefox-link',
50 gBrowser.selectedBrowser.contentWindow);
54 /* Global history observer that triggers for the two test URLs above. */
55 var historyObserver = {
56 onBeginUpdateBatch: function() {},
57 onEndUpdateBatch: function() {},
58 onVisit: function(aURI, aVisitID, aTime, aSessionId, aReferringId,
59 aTransitionType, _added) {},
60 onTitleChanged: function(aURI, aPageTitle) {},
61 onDeleteURI: function(aURI) {},
62 onClearHistory: function() {},
63 onPageChanged: function(aURI, aWhat, aValue) {
64 if (aWhat != Ci.nsINavHistoryObserver.ATTRIBUTE_FAVICON) {
70 ok(aValue, "Favicon value is not null for page without fragment.");
71 originalFavicon = aValue;
73 // Now that the favicon has loaded, click on fragment link.
74 // This should trigger the |case fragmenturl| below.
79 // If the fragment URL's favicon isn't set, this branch won't
80 // be called and the test will time out.
82 is(aValue, originalFavicon, "New favicon should be same as original favicon.");
84 // Let's explicitly check that we can get the favicon
85 // from nsINavHistoryService now.
86 let info = getNavHistoryEntry(makeURI(aURI));
87 ok(info, "There must be a history entry for the fragment.");
88 ok(info.icon, "The history entry must have an associated favicon.");
89 historyService.removeObserver(historyObserver, false);
90 gBrowser.removeCurrentTab();
94 onPageExpired: function(aURI, aVisitTime, aWholeEntry) {},
95 QueryInterface: function(iid) {
96 if (iid.equals(Ci.nsINavHistoryObserver) ||
97 iid.equals(Ci.nsISupports)) {
100 throw Cr.NS_ERROR_NO_INTERFACE;
103 historyService.addObserver(historyObserver, false);
105 function onPageLoad() {
106 gBrowser.selectedBrowser
107 .removeEventListener("DOMContentLoaded", arguments.callee, true);
111 // Make sure neither of the test pages haven't been loaded before.
112 var info = getNavHistoryEntry(makeURI(pageurl));
113 ok(!info, "The test page must not have been visited already.");
114 info = getNavHistoryEntry(makeURI(fragmenturl));
115 ok(!info, "The fragment test page must not have been visited already.");
117 // Now open the test page in a new tab.
118 gBrowser.selectedTab = gBrowser.addTab();
119 gBrowser.selectedBrowser.addEventListener(
120 "DOMContentLoaded", onPageLoad, true);
121 content.location = pageurl;