Commit 67c29b07 authored by David Bertoni's avatar David Bertoni Committed by Commit Bot

[Extensions] Enable some chrome.runtime tests for Service Workers.

Bug: 1093066
Change-Id: I295f3167a49054acd8498728bbb87981c13390f7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2427033
Auto-Submit: David Bertoni <dbertoni@chromium.org>
Reviewed-by: default avatarIstiaque Ahmed <lazyboy@chromium.org>
Commit-Queue: David Bertoni <dbertoni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#812939}
parent 2a5f3c47
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
// 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.
#include <memory>
#include "base/run_loop.h" #include "base/run_loop.h"
#include "chrome/browser/apps/platform_apps/app_browsertest_util.h" #include "chrome/browser/apps/platform_apps/app_browsertest_util.h"
#include "chrome/browser/extensions/extension_apitest.h" #include "chrome/browser/extensions/extension_apitest.h"
...@@ -13,9 +15,11 @@ ...@@ -13,9 +15,11 @@
#include "extensions/browser/api/runtime/runtime_api.h" #include "extensions/browser/api/runtime/runtime_api.h"
#include "extensions/browser/blocklist_state.h" #include "extensions/browser/blocklist_state.h"
#include "extensions/browser/extension_dialog_auto_confirm.h" #include "extensions/browser/extension_dialog_auto_confirm.h"
#include "extensions/browser/extension_function.h"
#include "extensions/browser/extension_prefs.h" #include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h" #include "extensions/browser/extension_registry.h"
#include "extensions/browser/test_extension_registry_observer.h" #include "extensions/browser/test_extension_registry_observer.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 "extensions/test/test_extension_dir.h" #include "extensions/test/test_extension_dir.h"
...@@ -24,13 +28,57 @@ ...@@ -24,13 +28,57 @@
namespace extensions { namespace extensions {
using ContextType = ExtensionBrowserTest::ContextType;
class RuntimeApiTest : public ExtensionApiTest,
public testing::WithParamInterface<ContextType> {
public:
RuntimeApiTest() {
// 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>();
}
RuntimeApiTest(const RuntimeApiTest&) = delete;
RuntimeApiTest& operator=(const RuntimeApiTest&) = delete;
const Extension* LoadExtensionWithParamFlag(const base::FilePath& path) {
int flags = kFlagEnableFileAccess;
if (GetParam() == ContextType::kServiceWorker)
flags |= ExtensionBrowserTest::kFlagRunAsServiceWorkerBasedExtension;
return LoadExtensionWithFlags(path, flags);
}
bool RunTestWithParamFlag(const std::string& extension_name) {
int flags = kFlagEnableFileAccess;
if (GetParam() == ContextType::kServiceWorker)
flags |= ExtensionBrowserTest::kFlagRunAsServiceWorkerBasedExtension;
return RunExtensionTestWithFlags(extension_name, flags, kFlagNone);
}
private:
std::unique_ptr<extensions::ScopedWorkerBasedExtensionsChannel>
current_channel_;
};
INSTANTIATE_TEST_SUITE_P(PersistentBackground,
RuntimeApiTest,
::testing::Values(ContextType::kPersistentBackground));
INSTANTIATE_TEST_SUITE_P(ServiceWorker,
RuntimeApiTest,
::testing::Values(ContextType::kServiceWorker));
// Tests the privileged components of chrome.runtime. // Tests the privileged components of chrome.runtime.
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ChromeRuntimePrivileged) { IN_PROC_BROWSER_TEST_P(RuntimeApiTest, ChromeRuntimePrivileged) {
ASSERT_TRUE(RunExtensionTest("runtime/privileged")) << message_; ASSERT_TRUE(RunTestWithParamFlag("runtime/privileged")) << message_;
} }
// Tests the unprivileged components of chrome.runtime. // Tests the unprivileged components of chrome.runtime.
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ChromeRuntimeUnprivileged) { IN_PROC_BROWSER_TEST_P(RuntimeApiTest, ChromeRuntimeUnprivileged) {
ASSERT_TRUE(StartEmbeddedTestServer()); ASSERT_TRUE(StartEmbeddedTestServer());
ASSERT_TRUE( ASSERT_TRUE(
LoadExtension(test_data_dir_.AppendASCII("runtime/content_script"))); LoadExtension(test_data_dir_.AppendASCII("runtime/content_script")));
...@@ -42,14 +90,29 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ChromeRuntimeUnprivileged) { ...@@ -42,14 +90,29 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ChromeRuntimeUnprivileged) {
EXPECT_TRUE(catcher.GetNextResult()) << message_; EXPECT_TRUE(catcher.GetNextResult()) << message_;
} }
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ChromeRuntimeUninstallURL) { IN_PROC_BROWSER_TEST_P(RuntimeApiTest, ChromeRuntimeUninstallURL) {
// TODO(https://crbug.com/977629): Currently, chrome.test.runWithUserGesture()
// doesn't support Service Worker-based extensions, so this is a workaround.
using ScopedUserGestureForTests =
ExtensionFunction::ScopedUserGestureForTests;
std::unique_ptr<ScopedUserGestureForTests> scoped_user_gesture;
if (GetParam() == ContextType::kServiceWorker)
scoped_user_gesture = std::make_unique<ScopedUserGestureForTests>();
// Auto-confirm the uninstall dialog. // Auto-confirm the uninstall dialog.
extensions::ScopedTestDialogAutoConfirm auto_confirm( extensions::ScopedTestDialogAutoConfirm auto_confirm(
extensions::ScopedTestDialogAutoConfirm::ACCEPT); extensions::ScopedTestDialogAutoConfirm::ACCEPT);
ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("runtime") ExtensionTestMessageListener ready_listener("ready", false);
.AppendASCII("uninstall_url") ASSERT_TRUE(
.AppendASCII("sets_uninstall_url"))); LoadExtensionWithParamFlag(test_data_dir_.AppendASCII("runtime")
ASSERT_TRUE(RunExtensionTest("runtime/uninstall_url")) << message_; .AppendASCII("uninstall_url")
.AppendASCII("sets_uninstall_url")));
EXPECT_TRUE(ready_listener.WaitUntilSatisfied());
ASSERT_TRUE(RunTestWithParamFlag("runtime/uninstall_url")) << message_;
}
IN_PROC_BROWSER_TEST_P(RuntimeApiTest, GetPlatformInfo) {
ASSERT_TRUE(RunTestWithParamFlag("runtime/get_platform_info")) << message_;
} }
namespace { namespace {
...@@ -283,14 +346,15 @@ IN_PROC_BROWSER_TEST_F(RuntimeAPIUpdateTest, ...@@ -283,14 +346,15 @@ IN_PROC_BROWSER_TEST_F(RuntimeAPIUpdateTest,
// Tests that when a blocklisted extension with a set uninstall url is // Tests that when a blocklisted extension with a set uninstall url is
// uninstalled, its uninstall url does not open. // uninstalled, its uninstall url does not open.
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, IN_PROC_BROWSER_TEST_P(RuntimeApiTest,
DoNotOpenUninstallUrlForBlocklistedExtensions) { DoNotOpenUninstallUrlForBlocklistedExtensions) {
ExtensionTestMessageListener ready_listener("ready", false);
// Load an extension that has set an uninstall url. // Load an extension that has set an uninstall url.
scoped_refptr<const extensions::Extension> extension = scoped_refptr<const extensions::Extension> extension =
LoadExtension(test_data_dir_.AppendASCII("runtime") LoadExtensionWithParamFlag(test_data_dir_.AppendASCII("runtime")
.AppendASCII("uninstall_url") .AppendASCII("uninstall_url")
.AppendASCII("sets_uninstall_url")); .AppendASCII("sets_uninstall_url"));
EXPECT_TRUE(ready_listener.WaitUntilSatisfied());
ASSERT_TRUE(extension.get()); ASSERT_TRUE(extension.get());
extension_service()->AddExtension(extension.get()); extension_service()->AddExtension(extension.get());
ASSERT_TRUE(extension_service()->IsExtensionEnabled(extension->id())); ASSERT_TRUE(extension_service()->IsExtensionEnabled(extension->id()));
...@@ -311,9 +375,12 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ...@@ -311,9 +375,12 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest,
EXPECT_EQ("about:blank", GetActiveUrl(browser())); EXPECT_EQ("about:blank", GetActiveUrl(browser()));
// Load the same extension again, except blocklist it after installation. // Load the same extension again, except blocklist it after installation.
extension = LoadExtension(test_data_dir_.AppendASCII("runtime") ExtensionTestMessageListener ready_listener_reload("ready", false);
.AppendASCII("uninstall_url") extension =
.AppendASCII("sets_uninstall_url")); LoadExtensionWithParamFlag(test_data_dir_.AppendASCII("runtime")
.AppendASCII("uninstall_url")
.AppendASCII("sets_uninstall_url"));
EXPECT_TRUE(ready_listener_reload.WaitUntilSatisfied());
extension_service()->AddExtension(extension.get()); extension_service()->AddExtension(extension.get());
ASSERT_TRUE(extension_service()->IsExtensionEnabled(extension->id())); ASSERT_TRUE(extension_service()->IsExtensionEnabled(extension->id()));
......
{
"name": "chrome.runtime.getPlatformInfo API Test",
"version": "1",
"manifest_version": 2,
"background": {
"scripts": ["test.js"],
"persistent": true
}
}
// 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.
// These are found here:
// https://developer.chrome.com/extensions/runtime#type-PlatformOs
let platformOsList = ['mac', 'win', 'android', 'cros', 'linux', 'openbsd'];
let platformArchList = ['arm', 'arm64', 'x86-32', 'x86-64', 'mips', 'mips64'];
let platformNaclArchList = ['arm', 'x86-32', 'x86-64', 'mips', 'mips64'];
chrome.test.runTests([
function testGetPlatformInfo() {
chrome.runtime.getPlatformInfo(function(platformInfo) {
// It would be possible for the C++ side of the test to assemble
// the expected information and supplied it as a custom param, but
// it should be enough to assert that the values are within those
// expected.
chrome.test.assertTrue(platformOsList.includes(platformInfo.os));
chrome.test.assertTrue(platformArchList.includes(platformInfo.arch));
chrome.test.assertTrue(
platformNaclArchList.includes(platformInfo.nacl_arch));
chrome.test.succeed();
});
},
]);
{ {
"name": "chrome.runtime API Test", "name": "chrome.runtime API Test",
// iegclhlplifhodhkoafiokenjoapiobj
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjzv7dI7Ygyh67VHE1DdidudpYf8PFfv8iucWvzO+3xpF/Dm5xNo7aQhPNiEaNfHwJQ7lsp4gc+C+4bbaVewBFspTruoSJhZc5uEfqxwovJwN+v1/SUFXTXQmQBv6gs0qZB4gBbl4caNQBlqrFwAMNisnu1V6UROna8rOJQ90D7Nv7TCwoVPKBfVshpFjdDOTeBg4iLctO3S/06QYqaTDrwVceSyHkVkvzBY6tc6mnYX0RZu78J9iL8bdqwfllOhs69cqoHHgrLdI6JdOyiuh6pBP6vxMlzSKWJ3YTNjaQTPwfOYaLMuzdl0v+YdzafIzV9zwe4Xiskk+5JNGt8b2rQIDAQAB",
"version": "1", "version": "1",
"manifest_version": 2, "manifest_version": 2,
"background": { "background": {
"scripts": ["test.js"] "scripts": ["test.js"],
"persistent": true
} }
} }
...@@ -7,6 +7,10 @@ var assertTrue = chrome.test.assertTrue; ...@@ -7,6 +7,10 @@ var assertTrue = chrome.test.assertTrue;
var fail = chrome.test.fail; var fail = chrome.test.fail;
var succeed = chrome.test.succeed; var succeed = chrome.test.succeed;
const isServiceWorker = ('ServiceWorkerGlobalScope' in self);
const extensionId = 'iegclhlplifhodhkoafiokenjoapiobj';
const serviceWorkerScriptName = 'generated_service_worker__.js';
function checkIsDefined(prop) { function checkIsDefined(prop) {
if (!chrome.runtime) { if (!chrome.runtime) {
fail('chrome.runtime is not defined'); fail('chrome.runtime is not defined');
...@@ -19,13 +23,29 @@ function checkIsDefined(prop) { ...@@ -19,13 +23,29 @@ function checkIsDefined(prop) {
return true; return true;
} }
function getLocation() {
return isServiceWorker ? self.location.toString() : window.location.href;
};
function getPath() {
return isServiceWorker ? '/' + serviceWorkerScriptName
: '/_generated_background_page.html';
};
chrome.test.runTests([ chrome.test.runTests([
function testID() {
if (!checkIsDefined('id'))
return;
assertEq(extensionId, chrome.runtime.id);
succeed();
},
function testGetURL() { function testGetURL() {
if (!checkIsDefined('getURL')) if (!checkIsDefined('getURL'))
return; return;
var url = chrome.runtime.getURL('_generated_background_page.html'); assertEq('chrome-extension://' + chrome.runtime.id + getPath(),
assertEq(url, window.location.href); getLocation());
succeed(); succeed();
}, },
...@@ -33,25 +53,20 @@ chrome.test.runTests([ ...@@ -33,25 +53,20 @@ chrome.test.runTests([
if (!checkIsDefined('getManifest')) if (!checkIsDefined('getManifest'))
return; return;
var manifest = chrome.runtime.getManifest(); var manifest = chrome.runtime.getManifest();
if (!manifest || !manifest.background || !manifest.background.scripts) { if (!manifest || !manifest.background ||
fail(); !(manifest.background.scripts || manifest.background.service_worker)) {
fail('Extension has no background or worker script.');
return; return;
} }
assertEq(manifest.name, 'chrome.runtime API Test'); assertEq('chrome.runtime API Test', manifest.name);
assertEq(manifest.version, '1'); assertEq('1', manifest.version);
assertEq(manifest.manifest_version, 2); assertEq(2, manifest.manifest_version);
assertEq(manifest.background.scripts, ['test.js']); if (manifest.background.scripts) {
assertEq(['test.js'], manifest.background.scripts);
} else {
assertEq(serviceWorkerScriptName, manifest.background.service_worker);
}
succeed(); succeed();
}, },
function testID() {
if (!checkIsDefined('id'))
return;
// We *could* get the browser to tell the test what the extension ID is,
// and compare against that. It's a pain. Testing for a non-empty ID should
// be good enough.
assertTrue(chrome.runtime.id != '', 'chrome.runtime.id is empty-string.');
succeed();
}
]); ]);
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
"<all_urls>" "<all_urls>"
], ],
"background": { "background": {
"scripts": ["test.js"] "scripts": ["test.js"],
"persistent": true
} }
} }
...@@ -3,3 +3,5 @@ ...@@ -3,3 +3,5 @@
// found in the LICENSE file. // found in the LICENSE file.
chrome.runtime.setUninstallURL("http://www.google.com"); chrome.runtime.setUninstallURL("http://www.google.com");
chrome.test.sendMessage('ready');
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
"manifest_version": 2, "manifest_version": 2,
"description": "Test extension that sets an uninstall URL.", "description": "Test extension that sets an uninstall URL.",
"background": { "background": {
"scripts": ["bg.js"] "scripts": ["bg.js"],
"persistent": true
} }
} }
...@@ -17,9 +17,9 @@ chrome.test.runTests([ ...@@ -17,9 +17,9 @@ chrome.test.runTests([
chrome.test.runWithUserGesture(pass(function() { chrome.test.runWithUserGesture(pass(function() {
chrome.management.uninstall(results[i].id, pass(function() { chrome.management.uninstall(results[i].id, pass(function() {
chrome.tabs.query({'url': uninstall_url}, pass(function(tabs) { chrome.tabs.query({'url': uninstall_url}, pass(function(tabs) {
chrome.test.assertEq(tabs.length, 1); chrome.test.assertEq(1, tabs.length);
chrome.test.assertEq(tabs[0].pendingUrl || tabs[0].url, chrome.test.assertEq(uninstall_url,
uninstall_url); tabs[0].pendingUrl || tabs[0].url);
})); }));
})); }));
})); }));
......
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