Commit 7379178e authored by David Bertoni's avatar David Bertoni Committed by Commit Bot

[Extensions] Add some Alarms API tests for Service Workers.

This CL adds testing for the Alamrs API with Service Worker-
based extensions through parameterized test fixtures.

Bug: 1093066
Change-Id: I86b576d50ec2786c0b042550878141e350d9059d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2250638Reviewed-by: default avatarArchana Simha <archanasimha@chromium.org>
Commit-Queue: David Bertoni <dbertoni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#779503}
parent c3de1340
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include "content/public/test/browser_test.h" #include "content/public/test/browser_test.h"
#include "extensions/browser/event_router.h" #include "extensions/browser/event_router.h"
#include "extensions/common/api/test.h" #include "extensions/common/api/test.h"
#include "extensions/common/scoped_worker_based_extensions_channel.h"
#include "extensions/test/extension_test_message_listener.h" #include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/result_catcher.h" #include "extensions/test/result_catcher.h"
#include "net/dns/mock_host_resolver.h" #include "net/dns/mock_host_resolver.h"
...@@ -15,12 +16,20 @@ using extensions::ResultCatcher; ...@@ -15,12 +16,20 @@ using extensions::ResultCatcher;
namespace extensions { namespace extensions {
class AlarmsApiTest : public ExtensionApiTest { using ContextType = ExtensionApiTest::ContextType;
class AlarmsApiTest : public ExtensionApiTest,
public testing::WithParamInterface<ContextType> {
public: public:
void SetUpOnMainThread() override { void SetUpOnMainThread() override {
ExtensionApiTest::SetUpOnMainThread(); ExtensionApiTest::SetUpOnMainThread();
host_resolver()->AddRule("*", "127.0.0.1"); host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(StartEmbeddedTestServer()); ASSERT_TRUE(StartEmbeddedTestServer());
// Service Workers are currently only available on certain channels, so set
// the channel for those tests.
if (GetParam() == ContextType::kServiceWorker)
current_channel_ = std::make_unique<ScopedWorkerBasedExtensionsChannel>();
} }
static std::unique_ptr<base::ListValue> BuildEventArguments( static std::unique_ptr<base::ListValue> BuildEventArguments(
...@@ -30,11 +39,30 @@ class AlarmsApiTest : public ExtensionApiTest { ...@@ -30,11 +39,30 @@ class AlarmsApiTest : public ExtensionApiTest {
info.last_message = last_message; info.last_message = last_message;
return api::test::OnMessage::Create(info); return api::test::OnMessage::Create(info);
} }
const Extension* LoadAlarmsExtensionIncognito(const char* path) {
int flags = kFlagEnableFileAccess | kFlagEnableIncognito;
if (GetParam() == ContextType::kServiceWorker)
flags |= kFlagRunAsServiceWorkerBasedExtension;
return LoadExtensionWithFlags(
test_data_dir_.AppendASCII("alarms").AppendASCII(path), flags);
}
private:
std::unique_ptr<ScopedWorkerBasedExtensionsChannel> current_channel_;
}; };
INSTANTIATE_TEST_SUITE_P(EventPage,
AlarmsApiTest,
::testing::Values(ContextType::kEventPage));
INSTANTIATE_TEST_SUITE_P(ServiceWorker,
AlarmsApiTest,
::testing::Values(ContextType::kServiceWorker));
// Tests that an alarm created by an extension with incognito split mode is // Tests that an alarm created by an extension with incognito split mode is
// only triggered in the browser context it was created in. // only triggered in the browser context it was created in.
IN_PROC_BROWSER_TEST_F(AlarmsApiTest, IncognitoSplit) { IN_PROC_BROWSER_TEST_P(AlarmsApiTest, IncognitoSplit) {
// We need 2 ResultCatchers because we'll be running the same test in both // We need 2 ResultCatchers because we'll be running the same test in both
// regular and incognito mode. // regular and incognito mode.
Profile* incognito_profile = browser()->profile()->GetOffTheRecordProfile(); Profile* incognito_profile = browser()->profile()->GetOffTheRecordProfile();
...@@ -48,8 +76,7 @@ IN_PROC_BROWSER_TEST_F(AlarmsApiTest, IncognitoSplit) { ...@@ -48,8 +76,7 @@ IN_PROC_BROWSER_TEST_F(AlarmsApiTest, IncognitoSplit) {
ExtensionTestMessageListener listener_incognito("ready: true", false); ExtensionTestMessageListener listener_incognito("ready: true", false);
ASSERT_TRUE(LoadExtensionIncognito( ASSERT_TRUE(LoadAlarmsExtensionIncognito("split"));
test_data_dir_.AppendASCII("alarms").AppendASCII("split")));
EXPECT_TRUE(listener.WaitUntilSatisfied()); EXPECT_TRUE(listener.WaitUntilSatisfied());
EXPECT_TRUE(listener_incognito.WaitUntilSatisfied()); EXPECT_TRUE(listener_incognito.WaitUntilSatisfied());
...@@ -67,12 +94,11 @@ IN_PROC_BROWSER_TEST_F(AlarmsApiTest, IncognitoSplit) { ...@@ -67,12 +94,11 @@ IN_PROC_BROWSER_TEST_F(AlarmsApiTest, IncognitoSplit) {
// Tests that the behavior for an alarm created in incognito context should be // Tests that the behavior for an alarm created in incognito context should be
// the same if incognito is in spanning mode. // the same if incognito is in spanning mode.
IN_PROC_BROWSER_TEST_F(AlarmsApiTest, IncognitoSpanning) { IN_PROC_BROWSER_TEST_P(AlarmsApiTest, IncognitoSpanning) {
ResultCatcher catcher; ResultCatcher catcher;
catcher.RestrictToBrowserContext(browser()->profile()); catcher.RestrictToBrowserContext(browser()->profile());
ASSERT_TRUE(LoadExtensionIncognito( ASSERT_TRUE(LoadAlarmsExtensionIncognito("spanning"));
test_data_dir_.AppendASCII("alarms").AppendASCII("spanning")));
// Open incognito window. // Open incognito window.
OpenURLOffTheRecord(browser()->profile(), GURL("about:blank")); OpenURLOffTheRecord(browser()->profile(), GURL("about:blank"));
......
{ {
"permissions": ["alarms"], "permissions": ["alarms"],
"background": { "background": {
"scripts": ["background.js"] "scripts": ["background.js"],
"persistent": false
}, },
"description": "An extension which triggers an alarm to ensure that when incognito is in split mode onAlarm is only called in the same context the alarm was created in.", "description": "An extension which triggers an alarm to ensure that when incognito is in split mode onAlarm is only called in the same context the alarm was created in.",
"manifest_version": 2, "manifest_version": 2,
...@@ -9,4 +10,3 @@ ...@@ -9,4 +10,3 @@
"name": "Alarm Incognito Split Test Extension", "name": "Alarm Incognito Split Test Extension",
"version": "0.1.1" "version": "0.1.1"
} }
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
"permissions": ["alarms"], "permissions": ["alarms"],
"background": { "background": {
"scripts": ["background.js"], "scripts": ["background.js"],
"persistent": false "persistent": false
}, },
"description": "An extension which triggers an alarm to ensure that when incognito is in split mode onAlarm is only called in the same context the alarm was created in.", "description": "An extension which triggers an alarm to ensure that when incognito is in split mode onAlarm is only called in the same context the alarm was created in.",
"manifest_version": 2, "manifest_version": 2,
......
...@@ -617,6 +617,10 @@ bool ExtensionFunction::HasOptionalArgument(size_t index) { ...@@ -617,6 +617,10 @@ bool ExtensionFunction::HasOptionalArgument(size_t index) {
void ExtensionFunction::WriteToConsole(blink::mojom::ConsoleMessageLevel level, void ExtensionFunction::WriteToConsole(blink::mojom::ConsoleMessageLevel level,
const std::string& message) { const std::string& message) {
// TODO(crbug.com/1096166): Service Worker-based extensions don't have a
// RenderFrameHost.
if (!render_frame_host_)
return;
// Only the main frame handles dev tools messages. // Only the main frame handles dev tools messages.
WebContents::FromRenderFrameHost(render_frame_host_) WebContents::FromRenderFrameHost(render_frame_host_)
->GetMainFrame() ->GetMainFrame()
......
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