<!DOCTYPE HTML>
<html>
<head>
  <title>WebExtension test</title>
  <meta charset="utf-8">
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script 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";

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

add_task(async function test_cookie_containers() {
  async function background() {
    // Sometimes there is a cookie without name/value when running tests.
    let cookiesAtStart = await browser.cookies.getAll({storeId: "firefox-default"});

    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,
      sameSite: "no_restriction",
      session: false,
      expirationDate: THE_FUTURE,
      storeId: "firefox-container-1",
      firstPartyDomain: "",
      partitionKey: null,
    };

    let cookie = await browser.cookies.set({
      url: TEST_URL, name: "name1", value: "value1",
      expirationDate: THE_FUTURE, storeId: "firefox-container-1",
    });
    browser.test.assertEq("firefox-container-1", cookie.storeId, "the cookie has the correct storeId");

    cookie = await browser.cookies.get({url: TEST_URL, name: "name1"});
    browser.test.assertEq(null, cookie, "get() without storeId returns null");

    cookie = await browser.cookies.get({url: TEST_URL, name: "name1", storeId: "firefox-container-1"});
    assertExpected(expected, cookie);

    let cookies = await browser.cookies.getAll({storeId: "firefox-default"});
    browser.test.assertEq(0, cookiesAtStart.length - cookies.length, "getAll() with default storeId hasn't added cookies");

    cookies = await browser.cookies.getAll({storeId: "firefox-container-1"});
    browser.test.assertEq(1, cookies.length, "one cookie found for matching domain");
    assertExpected(expected, cookies[0]);

    let details = await browser.cookies.remove({url: TEST_URL, name: "name1", storeId: "firefox-container-1"});
    assertExpected({url: TEST_URL, name: "name1", storeId: "firefox-container-1", firstPartyDomain: "", partitionKey: null}, details);

    cookie = await browser.cookies.get({url: TEST_URL, name: "name1", storeId: "firefox-container-1"});
    browser.test.assertEq(null, cookie, "removed cookie not found");

    browser.test.notifyPass("cookies");
  }

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

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

</script>

</body>
</html>
