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

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

Issue 5610006: Converted download UI tests to Browser tests. (Closed) Base URL: http://git.chromium.org/git/chromium.git@browser_tests
Patch Set: Changes per Pawel & Brett's comments. Created 9 years, 11 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) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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/ui_test_utils.h" 5 #include "chrome/test/ui_test_utils.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 30 matching lines...) Expand all
41 #endif 41 #endif
42 #include "gfx/size.h" 42 #include "gfx/size.h"
43 #include "googleurl/src/gurl.h" 43 #include "googleurl/src/gurl.h"
44 #include "net/base/net_util.h" 44 #include "net/base/net_util.h"
45 #include "testing/gtest/include/gtest/gtest.h" 45 #include "testing/gtest/include/gtest/gtest.h"
46 #include "third_party/skia/include/core/SkBitmap.h" 46 #include "third_party/skia/include/core/SkBitmap.h"
47 #include "third_party/skia/include/core/SkColor.h" 47 #include "third_party/skia/include/core/SkColor.h"
48 48
49 namespace ui_test_utils { 49 namespace ui_test_utils {
50 50
51 // Navigates the specified tab (via |disposition|) of |browser| to |url|,
52 // blocking until the |number_of_navigations| specified complete.
53 // |disposition| indicates what tab the download occurs in, and
54 // |browser_test_flags| controls what to wait for before continuing.
55 void NavigateToURLWithDispositionBlockUntilNavigationsComplete(
56 Browser* browser,
57 const GURL& url,
58 int number_of_navigations,
59 WindowOpenDisposition disposition,
60 int browser_test_flags);
61
51 namespace { 62 namespace {
52 63
53 // Used to block until a navigation completes. 64 // Used to block until a navigation completes.
54 class NavigationNotificationObserver : public NotificationObserver { 65 class NavigationNotificationObserver : public NotificationObserver {
55 public: 66 public:
56 NavigationNotificationObserver(NavigationController* controller, 67 NavigationNotificationObserver(NavigationController* controller,
57 int number_of_navigations) 68 int number_of_navigations)
58 : navigation_started_(false), 69 : navigation_started_(false),
59 navigations_completed_(0), 70 navigations_completed_(0),
60 number_of_navigations_(number_of_navigations) { 71 number_of_navigations_(number_of_navigations) {
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after
400 411
401 void WaitForNavigation(NavigationController* controller) { 412 void WaitForNavigation(NavigationController* controller) {
402 WaitForNavigations(controller, 1); 413 WaitForNavigations(controller, 1);
403 } 414 }
404 415
405 void WaitForNavigations(NavigationController* controller, 416 void WaitForNavigations(NavigationController* controller,
406 int number_of_navigations) { 417 int number_of_navigations) {
407 NavigationNotificationObserver observer(controller, number_of_navigations); 418 NavigationNotificationObserver observer(controller, number_of_navigations);
408 } 419 }
409 420
421 void WaitForWindowClosed(Browser* browser) {
422 TestNotificationObserver observer;
423 RegisterAndWait(&observer,
424 NotificationType::BROWSER_CLOSED,
Paweł Hajdan Jr. 2011/01/05 08:05:52 Please see my earlier comment about this.
ahendrickson 2011/01/06 16:47:29 Done.
425 Source<Browser>(browser));
426 }
427
410 void WaitForNewTab(Browser* browser) { 428 void WaitForNewTab(Browser* browser) {
411 TestNotificationObserver observer; 429 TestNotificationObserver observer;
412 RegisterAndWait(&observer, NotificationType::TAB_ADDED, 430 RegisterAndWait(&observer, NotificationType::TAB_ADDED,
413 Source<Browser>(browser)); 431 Source<Browser>(browser));
414 } 432 }
415 433
416 void WaitForBrowserActionUpdated(ExtensionAction* browser_action) { 434 void WaitForBrowserActionUpdated(ExtensionAction* browser_action) {
417 TestNotificationObserver observer; 435 TestNotificationObserver observer;
418 RegisterAndWait(&observer, NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, 436 RegisterAndWait(&observer, NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
419 Source<ExtensionAction>(browser_action)); 437 Source<ExtensionAction>(browser_action));
420 } 438 }
421 439
422 void WaitForLoadStop(NavigationController* controller) { 440 void WaitForLoadStop(NavigationController* controller) {
423 TestNotificationObserver observer; 441 TestNotificationObserver observer;
424 RegisterAndWait(&observer, NotificationType::LOAD_STOP, 442 RegisterAndWait(&observer, NotificationType::LOAD_STOP,
425 Source<NavigationController>(controller)); 443 Source<NavigationController>(controller));
426 } 444 }
427 445
428 Browser* WaitForNewBrowser() { 446 Browser* WaitForNewBrowser() {
429 TestNotificationObserver observer; 447 TestNotificationObserver observer;
430 RegisterAndWait(&observer, NotificationType::BROWSER_WINDOW_READY, 448 RegisterAndWait(&observer, NotificationType::BROWSER_WINDOW_READY,
431 NotificationService::AllSources()); 449 NotificationService::AllSources());
432 return Source<Browser>(observer.source()).ptr(); 450 Browser* new_browser = Source<Browser>(observer.source()).ptr();
451 return new_browser;
452 }
453
454 Browser* WaitForBrowserNotInSet(std::set<Browser*> excluded_browsers) {
455 TestNotificationObserver observer;
456 Browser* new_browser = GetBrowserNotInSet(excluded_browsers);
457 if (new_browser == NULL) {
458 new_browser = WaitForNewBrowser();
459 }
460 return new_browser;
433 } 461 }
434 462
435 void OpenURLOffTheRecord(Profile* profile, const GURL& url) { 463 void OpenURLOffTheRecord(Profile* profile, const GURL& url) {
436 Browser::OpenURLOffTheRecord(profile, url); 464 Browser::OpenURLOffTheRecord(profile, url);
437 Browser* browser = BrowserList::FindBrowserWithType( 465 Browser* browser = BrowserList::FindBrowserWithType(
438 profile->GetOffTheRecordProfile(), Browser::TYPE_NORMAL, false); 466 profile->GetOffTheRecordProfile(), Browser::TYPE_NORMAL, false);
439 WaitForNavigations(&browser->GetSelectedTabContents()->controller(), 1); 467 WaitForNavigations(&browser->GetSelectedTabContents()->controller(), 1);
440 } 468 }
441 469
442 void NavigateToURL(Browser* browser, const GURL& url) { 470 void NavigateToURL(Browser* browser, const GURL& url) {
443 NavigateToURLBlockUntilNavigationsComplete(browser, url, 1); 471 NavigateToURLWithDisposition(browser, url, CURRENT_TAB,
472 BROWSER_TEST_WAIT_FOR_NAVIGATION);
473 }
474
475 void NavigateToURLWithDisposition(Browser* browser,
476 const GURL& url,
477 WindowOpenDisposition disposition,
478 int browser_test_flags) {
479 NavigateToURLWithDispositionBlockUntilNavigationsComplete(
480 browser,
481 url,
482 1,
483 disposition,
484 browser_test_flags);
444 } 485 }
445 486
446 void NavigateToURLBlockUntilNavigationsComplete(Browser* browser, 487 void NavigateToURLBlockUntilNavigationsComplete(Browser* browser,
447 const GURL& url, 488 const GURL& url,
448 int number_of_navigations) { 489 int number_of_navigations) {
449 NavigationController* controller = 490 NavigateToURLWithDispositionBlockUntilNavigationsComplete(
450 &browser->GetSelectedTabContents()->controller(); 491 browser,
451 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED); 492 url,
452 WaitForNavigations(controller, number_of_navigations); 493 number_of_navigations,
494 CURRENT_TAB,
495 BROWSER_TEST_WAIT_FOR_NAVIGATION);
496 }
497
498 void NavigateToURLWithDispositionBlockUntilNavigationsComplete(
499 Browser* browser,
500 const GURL& url,
501 int number_of_navigations,
502 WindowOpenDisposition disposition,
503 int browser_test_flags) {
504 std::set<Browser*> initial_browsers;
505 for (std::vector<Browser*>::const_iterator iter = BrowserList::begin();
506 iter != BrowserList::end();
507 ++iter) {
508 initial_browsers.insert(*iter);
509 }
510 browser->OpenURL(url, GURL(), disposition, PageTransition::TYPED);
511 if (browser_test_flags & BROWSER_TEST_WAIT_FOR_BROWSER)
512 browser = WaitForBrowserNotInSet(initial_browsers);
513 if (browser_test_flags & BROWSER_TEST_WAIT_FOR_TAB)
514 WaitForNotification(NotificationType::TAB_ADDED);
515 if (!(browser_test_flags & BROWSER_TEST_WAIT_FOR_NAVIGATION)) {
516 // Some other flag caused the wait prior to this.
517 return;
518 }
519 TabContents* tab_contents = NULL;
520 if (disposition == NEW_BACKGROUND_TAB) {
521 // We've opened up a new tab, but not selected it.
522 tab_contents = browser->GetTabContentsAt(browser->selected_index() + 1);
523 EXPECT_TRUE(tab_contents != NULL)
524 << " Unable to wait for navigation to \"" << url.spec()
525 << "\" because the new tab is not available yet";
526 return;
527 } else if ((disposition == CURRENT_TAB) ||
528 (disposition == NEW_FOREGROUND_TAB) ||
529 (disposition == SINGLETON_TAB)) {
530 // The currently selected tab is the right one.
531 tab_contents = browser->GetSelectedTabContents();
532 }
533 if (tab_contents) {
534 NavigationController* controller = &tab_contents->controller();
535 WaitForNavigations(controller, number_of_navigations);
536 return;
537 }
538 EXPECT_TRUE(NULL != tab_contents) << " Unable to wait for navigation to \""
539 << url.spec() << "\""
540 << " because we can't get the tab contents";
453 } 541 }
454 542
455 DOMElementProxyRef GetActiveDOMDocument(Browser* browser) { 543 DOMElementProxyRef GetActiveDOMDocument(Browser* browser) {
456 JavaScriptExecutionController* executor = 544 JavaScriptExecutionController* executor =
457 new InProcessJavaScriptExecutionController( 545 new InProcessJavaScriptExecutionController(
458 browser->GetSelectedTabContents()->render_view_host()); 546 browser->GetSelectedTabContents()->render_view_host());
459 int element_handle; 547 int element_handle;
460 executor->ExecuteJavaScriptAndGetReturn("document;", &element_handle); 548 executor->ExecuteJavaScriptAndGetReturn("document;", &element_handle);
461 return executor->GetObjectProxy<DOMElementProxy>(element_handle); 549 return executor->GetObjectProxy<DOMElementProxy>(element_handle);
462 } 550 }
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 if (ordinal) 644 if (ordinal)
557 *ordinal = observer.active_match_ordinal(); 645 *ordinal = observer.active_match_ordinal();
558 return observer.number_of_matches(); 646 return observer.number_of_matches();
559 } 647 }
560 648
561 void WaitForNotification(NotificationType type) { 649 void WaitForNotification(NotificationType type) {
562 TestNotificationObserver observer; 650 TestNotificationObserver observer;
563 RegisterAndWait(&observer, type, NotificationService::AllSources()); 651 RegisterAndWait(&observer, type, NotificationService::AllSources());
564 } 652 }
565 653
654 void WaitForNotificationFrom(NotificationType type,
655 const NotificationSource& source) {
656 TestNotificationObserver observer;
657 RegisterAndWait(&observer, type, source);
658 }
659
566 void RegisterAndWait(NotificationObserver* observer, 660 void RegisterAndWait(NotificationObserver* observer,
567 NotificationType type, 661 NotificationType type,
568 const NotificationSource& source) { 662 const NotificationSource& source) {
569 NotificationRegistrar registrar; 663 NotificationRegistrar registrar;
570 registrar.Add(observer, type, source); 664 registrar.Add(observer, type, source);
571 RunMessageLoop(); 665 RunMessageLoop();
572 } 666 }
573 667
574 void WaitForBookmarkModelToLoad(BookmarkModel* model) { 668 void WaitForBookmarkModelToLoad(BookmarkModel* model) {
575 if (model->IsLoaded()) 669 if (model->IsLoaded())
(...skipping 23 matching lines...) Expand all
599 693
600 bool BringBrowserWindowToFront(const Browser* browser) { 694 bool BringBrowserWindowToFront(const Browser* browser) {
601 gfx::NativeWindow window = NULL; 695 gfx::NativeWindow window = NULL;
602 if (!GetNativeWindow(browser, &window)) 696 if (!GetNativeWindow(browser, &window))
603 return false; 697 return false;
604 698
605 ui_test_utils::ShowAndFocusNativeWindow(window); 699 ui_test_utils::ShowAndFocusNativeWindow(window);
606 return true; 700 return true;
607 } 701 }
608 702
703 Browser* GetBrowserNotInSet(std::set<Browser*> excluded_browsers) {
704 for (BrowserList::const_iterator iter = BrowserList::begin();
705 iter != BrowserList::end();
706 ++iter) {
707 if (excluded_browsers.find(*iter) == excluded_browsers.end())
708 return *iter;
709 }
710
711 return NULL;
712 }
713
609 bool SendKeyPressSync(const Browser* browser, 714 bool SendKeyPressSync(const Browser* browser,
610 app::KeyboardCode key, 715 app::KeyboardCode key,
611 bool control, 716 bool control,
612 bool shift, 717 bool shift,
613 bool alt, 718 bool alt,
614 bool command) { 719 bool command) {
615 base::TimeTicks start_time = base::TimeTicks::Now(); 720 base::TimeTicks start_time = base::TimeTicks::Now();
616 721
617 gfx::NativeWindow window = NULL; 722 gfx::NativeWindow window = NULL;
618 if (!GetNativeWindow(browser, &window)) 723 if (!GetNativeWindow(browser, &window))
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after
912 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap); 1017 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap);
913 } 1018 }
914 1019
915 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) { 1020 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) {
916 DCHECK(bitmap); 1021 DCHECK(bitmap);
917 SnapshotTaker taker; 1022 SnapshotTaker taker;
918 return taker.TakeEntirePageSnapshot(rvh, bitmap); 1023 return taker.TakeEntirePageSnapshot(rvh, bitmap);
919 } 1024 }
920 1025
921 } // namespace ui_test_utils 1026 } // namespace ui_test_utils
OLDNEW
« chrome/test/in_process_browser_test.h ('K') | « chrome/test/ui_test_utils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698