Skip to content

Commit

Permalink
Reland "Add ExecuteJavaScriptForTest and make all tests use it"
Browse files Browse the repository at this point in the history
Original issue's description:
> Additionally, restrict the URLs that ExecuteJavaScript can be invoked on
> to chrome-controlled URLs.
>
> R=jam@chromium.org
> BUG=507809
>
> Review URL: https://codereview.chromium.org/1123783002
>
> Cr-Commit-Position: refs/heads/master@{#340231}

R=mkwst@chromium.org
TBR=jam@chromium.org
BUG=507809
CQ_INCLUDE_TRYBOTS=tryserver.blink:linux_blink_rel

Review URL: https://codereview.chromium.org/1258593002 .

Cr-Commit-Position: refs/heads/master@{#340260}
  • Loading branch information
jeisinger committed Jul 24, 2015
1 parent 89c4fe5 commit 14ea977
Show file tree
Hide file tree
Showing 62 changed files with 305 additions and 150 deletions.
3 changes: 3 additions & 0 deletions android_webview/browser/aw_browser_main_parts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "base/files/file_path.h"
#include "base/path_service.h"
#include "content/public/browser/android/synchronous_compositor.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_switches.h"
Expand Down Expand Up @@ -118,6 +119,8 @@ void AwBrowserMainParts::PreMainMessageLoopRun() {
gfx::GLSurface::InitializeOneOff();
}

content::RenderFrameHost::AllowInjectingJavaScriptForAndroidWebView();

// This is needed for WebView Classic backwards compatibility
// See crbug.com/298495
content::SetMaxURLChars(20 * 1024 * 1024);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2069,6 +2069,22 @@ public void handleJavaScriptResult(String jsonResult) {
mWebContents.evaluateJavaScript(script, jsCallback);
}

public void evaluateJavaScriptForTests(String script, final ValueCallback<String> callback) {
if (TRACE) Log.d(TAG, "evaluateJavascriptForTests=" + script);
if (isDestroyed()) return;
JavaScriptCallback jsCallback = null;
if (callback != null) {
jsCallback = new JavaScriptCallback() {
@Override
public void handleJavaScriptResult(String jsonResult) {
callback.onReceiveValue(jsonResult);
}
};
}

mWebContents.evaluateJavaScriptForTests(script, jsCallback);
}

/**
* Post a message to a frame.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ public void run() {
AwSettings awSettings = awContents.getSettings();
awSettings.setJavaScriptEnabled(true);
awContents.addJavascriptInterface(new JavaScriptObject(callback), "bridge");
awContents.evaluateJavaScript("window.bridge.run();", null);
awContents.evaluateJavaScriptForTests("window.bridge.run();", null);
}
});
callback.waitForCallback(0, 1, WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void run() {
assertEquals("\"function\"", executeJavaScriptAndWaitForResult(
awContents, mContentsClient, "typeof test.destroy"));
int currentCallCount = client2.getOnPageFinishedHelper().getCallCount();
awContents.evaluateJavaScript("test.destroy()", null);
awContents.evaluateJavaScriptForTests("test.destroy()", null);

client2.getOnPageFinishedHelper().waitForCallback(currentCallCount);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ public void run() {
TestAwContentsClient.OnCreateWindowHelper onCreateWindowHelper =
parentAwContentsClient.getOnCreateWindowHelper();
int currentCallCount = onCreateWindowHelper.getCallCount();
parentAwContents.evaluateJavaScript(triggerScript, null);
parentAwContents.evaluateJavaScriptForTests(triggerScript, null);
onCreateWindowHelper.waitForCallback(
currentCallCount, 1, WAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void testGetPosition() throws Throwable {
loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(),
RAW_HTML, "text/html", false);

mAwContents.evaluateJavaScript("initiate_getCurrentPosition();", null);
mAwContents.evaluateJavaScriptForTests("initiate_getCurrentPosition();", null);

poll(new Callable<Boolean>() {
@Override
Expand All @@ -122,7 +122,7 @@ public Boolean call() throws Exception {
}
});

mAwContents.evaluateJavaScript("initiate_getCurrentPosition();", null);
mAwContents.evaluateJavaScriptForTests("initiate_getCurrentPosition();", null);
poll(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
Expand All @@ -140,7 +140,7 @@ public void testWatchPosition() throws Throwable {
loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(),
RAW_HTML, "text/html", false);

mAwContents.evaluateJavaScript("initiate_watchPosition();", null);
mAwContents.evaluateJavaScriptForTests("initiate_watchPosition();", null);

poll(new Callable<Boolean>() {
@Override
Expand All @@ -157,7 +157,7 @@ public void testPauseGeolocationOnPause() throws Throwable {
loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(),
RAW_HTML, "text/html", false);

mAwContents.evaluateJavaScript("initiate_watchPosition();", null);
mAwContents.evaluateJavaScriptForTests("initiate_watchPosition();", null);

poll(new Callable<Boolean>() {
@Override
Expand Down Expand Up @@ -215,7 +215,7 @@ public void run() {
loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(),
RAW_HTML, "text/html", false);

mAwContents.evaluateJavaScript("initiate_watchPosition();", null);
mAwContents.evaluateJavaScriptForTests("initiate_watchPosition();", null);

assertEquals(0, getPositionCountFromJS());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public boolean isSatisfied() {
testCase.getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
awContents.getWebContents().evaluateJavaScript(
awContents.getWebContents().evaluateJavaScriptForTests(
"var evObj = document.createEvent('Events'); "
+ "evObj.initEvent('click', true, false); "
+ "document.getElementById('" + linkId + "').dispatchEvent(evObj);"
Expand All @@ -65,7 +65,7 @@ public static String executeJavaScriptAndWaitForResult(
testCase.getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
onEvaluateJavaScriptResultHelper.evaluateJavaScript(
onEvaluateJavaScriptResultHelper.evaluateJavaScriptForTests(
awContents.getWebContents(), code);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public void testJavaScriptEvalIsCorrectlyOrderedWithinOneTab()
for (int i = 1; i <= 30; ++i) {
for (int j = 0; j < 5; ++j) {
// Start evaluation of a JavaScript script -- we don't need a result.
tab1.getWebContents().evaluateJavaScript("foobar();", null);
tab2.getWebContents().evaluateJavaScript("foobar();", null);
tab1.getWebContents().evaluateJavaScriptForTests("foobar();", null);
tab2.getWebContents().evaluateJavaScriptForTests("foobar();", null);
}
assertEquals("Incorrect JavaScript evaluation result on tab1",
i * 2,
Expand All @@ -65,8 +65,8 @@ public void testJavaScriptEvalIsCorrectlyOrderedWithinOneTab()
tab1.getWebContents(), "add2()")));
for (int j = 0; j < 5; ++j) {
// Start evaluation of a JavaScript script -- we don't need a result.
tab1.getWebContents().evaluateJavaScript("foobar();", null);
tab2.getWebContents().evaluateJavaScript("foobar();", null);
tab1.getWebContents().evaluateJavaScriptForTests("foobar();", null);
tab2.getWebContents().evaluateJavaScriptForTests("foobar();", null);
}
assertEquals("Incorrect JavaScript evaluation result on tab2",
i * 2 + 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ public String call() {
clickCancel(jsDialog);
scriptEvent.waitUntilHasValue();

scriptEvent.evaluateJavaScript(getActivity().getCurrentContentViewCore().getWebContents(),
scriptEvent.evaluateJavaScriptForTests(
getActivity().getCurrentContentViewCore().getWebContents(),
"alert('Android');");
assertTrue("No further dialog boxes should be shown.", scriptEvent.waitUntilHasValue());
}
Expand Down Expand Up @@ -310,7 +311,8 @@ private OnEvaluateJavaScriptResultHelper executeJavaScriptAndWaitForDialog(Strin
private OnEvaluateJavaScriptResultHelper executeJavaScriptAndWaitForDialog(
final OnEvaluateJavaScriptResultHelper helper, String script)
throws InterruptedException {
helper.evaluateJavaScript(getActivity().getCurrentContentViewCore().getWebContents(),
helper.evaluateJavaScriptForTests(
getActivity().getCurrentContentViewCore().getWebContents(),
script);
boolean criteriaSatisfied = CriteriaHelper.pollForCriteria(
new JavascriptAppModalDialogShownCriteria(true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void testSpawnPopupOnBackgroundTab() throws InterruptedException, Timeout
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
tab.getWebContents().evaluateJavaScript(
tab.getWebContents().evaluateJavaScriptForTests(
"(function() {"
+ " window.open('www.google.com');"
+ "})()",
Expand All @@ -158,7 +158,7 @@ public void testAlertDialogDoesNotChangeActiveModel() throws InterruptedExceptio
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
tab.getWebContents().evaluateJavaScript(
tab.getWebContents().evaluateJavaScriptForTests(
"(function() {"
+ " alert('hi');"
+ "})()",
Expand Down
12 changes: 6 additions & 6 deletions chrome/browser/apps/custom_launcher_page_browsertest_views.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ class CustomLauncherPageBrowserTest
enabled ? "launcherPageEnabled" : "launcherPageDisabled";

ExtensionTestMessageListener listener(test_message, false);
custom_page_frame->ExecuteJavaScript(enabled ? kLauncherPageEnableScript
: kLauncherPageDisableScript);
custom_page_frame->ExecuteJavaScriptForTests(
enabled ? kLauncherPageEnableScript : kLauncherPageDisableScript);
listener.WaitUntilSatisfied();
}

Expand Down Expand Up @@ -339,7 +339,7 @@ IN_PROC_BROWSER_TEST_F(CustomLauncherPageBrowserTest, LauncherPageShowAndHide) {
// if the app launcher is already showing.
{
ExtensionTestMessageListener listener("onPageProgressAt1", false);
custom_page_frame->ExecuteJavaScript(kLauncherPageShowScript);
custom_page_frame->ExecuteJavaScriptForTests(kLauncherPageShowScript);

listener.WaitUntilSatisfied();
EXPECT_TRUE(contents_view->IsStateActive(
Expand All @@ -353,7 +353,7 @@ IN_PROC_BROWSER_TEST_F(CustomLauncherPageBrowserTest, LauncherPageShowAndHide) {
app_list_view->GetWidget()->Close();

ExtensionTestMessageListener listener("onPageProgressAt1", false);
custom_page_frame->ExecuteJavaScript(kLauncherPageShowScript);
custom_page_frame->ExecuteJavaScriptForTests(kLauncherPageShowScript);

listener.WaitUntilSatisfied();

Expand All @@ -367,7 +367,7 @@ IN_PROC_BROWSER_TEST_F(CustomLauncherPageBrowserTest, LauncherPageShowAndHide) {
// Ensure launcherPage.hide() hides the launcher page when it's showing.
{
ExtensionTestMessageListener listener("onPageProgressAt0", false);
custom_page_frame->ExecuteJavaScript(kLauncherPageHideScript);
custom_page_frame->ExecuteJavaScriptForTests(kLauncherPageHideScript);

listener.WaitUntilSatisfied();

Expand All @@ -380,7 +380,7 @@ IN_PROC_BROWSER_TEST_F(CustomLauncherPageBrowserTest, LauncherPageShowAndHide) {
contents_view->SetActiveState(app_list::AppListModel::STATE_APPS, false);

ExtensionTestMessageListener listener("launcherPageHidden", false);
custom_page_frame->ExecuteJavaScript(kLauncherPageHideScript);
custom_page_frame->ExecuteJavaScriptForTests(kLauncherPageHideScript);
listener.WaitUntilSatisfied();

EXPECT_TRUE(
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/chromeos/login/lock/screen_locker_tester.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class WebUIScreenLockerTester : public ScreenLockerTester {
};

void WebUIScreenLockerTester::SetPassword(const std::string& password) {
webui()->GetWebContents()->GetMainFrame()->ExecuteJavaScript(
webui()->GetWebContents()->GetMainFrame()->ExecuteJavaScriptForTests(
base::ASCIIToUTF16(base::StringPrintf(
"$('pod-row').pods[0].passwordElement.value = '%s';",
password.c_str())));
Expand Down
3 changes: 2 additions & 1 deletion chrome/browser/download/download_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2237,7 +2237,8 @@ IN_PROC_BROWSER_TEST_F(DownloadTest, SavePageNonHTMLViaPost) {
&web_contents->GetController()));
content::RenderFrameHost* render_frame_host = web_contents->GetMainFrame();
ASSERT_TRUE(render_frame_host != NULL);
render_frame_host->ExecuteJavaScript(base::ASCIIToUTF16("SubmitForm()"));
render_frame_host->ExecuteJavaScriptForTests(
base::ASCIIToUTF16("SubmitForm()"));
observer.Wait();
EXPECT_EQ(jpeg_url, web_contents->GetURL());

Expand Down
16 changes: 8 additions & 8 deletions chrome/browser/errorpage_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ IN_PROC_BROWSER_TEST_F(ErrorPageTest, DNSError_DoSearch) {
// Can't use content::ExecuteScript because it waits for scripts to send
// notification that they've run, and scripts that trigger a navigation may
// not send that notification.
web_contents->GetMainFrame()->ExecuteJavaScript(
web_contents->GetMainFrame()->ExecuteJavaScriptForTests(
base::ASCIIToUTF16("document.getElementById('search-button').click();"));
nav_observer.Wait();
EXPECT_EQ(base::ASCIIToUTF16("Title Of More Awesomeness"),
Expand Down Expand Up @@ -666,7 +666,7 @@ IN_PROC_BROWSER_TEST_F(ErrorPageTest, DNSError_DoReload) {
// Can't use content::ExecuteScript because it waits for scripts to send
// notification that they've run, and scripts that trigger a navigation may
// not send that notification.
web_contents->GetMainFrame()->ExecuteJavaScript(
web_contents->GetMainFrame()->ExecuteJavaScriptForTests(
base::ASCIIToUTF16("document.getElementById('reload-button').click();"));
nav_observer.Wait();
ExpectDisplayingNavigationCorrections(browser(), net::ERR_NAME_NOT_RESOLVED);
Expand Down Expand Up @@ -699,12 +699,12 @@ IN_PROC_BROWSER_TEST_F(ErrorPageTest, DNSError_DoClickLink) {
"document.querySelector('a[href=\"http://mock.http/title2.html\"]')";
// The tracking request is triggered by onmousedown, so it catches middle
// mouse button clicks, as well as left clicks.
web_contents->GetMainFrame()->ExecuteJavaScript(
web_contents->GetMainFrame()->ExecuteJavaScriptForTests(
base::ASCIIToUTF16(link_selector + ".onmousedown();"));
// Can't use content::ExecuteScript because it waits for scripts to send
// notification that they've run, and scripts that trigger a navigation may
// not send that notification.
web_contents->GetMainFrame()->ExecuteJavaScript(
web_contents->GetMainFrame()->ExecuteJavaScriptForTests(
base::ASCIIToUTF16(link_selector + ".click();"));
EXPECT_EQ(base::ASCIIToUTF16("Title Of Awesomeness"),
title_watcher.WaitAndGetTitle());
Expand Down Expand Up @@ -790,7 +790,7 @@ IN_PROC_BROWSER_TEST_F(ErrorPageTest, IFrameDNSError_JavaScript) {
content::WindowedNotificationObserver load_observer(
content::NOTIFICATION_LOAD_STOP,
content::Source<NavigationController>(&wc->GetController()));
wc->GetMainFrame()->ExecuteJavaScript(base::ASCIIToUTF16(script));
wc->GetMainFrame()->ExecuteJavaScriptForTests(base::ASCIIToUTF16(script));
load_observer.Wait();

// Ensure we saw the expected failure.
Expand All @@ -810,7 +810,7 @@ IN_PROC_BROWSER_TEST_F(ErrorPageTest, IFrameDNSError_JavaScript) {
content::WindowedNotificationObserver load_observer(
content::NOTIFICATION_LOAD_STOP,
content::Source<NavigationController>(&wc->GetController()));
wc->GetMainFrame()->ExecuteJavaScript(base::ASCIIToUTF16(script));
wc->GetMainFrame()->ExecuteJavaScriptForTests(base::ASCIIToUTF16(script));
load_observer.Wait();
}

Expand All @@ -821,7 +821,7 @@ IN_PROC_BROWSER_TEST_F(ErrorPageTest, IFrameDNSError_JavaScript) {
content::WindowedNotificationObserver load_observer(
content::NOTIFICATION_LOAD_STOP,
content::Source<NavigationController>(&wc->GetController()));
wc->GetMainFrame()->ExecuteJavaScript(base::ASCIIToUTF16(script));
wc->GetMainFrame()->ExecuteJavaScriptForTests(base::ASCIIToUTF16(script));
load_observer.Wait();

EXPECT_EQ(fail_url, fail_observer.fail_url());
Expand Down Expand Up @@ -1000,7 +1000,7 @@ IN_PROC_BROWSER_TEST_F(ErrorPageAutoReloadTest, ManualReloadNotSuppressed) {
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
content::TestNavigationObserver nav_observer(web_contents, 1);
web_contents->GetMainFrame()->ExecuteJavaScript(
web_contents->GetMainFrame()->ExecuteJavaScriptForTests(
base::ASCIIToUTF16("document.getElementById('reload-button').click();"));
nav_observer.Wait();
EXPECT_FALSE(IsDisplayingText(browser(), "error.page.auto.reload"));
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/extensions/alert_apitest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, AlertBasic) {
extensions::ProcessManager::Get(browser()->profile())
->GetBackgroundHostForExtension(extension->id());
ASSERT_TRUE(host);
host->host_contents()->GetMainFrame()->ExecuteJavaScript(
host->host_contents()->GetMainFrame()->ExecuteJavaScriptForTests(
base::ASCIIToUTF16("alert('This should not crash.');"));

app_modal::AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class MediaGalleriesGalleryWatchApiTest : public ExtensionApiTest {
void ExecuteCmdAndCheckReply(const std::string& js_command,
const std::string& ok_message) {
ExtensionTestMessageListener listener(ok_message, false);
background_host_->GetMainFrame()->ExecuteJavaScript(
background_host_->GetMainFrame()->ExecuteJavaScriptForTests(
base::ASCIIToUTF16(js_command));
EXPECT_TRUE(listener.WaitUntilSatisfied());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ class DelayLoadStartAndExecuteJavascript
rvh_->GetMainFrame()->ExecuteJavaScriptWithUserGestureForTests(
base::UTF8ToUTF16(script_));
} else {
rvh_->GetMainFrame()->ExecuteJavaScript(base::UTF8ToUTF16(script_));
rvh_->GetMainFrame()->ExecuteJavaScriptForTests(
base::UTF8ToUTF16(script_));
}
script_was_executed_ = true;
}
Expand Down Expand Up @@ -674,7 +675,8 @@ IN_PROC_BROWSER_TEST_F(WebNavigationApiTest, CrossProcessAbort) {
// Ensure the cross-site navigation has started, then execute JavaScript
// to cause the renderer-initiated, non-user navigation.
cross_site_load.Wait();
tab->GetMainFrame()->ExecuteJavaScript(base::UTF8ToUTF16("navigate2()"));
tab->GetMainFrame()->ExecuteJavaScriptForTests(
base::UTF8ToUTF16("navigate2()"));

// Wait for the same-site navigation to start and resume the cross-site
// one, allowing it to commit.
Expand Down
3 changes: 2 additions & 1 deletion chrome/browser/geolocation/geolocation_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ IFrameLoader::IFrameLoader(Browser* browser, int iframe_id, const GURL& url)
"window.domAutomationController.setAutomationId(0);"
"window.domAutomationController.send(addIFrame(%d, \"%s\"));",
iframe_id, url.spec().c_str()));
web_contents->GetMainFrame()->ExecuteJavaScript(base::UTF8ToUTF16(script));
web_contents->GetMainFrame()->ExecuteJavaScriptForTests(
base::UTF8ToUTF16(script));
content::RunMessageLoop();

EXPECT_EQ(base::StringPrintf("\"%d\"", iframe_id), javascript_response_);
Expand Down
Loading

0 comments on commit 14ea977

Please sign in to comment.