Commit 8764d28d authored by David Bertoni's avatar David Bertoni Committed by Commit Bot

[Extensions] Refactor a Cookies API test.

This CL splits an existing Cookies API test into two separate tests,
which makes the bulk of the original test compatible with a Service
Worker-based extension. It also adds that test to the other Cookies
API tests that are run for both event page-based and Service Worker-
based extensions.

Bug: 1093066
Change-Id: If4ee1326a73a1c485d126bab54b2516f2892bc4d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2314876
Commit-Queue: David Bertoni <dbertoni@chromium.org>
Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#791356}
parent 9982e8f3
......@@ -13,16 +13,10 @@ namespace extensions {
using ContextType = ExtensionApiTest::ContextType;
// TODO(crbug.com/1093066): This test uses the DOM to set and
// check cookies for one test. Figure out how to isolate that
// test and adapt the rest of it for a SW-based extension.
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Cookies) {
ASSERT_TRUE(RunExtensionTestWithArg(
"cookies/api",
net::cookie_util::IsCookiesWithoutSameSiteMustBeSecureEnabled()
? "true"
: "false"))
<< message_;
// This test cannot be run by a Service Worked-based extension
// because it uses the Document object.
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ReadFromDocument) {
ASSERT_TRUE(RunExtensionTest("cookies/read_from_doc")) << message_;
}
class CookiesApiTest : public ExtensionApiTest,
......@@ -45,6 +39,16 @@ class CookiesApiTest : public ExtensionApiTest,
return RunTestWithFlags(extension_name, kFlagEnableIncognito);
}
bool RunTestWithArg(const std::string& extension_name,
const char* custom_arg) {
int browser_test_flags = kFlagNone;
if (GetParam() == ContextType::kServiceWorker)
browser_test_flags |= kFlagRunAsServiceWorkerBasedExtension;
return RunExtensionTestWithFlagsAndArg(extension_name, custom_arg,
browser_test_flags, kFlagNone);
}
bool RunTestWithFlags(const std::string& extension_name,
int browser_test_flags) {
if (GetParam() == ContextType::kServiceWorker)
......@@ -64,6 +68,15 @@ INSTANTIATE_TEST_SUITE_P(ServiceWorker,
CookiesApiTest,
::testing::Values(ContextType::kServiceWorker));
IN_PROC_BROWSER_TEST_P(CookiesApiTest, Cookies) {
ASSERT_TRUE(RunTestWithArg(
"cookies/api",
net::cookie_util::IsCookiesWithoutSameSiteMustBeSecureEnabled()
? "true"
: "false"))
<< message_;
}
IN_PROC_BROWSER_TEST_P(CookiesApiTest, CookiesEvents) {
ASSERT_TRUE(RunTest("cookies/events")) << message_;
}
......
......@@ -4,11 +4,11 @@
"manifest_version": 2,
"description": "end-to-end browser test for chrome.experimental.cookies API",
"background": {
"scripts": ["background.js"]
"scripts": ["background.js"],
"persistent": false
},
"permissions": [
"cookies",
"tabs",
"http://*.chrome_extensions.cookies.com/",
"http://chromium.cookies.com/",
"https://*.cookies.com/",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Ensure we started with an empty profile.
chrome.test.assertEq(document.cookie, "");
// Set some cookies with a few different modifiers.
var expire = new Date();
expire.setDate(expire.getDate() + 1); // tomorrow
document.cookie = "a=1";
document.cookie = "b=2; path=/; domain=" + location.host;
document.cookie = "c=3; path=/; expires=" + expire +
"; domain=" + location.host;
// Open a tab. This doesn't really prove we're writing to disk, but it is
// difficult to prove that without shutting down the process.
chrome.tabs.create({url: "tab.html"});
{
"name": "chrome.cookies.invalid_schemes",
"version": "0.1",
"manifest_version": 2,
"description": "Tests Cookies API.",
"background": {
"scripts": ["background.js"]
},
"permissions": ["cookies"]
}
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
function readCookie(name) {
var nameEQ = name + '=';
var cookies = document.cookie.split(';');
for(var i=0; i < cookies.length; i++) {
var c = cookies[i];
while (c.charAt(0)==' ') {
c = c.substring(1);
}
if (c.indexOf(nameEQ) == 0) {
return c.substring(nameEQ.length);
}
}
return null;
}
chrome.test.runTests([
function readCookiesFromDoc() {
chrome.test.assertEq('1', readCookie('a'));
chrome.test.assertEq('2', readCookie('b'));
chrome.test.assertEq('3', readCookie('c'));
chrome.test.assertEq(null, readCookie('nonexistent'));
chrome.test.succeed();
},
]);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment