diff options
author | avi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-15 01:08:15 +0000 |
---|---|---|
committer | avi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-15 01:08:15 +0000 |
commit | ff4657ee6daaa1470e70e1cd12f4f936ac02a631 (patch) | |
tree | 0c698502cfb42adbee636abd691606add549784c | |
parent | b0f4e50d43f4d1f8454bda67ab3540ee0e6fbcd5 (diff) | |
download | chromium_src-ff4657ee6daaa1470e70e1cd12f4f936ac02a631.zip chromium_src-ff4657ee6daaa1470e70e1cd12f4f936ac02a631.tar.gz chromium_src-ff4657ee6daaa1470e70e1cd12f4f936ac02a631.tar.bz2 |
Move browser_test_utils's ExecuteScript* to use RenderFrameHost.
BUG=304341
TEST=no change
Review URL: https://codereview.chromium.org/194903008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@257281 0039d316-1c4b-4281-b951-d872f2087c98
9 files changed, 171 insertions, 69 deletions
diff --git a/chrome/browser/chromeos/extensions/virtual_keyboard_browsertest.cc b/chrome/browser/chromeos/extensions/virtual_keyboard_browsertest.cc index a2da9e0..34ec260 100644 --- a/chrome/browser/chromeos/extensions/virtual_keyboard_browsertest.cc +++ b/chrome/browser/chromeos/extensions/virtual_keyboard_browsertest.cc @@ -50,13 +50,13 @@ class VirtualKeyboardBrowserTest : public InProcessBrowserTest { keyboard::switches::kEnableVirtualKeyboard); } - //Injects javascript in |file| into the keyboard page and runs test methods. + // Injects javascript in |file| into the keyboard page and runs test methods. void RunTest(const base::FilePath& file) { ui_test_utils::NavigateToURL(browser(), GURL("chrome://keyboard")); - content::RenderViewHost* rvh = browser()->tab_strip_model() - ->GetActiveWebContents()->GetRenderViewHost(); - ASSERT_TRUE(rvh); + content::WebContents* web_contents = + browser()->tab_strip_model()->GetActiveWebContents(); + ASSERT_TRUE(web_contents); // Inject testing scripts. InjectJavascript(kWebuiTestDir, kMockController); @@ -64,11 +64,11 @@ class VirtualKeyboardBrowserTest : public InProcessBrowserTest { InjectJavascript(kVirtualKeyboardTestDir, kBaseKeyboardTestFramework); InjectJavascript(kVirtualKeyboardTestDir, file); - ASSERT_TRUE(content::ExecuteScript(rvh, utf8_content_)); + ASSERT_TRUE(content::ExecuteScript(web_contents, utf8_content_)); // Inject DOM-automation test harness and run tests. std::vector<int> resource_ids; - EXPECT_TRUE(ExecuteWebUIResourceTest(rvh, resource_ids)); + EXPECT_TRUE(ExecuteWebUIResourceTest(web_contents, resource_ids)); } void showVirtualKeyboard() { diff --git a/chrome/browser/chromeos/file_manager/file_manager_jstest.cc b/chrome/browser/chromeos/file_manager/file_manager_jstest.cc index da77e43..a4cddc1 100644 --- a/chrome/browser/chromeos/file_manager/file_manager_jstest.cc +++ b/chrome/browser/chromeos/file_manager/file_manager_jstest.cc @@ -19,12 +19,12 @@ class FileManagerJsTest : public InProcessBrowserTest { base::FilePath(FILE_PATH_LITERAL("file_manager/unit_tests")), file); ui_test_utils::NavigateToURL(browser(), url); - content::RenderViewHost* rvh = browser()->tab_strip_model() - ->GetActiveWebContents()->GetRenderViewHost(); - ASSERT_TRUE(rvh); + content::WebContents* web_contents = + browser()->tab_strip_model()->GetActiveWebContents(); + ASSERT_TRUE(web_contents); const std::vector<int> empty_libraries; - EXPECT_TRUE(ExecuteWebUIResourceTest(rvh, empty_libraries)); + EXPECT_TRUE(ExecuteWebUIResourceTest(web_contents, empty_libraries)); } }; diff --git a/chrome/browser/extensions/api/extension_action/browser_action_apitest.cc b/chrome/browser/extensions/api/extension_action/browser_action_apitest.cc index 8953f954..aeffedf 100644 --- a/chrome/browser/extensions/api/extension_action/browser_action_apitest.cc +++ b/chrome/browser/extensions/api/extension_action/browser_action_apitest.cc @@ -729,9 +729,7 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, TestTriggerBrowserAction) { "window.domAutomationController.send(document.body.style." "backgroundColor);"; std::string result; - const std::string frame_xpath; - EXPECT_TRUE(content::ExecuteScriptInFrameAndExtractString( - tab, frame_xpath, script, &result)); + EXPECT_TRUE(content::ExecuteScriptAndExtractString(tab, script, &result)); EXPECT_EQ(result, "red"); } diff --git a/chrome/browser/extensions/api/extension_action/page_action_apitest.cc b/chrome/browser/extensions/api/extension_action/page_action_apitest.cc index 81bbc14..2247f7f 100644 --- a/chrome/browser/extensions/api/extension_action/page_action_apitest.cc +++ b/chrome/browser/extensions/api/extension_action/page_action_apitest.cc @@ -265,9 +265,7 @@ IN_PROC_BROWSER_TEST_F(PageActionApiTest, TestTriggerPageAction) { "window.domAutomationController.send(document.body.style." "backgroundColor);"; std::string result; - const std::string frame_xpath; - EXPECT_TRUE(content::ExecuteScriptInFrameAndExtractString( - tab, frame_xpath, script, &result)); + EXPECT_TRUE(content::ExecuteScriptAndExtractString(tab, script, &result)); EXPECT_EQ(result, "red"); } diff --git a/chrome/test/base/tracing_browsertest.cc b/chrome/test/base/tracing_browsertest.cc index 789f3f4..5a6b6f4 100644 --- a/chrome/test/base/tracing_browsertest.cc +++ b/chrome/test/base/tracing_browsertest.cc @@ -28,7 +28,8 @@ const char* g_event = "TheEvent"; class TracingBrowserTest : public InProcessBrowserTest { protected: // Execute some no-op javascript on the current tab - this triggers a trace - // event in RenderViewImpl::OnScriptEvalRequest (from the renderer process). + // event in RenderFrameImpl::OnJavaScriptExecuteRequest (from the renderer + // process). void ExecuteJavascriptOnCurrentTab() { content::RenderViewHost* rvh = browser()->tab_strip_model()-> GetActiveWebContents()->GetRenderViewHost(); @@ -79,7 +80,7 @@ IN_PROC_BROWSER_TEST_F(TracingBrowserTest, BeginTracingWithWatch) { // Child process events from same process. ASSERT_TRUE(BeginTracingWithWatch(g_category, g_category, - "OnScriptEvalRequest", 2)); + "OnJavaScriptExecuteRequest", 2)); ASSERT_NO_FATAL_FAILURE(ExecuteJavascriptOnCurrentTab()); ASSERT_NO_FATAL_FAILURE(ExecuteJavascriptOnCurrentTab()); EXPECT_TRUE(WaitForWatchEvent(no_timeout)); @@ -89,7 +90,7 @@ IN_PROC_BROWSER_TEST_F(TracingBrowserTest, BeginTracingWithWatch) { GURL url1("chrome://tracing/"); GURL url2("chrome://credits/"); ASSERT_TRUE(BeginTracingWithWatch(g_category, g_category, - "OnScriptEvalRequest", 2)); + "OnJavaScriptExecuteRequest", 2)); // Open two tabs to different URLs to encourage two separate renderer // processes. Each will fire an event that will be counted towards the total. ui_test_utils::NavigateToURLWithDisposition(browser(), url1, diff --git a/chrome/test/data/webui/webui_resource_browsertest.cc b/chrome/test/data/webui/webui_resource_browsertest.cc index 4a9efe7..4927f35 100644 --- a/chrome/test/data/webui/webui_resource_browsertest.cc +++ b/chrome/test/data/webui/webui_resource_browsertest.cc @@ -9,7 +9,6 @@ #include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/ui_test_utils.h" #include "content/public/browser/notification_registrar.h" -#include "content/public/browser/render_view_host.h" #include "content/public/browser/web_contents.h" #include "content/public/test/browser_test_utils.h" #include "grit/webui_resources.h" @@ -22,10 +21,10 @@ class WebUIResourceBrowserTest : public InProcessBrowserTest { base::FilePath(FILE_PATH_LITERAL("webui")), file); ui_test_utils::NavigateToURL(browser(), url); - content::RenderViewHost* rvh = browser()->tab_strip_model() - ->GetActiveWebContents()->GetRenderViewHost(); - ASSERT_TRUE(rvh); - EXPECT_TRUE(ExecuteWebUIResourceTest(rvh, include_libraries_)); + content::WebContents* web_contents = + browser()->tab_strip_model()->GetActiveWebContents(); + ASSERT_TRUE(web_contents); + EXPECT_TRUE(ExecuteWebUIResourceTest(web_contents, include_libraries_)); } // Queues the library corresponding to |resource_id| for injection into the diff --git a/content/public/test/browser_test_utils.cc b/content/public/test/browser_test_utils.cc index 96d3a3e..dd4a918 100644 --- a/content/public/test/browser_test_utils.cc +++ b/content/public/test/browser_test_utils.cc @@ -18,6 +18,7 @@ #include "content/public/browser/dom_operation_notification_details.h" #include "content/public/browser/notification_service.h" #include "content/public/browser/notification_types.h" +#include "content/public/browser/render_frame_host.h" #include "content/public/browser/render_process_host.h" #include "content/public/browser/render_view_host.h" #include "content/public/browser/web_contents.h" @@ -79,18 +80,25 @@ class DOMOperationObserver : public NotificationObserver, }; // Specifying a prototype so that we can add the WARN_UNUSED_RESULT attribute. -bool ExecuteScriptHelper(RenderViewHost* render_view_host, - const std::string& frame_xpath, - const std::string& original_script, - scoped_ptr<base::Value>* result) WARN_UNUSED_RESULT; +bool ExecuteScriptInFrameHelper( + RenderViewHost* render_view_host, + const std::string& frame_xpath, + const std::string& original_script, + scoped_ptr<base::Value>* result) WARN_UNUSED_RESULT; + +// Specifying a prototype so that we can add the WARN_UNUSED_RESULT attribute. +bool ExecuteScriptHelper( + RenderFrameHost* render_frame_host, + const std::string& original_script, + scoped_ptr<base::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 ExecuteScriptHelper(RenderViewHost* render_view_host, - const std::string& frame_xpath, - const std::string& original_script, - scoped_ptr<base::Value>* result) { +bool ExecuteScriptInFrameHelper(RenderViewHost* render_view_host, + const std::string& frame_xpath, + const std::string& original_script, + scoped_ptr<base::Value>* result) { // TODO(jcampan): we should make the domAutomationController not require an // automation id. std::string script = @@ -118,6 +126,38 @@ bool ExecuteScriptHelper(RenderViewHost* render_view_host, return true; } +// Executes the passed |original_script| in the frame specified by +// |render_frame_host|. If |result| is not NULL, stores the value that the +// evaluation of the script in |result|. Returns true on success. +bool ExecuteScriptHelper(RenderFrameHost* render_frame_host, + const std::string& original_script, + scoped_ptr<base::Value>* result) { + // TODO(jcampan): we should make the domAutomationController not require an + // automation id. + std::string script = + "window.domAutomationController.setAutomationId(0);" + original_script; + DOMOperationObserver dom_op_observer(render_frame_host->GetRenderViewHost()); + render_frame_host->ExecuteJavaScript(base::UTF8ToUTF16(script)); + std::string json; + if (!dom_op_observer.WaitAndGetResponse(&json)) { + DLOG(ERROR) << "Cannot communicate with DOMOperationObserver."; + return false; + } + + // Nothing more to do for callers that ignore the returned JS value. + if (!result) + return true; + + base::JSONReader reader(base::JSON_ALLOW_TRAILING_COMMAS); + result->reset(reader.ReadToValue(json)); + if (!result->get()) { + DLOG(ERROR) << reader.GetErrorMessage(); + return false; + } + + return true; +} + void BuildSimpleWebKeyEvent(blink::WebInputEvent::Type type, ui::KeyboardCode key_code, int native_key_code, @@ -397,6 +437,18 @@ ToRenderViewHost::ToRenderViewHost(RenderViewHost* render_view_host) : render_view_host_(render_view_host) { } +ToRenderFrameHost::ToRenderFrameHost(WebContents* web_contents) + : render_frame_host_(web_contents->GetMainFrame()) { +} + +ToRenderFrameHost::ToRenderFrameHost(RenderViewHost* render_view_host) + : render_frame_host_(render_view_host->GetMainFrame()) { +} + +ToRenderFrameHost::ToRenderFrameHost(RenderFrameHost* render_frame_host) + : render_frame_host_(render_frame_host) { +} + } // namespace internal bool ExecuteScriptInFrame(const internal::ToRenderViewHost& adapter, @@ -404,8 +456,8 @@ bool ExecuteScriptInFrame(const internal::ToRenderViewHost& adapter, const std::string& original_script) { std::string script = original_script + ";window.domAutomationController.send(0);"; - return ExecuteScriptHelper(adapter.render_view_host(), frame_xpath, script, - NULL); + return ExecuteScriptInFrameHelper( + adapter.render_view_host(), frame_xpath, script, NULL); } bool ExecuteScriptInFrameAndExtractInt( @@ -415,9 +467,11 @@ bool ExecuteScriptInFrameAndExtractInt( int* result) { DCHECK(result); scoped_ptr<base::Value> value; - if (!ExecuteScriptHelper(adapter.render_view_host(), frame_xpath, script, - &value) || !value.get()) + if (!ExecuteScriptInFrameHelper(adapter.render_view_host(), + frame_xpath, script, &value) || + !value.get()) { return false; + } return value->GetAsInteger(result); } @@ -429,9 +483,11 @@ bool ExecuteScriptInFrameAndExtractBool( bool* result) { DCHECK(result); scoped_ptr<base::Value> value; - if (!ExecuteScriptHelper(adapter.render_view_host(), frame_xpath, script, - &value) || !value.get()) + if (!ExecuteScriptInFrameHelper(adapter.render_view_host(), + frame_xpath, script, &value) || + !value.get()) { return false; + } return value->GetAsBoolean(result); } @@ -443,40 +499,61 @@ bool ExecuteScriptInFrameAndExtractString( std::string* result) { DCHECK(result); scoped_ptr<base::Value> value; - if (!ExecuteScriptHelper(adapter.render_view_host(), frame_xpath, script, - &value) || !value.get()) + if (!ExecuteScriptInFrameHelper(adapter.render_view_host(), + frame_xpath, script, &value) || + !value.get()) { return false; + } return value->GetAsString(result); } -bool ExecuteScript(const internal::ToRenderViewHost& adapter, +bool ExecuteScript(const internal::ToRenderFrameHost& adapter, const std::string& script) { - return ExecuteScriptInFrame(adapter, std::string(), script); + std::string new_script = + script + ";window.domAutomationController.send(0);"; + return ExecuteScriptHelper(adapter.render_frame_host(), new_script, NULL); } -bool ExecuteScriptAndExtractInt(const internal::ToRenderViewHost& adapter, +bool ExecuteScriptAndExtractInt(const internal::ToRenderFrameHost& adapter, const std::string& script, int* result) { - return ExecuteScriptInFrameAndExtractInt(adapter, std::string(), script, - result); + DCHECK(result); + scoped_ptr<base::Value> value; + if (!ExecuteScriptHelper(adapter.render_frame_host(), script, &value) || + !value.get()) { + return false; + } + + return value->GetAsInteger(result); } -bool ExecuteScriptAndExtractBool(const internal::ToRenderViewHost& adapter, +bool ExecuteScriptAndExtractBool(const internal::ToRenderFrameHost& adapter, const std::string& script, bool* result) { - return ExecuteScriptInFrameAndExtractBool(adapter, std::string(), script, - result); + DCHECK(result); + scoped_ptr<base::Value> value; + if (!ExecuteScriptHelper(adapter.render_frame_host(), script, &value) || + !value.get()) { + return false; + } + + return value->GetAsBoolean(result); } -bool ExecuteScriptAndExtractString(const internal::ToRenderViewHost& adapter, +bool ExecuteScriptAndExtractString(const internal::ToRenderFrameHost& adapter, const std::string& script, std::string* result) { - return ExecuteScriptInFrameAndExtractString(adapter, std::string(), script, - result); + DCHECK(result); + scoped_ptr<base::Value> value; + if (!ExecuteScriptHelper(adapter.render_frame_host(), script, &value) || + !value.get()) { + return false; + } + + return value->GetAsString(result); } -bool ExecuteWebUIResourceTest( - const internal::ToRenderViewHost& adapter, - const std::vector<int>& js_resource_ids) { +bool ExecuteWebUIResourceTest(WebContents* web_contents, + const std::vector<int>& js_resource_ids) { // Inject WebUI test runner script first prior to other scripts required to // run the test as scripts may depend on it being declared. std::vector<int> ids; @@ -491,11 +568,11 @@ bool ExecuteWebUIResourceTest( .AppendToString(&script); script.append("\n"); } - if (!content::ExecuteScript(adapter, script)) + if (!content::ExecuteScript(web_contents, script)) return false; content::DOMMessageQueue message_queue; - if (!content::ExecuteScript(adapter, "runTests()")) + if (!content::ExecuteScript(web_contents, "runTests()")) return false; std::string message; diff --git a/content/public/test/browser_test_utils.h b/content/public/test/browser_test_utils.h index 0457714..50310c0 100644 --- a/content/public/test/browser_test_utils.h +++ b/content/public/test/browser_test_utils.h @@ -113,12 +113,14 @@ void SimulateKeyPressWithCode(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 +namespace internal { +// Allow ExecuteScriptInFrame* methods to target either a WebContents or a +// RenderViewHost. Targetting a WebContents means executing the 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 { +// OBSOLETE; DO NOT USE! Use ExecuteScript* methods that target a +// RenderFrameHost instead. class ToRenderViewHost { public: ToRenderViewHost(WebContents* web_contents); @@ -129,13 +131,30 @@ class ToRenderViewHost { private: RenderViewHost* render_view_host_; }; + +// Allow ExecuteScript* methods to target either a WebContents or a +// RenderFrameHost. Targetting a WebContents means executing the script in the +// RenderFrameHost returned by WebContents::GetMainFrame(), which is the +// main frame. Pass a specific RenderFrameHost to target it. +class ToRenderFrameHost { + public: + ToRenderFrameHost(WebContents* web_contents); + ToRenderFrameHost(RenderViewHost* render_view_host); + ToRenderFrameHost(RenderFrameHost* render_frame_host); + + RenderFrameHost* render_frame_host() const { return render_frame_host_; } + + private: + RenderFrameHost* render_frame_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 +// 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. +// OBSOLETE; DO NOT USE! Use ExecuteScript and specify a RenderFrameHost. bool ExecuteScriptInFrame(const internal::ToRenderViewHost& adapter, const std::string& frame_xpath, const std::string& script) WARN_UNUSED_RESULT; @@ -145,6 +164,8 @@ bool ExecuteScriptInFrame(const internal::ToRenderViewHost& adapter, // 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. +// OBSOLETE; DO NOT USE! Use ExecuteScriptAndExtract[Int|Bool|String] and +// specify a RenderFrameHost. bool ExecuteScriptInFrameAndExtractInt( const internal::ToRenderViewHost& adapter, const std::string& frame_xpath, @@ -161,16 +182,24 @@ bool ExecuteScriptInFrameAndExtractString( 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, +// Executes the passed |script| in the specified 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 ExecuteScript(const internal::ToRenderFrameHost& adapter, const std::string& script) WARN_UNUSED_RESULT; -bool ExecuteScriptAndExtractInt(const internal::ToRenderViewHost& adapter, + +// The following methods executes the passed |script| in the specified frame and +// sets |result| to the 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 ExecuteScriptAndExtractInt(const internal::ToRenderFrameHost& adapter, const std::string& script, int* result) WARN_UNUSED_RESULT; -bool ExecuteScriptAndExtractBool(const internal::ToRenderViewHost& adapter, +bool ExecuteScriptAndExtractBool(const internal::ToRenderFrameHost& adapter, const std::string& script, bool* result) WARN_UNUSED_RESULT; -bool ExecuteScriptAndExtractString(const internal::ToRenderViewHost& adapter, +bool ExecuteScriptAndExtractString(const internal::ToRenderFrameHost& adapter, const std::string& script, std::string* result) WARN_UNUSED_RESULT; @@ -178,7 +207,7 @@ bool ExecuteScriptAndExtractString(const internal::ToRenderViewHost& adapter, // |js_resource_ids| prior to executing the tests. // // Returns true if tests ran successfully, false otherwise. -bool ExecuteWebUIResourceTest(const internal::ToRenderViewHost& adapter, +bool ExecuteWebUIResourceTest(WebContents* web_contents, const std::vector<int>& js_resource_ids); // Returns the cookies for the given url. diff --git a/content/test/webui_resource_browsertest.cc b/content/test/webui_resource_browsertest.cc index 71243be..18ee37e 100644 --- a/content/test/webui_resource_browsertest.cc +++ b/content/test/webui_resource_browsertest.cc @@ -27,9 +27,9 @@ class WebUIResourceBrowserTest : public ContentBrowserTest { ASSERT_TRUE(PathExists(file)); NavigateToURL(shell(), net::FilePathToFileURL(file)); - RenderViewHost* rvh = shell()->web_contents()->GetRenderViewHost(); - ASSERT_TRUE(rvh); - EXPECT_TRUE(ExecuteWebUIResourceTest(rvh, include_libraries_)); + content::WebContents* web_contents = shell()->web_contents(); + ASSERT_TRUE(web_contents); + EXPECT_TRUE(ExecuteWebUIResourceTest(web_contents, include_libraries_)); } void RunMediaInternalsTest(const base::FilePath::CharType* file) { |