Bug 894150 - Test.
[gecko.git] / docshell / test / browser / browser_bug420605.js
blob98576867d3f9f5af36c07fe8d7554bfbfc739f77
1 /* Test for Bug 420605
2  * https://bugzilla.mozilla.org/show_bug.cgi?id=420605
3  */
5 function test() {
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
15      * for a given URI */
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();
22         query.uri = aURI;
24         var result = historyService.executeQuery(query, options);
25         result.root.containerOpen = true;
27         if (!result.root.childCount) {
28             return null;
29         }
30         return result.root.getChild(0);
31     }
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.
35     var originalFavicon;
37     // Control flow in this test is a bit complicated.
38     //
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() {
47       _clickLinkTimes++;
48       if (_clickLinkTimes == 2) {
49         EventUtils.sendMouseEvent({type:'click'}, 'firefox-link',
50                                   gBrowser.selectedBrowser.contentWindow);
51       }
52     }
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) {
65                 return;
66             }
67             aURI = aURI.spec;
68             switch (aURI) {
69             case pageurl:
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.
75                 clickLinkIfReady();
77                 return;
78             case fragmenturl:
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();
91                 finish();
92             }
93         },
94         onPageExpired: function(aURI, aVisitTime, aWholeEntry) {},
95         QueryInterface: function(iid) {
96             if (iid.equals(Ci.nsINavHistoryObserver) ||
97                 iid.equals(Ci.nsISupports)) {
98                 return this;
99             }
100             throw Cr.NS_ERROR_NO_INTERFACE;
101         }
102     };
103     historyService.addObserver(historyObserver, false);
105     function onPageLoad() {
106       gBrowser.selectedBrowser
107               .removeEventListener("DOMContentLoaded", arguments.callee, true);
108       clickLinkIfReady();
109     }
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;