Bug 1197417 - [webext] Tests for browser.cookies. r=billm
authorTom Schuster <evilpies@gmail.com>
Fri, 02 Oct 2015 16:36:05 +0200
changeset 265769 687c8a36b56e648c6806f63a8ee3a3864547f920
parent 265768 e92426565d532e84dc71b6d41578d4cc23946b33
child 265770 636b763d4a927f956674db242f7a7f5b822f79b6
push id29470
push userphilringnalda@gmail.com
push dateSat, 03 Oct 2015 22:38:52 +0000
treeherderautoland@2e62c35afb2c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbillm
bugs1197417
milestone44.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 1197417 - [webext] Tests for browser.cookies. r=billm
toolkit/components/extensions/test/mochitest/mochitest.ini
toolkit/components/extensions/test/mochitest/test_ext_cookies.html
--- a/toolkit/components/extensions/test/mochitest/mochitest.ini
+++ b/toolkit/components/extensions/test/mochitest/mochitest.ini
@@ -23,8 +23,9 @@ support-files =
 [test_ext_generate.html]
 [test_ext_notifications.html]
 [test_ext_runtime_connect.html]
 [test_ext_runtime_disconnect.html]
 [test_ext_sandbox_var.html]
 [test_ext_sendmessage_reply.html]
 [test_ext_sendmessage_doublereply.html]
 [test_ext_storage.html]
+[test_ext_cookies.html]
new file mode 100644
--- /dev/null
+++ b/toolkit/components/extensions/test/mochitest/test_ext_cookies.html
@@ -0,0 +1,113 @@
+<!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="application/javascript;version=1.8">
+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 (var 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");
+  }
+
+  var TEST_URL = "http://example.org/";
+  var THE_FUTURE = Date.now() + 5 * 60;
+
+  var 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>