diff options
author | kkania@chromium.org <kkania@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-17 00:19:32 +0000 |
---|---|---|
committer | kkania@chromium.org <kkania@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-17 00:19:32 +0000 |
commit | c158a6699a9f5e5f56d913c0b06e0d83e8358591 (patch) | |
tree | 727e3eb0a5aac10658e35d2e389b057faed103bf /chrome/test/ui_test_utils.cc | |
parent | c7eca5b2c6f9a527d18d00486d2d7cd7dca9c7b1 (diff) | |
download | chromium_src-c158a6699a9f5e5f56d913c0b06e0d83e8358591.zip chromium_src-c158a6699a9f5e5f56d913c0b06e0d83e8358591.tar.gz chromium_src-c158a6699a9f5e5f56d913c0b06e0d83e8358591.tar.bz2 |
Add support for DOM interaction in browser tests via C++.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/660046
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@41789 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 b2b2258..2fb356c 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) { |