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

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: fetch/merge, no change 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 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 = 2;
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 MessageLoop::RunLoop run_loop;
275 MessageLoopForUI* ui_loop = 285 RunRunLoop(run_loop.AsWeakPtr());
276 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI) ? 286 }
277 MessageLoopForUI::current() : NULL; 287
278 MessageLoop::ScopedNestableTaskAllower allow(loop); 288 void RunRunLoop(const base::WeakPtr<MessageLoop::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 (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
282 #elif defined(TOOLKIT_VIEWS) 292 #if !defined(USE_AURA) && defined(TOOLKIT_VIEWS)
283 views::AcceleratorHandler handler; 293 views::AcceleratorHandler handler;
284 ui_loop->RunWithDispatcher(&handler); 294 run_loop->set_dispatcher(&handler);
285 #elif defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
286 ui_loop->RunWithDispatcher(NULL);
287 #else
288 ui_loop->Run();
289 #endif 295 #endif
296 run_loop->Run();
290 } else { 297 } else {
291 loop->Run(); 298 run_loop->Run();
292 } 299 }
293 } 300 }
294 301
302 static void DeferredQuitRunLoop(
303 const base::WeakPtr<MessageLoop::RunLoop>& run_loop,
304 int num_quit_deferrals) {
305 if (num_quit_deferrals <= 0) {
306 if (run_loop) run_loop->Stop();
307 } else {
308 MessageLoop::current()->PostTask(FROM_HERE,
309 base::Bind(&DeferredQuitRunLoop, run_loop, num_quit_deferrals - 1));
310 }
311 }
312
313 // Quit the MessageLoop Run of the specified |run_id|.
314 void QuitRunLoop(const base::WeakPtr<MessageLoop::RunLoop>& run_loop) {
315 DeferredQuitRunLoop(run_loop, kNumQuitDeferrals);
316 }
317
318 base::Closure QuitRunLoopClosure(
319 const base::WeakPtr<MessageLoop::RunLoop>& run_loop) {
320 return base::Bind(&QuitRunLoop, run_loop);
321 }
322
295 void RunAllPendingInMessageLoop() { 323 void RunAllPendingInMessageLoop() {
296 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 324 MessageLoop::current()->PostTask(FROM_HERE,
325 MessageLoop::QuitWhenIdleClosure());
297 ui_test_utils::RunMessageLoop(); 326 ui_test_utils::RunMessageLoop();
298 } 327 }
299 328
300 void RunAllPendingInMessageLoop(content::BrowserThread::ID thread_id) { 329 void RunAllPendingInMessageLoop(content::BrowserThread::ID thread_id) {
301 if (content::BrowserThread::CurrentlyOn(thread_id)) { 330 if (content::BrowserThread::CurrentlyOn(thread_id)) {
302 RunAllPendingInMessageLoop(); 331 RunAllPendingInMessageLoop();
303 return; 332 return;
304 } 333 }
305 content::BrowserThread::ID current_thread_id; 334 content::BrowserThread::ID current_thread_id;
306 if (!content::BrowserThread::GetCurrentThreadIdentifier(&current_thread_id)) { 335 if (!content::BrowserThread::GetCurrentThreadIdentifier(&current_thread_id)) {
307 NOTREACHED(); 336 NOTREACHED();
308 return; 337 return;
309 } 338 }
339
340 MessageLoop::RunLoop run_loop;
310 content::BrowserThread::PostTask(thread_id, FROM_HERE, 341 content::BrowserThread::PostTask(thread_id, FROM_HERE,
311 base::Bind(&RunAllPendingMessageAndSendQuit, current_thread_id)); 342 base::Bind(&RunAllPendingMessageAndSendQuit, current_thread_id,
312 343 run_loop.StopClosure()));
313 ui_test_utils::RunMessageLoop(); 344 ui_test_utils::RunRunLoop(run_loop.AsWeakPtr());
314 } 345 }
315 346
316 bool GetCurrentTabTitle(const Browser* browser, string16* title) { 347 bool GetCurrentTabTitle(const Browser* browser, string16* title) {
317 WebContents* web_contents = browser->GetActiveWebContents(); 348 WebContents* web_contents = browser->GetActiveWebContents();
318 if (!web_contents) 349 if (!web_contents)
319 return false; 350 return false;
320 NavigationEntry* last_entry = web_contents->GetController().GetActiveEntry(); 351 NavigationEntry* last_entry = web_contents->GetController().GetActiveEntry();
321 if (!last_entry) 352 if (!last_entry)
322 return false; 353 return false;
323 title->assign(last_entry->GetTitleForDisplay("")); 354 title->assign(last_entry->GetTitleForDisplay(""));
324 return true; 355 return true;
325 } 356 }
326 357
327 void WaitForNavigations(NavigationController* controller, 358 void WaitForNavigations(NavigationController* controller,
328 int number_of_navigations) { 359 int number_of_navigations) {
329 content::TestNavigationObserver observer( 360 content::TestNavigationObserver observer(
330 content::Source<NavigationController>(controller), NULL, 361 content::Source<NavigationController>(controller), NULL,
331 number_of_navigations); 362 number_of_navigations);
363 MessageLoop::RunLoop run_loop;
332 observer.WaitForObservation( 364 observer.WaitForObservation(
333 base::Bind(&ui_test_utils::RunMessageLoop), 365 base::Bind(&ui_test_utils::RunRunLoop, run_loop.AsWeakPtr()),
334 base::Bind(&MessageLoop::Quit, 366 base::Bind(&ui_test_utils::QuitRunLoop, run_loop.AsWeakPtr()));
335 base::Unretained(MessageLoopForUI::current())));
336 } 367 }
337 368
338 void WaitForNewTab(Browser* browser) { 369 void WaitForNewTab(Browser* browser) {
339 WindowedNotificationObserver observer( 370 WindowedNotificationObserver observer(
340 chrome::NOTIFICATION_TAB_ADDED, 371 chrome::NOTIFICATION_TAB_ADDED,
341 content::Source<content::WebContentsDelegate>(browser)); 372 content::Source<content::WebContentsDelegate>(browser));
342 observer.Wait(); 373 observer.Wait();
343 } 374 }
344 375
345 void WaitForLoadStop(WebContents* tab) { 376 void WaitForLoadStop(WebContents* tab) {
(...skipping 29 matching lines...) Expand all
375 Browser::OpenURLOffTheRecord(profile, url); 406 Browser::OpenURLOffTheRecord(profile, url);
376 Browser* browser = browser::FindTabbedBrowser( 407 Browser* browser = browser::FindTabbedBrowser(
377 profile->GetOffTheRecordProfile(), false); 408 profile->GetOffTheRecordProfile(), false);
378 WaitForNavigations(&browser->GetActiveWebContents()->GetController(), 1); 409 WaitForNavigations(&browser->GetActiveWebContents()->GetController(), 1);
379 } 410 }
380 411
381 void NavigateToURL(browser::NavigateParams* params) { 412 void NavigateToURL(browser::NavigateParams* params) {
382 content::TestNavigationObserver observer( 413 content::TestNavigationObserver observer(
383 content::NotificationService::AllSources(), NULL, 1); 414 content::NotificationService::AllSources(), NULL, 1);
384 browser::Navigate(params); 415 browser::Navigate(params);
416 MessageLoop::RunLoop run_loop;
385 observer.WaitForObservation( 417 observer.WaitForObservation(
386 base::Bind(&ui_test_utils::RunMessageLoop), 418 base::Bind(&ui_test_utils::RunRunLoop, run_loop.AsWeakPtr()),
387 base::Bind(&MessageLoop::Quit, 419 base::Bind(&ui_test_utils::QuitRunLoop, run_loop.AsWeakPtr()));
388 base::Unretained(MessageLoopForUI::current())));
389
390 } 420 }
391 421
392 void NavigateToURL(Browser* browser, const GURL& url) { 422 void NavigateToURL(Browser* browser, const GURL& url) {
393 NavigateToURLWithDisposition(browser, url, CURRENT_TAB, 423 NavigateToURLWithDisposition(browser, url, CURRENT_TAB,
394 BROWSER_TEST_WAIT_FOR_NAVIGATION); 424 BROWSER_TEST_WAIT_FOR_NAVIGATION);
395 } 425 }
396 426
397 // Navigates the specified tab (via |disposition|) of |browser| to |url|, 427 // Navigates the specified tab (via |disposition|) of |browser| to |url|,
398 // blocking until the |number_of_navigations| specified complete. 428 // blocking until the |number_of_navigations| specified complete.
399 // |disposition| indicates what tab the download occurs in, and 429 // |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() 479 << " Unable to wait for navigation to \"" << url.spec()
450 << "\" because the new tab is not available yet"; 480 << "\" because the new tab is not available yet";
451 return; 481 return;
452 } else if ((disposition == CURRENT_TAB) || 482 } else if ((disposition == CURRENT_TAB) ||
453 (disposition == NEW_FOREGROUND_TAB) || 483 (disposition == NEW_FOREGROUND_TAB) ||
454 (disposition == SINGLETON_TAB)) { 484 (disposition == SINGLETON_TAB)) {
455 // The currently selected tab is the right one. 485 // The currently selected tab is the right one.
456 web_contents = browser->GetActiveWebContents(); 486 web_contents = browser->GetActiveWebContents();
457 } 487 }
458 if (disposition == CURRENT_TAB) { 488 if (disposition == CURRENT_TAB) {
489 MessageLoop::RunLoop run_loop;
459 same_tab_observer.WaitForObservation( 490 same_tab_observer.WaitForObservation(
460 base::Bind(&ui_test_utils::RunMessageLoop), 491 base::Bind(&ui_test_utils::RunRunLoop, run_loop.AsWeakPtr()),
461 base::Bind(&MessageLoop::Quit, 492 base::Bind(&ui_test_utils::QuitRunLoop, run_loop.AsWeakPtr()));
462 base::Unretained(MessageLoopForUI::current())));
463 return; 493 return;
464 } else if (web_contents) { 494 } else if (web_contents) {
465 NavigationController* controller = &web_contents->GetController(); 495 NavigationController* controller = &web_contents->GetController();
466 WaitForNavigations(controller, number_of_navigations); 496 WaitForNavigations(controller, number_of_navigations);
467 return; 497 return;
468 } 498 }
469 EXPECT_TRUE(NULL != web_contents) << " Unable to wait for navigation to \"" 499 EXPECT_TRUE(NULL != web_contents) << " Unable to wait for navigation to \""
470 << url.spec() << "\"" 500 << url.spec() << "\""
471 << " because we can't get the tab contents"; 501 << " because we can't get the tab contents";
472 } 502 }
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
696 int type, 726 int type,
697 const content::NotificationSource& source) { 727 const content::NotificationSource& source) {
698 content::NotificationRegistrar registrar; 728 content::NotificationRegistrar registrar;
699 registrar.Add(observer, type, source); 729 registrar.Add(observer, type, source);
700 RunMessageLoop(); 730 RunMessageLoop();
701 } 731 }
702 732
703 void WaitForBookmarkModelToLoad(BookmarkModel* model) { 733 void WaitForBookmarkModelToLoad(BookmarkModel* model) {
704 if (model->IsLoaded()) 734 if (model->IsLoaded())
705 return; 735 return;
706 BookmarkLoadObserver observer; 736 MessageLoop::RunLoop run_loop;
737 BookmarkLoadObserver observer(run_loop.AsWeakPtr());
707 model->AddObserver(&observer); 738 model->AddObserver(&observer);
708 RunMessageLoop(); 739 RunRunLoop(run_loop.AsWeakPtr());
709 model->RemoveObserver(&observer); 740 model->RemoveObserver(&observer);
710 ASSERT_TRUE(model->IsLoaded()); 741 ASSERT_TRUE(model->IsLoaded());
711 } 742 }
712 743
713 void WaitForTemplateURLServiceToLoad(TemplateURLService* service) { 744 void WaitForTemplateURLServiceToLoad(TemplateURLService* service) {
714 if (service->loaded()) 745 if (service->loaded())
715 return; 746 return;
716 service->Load(); 747 service->Load();
717 TemplateURLServiceTestUtil::BlockTillServiceProcessesRequests(); 748 TemplateURLServiceTestUtil::BlockTillServiceProcessesRequests();
718 ASSERT_TRUE(service->loaded()); 749 ASSERT_TRUE(service->loaded());
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
756 787
757 bool SendKeyPressSync(const Browser* browser, 788 bool SendKeyPressSync(const Browser* browser,
758 ui::KeyboardCode key, 789 ui::KeyboardCode key,
759 bool control, 790 bool control,
760 bool shift, 791 bool shift,
761 bool alt, 792 bool alt,
762 bool command) { 793 bool command) {
763 gfx::NativeWindow window = NULL; 794 gfx::NativeWindow window = NULL;
764 if (!GetNativeWindow(browser, &window)) 795 if (!GetNativeWindow(browser, &window))
765 return false; 796 return false;
766 797 scoped_refptr<MessageLoopRunner> runner =
798 new MessageLoopRunner;
767 bool result; 799 bool result;
768 result = ui_controls::SendKeyPressNotifyWhenDone( 800 result = ui_controls::SendKeyPressNotifyWhenDone(
769 window, key, control, shift, alt, command, MessageLoop::QuitClosure()); 801 window, key, control, shift, alt, command, runner->QuitNowClosure());
770 #if defined(OS_WIN) 802 #if defined(OS_WIN)
771 if (!result && BringBrowserWindowToFront(browser)) { 803 if (!result && BringBrowserWindowToFront(browser)) {
772 result = ui_controls::SendKeyPressNotifyWhenDone( 804 result = ui_controls::SendKeyPressNotifyWhenDone(
773 window, key, control, shift, alt, command, MessageLoop::QuitClosure()); 805 window, key, control, shift, alt, command, runner->QuitNowClosure());
774 } 806 }
775 #endif 807 #endif
776 if (!result) { 808 if (!result) {
777 LOG(ERROR) << "ui_controls::SendKeyPressNotifyWhenDone failed"; 809 LOG(ERROR) << "ui_controls::SendKeyPressNotifyWhenDone failed";
778 return false; 810 return false;
779 } 811 }
780 812
781 // Run the message loop. It'll stop running when either the key was received 813 // 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 814 // or the test timed out (in which case testing::Test::HasFatalFailure should
783 // be set). 815 // be set).
784 RunMessageLoop(); 816 runner->Run();
785 return !testing::Test::HasFatalFailure(); 817 return !testing::Test::HasFatalFailure();
786 } 818 }
787 819
788 bool SendKeyPressAndWait(const Browser* browser, 820 bool SendKeyPressAndWait(const Browser* browser,
789 ui::KeyboardCode key, 821 ui::KeyboardCode key,
790 bool control, 822 bool control,
791 bool shift, 823 bool shift,
792 bool alt, 824 bool alt,
793 bool command, 825 bool command,
794 int type, 826 int type,
795 const content::NotificationSource& source) { 827 const content::NotificationSource& source) {
796 WindowedNotificationObserver observer(type, source); 828 WindowedNotificationObserver observer(type, source);
797 829
798 if (!SendKeyPressSync(browser, key, control, shift, alt, command)) 830 if (!SendKeyPressSync(browser, key, control, shift, alt, command))
799 return false; 831 return false;
800 832
801 observer.Wait(); 833 observer.Wait();
802 return !testing::Test::HasFatalFailure(); 834 return !testing::Test::HasFatalFailure();
803 } 835 }
804 836
805 bool SendMouseMoveSync(const gfx::Point& location) { 837 bool SendMouseMoveSync(const gfx::Point& location) {
806 if (!ui_controls::SendMouseMoveNotifyWhenDone(location.x(), location.y(), 838 scoped_refptr<MessageLoopRunner> runner =
807 MessageLoop::QuitClosure())) { 839 new MessageLoopRunner;
840 if (!ui_controls::SendMouseMoveNotifyWhenDone(
841 location.x(), location.y(), runner->QuitNowClosure())) {
808 return false; 842 return false;
809 } 843 }
810 RunMessageLoop(); 844 runner->Run();
811 return !testing::Test::HasFatalFailure(); 845 return !testing::Test::HasFatalFailure();
812 } 846 }
813 847
814 bool SendMouseEventsSync(ui_controls::MouseButton type, int state) { 848 bool SendMouseEventsSync(ui_controls::MouseButton type, int state) {
849 scoped_refptr<MessageLoopRunner> runner =
850 new MessageLoopRunner;
815 if (!ui_controls::SendMouseEventsNotifyWhenDone( 851 if (!ui_controls::SendMouseEventsNotifyWhenDone(
816 type, state, MessageLoop::QuitClosure())) { 852 type, state, runner->QuitNowClosure())) {
817 return false; 853 return false;
818 } 854 }
819 RunMessageLoop(); 855 runner->Run();
820 return !testing::Test::HasFatalFailure(); 856 return !testing::Test::HasFatalFailure();
821 } 857 }
822 858
823 TimedMessageLoopRunner::TimedMessageLoopRunner() 859 MessageLoopRunner::MessageLoopRunner() {
824 : loop_(new MessageLoopForUI()),
825 owned_(true),
826 quit_loop_invoked_(false) {
827 } 860 }
828 861
829 TimedMessageLoopRunner::~TimedMessageLoopRunner() { 862 MessageLoopRunner::~MessageLoopRunner() {
830 if (owned_)
831 delete loop_;
832 } 863 }
833 864
834 void TimedMessageLoopRunner::RunFor(int ms) { 865 void MessageLoopRunner::Run() {
835 QuitAfter(ms); 866 ui_test_utils::RunRunLoop(run_loop_.AsWeakPtr());
836 quit_loop_invoked_ = false;
837 loop_->Run();
838 } 867 }
839 868
840 void TimedMessageLoopRunner::Quit() { 869 base::Closure MessageLoopRunner::QuitNowClosure() {
841 quit_loop_invoked_ = true; 870 return base::Bind(&MessageLoopRunner::QuitNow, this);
842 loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure());
843 } 871 }
844 872
845 void TimedMessageLoopRunner::QuitAfter(int ms) { 873 void MessageLoopRunner::QuitNow() {
846 quit_loop_invoked_ = true; 874 ui_test_utils::QuitRunLoop(run_loop_.AsWeakPtr());
847 loop_->PostDelayedTask(
848 FROM_HERE,
849 MessageLoop::QuitClosure(),
850 base::TimeDelta::FromMilliseconds(ms));
851 } 875 }
852 876
853 TestWebSocketServer::TestWebSocketServer() 877 TestWebSocketServer::TestWebSocketServer()
854 : started_(false), 878 : started_(false),
855 port_(kDefaultWsPort), 879 port_(kDefaultWsPort),
856 secure_(false) { 880 secure_(false) {
857 #if defined(OS_POSIX) 881 #if defined(OS_POSIX)
858 process_group_id_ = base::kNullProcessHandle; 882 process_group_id_ = base::kNullProcessHandle;
859 #endif 883 #endif
860 } 884 }
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
1005 registrar_.Add(this, notification_type, source); 1029 registrar_.Add(this, notification_type, source);
1006 } 1030 }
1007 1031
1008 WindowedNotificationObserver::~WindowedNotificationObserver() {} 1032 WindowedNotificationObserver::~WindowedNotificationObserver() {}
1009 1033
1010 void WindowedNotificationObserver::Wait() { 1034 void WindowedNotificationObserver::Wait() {
1011 if (seen_) 1035 if (seen_)
1012 return; 1036 return;
1013 1037
1014 running_ = true; 1038 running_ = true;
1015 ui_test_utils::RunMessageLoop(); 1039 message_loop_runner_ = new MessageLoopRunner;
1040 message_loop_runner_->Run();
1016 EXPECT_TRUE(seen_); 1041 EXPECT_TRUE(seen_);
1017 } 1042 }
1018 1043
1019 void WindowedNotificationObserver::Observe( 1044 void WindowedNotificationObserver::Observe(
1020 int type, 1045 int type,
1021 const content::NotificationSource& source, 1046 const content::NotificationSource& source,
1022 const content::NotificationDetails& details) { 1047 const content::NotificationDetails& details) {
1023 source_ = source; 1048 source_ = source;
1024 details_ = details; 1049 details_ = details;
1025 seen_ = true; 1050 seen_ = true;
1026 if (!running_) 1051 if (!running_)
1027 return; 1052 return;
1028 1053
1029 MessageLoopForUI::current()->Quit(); 1054 message_loop_runner_->QuitNow();
1030 running_ = false; 1055 running_ = false;
1031 } 1056 }
1032 1057
1033 WindowedTabAddedNotificationObserver::WindowedTabAddedNotificationObserver( 1058 WindowedTabAddedNotificationObserver::WindowedTabAddedNotificationObserver(
1034 const content::NotificationSource& source) 1059 const content::NotificationSource& source)
1035 : WindowedNotificationObserver(chrome::NOTIFICATION_TAB_ADDED, source), 1060 : WindowedNotificationObserver(chrome::NOTIFICATION_TAB_ADDED, source),
1036 added_tab_(NULL) { 1061 added_tab_(NULL) {
1037 } 1062 }
1038 1063
1039 void WindowedTabAddedNotificationObserver::Observe( 1064 void WindowedTabAddedNotificationObserver::Observe(
(...skipping 30 matching lines...) Expand all
1070 expected_titles_.push_back(expected_title); 1095 expected_titles_.push_back(expected_title);
1071 } 1096 }
1072 1097
1073 TitleWatcher::~TitleWatcher() { 1098 TitleWatcher::~TitleWatcher() {
1074 } 1099 }
1075 1100
1076 const string16& TitleWatcher::WaitAndGetTitle() { 1101 const string16& TitleWatcher::WaitAndGetTitle() {
1077 if (expected_title_observed_) 1102 if (expected_title_observed_)
1078 return observed_title_; 1103 return observed_title_;
1079 quit_loop_on_observation_ = true; 1104 quit_loop_on_observation_ = true;
1080 ui_test_utils::RunMessageLoop(); 1105 message_loop_runner_ = new MessageLoopRunner;
1106 message_loop_runner_->Run();
1081 return observed_title_; 1107 return observed_title_;
1082 } 1108 }
1083 1109
1084 void TitleWatcher::Observe(int type, 1110 void TitleWatcher::Observe(int type,
1085 const content::NotificationSource& source, 1111 const content::NotificationSource& source,
1086 const content::NotificationDetails& details) { 1112 const content::NotificationDetails& details) {
1087 if (type == content::NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED) { 1113 if (type == content::NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED) {
1088 WebContents* source_contents = content::Source<WebContents>(source).ptr(); 1114 WebContents* source_contents = content::Source<WebContents>(source).ptr();
1089 ASSERT_EQ(web_contents_, source_contents); 1115 ASSERT_EQ(web_contents_, source_contents);
1090 } else if (type == content::NOTIFICATION_LOAD_STOP) { 1116 } else if (type == content::NOTIFICATION_LOAD_STOP) {
1091 NavigationController* controller = 1117 NavigationController* controller =
1092 content::Source<NavigationController>(source).ptr(); 1118 content::Source<NavigationController>(source).ptr();
1093 ASSERT_EQ(&web_contents_->GetController(), controller); 1119 ASSERT_EQ(&web_contents_->GetController(), controller);
1094 } else { 1120 } else {
1095 FAIL() << "Unexpected notification received."; 1121 FAIL() << "Unexpected notification received.";
1096 } 1122 }
1097 1123
1098 std::vector<string16>::const_iterator it = 1124 std::vector<string16>::const_iterator it =
1099 std::find(expected_titles_.begin(), 1125 std::find(expected_titles_.begin(),
1100 expected_titles_.end(), 1126 expected_titles_.end(),
1101 web_contents_->GetTitle()); 1127 web_contents_->GetTitle());
1102 if (it == expected_titles_.end()) 1128 if (it == expected_titles_.end())
1103 return; 1129 return;
1104 observed_title_ = *it; 1130 observed_title_ = *it;
1105 expected_title_observed_ = true; 1131 expected_title_observed_ = true;
1106 if (quit_loop_on_observation_) 1132 if (quit_loop_on_observation_) {
1107 MessageLoopForUI::current()->Quit(); 1133 // Only call Quit once, on first Observe:
1134 quit_loop_on_observation_ = false;
1135 message_loop_runner_->QuitNow();
1136 }
1108 } 1137 }
1109 1138
1110 BrowserAddedObserver::BrowserAddedObserver() 1139 BrowserAddedObserver::BrowserAddedObserver()
1111 : notification_observer_( 1140 : notification_observer_(
1112 chrome::NOTIFICATION_BROWSER_OPENED, 1141 chrome::NOTIFICATION_BROWSER_OPENED,
1113 content::NotificationService::AllSources()) { 1142 content::NotificationService::AllSources()) {
1114 original_browsers_.insert(BrowserList::begin(), BrowserList::end()); 1143 original_browsers_.insert(BrowserList::begin(), BrowserList::end());
1115 } 1144 }
1116 1145
1117 BrowserAddedObserver::~BrowserAddedObserver() { 1146 BrowserAddedObserver::~BrowserAddedObserver() {
(...skipping 14 matching lines...) Expand all
1132 DOMMessageQueue::~DOMMessageQueue() {} 1161 DOMMessageQueue::~DOMMessageQueue() {}
1133 1162
1134 void DOMMessageQueue::Observe(int type, 1163 void DOMMessageQueue::Observe(int type,
1135 const content::NotificationSource& source, 1164 const content::NotificationSource& source,
1136 const content::NotificationDetails& details) { 1165 const content::NotificationDetails& details) {
1137 content::Details<DomOperationNotificationDetails> dom_op_details(details); 1166 content::Details<DomOperationNotificationDetails> dom_op_details(details);
1138 content::Source<RenderViewHost> sender(source); 1167 content::Source<RenderViewHost> sender(source);
1139 message_queue_.push(dom_op_details->json); 1168 message_queue_.push(dom_op_details->json);
1140 if (waiting_for_message_) { 1169 if (waiting_for_message_) {
1141 waiting_for_message_ = false; 1170 waiting_for_message_ = false;
1142 MessageLoopForUI::current()->Quit(); 1171 message_loop_runner_->QuitNow();
1143 } 1172 }
1144 } 1173 }
1145 1174
1146 void DOMMessageQueue::ClearQueue() { 1175 void DOMMessageQueue::ClearQueue() {
1147 message_queue_ = std::queue<std::string>(); 1176 message_queue_ = std::queue<std::string>();
1148 } 1177 }
1149 1178
1150 bool DOMMessageQueue::WaitForMessage(std::string* message) { 1179 bool DOMMessageQueue::WaitForMessage(std::string* message) {
1151 if (message_queue_.empty()) { 1180 if (message_queue_.empty()) {
1152 waiting_for_message_ = true; 1181 waiting_for_message_ = true;
1153 // This will be quit when a new message comes in. 1182 // This will be quit when a new message comes in.
1154 RunMessageLoop(); 1183 message_loop_runner_ = new MessageLoopRunner;
1184 message_loop_runner_->Run();
1155 } 1185 }
1156 // The queue should not be empty, unless we were quit because of a timeout. 1186 // The queue should not be empty, unless we were quit because of a timeout.
1157 if (message_queue_.empty()) 1187 if (message_queue_.empty())
1158 return false; 1188 return false;
1159 if (message) 1189 if (message)
1160 *message = message_queue_.front(); 1190 *message = message_queue_.front();
1161 return true; 1191 return true;
1162 } 1192 }
1163 1193
1164 // Coordinates taking snapshots of a |RenderWidget|. 1194 // Coordinates taking snapshots of a |RenderWidget|.
1165 class SnapshotTaker { 1195 class SnapshotTaker {
1166 public: 1196 public:
1167 SnapshotTaker() : bitmap_(NULL) {} 1197 SnapshotTaker() : bitmap_(NULL) {}
1168 1198
1169 bool TakeRenderWidgetSnapshot(RenderWidgetHost* rwh, 1199 bool TakeRenderWidgetSnapshot(RenderWidgetHost* rwh,
1170 const gfx::Size& page_size, 1200 const gfx::Size& page_size,
1171 const gfx::Size& desired_size, 1201 const gfx::Size& desired_size,
1172 SkBitmap* bitmap) WARN_UNUSED_RESULT { 1202 SkBitmap* bitmap) WARN_UNUSED_RESULT {
1173 bitmap_ = bitmap; 1203 bitmap_ = bitmap;
1174 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator(); 1204 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
1175 generator->MonitorRenderer(rwh, true); 1205 generator->MonitorRenderer(rwh, true);
1176 snapshot_taken_ = false; 1206 snapshot_taken_ = false;
1177 generator->AskForSnapshot( 1207 generator->AskForSnapshot(
1178 rwh, 1208 rwh,
1179 base::Bind(&SnapshotTaker::OnSnapshotTaken, base::Unretained(this)), 1209 base::Bind(&SnapshotTaker::OnSnapshotTaken, base::Unretained(this)),
1180 page_size, 1210 page_size,
1181 desired_size); 1211 desired_size);
1182 ui_test_utils::RunMessageLoop(); 1212 message_loop_runner_ = new MessageLoopRunner;
1213 message_loop_runner_->Run();
1183 return snapshot_taken_; 1214 return snapshot_taken_;
1184 } 1215 }
1185 1216
1186 bool TakeEntirePageSnapshot(RenderViewHost* rvh, 1217 bool TakeEntirePageSnapshot(RenderViewHost* rvh,
1187 SkBitmap* bitmap) WARN_UNUSED_RESULT { 1218 SkBitmap* bitmap) WARN_UNUSED_RESULT {
1188 const wchar_t* script = 1219 const wchar_t* script =
1189 L"window.domAutomationController.send(" 1220 L"window.domAutomationController.send("
1190 L" JSON.stringify([document.width, document.height]))"; 1221 L" JSON.stringify([document.width, document.height]))";
1191 std::string json; 1222 std::string json;
1192 if (!ui_test_utils::ExecuteJavaScriptAndExtractString( 1223 if (!ui_test_utils::ExecuteJavaScriptAndExtractString(
(...skipping 14 matching lines...) Expand all
1207 // Take the snapshot. 1238 // Take the snapshot.
1208 gfx::Size page_size(width, height); 1239 gfx::Size page_size(width, height);
1209 return TakeRenderWidgetSnapshot(rvh, page_size, page_size, bitmap); 1240 return TakeRenderWidgetSnapshot(rvh, page_size, page_size, bitmap);
1210 } 1241 }
1211 1242
1212 private: 1243 private:
1213 // Called when the ThumbnailGenerator has taken the snapshot. 1244 // Called when the ThumbnailGenerator has taken the snapshot.
1214 void OnSnapshotTaken(const SkBitmap& bitmap) { 1245 void OnSnapshotTaken(const SkBitmap& bitmap) {
1215 *bitmap_ = bitmap; 1246 *bitmap_ = bitmap;
1216 snapshot_taken_ = true; 1247 snapshot_taken_ = true;
1217 MessageLoop::current()->Quit(); 1248 message_loop_runner_->QuitNow();
1218 } 1249 }
1219 1250
1220 SkBitmap* bitmap_; 1251 SkBitmap* bitmap_;
1221 // Whether the snapshot was actually taken and received by this SnapshotTaker. 1252 // Whether the snapshot was actually taken and received by this SnapshotTaker.
1222 // This will be false if the test times out. 1253 // This will be false if the test times out.
1223 bool snapshot_taken_; 1254 bool snapshot_taken_;
1255 scoped_refptr<MessageLoopRunner> message_loop_runner_;
1224 1256
1225 DISALLOW_COPY_AND_ASSIGN(SnapshotTaker); 1257 DISALLOW_COPY_AND_ASSIGN(SnapshotTaker);
1226 }; 1258 };
1227 1259
1228 bool TakeRenderWidgetSnapshot(RenderWidgetHost* rwh, 1260 bool TakeRenderWidgetSnapshot(RenderWidgetHost* rwh,
1229 const gfx::Size& page_size, 1261 const gfx::Size& page_size,
1230 SkBitmap* bitmap) { 1262 SkBitmap* bitmap) {
1231 DCHECK(bitmap); 1263 DCHECK(bitmap);
1232 SnapshotTaker taker; 1264 SnapshotTaker taker;
1233 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap); 1265 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap);
1234 } 1266 }
1235 1267
1236 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) { 1268 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) {
1237 DCHECK(bitmap); 1269 DCHECK(bitmap);
1238 SnapshotTaker taker; 1270 SnapshotTaker taker;
1239 return taker.TakeEntirePageSnapshot(rvh, bitmap); 1271 return taker.TakeEntirePageSnapshot(rvh, bitmap);
1240 } 1272 }
1241 1273
1242 void OverrideGeolocation(double latitude, double longitude) { 1274 void OverrideGeolocation(double latitude, double longitude) {
1243 content::Geoposition position; 1275 content::Geoposition position;
1244 position.latitude = latitude; 1276 position.latitude = latitude;
1245 position.longitude = longitude; 1277 position.longitude = longitude;
1246 position.altitude = 0.; 1278 position.altitude = 0.;
1247 position.accuracy = 0.; 1279 position.accuracy = 0.;
1248 position.timestamp = base::Time::Now(); 1280 position.timestamp = base::Time::Now();
1281 scoped_refptr<MessageLoopRunner> runner =
1282 new MessageLoopRunner;
1249 content::OverrideLocationForTesting(position, 1283 content::OverrideLocationForTesting(position,
1250 base::Bind(MessageLoop::QuitClosure())); 1284 runner->QuitNowClosure());
1251 RunMessageLoop(); 1285 runner->Run();
1252 } 1286 }
1253 1287
1254 namespace internal { 1288 namespace internal {
1255 1289
1256 void ClickTask(ui_controls::MouseButton button, 1290 void ClickTask(ui_controls::MouseButton button,
1257 int state, 1291 int state,
1258 const base::Closure& followup) { 1292 const base::Closure& followup) {
1259 if (!followup.is_null()) 1293 if (!followup.is_null())
1260 ui_controls::SendMouseEventsNotifyWhenDone(button, state, followup); 1294 ui_controls::SendMouseEventsNotifyWhenDone(button, state, followup);
1261 else 1295 else
1262 ui_controls::SendMouseEvents(button, state); 1296 ui_controls::SendMouseEvents(button, state);
1263 } 1297 }
1264 1298
1265 } // namespace internal 1299 } // namespace internal
1266 } // namespace ui_test_utils 1300 } // namespace ui_test_utils
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698