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

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

Powered by Google App Engine
This is Rietveld 408576698