diff options
author | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-08 06:41:43 +0000 |
---|---|---|
committer | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-04-08 06:41:43 +0000 |
commit | 93b86a850b7b1b3f46a7c4fc4521fefa8ca56e8d (patch) | |
tree | 379ae4e770b1a30e925852ee51e58250a4c7d3fd /chrome/test | |
parent | f039cd1a367ad89d07a2344b6a8e3f39cd5b0758 (diff) | |
download | chromium_src-93b86a850b7b1b3f46a7c4fc4521fefa8ca56e8d.zip chromium_src-93b86a850b7b1b3f46a7c4fc4521fefa8ca56e8d.tar.gz chromium_src-93b86a850b7b1b3f46a7c4fc4521fefa8ca56e8d.tar.bz2 |
[GTTF] Clean up browser tests:
- use more ui_test_utils functions to simplify the code
- expose more consistent and powerful utilities in ui_test_utils
- minor style improvements
- move some tests from DISABLED to FLAKY so we don't lose coverage
- remove redundant timeouts
- check more return values
TEST=browser_tests
BUG=none
Review URL: http://codereview.chromium.org/1571002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@43931 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/test')
-rw-r--r-- | chrome/test/ui_test_utils.cc | 116 | ||||
-rw-r--r-- | chrome/test/ui_test_utils.h | 40 |
2 files changed, 71 insertions, 85 deletions
diff --git a/chrome/test/ui_test_utils.cc b/chrome/test/ui_test_utils.cc index b334e05..612d921 100644 --- a/chrome/test/ui_test_utils.cc +++ b/chrome/test/ui_test_utils.cc @@ -215,62 +215,6 @@ class DownloadsCompleteObserver : public DownloadManager::Observer, DISALLOW_COPY_AND_ASSIGN(DownloadsCompleteObserver); }; -template <class T> -class SimpleNotificationObserver : public NotificationObserver { - public: - SimpleNotificationObserver(NotificationType notification_type, - T* source) { - registrar_.Add(this, notification_type, Source<T>(source)); - ui_test_utils::RunMessageLoop(); - } - - virtual void Observe(NotificationType type, - const NotificationSource& source, - const NotificationDetails& details) { - MessageLoopForUI::current()->Quit(); - } - - private: - NotificationRegistrar registrar_; - - DISALLOW_COPY_AND_ASSIGN(SimpleNotificationObserver); -}; - -// SimpleNotificationObserver that waits for a single notification. When the -// notification is observer the source (of type S) is recorded and the message -// loop stopped. Use |source()| to access the source after the constructor -// returns. -template <class S> -class SimpleNotificationObserverWithSource : public NotificationObserver { - public: - SimpleNotificationObserverWithSource(NotificationType notification_type, - const NotificationSource& source) - : source_(NULL) { - registrar_.Add(this, notification_type, source); - ui_test_utils::RunMessageLoop(); - } - - virtual void Observe(NotificationType type, - const NotificationSource& source, - const NotificationDetails& details) { - source_ = Source<S>(source).ptr(); - - // Remove observer now, so that if there any other notifications we don't - // clobber source_. - registrar_.RemoveAll(); - - MessageLoopForUI::current()->Quit(); - } - - S* source() const { return source_; } - - private: - S* source_; - NotificationRegistrar registrar_; - - DISALLOW_COPY_AND_ASSIGN(SimpleNotificationObserverWithSource); -}; - class LanguageDetectionNotificationObserver : public NotificationObserver { public: explicit LanguageDetectionNotificationObserver(TabContents* tab) { @@ -438,26 +382,28 @@ void WaitForNavigations(NavigationController* controller, } void WaitForNewTab(Browser* browser) { - SimpleNotificationObserver<Browser> - new_tab_observer(NotificationType::TAB_ADDED, browser); + TestNotificationObserver observer; + RegisterAndWait(&observer, NotificationType::TAB_ADDED, + Source<Browser>(browser)); } void WaitForBrowserActionUpdated(ExtensionAction* browser_action) { - SimpleNotificationObserver<ExtensionAction> - observer(NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, - browser_action); + TestNotificationObserver observer; + RegisterAndWait(&observer, NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, + Source<ExtensionAction>(browser_action)); } void WaitForLoadStop(NavigationController* controller) { - SimpleNotificationObserver<NavigationController> - new_tab_observer(NotificationType::LOAD_STOP, controller); + TestNotificationObserver observer; + RegisterAndWait(&observer, NotificationType::LOAD_STOP, + Source<NavigationController>(controller)); } Browser* WaitForNewBrowser() { - SimpleNotificationObserverWithSource<Browser> observer( - NotificationType::BROWSER_WINDOW_READY, - NotificationService::AllSources()); - return observer.source(); + TestNotificationObserver observer; + RegisterAndWait(&observer, NotificationType::BROWSER_WINDOW_READY, + NotificationService::AllSources()); + return Source<Browser>(observer.source()).ptr(); } void OpenURLOffTheRecord(Profile* profile, const GURL& url) { @@ -571,28 +517,30 @@ void WaitForDownloadCount(DownloadManager* download_manager, size_t count) { } AppModalDialog* WaitForAppModalDialog() { - SimpleNotificationObserverWithSource<AppModalDialog> observer( - NotificationType::APP_MODAL_DIALOG_SHOWN, - NotificationService::AllSources()); - return observer.source(); + TestNotificationObserver observer; + RegisterAndWait(&observer, NotificationType::APP_MODAL_DIALOG_SHOWN, + NotificationService::AllSources()); + return Source<AppModalDialog>(observer.source()).ptr(); } void CrashTab(TabContents* tab) { RenderProcessHost* rph = tab->render_view_host()->process(); base::KillProcess(rph->GetHandle(), 0, false); - SimpleNotificationObserver<RenderProcessHost> - crash_observer(NotificationType::RENDERER_PROCESS_CLOSED, rph); + TestNotificationObserver observer; + RegisterAndWait(&observer, NotificationType::RENDERER_PROCESS_CLOSED, + Source<RenderProcessHost>(rph)); } void WaitForFocusChange(RenderViewHost* rvh) { - SimpleNotificationObserver<RenderViewHost> - focus_observer(NotificationType::FOCUS_CHANGED_IN_PAGE, rvh); + TestNotificationObserver observer; + RegisterAndWait(&observer, NotificationType::FOCUS_CHANGED_IN_PAGE, + Source<RenderViewHost>(rvh)); } void WaitForFocusInBrowser(Browser* browser) { - SimpleNotificationObserver<Browser> - focus_observer(NotificationType::FOCUS_RETURNED_TO_BROWSER, - browser); + TestNotificationObserver observer; + RegisterAndWait(&observer, NotificationType::FOCUS_RETURNED_TO_BROWSER, + Source<Browser>(browser)); } std::string WaitForLanguageDetection(TabContents* tab) { @@ -609,10 +557,16 @@ int FindInPage(TabContents* tab_contents, const string16& search_string, return observer.number_of_matches(); } -void RegisterAndWait(NotificationType::Type type, - NotificationObserver* observer) { +void WaitForNotification(NotificationType::Type type) { + TestNotificationObserver observer; + RegisterAndWait(&observer, type, NotificationService::AllSources()); +} + +void RegisterAndWait(NotificationObserver* observer, + NotificationType::Type type, + const NotificationSource& source) { NotificationRegistrar registrar; - registrar.Add(observer, type, NotificationService::AllSources()); + registrar.Add(observer, type, source); RunMessageLoop(); } diff --git a/chrome/test/ui_test_utils.h b/chrome/test/ui_test_utils.h index b247a19..9543f9d 100644 --- a/chrome/test/ui_test_utils.h +++ b/chrome/test/ui_test_utils.h @@ -175,10 +175,14 @@ bool IsViewFocused(const Browser* browser, ViewID vid); // Simulates a mouse click on a View in the browser. void ClickOnView(const Browser* browser, ViewID vid); -// Register |observer| for the given |type| and run the message loop until -// the observer posts a quit task. -void RegisterAndWait(NotificationType::Type type, - NotificationObserver* observer); +// Blocks until a notification for given |type| is received. +void WaitForNotification(NotificationType::Type type); + +// Register |observer| for the given |type| and |source| and run +// the message loop until the observer posts a quit task. +void RegisterAndWait(NotificationObserver* observer, + NotificationType::Type type, + const NotificationSource& source); // Run a message loop only for the specified amount of time. class TimedMessageLoopRunner { @@ -252,6 +256,34 @@ class TestWebSocketServer { DISALLOW_COPY_AND_ASSIGN(TestWebSocketServer); }; +// A notification observer which quits the message loop when a notification +// is received. It also records the source and details of the notification. +class TestNotificationObserver : public NotificationObserver { + public: + TestNotificationObserver() : source_(NotificationService::AllSources()) { + } + + virtual void Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) { + source_ = source; + details_ = details; + MessageLoopForUI::current()->Quit(); + } + + const NotificationSource& source() const { + return source_; + } + + const NotificationDetails& details() const { + return details_; + } + + private: + NotificationSource source_; + NotificationDetails details_; +}; + // A WindowedNotificationObserver allows code to watch for a notification // over a window of time. Typically testing code will need to do something // like this: |