mobile/android/components/extensions/test/mochitest/test_ext_browsingData_cookies_cache.html
author Emilio Cobos Álvarez <emilio@crisal.io>
Sat, 07 Dec 2019 00:07:58 +0000
changeset 505931 8d30b15ca8f932b30ffdf55910fe6e9e2036ca73
parent 476343 fd04e3ff79c48f86b7f4f5ada7fb18a93ed600a1
permissions -rw-r--r--
Bug 1486964 - Disable -moz-touch-enabled in all channels. r=hiro We've had it disabled on Nightly with no fallout for a while, and having it enabled breaks websites with touch screens as described in this bug and related ones. Given the above, let's disable it in all channels, and we'll remove the code in the next cycle. Differential Revision: https://phabricator.services.mozilla.com/D56264

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

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

const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

const COOKIE = {
  host: "example.com",
  name: "test_cookie",
  path: "/",
};
let since, oldCookie;

function addCookie(cookie) {
  let expiry =  Date.now() / 1000 + 10000;
  Services.cookies.add(cookie.host, cookie.path, cookie.name, "test", false, false, false, expiry, {}, Ci.nsICookie.SAMESITE_NONE);
  ok(Services.cookies.cookieExists(cookie.host, cookie.path, cookie.name, {}), `Cookie ${cookie.name} was created.`);
}

async function setUpCookies() {
  // Add a cookie which will end up with an older creationTime.
  oldCookie = Object.assign({}, COOKIE, {name: Date.now()});
  addCookie(oldCookie);
  await new Promise(resolve => setTimeout(resolve, 20));
  since = Date.now();
  await new Promise(resolve => setTimeout(resolve, 10));

  // Add a cookie which will end up with a more recent creationTime.
  addCookie(COOKIE);
}

add_task(async function testCookies() {
  function background() {
    browser.test.onMessage.addListener(async (msg, options) => {
      if (msg == "removeCookies") {
        await browser.browsingData.removeCookies(options);
      } else {
        await browser.browsingData.remove(options, {cookies: true});
      }
      browser.test.sendMessage("cookiesRemoved");
    });
  }

  let extension = ExtensionTestUtils.loadExtension({
    background,
    manifest: {
      permissions: ["browsingData"],
    },
  });

  async function testRemovalMethod(method) {
    // Clear cookies with a recent since value.
    await setUpCookies();
    extension.sendMessage(method, {since});
    await extension.awaitMessage("cookiesRemoved");

    ok(Services.cookies.cookieExists(oldCookie.host, oldCookie.path, oldCookie.name, {}), "Old cookie was not removed.");
    ok(!Services.cookies.cookieExists(COOKIE.host, COOKIE.path, COOKIE.name, {}), "Recent cookie was removed.");

    // Clear cookies with an old since value.
    await setUpCookies();
    addCookie(COOKIE);
    extension.sendMessage(method, {since: since - 100000});
    await extension.awaitMessage("cookiesRemoved");

    ok(!Services.cookies.cookieExists(oldCookie.host, oldCookie.path, oldCookie.name, {}), "Old cookie was removed.");
    ok(!Services.cookies.cookieExists(COOKIE.host, COOKIE.path, COOKIE.name, {}), "Recent cookie was removed.");

    // Clear cookies with no since value and valid originTypes.
    await setUpCookies();
    extension.sendMessage(
      method,
      {originTypes: {unprotectedWeb: true, protectedWeb: false}});
    await extension.awaitMessage("cookiesRemoved");

    ok(!Services.cookies.cookieExists(COOKIE.host, COOKIE.path, COOKIE.name, {}), `Cookie ${COOKIE.name}  was removed.`);
    ok(!Services.cookies.cookieExists(oldCookie.host, oldCookie.path, oldCookie.name, {}), `Cookie ${oldCookie.name}  was removed.`);
  }

  await extension.startup();

  await testRemovalMethod("removeCookies");
  await testRemovalMethod("remove");

  await extension.unload();
});

add_task(async function testCache() {
  function background() {
    browser.test.onMessage.addListener(async msg => {
      if (msg == "removeCache") {
        await browser.browsingData.removeCache({});
      } else {
        await browser.browsingData.remove({}, {cache: true});
      }
      browser.test.sendMessage("cacheRemoved");
    });
  }

  let extension = ExtensionTestUtils.loadExtension({
    background,
    manifest: {
      permissions: ["browsingData"],
    },
  });

  // Returns a promise when 'cacheservice:empty-cache' event is fired
  function topicObserved() {
    return new Promise(resolve => {
      let observe = (subject, topic, data) => {
        Services.obs.removeObserver(observe, "cacheservice:empty-cache");
        resolve(data);
      };
      Services.obs.addObserver(observe, "cacheservice:empty-cache");
    });
  }

  async function testRemovalMethod(method) {
    // We can assume the notification works properly, so we only need to observe
    // the notification to know the cache was cleared.
    let awaitNotification = topicObserved();
    extension.sendMessage(method);
    await awaitNotification;
    await extension.awaitMessage("cacheRemoved");
  }

  await extension.startup();

  await testRemovalMethod("removeCache");
  await testRemovalMethod("remove");

  await extension.unload();
});
</script>

</body>
</html>