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

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

Issue 10479018: Add base::RunLoop and update ui_test_utils to use it to reduce flakiness (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: added printfs to debug timeout which doesn't happen locally Created 8 years, 6 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
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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 97
98 class DOMOperationObserver : public content::NotificationObserver, 98 class DOMOperationObserver : public content::NotificationObserver,
99 public content::WebContentsObserver { 99 public content::WebContentsObserver {
100 public: 100 public:
101 explicit DOMOperationObserver(RenderViewHost* render_view_host) 101 explicit DOMOperationObserver(RenderViewHost* render_view_host)
102 : content::WebContentsObserver( 102 : content::WebContentsObserver(
103 WebContents::FromRenderViewHost(render_view_host)), 103 WebContents::FromRenderViewHost(render_view_host)),
104 did_respond_(false) { 104 did_respond_(false) {
105 registrar_.Add(this, content::NOTIFICATION_DOM_OPERATION_RESPONSE, 105 registrar_.Add(this, content::NOTIFICATION_DOM_OPERATION_RESPONSE,
106 content::Source<RenderViewHost>(render_view_host)); 106 content::Source<RenderViewHost>(render_view_host));
107 ui_test_utils::RunMessageLoop(); 107 message_loop_runner_.Run();
108 } 108 }
109 109
110 virtual void Observe(int type, 110 virtual void Observe(int type,
111 const content::NotificationSource& source, 111 const content::NotificationSource& source,
112 const content::NotificationDetails& details) OVERRIDE { 112 const content::NotificationDetails& details) OVERRIDE {
113 DCHECK(type == content::NOTIFICATION_DOM_OPERATION_RESPONSE); 113 DCHECK(type == content::NOTIFICATION_DOM_OPERATION_RESPONSE);
114 content::Details<DomOperationNotificationDetails> dom_op_details(details); 114 content::Details<DomOperationNotificationDetails> dom_op_details(details);
115 response_ = dom_op_details->json; 115 response_ = dom_op_details->json;
116 did_respond_ = true; 116 did_respond_ = true;
117 MessageLoopForUI::current()->Quit(); 117 message_loop_runner_.QuitNow();
118 } 118 }
119 119
120 // Overridden from content::WebContentsObserver: 120 // Overridden from content::WebContentsObserver:
121 virtual void RenderViewGone(base::TerminationStatus status) OVERRIDE { 121 virtual void RenderViewGone(base::TerminationStatus status) OVERRIDE {
122 MessageLoopForUI::current()->Quit(); 122 message_loop_runner_.QuitNow();
123 } 123 }
124 124
125 bool GetResponse(std::string* response) WARN_UNUSED_RESULT { 125 bool GetResponse(std::string* response) WARN_UNUSED_RESULT {
126 *response = response_; 126 *response = response_;
127 return did_respond_; 127 return did_respond_;
128 } 128 }
129 129
130 private: 130 private:
131 content::NotificationRegistrar registrar_; 131 content::NotificationRegistrar registrar_;
132 std::string response_; 132 std::string response_;
133 bool did_respond_; 133 bool did_respond_;
134 MessageLoopRunner message_loop_runner_;
134 135
135 DISALLOW_COPY_AND_ASSIGN(DOMOperationObserver); 136 DISALLOW_COPY_AND_ASSIGN(DOMOperationObserver);
136 }; 137 };
137 138
138 class FindInPageNotificationObserver : public content::NotificationObserver { 139 class FindInPageNotificationObserver : public content::NotificationObserver {
139 public: 140 public:
140 explicit FindInPageNotificationObserver(TabContentsWrapper* parent_tab) 141 explicit FindInPageNotificationObserver(TabContentsWrapper* parent_tab)
141 : parent_tab_(parent_tab), 142 : parent_tab_(parent_tab),
142 active_match_ordinal_(-1), 143 active_match_ordinal_(-1),
143 number_of_matches_(0) { 144 number_of_matches_(0) {
144 current_find_request_id_ = 145 current_find_request_id_ =
145 parent_tab->find_tab_helper()->current_find_request_id(); 146 parent_tab->find_tab_helper()->current_find_request_id();
146 registrar_.Add(this, chrome::NOTIFICATION_FIND_RESULT_AVAILABLE, 147 registrar_.Add(this, chrome::NOTIFICATION_FIND_RESULT_AVAILABLE,
147 content::Source<WebContents>(parent_tab_->web_contents())); 148 content::Source<WebContents>(parent_tab_->web_contents()));
148 ui_test_utils::RunMessageLoop(); 149 message_loop_runner_.Run();
149 } 150 }
150 151
151 int active_match_ordinal() const { return active_match_ordinal_; } 152 int active_match_ordinal() const { return active_match_ordinal_; }
152 153
153 int number_of_matches() const { return number_of_matches_; } 154 int number_of_matches() const { return number_of_matches_; }
154 155
155 virtual void Observe(int type, const content::NotificationSource& source, 156 virtual void Observe(int type, const content::NotificationSource& source,
156 const content::NotificationDetails& details) { 157 const content::NotificationDetails& details) {
157 if (type == chrome::NOTIFICATION_FIND_RESULT_AVAILABLE) { 158 if (type == chrome::NOTIFICATION_FIND_RESULT_AVAILABLE) {
158 content::Details<FindNotificationDetails> find_details(details); 159 content::Details<FindNotificationDetails> find_details(details);
159 if (find_details->request_id() == current_find_request_id_) { 160 if (find_details->request_id() == current_find_request_id_) {
160 // We get multiple responses and one of those will contain the ordinal. 161 // We get multiple responses and one of those will contain the ordinal.
161 // This message comes to us before the final update is sent. 162 // This message comes to us before the final update is sent.
162 if (find_details->active_match_ordinal() > -1) 163 if (find_details->active_match_ordinal() > -1)
163 active_match_ordinal_ = find_details->active_match_ordinal(); 164 active_match_ordinal_ = find_details->active_match_ordinal();
164 if (find_details->final_update()) { 165 if (find_details->final_update()) {
165 number_of_matches_ = find_details->number_of_matches(); 166 number_of_matches_ = find_details->number_of_matches();
166 MessageLoopForUI::current()->Quit(); 167 message_loop_runner_.QuitNow();
167 } else { 168 } else {
168 DVLOG(1) << "Ignoring, since we only care about the final message"; 169 DVLOG(1) << "Ignoring, since we only care about the final message";
169 } 170 }
170 } 171 }
171 } else { 172 } else {
172 NOTREACHED(); 173 NOTREACHED();
173 } 174 }
174 } 175 }
175 176
176 private: 177 private:
177 content::NotificationRegistrar registrar_; 178 content::NotificationRegistrar registrar_;
178 TabContentsWrapper* parent_tab_; 179 TabContentsWrapper* parent_tab_;
179 // We will at some point (before final update) be notified of the ordinal and 180 // We will at some point (before final update) be notified of the ordinal and
180 // we need to preserve it so we can send it later. 181 // we need to preserve it so we can send it later.
181 int active_match_ordinal_; 182 int active_match_ordinal_;
182 int number_of_matches_; 183 int number_of_matches_;
183 // The id of the current find request, obtained from WebContents. Allows us 184 // The id of the current find request, obtained from WebContents. Allows us
184 // to monitor when the search completes. 185 // to monitor when the search completes.
185 int current_find_request_id_; 186 int current_find_request_id_;
187 MessageLoopRunner message_loop_runner_;
186 188
187 DISALLOW_COPY_AND_ASSIGN(FindInPageNotificationObserver); 189 DISALLOW_COPY_AND_ASSIGN(FindInPageNotificationObserver);
188 }; 190 };
189 191
190 class InProcessJavaScriptExecutionController 192 class InProcessJavaScriptExecutionController
191 : public base::RefCounted<InProcessJavaScriptExecutionController>, 193 : public base::RefCounted<InProcessJavaScriptExecutionController>,
192 public JavaScriptExecutionController { 194 public JavaScriptExecutionController {
193 public: 195 public:
194 explicit InProcessJavaScriptExecutionController( 196 explicit InProcessJavaScriptExecutionController(
195 RenderViewHost* render_view_host) 197 RenderViewHost* render_view_host)
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 base::JSONReader reader(base::JSON_ALLOW_TRAILING_COMMAS); 256 base::JSONReader reader(base::JSON_ALLOW_TRAILING_COMMAS);
255 result->reset(reader.ReadToValue(json)); 257 result->reset(reader.ReadToValue(json));
256 if (!result->get()) { 258 if (!result->get()) {
257 DLOG(ERROR) << reader.GetErrorMessage(); 259 DLOG(ERROR) << reader.GetErrorMessage();
258 return false; 260 return false;
259 } 261 }
260 262
261 return true; 263 return true;
262 } 264 }
263 265
264 void RunAllPendingMessageAndSendQuit(content::BrowserThread::ID thread_id) { 266 void RunAllPendingMessageAndSendQuit(content::BrowserThread::ID thread_id,
265 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 267 int message_loop_run_id) {
268 MessageLoop::current()->PostTask(FROM_HERE,
269 MessageLoop::QuitWhenIdleClosure());
266 RunMessageLoop(); 270 RunMessageLoop();
267 content::BrowserThread::PostTask(thread_id, FROM_HERE, 271 content::BrowserThread::PostTask(thread_id, FROM_HERE,
268 MessageLoop::QuitClosure()); 272 MessageLoop::QuitNowByIDClosure(message_loop_run_id));
269 } 273 }
270 274
271 } // namespace 275 } // namespace
272 276
273 void RunMessageLoop() { 277 void RunMessageLoop() {
274 MessageLoop* loop = MessageLoop::current(); 278 MessageLoop* loop = MessageLoop::current();
275 MessageLoopForUI* ui_loop = 279 MessageLoopForUI* ui_loop =
276 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI) ? 280 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI) ?
277 MessageLoopForUI::current() : NULL; 281 MessageLoopForUI::current() : NULL;
278 MessageLoop::ScopedNestableTaskAllower allow(loop); 282 MessageLoop::ScopedNestableTaskAllower allow(loop);
279 if (ui_loop) { 283 if (ui_loop) {
280 #if defined(USE_AURA) 284 #if defined(USE_AURA)
281 ui_loop->Run(); 285 ui_loop->Run();
282 #elif defined(TOOLKIT_VIEWS) 286 #elif defined(TOOLKIT_VIEWS)
283 views::AcceleratorHandler handler; 287 views::AcceleratorHandler handler;
284 ui_loop->RunWithDispatcher(&handler); 288 ui_loop->RunWithDispatcher(&handler);
285 #elif defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) 289 #elif defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
286 ui_loop->RunWithDispatcher(NULL); 290 ui_loop->RunWithDispatcher(NULL);
287 #else 291 #else
288 ui_loop->Run(); 292 ui_loop->Run();
289 #endif 293 #endif
290 } else { 294 } else {
291 loop->Run(); 295 loop->Run();
292 } 296 }
293 } 297 }
294 298
295 void RunAllPendingInMessageLoop() { 299 void RunAllPendingInMessageLoop() {
296 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 300 MessageLoop::current()->PostTask(FROM_HERE,
301 MessageLoop::QuitWhenIdleClosure());
297 ui_test_utils::RunMessageLoop(); 302 ui_test_utils::RunMessageLoop();
298 } 303 }
299 304
300 void RunAllPendingInMessageLoop(content::BrowserThread::ID thread_id) { 305 void RunAllPendingInMessageLoop(content::BrowserThread::ID thread_id) {
301 if (content::BrowserThread::CurrentlyOn(thread_id)) { 306 if (content::BrowserThread::CurrentlyOn(thread_id)) {
302 RunAllPendingInMessageLoop(); 307 RunAllPendingInMessageLoop();
303 return; 308 return;
304 } 309 }
305 content::BrowserThread::ID current_thread_id; 310 content::BrowserThread::ID current_thread_id;
306 if (!content::BrowserThread::GetCurrentThreadIdentifier(&current_thread_id)) { 311 if (!content::BrowserThread::GetCurrentThreadIdentifier(&current_thread_id)) {
307 NOTREACHED(); 312 NOTREACHED();
308 return; 313 return;
309 } 314 }
310 content::BrowserThread::PostTask(thread_id, FROM_HERE, 315 content::BrowserThread::PostTask(thread_id, FROM_HERE,
311 base::Bind(&RunAllPendingMessageAndSendQuit, current_thread_id)); 316 base::Bind(&RunAllPendingMessageAndSendQuit, current_thread_id,
317 MessageLoop::current()->next_run_id()));
312 318
313 ui_test_utils::RunMessageLoop(); 319 ui_test_utils::RunMessageLoop();
314 } 320 }
315 321
316 bool GetCurrentTabTitle(const Browser* browser, string16* title) { 322 bool GetCurrentTabTitle(const Browser* browser, string16* title) {
317 WebContents* web_contents = browser->GetSelectedWebContents(); 323 WebContents* web_contents = browser->GetSelectedWebContents();
318 if (!web_contents) 324 if (!web_contents)
319 return false; 325 return false;
320 NavigationEntry* last_entry = web_contents->GetController().GetActiveEntry(); 326 NavigationEntry* last_entry = web_contents->GetController().GetActiveEntry();
321 if (!last_entry) 327 if (!last_entry)
322 return false; 328 return false;
323 title->assign(last_entry->GetTitleForDisplay("")); 329 title->assign(last_entry->GetTitleForDisplay(""));
324 return true; 330 return true;
325 } 331 }
326 332
327 void WaitForNavigations(NavigationController* controller, 333 void WaitForNavigations(NavigationController* controller,
328 int number_of_navigations) { 334 int number_of_navigations) {
329 content::TestNavigationObserver observer( 335 content::TestNavigationObserver observer(
330 content::Source<NavigationController>(controller), NULL, 336 content::Source<NavigationController>(controller), NULL,
331 number_of_navigations); 337 number_of_navigations);
338 int message_loop_run_id = MessageLoop::current()->next_run_id();
332 observer.WaitForObservation( 339 observer.WaitForObservation(
333 base::Bind(&ui_test_utils::RunMessageLoop), 340 base::Bind(&ui_test_utils::RunMessageLoop),
334 base::Bind(&MessageLoop::Quit, 341 MessageLoop::QuitNowByIDClosure(message_loop_run_id));
335 base::Unretained(MessageLoopForUI::current())));
336 } 342 }
337 343
338 void WaitForNewTab(Browser* browser) { 344 void WaitForNewTab(Browser* browser) {
339 WindowedNotificationObserver observer( 345 WindowedNotificationObserver observer(
340 chrome::NOTIFICATION_TAB_ADDED, 346 chrome::NOTIFICATION_TAB_ADDED,
341 content::Source<content::WebContentsDelegate>(browser)); 347 content::Source<content::WebContentsDelegate>(browser));
342 observer.Wait(); 348 observer.Wait();
343 } 349 }
344 350
345 void WaitForBrowserActionUpdated(ExtensionAction* browser_action) { 351 void WaitForBrowserActionUpdated(ExtensionAction* browser_action) {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 Browser::OpenURLOffTheRecord(profile, url); 388 Browser::OpenURLOffTheRecord(profile, url);
383 Browser* browser = browser::FindTabbedBrowser( 389 Browser* browser = browser::FindTabbedBrowser(
384 profile->GetOffTheRecordProfile(), false); 390 profile->GetOffTheRecordProfile(), false);
385 WaitForNavigations(&browser->GetSelectedWebContents()->GetController(), 1); 391 WaitForNavigations(&browser->GetSelectedWebContents()->GetController(), 1);
386 } 392 }
387 393
388 void NavigateToURL(browser::NavigateParams* params) { 394 void NavigateToURL(browser::NavigateParams* params) {
389 content::TestNavigationObserver observer( 395 content::TestNavigationObserver observer(
390 content::NotificationService::AllSources(), NULL, 1); 396 content::NotificationService::AllSources(), NULL, 1);
391 browser::Navigate(params); 397 browser::Navigate(params);
398 int message_loop_run_id = MessageLoop::current()->next_run_id();
392 observer.WaitForObservation( 399 observer.WaitForObservation(
393 base::Bind(&ui_test_utils::RunMessageLoop), 400 base::Bind(&ui_test_utils::RunMessageLoop),
394 base::Bind(&MessageLoop::Quit, 401 MessageLoop::QuitNowByIDClosure(message_loop_run_id));
395 base::Unretained(MessageLoopForUI::current())));
396
397 } 402 }
398 403
399 void NavigateToURL(Browser* browser, const GURL& url) { 404 void NavigateToURL(Browser* browser, const GURL& url) {
400 NavigateToURLWithDisposition(browser, url, CURRENT_TAB, 405 NavigateToURLWithDisposition(browser, url, CURRENT_TAB,
401 BROWSER_TEST_WAIT_FOR_NAVIGATION); 406 BROWSER_TEST_WAIT_FOR_NAVIGATION);
402 } 407 }
403 408
404 // Navigates the specified tab (via |disposition|) of |browser| to |url|, 409 // Navigates the specified tab (via |disposition|) of |browser| to |url|,
405 // blocking until the |number_of_navigations| specified complete. 410 // blocking until the |number_of_navigations| specified complete.
406 // |disposition| indicates what tab the download occurs in, and 411 // |disposition| indicates what tab the download occurs in, and
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 << " Unable to wait for navigation to \"" << url.spec() 461 << " Unable to wait for navigation to \"" << url.spec()
457 << "\" because the new tab is not available yet"; 462 << "\" because the new tab is not available yet";
458 return; 463 return;
459 } else if ((disposition == CURRENT_TAB) || 464 } else if ((disposition == CURRENT_TAB) ||
460 (disposition == NEW_FOREGROUND_TAB) || 465 (disposition == NEW_FOREGROUND_TAB) ||
461 (disposition == SINGLETON_TAB)) { 466 (disposition == SINGLETON_TAB)) {
462 // The currently selected tab is the right one. 467 // The currently selected tab is the right one.
463 web_contents = browser->GetSelectedWebContents(); 468 web_contents = browser->GetSelectedWebContents();
464 } 469 }
465 if (disposition == CURRENT_TAB) { 470 if (disposition == CURRENT_TAB) {
471 int message_loop_run_id = MessageLoop::current()->next_run_id();
466 same_tab_observer.WaitForObservation( 472 same_tab_observer.WaitForObservation(
467 base::Bind(&ui_test_utils::RunMessageLoop), 473 base::Bind(&ui_test_utils::RunMessageLoop),
468 base::Bind(&MessageLoop::Quit, 474 MessageLoop::QuitNowByIDClosure(message_loop_run_id));
469 base::Unretained(MessageLoopForUI::current())));
470 return; 475 return;
471 } else if (web_contents) { 476 } else if (web_contents) {
472 NavigationController* controller = &web_contents->GetController(); 477 NavigationController* controller = &web_contents->GetController();
473 WaitForNavigations(controller, number_of_navigations); 478 WaitForNavigations(controller, number_of_navigations);
474 return; 479 return;
475 } 480 }
476 EXPECT_TRUE(NULL != web_contents) << " Unable to wait for navigation to \"" 481 EXPECT_TRUE(NULL != web_contents) << " Unable to wait for navigation to \""
477 << url.spec() << "\"" 482 << url.spec() << "\""
478 << " because we can't get the tab contents"; 483 << " because we can't get the tab contents";
479 } 484 }
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
763 768
764 bool SendKeyPressSync(const Browser* browser, 769 bool SendKeyPressSync(const Browser* browser,
765 ui::KeyboardCode key, 770 ui::KeyboardCode key,
766 bool control, 771 bool control,
767 bool shift, 772 bool shift,
768 bool alt, 773 bool alt,
769 bool command) { 774 bool command) {
770 gfx::NativeWindow window = NULL; 775 gfx::NativeWindow window = NULL;
771 if (!GetNativeWindow(browser, &window)) 776 if (!GetNativeWindow(browser, &window))
772 return false; 777 return false;
773 778 scoped_refptr<MessageLoopRunnerClosure> runner =
779 new MessageLoopRunnerClosure;
774 bool result; 780 bool result;
775 result = ui_controls::SendKeyPressNotifyWhenDone( 781 result = ui_controls::SendKeyPressNotifyWhenDone(
776 window, key, control, shift, alt, command, MessageLoop::QuitClosure()); 782 window, key, control, shift, alt, command, runner->QuitNowClosure());
777 #if defined(OS_WIN) 783 #if defined(OS_WIN)
778 if (!result && BringBrowserWindowToFront(browser)) { 784 if (!result && BringBrowserWindowToFront(browser)) {
779 result = ui_controls::SendKeyPressNotifyWhenDone( 785 result = ui_controls::SendKeyPressNotifyWhenDone(
780 window, key, control, shift, alt, command, MessageLoop::QuitClosure()); 786 window, key, control, shift, alt, command, runner->QuitNowClosure());
781 } 787 }
782 #endif 788 #endif
783 if (!result) { 789 if (!result) {
784 LOG(ERROR) << "ui_controls::SendKeyPressNotifyWhenDone failed"; 790 LOG(ERROR) << "ui_controls::SendKeyPressNotifyWhenDone failed";
785 return false; 791 return false;
786 } 792 }
787 793
788 // Run the message loop. It'll stop running when either the key was received 794 // Run the message loop. It'll stop running when either the key was received
789 // or the test timed out (in which case testing::Test::HasFatalFailure should 795 // or the test timed out (in which case testing::Test::HasFatalFailure should
790 // be set). 796 // be set).
791 RunMessageLoop(); 797 runner->Run();
792 return !testing::Test::HasFatalFailure(); 798 return !testing::Test::HasFatalFailure();
793 } 799 }
794 800
795 bool SendKeyPressAndWait(const Browser* browser, 801 bool SendKeyPressAndWait(const Browser* browser,
796 ui::KeyboardCode key, 802 ui::KeyboardCode key,
797 bool control, 803 bool control,
798 bool shift, 804 bool shift,
799 bool alt, 805 bool alt,
800 bool command, 806 bool command,
801 int type, 807 int type,
802 const content::NotificationSource& source) { 808 const content::NotificationSource& source) {
803 WindowedNotificationObserver observer(type, source); 809 WindowedNotificationObserver observer(type, source);
804 810
805 if (!SendKeyPressSync(browser, key, control, shift, alt, command)) 811 if (!SendKeyPressSync(browser, key, control, shift, alt, command))
806 return false; 812 return false;
807 813
808 observer.Wait(); 814 observer.Wait();
809 return !testing::Test::HasFatalFailure(); 815 return !testing::Test::HasFatalFailure();
810 } 816 }
811 817
812 bool SendMouseMoveSync(const gfx::Point& location) { 818 bool SendMouseMoveSync(const gfx::Point& location) {
813 if (!ui_controls::SendMouseMoveNotifyWhenDone(location.x(), location.y(), 819 scoped_refptr<MessageLoopRunnerClosure> runner =
814 MessageLoop::QuitClosure())) { 820 new MessageLoopRunnerClosure;
821 if (!ui_controls::SendMouseMoveNotifyWhenDone(
822 location.x(), location.y(), runner->QuitNowClosure())) {
815 return false; 823 return false;
816 } 824 }
817 RunMessageLoop(); 825 runner->Run();
818 return !testing::Test::HasFatalFailure(); 826 return !testing::Test::HasFatalFailure();
819 } 827 }
820 828
821 bool SendMouseEventsSync(ui_controls::MouseButton type, int state) { 829 bool SendMouseEventsSync(ui_controls::MouseButton type, int state) {
830 scoped_refptr<MessageLoopRunnerClosure> runner =
831 new MessageLoopRunnerClosure;
822 if (!ui_controls::SendMouseEventsNotifyWhenDone( 832 if (!ui_controls::SendMouseEventsNotifyWhenDone(
823 type, state, MessageLoop::QuitClosure())) { 833 type, state, runner->QuitNowClosure())) {
824 return false; 834 return false;
825 } 835 }
826 RunMessageLoop(); 836 runner->Run();
827 return !testing::Test::HasFatalFailure(); 837 return !testing::Test::HasFatalFailure();
828 } 838 }
829 839
830 TimedMessageLoopRunner::TimedMessageLoopRunner() 840 void MessageLoopRunner::Run() {
831 : loop_(new MessageLoopForUI()), 841 CHECK(message_loop_run_id_ == 0) << "Nested runs must use another instance";
jar (doing other things) 2012/06/14 04:50:29 I think you need to be explicit in the message loo
jbates 2012/06/15 02:20:38 Fixed by removing this code.
832 owned_(true), 842 message_loop_run_id_ = MessageLoop::current()->next_run_id();
833 quit_loop_invoked_(false) { 843 ui_test_utils::RunMessageLoop();
834 } 844 }
835 845
836 TimedMessageLoopRunner::~TimedMessageLoopRunner() { 846 void MessageLoopRunner::QuitNow() {
837 if (owned_) 847 MessageLoopForUI::current()->PostTask(
838 delete loop_; 848 FROM_HERE,
849 MessageLoop::QuitNowByIDClosure(message_loop_run_id_));
850 message_loop_run_id_ = 0;
839 } 851 }
840 852
841 void TimedMessageLoopRunner::RunFor(int ms) { 853 MessageLoopRunnerClosure::MessageLoopRunnerClosure()
842 QuitAfter(ms); 854 : run_called_(false),
843 quit_loop_invoked_ = false; 855 quit_received_(false),
844 loop_->Run(); 856 running_(false) {
845 } 857 }
846 858
847 void TimedMessageLoopRunner::Quit() { 859 MessageLoopRunnerClosure::~MessageLoopRunnerClosure() {
848 quit_loop_invoked_ = true;
849 loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure());
850 } 860 }
851 861
852 void TimedMessageLoopRunner::QuitAfter(int ms) { 862 void MessageLoopRunnerClosure::Run() {
853 quit_loop_invoked_ = true; 863 CHECK(!run_called_);
854 loop_->PostDelayedTask( 864 run_called_ = true;
855 FROM_HERE, 865
856 MessageLoop::QuitClosure(), 866 if (quit_received_)
857 base::TimeDelta::FromMilliseconds(ms)); 867 return;
868
869 running_ = true;
870 message_loop_runner_.Run();
871 running_ = false;
872 }
873
874 base::Closure MessageLoopRunnerClosure::QuitNowClosure() {
875 return base::Bind(&MessageLoopRunnerClosure::QuitNow, this);
876 }
877
878 void MessageLoopRunnerClosure::QuitNow() {
879 quit_received_ = true;
880 if (running_)
881 message_loop_runner_.QuitNow();
858 } 882 }
859 883
860 TestWebSocketServer::TestWebSocketServer() 884 TestWebSocketServer::TestWebSocketServer()
861 : started_(false), 885 : started_(false),
862 port_(kDefaultWsPort), 886 port_(kDefaultWsPort),
863 secure_(false) { 887 secure_(false) {
864 #if defined(OS_POSIX) 888 #if defined(OS_POSIX)
865 process_group_id_ = base::kNullProcessHandle; 889 process_group_id_ = base::kNullProcessHandle;
866 #endif 890 #endif
867 } 891 }
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
1012 registrar_.Add(this, notification_type, source); 1036 registrar_.Add(this, notification_type, source);
1013 } 1037 }
1014 1038
1015 WindowedNotificationObserver::~WindowedNotificationObserver() {} 1039 WindowedNotificationObserver::~WindowedNotificationObserver() {}
1016 1040
1017 void WindowedNotificationObserver::Wait() { 1041 void WindowedNotificationObserver::Wait() {
1018 if (seen_) 1042 if (seen_)
1019 return; 1043 return;
1020 1044
1021 running_ = true; 1045 running_ = true;
1022 ui_test_utils::RunMessageLoop(); 1046 message_loop_runner_.Run();
1023 EXPECT_TRUE(seen_); 1047 EXPECT_TRUE(seen_);
1024 } 1048 }
1025 1049
1026 void WindowedNotificationObserver::Observe( 1050 void WindowedNotificationObserver::Observe(
1027 int type, 1051 int type,
1028 const content::NotificationSource& source, 1052 const content::NotificationSource& source,
1029 const content::NotificationDetails& details) { 1053 const content::NotificationDetails& details) {
1030 source_ = source; 1054 source_ = source;
1031 details_ = details; 1055 details_ = details;
1032 seen_ = true; 1056 seen_ = true;
1033 if (!running_) 1057 if (!running_)
1034 return; 1058 return;
1035 1059
1036 MessageLoopForUI::current()->Quit(); 1060 message_loop_runner_.QuitNow();
1037 running_ = false; 1061 running_ = false;
1038 } 1062 }
1039 1063
1040 WindowedTabAddedNotificationObserver::WindowedTabAddedNotificationObserver( 1064 WindowedTabAddedNotificationObserver::WindowedTabAddedNotificationObserver(
1041 const content::NotificationSource& source) 1065 const content::NotificationSource& source)
1042 : WindowedNotificationObserver(chrome::NOTIFICATION_TAB_ADDED, source), 1066 : WindowedNotificationObserver(chrome::NOTIFICATION_TAB_ADDED, source),
1043 added_tab_(NULL) { 1067 added_tab_(NULL) {
1044 } 1068 }
1045 1069
1046 void WindowedTabAddedNotificationObserver::Observe( 1070 void WindowedTabAddedNotificationObserver::Observe(
(...skipping 30 matching lines...) Expand all
1077 expected_titles_.push_back(expected_title); 1101 expected_titles_.push_back(expected_title);
1078 } 1102 }
1079 1103
1080 TitleWatcher::~TitleWatcher() { 1104 TitleWatcher::~TitleWatcher() {
1081 } 1105 }
1082 1106
1083 const string16& TitleWatcher::WaitAndGetTitle() { 1107 const string16& TitleWatcher::WaitAndGetTitle() {
1084 if (expected_title_observed_) 1108 if (expected_title_observed_)
1085 return observed_title_; 1109 return observed_title_;
1086 quit_loop_on_observation_ = true; 1110 quit_loop_on_observation_ = true;
1087 ui_test_utils::RunMessageLoop(); 1111 message_loop_runner_.Run();
1088 return observed_title_; 1112 return observed_title_;
1089 } 1113 }
1090 1114
1091 void TitleWatcher::Observe(int type, 1115 void TitleWatcher::Observe(int type,
1092 const content::NotificationSource& source, 1116 const content::NotificationSource& source,
1093 const content::NotificationDetails& details) { 1117 const content::NotificationDetails& details) {
1094 if (type == content::NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED) { 1118 if (type == content::NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED) {
1095 WebContents* source_contents = content::Source<WebContents>(source).ptr(); 1119 WebContents* source_contents = content::Source<WebContents>(source).ptr();
1096 ASSERT_EQ(web_contents_, source_contents); 1120 ASSERT_EQ(web_contents_, source_contents);
1097 } else if (type == content::NOTIFICATION_LOAD_STOP) { 1121 } else if (type == content::NOTIFICATION_LOAD_STOP) {
1098 NavigationController* controller = 1122 NavigationController* controller =
1099 content::Source<NavigationController>(source).ptr(); 1123 content::Source<NavigationController>(source).ptr();
1100 ASSERT_EQ(&web_contents_->GetController(), controller); 1124 ASSERT_EQ(&web_contents_->GetController(), controller);
1101 } else { 1125 } else {
1102 FAIL() << "Unexpected notification received."; 1126 FAIL() << "Unexpected notification received.";
1103 } 1127 }
1104 1128
1105 std::vector<string16>::const_iterator it = 1129 std::vector<string16>::const_iterator it =
1106 std::find(expected_titles_.begin(), 1130 std::find(expected_titles_.begin(),
1107 expected_titles_.end(), 1131 expected_titles_.end(),
1108 web_contents_->GetTitle()); 1132 web_contents_->GetTitle());
1109 if (it == expected_titles_.end()) 1133 if (it == expected_titles_.end())
1110 return; 1134 return;
1111 observed_title_ = *it; 1135 observed_title_ = *it;
1112 expected_title_observed_ = true; 1136 expected_title_observed_ = true;
1113 if (quit_loop_on_observation_) 1137 if (quit_loop_on_observation_) {
1114 MessageLoopForUI::current()->Quit(); 1138 // Only call Quit once, on first Observe:
1139 quit_loop_on_observation_ = false;
1140 message_loop_runner_.QuitNow();
1141 }
1115 } 1142 }
1116 1143
1117 BrowserAddedObserver::BrowserAddedObserver() 1144 BrowserAddedObserver::BrowserAddedObserver()
1118 : notification_observer_( 1145 : notification_observer_(
1119 chrome::NOTIFICATION_BROWSER_OPENED, 1146 chrome::NOTIFICATION_BROWSER_OPENED,
1120 content::NotificationService::AllSources()) { 1147 content::NotificationService::AllSources()) {
1121 original_browsers_.insert(BrowserList::begin(), BrowserList::end()); 1148 original_browsers_.insert(BrowserList::begin(), BrowserList::end());
1122 } 1149 }
1123 1150
1124 BrowserAddedObserver::~BrowserAddedObserver() { 1151 BrowserAddedObserver::~BrowserAddedObserver() {
(...skipping 14 matching lines...) Expand all
1139 DOMMessageQueue::~DOMMessageQueue() {} 1166 DOMMessageQueue::~DOMMessageQueue() {}
1140 1167
1141 void DOMMessageQueue::Observe(int type, 1168 void DOMMessageQueue::Observe(int type,
1142 const content::NotificationSource& source, 1169 const content::NotificationSource& source,
1143 const content::NotificationDetails& details) { 1170 const content::NotificationDetails& details) {
1144 content::Details<DomOperationNotificationDetails> dom_op_details(details); 1171 content::Details<DomOperationNotificationDetails> dom_op_details(details);
1145 content::Source<RenderViewHost> sender(source); 1172 content::Source<RenderViewHost> sender(source);
1146 message_queue_.push(dom_op_details->json); 1173 message_queue_.push(dom_op_details->json);
1147 if (waiting_for_message_) { 1174 if (waiting_for_message_) {
1148 waiting_for_message_ = false; 1175 waiting_for_message_ = false;
1149 MessageLoopForUI::current()->Quit(); 1176 message_loop_runner_.QuitNow();
1150 } 1177 }
1151 } 1178 }
1152 1179
1153 void DOMMessageQueue::ClearQueue() { 1180 void DOMMessageQueue::ClearQueue() {
1154 message_queue_ = std::queue<std::string>(); 1181 message_queue_ = std::queue<std::string>();
1155 } 1182 }
1156 1183
1157 bool DOMMessageQueue::WaitForMessage(std::string* message) { 1184 bool DOMMessageQueue::WaitForMessage(std::string* message) {
1158 if (message_queue_.empty()) { 1185 if (message_queue_.empty()) {
1159 waiting_for_message_ = true; 1186 waiting_for_message_ = true;
1160 // This will be quit when a new message comes in. 1187 // This will be quit when a new message comes in.
1161 RunMessageLoop(); 1188 message_loop_runner_.Run();
1162 } 1189 }
1163 // The queue should not be empty, unless we were quit because of a timeout. 1190 // The queue should not be empty, unless we were quit because of a timeout.
1164 if (message_queue_.empty()) 1191 if (message_queue_.empty())
1165 return false; 1192 return false;
1166 if (message) 1193 if (message)
1167 *message = message_queue_.front(); 1194 *message = message_queue_.front();
1168 return true; 1195 return true;
1169 } 1196 }
1170 1197
1171 // Coordinates taking snapshots of a |RenderWidget|. 1198 // Coordinates taking snapshots of a |RenderWidget|.
1172 class SnapshotTaker { 1199 class SnapshotTaker {
1173 public: 1200 public:
1174 SnapshotTaker() : bitmap_(NULL) {} 1201 SnapshotTaker() : bitmap_(NULL) {}
1175 1202
1176 bool TakeRenderWidgetSnapshot(RenderWidgetHost* rwh, 1203 bool TakeRenderWidgetSnapshot(RenderWidgetHost* rwh,
1177 const gfx::Size& page_size, 1204 const gfx::Size& page_size,
1178 const gfx::Size& desired_size, 1205 const gfx::Size& desired_size,
1179 SkBitmap* bitmap) WARN_UNUSED_RESULT { 1206 SkBitmap* bitmap) WARN_UNUSED_RESULT {
1180 bitmap_ = bitmap; 1207 bitmap_ = bitmap;
1181 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator(); 1208 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
1182 generator->MonitorRenderer(rwh, true); 1209 generator->MonitorRenderer(rwh, true);
1183 snapshot_taken_ = false; 1210 snapshot_taken_ = false;
1184 generator->AskForSnapshot( 1211 generator->AskForSnapshot(
1185 rwh, 1212 rwh,
1186 base::Bind(&SnapshotTaker::OnSnapshotTaken, base::Unretained(this)), 1213 base::Bind(&SnapshotTaker::OnSnapshotTaken, base::Unretained(this)),
1187 page_size, 1214 page_size,
1188 desired_size); 1215 desired_size);
1189 ui_test_utils::RunMessageLoop(); 1216 message_loop_runner_.Run();
1190 return snapshot_taken_; 1217 return snapshot_taken_;
1191 } 1218 }
1192 1219
1193 bool TakeEntirePageSnapshot(RenderViewHost* rvh, 1220 bool TakeEntirePageSnapshot(RenderViewHost* rvh,
1194 SkBitmap* bitmap) WARN_UNUSED_RESULT { 1221 SkBitmap* bitmap) WARN_UNUSED_RESULT {
1195 const wchar_t* script = 1222 const wchar_t* script =
1196 L"window.domAutomationController.send(" 1223 L"window.domAutomationController.send("
1197 L" JSON.stringify([document.width, document.height]))"; 1224 L" JSON.stringify([document.width, document.height]))";
1198 std::string json; 1225 std::string json;
1199 if (!ui_test_utils::ExecuteJavaScriptAndExtractString( 1226 if (!ui_test_utils::ExecuteJavaScriptAndExtractString(
(...skipping 14 matching lines...) Expand all
1214 // Take the snapshot. 1241 // Take the snapshot.
1215 gfx::Size page_size(width, height); 1242 gfx::Size page_size(width, height);
1216 return TakeRenderWidgetSnapshot(rvh, page_size, page_size, bitmap); 1243 return TakeRenderWidgetSnapshot(rvh, page_size, page_size, bitmap);
1217 } 1244 }
1218 1245
1219 private: 1246 private:
1220 // Called when the ThumbnailGenerator has taken the snapshot. 1247 // Called when the ThumbnailGenerator has taken the snapshot.
1221 void OnSnapshotTaken(const SkBitmap& bitmap) { 1248 void OnSnapshotTaken(const SkBitmap& bitmap) {
1222 *bitmap_ = bitmap; 1249 *bitmap_ = bitmap;
1223 snapshot_taken_ = true; 1250 snapshot_taken_ = true;
1224 MessageLoop::current()->Quit(); 1251 message_loop_runner_.QuitNow();
1225 } 1252 }
1226 1253
1227 SkBitmap* bitmap_; 1254 SkBitmap* bitmap_;
1228 // Whether the snapshot was actually taken and received by this SnapshotTaker. 1255 // Whether the snapshot was actually taken and received by this SnapshotTaker.
1229 // This will be false if the test times out. 1256 // This will be false if the test times out.
1230 bool snapshot_taken_; 1257 bool snapshot_taken_;
1258 MessageLoopRunner message_loop_runner_;
1231 1259
1232 DISALLOW_COPY_AND_ASSIGN(SnapshotTaker); 1260 DISALLOW_COPY_AND_ASSIGN(SnapshotTaker);
1233 }; 1261 };
1234 1262
1235 bool TakeRenderWidgetSnapshot(RenderWidgetHost* rwh, 1263 bool TakeRenderWidgetSnapshot(RenderWidgetHost* rwh,
1236 const gfx::Size& page_size, 1264 const gfx::Size& page_size,
1237 SkBitmap* bitmap) { 1265 SkBitmap* bitmap) {
1238 DCHECK(bitmap); 1266 DCHECK(bitmap);
1239 SnapshotTaker taker; 1267 SnapshotTaker taker;
1240 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap); 1268 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap);
1241 } 1269 }
1242 1270
1243 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) { 1271 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) {
1244 DCHECK(bitmap); 1272 DCHECK(bitmap);
1245 SnapshotTaker taker; 1273 SnapshotTaker taker;
1246 return taker.TakeEntirePageSnapshot(rvh, bitmap); 1274 return taker.TakeEntirePageSnapshot(rvh, bitmap);
1247 } 1275 }
1248 1276
1249 void OverrideGeolocation(double latitude, double longitude) { 1277 void OverrideGeolocation(double latitude, double longitude) {
1250 content::Geoposition position; 1278 content::Geoposition position;
1251 position.latitude = latitude; 1279 position.latitude = latitude;
1252 position.longitude = longitude; 1280 position.longitude = longitude;
1253 position.altitude = 0.; 1281 position.altitude = 0.;
1254 position.accuracy = 0.; 1282 position.accuracy = 0.;
1255 position.timestamp = base::Time::Now(); 1283 position.timestamp = base::Time::Now();
1284 scoped_refptr<MessageLoopRunnerClosure> runner =
1285 new MessageLoopRunnerClosure;
1256 content::OverrideLocationForTesting(position, 1286 content::OverrideLocationForTesting(position,
1257 base::Bind(MessageLoop::QuitClosure())); 1287 runner->QuitNowClosure());
1258 RunMessageLoop(); 1288 runner->Run();
1259 } 1289 }
1260 1290
1261 namespace internal { 1291 namespace internal {
1262 1292
1263 void ClickTask(ui_controls::MouseButton button, 1293 void ClickTask(ui_controls::MouseButton button,
1264 int state, 1294 int state,
1265 const base::Closure& followup) { 1295 const base::Closure& followup) {
1266 if (!followup.is_null()) 1296 if (!followup.is_null())
1267 ui_controls::SendMouseEventsNotifyWhenDone(button, state, followup); 1297 ui_controls::SendMouseEventsNotifyWhenDone(button, state, followup);
1268 else 1298 else
1269 ui_controls::SendMouseEvents(button, state); 1299 ui_controls::SendMouseEvents(button, state);
1270 } 1300 }
1271 1301
1272 } // namespace internal 1302 } // namespace internal
1273 } // namespace ui_test_utils 1303 } // namespace ui_test_utils
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698