Skip to content

Commit

Permalink
[Extensions] Rename functions to indicate they're deprecated.
Browse files Browse the repository at this point in the history
This CL adds the "Deprecated" suffix to two functions:

1. browsertest_util::ExecuteScriptInBackgroundPage
2. ExtensionBrowserTest::ExecuteScriptInBackgroundPage

These functions use the domAutomationController to send script
results instead of the new chrome.test.sendScriptResult function.
This CL also introduces two new functions with the old names that
use the new function, along with a new browser test.

In the case where it's trivial to update the script that's being
executed to use the new function, that's been done in this CL. In
non-trivial cases, that will happen in follow-on CLs.

Bug: 1093066
AX-Relnotes: n/a.
Change-Id: I1ee280d9bcaa2f23b0354e5c7a50e89833215fc4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4330572
Reviewed-by: Anastasia Helfinstein <anastasi@google.com>
Commit-Queue: Edman Anjos <edman@chromium.org>
Reviewed-by: Dominick Ng <dominickn@chromium.org>
Reviewed-by: Jiewei Qian <qjw@chromium.org>
Reviewed-by: Yury Khmel <khmel@chromium.org>
Auto-Submit: David Bertoni <dbertoni@chromium.org>
Reviewed-by: Edman Anjos <edman@chromium.org>
Reviewed-by: Adam Rice <ricea@chromium.org>
Reviewed-by: Devlin Cronin <rdevlin.cronin@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1122332}
  • Loading branch information
David Bertoni authored and Chromium LUCI CQ committed Mar 27, 2023
1 parent bb233dc commit 181bfc5
Show file tree
Hide file tree
Showing 32 changed files with 391 additions and 259 deletions.
4 changes: 2 additions & 2 deletions chrome/browser/apps/guest_view/web_view_browsertest.cc
Expand Up @@ -5540,9 +5540,9 @@ IN_PROC_BROWSER_TEST_F(GuestViewExtensionNameCollisionTest,
ASSERT_TRUE(loaded_listener.WaitUntilSatisfied());

const std::string script =
"window.domAutomationController.send("
"chrome.test.sendScriptResult("
" window.testPassed ? 'PASSED' : 'FAILED');";
const std::string test_passed =
const base::Value test_passed =
ExecuteScriptInBackgroundPage(extension->id(), script);
EXPECT_EQ("PASSED", test_passed);
}
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/ash/accessibility/autoclick_browsertest.cc
Expand Up @@ -106,11 +106,11 @@ class AutoclickBrowserTest : public InProcessBrowserTest {
(async function() {
window.accessibilityCommon.setFeatureLoadCallbackForTest('autoclick',
() => {
window.domAutomationController.send('ready');
chrome.test.sendScriptResult('ready');
});
})();
)JS");
std::string result =
base::Value result =
extensions::browsertest_util::ExecuteScriptInBackgroundPage(
browser()->profile(),
extension_misc::kAccessibilityCommonExtensionId, script);
Expand Down
9 changes: 5 additions & 4 deletions chrome/browser/ash/accessibility/dictation_browsertest.cc
Expand Up @@ -545,10 +545,11 @@ class DictationTestBase : public InProcessBrowserTest,
}

std::string ExecuteAccessibilityCommonScript(const std::string& script) {
return extensions::browsertest_util::ExecuteScriptInBackgroundPage(
/*context=*/browser()->profile(),
/*extension_id=*/extension_misc::kAccessibilityCommonExtensionId,
/*script=*/script);
return extensions::browsertest_util::
ExecuteScriptInBackgroundPageDeprecated(
/*context=*/browser()->profile(),
/*extension_id=*/extension_misc::kAccessibilityCommonExtensionId,
/*script=*/script);
}

std::string GetClipboardText() {
Expand Down
Expand Up @@ -124,11 +124,11 @@ class FullscreenMagnifierControllerTest : public InProcessBrowserTest {
() => {
window.accessibilityCommon.magnifier_.setIsInitializingForTest(
false);
window.domAutomationController.send('ready');
chrome.test.sendScriptResult('ready');
});
})();
)JS");
std::string result =
base::Value result =
extensions::browsertest_util::ExecuteScriptInBackgroundPage(
browser()->profile(),
extension_misc::kAccessibilityCommonExtensionId, script);
Expand Down
12 changes: 2 additions & 10 deletions chrome/browser/ash/accessibility/select_to_speak_browsertest.cc
Expand Up @@ -196,13 +196,6 @@ class SelectToSpeakTest : public InProcessBrowserTest {
return weak_ptr_factory_.GetWeakPtr();
}

void RunJavaScriptInSelectToSpeakBackgroundPage(const std::string& script) {
extensions::browsertest_util::ExecuteScriptInBackgroundPage(
/*context=*/browser()->profile(),
/*extension_id=*/extension_misc::kSelectToSpeakExtensionId,
/*script=*/script);
}

content::WebContents* GetWebContents() {
return browser()->tab_strip_model()->GetActiveWebContents();
}
Expand All @@ -218,21 +211,20 @@ class SelectToSpeakTest : public InProcessBrowserTest {
(async function() {
let module = await import('./select_to_speak_main.js');
module.selectToSpeak.setOnLoadDesktopCallbackForTest(() => {
window.domAutomationController.send('ready');
chrome.test.sendScriptResult('ready');
});
// Set enhanced network voices dialog as shown, because the pref
// change takes some time to propagate.
module.selectToSpeak.prefsManager_.enhancedVoicesDialogShown_ = true;
})();
)JS");
std::string result =
base::Value result =
extensions::browsertest_util::ExecuteScriptInBackgroundPage(
browser()->profile(), extension_misc::kSelectToSpeakExtensionId,
script);
ASSERT_EQ("ready", result);
}

base::test::ScopedFeatureList scoped_feature_list_;
std::unique_ptr<base::RunLoop> loop_runner_;
std::unique_ptr<base::RunLoop> highlights_runner_;
std::unique_ptr<base::RunLoop> tray_loop_runner_;
Expand Down
Expand Up @@ -180,7 +180,7 @@ void LoggedInSpokenFeedbackTest::DisableEarcons() {

void LoggedInSpokenFeedbackTest::ImportJSModuleForChromeVox(std::string name,
std::string path) {
extensions::browsertest_util::ExecuteScriptInBackgroundPage(
extensions::browsertest_util::ExecuteScriptInBackgroundPageDeprecated(
browser()->profile(), extension_misc::kChromeVoxExtensionId,
"import('" + path +
"').then(mod => {"
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/ash/accessibility/switch_access_browsertest.cc
Expand Up @@ -94,7 +94,7 @@ class SwitchAccessTest : public InProcessBrowserTest {
<< test_support_path;

std::string result =
extensions::browsertest_util::ExecuteScriptInBackgroundPage(
extensions::browsertest_util::ExecuteScriptInBackgroundPageDeprecated(
browser()->profile(), extension_misc::kSwitchAccessExtensionId,
script);
ASSERT_EQ("ready", result);
Expand All @@ -103,7 +103,7 @@ class SwitchAccessTest : public InProcessBrowserTest {
// Run js snippet and wait for it to finish.
void WaitForJS(const std::string& js_to_eval) {
std::string result =
extensions::browsertest_util::ExecuteScriptInBackgroundPage(
extensions::browsertest_util::ExecuteScriptInBackgroundPageDeprecated(
browser()->profile(), extension_misc::kSwitchAccessExtensionId,
js_to_eval,
extensions::browsertest_util::ScriptUserActivation::kDontActivate);
Expand Down
Expand Up @@ -690,19 +690,19 @@ IN_PROC_BROWSER_TEST_F(
// Extension should be loaded only once.
EXPECT_EQ(ready_observer.fired_times(), 1);

constexpr char kSetInStorageAPI[] =
static constexpr char kSetInStorageAPI[] =
R"(chrome.storage.local.set(
{test: 'testValue'},
() => { domAutomationController.send('') });)";
() => { chrome.test.sendScriptResult('') });)";
// Store some data using Storage API for the extension.
extensions::browsertest_util::ExecuteScriptInBackgroundPage(
app_profile, extension_misc::kChromeVoxExtensionId, kSetInStorageAPI);

// Expect the data to be saved.
constexpr char kGetFromStorageAPI[] =
static constexpr char kGetFromStorageAPI[] =
R"(chrome.storage.local.get(
'test',
(value) => domAutomationController.send(value.test));)";
(value) => chrome.test.sendScriptResult(value.test));)";
EXPECT_EQ("testValue",
extensions::browsertest_util::ExecuteScriptInBackgroundPage(
app_profile, extension_misc::kChromeVoxExtensionId,
Expand Down Expand Up @@ -746,12 +746,12 @@ IN_PROC_BROWSER_TEST_F(
EXPECT_EQ(ready_observer.fired_times(), 1);

// Expect the data to be cleared.
constexpr char kGetFromStorageAPI[] =
static constexpr char kGetFromStorageAPI[] =
R"(
chrome.storage.local.get(
"test",
function(value) {
domAutomationController.send(value.test == undefined ?
chrome.test.sendScriptResult(value.test == undefined ?
"<none>" : value.test);
}
);
Expand All @@ -761,9 +761,9 @@ IN_PROC_BROWSER_TEST_F(
app_profile, extension_misc::kChromeVoxExtensionId,
kGetFromStorageAPI));

constexpr char kGetFromLocalStorage[] =
static constexpr char kGetFromLocalStorage[] =
R"(
domAutomationController.send(
chrome.test.sendScriptResult(
localStorage.getItem('test2') == undefined ?
"<none>" : localStorage.getItem('test2'));
)";
Expand Down
Expand Up @@ -1839,7 +1839,7 @@ void SystemWebAppAccessibilityTest::EnableChromeVox() {
AccessibilityManager::Get()->EnableSpokenFeedback(true);
speech_monitor_.ExpectSpeechPattern("*");
speech_monitor_.Call([this]() {
extensions::browsertest_util::ExecuteScriptInBackgroundPage(
extensions::browsertest_util::ExecuteScriptInBackgroundPageDeprecated(
browser()->profile(), extension_misc::kChromeVoxExtensionId, R"JS(
import('/chromevox/background/chromevox_state.js').then(
module => module.ChromeVoxState.ready().then(() =>
Expand Down
25 changes: 13 additions & 12 deletions chrome/browser/extensions/active_tab_apitest.cc
Expand Up @@ -164,13 +164,13 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, FileURLs) {
EXPECT_TRUE(background_page_ready.WaitUntilSatisfied());

auto can_xhr_file_urls = [this, &extension_id]() {
constexpr char script[] = R"(
static constexpr char script[] = R"(
var req = new XMLHttpRequest();
var url = '%s';
req.open('GET', url, true);
req.onload = function() {
if (req.responseText === 'Hello!')
window.domAutomationController.send('true');
chrome.test.sendScriptResult('true');
// Even for a successful request, the status code might be 0. Ensure
// that onloadend is not subsequently called if the request is
Expand All @@ -183,20 +183,20 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, FileURLs) {
// 'error' event).
req.onloadend = function() {
if (req.status === 0)
window.domAutomationController.send('false');
chrome.test.sendScriptResult('false');
};
req.send();
)";

base::FilePath test_file =
test_data_dir_.DirName().AppendASCII("test_file.txt");
std::string result = ExecuteScriptInBackgroundPage(
base::Value result = ExecuteScriptInBackgroundPage(
extension_id,
base::StringPrintf(script,
net::FilePathToFileURL(test_file).spec().c_str()));

EXPECT_TRUE(result == "true" || result == "false");
return result == "true";
EXPECT_TRUE(result.is_string() && (result == "true" || result == "false"));
return result.is_string() && result == "true";
};

auto can_load_file_iframe = [this, &extension_id]() {
Expand Down Expand Up @@ -230,7 +230,7 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, FileURLs) {
};

auto can_script_tab = [this, &extension_id](int tab_id) {
constexpr char script[] = R"(
static constexpr char script[] = R"(
var tabID = %d;
chrome.tabs.executeScript(
tabID, {code: 'console.log("injected");'}, function() {
Expand All @@ -240,19 +240,20 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, FileURLs) {
if (chrome.runtime.lastError &&
expectedError != chrome.runtime.lastError.message) {
window.domAutomationController.send(
chrome.test.sendScriptResult(
'unexpected error: ' + chrome.runtime.lastError.message);
} else {
window.domAutomationController.send(
chrome.test.sendScriptResult(
chrome.runtime.lastError ? 'false' : 'true');
}
});
)";

std::string result = ExecuteScriptInBackgroundPage(
base::Value result = ExecuteScriptInBackgroundPage(
extension_id, base::StringPrintf(script, tab_id));
EXPECT_TRUE(result == "true" || result == "false") << result;
return result == "true";
EXPECT_TRUE(result.is_string());
EXPECT_TRUE(result == "true" || result == "false");
return result.is_string() && result == "true";
};

auto get_active_tab_id = [this]() {
Expand Down
Expand Up @@ -51,12 +51,12 @@ class CrashReportPrivateApiTest : public ExtensionApiTest {
void SetUpOnMainThread() override {
ExtensionApiTest::SetUpOnMainThread();

constexpr char kKey[] =
static constexpr char kKey[] =
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+uU63MD6T82Ldq5wjrDFn5mGmPnnnj"
"WZBWxYXfpG4kVf0s+p24VkXwTXsxeI12bRm8/ft9sOq0XiLfgQEh5JrVUZqvFlaZYoS+g"
"iZfUqzKFGMLa4uiSMDnvv+byxrqAepKz5G8XX/q5Wm5cvpdjwgiu9z9iM768xJy+Ca/G5"
"qQwIDAQAB";
constexpr char kManifestTemplate[] =
static constexpr char kManifestTemplate[] =
R"({
"key": "%s",
"name": "chrome.crashReportPrivate basic extension tests",
Expand Down Expand Up @@ -97,12 +97,12 @@ class CrashReportPrivateApiTest : public ExtensionApiTest {
};

IN_PROC_BROWSER_TEST_F(CrashReportPrivateApiTest, Basic) {
constexpr char kTestScript[] = R"(
static constexpr char kTestScript[] = R"(
chrome.crashReportPrivate.reportError({
message: "hi",
url: "http://www.test.com",
},
() => window.domAutomationController.send(""));
() => chrome.test.sendScriptResult(""));
)";
ExecuteScriptInBackgroundPage(extension_->id(), kTestScript);

Expand All @@ -126,7 +126,7 @@ IN_PROC_BROWSER_TEST_F(CrashReportPrivateApiTest, Basic) {
}

IN_PROC_BROWSER_TEST_F(CrashReportPrivateApiTest, ExtraParamsAndStackTrace) {
constexpr char kTestScript[] = R"-(
static constexpr char kTestScript[] = R"-(
chrome.crashReportPrivate.reportError({
message: "hi",
url: "http://www.test.com/foo",
Expand All @@ -137,7 +137,7 @@ IN_PROC_BROWSER_TEST_F(CrashReportPrivateApiTest, ExtraParamsAndStackTrace) {
debugId: "2751679EE:233977D75E03BAC9DA/255DD0",
stackTrace: " at <anonymous>:1:1",
},
() => window.domAutomationController.send(""));
() => chrome.test.sendScriptResult(""));
)-";
ExecuteScriptInBackgroundPage(extension_->id(), kTestScript);

Expand Down Expand Up @@ -165,7 +165,7 @@ IN_PROC_BROWSER_TEST_F(CrashReportPrivateApiTest, ExtraParamsAndStackTrace) {
}

IN_PROC_BROWSER_TEST_F(CrashReportPrivateApiTest, StackTraceWithErrorMessage) {
constexpr char kTestScript[] = R"(
static constexpr char kTestScript[] = R"(
chrome.crashReportPrivate.reportError({
message: "hi",
url: "http://www.test.com/foo",
Expand All @@ -175,7 +175,7 @@ IN_PROC_BROWSER_TEST_F(CrashReportPrivateApiTest, StackTraceWithErrorMessage) {
columnNumber: 456,
stackTrace: 'hi'
},
() => window.domAutomationController.send(""));
() => chrome.test.sendScriptResult(""));
)";
ExecuteScriptInBackgroundPage(extension_->id(), kTestScript);

Expand All @@ -201,7 +201,7 @@ IN_PROC_BROWSER_TEST_F(CrashReportPrivateApiTest, StackTraceWithErrorMessage) {
IN_PROC_BROWSER_TEST_F(CrashReportPrivateApiTest, RedactMessage) {
// We use the feedback APIs redaction tool, which scrubs many different types
// of PII. As a sanity check, test if MAC addresses are redacted.
constexpr char kTestScript[] = R"(
static constexpr char kTestScript[] = R"(
chrome.crashReportPrivate.reportError({
message: "06-00-00-00-00-00",
url: "http://www.test.com/foo",
Expand All @@ -210,7 +210,7 @@ IN_PROC_BROWSER_TEST_F(CrashReportPrivateApiTest, RedactMessage) {
lineNumber: 123,
columnNumber: 456,
},
() => window.domAutomationController.send(""));
() => chrome.test.sendScriptResult(""));
)";
ExecuteScriptInBackgroundPage(extension_->id(), kTestScript);

Expand Down Expand Up @@ -244,13 +244,13 @@ IN_PROC_BROWSER_TEST_F(CrashReportPrivateApiTest, SuppressedIfDevtoolsOpen) {
DevToolsWindow* devtools_window =
DevToolsWindowTesting::OpenDevToolsWindowSync(
web_contents, false /** is devtools docked. */);
constexpr char kTestScript[] = R"(
static constexpr char kTestScript[] = R"(
chrome.crashReportPrivate.reportError({
message: "hi",
url: "http://www.test.com",
},
() => {
window.domAutomationController.send(chrome.runtime.lastError ?
chrome.test.sendScriptResult(chrome.runtime.lastError ?
chrome.runtime.lastError.message : "")
});
)";
Expand Down Expand Up @@ -278,7 +278,7 @@ IN_PROC_BROWSER_TEST_F(CrashReportPrivateApiTest, CalledFromWebContentsInTab) {
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_TRUE(NavigateToURL(web_content, extension_context_url));

constexpr char kTestScript[] = R"(
static constexpr char kTestScript[] = R"(
chrome.crashReportPrivate.reportError({
message: "hi",
url: "http://www.test.com",
Expand Down Expand Up @@ -339,7 +339,7 @@ IN_PROC_BROWSER_TEST_P(CrashReportPrivateCalledFromSwaTest,
const GURL extension_context_url("chrome://media-app");
EXPECT_TRUE(NavigateToURL(web_content, extension_context_url));

constexpr char kTestScript[] = R"(
static constexpr char kTestScript[] = R"(
chrome.crashReportPrivate.reportError({
message: "hi",
url: "http://www.test.com",
Expand Down Expand Up @@ -376,7 +376,7 @@ IN_PROC_BROWSER_TEST_P(CrashReportPrivateCalledFromSwaTest,
MockCrashEndpoint endpoint(embedded_test_server());
ScopedMockChromeJsErrorReportProcessor processor(endpoint);

constexpr char kTestScript[] = R"(
static constexpr char kTestScript[] = R"(
chrome.crashReportPrivate.reportError({
message: "hi",
url: "http://www.test.com",
Expand Down

0 comments on commit 181bfc5

Please sign in to comment.