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

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: jar feedback, compile errors Created 8 years, 5 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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 using content::NavigationController; 84 using content::NavigationController;
85 using content::NavigationEntry; 85 using content::NavigationEntry;
86 using content::OpenURLParams; 86 using content::OpenURLParams;
87 using content::RenderViewHost; 87 using content::RenderViewHost;
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 // Number of times to repost a QuitNow task so that the MessageLoop finishes up
95 // pending tasks and tasks posted by those pending tasks without risking the
96 // potential hang behavior of MessageLoop::QuitWhenIdle.
97 static const int kNumQuitDeferrals = 5;
98
94 namespace ui_test_utils { 99 namespace ui_test_utils {
95 100
96 namespace { 101 namespace {
97 102
98 class DOMOperationObserver : public content::NotificationObserver, 103 class DOMOperationObserver : public content::NotificationObserver,
99 public content::WebContentsObserver { 104 public content::WebContentsObserver {
100 public: 105 public:
101 explicit DOMOperationObserver(RenderViewHost* render_view_host) 106 explicit DOMOperationObserver(RenderViewHost* render_view_host)
102 : content::WebContentsObserver( 107 : content::WebContentsObserver(
103 WebContents::FromRenderViewHost(render_view_host)), 108 WebContents::FromRenderViewHost(render_view_host)),
104 did_respond_(false) { 109 did_respond_(false) {
105 registrar_.Add(this, content::NOTIFICATION_DOM_OPERATION_RESPONSE, 110 registrar_.Add(this, content::NOTIFICATION_DOM_OPERATION_RESPONSE,
106 content::Source<RenderViewHost>(render_view_host)); 111 content::Source<RenderViewHost>(render_view_host));
107 ui_test_utils::RunMessageLoop(); 112 message_loop_runner_ = new MessageLoopRunner;
113 message_loop_runner_->Run();
108 } 114 }
109 115
110 virtual void Observe(int type, 116 virtual void Observe(int type,
111 const content::NotificationSource& source, 117 const content::NotificationSource& source,
112 const content::NotificationDetails& details) OVERRIDE { 118 const content::NotificationDetails& details) OVERRIDE {
113 DCHECK(type == content::NOTIFICATION_DOM_OPERATION_RESPONSE); 119 DCHECK(type == content::NOTIFICATION_DOM_OPERATION_RESPONSE);
114 content::Details<DomOperationNotificationDetails> dom_op_details(details); 120 content::Details<DomOperationNotificationDetails> dom_op_details(details);
115 response_ = dom_op_details->json; 121 response_ = dom_op_details->json;
116 did_respond_ = true; 122 did_respond_ = true;
117 MessageLoopForUI::current()->Quit(); 123 message_loop_runner_->QuitNow();
118 } 124 }
119 125
120 // Overridden from content::WebContentsObserver: 126 // Overridden from content::WebContentsObserver:
121 virtual void RenderViewGone(base::TerminationStatus status) OVERRIDE { 127 virtual void RenderViewGone(base::TerminationStatus status) OVERRIDE {
122 MessageLoopForUI::current()->Quit(); 128 message_loop_runner_->QuitNow();
123 } 129 }
124 130
125 bool GetResponse(std::string* response) WARN_UNUSED_RESULT { 131 bool GetResponse(std::string* response) WARN_UNUSED_RESULT {
126 *response = response_; 132 *response = response_;
127 return did_respond_; 133 return did_respond_;
128 } 134 }
129 135
130 private: 136 private:
131 content::NotificationRegistrar registrar_; 137 content::NotificationRegistrar registrar_;
132 std::string response_; 138 std::string response_;
133 bool did_respond_; 139 bool did_respond_;
140 scoped_refptr<MessageLoopRunner> message_loop_runner_;
134 141
135 DISALLOW_COPY_AND_ASSIGN(DOMOperationObserver); 142 DISALLOW_COPY_AND_ASSIGN(DOMOperationObserver);
136 }; 143 };
137 144
138 class FindInPageNotificationObserver : public content::NotificationObserver { 145 class FindInPageNotificationObserver : public content::NotificationObserver {
139 public: 146 public:
140 explicit FindInPageNotificationObserver(TabContents* parent_tab) 147 explicit FindInPageNotificationObserver(TabContents* parent_tab)
141 : parent_tab_(parent_tab), 148 : parent_tab_(parent_tab),
142 active_match_ordinal_(-1), 149 active_match_ordinal_(-1),
143 number_of_matches_(0) { 150 number_of_matches_(0) {
144 current_find_request_id_ = 151 current_find_request_id_ =
145 parent_tab->find_tab_helper()->current_find_request_id(); 152 parent_tab->find_tab_helper()->current_find_request_id();
146 registrar_.Add(this, chrome::NOTIFICATION_FIND_RESULT_AVAILABLE, 153 registrar_.Add(this, chrome::NOTIFICATION_FIND_RESULT_AVAILABLE,
147 content::Source<WebContents>(parent_tab_->web_contents())); 154 content::Source<WebContents>(parent_tab_->web_contents()));
148 ui_test_utils::RunMessageLoop(); 155 message_loop_runner_ = new MessageLoopRunner;
156 message_loop_runner_->Run();
149 } 157 }
150 158
151 int active_match_ordinal() const { return active_match_ordinal_; } 159 int active_match_ordinal() const { return active_match_ordinal_; }
152 160
153 int number_of_matches() const { return number_of_matches_; } 161 int number_of_matches() const { return number_of_matches_; }
154 162
155 virtual void Observe(int type, const content::NotificationSource& source, 163 virtual void Observe(int type, const content::NotificationSource& source,
156 const content::NotificationDetails& details) { 164 const content::NotificationDetails& details) {
157 if (type == chrome::NOTIFICATION_FIND_RESULT_AVAILABLE) { 165 if (type == chrome::NOTIFICATION_FIND_RESULT_AVAILABLE) {
158 content::Details<FindNotificationDetails> find_details(details); 166 content::Details<FindNotificationDetails> find_details(details);
159 if (find_details->request_id() == current_find_request_id_) { 167 if (find_details->request_id() == current_find_request_id_) {
160 // We get multiple responses and one of those will contain the ordinal. 168 // We get multiple responses and one of those will contain the ordinal.
161 // This message comes to us before the final update is sent. 169 // This message comes to us before the final update is sent.
162 if (find_details->active_match_ordinal() > -1) 170 if (find_details->active_match_ordinal() > -1)
163 active_match_ordinal_ = find_details->active_match_ordinal(); 171 active_match_ordinal_ = find_details->active_match_ordinal();
164 if (find_details->final_update()) { 172 if (find_details->final_update()) {
165 number_of_matches_ = find_details->number_of_matches(); 173 number_of_matches_ = find_details->number_of_matches();
166 MessageLoopForUI::current()->Quit(); 174 message_loop_runner_->QuitNow();
167 } else { 175 } else {
168 DVLOG(1) << "Ignoring, since we only care about the final message"; 176 DVLOG(1) << "Ignoring, since we only care about the final message";
169 } 177 }
170 } 178 }
171 } else { 179 } else {
172 NOTREACHED(); 180 NOTREACHED();
173 } 181 }
174 } 182 }
175 183
176 private: 184 private:
177 content::NotificationRegistrar registrar_; 185 content::NotificationRegistrar registrar_;
178 TabContents* parent_tab_; 186 TabContents* parent_tab_;
179 // We will at some point (before final update) be notified of the ordinal and 187 // 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. 188 // we need to preserve it so we can send it later.
181 int active_match_ordinal_; 189 int active_match_ordinal_;
182 int number_of_matches_; 190 int number_of_matches_;
183 // The id of the current find request, obtained from WebContents. Allows us 191 // The id of the current find request, obtained from WebContents. Allows us
184 // to monitor when the search completes. 192 // to monitor when the search completes.
185 int current_find_request_id_; 193 int current_find_request_id_;
194 scoped_refptr<MessageLoopRunner> message_loop_runner_;
186 195
187 DISALLOW_COPY_AND_ASSIGN(FindInPageNotificationObserver); 196 DISALLOW_COPY_AND_ASSIGN(FindInPageNotificationObserver);
188 }; 197 };
189 198
190 class InProcessJavaScriptExecutionController 199 class InProcessJavaScriptExecutionController
191 : public base::RefCounted<InProcessJavaScriptExecutionController>, 200 : public base::RefCounted<InProcessJavaScriptExecutionController>,
192 public JavaScriptExecutionController { 201 public JavaScriptExecutionController {
193 public: 202 public:
194 explicit InProcessJavaScriptExecutionController( 203 explicit InProcessJavaScriptExecutionController(
195 RenderViewHost* render_view_host) 204 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); 263 base::JSONReader reader(base::JSON_ALLOW_TRAILING_COMMAS);
255 result->reset(reader.ReadToValue(json)); 264 result->reset(reader.ReadToValue(json));
256 if (!result->get()) { 265 if (!result->get()) {
257 DLOG(ERROR) << reader.GetErrorMessage(); 266 DLOG(ERROR) << reader.GetErrorMessage();
258 return false; 267 return false;
259 } 268 }
260 269
261 return true; 270 return true;
262 } 271 }
263 272
264 void RunAllPendingMessageAndSendQuit(content::BrowserThread::ID thread_id) { 273 void RunAllPendingMessageAndSendQuit(content::BrowserThread::ID thread_id,
265 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 274 const base::Closure& quit_task) {
275 MessageLoop::current()->PostTask(FROM_HERE,
276 MessageLoop::QuitWhenIdleClosure());
266 RunMessageLoop(); 277 RunMessageLoop();
267 content::BrowserThread::PostTask(thread_id, FROM_HERE, 278 content::BrowserThread::PostTask(thread_id, FROM_HERE, quit_task);
268 MessageLoop::QuitClosure());
269 } 279 }
270 280
271 } // namespace 281 } // namespace
272 282
273 void RunMessageLoop() { 283 void RunMessageLoop() {
274 MessageLoop* loop = MessageLoop::current(); 284 base::RunLoop run_loop;
275 MessageLoopForUI* ui_loop = 285 RunThisRunLoop(run_loop.AsWeakPtr());
276 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI) ? 286 }
277 MessageLoopForUI::current() : NULL; 287
278 MessageLoop::ScopedNestableTaskAllower allow(loop); 288 void RunThisRunLoop(const base::WeakPtr<base::RunLoop>& run_loop) {
279 if (ui_loop) { 289 CHECK(run_loop);
280 #if defined(USE_AURA) 290 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current());
281 ui_loop->Run(); 291 #if !defined(USE_AURA) && defined(TOOLKIT_VIEWS)
282 #elif defined(TOOLKIT_VIEWS) 292 scoped_ptr<views::AcceleratorHandler> handler;
283 views::AcceleratorHandler handler; 293 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
284 ui_loop->RunWithDispatcher(&handler); 294 handler = new views::AcceleratorHandler;
285 #elif defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) 295 run_loop->set_dispatcher(handler.get());
286 ui_loop->RunWithDispatcher(NULL); 296 }
287 #else
288 ui_loop->Run();
289 #endif 297 #endif
298 run_loop->Run();
299 }
300
301 static void DeferredQuitRunLoop(
302 const base::WeakPtr<base::RunLoop>& run_loop,
303 int num_quit_deferrals) {
304 if (!run_loop)
305 return;
306 if (num_quit_deferrals <= 0) {
307 run_loop->Quit();
290 } else { 308 } else {
291 loop->Run(); 309 MessageLoop::current()->PostTask(FROM_HERE,
310 base::Bind(&DeferredQuitRunLoop, run_loop, num_quit_deferrals - 1));
292 } 311 }
293 } 312 }
294 313
314 void QuitThisRunLoop(const base::WeakPtr<base::RunLoop>& run_loop) {
315 DeferredQuitRunLoop(run_loop, kNumQuitDeferrals);
316 }
317
295 void RunAllPendingInMessageLoop() { 318 void RunAllPendingInMessageLoop() {
296 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 319 MessageLoop::current()->PostTask(FROM_HERE,
320 MessageLoop::QuitWhenIdleClosure());
297 ui_test_utils::RunMessageLoop(); 321 ui_test_utils::RunMessageLoop();
298 } 322 }
299 323
300 void RunAllPendingInMessageLoop(content::BrowserThread::ID thread_id) { 324 void RunAllPendingInMessageLoop(content::BrowserThread::ID thread_id) {
301 if (content::BrowserThread::CurrentlyOn(thread_id)) { 325 if (content::BrowserThread::CurrentlyOn(thread_id)) {
302 RunAllPendingInMessageLoop(); 326 RunAllPendingInMessageLoop();
303 return; 327 return;
304 } 328 }
305 content::BrowserThread::ID current_thread_id; 329 content::BrowserThread::ID current_thread_id;
306 if (!content::BrowserThread::GetCurrentThreadIdentifier(&current_thread_id)) { 330 if (!content::BrowserThread::GetCurrentThreadIdentifier(&current_thread_id)) {
307 NOTREACHED(); 331 NOTREACHED();
308 return; 332 return;
309 } 333 }
334
335 base::RunLoop run_loop;
310 content::BrowserThread::PostTask(thread_id, FROM_HERE, 336 content::BrowserThread::PostTask(thread_id, FROM_HERE,
311 base::Bind(&RunAllPendingMessageAndSendQuit, current_thread_id)); 337 base::Bind(&RunAllPendingMessageAndSendQuit, current_thread_id,
312 338 run_loop.QuitClosure()));
313 ui_test_utils::RunMessageLoop(); 339 ui_test_utils::RunThisRunLoop(run_loop.AsWeakPtr());
314 } 340 }
315 341
316 bool GetCurrentTabTitle(const Browser* browser, string16* title) { 342 bool GetCurrentTabTitle(const Browser* browser, string16* title) {
317 WebContents* web_contents = browser->GetActiveWebContents(); 343 WebContents* web_contents = browser->GetActiveWebContents();
318 if (!web_contents) 344 if (!web_contents)
319 return false; 345 return false;
320 NavigationEntry* last_entry = web_contents->GetController().GetActiveEntry(); 346 NavigationEntry* last_entry = web_contents->GetController().GetActiveEntry();
321 if (!last_entry) 347 if (!last_entry)
322 return false; 348 return false;
323 title->assign(last_entry->GetTitleForDisplay("")); 349 title->assign(last_entry->GetTitleForDisplay(""));
324 return true; 350 return true;
325 } 351 }
326 352
327 void WaitForNavigations(NavigationController* controller, 353 void WaitForNavigations(NavigationController* controller,
328 int number_of_navigations) { 354 int number_of_navigations) {
329 content::TestNavigationObserver observer( 355 content::TestNavigationObserver observer(
330 content::Source<NavigationController>(controller), NULL, 356 content::Source<NavigationController>(controller), NULL,
331 number_of_navigations); 357 number_of_navigations);
358 base::RunLoop run_loop;
332 observer.WaitForObservation( 359 observer.WaitForObservation(
333 base::Bind(&ui_test_utils::RunMessageLoop), 360 base::Bind(&ui_test_utils::RunThisRunLoop, run_loop.AsWeakPtr()),
334 base::Bind(&MessageLoop::Quit, 361 base::Bind(&ui_test_utils::QuitThisRunLoop, run_loop.AsWeakPtr()));
335 base::Unretained(MessageLoopForUI::current())));
336 } 362 }
337 363
338 void WaitForNewTab(Browser* browser) { 364 void WaitForNewTab(Browser* browser) {
339 WindowedNotificationObserver observer( 365 WindowedNotificationObserver observer(
340 chrome::NOTIFICATION_TAB_ADDED, 366 chrome::NOTIFICATION_TAB_ADDED,
341 content::Source<content::WebContentsDelegate>(browser)); 367 content::Source<content::WebContentsDelegate>(browser));
342 observer.Wait(); 368 observer.Wait();
343 } 369 }
344 370
345 void WaitForLoadStop(WebContents* tab) { 371 void WaitForLoadStop(WebContents* tab) {
(...skipping 29 matching lines...) Expand all
375 Browser::OpenURLOffTheRecord(profile, url); 401 Browser::OpenURLOffTheRecord(profile, url);
376 Browser* browser = browser::FindTabbedBrowser( 402 Browser* browser = browser::FindTabbedBrowser(
377 profile->GetOffTheRecordProfile(), false); 403 profile->GetOffTheRecordProfile(), false);
378 WaitForNavigations(&browser->GetActiveWebContents()->GetController(), 1); 404 WaitForNavigations(&browser->GetActiveWebContents()->GetController(), 1);
379 } 405 }
380 406
381 void NavigateToURL(browser::NavigateParams* params) { 407 void NavigateToURL(browser::NavigateParams* params) {
382 content::TestNavigationObserver observer( 408 content::TestNavigationObserver observer(
383 content::NotificationService::AllSources(), NULL, 1); 409 content::NotificationService::AllSources(), NULL, 1);
384 browser::Navigate(params); 410 browser::Navigate(params);
411 base::RunLoop run_loop;
385 observer.WaitForObservation( 412 observer.WaitForObservation(
386 base::Bind(&ui_test_utils::RunMessageLoop), 413 base::Bind(&ui_test_utils::RunThisRunLoop, run_loop.AsWeakPtr()),
387 base::Bind(&MessageLoop::Quit, 414 base::Bind(&ui_test_utils::QuitThisRunLoop, run_loop.AsWeakPtr()));
388 base::Unretained(MessageLoopForUI::current())));
389
390 } 415 }
391 416
392 void NavigateToURL(Browser* browser, const GURL& url) { 417 void NavigateToURL(Browser* browser, const GURL& url) {
393 NavigateToURLWithDisposition(browser, url, CURRENT_TAB, 418 NavigateToURLWithDisposition(browser, url, CURRENT_TAB,
394 BROWSER_TEST_WAIT_FOR_NAVIGATION); 419 BROWSER_TEST_WAIT_FOR_NAVIGATION);
395 } 420 }
396 421
397 // Navigates the specified tab (via |disposition|) of |browser| to |url|, 422 // Navigates the specified tab (via |disposition|) of |browser| to |url|,
398 // blocking until the |number_of_navigations| specified complete. 423 // blocking until the |number_of_navigations| specified complete.
399 // |disposition| indicates what tab the download occurs in, and 424 // |disposition| indicates what tab the download occurs in, and
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 << " Unable to wait for navigation to \"" << url.spec() 474 << " Unable to wait for navigation to \"" << url.spec()
450 << "\" because the new tab is not available yet"; 475 << "\" because the new tab is not available yet";
451 return; 476 return;
452 } else if ((disposition == CURRENT_TAB) || 477 } else if ((disposition == CURRENT_TAB) ||
453 (disposition == NEW_FOREGROUND_TAB) || 478 (disposition == NEW_FOREGROUND_TAB) ||
454 (disposition == SINGLETON_TAB)) { 479 (disposition == SINGLETON_TAB)) {
455 // The currently selected tab is the right one. 480 // The currently selected tab is the right one.
456 web_contents = browser->GetActiveWebContents(); 481 web_contents = browser->GetActiveWebContents();
457 } 482 }
458 if (disposition == CURRENT_TAB) { 483 if (disposition == CURRENT_TAB) {
484 base::RunLoop run_loop;
459 same_tab_observer.WaitForObservation( 485 same_tab_observer.WaitForObservation(
460 base::Bind(&ui_test_utils::RunMessageLoop), 486 base::Bind(&ui_test_utils::RunThisRunLoop, run_loop.AsWeakPtr()),
461 base::Bind(&MessageLoop::Quit, 487 base::Bind(&ui_test_utils::QuitThisRunLoop, run_loop.AsWeakPtr()));
462 base::Unretained(MessageLoopForUI::current())));
463 return; 488 return;
464 } else if (web_contents) { 489 } else if (web_contents) {
465 NavigationController* controller = &web_contents->GetController(); 490 NavigationController* controller = &web_contents->GetController();
466 WaitForNavigations(controller, number_of_navigations); 491 WaitForNavigations(controller, number_of_navigations);
467 return; 492 return;
468 } 493 }
469 EXPECT_TRUE(NULL != web_contents) << " Unable to wait for navigation to \"" 494 EXPECT_TRUE(NULL != web_contents) << " Unable to wait for navigation to \""
470 << url.spec() << "\"" 495 << url.spec() << "\""
471 << " because we can't get the tab contents"; 496 << " because we can't get the tab contents";
472 } 497 }
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
696 int type, 721 int type,
697 const content::NotificationSource& source) { 722 const content::NotificationSource& source) {
698 content::NotificationRegistrar registrar; 723 content::NotificationRegistrar registrar;
699 registrar.Add(observer, type, source); 724 registrar.Add(observer, type, source);
700 RunMessageLoop(); 725 RunMessageLoop();
701 } 726 }
702 727
703 void WaitForBookmarkModelToLoad(BookmarkModel* model) { 728 void WaitForBookmarkModelToLoad(BookmarkModel* model) {
704 if (model->IsLoaded()) 729 if (model->IsLoaded())
705 return; 730 return;
706 BookmarkLoadObserver observer; 731 base::RunLoop run_loop;
732 BookmarkLoadObserver observer(run_loop.AsWeakPtr());
707 model->AddObserver(&observer); 733 model->AddObserver(&observer);
708 RunMessageLoop(); 734 RunThisRunLoop(run_loop.AsWeakPtr());
709 model->RemoveObserver(&observer); 735 model->RemoveObserver(&observer);
710 ASSERT_TRUE(model->IsLoaded()); 736 ASSERT_TRUE(model->IsLoaded());
711 } 737 }
712 738
713 void WaitForTemplateURLServiceToLoad(TemplateURLService* service) { 739 void WaitForTemplateURLServiceToLoad(TemplateURLService* service) {
714 if (service->loaded()) 740 if (service->loaded())
715 return; 741 return;
716 service->Load(); 742 service->Load();
717 TemplateURLServiceTestUtil::BlockTillServiceProcessesRequests(); 743 TemplateURLServiceTestUtil::BlockTillServiceProcessesRequests();
718 ASSERT_TRUE(service->loaded()); 744 ASSERT_TRUE(service->loaded());
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
756 782
757 bool SendKeyPressSync(const Browser* browser, 783 bool SendKeyPressSync(const Browser* browser,
758 ui::KeyboardCode key, 784 ui::KeyboardCode key,
759 bool control, 785 bool control,
760 bool shift, 786 bool shift,
761 bool alt, 787 bool alt,
762 bool command) { 788 bool command) {
763 gfx::NativeWindow window = NULL; 789 gfx::NativeWindow window = NULL;
764 if (!GetNativeWindow(browser, &window)) 790 if (!GetNativeWindow(browser, &window))
765 return false; 791 return false;
766 792 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner;
767 bool result; 793 bool result;
768 result = ui_controls::SendKeyPressNotifyWhenDone( 794 result = ui_controls::SendKeyPressNotifyWhenDone(
769 window, key, control, shift, alt, command, MessageLoop::QuitClosure()); 795 window, key, control, shift, alt, command, runner->QuitNowClosure());
770 #if defined(OS_WIN) 796 #if defined(OS_WIN)
771 if (!result && BringBrowserWindowToFront(browser)) { 797 if (!result && BringBrowserWindowToFront(browser)) {
772 result = ui_controls::SendKeyPressNotifyWhenDone( 798 result = ui_controls::SendKeyPressNotifyWhenDone(
773 window, key, control, shift, alt, command, MessageLoop::QuitClosure()); 799 window, key, control, shift, alt, command, runner->QuitNowClosure());
774 } 800 }
775 #endif 801 #endif
776 if (!result) { 802 if (!result) {
777 LOG(ERROR) << "ui_controls::SendKeyPressNotifyWhenDone failed"; 803 LOG(ERROR) << "ui_controls::SendKeyPressNotifyWhenDone failed";
778 return false; 804 return false;
779 } 805 }
780 806
781 // Run the message loop. It'll stop running when either the key was received 807 // Run the message loop. It'll stop running when either the key was received
782 // or the test timed out (in which case testing::Test::HasFatalFailure should 808 // or the test timed out (in which case testing::Test::HasFatalFailure should
783 // be set). 809 // be set).
784 RunMessageLoop(); 810 runner->Run();
785 return !testing::Test::HasFatalFailure(); 811 return !testing::Test::HasFatalFailure();
786 } 812 }
787 813
788 bool SendKeyPressAndWait(const Browser* browser, 814 bool SendKeyPressAndWait(const Browser* browser,
789 ui::KeyboardCode key, 815 ui::KeyboardCode key,
790 bool control, 816 bool control,
791 bool shift, 817 bool shift,
792 bool alt, 818 bool alt,
793 bool command, 819 bool command,
794 int type, 820 int type,
795 const content::NotificationSource& source) { 821 const content::NotificationSource& source) {
796 WindowedNotificationObserver observer(type, source); 822 WindowedNotificationObserver observer(type, source);
797 823
798 if (!SendKeyPressSync(browser, key, control, shift, alt, command)) 824 if (!SendKeyPressSync(browser, key, control, shift, alt, command))
799 return false; 825 return false;
800 826
801 observer.Wait(); 827 observer.Wait();
802 return !testing::Test::HasFatalFailure(); 828 return !testing::Test::HasFatalFailure();
803 } 829 }
804 830
805 bool SendMouseMoveSync(const gfx::Point& location) { 831 bool SendMouseMoveSync(const gfx::Point& location) {
806 if (!ui_controls::SendMouseMoveNotifyWhenDone(location.x(), location.y(), 832 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner;
807 MessageLoop::QuitClosure())) { 833 if (!ui_controls::SendMouseMoveNotifyWhenDone(
834 location.x(), location.y(), runner->QuitNowClosure())) {
808 return false; 835 return false;
809 } 836 }
810 RunMessageLoop(); 837 runner->Run();
811 return !testing::Test::HasFatalFailure(); 838 return !testing::Test::HasFatalFailure();
812 } 839 }
813 840
814 bool SendMouseEventsSync(ui_controls::MouseButton type, int state) { 841 bool SendMouseEventsSync(ui_controls::MouseButton type, int state) {
842 scoped_refptr<MessageLoopRunner> runner =
843 new MessageLoopRunner;
jar (doing other things) 2012/06/25 21:19:43 nit: unwrap line.
jbates 2012/06/27 02:06:37 Done.
815 if (!ui_controls::SendMouseEventsNotifyWhenDone( 844 if (!ui_controls::SendMouseEventsNotifyWhenDone(
816 type, state, MessageLoop::QuitClosure())) { 845 type, state, runner->QuitNowClosure())) {
817 return false; 846 return false;
818 } 847 }
819 RunMessageLoop(); 848 runner->Run();
820 return !testing::Test::HasFatalFailure(); 849 return !testing::Test::HasFatalFailure();
821 } 850 }
822 851
823 TimedMessageLoopRunner::TimedMessageLoopRunner() 852 MessageLoopRunner::MessageLoopRunner() {
824 : loop_(new MessageLoopForUI()),
825 owned_(true),
826 quit_loop_invoked_(false) {
827 } 853 }
828 854
829 TimedMessageLoopRunner::~TimedMessageLoopRunner() { 855 MessageLoopRunner::~MessageLoopRunner() {
830 if (owned_)
831 delete loop_;
832 } 856 }
833 857
834 void TimedMessageLoopRunner::RunFor(int ms) { 858 void MessageLoopRunner::Run() {
835 QuitAfter(ms); 859 ui_test_utils::RunThisRunLoop(run_loop_.AsWeakPtr());
836 quit_loop_invoked_ = false;
837 loop_->Run();
838 } 860 }
839 861
840 void TimedMessageLoopRunner::Quit() { 862 base::Closure MessageLoopRunner::QuitNowClosure() {
841 quit_loop_invoked_ = true; 863 return base::Bind(&MessageLoopRunner::QuitNow, this);
842 loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure());
843 } 864 }
844 865
845 void TimedMessageLoopRunner::QuitAfter(int ms) { 866 void MessageLoopRunner::QuitNow() {
846 quit_loop_invoked_ = true; 867 ui_test_utils::QuitThisRunLoop(run_loop_.AsWeakPtr());
847 loop_->PostDelayedTask(
848 FROM_HERE,
849 MessageLoop::QuitClosure(),
850 base::TimeDelta::FromMilliseconds(ms));
851 } 868 }
852 869
853 TestWebSocketServer::TestWebSocketServer() 870 TestWebSocketServer::TestWebSocketServer()
854 : started_(false), 871 : started_(false),
855 port_(kDefaultWsPort), 872 port_(kDefaultWsPort),
856 secure_(false) { 873 secure_(false) {
857 #if defined(OS_POSIX) 874 #if defined(OS_POSIX)
858 process_group_id_ = base::kNullProcessHandle; 875 process_group_id_ = base::kNullProcessHandle;
859 #endif 876 #endif
860 } 877 }
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
1005 registrar_.Add(this, notification_type, source); 1022 registrar_.Add(this, notification_type, source);
1006 } 1023 }
1007 1024
1008 WindowedNotificationObserver::~WindowedNotificationObserver() {} 1025 WindowedNotificationObserver::~WindowedNotificationObserver() {}
1009 1026
1010 void WindowedNotificationObserver::Wait() { 1027 void WindowedNotificationObserver::Wait() {
1011 if (seen_) 1028 if (seen_)
1012 return; 1029 return;
1013 1030
1014 running_ = true; 1031 running_ = true;
1015 ui_test_utils::RunMessageLoop(); 1032 message_loop_runner_ = new MessageLoopRunner;
1033 message_loop_runner_->Run();
1016 EXPECT_TRUE(seen_); 1034 EXPECT_TRUE(seen_);
1017 } 1035 }
1018 1036
1019 void WindowedNotificationObserver::Observe( 1037 void WindowedNotificationObserver::Observe(
1020 int type, 1038 int type,
1021 const content::NotificationSource& source, 1039 const content::NotificationSource& source,
1022 const content::NotificationDetails& details) { 1040 const content::NotificationDetails& details) {
1023 source_ = source; 1041 source_ = source;
1024 details_ = details; 1042 details_ = details;
1025 seen_ = true; 1043 seen_ = true;
1026 if (!running_) 1044 if (!running_)
1027 return; 1045 return;
1028 1046
1029 MessageLoopForUI::current()->Quit(); 1047 message_loop_runner_->QuitNow();
1030 running_ = false; 1048 running_ = false;
1031 } 1049 }
1032 1050
1033 WindowedTabAddedNotificationObserver::WindowedTabAddedNotificationObserver( 1051 WindowedTabAddedNotificationObserver::WindowedTabAddedNotificationObserver(
1034 const content::NotificationSource& source) 1052 const content::NotificationSource& source)
1035 : WindowedNotificationObserver(chrome::NOTIFICATION_TAB_ADDED, source), 1053 : WindowedNotificationObserver(chrome::NOTIFICATION_TAB_ADDED, source),
1036 added_tab_(NULL) { 1054 added_tab_(NULL) {
1037 } 1055 }
1038 1056
1039 void WindowedTabAddedNotificationObserver::Observe( 1057 void WindowedTabAddedNotificationObserver::Observe(
(...skipping 30 matching lines...) Expand all
1070 expected_titles_.push_back(expected_title); 1088 expected_titles_.push_back(expected_title);
1071 } 1089 }
1072 1090
1073 TitleWatcher::~TitleWatcher() { 1091 TitleWatcher::~TitleWatcher() {
1074 } 1092 }
1075 1093
1076 const string16& TitleWatcher::WaitAndGetTitle() { 1094 const string16& TitleWatcher::WaitAndGetTitle() {
1077 if (expected_title_observed_) 1095 if (expected_title_observed_)
1078 return observed_title_; 1096 return observed_title_;
1079 quit_loop_on_observation_ = true; 1097 quit_loop_on_observation_ = true;
1080 ui_test_utils::RunMessageLoop(); 1098 message_loop_runner_ = new MessageLoopRunner;
1099 message_loop_runner_->Run();
1081 return observed_title_; 1100 return observed_title_;
1082 } 1101 }
1083 1102
1084 void TitleWatcher::Observe(int type, 1103 void TitleWatcher::Observe(int type,
1085 const content::NotificationSource& source, 1104 const content::NotificationSource& source,
1086 const content::NotificationDetails& details) { 1105 const content::NotificationDetails& details) {
1087 if (type == content::NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED) { 1106 if (type == content::NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED) {
1088 WebContents* source_contents = content::Source<WebContents>(source).ptr(); 1107 WebContents* source_contents = content::Source<WebContents>(source).ptr();
1089 ASSERT_EQ(web_contents_, source_contents); 1108 ASSERT_EQ(web_contents_, source_contents);
1090 } else if (type == content::NOTIFICATION_LOAD_STOP) { 1109 } else if (type == content::NOTIFICATION_LOAD_STOP) {
1091 NavigationController* controller = 1110 NavigationController* controller =
1092 content::Source<NavigationController>(source).ptr(); 1111 content::Source<NavigationController>(source).ptr();
1093 ASSERT_EQ(&web_contents_->GetController(), controller); 1112 ASSERT_EQ(&web_contents_->GetController(), controller);
1094 } else { 1113 } else {
1095 FAIL() << "Unexpected notification received."; 1114 FAIL() << "Unexpected notification received.";
1096 } 1115 }
1097 1116
1098 std::vector<string16>::const_iterator it = 1117 std::vector<string16>::const_iterator it =
1099 std::find(expected_titles_.begin(), 1118 std::find(expected_titles_.begin(),
1100 expected_titles_.end(), 1119 expected_titles_.end(),
1101 web_contents_->GetTitle()); 1120 web_contents_->GetTitle());
1102 if (it == expected_titles_.end()) 1121 if (it == expected_titles_.end())
1103 return; 1122 return;
1104 observed_title_ = *it; 1123 observed_title_ = *it;
1105 expected_title_observed_ = true; 1124 expected_title_observed_ = true;
1106 if (quit_loop_on_observation_) 1125 if (quit_loop_on_observation_) {
1107 MessageLoopForUI::current()->Quit(); 1126 // Only call Quit once, on first Observe:
1127 quit_loop_on_observation_ = false;
1128 message_loop_runner_->QuitNow();
1129 }
1108 } 1130 }
1109 1131
1110 BrowserAddedObserver::BrowserAddedObserver() 1132 BrowserAddedObserver::BrowserAddedObserver()
1111 : notification_observer_( 1133 : notification_observer_(
1112 chrome::NOTIFICATION_BROWSER_OPENED, 1134 chrome::NOTIFICATION_BROWSER_OPENED,
1113 content::NotificationService::AllSources()) { 1135 content::NotificationService::AllSources()) {
1114 original_browsers_.insert(BrowserList::begin(), BrowserList::end()); 1136 original_browsers_.insert(BrowserList::begin(), BrowserList::end());
1115 } 1137 }
1116 1138
1117 BrowserAddedObserver::~BrowserAddedObserver() { 1139 BrowserAddedObserver::~BrowserAddedObserver() {
(...skipping 14 matching lines...) Expand all
1132 DOMMessageQueue::~DOMMessageQueue() {} 1154 DOMMessageQueue::~DOMMessageQueue() {}
1133 1155
1134 void DOMMessageQueue::Observe(int type, 1156 void DOMMessageQueue::Observe(int type,
1135 const content::NotificationSource& source, 1157 const content::NotificationSource& source,
1136 const content::NotificationDetails& details) { 1158 const content::NotificationDetails& details) {
1137 content::Details<DomOperationNotificationDetails> dom_op_details(details); 1159 content::Details<DomOperationNotificationDetails> dom_op_details(details);
1138 content::Source<RenderViewHost> sender(source); 1160 content::Source<RenderViewHost> sender(source);
1139 message_queue_.push(dom_op_details->json); 1161 message_queue_.push(dom_op_details->json);
1140 if (waiting_for_message_) { 1162 if (waiting_for_message_) {
1141 waiting_for_message_ = false; 1163 waiting_for_message_ = false;
1142 MessageLoopForUI::current()->Quit(); 1164 message_loop_runner_->QuitNow();
1143 } 1165 }
1144 } 1166 }
1145 1167
1146 void DOMMessageQueue::ClearQueue() { 1168 void DOMMessageQueue::ClearQueue() {
1147 message_queue_ = std::queue<std::string>(); 1169 message_queue_ = std::queue<std::string>();
1148 } 1170 }
1149 1171
1150 bool DOMMessageQueue::WaitForMessage(std::string* message) { 1172 bool DOMMessageQueue::WaitForMessage(std::string* message) {
1151 if (message_queue_.empty()) { 1173 if (message_queue_.empty()) {
1152 waiting_for_message_ = true; 1174 waiting_for_message_ = true;
1153 // This will be quit when a new message comes in. 1175 // This will be quit when a new message comes in.
1154 RunMessageLoop(); 1176 message_loop_runner_ = new MessageLoopRunner;
1177 message_loop_runner_->Run();
1155 } 1178 }
1156 // The queue should not be empty, unless we were quit because of a timeout. 1179 // The queue should not be empty, unless we were quit because of a timeout.
1157 if (message_queue_.empty()) 1180 if (message_queue_.empty())
1158 return false; 1181 return false;
1159 if (message) 1182 if (message)
1160 *message = message_queue_.front(); 1183 *message = message_queue_.front();
1161 return true; 1184 return true;
1162 } 1185 }
1163 1186
1164 // Coordinates taking snapshots of a |RenderWidget|. 1187 // Coordinates taking snapshots of a |RenderWidget|.
1165 class SnapshotTaker { 1188 class SnapshotTaker {
1166 public: 1189 public:
1167 SnapshotTaker() : bitmap_(NULL) {} 1190 SnapshotTaker() : bitmap_(NULL) {}
1168 1191
1169 bool TakeRenderWidgetSnapshot(RenderWidgetHost* rwh, 1192 bool TakeRenderWidgetSnapshot(RenderWidgetHost* rwh,
1170 const gfx::Size& page_size, 1193 const gfx::Size& page_size,
1171 const gfx::Size& desired_size, 1194 const gfx::Size& desired_size,
1172 SkBitmap* bitmap) WARN_UNUSED_RESULT { 1195 SkBitmap* bitmap) WARN_UNUSED_RESULT {
1173 bitmap_ = bitmap; 1196 bitmap_ = bitmap;
1174 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator(); 1197 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
1175 generator->MonitorRenderer(rwh, true); 1198 generator->MonitorRenderer(rwh, true);
1176 snapshot_taken_ = false; 1199 snapshot_taken_ = false;
1177 generator->AskForSnapshot( 1200 generator->AskForSnapshot(
1178 rwh, 1201 rwh,
1179 base::Bind(&SnapshotTaker::OnSnapshotTaken, base::Unretained(this)), 1202 base::Bind(&SnapshotTaker::OnSnapshotTaken, base::Unretained(this)),
1180 page_size, 1203 page_size,
1181 desired_size); 1204 desired_size);
1182 ui_test_utils::RunMessageLoop(); 1205 message_loop_runner_ = new MessageLoopRunner;
1206 message_loop_runner_->Run();
1183 return snapshot_taken_; 1207 return snapshot_taken_;
1184 } 1208 }
1185 1209
1186 bool TakeEntirePageSnapshot(RenderViewHost* rvh, 1210 bool TakeEntirePageSnapshot(RenderViewHost* rvh,
1187 SkBitmap* bitmap) WARN_UNUSED_RESULT { 1211 SkBitmap* bitmap) WARN_UNUSED_RESULT {
1188 const wchar_t* script = 1212 const wchar_t* script =
1189 L"window.domAutomationController.send(" 1213 L"window.domAutomationController.send("
1190 L" JSON.stringify([document.width, document.height]))"; 1214 L" JSON.stringify([document.width, document.height]))";
1191 std::string json; 1215 std::string json;
1192 if (!ui_test_utils::ExecuteJavaScriptAndExtractString( 1216 if (!ui_test_utils::ExecuteJavaScriptAndExtractString(
(...skipping 14 matching lines...) Expand all
1207 // Take the snapshot. 1231 // Take the snapshot.
1208 gfx::Size page_size(width, height); 1232 gfx::Size page_size(width, height);
1209 return TakeRenderWidgetSnapshot(rvh, page_size, page_size, bitmap); 1233 return TakeRenderWidgetSnapshot(rvh, page_size, page_size, bitmap);
1210 } 1234 }
1211 1235
1212 private: 1236 private:
1213 // Called when the ThumbnailGenerator has taken the snapshot. 1237 // Called when the ThumbnailGenerator has taken the snapshot.
1214 void OnSnapshotTaken(const SkBitmap& bitmap) { 1238 void OnSnapshotTaken(const SkBitmap& bitmap) {
1215 *bitmap_ = bitmap; 1239 *bitmap_ = bitmap;
1216 snapshot_taken_ = true; 1240 snapshot_taken_ = true;
1217 MessageLoop::current()->Quit(); 1241 message_loop_runner_->QuitNow();
1218 } 1242 }
1219 1243
1220 SkBitmap* bitmap_; 1244 SkBitmap* bitmap_;
1221 // Whether the snapshot was actually taken and received by this SnapshotTaker. 1245 // Whether the snapshot was actually taken and received by this SnapshotTaker.
1222 // This will be false if the test times out. 1246 // This will be false if the test times out.
1223 bool snapshot_taken_; 1247 bool snapshot_taken_;
1248 scoped_refptr<MessageLoopRunner> message_loop_runner_;
1224 1249
1225 DISALLOW_COPY_AND_ASSIGN(SnapshotTaker); 1250 DISALLOW_COPY_AND_ASSIGN(SnapshotTaker);
1226 }; 1251 };
1227 1252
1228 bool TakeRenderWidgetSnapshot(RenderWidgetHost* rwh, 1253 bool TakeRenderWidgetSnapshot(RenderWidgetHost* rwh,
1229 const gfx::Size& page_size, 1254 const gfx::Size& page_size,
1230 SkBitmap* bitmap) { 1255 SkBitmap* bitmap) {
1231 DCHECK(bitmap); 1256 DCHECK(bitmap);
1232 SnapshotTaker taker; 1257 SnapshotTaker taker;
1233 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap); 1258 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap);
1234 } 1259 }
1235 1260
1236 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) { 1261 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) {
1237 DCHECK(bitmap); 1262 DCHECK(bitmap);
1238 SnapshotTaker taker; 1263 SnapshotTaker taker;
1239 return taker.TakeEntirePageSnapshot(rvh, bitmap); 1264 return taker.TakeEntirePageSnapshot(rvh, bitmap);
1240 } 1265 }
1241 1266
1242 void OverrideGeolocation(double latitude, double longitude) { 1267 void OverrideGeolocation(double latitude, double longitude) {
1243 content::Geoposition position; 1268 content::Geoposition position;
1244 position.latitude = latitude; 1269 position.latitude = latitude;
1245 position.longitude = longitude; 1270 position.longitude = longitude;
1246 position.altitude = 0.; 1271 position.altitude = 0.;
1247 position.accuracy = 0.; 1272 position.accuracy = 0.;
1248 position.timestamp = base::Time::Now(); 1273 position.timestamp = base::Time::Now();
1274 scoped_refptr<MessageLoopRunner> runner =
1275 new MessageLoopRunner;
jar (doing other things) 2012/06/25 21:19:43 nit: unwrap line.
jbates 2012/06/27 02:06:37 Done.
1249 content::OverrideLocationForTesting(position, 1276 content::OverrideLocationForTesting(position,
1250 base::Bind(MessageLoop::QuitClosure())); 1277 runner->QuitNowClosure());
1251 RunMessageLoop(); 1278 runner->Run();
1252 } 1279 }
1253 1280
1254 namespace internal { 1281 namespace internal {
1255 1282
1256 void ClickTask(ui_controls::MouseButton button, 1283 void ClickTask(ui_controls::MouseButton button,
1257 int state, 1284 int state,
1258 const base::Closure& followup) { 1285 const base::Closure& followup) {
1259 if (!followup.is_null()) 1286 if (!followup.is_null())
1260 ui_controls::SendMouseEventsNotifyWhenDone(button, state, followup); 1287 ui_controls::SendMouseEventsNotifyWhenDone(button, state, followup);
1261 else 1288 else
1262 ui_controls::SendMouseEvents(button, state); 1289 ui_controls::SendMouseEvents(button, state);
1263 } 1290 }
1264 1291
1265 } // namespace internal 1292 } // namespace internal
1266 } // namespace ui_test_utils 1293 } // namespace ui_test_utils
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698