diff options
author | ahendrickson@chromium.org <ahendrickson@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-10 17:08:44 +0000 |
---|---|---|
committer | ahendrickson@chromium.org <ahendrickson@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-10 17:08:44 +0000 |
commit | 46f979d4c5716549777cef154423fa4fb76210d9 (patch) | |
tree | 8e11b421cea629422f64f7144890abc8524e7eb5 /chrome/test | |
parent | 8050ca9e629f560af14653566de54a12dc2bcc78 (diff) | |
download | chromium_src-46f979d4c5716549777cef154423fa4fb76210d9.zip chromium_src-46f979d4c5716549777cef154423fa4fb76210d9.tar.gz chromium_src-46f979d4c5716549777cef154423fa4fb76210d9.tar.bz2 |
Converted download UI tests to Browser tests.
Converted NavigateWithURLAsync/WaitForDownloadShelfVisible calls to NavigateToURLWithDisposition. This waits until the navigation is done before returning, avoiding the need for the 'sleep and wait' loop.
BUG=none
TEST=Run browser_tests --gtest_filter=DownloadTest.*
Review URL: http://codereview.chromium.org/5610006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@70900 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/test')
-rw-r--r-- | chrome/test/in_process_browser_test.cc | 8 | ||||
-rw-r--r-- | chrome/test/in_process_browser_test.h | 3 | ||||
-rw-r--r-- | chrome/test/ui_test_utils.cc | 102 | ||||
-rw-r--r-- | chrome/test/ui_test_utils.h | 41 |
4 files changed, 149 insertions, 5 deletions
diff --git a/chrome/test/in_process_browser_test.cc b/chrome/test/in_process_browser_test.cc index 5815fad..ff428c9 100644 --- a/chrome/test/in_process_browser_test.cc +++ b/chrome/test/in_process_browser_test.cc @@ -274,6 +274,14 @@ Browser* InProcessBrowserTest::CreateBrowser(Profile* profile) { return browser; } +Browser* InProcessBrowserTest::CreateIncognitoBrowser() { + // Create a new browser with using the incognito profile. + Browser* incognito = + Browser::Create(browser()->profile()->GetOffTheRecordProfile()); + InitializeBrowser(incognito); + return incognito; +} + Browser* InProcessBrowserTest::CreateBrowserForPopup(Profile* profile) { Browser* browser = Browser::CreateForType(Browser::TYPE_POPUP, profile); InitializeBrowser(browser); diff --git a/chrome/test/in_process_browser_test.h b/chrome/test/in_process_browser_test.h index 42cadbb..169ad47 100644 --- a/chrome/test/in_process_browser_test.h +++ b/chrome/test/in_process_browser_test.h @@ -145,6 +145,9 @@ class InProcessBrowserTest : public testing::Test { // This is invoked from Setup. virtual Browser* CreateBrowser(Profile* profile); + // Similar to |CreateBrowser|, but creates an incognito browser. + virtual Browser* CreateIncognitoBrowser(); + // Creates a browser for a popup window with a single tab (about:blank), waits // for the tab to finish loading, and shows the browser. Browser* CreateBrowserForPopup(Profile* profile); diff --git a/chrome/test/ui_test_utils.cc b/chrome/test/ui_test_utils.cc index 0be4bd8..7eba05c 100644 --- a/chrome/test/ui_test_utils.cc +++ b/chrome/test/ui_test_utils.cc @@ -432,6 +432,17 @@ Browser* WaitForNewBrowser() { return Source<Browser>(observer.source()).ptr(); } +Browser* WaitForBrowserNotInSet(std::set<Browser*> excluded_browsers) { + TestNotificationObserver observer; + Browser* new_browser = GetBrowserNotInSet(excluded_browsers); + if (new_browser == NULL) { + new_browser = WaitForNewBrowser(); + // The new browser should never be in |excluded_browsers|. + DCHECK(!ContainsKey(excluded_browsers, new_browser)); + } + return new_browser; +} + void OpenURLOffTheRecord(Profile* profile, const GURL& url) { Browser::OpenURLOffTheRecord(profile, url); Browser* browser = BrowserList::FindBrowserWithType( @@ -440,16 +451,80 @@ void OpenURLOffTheRecord(Profile* profile, const GURL& url) { } void NavigateToURL(Browser* browser, const GURL& url) { - NavigateToURLBlockUntilNavigationsComplete(browser, url, 1); + NavigateToURLWithDisposition(browser, url, CURRENT_TAB, + BROWSER_TEST_WAIT_FOR_NAVIGATION); +} + +// Navigates the specified tab (via |disposition|) of |browser| to |url|, +// blocking until the |number_of_navigations| specified complete. +// |disposition| indicates what tab the download occurs in, and +// |browser_test_flags| controls what to wait for before continuing. +static void NavigateToURLWithDispositionBlockUntilNavigationsComplete( + Browser* browser, + const GURL& url, + int number_of_navigations, + WindowOpenDisposition disposition, + int browser_test_flags) { + std::set<Browser*> initial_browsers; + for (std::vector<Browser*>::const_iterator iter = BrowserList::begin(); + iter != BrowserList::end(); + ++iter) { + initial_browsers.insert(*iter); + } + browser->OpenURL(url, GURL(), disposition, PageTransition::TYPED); + if (browser_test_flags & BROWSER_TEST_WAIT_FOR_BROWSER) + browser = WaitForBrowserNotInSet(initial_browsers); + if (browser_test_flags & BROWSER_TEST_WAIT_FOR_TAB) + WaitForNotification(NotificationType::TAB_ADDED); + if (!(browser_test_flags & BROWSER_TEST_WAIT_FOR_NAVIGATION)) { + // Some other flag caused the wait prior to this. + return; + } + TabContents* tab_contents = NULL; + if (disposition == NEW_BACKGROUND_TAB) { + // We've opened up a new tab, but not selected it. + tab_contents = browser->GetTabContentsAt(browser->selected_index() + 1); + EXPECT_TRUE(tab_contents != NULL) + << " Unable to wait for navigation to \"" << url.spec() + << "\" because the new tab is not available yet"; + return; + } else if ((disposition == CURRENT_TAB) || + (disposition == NEW_FOREGROUND_TAB) || + (disposition == SINGLETON_TAB)) { + // The currently selected tab is the right one. + tab_contents = browser->GetSelectedTabContents(); + } + if (tab_contents) { + NavigationController* controller = &tab_contents->controller(); + WaitForNavigations(controller, number_of_navigations); + return; + } + EXPECT_TRUE(NULL != tab_contents) << " Unable to wait for navigation to \"" + << url.spec() << "\"" + << " because we can't get the tab contents"; +} + +void NavigateToURLWithDisposition(Browser* browser, + const GURL& url, + WindowOpenDisposition disposition, + int browser_test_flags) { + NavigateToURLWithDispositionBlockUntilNavigationsComplete( + browser, + url, + 1, + disposition, + browser_test_flags); } void NavigateToURLBlockUntilNavigationsComplete(Browser* browser, const GURL& url, int number_of_navigations) { - NavigationController* controller = - &browser->GetSelectedTabContents()->controller(); - browser->OpenURL(url, GURL(), CURRENT_TAB, PageTransition::TYPED); - WaitForNavigations(controller, number_of_navigations); + NavigateToURLWithDispositionBlockUntilNavigationsComplete( + browser, + url, + number_of_navigations, + CURRENT_TAB, + BROWSER_TEST_WAIT_FOR_NAVIGATION); } DOMElementProxyRef GetActiveDOMDocument(Browser* browser) { @@ -563,6 +638,12 @@ void WaitForNotification(NotificationType type) { RegisterAndWait(&observer, type, NotificationService::AllSources()); } +void WaitForNotificationFrom(NotificationType type, + const NotificationSource& source) { + TestNotificationObserver observer; + RegisterAndWait(&observer, type, source); +} + void RegisterAndWait(NotificationObserver* observer, NotificationType type, const NotificationSource& source) { @@ -606,6 +687,17 @@ bool BringBrowserWindowToFront(const Browser* browser) { return true; } +Browser* GetBrowserNotInSet(std::set<Browser*> excluded_browsers) { + for (BrowserList::const_iterator iter = BrowserList::begin(); + iter != BrowserList::end(); + ++iter) { + if (excluded_browsers.find(*iter) == excluded_browsers.end()) + return *iter; + } + + return NULL; +} + bool SendKeyPressSync(const Browser* browser, app::KeyboardCode key, bool control, diff --git a/chrome/test/ui_test_utils.h b/chrome/test/ui_test_utils.h index 5299451..4902396 100644 --- a/chrome/test/ui_test_utils.h +++ b/chrome/test/ui_test_utils.h @@ -24,6 +24,7 @@ #include "chrome/test/automation/dom_element_proxy.h" #include "gfx/native_widget_types.h" #include "testing/gtest/include/gtest/gtest.h" +#include "webkit/glue/window_open_disposition.h" class AppModalDialog; class BookmarkModel; @@ -52,6 +53,23 @@ class Size; // A collections of functions designed for use with InProcessBrowserTest. namespace ui_test_utils { +// Flags to indicate what to wait for in a navigation test. +// They can be ORed together. +// The order in which the waits happen when more than one is selected, is: +// Browser +// Tab +// Navigation +enum BrowserTestWaitFlags { + BROWSER_TEST_NONE = 0, // Don't wait for anything. + BROWSER_TEST_WAIT_FOR_BROWSER = 1 << 0, // Wait for a new browser. + BROWSER_TEST_WAIT_FOR_TAB = 1 << 1, // Wait for a new tab. + BROWSER_TEST_WAIT_FOR_NAVIGATION = 1 << 2, // Wait for navigation to finish. + + BROWSER_TEST_MASK = BROWSER_TEST_WAIT_FOR_BROWSER | + BROWSER_TEST_WAIT_FOR_TAB | + BROWSER_TEST_WAIT_FOR_NAVIGATION +}; + // Turns on nestable tasks, runs the message loop, then resets nestable tasks // to what they were originally. Prefer this over MessageLoop::Run for in // process browser tests that need to block until a condition is met. @@ -96,6 +114,12 @@ void WaitForLoadStop(NavigationController* controller); // Waits for a new browser to be created, returning the browser. Browser* WaitForNewBrowser(); +// Waits for a new browser to be created, returning the browser. +// Pass in the number of browsers that exist before the navigation starts in +// |start_count|, and it will exit even if the notification occurs before it's +// called. +Browser* WaitForNewBrowserWithCount(size_t start_count); + // Opens |url| in an incognito browser window with the off the record profile of // |profile|, blocking until the navigation finishes. This will create a new // browser if a browser with the off the record profile does not exist. @@ -105,6 +129,15 @@ void OpenURLOffTheRecord(Profile* profile, const GURL& url); // navigation finishes. void NavigateToURL(Browser* browser, const GURL& url); +// Navigates the specified tab of |browser| to |url|, blocking until the +// navigation finishes. +// |disposition| indicates what tab the navigation occurs in, and +// |browser_test_flags| controls what to wait for before continuing. +void NavigateToURLWithDisposition(Browser* browser, + const GURL& url, + WindowOpenDisposition disposition, + int browser_test_flags); + // Navigates the selected tab of |browser| to |url|, blocking until the // number of navigations specified complete. void NavigateToURLBlockUntilNavigationsComplete(Browser* browser, @@ -191,6 +224,11 @@ void ClickOnView(const Browser* browser, ViewID vid); // Blocks until a notification for given |type| is received. void WaitForNotification(NotificationType type); +// Blocks until a notification for given |type| from the specified |source| +// is received. +void WaitForNotificationFrom(NotificationType type, + const NotificationSource& source); + // Register |observer| for the given |type| and |source| and run // the message loop until the observer posts a quit task. void RegisterAndWait(NotificationObserver* observer, @@ -212,6 +250,9 @@ bool GetNativeWindow(const Browser* browser, gfx::NativeWindow* native_window) // success. bool BringBrowserWindowToFront(const Browser* browser) WARN_UNUSED_RESULT; +// Gets the first browser that is not in the specified set. +Browser* GetBrowserNotInSet(std::set<Browser*> excluded_browsers); + // Sends a key press, blocking until the key press is received or the test times // out. This uses ui_controls::SendKeyPress, see it for details. Returns true // if the event was successfully sent and received. |