Bug 1354229 - Re-enable cookie tests in private windows r=mixedpuppy
authorRob Wu <rob@robwu.nl>
Sun, 11 Jun 2017 17:19:13 +0200
changeset 364995 5707a502bb84056e1240d642a0cc3b309691c2a4
parent 364994 114095814191ba58c741c045dd1d501993607de7
child 364996 a1b87937f170e1cd542201444da4dbeceb4f7681
push id45146
push userrob@robwu.nl
push dateTue, 20 Jun 2017 23:38:43 +0000
treeherderautoland@a1b87937f170 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmixedpuppy
bugs1354229, 1309637
milestone56.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1354229 - Re-enable cookie tests in private windows r=mixedpuppy Re-enable the cookie tests in private windows to get test coverage for the patch for bug 1354229, while trying to work around the issue that caused the test failure in the first place (https://bugzil.la/1309637#c8). MozReview-Commit-ID: HXjPTi4gs2s
toolkit/components/extensions/test/mochitest/test_ext_cookies.html
--- a/toolkit/components/extensions/test/mochitest/test_ext_cookies.html
+++ b/toolkit/components/extensions/test/mochitest/test_ext_cookies.html
@@ -18,16 +18,50 @@ add_task(async function test_cookies() {
     function assertExpected(expected, cookie) {
       for (let key of Object.keys(cookie)) {
         browser.test.assertTrue(key in expected, `found property ${key}`);
         browser.test.assertEq(expected[key], cookie[key], `property value for ${key} is correct`);
       }
       browser.test.assertEq(Object.keys(expected).length, Object.keys(cookie).length, "all expected properties found");
     }
 
+    async function getDocumentCookie(tabId) {
+      let results = await browser.tabs.executeScript(tabId, {
+        code: "document.cookie",
+      });
+      browser.test.assertEq(results.length, 1, "executeScript returns one result");
+      return results[0];
+    }
+
+    async function openPrivateWindowAndTab(TEST_URL) {
+      // Add some random suffix to make sure that we select the right tab.
+      const PRIVATE_TEST_URL = TEST_URL + "?random" + Math.random();
+
+      let tabReadyPromise = new Promise((resolve) => {
+        browser.webNavigation.onDOMContentLoaded.addListener(function listener({tabId}) {
+          browser.webNavigation.onDOMContentLoaded.removeListener(listener);
+          resolve(tabId);
+        }, {
+          url: [{
+            urlPrefix: PRIVATE_TEST_URL,
+          }],
+        });
+      });
+      // This tab is opened for two purposes:
+      // 1. To allow tests to run content scripts in the context of a tab,
+      //    for fetching the value of document.cookie.
+      // 2. To work around bug 1309637, based on the analysis in comment 8.
+      let {id: windowId} = await browser.windows.create({
+        incognito: true,
+        url: PRIVATE_TEST_URL,
+      });
+      let tabId = await tabReadyPromise;
+      return {windowId, tabId};
+    }
+
     const TEST_URL = "http://example.org/";
     const TEST_SECURE_URL = "https://example.org/";
     const THE_FUTURE = Date.now() + 5 * 60;
     const TEST_PATH = "set_path";
     const TEST_URL_WITH_PATH = TEST_URL + TEST_PATH;
     const TEST_COOKIE_PATH = `/${TEST_PATH}`;
     const STORE_ID = "firefox-default";
     const PRIVATE_STORE_ID = "firefox-private";
@@ -83,28 +117,27 @@ add_task(async function test_cookies() {
     browser.test.assertEq(null, cookie, "removed cookie not found");
 
     let stores = await browser.cookies.getAllCookieStores();
     browser.test.assertEq(1, stores.length, "expected number of stores returned");
     browser.test.assertEq(STORE_ID, stores[0].id, "expected store id returned");
     browser.test.assertEq(1, stores[0].tabIds.length, "one tabId returned for store");
     browser.test.assertEq("number", typeof stores[0].tabIds[0], "tabId is a number");
 
-    // This part causes intermittent failures see Bug 1309637.
-    if (false) {
-      let privateWindow = await browser.windows.create({incognito: true});
+    {
+      let {windowId} = await openPrivateWindowAndTab(TEST_URL);
       let stores = await browser.cookies.getAllCookieStores();
 
       browser.test.assertEq(2, stores.length, "expected number of stores returned");
       browser.test.assertEq(STORE_ID, stores[0].id, "expected store id returned");
       browser.test.assertEq(1, stores[0].tabIds.length, "one tab returned for store");
       browser.test.assertEq(PRIVATE_STORE_ID, stores[1].id, "expected private store id returned");
       browser.test.assertEq(1, stores[0].tabIds.length, "one tab returned for private store");
 
-      await browser.windows.remove(privateWindow.id);
+      await browser.windows.remove(windowId);
     }
 
     cookie = await browser.cookies.set({url: TEST_URL, name: "name2", domain: ".example.org", expirationDate: THE_FUTURE});
     browser.test.assertEq(false, cookie.hostOnly, "cookie is not a hostOnly cookie");
 
     details = await browser.cookies.remove({url: TEST_URL, name: "name2"});
     assertExpected({url: TEST_URL, name: "name2", storeId: STORE_ID}, details);
 
@@ -175,59 +208,61 @@ add_task(async function test_cookies() {
     details = await browser.cookies.remove({url: TEST_URL, name: "name1"});
     assertExpected({url: TEST_URL, name: "name1", storeId: STORE_ID}, details);
 
     cookie = await browser.cookies.set({url: TEST_URL});
     browser.test.assertEq("", cookie.name, "default name set");
     browser.test.assertEq("", cookie.value, "default value set");
     browser.test.assertEq(true, cookie.session, "no expiry date created session cookie");
 
-    // This part causes intermittent failures see Bug 1309637.
-    if (false) {
-      let privateWindow = await browser.windows.create({incognito: true});
+    {
+      let {tabId, windowId} = await openPrivateWindowAndTab(TEST_URL);
 
-      // Hacky work-around for bugzil.la/1309637
-      await new Promise(resolve => setTimeout(resolve, 700));
+      browser.test.assertEq("", await getDocumentCookie(tabId), "initially no cookie");
 
       let cookie = await browser.cookies.set({url: TEST_URL, name: "store", value: "private", expirationDate: THE_FUTURE, storeId: PRIVATE_STORE_ID});
       browser.test.assertEq("private", cookie.value, "set the private cookie");
 
       cookie = await browser.cookies.set({url: TEST_URL, name: "store", value: "default", expirationDate: THE_FUTURE, storeId: STORE_ID});
       browser.test.assertEq("default", cookie.value, "set the default cookie");
 
       cookie = await browser.cookies.get({url: TEST_URL, name: "store", storeId: PRIVATE_STORE_ID});
       browser.test.assertEq("private", cookie.value, "get the private cookie");
       browser.test.assertEq(PRIVATE_STORE_ID, cookie.storeId, "get the private cookie storeId");
 
       cookie = await browser.cookies.get({url: TEST_URL, name: "store", storeId: STORE_ID});
       browser.test.assertEq("default", cookie.value, "get the default cookie");
       browser.test.assertEq(STORE_ID, cookie.storeId, "get the default cookie storeId");
 
+      browser.test.assertEq("store=private", await getDocumentCookie(tabId), "private document.cookie should be set");
+
       let details = await browser.cookies.remove({url: TEST_URL, name: "store", storeId: STORE_ID});
       assertExpected({url: TEST_URL, name: "store", storeId: STORE_ID}, details);
 
       cookie = await browser.cookies.get({url: TEST_URL, name: "store", storeId: STORE_ID});
       browser.test.assertEq(null, cookie, "deleted the default cookie");
 
       details = await browser.cookies.remove({url: TEST_URL, name: "store", storeId: PRIVATE_STORE_ID});
       assertExpected({url: TEST_URL, name: "store", storeId: PRIVATE_STORE_ID}, details);
 
       cookie = await browser.cookies.get({url: TEST_URL, name: "store", storeId: PRIVATE_STORE_ID});
       browser.test.assertEq(null, cookie, "deleted the private cookie");
 
-      await browser.windows.remove(privateWindow.id);
+      browser.test.assertEq("", await getDocumentCookie(tabId), "private document.cookie should be removed");
+
+      await browser.windows.remove(windowId);
     }
 
     browser.test.notifyPass("cookies");
   }
 
   let extension = ExtensionTestUtils.loadExtension({
     background,
     manifest: {
-      permissions: ["cookies", "*://example.org/"],
+      permissions: ["cookies", "*://example.org/", "webNavigation"],
     },
   });
 
   await extension.startup();
   await extension.awaitFinish("cookies");
   await extension.unload();
 });