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

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: Removed WaitForWindowClosed(). 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(
Paweł Hajdan Jr. 2011/01/06 20:45:48 nit: This forward-declaration is really weird and
ahendrickson 2011/01/06 22:33:39 IIRC, it's required for GCC compilation.
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 361 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 void WaitForLoadStop(NavigationController* controller) { 433 void WaitForLoadStop(NavigationController* controller) {
423 TestNotificationObserver observer; 434 TestNotificationObserver observer;
424 RegisterAndWait(&observer, NotificationType::LOAD_STOP, 435 RegisterAndWait(&observer, NotificationType::LOAD_STOP,
425 Source<NavigationController>(controller)); 436 Source<NavigationController>(controller));
426 } 437 }
427 438
428 Browser* WaitForNewBrowser() { 439 Browser* WaitForNewBrowser() {
429 TestNotificationObserver observer; 440 TestNotificationObserver observer;
430 RegisterAndWait(&observer, NotificationType::BROWSER_WINDOW_READY, 441 RegisterAndWait(&observer, NotificationType::BROWSER_WINDOW_READY,
431 NotificationService::AllSources()); 442 NotificationService::AllSources());
432 return Source<Browser>(observer.source()).ptr(); 443 Browser* new_browser = Source<Browser>(observer.source()).ptr();
Paweł Hajdan Jr. 2011/01/06 20:45:48 nit: Why not just return Source ... ?
ahendrickson 2011/01/06 22:33:39 Done.
444 return new_browser;
445 }
446
447 Browser* WaitForBrowserNotInSet(std::set<Browser*> excluded_browsers) {
448 TestNotificationObserver observer;
449 Browser* new_browser = GetBrowserNotInSet(excluded_browsers);
450 if (new_browser == NULL) {
Paweł Hajdan Jr. 2011/01/06 20:45:48 nit: if (!new_browser), no braces {}
ahendrickson 2011/01/06 22:33:39 Done.
451 new_browser = WaitForNewBrowser();
Paweł Hajdan Jr. 2011/01/06 20:45:48 Should we somehow check it's not in |excluded_brow
ahendrickson 2011/01/06 22:33:39 For what I'm doing, it's not an issue. My underst
Randy Smith (Not in Mondays) 2011/01/06 23:27:35 Yeah, but if it's in ui_test_utils.cc, it should b
452 }
453 return new_browser;
433 } 454 }
434 455
435 void OpenURLOffTheRecord(Profile* profile, const GURL& url) { 456 void OpenURLOffTheRecord(Profile* profile, const GURL& url) {
436 Browser::OpenURLOffTheRecord(profile, url); 457 Browser::OpenURLOffTheRecord(profile, url);
437 Browser* browser = BrowserList::FindBrowserWithType( 458 Browser* browser = BrowserList::FindBrowserWithType(
438 profile->GetOffTheRecordProfile(), Browser::TYPE_NORMAL, false); 459 profile->GetOffTheRecordProfile(), Browser::TYPE_NORMAL, false);
439 WaitForNavigations(&browser->GetSelectedTabContents()->controller(), 1); 460 WaitForNavigations(&browser->GetSelectedTabContents()->controller(), 1);
440 } 461 }
441 462
442 void NavigateToURL(Browser* browser, const GURL& url) { 463 void NavigateToURL(Browser* browser, const GURL& url) {
443 NavigateToURLBlockUntilNavigationsComplete(browser, url, 1); 464 NavigateToURLWithDisposition(browser, url, CURRENT_TAB,
465 BROWSER_TEST_WAIT_FOR_NAVIGATION);
466 }
467
468 void NavigateToURLWithDisposition(Browser* browser,
469 const GURL& url,
470 WindowOpenDisposition disposition,
471 int browser_test_flags) {
472 NavigateToURLWithDispositionBlockUntilNavigationsComplete(
473 browser,
474 url,
475 1,
476 disposition,
477 browser_test_flags);
444 } 478 }
445 479
446 void NavigateToURLBlockUntilNavigationsComplete(Browser* browser, 480 void NavigateToURLBlockUntilNavigationsComplete(Browser* browser,
447 const GURL& url, 481 const GURL& url,
448 int number_of_navigations) { 482 int number_of_navigations) {
449 NavigationController* controller = 483 NavigateToURLWithDispositionBlockUntilNavigationsComplete(
450 &browser->GetSelectedTabContents()->controller(); 484 browser,
451 browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED); 485 url,
452 WaitForNavigations(controller, number_of_navigations); 486 number_of_navigations,
487 CURRENT_TAB,
488 BROWSER_TEST_WAIT_FOR_NAVIGATION);
489 }
490
491 void NavigateToURLWithDispositionBlockUntilNavigationsComplete(
492 Browser* browser,
493 const GURL& url,
494 int number_of_navigations,
495 WindowOpenDisposition disposition,
496 int browser_test_flags) {
497 std::set<Browser*> initial_browsers;
498 for (std::vector<Browser*>::const_iterator iter = BrowserList::begin();
499 iter != BrowserList::end();
500 ++iter) {
501 initial_browsers.insert(*iter);
502 }
503 browser->OpenURL(url, GURL(), disposition, PageTransition::TYPED);
504 if (browser_test_flags & BROWSER_TEST_WAIT_FOR_BROWSER)
505 browser = WaitForBrowserNotInSet(initial_browsers);
506 if (browser_test_flags & BROWSER_TEST_WAIT_FOR_TAB)
507 WaitForNotification(NotificationType::TAB_ADDED);
508 if (!(browser_test_flags & BROWSER_TEST_WAIT_FOR_NAVIGATION)) {
509 // Some other flag caused the wait prior to this.
510 return;
511 }
512 TabContents* tab_contents = NULL;
513 if (disposition == NEW_BACKGROUND_TAB) {
514 // We've opened up a new tab, but not selected it.
515 tab_contents = browser->GetTabContentsAt(browser->selected_index() + 1);
516 EXPECT_TRUE(tab_contents != NULL)
517 << " Unable to wait for navigation to \"" << url.spec()
518 << "\" because the new tab is not available yet";
519 return;
520 } else if ((disposition == CURRENT_TAB) ||
521 (disposition == NEW_FOREGROUND_TAB) ||
522 (disposition == SINGLETON_TAB)) {
523 // The currently selected tab is the right one.
524 tab_contents = browser->GetSelectedTabContents();
525 }
526 if (tab_contents) {
527 NavigationController* controller = &tab_contents->controller();
528 WaitForNavigations(controller, number_of_navigations);
529 return;
530 }
531 EXPECT_TRUE(NULL != tab_contents) << " Unable to wait for navigation to \""
532 << url.spec() << "\""
533 << " because we can't get the tab contents";
453 } 534 }
454 535
455 DOMElementProxyRef GetActiveDOMDocument(Browser* browser) { 536 DOMElementProxyRef GetActiveDOMDocument(Browser* browser) {
456 JavaScriptExecutionController* executor = 537 JavaScriptExecutionController* executor =
457 new InProcessJavaScriptExecutionController( 538 new InProcessJavaScriptExecutionController(
458 browser->GetSelectedTabContents()->render_view_host()); 539 browser->GetSelectedTabContents()->render_view_host());
459 int element_handle; 540 int element_handle;
460 executor->ExecuteJavaScriptAndGetReturn("document;", &element_handle); 541 executor->ExecuteJavaScriptAndGetReturn("document;", &element_handle);
461 return executor->GetObjectProxy<DOMElementProxy>(element_handle); 542 return executor->GetObjectProxy<DOMElementProxy>(element_handle);
462 } 543 }
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 if (ordinal) 637 if (ordinal)
557 *ordinal = observer.active_match_ordinal(); 638 *ordinal = observer.active_match_ordinal();
558 return observer.number_of_matches(); 639 return observer.number_of_matches();
559 } 640 }
560 641
561 void WaitForNotification(NotificationType type) { 642 void WaitForNotification(NotificationType type) {
562 TestNotificationObserver observer; 643 TestNotificationObserver observer;
563 RegisterAndWait(&observer, type, NotificationService::AllSources()); 644 RegisterAndWait(&observer, type, NotificationService::AllSources());
564 } 645 }
565 646
647 void WaitForNotificationFrom(NotificationType type,
648 const NotificationSource& source) {
649 TestNotificationObserver observer;
650 RegisterAndWait(&observer, type, source);
651 }
652
566 void RegisterAndWait(NotificationObserver* observer, 653 void RegisterAndWait(NotificationObserver* observer,
567 NotificationType type, 654 NotificationType type,
568 const NotificationSource& source) { 655 const NotificationSource& source) {
569 NotificationRegistrar registrar; 656 NotificationRegistrar registrar;
570 registrar.Add(observer, type, source); 657 registrar.Add(observer, type, source);
571 RunMessageLoop(); 658 RunMessageLoop();
572 } 659 }
573 660
574 void WaitForBookmarkModelToLoad(BookmarkModel* model) { 661 void WaitForBookmarkModelToLoad(BookmarkModel* model) {
575 if (model->IsLoaded()) 662 if (model->IsLoaded())
(...skipping 23 matching lines...) Expand all
599 686
600 bool BringBrowserWindowToFront(const Browser* browser) { 687 bool BringBrowserWindowToFront(const Browser* browser) {
601 gfx::NativeWindow window = NULL; 688 gfx::NativeWindow window = NULL;
602 if (!GetNativeWindow(browser, &window)) 689 if (!GetNativeWindow(browser, &window))
603 return false; 690 return false;
604 691
605 ui_test_utils::ShowAndFocusNativeWindow(window); 692 ui_test_utils::ShowAndFocusNativeWindow(window);
606 return true; 693 return true;
607 } 694 }
608 695
696 Browser* GetBrowserNotInSet(std::set<Browser*> excluded_browsers) {
697 for (BrowserList::const_iterator iter = BrowserList::begin();
698 iter != BrowserList::end();
699 ++iter) {
700 if (excluded_browsers.find(*iter) == excluded_browsers.end())
701 return *iter;
702 }
703
704 return NULL;
705 }
706
609 bool SendKeyPressSync(const Browser* browser, 707 bool SendKeyPressSync(const Browser* browser,
610 app::KeyboardCode key, 708 app::KeyboardCode key,
611 bool control, 709 bool control,
612 bool shift, 710 bool shift,
613 bool alt, 711 bool alt,
614 bool command) { 712 bool command) {
615 base::TimeTicks start_time = base::TimeTicks::Now(); 713 base::TimeTicks start_time = base::TimeTicks::Now();
616 714
617 gfx::NativeWindow window = NULL; 715 gfx::NativeWindow window = NULL;
618 if (!GetNativeWindow(browser, &window)) 716 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); 1010 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap);
913 } 1011 }
914 1012
915 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) { 1013 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) {
916 DCHECK(bitmap); 1014 DCHECK(bitmap);
917 SnapshotTaker taker; 1015 SnapshotTaker taker;
918 return taker.TakeEntirePageSnapshot(rvh, bitmap); 1016 return taker.TakeEntirePageSnapshot(rvh, bitmap);
919 } 1017 }
920 1018
921 } // namespace ui_test_utils 1019 } // namespace ui_test_utils
OLDNEW
« chrome/browser/download/download_browsertest.cc ('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