Bug 1376973 - Part 2: Add test cases for making sure that favicons of allTabs popup menu is using correct originAttributes. r=arthuredelstein,baku

This patch adds test cases for testing favicons of allTabs menu in terms of originAttributes.
It adds tests in existing tests, browser_favicon_userContextId.js and browser_favicon_firstParty.js.
The test will open tabs with different originAttributes and trigger the popup of
allTabs menu. Then it will verify that whether the network requests of favicon
have correct originAttributes.

MozReview-Commit-ID: 4Aa7RDFdosA

--HG--
extra : rebase_source : 407c1c356387f9f05e2fca37cd74339521f2d2f2
This commit is contained in:
Tim Huang 2017-10-20 09:19:58 +08:00
Родитель ef781411f3
Коммит e1b4f2267e
2 изменённых файлов: 222 добавлений и 6 удалений

Просмотреть файл

@ -6,6 +6,10 @@ const { classes: Cc, Constructor: CC, interfaces: Ci, utils: Cu } = Components;
Cu.import("resource://gre/modules/PlacesUtils.jsm");
let EventUtils = {};
Services.scriptloader.loadSubScript(
"chrome://mochikit/content/tests/SimpleTest/EventUtils.js", EventUtils);
const FIRST_PARTY_ONE = "example.com";
const FIRST_PARTY_TWO = "example.org";
const THIRD_PARTY = "mochi.test:8888";
@ -52,9 +56,10 @@ function clearAllPlacesFavicons() {
});
}
function observeFavicon(aFirstPartyDomain, aExpectedCookie, aPageURI) {
function observeFavicon(aFirstPartyDomain, aExpectedCookie, aPageURI, aOnlyXUL) {
let faviconReqXUL = false;
let faviconReqPlaces = false;
// If aOnlyXUL is true, we only care about the favicon request from XUL.
let faviconReqPlaces = aOnlyXUL === true;
let expectedPrincipal = Services.scriptSecurityManager
.createCodebasePrincipal(aPageURI, { firstPartyDomain: aFirstPartyDomain });
@ -244,6 +249,85 @@ async function doTest(aTestPage, aExpectedCookies, aFaviconURL) {
await BrowserTestUtils.removeTab(tabInfo.tab);
}
async function doTestForAllTabsFavicon(aTestPage, aExpectedCookies, aIsThirdParty) {
let firstPageURI = makeURI(TEST_SITE_ONE + aTestPage);
let secondPageURI = makeURI(TEST_SITE_TWO + aTestPage);
let faviconURI = aIsThirdParty ? THIRD_PARTY_SITE + FAVICON_URI :
TEST_SITE_ONE + FAVICON_URI;
// Set the 'overflow' attribute to make allTabs button available.
let tabBrowser = document.getElementById("tabbrowser-tabs");
let allTabsBtn = document.getElementById("alltabs-button");
tabBrowser.setAttribute("overflow", true);
// Start to observe the event of that the favicon has been fully loaded.
let promiseFaviconLoaded = waitOnFaviconLoaded(faviconURI);
// Open the tab for the first site.
let tabInfo = await openTab(TEST_SITE_ONE + aTestPage);
// Waiting until the favicon loaded.
await promiseFaviconLoaded;
// We need to clear the image cache here for making sure the network request will
// be made for the favicon of allTabs menuitem.
clearAllImageCaches();
// Start to observe the allTabs favicon requests earlier in case we miss it.
let promiseObserveFavicon = observeFavicon(FIRST_PARTY_ONE, aExpectedCookies[0],
firstPageURI, true);
// Make the popup of allTabs showing up and trigger the loading of the favicon.
let allTabsPopupShownPromise = BrowserTestUtils.waitForEvent(allTabsBtn, "popupshown");
EventUtils.synthesizeMouseAtCenter(allTabsBtn, {});
await promiseObserveFavicon;
await allTabsPopupShownPromise;
// Close the popup of allTabs and wait until it's done.
let allTabsPopupHiddenPromise = BrowserTestUtils.waitForEvent(allTabsBtn, "popuphidden");
EventUtils.synthesizeMouseAtCenter(allTabsBtn, {});
await allTabsPopupHiddenPromise;
// Close the tab.
await BrowserTestUtils.removeTab(tabInfo.tab);
faviconURI = aIsThirdParty ? THIRD_PARTY_SITE + FAVICON_URI :
TEST_SITE_TWO + FAVICON_URI;
// Start to observe the event of that favicon has been fully loaded.
promiseFaviconLoaded = waitOnFaviconLoaded(faviconURI);
// Open the tab for the second site.
tabInfo = await openTab(TEST_SITE_TWO + aTestPage);
// Wait until the favicon is fully loaded.
await promiseFaviconLoaded;
// Clear the image cache for the favicon of the second site.
clearAllImageCaches();
// Start to observe the allTabs favicon requests earlier in case we miss it.
promiseObserveFavicon = observeFavicon(FIRST_PARTY_TWO, aExpectedCookies[1],
secondPageURI, true);
// Make the popup of allTabs showing up again.
allTabsPopupShownPromise = BrowserTestUtils.waitForEvent(allTabsBtn, "popupshown");
EventUtils.synthesizeMouseAtCenter(allTabsBtn, {});
await promiseObserveFavicon;
await allTabsPopupShownPromise;
// Close the popup of allTabs and wait until it's done.
allTabsPopupHiddenPromise = BrowserTestUtils.waitForEvent(allTabsBtn, "popuphidden");
EventUtils.synthesizeMouseAtCenter(allTabsBtn, {});
await allTabsPopupHiddenPromise;
// Close the tab.
await BrowserTestUtils.removeTab(tabInfo.tab);
// Reset the 'overflow' attribute to make the allTabs button hidden again.
tabBrowser.removeAttribute("overflow");
}
add_task(async function setup() {
// Make sure first party isolation is enabled.
await SpecialPowers.pushPrefEnv({"set": [
@ -282,6 +366,26 @@ add_task(async function test_favicon_firstParty() {
}
});
add_task(async function test_allTabs_favicon_firstParty() {
for (let testThirdParty of [false, true]) {
// Clear all image caches and network caches before running the test.
clearAllImageCaches();
Services.cache2.clear();
// Clear Places favicon caches.
await clearAllPlacesFavicons();
let cookies = await generateCookies(testThirdParty);
if (testThirdParty) {
await doTestForAllTabsFavicon(TEST_THIRD_PARTY_PAGE, cookies, testThirdParty);
} else {
await doTestForAllTabsFavicon(TEST_PAGE, cookies, testThirdParty);
}
}
});
add_task(async function test_favicon_cache_firstParty() {
// Clear all image caches and network caches before running the test.
clearAllImageCaches();

Просмотреть файл

@ -7,6 +7,10 @@ const { classes: Cc, Constructor: CC, interfaces: Ci, utils: Cu } = Components;
XPCOMUtils.defineLazyModuleGetter(this, "Promise",
"resource://gre/modules/Promise.jsm");
let EventUtils = {};
Services.scriptloader.loadSubScript(
"chrome://mochikit/content/tests/SimpleTest/EventUtils.js", EventUtils);
const TEST_SITE = "http://example.net";
const TEST_THIRD_PARTY_SITE = "http://mochi.test:8888";
@ -52,8 +56,8 @@ function clearAllPlacesFavicons() {
});
}
function FaviconObserver(aUserContextId, aExpectedCookie, aPageURI, aFaviconURL) {
this.reset(aUserContextId, aExpectedCookie, aPageURI, aFaviconURL);
function FaviconObserver(aUserContextId, aExpectedCookie, aPageURI, aFaviconURL, aOnlyXUL) {
this.reset(aUserContextId, aExpectedCookie, aPageURI, aFaviconURL, aOnlyXUL);
}
FaviconObserver.prototype = {
@ -108,13 +112,14 @@ FaviconObserver.prototype = {
}
},
reset(aUserContextId, aExpectedCookie, aPageURI, aFaviconURL) {
reset(aUserContextId, aExpectedCookie, aPageURI, aFaviconURL, aOnlyXUL) {
this._curUserContextId = aUserContextId;
this._expectedCookie = aExpectedCookie;
this._expectedPrincipal = Services.scriptSecurityManager
.createCodebasePrincipal(aPageURI, { userContextId: aUserContextId });
this._faviconReqXUL = false;
this._faviconReqPlaces = false;
// If aOnlyXUL is true, we only care about the favicon request from XUL.
this._faviconReqPlaces = aOnlyXUL === true;
this._faviconURL = aFaviconURL;
this._faviconLoaded = new Promise.defer();
},
@ -201,6 +206,87 @@ async function doTest(aTestPage, aFaviconHost, aFaviconURL) {
await BrowserTestUtils.removeTab(tabInfo.tab);
}
async function doTestForAllTabsFavicon(aTestPage, aFaviconHost, aFaviconURL) {
let cookies = await generateCookies(aFaviconHost);
let pageURI = makeURI(aTestPage);
// Set the 'overflow' attribute to make allTabs button available.
let tabBrowser = document.getElementById("tabbrowser-tabs");
let allTabsBtn = document.getElementById("alltabs-button");
tabBrowser.setAttribute("overflow", true);
// Create the observer object for observing request channels of the personal
// container.
let observer = new FaviconObserver(USER_CONTEXT_ID_PERSONAL, cookies[0], pageURI, aFaviconURL, true);
// Add the observer earlier in case we miss it.
let promiseWaitOnFaviconLoaded = waitOnFaviconLoaded(aFaviconURL);
// Open the tab with the personal container.
let tabInfo = await openTabInUserContext(aTestPage, USER_CONTEXT_ID_PERSONAL);
// Waiting for favicon loaded.
await promiseWaitOnFaviconLoaded;
// We need to clear the image cache here for making sure the network request will
// be made for the favicon of allTabs menuitem.
clearAllImageCaches();
// Add the observer for listening favicon requests.
Services.obs.addObserver(observer, "http-on-modify-request");
// Make the popup of allTabs showing up and trigger the loading of the favicon.
let allTabsPopupShownPromise = BrowserTestUtils.waitForEvent(allTabsBtn, "popupshown");
EventUtils.synthesizeMouseAtCenter(allTabsBtn, {});
await observer.promise;
await allTabsPopupShownPromise;
// Close the popup of allTabs and wait until it's done.
let allTabsPopupHiddenPromise = BrowserTestUtils.waitForEvent(allTabsBtn, "popuphidden");
EventUtils.synthesizeMouseAtCenter(allTabsBtn, {});
await allTabsPopupHiddenPromise;
// Remove the observer for not receiving the favicon requests for opening a tab
// since we want to focus on the favicon of allTabs menu here.
Services.obs.removeObserver(observer, "http-on-modify-request");
// Close the tab.
await BrowserTestUtils.removeTab(tabInfo.tab);
// Open the tab under the work container and wait until the favicon is loaded.
promiseWaitOnFaviconLoaded = waitOnFaviconLoaded(aFaviconURL);
tabInfo = await openTabInUserContext(aTestPage, USER_CONTEXT_ID_WORK);
await promiseWaitOnFaviconLoaded;
// Clear the image cache again.
clearAllImageCaches();
// Reset the observer for observing requests for the work container.
observer.reset(USER_CONTEXT_ID_WORK, cookies[1], pageURI, aFaviconURL, true);
// Add the observer back for listening the favicon requests for allTabs menuitem.
Services.obs.addObserver(observer, "http-on-modify-request");
// Make the popup of allTabs showing up again.
allTabsPopupShownPromise = BrowserTestUtils.waitForEvent(allTabsBtn, "popupshown");
EventUtils.synthesizeMouseAtCenter(allTabsBtn, {});
await observer.promise;
await allTabsPopupShownPromise;
// Close the popup of allTabs and wait until it's done.
allTabsPopupHiddenPromise = BrowserTestUtils.waitForEvent(allTabsBtn, "popuphidden");
EventUtils.synthesizeMouseAtCenter(allTabsBtn, {});
await allTabsPopupHiddenPromise;
Services.obs.removeObserver(observer, "http-on-modify-request");
// Close the tab.
await BrowserTestUtils.removeTab(tabInfo.tab);
// Reset the 'overflow' attribute to make the allTabs button hidden again.
tabBrowser.removeAttribute("overflow");
}
add_task(async function setup() {
// Make sure userContext is enabled.
await SpecialPowers.pushPrefEnv({"set": [
@ -247,3 +333,29 @@ add_task(async function test_thirdPartyFavicon_userContextId() {
await doTest(TEST_THIRD_PARTY_PAGE, TEST_THIRD_PARTY_SITE, THIRD_PARTY_FAVICON_URI);
});
add_task(async function test_allTabs_favicon_userContextId() {
// Clear all image caches before running the test.
clearAllImageCaches();
// Clear all network caches.
Services.cache2.clear();
// Clear Places favicon caches.
await clearAllPlacesFavicons();
await doTestForAllTabsFavicon(TEST_PAGE, TEST_SITE, FAVICON_URI);
});
add_task(async function test_allTabs_thirdPartyFavicon_userContextId() {
// Clear all image caches before running the test.
clearAllImageCaches();
// Clear all network caches.
Services.cache2.clear();
// Clear Places favicon caches.
await clearAllPlacesFavicons();
await doTestForAllTabsFavicon(TEST_THIRD_PARTY_PAGE, TEST_THIRD_PARTY_SITE, THIRD_PARTY_FAVICON_URI);
});