Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(793)

Side by Side Diff: chrome/test/base/ui_test_utils.cc

Issue 10795090: Move ExecuteJavaScript functions from ui_test_utils.h to browser_test_utils.h so they can be reused… (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/test/base/ui_test_utils.h ('k') | chrome/test/gpu/gpu_pixel_browsertest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/test/base/ui_test_utils.h" 5 #include "chrome/test/base/ui_test_utils.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <windows.h> 8 #include <windows.h>
9 #endif 9 #endif
10 10
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 using content::RenderWidgetHost; 88 using content::RenderWidgetHost;
89 using content::Referrer; 89 using content::Referrer;
90 using content::WebContents; 90 using content::WebContents;
91 91
92 static const int kDefaultWsPort = 8880; 92 static const int kDefaultWsPort = 8880;
93 93
94 namespace ui_test_utils { 94 namespace ui_test_utils {
95 95
96 namespace { 96 namespace {
97 97
98 class DOMOperationObserver : public content::NotificationObserver,
99 public content::WebContentsObserver {
100 public:
101 explicit DOMOperationObserver(RenderViewHost* render_view_host)
102 : content::WebContentsObserver(
103 WebContents::FromRenderViewHost(render_view_host)),
104 did_respond_(false) {
105 registrar_.Add(this, content::NOTIFICATION_DOM_OPERATION_RESPONSE,
106 content::Source<RenderViewHost>(render_view_host));
107 message_loop_runner_ = new content::MessageLoopRunner;
108 }
109
110 virtual void Observe(int type,
111 const content::NotificationSource& source,
112 const content::NotificationDetails& details) OVERRIDE {
113 DCHECK(type == content::NOTIFICATION_DOM_OPERATION_RESPONSE);
114 content::Details<DomOperationNotificationDetails> dom_op_details(details);
115 response_ = dom_op_details->json;
116 did_respond_ = true;
117 message_loop_runner_->Quit();
118 }
119
120 // Overridden from content::WebContentsObserver:
121 virtual void RenderViewGone(base::TerminationStatus status) OVERRIDE {
122 message_loop_runner_->Quit();
123 }
124
125 bool WaitAndGetResponse(std::string* response) WARN_UNUSED_RESULT {
126 message_loop_runner_->Run();
127 *response = response_;
128 return did_respond_;
129 }
130
131 private:
132 content::NotificationRegistrar registrar_;
133 std::string response_;
134 bool did_respond_;
135 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
136
137 DISALLOW_COPY_AND_ASSIGN(DOMOperationObserver);
138 };
139
140 class FindInPageNotificationObserver : public content::NotificationObserver { 98 class FindInPageNotificationObserver : public content::NotificationObserver {
141 public: 99 public:
142 explicit FindInPageNotificationObserver(TabContents* parent_tab) 100 explicit FindInPageNotificationObserver(TabContents* parent_tab)
143 : parent_tab_(parent_tab), 101 : parent_tab_(parent_tab),
144 active_match_ordinal_(-1), 102 active_match_ordinal_(-1),
145 number_of_matches_(0) { 103 number_of_matches_(0) {
146 current_find_request_id_ = 104 current_find_request_id_ =
147 parent_tab->find_tab_helper()->current_find_request_id(); 105 parent_tab->find_tab_helper()->current_find_request_id();
148 registrar_.Add(this, chrome::NOTIFICATION_FIND_RESULT_AVAILABLE, 106 registrar_.Add(this, chrome::NOTIFICATION_FIND_RESULT_AVAILABLE,
149 content::Source<WebContents>(parent_tab_->web_contents())); 107 content::Source<WebContents>(parent_tab_->web_contents()));
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 int active_match_ordinal_; 142 int active_match_ordinal_;
185 int number_of_matches_; 143 int number_of_matches_;
186 // The id of the current find request, obtained from WebContents. Allows us 144 // The id of the current find request, obtained from WebContents. Allows us
187 // to monitor when the search completes. 145 // to monitor when the search completes.
188 int current_find_request_id_; 146 int current_find_request_id_;
189 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; 147 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
190 148
191 DISALLOW_COPY_AND_ASSIGN(FindInPageNotificationObserver); 149 DISALLOW_COPY_AND_ASSIGN(FindInPageNotificationObserver);
192 }; 150 };
193 151
194 // Specifying a prototype so that we can add the WARN_UNUSED_RESULT attribute.
195 bool ExecuteJavaScriptHelper(RenderViewHost* render_view_host,
196 const std::wstring& frame_xpath,
197 const std::wstring& original_script,
198 scoped_ptr<Value>* result) WARN_UNUSED_RESULT;
199
200 // Executes the passed |original_script| in the frame pointed to by
201 // |frame_xpath|. If |result| is not NULL, stores the value that the evaluation
202 // of the script in |result|. Returns true on success.
203 bool ExecuteJavaScriptHelper(RenderViewHost* render_view_host,
204 const std::wstring& frame_xpath,
205 const std::wstring& original_script,
206 scoped_ptr<Value>* result) {
207 // TODO(jcampan): we should make the domAutomationController not require an
208 // automation id.
209 std::wstring script = L"window.domAutomationController.setAutomationId(0);" +
210 original_script;
211 DOMOperationObserver dom_op_observer(render_view_host);
212 render_view_host->ExecuteJavascriptInWebFrame(WideToUTF16Hack(frame_xpath),
213 WideToUTF16Hack(script));
214 std::string json;
215 if (!dom_op_observer.WaitAndGetResponse(&json)) {
216 DLOG(ERROR) << "Cannot communicate with DOMOperationObserver.";
217 return false;
218 }
219
220 // Nothing more to do for callers that ignore the returned JS value.
221 if (!result)
222 return true;
223
224 base::JSONReader reader(base::JSON_ALLOW_TRAILING_COMMAS);
225 result->reset(reader.ReadToValue(json));
226 if (!result->get()) {
227 DLOG(ERROR) << reader.GetErrorMessage();
228 return false;
229 }
230
231 return true;
232 }
233
234 void RunAllPendingMessageAndSendQuit(content::BrowserThread::ID thread_id, 152 void RunAllPendingMessageAndSendQuit(content::BrowserThread::ID thread_id,
235 const base::Closure& quit_task) { 153 const base::Closure& quit_task) {
236 MessageLoop::current()->PostTask(FROM_HERE, 154 MessageLoop::current()->PostTask(FROM_HERE,
237 MessageLoop::QuitWhenIdleClosure()); 155 MessageLoop::QuitWhenIdleClosure());
238 RunMessageLoop(); 156 RunMessageLoop();
239 content::BrowserThread::PostTask(thread_id, FROM_HERE, quit_task); 157 content::BrowserThread::PostTask(thread_id, FROM_HERE, quit_task);
240 } 158 }
241 159
242 } // namespace 160 } // namespace
243 161
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 const GURL& url, 357 const GURL& url,
440 int number_of_navigations) { 358 int number_of_navigations) {
441 NavigateToURLWithDispositionBlockUntilNavigationsComplete( 359 NavigateToURLWithDispositionBlockUntilNavigationsComplete(
442 browser, 360 browser,
443 url, 361 url,
444 number_of_navigations, 362 number_of_navigations,
445 CURRENT_TAB, 363 CURRENT_TAB,
446 BROWSER_TEST_WAIT_FOR_NAVIGATION); 364 BROWSER_TEST_WAIT_FOR_NAVIGATION);
447 } 365 }
448 366
449 bool ExecuteJavaScript(RenderViewHost* render_view_host,
450 const std::wstring& frame_xpath,
451 const std::wstring& original_script) {
452 std::wstring script =
453 original_script + L";window.domAutomationController.send(0);";
454 return ExecuteJavaScriptHelper(render_view_host, frame_xpath, script, NULL);
455 }
456
457 bool ExecuteJavaScriptAndExtractInt(RenderViewHost* render_view_host,
458 const std::wstring& frame_xpath,
459 const std::wstring& script,
460 int* result) {
461 DCHECK(result);
462 scoped_ptr<Value> value;
463 if (!ExecuteJavaScriptHelper(render_view_host, frame_xpath, script, &value) ||
464 !value.get())
465 return false;
466
467 return value->GetAsInteger(result);
468 }
469
470 bool ExecuteJavaScriptAndExtractBool(RenderViewHost* render_view_host,
471 const std::wstring& frame_xpath,
472 const std::wstring& script,
473 bool* result) {
474 DCHECK(result);
475 scoped_ptr<Value> value;
476 if (!ExecuteJavaScriptHelper(render_view_host, frame_xpath, script, &value) ||
477 !value.get())
478 return false;
479
480 return value->GetAsBoolean(result);
481 }
482
483 bool ExecuteJavaScriptAndExtractString(RenderViewHost* render_view_host,
484 const std::wstring& frame_xpath,
485 const std::wstring& script,
486 std::string* result) {
487 DCHECK(result);
488 scoped_ptr<Value> value;
489 if (!ExecuteJavaScriptHelper(render_view_host, frame_xpath, script, &value) ||
490 !value.get())
491 return false;
492
493 return value->GetAsString(result);
494 }
495
496 FilePath GetTestFilePath(const FilePath& dir, const FilePath& file) { 367 FilePath GetTestFilePath(const FilePath& dir, const FilePath& file) {
497 FilePath path; 368 FilePath path;
498 PathService::Get(chrome::DIR_TEST_DATA, &path); 369 PathService::Get(chrome::DIR_TEST_DATA, &path);
499 return path.Append(dir).Append(file); 370 return path.Append(dir).Append(file);
500 } 371 }
501 372
502 GURL GetTestUrl(const FilePath& dir, const FilePath& file) { 373 GURL GetTestUrl(const FilePath& dir, const FilePath& file) {
503 return net::FilePathToFileURL(GetTestFilePath(dir, file)); 374 return net::FilePathToFileURL(GetTestFilePath(dir, file));
504 } 375 }
505 376
(...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after
938 message_loop_runner_->Run(); 809 message_loop_runner_->Run();
939 return snapshot_taken_; 810 return snapshot_taken_;
940 } 811 }
941 812
942 bool TakeEntirePageSnapshot(RenderViewHost* rvh, 813 bool TakeEntirePageSnapshot(RenderViewHost* rvh,
943 SkBitmap* bitmap) WARN_UNUSED_RESULT { 814 SkBitmap* bitmap) WARN_UNUSED_RESULT {
944 const wchar_t* script = 815 const wchar_t* script =
945 L"window.domAutomationController.send(" 816 L"window.domAutomationController.send("
946 L" JSON.stringify([document.width, document.height]))"; 817 L" JSON.stringify([document.width, document.height]))";
947 std::string json; 818 std::string json;
948 if (!ui_test_utils::ExecuteJavaScriptAndExtractString( 819 if (!content::ExecuteJavaScriptAndExtractString(rvh, L"", script, &json))
949 rvh, L"", script, &json))
950 return false; 820 return false;
951 821
952 // Parse the JSON. 822 // Parse the JSON.
953 std::vector<int> dimensions; 823 std::vector<int> dimensions;
954 scoped_ptr<Value> value( 824 scoped_ptr<Value> value(
955 base::JSONReader::Read(json, base::JSON_ALLOW_TRAILING_COMMAS)); 825 base::JSONReader::Read(json, base::JSON_ALLOW_TRAILING_COMMAS));
956 if (!value->IsType(Value::TYPE_LIST)) 826 if (!value->IsType(Value::TYPE_LIST))
957 return false; 827 return false;
958 ListValue* list = static_cast<ListValue*>(value.get()); 828 ListValue* list = static_cast<ListValue*>(value.get());
959 int width, height; 829 int width, height;
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
1015 int state, 885 int state,
1016 const base::Closure& followup) { 886 const base::Closure& followup) {
1017 if (!followup.is_null()) 887 if (!followup.is_null())
1018 ui_controls::SendMouseEventsNotifyWhenDone(button, state, followup); 888 ui_controls::SendMouseEventsNotifyWhenDone(button, state, followup);
1019 else 889 else
1020 ui_controls::SendMouseEvents(button, state); 890 ui_controls::SendMouseEvents(button, state);
1021 } 891 }
1022 892
1023 } // namespace internal 893 } // namespace internal
1024 } // namespace ui_test_utils 894 } // namespace ui_test_utils
OLDNEW
« no previous file with comments | « chrome/test/base/ui_test_utils.h ('k') | chrome/test/gpu/gpu_pixel_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698