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

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

Issue 10803026: Get content_browsertest working. I've added a simple test for now, and will convert the tests in sr… (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: get it working on mac + linux, fix unittests crash, and fix unittest link error and add missing dep… 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/gpu/test_support_gpu.gypi » ('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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 #include "content/public/browser/geolocation.h" 56 #include "content/public/browser/geolocation.h"
57 #include "content/public/browser/navigation_controller.h" 57 #include "content/public/browser/navigation_controller.h"
58 #include "content/public/browser/navigation_entry.h" 58 #include "content/public/browser/navigation_entry.h"
59 #include "content/public/browser/notification_service.h" 59 #include "content/public/browser/notification_service.h"
60 #include "content/public/browser/render_process_host.h" 60 #include "content/public/browser/render_process_host.h"
61 #include "content/public/browser/render_view_host.h" 61 #include "content/public/browser/render_view_host.h"
62 #include "content/public/browser/web_contents.h" 62 #include "content/public/browser/web_contents.h"
63 #include "content/public/browser/web_contents_observer.h" 63 #include "content/public/browser/web_contents_observer.h"
64 #include "content/public/browser/web_contents_view.h" 64 #include "content/public/browser/web_contents_view.h"
65 #include "content/public/common/geoposition.h" 65 #include "content/public/common/geoposition.h"
66 #include "content/public/test/browser_test_utils.h"
66 #include "content/public/test/test_navigation_observer.h" 67 #include "content/public/test/test_navigation_observer.h"
67 #include "googleurl/src/gurl.h" 68 #include "googleurl/src/gurl.h"
68 #include "net/base/net_util.h" 69 #include "net/base/net_util.h"
69 #include "net/test/python_utils.h" 70 #include "net/test/python_utils.h"
70 #include "testing/gtest/include/gtest/gtest.h" 71 #include "testing/gtest/include/gtest/gtest.h"
71 #include "third_party/skia/include/core/SkBitmap.h" 72 #include "third_party/skia/include/core/SkBitmap.h"
72 #include "third_party/skia/include/core/SkColor.h" 73 #include "third_party/skia/include/core/SkColor.h"
73 #include "ui/gfx/size.h" 74 #include "ui/gfx/size.h"
74 #include "ui/ui_controls/ui_controls.h" 75 #include "ui/ui_controls/ui_controls.h"
75 76
76 #if defined(TOOLKIT_VIEWS)
77 #include "ui/views/focus/accelerator_handler.h"
78 #endif
79
80 #if defined(USE_AURA) 77 #if defined(USE_AURA)
81 #include "ash/shell.h" 78 #include "ash/shell.h"
82 #include "ui/aura/root_window.h" 79 #include "ui/aura/root_window.h"
83 #endif 80 #endif
84 81
85 using content::DomOperationNotificationDetails; 82 using content::DomOperationNotificationDetails;
86 using content::NativeWebKeyboardEvent; 83 using content::NativeWebKeyboardEvent;
87 using content::NavigationController; 84 using content::NavigationController;
88 using content::NavigationEntry; 85 using content::NavigationEntry;
89 using content::OpenURLParams; 86 using content::OpenURLParams;
90 using content::RenderViewHost; 87 using content::RenderViewHost;
91 using content::RenderWidgetHost; 88 using content::RenderWidgetHost;
92 using content::Referrer; 89 using content::Referrer;
93 using content::WebContents; 90 using content::WebContents;
94 91
95 static const int kDefaultWsPort = 8880; 92 static const int kDefaultWsPort = 8880;
96 93
97 // Number of times to repost a Quit task so that the MessageLoop finishes up
98 // pending tasks and tasks posted by those pending tasks without risking the
99 // potential hang behavior of MessageLoop::QuitWhenIdle.
100 // The criteria for choosing this number: it should be high enough to make the
101 // quit act like QuitWhenIdle, while taking into account that any page which is
102 // animating may be rendering another frame for each quit deferral. For an
103 // animating page, the potential delay to quitting the RunLoop would be
104 // kNumQuitDeferrals * frame_render_time. Some perf tests run slow, such as
105 // 200ms/frame.
106 static const int kNumQuitDeferrals = 10;
107
108 namespace ui_test_utils { 94 namespace ui_test_utils {
109 95
110 namespace { 96 namespace {
111 97
112 class DOMOperationObserver : public content::NotificationObserver, 98 class DOMOperationObserver : public content::NotificationObserver,
113 public content::WebContentsObserver { 99 public content::WebContentsObserver {
114 public: 100 public:
115 explicit DOMOperationObserver(RenderViewHost* render_view_host) 101 explicit DOMOperationObserver(RenderViewHost* render_view_host)
116 : content::WebContentsObserver( 102 : content::WebContentsObserver(
117 WebContents::FromRenderViewHost(render_view_host)), 103 WebContents::FromRenderViewHost(render_view_host)),
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 MessageLoop::current()->PostTask(FROM_HERE, 270 MessageLoop::current()->PostTask(FROM_HERE,
285 MessageLoop::QuitWhenIdleClosure()); 271 MessageLoop::QuitWhenIdleClosure());
286 RunMessageLoop(); 272 RunMessageLoop();
287 content::BrowserThread::PostTask(thread_id, FROM_HERE, quit_task); 273 content::BrowserThread::PostTask(thread_id, FROM_HERE, quit_task);
288 } 274 }
289 275
290 } // namespace 276 } // namespace
291 277
292 void RunMessageLoop() { 278 void RunMessageLoop() {
293 base::RunLoop run_loop; 279 base::RunLoop run_loop;
294 RunThisRunLoop(&run_loop); 280 content::RunThisRunLoop(&run_loop);
295 }
296
297 void RunThisRunLoop(base::RunLoop* run_loop) {
298 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current());
299 #if !defined(USE_AURA) && defined(TOOLKIT_VIEWS)
300 scoped_ptr<views::AcceleratorHandler> handler;
301 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
302 handler.reset(new views::AcceleratorHandler);
303 run_loop->set_dispatcher(handler.get());
304 }
305 #endif
306 run_loop->Run();
307 }
308
309 // TODO(jbates) move this to a new test_utils.cc in content/test/
310 static void DeferredQuitRunLoop(const base::Closure& quit_task,
311 int num_quit_deferrals) {
312 if (num_quit_deferrals <= 0) {
313 quit_task.Run();
314 } else {
315 MessageLoop::current()->PostTask(FROM_HERE,
316 base::Bind(&DeferredQuitRunLoop, quit_task, num_quit_deferrals - 1));
317 }
318 }
319
320 base::Closure GetQuitTaskForRunLoop(base::RunLoop* run_loop) {
321 return base::Bind(&DeferredQuitRunLoop, run_loop->QuitClosure(),
322 kNumQuitDeferrals);
323 } 281 }
324 282
325 void RunAllPendingInMessageLoop() { 283 void RunAllPendingInMessageLoop() {
326 MessageLoop::current()->PostTask(FROM_HERE, 284 MessageLoop::current()->PostTask(FROM_HERE,
327 MessageLoop::QuitWhenIdleClosure()); 285 MessageLoop::QuitWhenIdleClosure());
328 ui_test_utils::RunMessageLoop(); 286 ui_test_utils::RunMessageLoop();
329 } 287 }
330 288
331 void RunAllPendingInMessageLoop(content::BrowserThread::ID thread_id) { 289 void RunAllPendingInMessageLoop(content::BrowserThread::ID thread_id) {
332 if (content::BrowserThread::CurrentlyOn(thread_id)) { 290 if (content::BrowserThread::CurrentlyOn(thread_id)) {
333 RunAllPendingInMessageLoop(); 291 RunAllPendingInMessageLoop();
334 return; 292 return;
335 } 293 }
336 content::BrowserThread::ID current_thread_id; 294 content::BrowserThread::ID current_thread_id;
337 if (!content::BrowserThread::GetCurrentThreadIdentifier(&current_thread_id)) { 295 if (!content::BrowserThread::GetCurrentThreadIdentifier(&current_thread_id)) {
338 NOTREACHED(); 296 NOTREACHED();
339 return; 297 return;
340 } 298 }
341 299
342 base::RunLoop run_loop; 300 base::RunLoop run_loop;
343 content::BrowserThread::PostTask(thread_id, FROM_HERE, 301 content::BrowserThread::PostTask(thread_id, FROM_HERE,
344 base::Bind(&RunAllPendingMessageAndSendQuit, current_thread_id, 302 base::Bind(&RunAllPendingMessageAndSendQuit, current_thread_id,
345 run_loop.QuitClosure())); 303 run_loop.QuitClosure()));
346 ui_test_utils::RunThisRunLoop(&run_loop); 304 content::RunThisRunLoop(&run_loop);
347 } 305 }
348 306
349 bool GetCurrentTabTitle(const Browser* browser, string16* title) { 307 bool GetCurrentTabTitle(const Browser* browser, string16* title) {
350 WebContents* web_contents = chrome::GetActiveWebContents(browser); 308 WebContents* web_contents = chrome::GetActiveWebContents(browser);
351 if (!web_contents) 309 if (!web_contents)
352 return false; 310 return false;
353 NavigationEntry* last_entry = web_contents->GetController().GetActiveEntry(); 311 NavigationEntry* last_entry = web_contents->GetController().GetActiveEntry();
354 if (!last_entry) 312 if (!last_entry)
355 return false; 313 return false;
356 title->assign(last_entry->GetTitleForDisplay("")); 314 title->assign(last_entry->GetTitleForDisplay(""));
357 return true; 315 return true;
358 } 316 }
359 317
360 void WaitForNavigations(NavigationController* controller, 318 void WaitForNavigations(NavigationController* controller,
361 int number_of_navigations) { 319 int number_of_navigations) {
362 content::TestNavigationObserver observer( 320 content::TestNavigationObserver observer(
363 content::Source<NavigationController>(controller), NULL, 321 content::Source<NavigationController>(controller), NULL,
364 number_of_navigations); 322 number_of_navigations);
365 base::RunLoop run_loop; 323 base::RunLoop run_loop;
366 observer.WaitForObservation( 324 observer.WaitForObservation(
367 base::Bind(&ui_test_utils::RunThisRunLoop, base::Unretained(&run_loop)), 325 base::Bind(&content::RunThisRunLoop, base::Unretained(&run_loop)),
368 ui_test_utils::GetQuitTaskForRunLoop(&run_loop)); 326 content::GetQuitTaskForRunLoop(&run_loop));
369 } 327 }
370 328
371 void WaitForNewTab(Browser* browser) { 329 void WaitForNewTab(Browser* browser) {
372 WindowedNotificationObserver observer( 330 WindowedNotificationObserver observer(
373 chrome::NOTIFICATION_TAB_ADDED, 331 chrome::NOTIFICATION_TAB_ADDED,
374 content::Source<content::WebContentsDelegate>(browser)); 332 content::Source<content::WebContentsDelegate>(browser));
375 observer.Wait(); 333 observer.Wait();
376 } 334 }
377 335
378 void WaitForLoadStop(WebContents* tab) { 336 void WaitForLoadStop(WebContents* tab) {
(...skipping 26 matching lines...) Expand all
405 1); 363 1);
406 return browser; 364 return browser;
407 } 365 }
408 366
409 void NavigateToURL(chrome::NavigateParams* params) { 367 void NavigateToURL(chrome::NavigateParams* params) {
410 content::TestNavigationObserver observer( 368 content::TestNavigationObserver observer(
411 content::NotificationService::AllSources(), NULL, 1); 369 content::NotificationService::AllSources(), NULL, 1);
412 chrome::Navigate(params); 370 chrome::Navigate(params);
413 base::RunLoop run_loop; 371 base::RunLoop run_loop;
414 observer.WaitForObservation( 372 observer.WaitForObservation(
415 base::Bind(&ui_test_utils::RunThisRunLoop, base::Unretained(&run_loop)), 373 base::Bind(&content::RunThisRunLoop, base::Unretained(&run_loop)),
416 ui_test_utils::GetQuitTaskForRunLoop(&run_loop)); 374 content::GetQuitTaskForRunLoop(&run_loop));
417 } 375 }
418 376
419 void NavigateToURL(Browser* browser, const GURL& url) { 377 void NavigateToURL(Browser* browser, const GURL& url) {
420 NavigateToURLWithDisposition(browser, url, CURRENT_TAB, 378 NavigateToURLWithDisposition(browser, url, CURRENT_TAB,
421 BROWSER_TEST_WAIT_FOR_NAVIGATION); 379 BROWSER_TEST_WAIT_FOR_NAVIGATION);
422 } 380 }
423 381
424 // Navigates the specified tab (via |disposition|) of |browser| to |url|, 382 // Navigates the specified tab (via |disposition|) of |browser| to |url|,
425 // blocking until the |number_of_navigations| specified complete. 383 // blocking until the |number_of_navigations| specified complete.
426 // |disposition| indicates what tab the download occurs in, and 384 // |disposition| indicates what tab the download occurs in, and
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
479 return; 437 return;
480 } else if ((disposition == CURRENT_TAB) || 438 } else if ((disposition == CURRENT_TAB) ||
481 (disposition == NEW_FOREGROUND_TAB) || 439 (disposition == NEW_FOREGROUND_TAB) ||
482 (disposition == SINGLETON_TAB)) { 440 (disposition == SINGLETON_TAB)) {
483 // The currently selected tab is the right one. 441 // The currently selected tab is the right one.
484 web_contents = chrome::GetActiveWebContents(browser); 442 web_contents = chrome::GetActiveWebContents(browser);
485 } 443 }
486 if (disposition == CURRENT_TAB) { 444 if (disposition == CURRENT_TAB) {
487 base::RunLoop run_loop; 445 base::RunLoop run_loop;
488 same_tab_observer.WaitForObservation( 446 same_tab_observer.WaitForObservation(
489 base::Bind(&ui_test_utils::RunThisRunLoop, base::Unretained(&run_loop)), 447 base::Bind(&content::RunThisRunLoop, base::Unretained(&run_loop)),
490 ui_test_utils::GetQuitTaskForRunLoop(&run_loop)); 448 content::GetQuitTaskForRunLoop(&run_loop));
491 return; 449 return;
492 } else if (web_contents) { 450 } else if (web_contents) {
493 NavigationController* controller = &web_contents->GetController(); 451 NavigationController* controller = &web_contents->GetController();
494 WaitForNavigations(controller, number_of_navigations); 452 WaitForNavigations(controller, number_of_navigations);
495 return; 453 return;
496 } 454 }
497 EXPECT_TRUE(NULL != web_contents) << " Unable to wait for navigation to \"" 455 EXPECT_TRUE(NULL != web_contents) << " Unable to wait for navigation to \""
498 << url.spec() << "\"" 456 << url.spec() << "\""
499 << " because we can't get the tab contents"; 457 << " because we can't get the tab contents";
500 } 458 }
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
731 const content::NotificationSource& source) { 689 const content::NotificationSource& source) {
732 content::NotificationRegistrar registrar; 690 content::NotificationRegistrar registrar;
733 registrar.Add(observer, type, source); 691 registrar.Add(observer, type, source);
734 RunMessageLoop(); 692 RunMessageLoop();
735 } 693 }
736 694
737 void WaitForBookmarkModelToLoad(BookmarkModel* model) { 695 void WaitForBookmarkModelToLoad(BookmarkModel* model) {
738 if (model->IsLoaded()) 696 if (model->IsLoaded())
739 return; 697 return;
740 base::RunLoop run_loop; 698 base::RunLoop run_loop;
741 BookmarkLoadObserver observer(GetQuitTaskForRunLoop(&run_loop)); 699 BookmarkLoadObserver observer(content::GetQuitTaskForRunLoop(&run_loop));
742 model->AddObserver(&observer); 700 model->AddObserver(&observer);
743 RunThisRunLoop(&run_loop); 701 content::RunThisRunLoop(&run_loop);
744 model->RemoveObserver(&observer); 702 model->RemoveObserver(&observer);
745 ASSERT_TRUE(model->IsLoaded()); 703 ASSERT_TRUE(model->IsLoaded());
746 } 704 }
747 705
748 void WaitForTemplateURLServiceToLoad(TemplateURLService* service) { 706 void WaitForTemplateURLServiceToLoad(TemplateURLService* service) {
749 if (service->loaded()) 707 if (service->loaded())
750 return; 708 return;
751 service->Load(); 709 service->Load();
752 TemplateURLServiceTestUtil::BlockTillServiceProcessesRequests(); 710 TemplateURLServiceTestUtil::BlockTillServiceProcessesRequests();
753 ASSERT_TRUE(service->loaded()); 711 ASSERT_TRUE(service->loaded());
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
857 return !testing::Test::HasFatalFailure(); 815 return !testing::Test::HasFatalFailure();
858 } 816 }
859 817
860 MessageLoopRunner::MessageLoopRunner() { 818 MessageLoopRunner::MessageLoopRunner() {
861 } 819 }
862 820
863 MessageLoopRunner::~MessageLoopRunner() { 821 MessageLoopRunner::~MessageLoopRunner() {
864 } 822 }
865 823
866 void MessageLoopRunner::Run() { 824 void MessageLoopRunner::Run() {
867 ui_test_utils::RunThisRunLoop(&run_loop_); 825 content::RunThisRunLoop(&run_loop_);
868 } 826 }
869 827
870 base::Closure MessageLoopRunner::QuitClosure() { 828 base::Closure MessageLoopRunner::QuitClosure() {
871 return base::Bind(&MessageLoopRunner::Quit, this); 829 return base::Bind(&MessageLoopRunner::Quit, this);
872 } 830 }
873 831
874 void MessageLoopRunner::Quit() { 832 void MessageLoopRunner::Quit() {
875 ui_test_utils::GetQuitTaskForRunLoop(&run_loop_).Run(); 833 content::GetQuitTaskForRunLoop(&run_loop_).Run();
876 } 834 }
877 835
878 TestWebSocketServer::TestWebSocketServer() 836 TestWebSocketServer::TestWebSocketServer()
879 : started_(false), 837 : started_(false),
880 port_(kDefaultWsPort), 838 port_(kDefaultWsPort),
881 secure_(false) { 839 secure_(false) {
882 #if defined(OS_POSIX) 840 #if defined(OS_POSIX)
883 process_group_id_ = base::kNullProcessHandle; 841 process_group_id_ = base::kNullProcessHandle;
884 #endif 842 #endif
885 } 843 }
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after
1290 int state, 1248 int state,
1291 const base::Closure& followup) { 1249 const base::Closure& followup) {
1292 if (!followup.is_null()) 1250 if (!followup.is_null())
1293 ui_controls::SendMouseEventsNotifyWhenDone(button, state, followup); 1251 ui_controls::SendMouseEventsNotifyWhenDone(button, state, followup);
1294 else 1252 else
1295 ui_controls::SendMouseEvents(button, state); 1253 ui_controls::SendMouseEvents(button, state);
1296 } 1254 }
1297 1255
1298 } // namespace internal 1256 } // namespace internal
1299 } // namespace ui_test_utils 1257 } // namespace ui_test_utils
OLDNEW
« no previous file with comments | « chrome/test/base/ui_test_utils.h ('k') | chrome/test/gpu/test_support_gpu.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698