michael@0: /* Test for Bug 420605 michael@0: * https://bugzilla.mozilla.org/show_bug.cgi?id=420605 michael@0: */ michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: michael@0: var pageurl = "http://mochi.test:8888/browser/docshell/test/browser/file_bug420605.html"; michael@0: var fragmenturl = "http://mochi.test:8888/browser/docshell/test/browser/file_bug420605.html#firefox"; michael@0: michael@0: var historyService = Cc["@mozilla.org/browser/nav-history-service;1"] michael@0: .getService(Ci.nsINavHistoryService); michael@0: michael@0: /* Queries nsINavHistoryService and returns a single history entry michael@0: * for a given URI */ michael@0: function getNavHistoryEntry(aURI) { michael@0: var options = historyService.getNewQueryOptions(); michael@0: options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_HISTORY; michael@0: options.maxResults = 1; michael@0: michael@0: var query = historyService.getNewQuery(); michael@0: query.uri = aURI; michael@0: michael@0: var result = historyService.executeQuery(query, options); michael@0: result.root.containerOpen = true; michael@0: michael@0: if (!result.root.childCount) { michael@0: return null; michael@0: } michael@0: return result.root.getChild(0); michael@0: } michael@0: michael@0: // We'll save the favicon URL of the orignal page here and check that the michael@0: // page with a hash has the same favicon. michael@0: var originalFavicon; michael@0: michael@0: // Control flow in this test is a bit complicated. michael@0: // michael@0: // When the page loads, onPageLoad (the DOMContentLoaded handler) and michael@0: // historyObserver::onPageChanged are both called, in some order. Once michael@0: // they've both run, we click a fragment link in the content page michael@0: // (clickLinkIfReady), which should trigger another onPageChanged event, michael@0: // this time for the fragment's URL. michael@0: michael@0: var _clickLinkTimes = 0; michael@0: function clickLinkIfReady() { michael@0: _clickLinkTimes++; michael@0: if (_clickLinkTimes == 2) { michael@0: EventUtils.sendMouseEvent({type:'click'}, 'firefox-link', michael@0: gBrowser.selectedBrowser.contentWindow); michael@0: } michael@0: } michael@0: michael@0: /* Global history observer that triggers for the two test URLs above. */ michael@0: var historyObserver = { michael@0: onBeginUpdateBatch: function() {}, michael@0: onEndUpdateBatch: function() {}, michael@0: onVisit: function(aURI, aVisitID, aTime, aSessionId, aReferringId, michael@0: aTransitionType, _added) {}, michael@0: onTitleChanged: function(aURI, aPageTitle) {}, michael@0: onDeleteURI: function(aURI) {}, michael@0: onClearHistory: function() {}, michael@0: onPageChanged: function(aURI, aWhat, aValue) { michael@0: if (aWhat != Ci.nsINavHistoryObserver.ATTRIBUTE_FAVICON) { michael@0: return; michael@0: } michael@0: aURI = aURI.spec; michael@0: switch (aURI) { michael@0: case pageurl: michael@0: ok(aValue, "Favicon value is not null for page without fragment."); michael@0: originalFavicon = aValue; michael@0: michael@0: // Now that the favicon has loaded, click on fragment link. michael@0: // This should trigger the |case fragmenturl| below. michael@0: clickLinkIfReady(); michael@0: michael@0: return; michael@0: case fragmenturl: michael@0: // If the fragment URL's favicon isn't set, this branch won't michael@0: // be called and the test will time out. michael@0: michael@0: is(aValue, originalFavicon, "New favicon should be same as original favicon."); michael@0: michael@0: // Let's explicitly check that we can get the favicon michael@0: // from nsINavHistoryService now. michael@0: let info = getNavHistoryEntry(makeURI(aURI)); michael@0: ok(info, "There must be a history entry for the fragment."); michael@0: ok(info.icon, "The history entry must have an associated favicon."); michael@0: historyService.removeObserver(historyObserver, false); michael@0: gBrowser.removeCurrentTab(); michael@0: finish(); michael@0: } michael@0: }, michael@0: onPageExpired: function(aURI, aVisitTime, aWholeEntry) {}, michael@0: QueryInterface: function(iid) { michael@0: if (iid.equals(Ci.nsINavHistoryObserver) || michael@0: iid.equals(Ci.nsISupports)) { michael@0: return this; michael@0: } michael@0: throw Cr.NS_ERROR_NO_INTERFACE; michael@0: } michael@0: }; michael@0: historyService.addObserver(historyObserver, false); michael@0: michael@0: function onPageLoad() { michael@0: gBrowser.selectedBrowser michael@0: .removeEventListener("DOMContentLoaded", arguments.callee, true); michael@0: clickLinkIfReady(); michael@0: } michael@0: michael@0: // Make sure neither of the test pages haven't been loaded before. michael@0: var info = getNavHistoryEntry(makeURI(pageurl)); michael@0: ok(!info, "The test page must not have been visited already."); michael@0: info = getNavHistoryEntry(makeURI(fragmenturl)); michael@0: ok(!info, "The fragment test page must not have been visited already."); michael@0: michael@0: // Now open the test page in a new tab. michael@0: gBrowser.selectedTab = gBrowser.addTab(); michael@0: gBrowser.selectedBrowser.addEventListener( michael@0: "DOMContentLoaded", onPageLoad, true); michael@0: content.location = pageurl; michael@0: }