browser_bug420605.js (4174B)
1 /* Test for Bug 420605 2 * https://bugzilla.mozilla.org/show_bug.cgi?id=420605 3 */ 4 5 const { PlacesTestUtils } = ChromeUtils.importESModule( 6 "resource://testing-common/PlacesTestUtils.sys.mjs" 7 ); 8 9 add_task(async function test() { 10 var pageurl = 11 "http://mochi.test:8888/browser/docshell/test/browser/file_bug420605.html"; 12 var fragmenturl = 13 "http://mochi.test:8888/browser/docshell/test/browser/file_bug420605.html#firefox"; 14 15 /* Queries nsINavHistoryService and returns a single history entry 16 * for a given URI */ 17 function getNavHistoryEntry(aURI) { 18 var options = PlacesUtils.history.getNewQueryOptions(); 19 options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_HISTORY; 20 options.maxResults = 1; 21 22 var query = PlacesUtils.history.getNewQuery(); 23 query.uri = aURI; 24 var result = PlacesUtils.history.executeQuery(query, options); 25 result.root.containerOpen = true; 26 27 if (!result.root.childCount) { 28 return null; 29 } 30 return result.root.getChild(0); 31 } 32 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; 36 37 // Control flow in this test is a bit complicated. 38 // 39 // When the page loads, onPageLoad (the DOMContentLoaded handler) and 40 // favicon-changed 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 favicon-changed event, 43 // this time for the fragment's URL. 44 45 var _clickLinkTimes = 0; 46 function clickLinkIfReady() { 47 _clickLinkTimes++; 48 if (_clickLinkTimes == 2) { 49 BrowserTestUtils.synthesizeMouseAtCenter( 50 "#firefox-link", 51 {}, 52 gBrowser.selectedBrowser 53 ); 54 } 55 } 56 57 function onPageLoad() { 58 clickLinkIfReady(); 59 } 60 61 // Make sure neither of the test pages haven't been loaded before. 62 var info = getNavHistoryEntry(makeURI(pageurl)); 63 ok(!info, "The test page must not have been visited already."); 64 info = getNavHistoryEntry(makeURI(fragmenturl)); 65 ok(!info, "The fragment test page must not have been visited already."); 66 67 let promiseIcon1 = PlacesTestUtils.waitForNotification( 68 "favicon-changed", 69 events => 70 events.some(e => { 71 if (e.url == pageurl) { 72 ok( 73 e.faviconUrl, 74 "Favicon value is not null for page without fragment." 75 ); 76 originalFavicon = e.faviconUrl; 77 78 // Now that the favicon has loaded, click on fragment link. 79 // This should trigger the |case fragmenturl| below. 80 clickLinkIfReady(); 81 return true; 82 } 83 return false; 84 }) 85 ); 86 let promiseIcon2 = PlacesTestUtils.waitForNotification( 87 "favicon-changed", 88 events => 89 events.some(e => { 90 if (e.url == fragmenturl) { 91 // If the fragment URL's favicon isn't set, this branch won't 92 // be called and the test will time out. 93 is( 94 e.faviconUrl, 95 originalFavicon, 96 "New favicon should be same as original favicon." 97 ); 98 ok( 99 e.faviconUrl, 100 "Favicon value is not null for page without fragment." 101 ); 102 originalFavicon = e.faviconUrl; 103 104 // Now that the favicon has loaded, click on fragment link. 105 // This should trigger the |case fragmenturl| below. 106 clickLinkIfReady(); 107 return true; 108 } 109 return false; 110 }) 111 ); 112 113 // Now open the test page in a new tab. 114 gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser); 115 BrowserTestUtils.waitForContentEvent( 116 gBrowser.selectedBrowser, 117 "DOMContentLoaded", 118 true 119 ).then(onPageLoad); 120 BrowserTestUtils.startLoadingURIString(gBrowser.selectedBrowser, pageurl); 121 122 await promiseIcon1; 123 await promiseIcon2; 124 125 // Let's explicitly check that we can get the favicon 126 // from nsINavHistoryService now. 127 info = getNavHistoryEntry(makeURI(fragmenturl)); 128 ok(info, "There must be a history entry for the fragment."); 129 ok(info.icon, "The history entry must have an associated favicon."); 130 gBrowser.removeCurrentTab(); 131 });