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

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

Powered by Google App Engine
This is Rietveld 408576698