diff options
author | darin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-04 18:30:43 +0000 |
---|---|---|
committer | darin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-04 18:30:43 +0000 |
commit | b6987e0e556a5f72e5e783aae0e30e883cf66f91 (patch) | |
tree | fb07c2c5803510285ef0d030d42eb6ced5a8d2b7 /content | |
parent | 53bc1a4364a3a75344e20f94aedff2eca2f6f9af (diff) | |
download | chromium_src-b6987e0e556a5f72e5e783aae0e30e883cf66f91.zip chromium_src-b6987e0e556a5f72e5e783aae0e30e883cf66f91.tar.gz chromium_src-b6987e0e556a5f72e5e783aae0e30e883cf66f91.tar.bz2 |
Simplify ExecuteJavaScript* functions.
Remove the "Java" and rename to ExecuteScript*. This better matches
conventions elsewhere in the codebase.
Introduce ExecuteScriptInFrame* variants for the less common case
where script execution in a subframe is desired.
Make it possible to pass the ExecuteScript* family of functions either
a WebContents pointer or a RenderViewHost pointer to further simplify
callsites.
R=jam@chromium.org
BUG=none
Review URL: https://chromiumcodereview.appspot.com/11753009
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@175156 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content')
-rw-r--r-- | content/browser/bookmarklet_browsertest.cc | 5 | ||||
-rw-r--r-- | content/browser/database_browsertest.cc | 10 | ||||
-rw-r--r-- | content/browser/dom_storage/dom_storage_browsertest.cc | 5 | ||||
-rw-r--r-- | content/browser/encrypted_media_browsertest.cc | 5 | ||||
-rw-r--r-- | content/browser/fileapi/file_system_browsertest.cc | 5 | ||||
-rw-r--r-- | content/browser/in_process_webkit/indexed_db_browsertest.cc | 5 | ||||
-rw-r--r-- | content/browser/loader/resource_dispatcher_host_browsertest.cc | 21 | ||||
-rw-r--r-- | content/browser/renderer_host/render_view_host_manager_browsertest.cc | 175 | ||||
-rw-r--r-- | content/browser/renderer_host/render_widget_host_view_win_browsertest.cc | 30 | ||||
-rw-r--r-- | content/browser/session_history_browsertest.cc | 20 | ||||
-rw-r--r-- | content/browser/site_per_process_browsertest.cc | 3 | ||||
-rw-r--r-- | content/browser/webrtc_browsertest.cc | 5 | ||||
-rw-r--r-- | content/public/test/browser_test_utils.cc | 106 | ||||
-rw-r--r-- | content/public/test/browser_test_utils.h | 63 |
14 files changed, 234 insertions, 224 deletions
diff --git a/content/browser/bookmarklet_browsertest.cc b/content/browser/bookmarklet_browsertest.cc index 81eda413..bfdea1a 100644 --- a/content/browser/bookmarklet_browsertest.cc +++ b/content/browser/bookmarklet_browsertest.cc @@ -22,9 +22,8 @@ class BookmarkletTest : public ContentBrowserTest { std::string GetBodyText() { std::string body_text; - EXPECT_TRUE(ExecuteJavaScriptAndExtractString( - shell()->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractString( + shell()->web_contents(), "window.domAutomationController.send(document.body.innerText);", &body_text)); return body_text; diff --git a/content/browser/database_browsertest.cc b/content/browser/database_browsertest.cc index 0c49cfb..48f7304 100644 --- a/content/browser/database_browsertest.cc +++ b/content/browser/database_browsertest.cc @@ -27,9 +27,8 @@ class DatabaseTest : public ContentBrowserTest { const std::string& script, const std::string& result) { std::string data; - ASSERT_TRUE(ExecuteJavaScriptAndExtractString( - shell->web_contents()->GetRenderViewHost(), - std::string(), + ASSERT_TRUE(ExecuteScriptAndExtractString( + shell->web_contents(), script, &data)); ASSERT_EQ(data, result); @@ -65,9 +64,8 @@ class DatabaseTest : public ContentBrowserTest { bool HasTable(Shell* shell) { std::string data; - CHECK(ExecuteJavaScriptAndExtractString( - shell->web_contents()->GetRenderViewHost(), - std::string(), + CHECK(ExecuteScriptAndExtractString( + shell->web_contents(), "getRecords()", &data)); return data != "getRecords error: [object SQLError]"; diff --git a/content/browser/dom_storage/dom_storage_browsertest.cc b/content/browser/dom_storage/dom_storage_browsertest.cc index 84405f2..3591c67 100644 --- a/content/browser/dom_storage/dom_storage_browsertest.cc +++ b/content/browser/dom_storage/dom_storage_browsertest.cc @@ -28,9 +28,8 @@ class DomStorageBrowserTest : public ContentBrowserTest { std::string result = the_browser->web_contents()->GetURL().ref(); if (result != "pass") { std::string js_result; - ASSERT_TRUE(ExecuteJavaScriptAndExtractString( - the_browser->web_contents()->GetRenderViewHost(), - std::string(), + ASSERT_TRUE(ExecuteScriptAndExtractString( + the_browser->web_contents(), "window.domAutomationController.send(getLog())", &js_result)); FAIL() << "Failed: " << js_result; diff --git a/content/browser/encrypted_media_browsertest.cc b/content/browser/encrypted_media_browsertest.cc index 5cd993f..1d32e1d 100644 --- a/content/browser/encrypted_media_browsertest.cc +++ b/content/browser/encrypted_media_browsertest.cc @@ -76,9 +76,8 @@ class EncryptedMediaTest : public testing::WithParamInterface<const char*>, if (final_title == kFailed) { std::string fail_message; - EXPECT_TRUE(ExecuteJavaScriptAndExtractString( - shell()->web_contents()->GetRenderViewHost(), - std::string(), + EXPECT_TRUE(ExecuteScriptAndExtractString( + shell()->web_contents(), "window.domAutomationController.send(failMessage);", &fail_message)); LOG(INFO) << "Test failed: " << fail_message; diff --git a/content/browser/fileapi/file_system_browsertest.cc b/content/browser/fileapi/file_system_browsertest.cc index e01f08b..29bdbd3 100644 --- a/content/browser/fileapi/file_system_browsertest.cc +++ b/content/browser/fileapi/file_system_browsertest.cc @@ -41,9 +41,8 @@ class FileSystemBrowserTest : public ContentBrowserTest { std::string result = the_browser->web_contents()->GetURL().ref(); if (result != "pass") { std::string js_result; - ASSERT_TRUE(ExecuteJavaScriptAndExtractString( - the_browser->web_contents()->GetRenderViewHost(), - std::string(), + ASSERT_TRUE(ExecuteScriptAndExtractString( + the_browser->web_contents(), "window.domAutomationController.send(getLog())", &js_result)); FAIL() << "Failed: " << js_result; diff --git a/content/browser/in_process_webkit/indexed_db_browsertest.cc b/content/browser/in_process_webkit/indexed_db_browsertest.cc index e8f3222..c7ce5e7b 100644 --- a/content/browser/in_process_webkit/indexed_db_browsertest.cc +++ b/content/browser/in_process_webkit/indexed_db_browsertest.cc @@ -49,9 +49,8 @@ class IndexedDBBrowserTest : public ContentBrowserTest { std::string result = the_browser->web_contents()->GetURL().ref(); if (result != "pass") { std::string js_result; - ASSERT_TRUE(ExecuteJavaScriptAndExtractString( - the_browser->web_contents()->GetRenderViewHost(), - std::string(), + ASSERT_TRUE(ExecuteScriptAndExtractString( + the_browser->web_contents(), "window.domAutomationController.send(getLog())", &js_result)); FAIL() << "Failed: " << js_result; diff --git a/content/browser/loader/resource_dispatcher_host_browsertest.cc b/content/browser/loader/resource_dispatcher_host_browsertest.cc index a9457ea..0257b57 100644 --- a/content/browser/loader/resource_dispatcher_host_browsertest.cc +++ b/content/browser/loader/resource_dispatcher_host_browsertest.cc @@ -47,10 +47,6 @@ class ResourceDispatcherHostBrowserTest : public ContentBrowserTest, got_downloads_ = !!manager->InProgressCount(); } - RenderViewHost* render_view_host() { - return shell()->web_contents()->GetRenderViewHost(); - } - GURL GetMockURL(const std::string& file) { return URLRequestMockHTTPJob::GetMockUrl(FilePath().AppendASCII(file)); } @@ -69,7 +65,7 @@ class ResourceDispatcherHostBrowserTest : public ContentBrowserTest, ShellAddedObserver new_shell_observer; // Create dynamic popup. - if (!ExecuteJavaScript(render_view_host(), "", "OpenPopup();")) + if (!ExecuteScript(shell()->web_contents(), "OpenPopup();")) return false; Shell* new_shell = new_shell_observer.GetShell(); @@ -165,9 +161,8 @@ IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, SyncXMLHttpRequest) { // Let's check the XMLHttpRequest ran successfully. bool success = false; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - shell()->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + shell()->web_contents(), "window.domAutomationController.send(DidSyncRequestSucceed());", &success)); EXPECT_TRUE(success); @@ -183,9 +178,8 @@ IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, // Let's check the XMLHttpRequest ran successfully. bool success = false; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - shell()->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + shell()->web_contents(), "window.domAutomationController.send(DidSucceed());", &success)); EXPECT_TRUE(success); @@ -346,9 +340,8 @@ IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, std::string redirect_script = "window.location='" + test_url.possibly_invalid_spec() + "';" + "window.domAutomationController.send(true);"; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - shell()->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + shell()->web_contents(), redirect_script, &success)); EXPECT_EQ(expected_title16, title_watcher.WaitAndGetTitle()); diff --git a/content/browser/renderer_host/render_view_host_manager_browsertest.cc b/content/browser/renderer_host/render_view_host_manager_browsertest.cc index 32ed998..94b33b0 100644 --- a/content/browser/renderer_host/render_view_host_manager_browsertest.cc +++ b/content/browser/renderer_host/render_view_host_manager_browsertest.cc @@ -131,9 +131,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, NoScriptAccessAfterSwapOut) { // Open a same-site link in a new window. ShellAddedObserver new_shell_observer; bool success = false; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - shell()->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + shell()->web_contents(), "window.domAutomationController.send(clickSameSiteTargetedLink());", &success)); EXPECT_TRUE(success); @@ -151,9 +150,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, NoScriptAccessAfterSwapOut) { // We should have access to the opened window's location. success = false; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - shell()->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + shell()->web_contents(), "window.domAutomationController.send(testScriptAccessToWindow());", &success)); EXPECT_TRUE(success); @@ -166,9 +164,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, NoScriptAccessAfterSwapOut) { // We should no longer have script access to the opened window's location. success = false; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - shell()->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + shell()->web_contents(), "window.domAutomationController.send(testScriptAccessToWindow());", &success)); EXPECT_FALSE(success); @@ -202,9 +199,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, // Test clicking a rel=noreferrer + target=blank link. ShellAddedObserver new_shell_observer; bool success = false; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - shell()->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + shell()->web_contents(), "window.domAutomationController.send(clickNoRefTargetBlankLink());", &success)); EXPECT_TRUE(success); @@ -256,9 +252,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, // Test clicking a same-site rel=noreferrer + target=foo link. ShellAddedObserver new_shell_observer; bool success = false; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - shell()->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + shell()->web_contents(), "window.domAutomationController.send(clickSameSiteNoRefTargetedLink());", &success)); EXPECT_TRUE(success); @@ -310,9 +305,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, // Test clicking a target=blank link. ShellAddedObserver new_shell_observer; bool success = false; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - shell()->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + shell()->web_contents(), "window.domAutomationController.send(clickTargetBlankLink());", &success)); EXPECT_TRUE(success); @@ -358,9 +352,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, // Test clicking a rel=noreferrer link. bool success = false; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - shell()->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + shell()->web_contents(), "window.domAutomationController.send(clickNoRefLink());", &success)); EXPECT_TRUE(success); @@ -406,9 +399,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, // Test clicking a target=foo link. ShellAddedObserver new_shell_observer; bool success = false; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - shell()->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + shell()->web_contents(), "window.domAutomationController.send(clickSameSiteTargetedLink());", &success)); EXPECT_TRUE(success); @@ -435,9 +427,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, NOTIFICATION_NAV_ENTRY_COMMITTED, Source<NavigationController>( &new_shell->web_contents()->GetController())); - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - shell()->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + shell()->web_contents(), "window.domAutomationController.send(clickSameSiteTargetedLink());", &success)); EXPECT_TRUE(success); @@ -456,9 +447,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, WindowedNotificationObserver close_observer( NOTIFICATION_WEB_CONTENTS_DESTROYED, Source<WebContents>(new_shell->web_contents())); - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - shell()->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + shell()->web_contents(), "window.domAutomationController.send(testCloseWindow());", &success)); EXPECT_TRUE(success); @@ -492,9 +482,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, DisownOpener) { // Test clicking a target=_blank link. ShellAddedObserver new_shell_observer; bool success = false; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - shell()->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + shell()->web_contents(), "window.domAutomationController.send(clickSameSiteTargetBlankLink());", &success)); EXPECT_TRUE(success); @@ -517,10 +506,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, DisownOpener) { EXPECT_NE(orig_site_instance, new_site_instance); // Now disown the opener. - EXPECT_TRUE(ExecuteJavaScript( - new_shell->web_contents()->GetRenderViewHost(), - "", - "window.opener = null;")); + EXPECT_TRUE(ExecuteScript(new_shell->web_contents(), + "window.opener = null;")); // Go back and ensure the opener is still null. { @@ -532,9 +519,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, DisownOpener) { back_nav_load_observer.Wait(); } success = false; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - new_shell->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + new_shell->web_contents(), "window.domAutomationController.send(window.opener == null);", &success)); EXPECT_TRUE(success); @@ -542,9 +528,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, DisownOpener) { // Now navigate forward again (creating a new process) and check opener. NavigateToURL(new_shell, https_server.GetURL("files/title1.html")); success = false; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - new_shell->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + new_shell->web_contents(), "window.domAutomationController.send(window.opener == null);", &success)); EXPECT_TRUE(success); @@ -592,9 +577,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, // First, a named target=foo window. ShellAddedObserver new_shell_observer; bool success = false; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - opener_contents->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + opener_contents, "window.domAutomationController.send(clickSameSiteTargetedLink());", &success)); EXPECT_TRUE(success); @@ -612,9 +596,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, // Second, a target=_blank window. ShellAddedObserver new_shell_observer2; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - shell()->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + shell()->web_contents(), "window.domAutomationController.send(clickSameSiteTargetBlankLink());", &success)); EXPECT_TRUE(success); @@ -639,11 +622,10 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, // 2) Fail to post a message from the foo window to the opener if the target // origin is wrong. We won't see an error, but we can check for the right // number of received messages below. - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - foo_contents->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + foo_contents, "window.domAutomationController.send(postToOpener('msg'," - "'http://google.com'));", + " 'http://google.com'));", &success)); EXPECT_TRUE(success); ASSERT_FALSE(opener_manager->GetSwappedOutRenderViewHost(orig_site_instance)); @@ -653,9 +635,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, WindowedNotificationObserver title_observer( NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED, Source<WebContents>(foo_contents)); - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - foo_contents->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + foo_contents, "window.domAutomationController.send(postToOpener('msg','*'));", &success)); EXPECT_TRUE(success); @@ -665,15 +646,13 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, // We should have received only 1 message in the opener and "foo" tabs, // and updated the title. int opener_received_messages = 0; - EXPECT_TRUE(ExecuteJavaScriptAndExtractInt( - opener_contents->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractInt( + opener_contents, "window.domAutomationController.send(window.receivedMessages);", &opener_received_messages)); int foo_received_messages = 0; - EXPECT_TRUE(ExecuteJavaScriptAndExtractInt( - foo_contents->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractInt( + foo_contents, "window.domAutomationController.send(window.receivedMessages);", &foo_received_messages)); EXPECT_EQ(1, foo_received_messages); @@ -685,9 +664,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, WindowedNotificationObserver title_observer2( NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED, Source<WebContents>(foo_contents)); - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - new_contents->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + new_contents, "window.domAutomationController.send(postToFoo('msg2'));", &success)); EXPECT_TRUE(success); @@ -731,9 +709,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, // Test clicking a target=foo link. ShellAddedObserver new_shell_observer; bool success = false; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - orig_contents->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + orig_contents, "window.domAutomationController.send(clickSameSiteTargetedLink());", &success)); EXPECT_TRUE(success); @@ -760,9 +737,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, NOTIFICATION_NAV_ENTRY_COMMITTED, Source<NavigationController>( &orig_contents->GetController())); - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - new_shell->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + new_shell->web_contents(), "window.domAutomationController.send(navigateOpener());", &success)); EXPECT_TRUE(success); @@ -803,9 +779,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, // Test clicking a target=foo link. ShellAddedObserver new_shell_observer; bool success = false; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - shell()->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + shell()->web_contents(), "window.domAutomationController.send(clickSameSiteTargetedLink());", &success)); EXPECT_TRUE(success); @@ -885,9 +860,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, ClickLinkAfter204Error) { // Renderer-initiated navigations should work. bool success = false; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - shell()->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + shell()->web_contents(), "window.domAutomationController.send(clickNoRefLink());", &success)); EXPECT_TRUE(success); @@ -1045,9 +1019,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, // Open a same-site link in a new widnow. ShellAddedObserver new_shell_observer; bool success = false; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - shell()->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + shell()->web_contents(), "window.domAutomationController.send(clickSameSiteTargetedLink());", &success)); EXPECT_TRUE(success); @@ -1059,11 +1032,11 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, new_shell->web_contents()->GetURL().path()); RenderViewHost* rvh = new_shell->web_contents()->GetRenderViewHost(); - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( + + EXPECT_TRUE(ExecuteScriptAndExtractBool( rvh, - "", "window.domAutomationController.send(" - "document.webkitVisibilityState == 'visible');", + " document.webkitVisibilityState == 'visible');", &success)); EXPECT_TRUE(success); @@ -1071,11 +1044,10 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, // tab's existing RenderView, causing it become hidden. NavigateToURL(new_shell, https_server.GetURL("files/title1.html")); - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( + EXPECT_TRUE(ExecuteScriptAndExtractBool( rvh, - "", "window.domAutomationController.send(" - "document.webkitVisibilityState == 'hidden');", + " document.webkitVisibilityState == 'hidden');", &success)); EXPECT_TRUE(success); @@ -1090,17 +1062,15 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, back_nav_load_observer.Wait(); } - EXPECT_EQ("/files/navigate_opener.html", new_shell->web_contents()->GetURL().path()); EXPECT_EQ(rvh, new_shell->web_contents()->GetRenderViewHost()); - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( + EXPECT_TRUE(ExecuteScriptAndExtractBool( rvh, - "", "window.domAutomationController.send(" - "document.webkitVisibilityState == 'visible');", + " document.webkitVisibilityState == 'visible');", &success)); EXPECT_TRUE(success); } @@ -1268,9 +1238,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, DISABLED_FrameTreeUpdates) { EXPECT_TRUE(orig_site_instance != NULL); ShellAddedObserver shell_observer1; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - opener_contents->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + opener_contents, "window.domAutomationController.send(openWindow('1-3.html'));", &success)); EXPECT_TRUE(success); @@ -1290,9 +1259,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, DISABLED_FrameTreeUpdates) { EXPECT_NE(orig_site_instance, site_instance1); ShellAddedObserver shell_observer2; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - opener_contents->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + opener_contents, "window.domAutomationController.send(openWindow('../title2.html'));", &success)); EXPECT_TRUE(success); @@ -1351,9 +1319,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, DISABLED_FrameTreeUpdates) { // Now let's ensure that using JS to add/remove frames results in proper // updates. - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - opener_contents->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + opener_contents, "window.domAutomationController.send(removeFrame());", &success)); EXPECT_TRUE(success); @@ -1367,9 +1334,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, DISABLED_FrameTreeUpdates) { NOTIFICATION_LOAD_STOP, Source<NavigationController>( &opener_contents->GetController())); - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - opener_contents->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + opener_contents, "window.domAutomationController.send(addFrame());", &success)); EXPECT_TRUE(success); @@ -1419,9 +1385,8 @@ IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, // Open a same-site page in a new window. ShellAddedObserver new_shell_observer; bool success = false; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - shell()->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + shell()->web_contents(), "window.domAutomationController.send(openWindow());", &success)); EXPECT_TRUE(success); diff --git a/content/browser/renderer_host/render_widget_host_view_win_browsertest.cc b/content/browser/renderer_host/render_widget_host_view_win_browsertest.cc index 8c0bb30..77763ab 100644 --- a/content/browser/renderer_host/render_widget_host_view_win_browsertest.cc +++ b/content/browser/renderer_host/render_widget_host_view_win_browsertest.cc @@ -40,9 +40,8 @@ IN_PROC_BROWSER_TEST_F(RenderWidgetHostViewWinTest, // Focus to the text field, the IME should be enabled. bool success = false; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - shell()->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + shell()->web_contents(), "window.domAutomationController.send(text01_focus());", &success)); EXPECT_TRUE(success); @@ -52,9 +51,8 @@ IN_PROC_BROWSER_TEST_F(RenderWidgetHostViewWinTest, // Focus to the password field, the IME should be disabled. success = false; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - shell()->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + shell()->web_contents(), "window.domAutomationController.send(password02_focus());", &success)); EXPECT_TRUE(success); @@ -80,9 +78,8 @@ IN_PROC_BROWSER_TEST_F(RenderWidgetHostViewWinTest, // Focus to the text field, the IME should be enabled. bool success = false; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - shell()->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + shell()->web_contents(), "window.domAutomationController.send(text01_focus());", &success)); EXPECT_TRUE(success); @@ -92,9 +89,8 @@ IN_PROC_BROWSER_TEST_F(RenderWidgetHostViewWinTest, // Focus to another text field, the IME should be enabled. success = false; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - shell()->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + shell()->web_contents(), "window.domAutomationController.send(text02_focus());", &success)); EXPECT_TRUE(success); @@ -120,9 +116,8 @@ IN_PROC_BROWSER_TEST_F(RenderWidgetHostViewWinTest, // Focus to the password field, the IME should be disabled. bool success = false; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - shell()->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + shell()->web_contents(), "window.domAutomationController.send(password01_focus());", &success)); EXPECT_TRUE(success); @@ -132,9 +127,8 @@ IN_PROC_BROWSER_TEST_F(RenderWidgetHostViewWinTest, // Focus to the another password field, the IME should be disabled. success = false; - EXPECT_TRUE(ExecuteJavaScriptAndExtractBool( - shell()->web_contents()->GetRenderViewHost(), - "", + EXPECT_TRUE(ExecuteScriptAndExtractBool( + shell()->web_contents(), "window.domAutomationController.send(password02_focus());", &success)); EXPECT_TRUE(success); diff --git a/content/browser/session_history_browsertest.cc b/content/browser/session_history_browsertest.cc index c81f89c..c57f1a6 100644 --- a/content/browser/session_history_browsertest.cc +++ b/content/browser/session_history_browsertest.cc @@ -429,18 +429,16 @@ IN_PROC_BROWSER_TEST_F(SessionHistoryTest, LocationChangeInSubframe) { // http://code.google.com/p/chromium/issues/detail?id=56267 IN_PROC_BROWSER_TEST_F(SessionHistoryTest, HistoryLength) { int length; - ASSERT_TRUE(ExecuteJavaScriptAndExtractInt( - shell()->web_contents()->GetRenderViewHost(), - "", + ASSERT_TRUE(ExecuteScriptAndExtractInt( + shell()->web_contents(), "domAutomationController.send(history.length)", &length)); EXPECT_EQ(1, length); NavigateToURL(shell(), GetURL("title1.html")); - ASSERT_TRUE(ExecuteJavaScriptAndExtractInt( - shell()->web_contents()->GetRenderViewHost(), - "", + ASSERT_TRUE(ExecuteScriptAndExtractInt( + shell()->web_contents(), "domAutomationController.send(history.length)", &length)); EXPECT_EQ(2, length); @@ -448,9 +446,8 @@ IN_PROC_BROWSER_TEST_F(SessionHistoryTest, HistoryLength) { // Now test that history.length is updated when the navigation is committed. NavigateToURL(shell(), GetURL("record_length.html")); - ASSERT_TRUE(ExecuteJavaScriptAndExtractInt( - shell()->web_contents()->GetRenderViewHost(), - "", + ASSERT_TRUE(ExecuteScriptAndExtractInt( + shell()->web_contents(), "domAutomationController.send(history.length)", &length)); EXPECT_EQ(3, length); @@ -461,9 +458,8 @@ IN_PROC_BROWSER_TEST_F(SessionHistoryTest, HistoryLength) { // Ensure history.length is properly truncated. NavigateToURL(shell(), GetURL("title2.html")); - ASSERT_TRUE(ExecuteJavaScriptAndExtractInt( - shell()->web_contents()->GetRenderViewHost(), - "", + ASSERT_TRUE(ExecuteScriptAndExtractInt( + shell()->web_contents(), "domAutomationController.send(history.length)", &length)); EXPECT_EQ(2, length); diff --git a/content/browser/site_per_process_browsertest.cc b/content/browser/site_per_process_browsertest.cc index b215efe..d71bbe0 100644 --- a/content/browser/site_per_process_browsertest.cc +++ b/content/browser/site_per_process_browsertest.cc @@ -152,8 +152,7 @@ class SitePerProcessBrowserTest : public ContentBrowserTest { NOTIFICATION_LOAD_STOP, Source<NavigationController>( &shell()->web_contents()->GetController())); - bool result = ExecuteJavaScript(window->web_contents()->GetRenderViewHost(), - "", script); + bool result = ExecuteScript(window->web_contents(), script); load_observer.Wait(); return result; } diff --git a/content/browser/webrtc_browsertest.cc b/content/browser/webrtc_browsertest.cc index 3e5b224..2343e0a 100644 --- a/content/browser/webrtc_browsertest.cc +++ b/content/browser/webrtc_browsertest.cc @@ -30,10 +30,7 @@ class WebrtcBrowserTest: public ContentBrowserTest { } protected: bool ExecuteJavascript(const std::string& javascript) { - RenderViewHost* render_view_host = - shell()->web_contents()->GetRenderViewHost(); - - return ExecuteJavaScript(render_view_host, "", javascript); + return ExecuteScript(shell()->web_contents(), javascript); } void ExpectTitle(const std::string& expected_title) const { diff --git a/content/public/test/browser_test_utils.cc b/content/public/test/browser_test_utils.cc index f793a37..69d0b98cd 100644 --- a/content/public/test/browser_test_utils.cc +++ b/content/public/test/browser_test_utils.cc @@ -39,11 +39,11 @@ namespace { class DOMOperationObserver : public NotificationObserver, public WebContentsObserver { public: - explicit DOMOperationObserver(RenderViewHost* render_view_host) - : WebContentsObserver(WebContents::FromRenderViewHost(render_view_host)), + explicit DOMOperationObserver(RenderViewHost* rvh) + : WebContentsObserver(WebContents::FromRenderViewHost(rvh)), did_respond_(false) { registrar_.Add(this, NOTIFICATION_DOM_OPERATION_RESPONSE, - Source<RenderViewHost>(render_view_host)); + Source<RenderViewHost>(rvh)); message_loop_runner_ = new MessageLoopRunner; } @@ -78,18 +78,18 @@ class DOMOperationObserver : public NotificationObserver, }; // Specifying a prototype so that we can add the WARN_UNUSED_RESULT attribute. -bool ExecuteJavaScriptHelper(RenderViewHost* render_view_host, - const std::string& frame_xpath, - const std::string& original_script, - scoped_ptr<Value>* result) WARN_UNUSED_RESULT; +bool ExecuteScriptHelper(RenderViewHost* render_view_host, + const std::string& frame_xpath, + const std::string& original_script, + scoped_ptr<Value>* result) WARN_UNUSED_RESULT; // Executes the passed |original_script| in the frame pointed to by // |frame_xpath|. If |result| is not NULL, stores the value that the evaluation // of the script in |result|. Returns true on success. -bool ExecuteJavaScriptHelper(RenderViewHost* render_view_host, - const std::string& frame_xpath, - const std::string& original_script, - scoped_ptr<Value>* result) { +bool ExecuteScriptHelper(RenderViewHost* render_view_host, + const std::string& frame_xpath, + const std::string& original_script, + scoped_ptr<Value>* result) { // TODO(jcampan): we should make the domAutomationController not require an // automation id. std::string script = @@ -280,53 +280,93 @@ void SimulateKeyPress(WebContents* web_contents, web_contents->GetRenderViewHost()->ForwardKeyboardEvent(event_up); } -bool ExecuteJavaScript(RenderViewHost* render_view_host, - const std::string& frame_xpath, - const std::string& original_script) { +namespace internal { + +ToRenderViewHost::ToRenderViewHost(WebContents* web_contents) + : render_view_host_(web_contents->GetRenderViewHost()) { +} + +ToRenderViewHost::ToRenderViewHost(RenderViewHost* render_view_host) + : render_view_host_(render_view_host) { +} + +} // namespace internal + +bool ExecuteScriptInFrame(const internal::ToRenderViewHost& adapter, + const std::string& frame_xpath, + const std::string& original_script) { std::string script = original_script + ";window.domAutomationController.send(0);"; - return ExecuteJavaScriptHelper(render_view_host, frame_xpath, script, NULL); + return ExecuteScriptHelper(adapter.render_view_host(), frame_xpath, script, + NULL); } -bool ExecuteJavaScriptAndExtractInt(RenderViewHost* render_view_host, - const std::string& frame_xpath, - const std::string& script, - int* result) { +bool ExecuteScriptInFrameAndExtractInt( + const internal::ToRenderViewHost& adapter, + const std::string& frame_xpath, + const std::string& script, + int* result) { DCHECK(result); scoped_ptr<Value> value; - if (!ExecuteJavaScriptHelper(render_view_host, frame_xpath, script, &value) || - !value.get()) + if (!ExecuteScriptHelper(adapter.render_view_host(), frame_xpath, script, + &value) || !value.get()) return false; return value->GetAsInteger(result); } -bool ExecuteJavaScriptAndExtractBool(RenderViewHost* render_view_host, - const std::string& frame_xpath, - const std::string& script, - bool* result) { +bool ExecuteScriptInFrameAndExtractBool( + const internal::ToRenderViewHost& adapter, + const std::string& frame_xpath, + const std::string& script, + bool* result) { DCHECK(result); scoped_ptr<Value> value; - if (!ExecuteJavaScriptHelper(render_view_host, frame_xpath, script, &value) || - !value.get()) + if (!ExecuteScriptHelper(adapter.render_view_host(), frame_xpath, script, + &value) || !value.get()) return false; return value->GetAsBoolean(result); } -bool ExecuteJavaScriptAndExtractString(RenderViewHost* render_view_host, - const std::string& frame_xpath, - const std::string& script, - std::string* result) { +bool ExecuteScriptInFrameAndExtractString( + const internal::ToRenderViewHost& adapter, + const std::string& frame_xpath, + const std::string& script, + std::string* result) { DCHECK(result); scoped_ptr<Value> value; - if (!ExecuteJavaScriptHelper(render_view_host, frame_xpath, script, &value) || - !value.get()) + if (!ExecuteScriptHelper(adapter.render_view_host(), frame_xpath, script, + &value) || !value.get()) return false; return value->GetAsString(result); } +bool ExecuteScript(const internal::ToRenderViewHost& adapter, + const std::string& script) { + return ExecuteScriptInFrame(adapter, std::string(), script); +} + +bool ExecuteScriptAndExtractInt(const internal::ToRenderViewHost& adapter, + const std::string& script, int* result) { + return ExecuteScriptInFrameAndExtractInt(adapter, std::string(), script, + result); +} + +bool ExecuteScriptAndExtractBool(const internal::ToRenderViewHost& adapter, + const std::string& script, bool* result) { + return ExecuteScriptInFrameAndExtractBool(adapter, std::string(), script, + result); +} + +bool ExecuteScriptAndExtractString(const internal::ToRenderViewHost& adapter, + const std::string& script, + std::string* result) { + return ExecuteScriptInFrameAndExtractString(adapter, std::string(), script, + result); +} + std::string GetCookies(BrowserContext* browser_context, const GURL& url) { std::string cookies; base::WaitableEvent event(true, false); diff --git a/content/public/test/browser_test_utils.h b/content/public/test/browser_test_utils.h index 8f7d380..5ae5644 100644 --- a/content/public/test/browser_test_utils.h +++ b/content/public/test/browser_test_utils.h @@ -78,34 +78,67 @@ void SimulateKeyPress(WebContents* web_contents, bool alt, bool command); +// Allow ExecuteScript* methods to target either a WebContents or a +// RenderViewHost. Targetting a WebContents means executing script in the +// RenderViewHost returned by WebContents::GetRenderViewHost(), which is the +// "current" RenderViewHost. Pass a specific RenderViewHost to target, for +// example, a "swapped-out" RenderViewHost. +namespace internal { +class ToRenderViewHost { + public: + ToRenderViewHost(WebContents* web_contents); + ToRenderViewHost(RenderViewHost* render_view_host); + + RenderViewHost* render_view_host() const { return render_view_host_; } + + private: + RenderViewHost* render_view_host_; +}; +} // namespace internal + // Executes the passed |script| in the frame pointed to by |frame_xpath| (use // empty string for main frame). The |script| should not invoke // domAutomationController.send(); otherwise, your test will hang or be flaky. // If you want to extract a result, use one of the below functions. // Returns true on success. -bool ExecuteJavaScript(RenderViewHost* render_view_host, - const std::string& frame_xpath, - const std::string& script) WARN_UNUSED_RESULT; +bool ExecuteScriptInFrame(const internal::ToRenderViewHost& adapter, + const std::string& frame_xpath, + const std::string& script) WARN_UNUSED_RESULT; // The following methods executes the passed |script| in the frame pointed to by // |frame_xpath| (use empty string for main frame) and sets |result| to the -// value returned by the script evaluation. -// They return true on success, false if the script evaluation failed or did not +// value passed to "window.domAutomationController.send" by the executed script. +// They return true on success, false if the script execution failed or did not // evaluate to the expected type. -bool ExecuteJavaScriptAndExtractInt(RenderViewHost* render_view_host, - const std::string& frame_xpath, - const std::string& script, - int* result) WARN_UNUSED_RESULT; -bool ExecuteJavaScriptAndExtractBool(RenderViewHost* render_view_host, - const std::string& frame_xpath, - const std::string& script, - bool* result) WARN_UNUSED_RESULT; -bool ExecuteJavaScriptAndExtractString( - RenderViewHost* render_view_host, +bool ExecuteScriptInFrameAndExtractInt( + const internal::ToRenderViewHost& adapter, + const std::string& frame_xpath, + const std::string& script, + int* result) WARN_UNUSED_RESULT; +bool ExecuteScriptInFrameAndExtractBool( + const internal::ToRenderViewHost& adapter, + const std::string& frame_xpath, + const std::string& script, + bool* result) WARN_UNUSED_RESULT; +bool ExecuteScriptInFrameAndExtractString( + const internal::ToRenderViewHost& adapter, const std::string& frame_xpath, const std::string& script, std::string* result) WARN_UNUSED_RESULT; +// Top-frame script execution helpers (a.k.a., the common case): +bool ExecuteScript(const internal::ToRenderViewHost& adapter, + const std::string& script) WARN_UNUSED_RESULT; +bool ExecuteScriptAndExtractInt(const internal::ToRenderViewHost& adapter, + const std::string& script, + int* result) WARN_UNUSED_RESULT; +bool ExecuteScriptAndExtractBool(const internal::ToRenderViewHost& adapter, + const std::string& script, + bool* result) WARN_UNUSED_RESULT; +bool ExecuteScriptAndExtractString(const internal::ToRenderViewHost& adapter, + const std::string& script, + std::string* result) WARN_UNUSED_RESULT; + // Returns the cookies for the given url. std::string GetCookies(BrowserContext* browser_context, const GURL& url); |