Bug 1283709 - Test if cookies are restored correctly to container tabs. r=mdeboer, a=lizzard
authorJonathan Hao <jhao@mozilla.com>
Mon, 18 Jul 2016 00:34:00 +0200
changeset 339985 ff681ca2951f2338dc2738b29affac4c7e12e62c
parent 339984 ad99b4dccc1741a0830ad00e4c28d9b62fa6ca5d
child 339986 c6dcf10108bdcb88f61533e589b4eb55255d38c1
push id6249
push userjlund@mozilla.com
push dateMon, 01 Aug 2016 13:59:36 +0000
treeherdermozilla-beta@bad9d4f5bf7e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmdeboer, lizzard
bugs1283709
milestone49.0a2
Bug 1283709 - Test if cookies are restored correctly to container tabs. r=mdeboer, a=lizzard
browser/components/sessionstore/test/browser_sessionStoreContainer.js
--- a/browser/components/sessionstore/test/browser_sessionStoreContainer.js
+++ b/browser/components/sessionstore/test/browser_sessionStoreContainer.js
@@ -179,8 +179,78 @@ add_task(function* () {
                    "The document has the correct userContextId");
     });
   }
 
   yield BrowserTestUtils.closeWindow(win);
   yield BrowserTestUtils.closeWindow(win2);
 });
 
+// Opens "uri" in a new tab with the provided userContextId and focuses it.
+// Returns the newly opened tab.
+function* openTabInUserContext(userContextId) {
+  // Open the tab in the correct userContextId.
+  let tab = gBrowser.addTab("http://example.com", { userContextId });
+
+  // Select tab and make sure its browser is focused.
+  gBrowser.selectedTab = tab;
+  tab.ownerDocument.defaultView.focus();
+
+  let browser = gBrowser.getBrowserForTab(tab);
+  yield BrowserTestUtils.browserLoaded(browser);
+  return { tab, browser };
+}
+
+function waitForNewCookie() {
+  return new Promise(resolve => {
+    Services.obs.addObserver(function observer(subj, topic, data) {
+      let cookie = subj.QueryInterface(Ci.nsICookie2);
+      if (data == "added") {
+        Services.obs.removeObserver(observer, topic);
+        resolve();
+      }
+    }, "cookie-changed", false);
+  });
+}
+
+add_task(function* () {
+  const USER_CONTEXTS = [
+    "default",
+    "personal",
+    "work",
+  ];
+
+  const ss = Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
+  const { TabStateFlusher } = Cu.import("resource:///modules/sessionstore/TabStateFlusher.jsm", {});
+
+  // Make sure userContext is enabled.
+  yield SpecialPowers.pushPrefEnv({
+    "set": [ [ "privacy.userContext.enabled", true ] ]
+  });
+
+  let lastSessionRestore;
+  for (let userContextId of Object.keys(USER_CONTEXTS)) {
+    // Load the page in 3 different contexts and set a cookie
+    // which should only be visible in that context.
+    let cookie = USER_CONTEXTS[userContextId];
+
+    // Open our tab in the given user context.
+    let { tab, browser } = yield* openTabInUserContext(userContextId);
+
+    yield Promise.all([
+      waitForNewCookie(),
+      ContentTask.spawn(browser, cookie, cookie => content.document.cookie = cookie)
+    ]);
+
+    // Ensure the tab's session history is up-to-date.
+    yield TabStateFlusher.flush(browser);
+
+    lastSessionRestore = ss.getWindowState(window);
+
+    // Remove the tab.
+    gBrowser.removeTab(tab);
+  }
+
+  let state = JSON.parse(lastSessionRestore);
+  is(state.windows[0].cookies.length, USER_CONTEXTS.length,
+    "session restore should have each container's cookie");
+});
+