1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/browser/background_script_executor.h"
#include "base/test/bind.h"
#include "base/test/values_test_util.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/test/browser_test.h"
#include "extensions/browser/script_executor.h"
#include "extensions/common/extension.h"
#include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/test_extension_dir.h"
namespace extensions {
using BackgroundScriptExecutorBrowserTest = ExtensionBrowserTest;
// Tests the ability to run JS in an extension-registered service worker.
IN_PROC_BROWSER_TEST_F(BackgroundScriptExecutorBrowserTest,
ExecuteScriptInServiceWorker) {
constexpr char kManifest[] =
R"({
"name": "Test",
"manifest_version": 3,
"background": {"service_worker": "background.js"},
"version": "0.1"
})";
constexpr char kBackgroundScript[] =
R"(self.myTestFlag = 'HELLO!';
chrome.test.sendMessage('ready');)";
TestExtensionDir test_dir;
test_dir.WriteManifest(kManifest);
test_dir.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundScript);
ExtensionTestMessageListener listener("ready");
const Extension* extension = LoadExtension(test_dir.UnpackedPath());
ASSERT_TRUE(extension);
ASSERT_TRUE(listener.WaitUntilSatisfied());
{
// Synchronous result.
base::Value value = BackgroundScriptExecutor::ExecuteScript(
profile(), extension->id(), "chrome.test.sendScriptResult(myTestFlag);",
BackgroundScriptExecutor::ResultCapture::kSendScriptResult);
EXPECT_THAT(value, base::test::IsJson(R"("HELLO!")"));
}
{
// Asynchronous result.
static constexpr char kScript[] =
R"(setTimeout(() => {
chrome.test.sendScriptResult(myTestFlag);
});)";
base::Value value = BackgroundScriptExecutor::ExecuteScript(
profile(), extension->id(), kScript,
BackgroundScriptExecutor::ResultCapture::kSendScriptResult);
EXPECT_THAT(value, base::test::IsJson(R"("HELLO!")"));
}
}
#if !BUILDFLAG(IS_ANDROID)
// Only manifest V3 and above is supported on desktop android.
// Tests the ability to run JS in an extension background page.
IN_PROC_BROWSER_TEST_F(BackgroundScriptExecutorBrowserTest,
ExecuteScriptInBackgroundPage) {
constexpr char kManifest[] =
R"({
"name": "Test",
"manifest_version": 2,
"background": {"scripts": ["background.js"]},
"version": "0.1"
})";
constexpr char kBackgroundScript[] = R"(
function createResult() {
return {
testFlag: 'flag',
userGesture: chrome.test.isProcessingUserGesture(),
};
})";
TestExtensionDir test_dir;
test_dir.WriteManifest(kManifest);
test_dir.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundScript);
const Extension* extension = LoadExtension(test_dir.UnpackedPath());
ASSERT_TRUE(extension);
{
// Synchronous result with no user gesture.
// NOTE: This test has to come first. User gestures are timed, so once a
// script executes with a user gesture, it affects subsequent injections
// because the gesture is still considered active.
// (This is okay because we really only need to check once each for user
// gesture; if we wanted to do more involved testing, we'll need to pull
// these two tests apart or otherwise flush the gesture state.)
base::Value value = BackgroundScriptExecutor::ExecuteScript(
profile(), extension->id(),
"chrome.test.sendScriptResult(createResult());",
BackgroundScriptExecutor::ResultCapture::kSendScriptResult,
browsertest_util::ScriptUserActivation::kDontActivate);
EXPECT_THAT(value, base::test::IsJson(
R"({"testFlag":"flag","userGesture":false})"));
}
{
// Synchronous result.
base::Value value = BackgroundScriptExecutor::ExecuteScript(
profile(), extension->id(),
"chrome.test.sendScriptResult(createResult());",
BackgroundScriptExecutor::ResultCapture::kSendScriptResult,
browsertest_util::ScriptUserActivation::kActivate);
EXPECT_THAT(
value, base::test::IsJson(R"({"testFlag":"flag","userGesture":true})"));
}
{
// Asynchronous result with sendScriptResult().
static constexpr char kScript[] =
R"(setTimeout(() => {
chrome.test.sendScriptResult(createResult());
}, 0);)";
base::Value value = BackgroundScriptExecutor::ExecuteScript(
profile(), extension->id(), kScript,
BackgroundScriptExecutor::ResultCapture::kSendScriptResult,
browsertest_util::ScriptUserActivation::kActivate);
EXPECT_THAT(
value, base::test::IsJson(R"({"testFlag":"flag","userGesture":true})"));
}
{
// Asynchronous result with domAutomationController.send().
static constexpr char kScript[] =
R"(setTimeout(() => {
window.domAutomationController.send(createResult());
}, 0);)";
base::Value value = BackgroundScriptExecutor::ExecuteScript(
profile(), extension->id(), kScript,
BackgroundScriptExecutor::ResultCapture::kWindowDomAutomationController,
browsertest_util::ScriptUserActivation::kActivate);
EXPECT_THAT(
value, base::test::IsJson(R"({"testFlag":"flag","userGesture":true})"));
}
}
#endif // !BUILDFLAG(IS_ANDROID)
} // namespace extensions
|