Commit 0213a358 authored by David Bertoni's avatar David Bertoni Committed by Commit Bot

[Extensions] Add chrome.management API tests for Service Workers.

Bug: 1093066
Change-Id: I678072e496d37812aad610fdef4772e7bb5621db
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2408212Reviewed-by: default avatarIstiaque Ahmed <lazyboy@chromium.org>
Commit-Queue: David Bertoni <dbertoni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#810560}
parent d7313471
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include "extensions/browser/extension_registry.h" #include "extensions/browser/extension_registry.h"
#include "extensions/browser/notification_types.h" #include "extensions/browser/notification_types.h"
#include "extensions/common/extension_builder.h" #include "extensions/common/extension_builder.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"
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
...@@ -57,11 +58,45 @@ class ExtensionManagementApiBrowserTest : public ExtensionBrowserTest { ...@@ -57,11 +58,45 @@ class ExtensionManagementApiBrowserTest : public ExtensionBrowserTest {
ScopedInstallVerifierBypassForTest install_verifier_bypass_; ScopedInstallVerifierBypassForTest install_verifier_bypass_;
}; };
using ContextType = ExtensionBrowserTest::ContextType;
class ExtensionManagementApiTestWithBackgroundType
: public ExtensionManagementApiBrowserTest,
public testing::WithParamInterface<ContextType> {
public:
ExtensionManagementApiTestWithBackgroundType() {
// 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>();
}
const Extension* LoadExtensionWithParamFlags(const base::FilePath& path) {
int flags = kFlagEnableFileAccess;
if (GetParam() == ContextType::kServiceWorker)
flags |= ExtensionBrowserTest::kFlagRunAsServiceWorkerBasedExtension;
return LoadExtensionWithFlags(path, flags);
}
private:
std::unique_ptr<extensions::ScopedWorkerBasedExtensionsChannel>
current_channel_;
};
INSTANTIATE_TEST_SUITE_P(PersistentBackground,
ExtensionManagementApiTestWithBackgroundType,
::testing::Values(ContextType::kPersistentBackground));
INSTANTIATE_TEST_SUITE_P(ServiceWorker,
ExtensionManagementApiTestWithBackgroundType,
::testing::Values(ContextType::kServiceWorker));
// We test this here instead of in an ExtensionApiTest because normal extensions // We test this here instead of in an ExtensionApiTest because normal extensions
// are not allowed to call the install function. // are not allowed to call the install function.
IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, InstallEvent) { IN_PROC_BROWSER_TEST_P(ExtensionManagementApiTestWithBackgroundType,
InstallEvent) {
ExtensionTestMessageListener listener1("ready", false); ExtensionTestMessageListener listener1("ready", false);
ASSERT_TRUE(LoadExtension( ASSERT_TRUE(LoadExtensionWithParamFlags(
test_data_dir_.AppendASCII("management/install_event"))); test_data_dir_.AppendASCII("management/install_event")));
ASSERT_TRUE(listener1.WaitUntilSatisfied()); ASSERT_TRUE(listener1.WaitUntilSatisfied());
...@@ -71,14 +106,15 @@ IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, InstallEvent) { ...@@ -71,14 +106,15 @@ IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, InstallEvent) {
ASSERT_TRUE(listener2.WaitUntilSatisfied()); ASSERT_TRUE(listener2.WaitUntilSatisfied());
} }
IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, LaunchApp) { IN_PROC_BROWSER_TEST_P(ExtensionManagementApiTestWithBackgroundType,
LaunchApp) {
ExtensionTestMessageListener listener1("app_launched", false); ExtensionTestMessageListener listener1("app_launched", false);
ExtensionTestMessageListener listener2("got_expected_error", false); ExtensionTestMessageListener listener2("got_expected_error", false);
ASSERT_TRUE(LoadExtension( ASSERT_TRUE(LoadExtension(
test_data_dir_.AppendASCII("management/simple_extension"))); test_data_dir_.AppendASCII("management/simple_extension")));
ASSERT_TRUE(LoadExtension( ASSERT_TRUE(LoadExtension(
test_data_dir_.AppendASCII("management/packaged_app"))); test_data_dir_.AppendASCII("management/packaged_app")));
ASSERT_TRUE(LoadExtension( ASSERT_TRUE(LoadExtensionWithParamFlags(
test_data_dir_.AppendASCII("management/launch_app"))); test_data_dir_.AppendASCII("management/launch_app")));
ASSERT_TRUE(listener1.WaitUntilSatisfied()); ASSERT_TRUE(listener1.WaitUntilSatisfied());
ASSERT_TRUE(listener2.WaitUntilSatisfied()); ASSERT_TRUE(listener2.WaitUntilSatisfied());
...@@ -86,7 +122,7 @@ IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, LaunchApp) { ...@@ -86,7 +122,7 @@ IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, LaunchApp) {
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, IN_PROC_BROWSER_TEST_P(ExtensionManagementApiTestWithBackgroundType,
NoDemoModeAppLaunchSourceReported) { NoDemoModeAppLaunchSourceReported) {
EXPECT_FALSE(chromeos::DemoSession::IsDeviceInDemoMode()); EXPECT_FALSE(chromeos::DemoSession::IsDeviceInDemoMode());
...@@ -105,7 +141,7 @@ IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, ...@@ -105,7 +141,7 @@ IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest,
histogram_tester.ExpectTotalCount("DemoMode.AppLaunchSource", 0); histogram_tester.ExpectTotalCount("DemoMode.AppLaunchSource", 0);
} }
IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, IN_PROC_BROWSER_TEST_P(ExtensionManagementApiTestWithBackgroundType,
DemoModeAppLaunchSourceReported) { DemoModeAppLaunchSourceReported) {
chromeos::DemoSession::SetDemoConfigForTesting( chromeos::DemoSession::SetDemoConfigForTesting(
chromeos::DemoSession::DemoModeConfig::kOnline); chromeos::DemoSession::DemoModeConfig::kOnline);
...@@ -130,7 +166,7 @@ IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, ...@@ -130,7 +166,7 @@ IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest,
#endif #endif
IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, IN_PROC_BROWSER_TEST_P(ExtensionManagementApiTestWithBackgroundType,
LaunchAppFromBackground) { LaunchAppFromBackground) {
ExtensionTestMessageListener listener1("success", false); ExtensionTestMessageListener listener1("success", false);
ASSERT_TRUE(LoadExtension( ASSERT_TRUE(LoadExtension(
...@@ -140,30 +176,30 @@ IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, ...@@ -140,30 +176,30 @@ IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest,
ASSERT_TRUE(listener1.WaitUntilSatisfied()); ASSERT_TRUE(listener1.WaitUntilSatisfied());
} }
IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, IN_PROC_BROWSER_TEST_P(ExtensionManagementApiTestWithBackgroundType,
SelfUninstall) { SelfUninstall) {
ExtensionTestMessageListener listener1("success", false); ExtensionTestMessageListener listener1("success", false);
ASSERT_TRUE(LoadExtension( ASSERT_TRUE(LoadExtensionWithParamFlags(
test_data_dir_.AppendASCII("management/self_uninstall_helper"))); test_data_dir_.AppendASCII("management/self_uninstall_helper")));
ASSERT_TRUE(LoadExtension( ASSERT_TRUE(LoadExtensionWithParamFlags(
test_data_dir_.AppendASCII("management/self_uninstall"))); test_data_dir_.AppendASCII("management/self_uninstall")));
ASSERT_TRUE(listener1.WaitUntilSatisfied()); ASSERT_TRUE(listener1.WaitUntilSatisfied());
} }
IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, IN_PROC_BROWSER_TEST_P(ExtensionManagementApiTestWithBackgroundType,
SelfUninstallNoPermissions) { SelfUninstallNoPermissions) {
ExtensionTestMessageListener listener1("success", false); ExtensionTestMessageListener listener1("success", false);
ASSERT_TRUE(LoadExtension( ASSERT_TRUE(LoadExtensionWithParamFlags(
test_data_dir_.AppendASCII("management/self_uninstall_helper"))); test_data_dir_.AppendASCII("management/self_uninstall_helper")));
ASSERT_TRUE(LoadExtension( ASSERT_TRUE(LoadExtensionWithParamFlags(
test_data_dir_.AppendASCII("management/self_uninstall_noperm"))); test_data_dir_.AppendASCII("management/self_uninstall_noperm")));
ASSERT_TRUE(listener1.WaitUntilSatisfied()); ASSERT_TRUE(listener1.WaitUntilSatisfied());
} }
IN_PROC_BROWSER_TEST_F(ExtensionManagementApiBrowserTest, IN_PROC_BROWSER_TEST_P(ExtensionManagementApiTestWithBackgroundType,
GetSelfNoPermissions) { GetSelfNoPermissions) {
ExtensionTestMessageListener listener1("success", false); ExtensionTestMessageListener listener1("success", false);
ASSERT_TRUE(LoadExtension( ASSERT_TRUE(LoadExtensionWithParamFlags(
test_data_dir_.AppendASCII("management/get_self"))); test_data_dir_.AppendASCII("management/get_self")));
ASSERT_TRUE(listener1.WaitUntilSatisfied()); ASSERT_TRUE(listener1.WaitUntilSatisfied());
} }
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
"version": "1", "version": "1",
"manifest_version": 2, "manifest_version": 2,
"background": { "background": {
"scripts": ["background.js"] "scripts": ["background.js"],
"persistent": true
} }
} }
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
"manifest_version": 2, "manifest_version": 2,
"permissions": ["management"], "permissions": ["management"],
"background": { "background": {
"page": "background.html" "scripts": ["test.js"],
"persistent": true
} }
} }
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
"manifest_version": 2, "manifest_version": 2,
"permissions": ["management"], "permissions": ["management"],
"background": { "background": {
"page": "background.html" "scripts": ["test.js"],
"persistent": true
} }
} }
\ No newline at end of file
...@@ -2,24 +2,21 @@ ...@@ -2,24 +2,21 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
window.onload = function() { chrome.management.getAll(function(items) {
chrome.management.getAll(function(items) { for (var i in items) {
for (var i in items) { var item = items[i];
var item = items[i]; if (item.name == 'packaged_app') {
if (item.name == "packaged_app") { chrome.management.launchApp(item.id);
chrome.management.launchApp(item.id);
}
if (item.name == "simple_extension") {
// Try launching a non-app extension, which should fail.
var expected_error = "Extension " + item.id + " is not an App.";
chrome.management.launchApp(item.id, function() {
if (chrome.runtime.lastError &&
chrome.runtime.lastError.message == expected_error) {
chrome.test.sendMessage("got_expected_error");
}
});
}
} }
}); if (item.name == 'simple_extension') {
}; // Try launching a non-app extension, which should fail.
var expected_error = 'Extension ' + item.id + ' is not an App.';
chrome.management.launchApp(item.id, function() {
if (chrome.runtime.lastError &&
chrome.runtime.lastError.message == expected_error) {
chrome.test.sendMessage('got_expected_error');
}
});
}
}
});
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
"manifest_version": 2, "manifest_version": 2,
"permissions": ["management", "tabs"], "permissions": ["management", "tabs"],
"background": { "background": {
"page": "background.html" "scripts": ["test.js"],
"persistent": true
} }
} }
...@@ -2,17 +2,15 @@ ...@@ -2,17 +2,15 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
window.onload = function() { chrome.management.getAll(function(items) {
chrome.management.getAll(function(items) { for (var i in items) {
for (var i in items) { var item = items[i];
var item = items[i]; if (item.name == 'packaged_app') {
if (item.name == "packaged_app") { launchFromBackground(item.id);
launchFromBackground(item.id); break;
break;
}
} }
}); }
}; });
function launchFromBackground(appId) { function launchFromBackground(appId) {
// Create a new 'popup' window so the last active window isn't 'normal'. // Create a new 'popup' window so the last active window isn't 'normal'.
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
"manifest_version": 2, "manifest_version": 2,
"permissions": ["management"], "permissions": ["management"],
"background": { "background": {
"scripts": ["background.js"] "scripts": ["background.js"],
"persistent": true
} }
} }
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
"manifest_version": 2, "manifest_version": 2,
"permissions": ["management"], "permissions": ["management"],
"background": { "background": {
"scripts": ["background.js"] "scripts": ["background.js"],
"persistent": true
} }
} }
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
"version": "0.1", "version": "0.1",
"manifest_version": 2, "manifest_version": 2,
"background": { "background": {
"scripts": ["background.js"] "scripts": ["background.js"],
"persistent": true
} }
} }
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