toolkit/components/extensions/test/mochitest/test_ext_cookies_containers.html
author Andrea Marchesini <amarchesini@mozilla.com>
Fri, 28 Oct 2016 10:16:06 +0200
changeset 319973 691162eba71737f765cb0dda5470a3a516e0d47d
permissions -rw-r--r--
Bug 1302697 - Containers and WebExtensions - part 2 - Cookie API, r=kmag

<!DOCTYPE HTML>
<html>
<head>
  <title>WebExtension test</title>
  <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"></script>
  <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script type="text/javascript" src="chrome_head.js"></script>
  <script type="text/javascript" src="head.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
</head>
<body>

<script type="text/javascript">
"use strict";

add_task(function* setup() {
  // make sure userContext is enabled.
  return SpecialPowers.pushPrefEnv({"set": [
    ["privacy.userContext.enabled", true],
  ]});
});

add_task(function* test_cookie_containers() {
  function background() {
    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");
    }

    const TEST_URL = "http://example.org/";
    const THE_FUTURE = Date.now() + 5 * 60;

    let expected = {
      name: "name1",
      value: "value1",
      domain: "example.org",
      hostOnly: true,
      path: "/",
      secure: false,
      httpOnly: false,
      session: false,
      expirationDate: THE_FUTURE,
      storeId: "firefox-container-1",
    };

    browser.cookies.set({url: TEST_URL, name: "name1", value: "value1",
                         expirationDate: THE_FUTURE, storeId: "firefox-container-1"})
    .then(cookie => {
      browser.test.assertEq("firefox-container-1", cookie.storeId, "the cookie has the correct storeId");
      return browser.cookies.get({url: TEST_URL, name: "name1"});
    })
    .then(cookie => {
      browser.test.assertEq(null, cookie, "get() without storeId returns null");
      return browser.cookies.get({url: TEST_URL, name: "name1", storeId: "firefox-container-1"});
    })
    .then(cookie => {
      assertExpected(expected, cookie);
      return browser.cookies.getAll({storeId: "firefox-default"});
    })
    .then(cookies => {
      browser.test.assertEq(0, cookies.length, "getAll() with default storeId returns an empty array");
      return browser.cookies.getAll({storeId: "firefox-container-1"});
    })
    .then(cookies => {
      browser.test.assertEq(1, cookies.length, "one cookie found for matching domain");
      assertExpected(expected, cookies[0]);
      return browser.cookies.remove({url: TEST_URL, name: "name1", storeId: "firefox-container-1"});
    })
    .then(details => {
      assertExpected({url: TEST_URL, name: "name1", storeId: "firefox-container-1"}, details);
      return browser.cookies.get({url: TEST_URL, name: "name1", storeId: "firefox-container-1"});
    })
    .then(cookie => {
      browser.test.assertEq(null, cookie, "removed cookie not found");
    })
    .then(() => {
      browser.test.notifyPass("cookies");
    });
  }

  let extension = ExtensionTestUtils.loadExtension({
    background,
    manifest: {
      permissions: ["cookies", "*://example.org/"],
    },
  });

  yield extension.startup();
  yield extension.awaitFinish("cookies");
  yield extension.unload();
});

</script>

</body>
</html>