toolkit/components/extensions/test/mochitest/test_ext_cookies.html
author Kris Maglione <maglione.k@gmail.com>
Tue, 22 Dec 2015 23:43:23 -0500
changeset 317182 a357c7e76527ac42fc450826d37f57c3c99ad97f
parent 298467 687c8a36b56e648c6806f63a8ee3a3864547f920
permissions -rw-r--r--
Bug 1234755: [webext] Make JS in HTML files pass ESLint. r?billm This also changes the `type` attribute in our script tags to text/javascript, since that's currently all the eslint-html plugin supports.

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

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

function backgroundScript() {
  function get(details) {
    return new Promise(resolve => {
      browser.cookies.get(details, resolve);
    });
  }

  function getAll(details) {
    return new Promise(resolve => {
      browser.cookies.getAll(details, resolve);
    });
  }

  function set(details) {
    return new Promise(resolve => {
      browser.cookies.set(details, resolve);
    });
  }

  function remove(details) {
    return new Promise(resolve => {
      browser.cookies.remove(details, resolve);
    });
  }

  function getAllCookieStores() {
    return new Promise(resolve => {
      browser.cookies.getAllCookieStores(resolve);
    });
  }

  function assertExpected(cookie, expected) {
    for (let key of Object.keys(cookie)) {
      browser.test.assertTrue(key in expected, "found property " + key);
      browser.test.assertEq(cookie[key], expected[key], "property value for " + key + " is wrong");
    }
    browser.test.assertEq(Object.keys(cookie).length, Object.keys(expected).length, "all expected properties found");
  }

  let TEST_URL = "http://example.org/";
  let 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-default",
  };

  set({url: TEST_URL, name: "name1", value: "value1", expirationDate: THE_FUTURE}).then(cookie => {
    assertExpected(cookie, expected);
    return get({url: TEST_URL, name: "name1"});
  }).then(cookie => {
    assertExpected(cookie, expected);
    return getAll({domain: "example.org"});
  }).then(cookies => {
    browser.test.assertEq(cookies.length, 1, "only found one cookie for example.org");
    assertExpected(cookies[0], expected);
    return remove({url: TEST_URL, name: "name1"});
  }).then(details => {
    assertExpected(details, {url: TEST_URL, name: "name1", storeId: "firefox-default"});
    return get({url: TEST_URL, name: "name1"});
  }).then(cookie => {
    browser.test.assertEq(cookie, null);
    return getAllCookieStores();
  }).then(stores => {
    browser.test.assertEq(stores.length, 1);
    browser.test.assertEq(stores[0].id, "firefox-default");
    browser.test.assertEq(stores[0].tabIds.length, 0); // Todo: Implement this.
  }).then(() => {
    browser.test.notifyPass("cookies");
  });
}

let extensionData = {
  background: "(" + backgroundScript.toString() + ")()",
  manifest: {
    permissions: ["cookies"],
  },
};

add_task(function* test_contentscript() {
  let extension = ExtensionTestUtils.loadExtension(extensionData);
  yield extension.startup();
  info("extension loaded");
  yield extension.awaitFinish("cookies");
  yield extension.unload();
  info("extension unloaded");
});

</script>

</body>
</html>