diff options
author | kkania@chromium.org <kkania@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-18 23:43:11 +0000 |
---|---|---|
committer | kkania@chromium.org <kkania@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-18 23:43:11 +0000 |
commit | a9602de83c13669096f15e07e3f613461c58afbc (patch) | |
tree | 5a0f434b63ef6311b309ec2ca796c58374add0bc /chrome/test/ui_test_utils.cc | |
parent | 9d3bd0e747adae3c4caf9ddccf8e08a4de68e3bc (diff) | |
download | chromium_src-a9602de83c13669096f15e07e3f613461c58afbc.zip chromium_src-a9602de83c13669096f15e07e3f613461c58afbc.tar.gz chromium_src-a9602de83c13669096f15e07e3f613461c58afbc.tar.bz2 |
Add support for interacting with the DOM in browser_tests.
BUG=none
TEST=none
Reivew url: http://codereview.chromium.org/660046
Review URL: http://codereview.chromium.org/1051005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@42026 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/test/ui_test_utils.cc')
-rw-r--r-- | chrome/test/ui_test_utils.cc | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/chrome/test/ui_test_utils.cc b/chrome/test/ui_test_utils.cc index c93ec1c..f04b02e 100644 --- a/chrome/test/ui_test_utils.cc +++ b/chrome/test/ui_test_utils.cc @@ -10,6 +10,7 @@ #include "base/message_loop.h" #include "base/path_service.h" #include "base/process_util.h" +#include "base/utf_string_conversions.h" #include "base/values.h" #include "chrome/browser/browser.h" #include "chrome/browser/browser_list.h" @@ -25,6 +26,7 @@ #include "chrome/common/extensions/extension_action.h" #include "chrome/common/notification_registrar.h" #include "chrome/common/notification_service.h" +#include "chrome/test/automation/javascript_execution_controller.h" #if defined(TOOLKIT_VIEWS) #include "views/focus/accelerator_handler.h" #endif @@ -85,7 +87,8 @@ class NavigationNotificationObserver : public NotificationObserver { class DOMOperationObserver : public NotificationObserver { public: - explicit DOMOperationObserver(RenderViewHost* render_view_host) { + explicit DOMOperationObserver(RenderViewHost* render_view_host) + : did_respond_(false) { registrar_.Add(this, NotificationType::DOM_OPERATION_RESPONSE, Source<RenderViewHost>(render_view_host)); ui_test_utils::RunMessageLoop(); @@ -97,14 +100,21 @@ class DOMOperationObserver : public NotificationObserver { DCHECK(type == NotificationType::DOM_OPERATION_RESPONSE); Details<DomOperationNotificationDetails> dom_op_details(details); response_ = dom_op_details->json(); + did_respond_ = true; MessageLoopForUI::current()->Quit(); } + bool GetResponse(std::string* response) { + *response = response_; + return did_respond_; + } + std::string response() const { return response_; } private: NotificationRegistrar registrar_; std::string response_; + bool did_respond_; DISALLOW_COPY_AND_ASSIGN(DOMOperationObserver); }; @@ -337,6 +347,36 @@ class FindInPageNotificationObserver : public NotificationObserver { DISALLOW_COPY_AND_ASSIGN(FindInPageNotificationObserver); }; +class InProcessJavaScriptExecutionController + : public base::RefCounted<InProcessJavaScriptExecutionController>, + public JavaScriptExecutionController { + public: + explicit InProcessJavaScriptExecutionController( + RenderViewHost* render_view_host) + : render_view_host_(render_view_host) {} + + protected: + // Executes |script| and sets the JSON response |json|. + bool ExecuteJavaScriptAndGetJSON(const std::string& script, + std::string* json) { + render_view_host_->ExecuteJavascriptInWebFrame(L"", UTF8ToWide(script)); + DOMOperationObserver dom_op_observer(render_view_host_); + return dom_op_observer.GetResponse(json); + } + + void FirstObjectAdded() { + AddRef(); + } + + void LastObjectRemoved() { + Release(); + } + + private: + // Weak pointer to the associated RenderViewHost. + RenderViewHost* render_view_host_; +}; + } // namespace void RunMessageLoop() { @@ -427,6 +467,15 @@ void NavigateToURLBlockUntilNavigationsComplete(Browser* browser, WaitForNavigations(controller, number_of_navigations); } +DOMElementProxyRef GetActiveDOMDocument(Browser* browser) { + JavaScriptExecutionController* executor = + new InProcessJavaScriptExecutionController( + browser->GetSelectedTabContents()->render_view_host()); + DOMElementProxy* main_doc = NULL; + executor->ExecuteJavaScriptAndParse("document;", &main_doc); + return main_doc; +} + Value* ExecuteJavaScript(RenderViewHost* render_view_host, const std::wstring& frame_xpath, const std::wstring& original_script) { |