diff options
author | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-07-20 05:40:59 +0000 |
---|---|---|
committer | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-07-20 05:40:59 +0000 |
commit | 35daebe09bf03548ca99e58bc6ebb57d5b7a520d (patch) | |
tree | 0f50f5e4bbb1579d98154d50800543d9ea05301b /content | |
parent | 86fcebea2f4f126302400b13a8176a7e43d0a7ab (diff) | |
download | chromium_src-35daebe09bf03548ca99e58bc6ebb57d5b7a520d.zip chromium_src-35daebe09bf03548ca99e58bc6ebb57d5b7a520d.tar.gz chromium_src-35daebe09bf03548ca99e58bc6ebb57d5b7a520d.tar.bz2 |
Move TitleWatcher from ui_test_utils.h to browser_test_utils.h so it can be reused by content_browsertests.
BUG=90448
TBR=scottmg
Review URL: https://chromiumcodereview.appspot.com/10815025
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@147618 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content')
-rw-r--r-- | content/browser/media_browsertest.cc | 3 | ||||
-rw-r--r-- | content/browser/mouseleave_browsertest.cc | 8 | ||||
-rw-r--r-- | content/browser/plugin_browsertest.cc | 9 | ||||
-rw-r--r-- | content/browser/renderer_host/resource_dispatcher_host_browsertest.cc | 5 | ||||
-rw-r--r-- | content/browser/session_history_browsertest.cc | 3 | ||||
-rw-r--r-- | content/browser/worker_host/test/worker_browsertest.cc | 3 | ||||
-rw-r--r-- | content/public/test/browser_test_utils.h | 48 | ||||
-rw-r--r-- | content/test/browser_test_utils.cc | 72 | ||||
-rw-r--r-- | content/test/layout_browsertest.cc | 3 |
9 files changed, 140 insertions, 14 deletions
diff --git a/content/browser/media_browsertest.cc b/content/browser/media_browsertest.cc index d90f450..10f0fc4 100644 --- a/content/browser/media_browsertest.cc +++ b/content/browser/media_browsertest.cc @@ -10,6 +10,7 @@ #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser_tabstrip.h" #include "chrome/test/base/ui_test_utils.h" +#include "content/public/test/browser_test_utils.h" #include "content/test/layout_browsertest.h" #include "googleurl/src/gurl.h" @@ -30,7 +31,7 @@ class MediaTest : public InProcessBrowserTest { const string16 kPlaying = ASCIIToUTF16("PLAYING"); const string16 kFailed = ASCIIToUTF16("FAILED"); const string16 kError = ASCIIToUTF16("ERROR"); - ui_test_utils::TitleWatcher title_watcher( + content::TitleWatcher title_watcher( chrome::GetActiveWebContents(browser()), kPlaying); title_watcher.AlsoWaitForTitle(kFailed); title_watcher.AlsoWaitForTitle(kError); diff --git a/content/browser/mouseleave_browsertest.cc b/content/browser/mouseleave_browsertest.cc index 367665e..def9789 100644 --- a/content/browser/mouseleave_browsertest.cc +++ b/content/browser/mouseleave_browsertest.cc @@ -10,6 +10,7 @@ #include "chrome/test/base/ui_test_utils.h" #include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents_view.h" +#include "content/public/test/browser_test_utils.h" #include "ui/ui_controls/ui_controls.h" namespace { @@ -51,7 +52,7 @@ IN_PROC_BROWSER_TEST_F(MouseLeaveTest, MAYBE_TestOnMouseOut) { // Navigate to the test html page. string16 load_expected_title(ASCIIToUTF16("onload")); - ui_test_utils::TitleWatcher load_title_watcher(tab, load_expected_title); + content::TitleWatcher load_title_watcher(tab, load_expected_title); ui_test_utils::NavigateToURL(browser(), test_url); // Wait for the onload() handler to complete so we can do the // next part of the test. @@ -63,8 +64,7 @@ IN_PROC_BROWSER_TEST_F(MouseLeaveTest, MAYBE_TestOnMouseOut) { // Wait on the correct intermediate title. string16 entered_expected_title(ASCIIToUTF16("entered")); - ui_test_utils::TitleWatcher entered_title_watcher( - tab, entered_expected_title); + content::TitleWatcher entered_title_watcher(tab, entered_expected_title); EXPECT_EQ(entered_expected_title, entered_title_watcher.WaitAndGetTitle()); // Move the cursor above the content again, which should trigger @@ -73,7 +73,7 @@ IN_PROC_BROWSER_TEST_F(MouseLeaveTest, MAYBE_TestOnMouseOut) { // Wait on the correct final value of the cookie. string16 left_expected_title(ASCIIToUTF16("left")); - ui_test_utils::TitleWatcher left_title_watcher(tab, left_expected_title); + content::TitleWatcher left_title_watcher(tab, left_expected_title); EXPECT_EQ(left_expected_title, left_title_watcher.WaitAndGetTitle()); } diff --git a/content/browser/plugin_browsertest.cc b/content/browser/plugin_browsertest.cc index 4cd42bf..d8ab3a1 100644 --- a/content/browser/plugin_browsertest.cc +++ b/content/browser/plugin_browsertest.cc @@ -11,6 +11,7 @@ #include "chrome/test/base/ui_test_utils.h" #include "content/public/browser/browser_thread.h" #include "content/public/common/content_switches.h" +#include "content/public/test/browser_test_utils.h" #include "content/test/net/url_request_mock_http_job.h" #include "webkit/plugins/plugin_switches.h" @@ -84,7 +85,7 @@ class PluginTest : public InProcessBrowserTest { void LoadAndWait(const GURL& url) { string16 expected_title(ASCIIToUTF16("OK")); - ui_test_utils::TitleWatcher title_watcher( + content::TitleWatcher title_watcher( chrome::GetActiveWebContents(browser()), expected_title); title_watcher.AlsoWaitForTitle(ASCIIToUTF16("FAIL")); title_watcher.AlsoWaitForTitle(ASCIIToUTF16("plugin_not_found")); @@ -175,7 +176,7 @@ IN_PROC_BROWSER_TEST_F(PluginTest, browser(), GetURL("execute_script_delete_in_mouse_up.html")); string16 expected_title(ASCIIToUTF16("OK")); - ui_test_utils::TitleWatcher title_watcher( + content::TitleWatcher title_watcher( chrome::GetActiveWebContents(browser()), expected_title); title_watcher.AlsoWaitForTitle(ASCIIToUTF16("FAIL")); ui_test_utils::SimulateMouseClick(chrome::GetActiveWebContents(browser())); @@ -202,7 +203,7 @@ IN_PROC_BROWSER_TEST_F(PluginTest, SelfDeletePluginInvokeAlert) { 0); string16 expected_title(ASCIIToUTF16("OK")); - ui_test_utils::TitleWatcher title_watcher( + content::TitleWatcher title_watcher( chrome::GetActiveWebContents(browser()), expected_title); title_watcher.AlsoWaitForTitle(ASCIIToUTF16("FAIL")); @@ -400,7 +401,7 @@ IN_PROC_BROWSER_TEST_F(PluginTest, DISABLED_PluginConvertPointTest) { ui_test_utils::NavigateToURL(browser(), GetURL("convert_point.html")); string16 expected_title(ASCIIToUTF16("OK")); - ui_test_utils::TitleWatcher title_watcher( + content::TitleWatcher title_watcher( chrome::GetActiveWebContents(browser()), expected_title); title_watcher.AlsoWaitForTitle(ASCIIToUTF16("FAIL")); // TODO(stuartmorgan): When the automation system supports sending clicks, diff --git a/content/browser/renderer_host/resource_dispatcher_host_browsertest.cc b/content/browser/renderer_host/resource_dispatcher_host_browsertest.cc index 6ee54c5..763a259 100644 --- a/content/browser/renderer_host/resource_dispatcher_host_browsertest.cc +++ b/content/browser/renderer_host/resource_dispatcher_host_browsertest.cc @@ -18,6 +18,7 @@ #include "content/public/browser/notification_types.h" #include "content/public/browser/web_contents.h" #include "content/public/common/url_constants.h" +#include "content/public/test/browser_test_utils.h" #include "content/test/net/url_request_failed_job.h" #include "content/test/net/url_request_mock_http_job.h" #include "net/base/net_errors.h" @@ -86,7 +87,7 @@ class ResourceDispatcherHostBrowserTest : public InProcessBrowserTest, const std::string& expected_title, int expected_navigations) { string16 expected_title16(ASCIIToUTF16(expected_title)); - ui_test_utils::TitleWatcher title_watcher( + content::TitleWatcher title_watcher( chrome::GetActiveWebContents(browser()), expected_title16); ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( browser(), url, expected_navigations); @@ -400,7 +401,7 @@ IN_PROC_BROWSER_TEST_F(ResourceDispatcherHostBrowserTest, // pages of which the error page is one. Instead, use automation to kick // off the navigation, and wait to see that the tab loads. string16 expected_title16(ASCIIToUTF16("Title Of Awesomeness")); - ui_test_utils::TitleWatcher title_watcher( + content::TitleWatcher title_watcher( chrome::GetActiveWebContents(browser()), expected_title16); bool success; diff --git a/content/browser/session_history_browsertest.cc b/content/browser/session_history_browsertest.cc index fe28fe4..e918988 100644 --- a/content/browser/session_history_browsertest.cc +++ b/content/browser/session_history_browsertest.cc @@ -13,6 +13,7 @@ #include "content/public/browser/notification_types.h" #include "content/public/browser/web_contents.h" #include "content/public/common/url_constants.h" +#include "content/public/test/browser_test_utils.h" #include "net/test/test_server.h" #include "testing/gtest/include/gtest/gtest.h" @@ -68,7 +69,7 @@ class SessionHistoryTest : public InProcessBrowserTest { void NavigateAndCheckTitle(const char* filename, const std::string& expected_title) { string16 expected_title16(ASCIIToUTF16(expected_title)); - ui_test_utils::TitleWatcher title_watcher( + content::TitleWatcher title_watcher( chrome::GetActiveWebContents(browser()), expected_title16); ui_test_utils::NavigateToURL(browser(), GetURL(filename)); ASSERT_EQ(expected_title16, title_watcher.WaitAndGetTitle()); diff --git a/content/browser/worker_host/test/worker_browsertest.cc b/content/browser/worker_host/test/worker_browsertest.cc index 0532452..dc50052 100644 --- a/content/browser/worker_host/test/worker_browsertest.cc +++ b/content/browser/worker_host/test/worker_browsertest.cc @@ -18,6 +18,7 @@ #include "content/browser/worker_host/worker_service_impl.h" #include "content/public/browser/browser_thread.h" #include "content/public/common/content_paths.h" +#include "content/public/test/browser_test_utils.h" #include "content/test/layout_browsertest.h" #include "googleurl/src/gurl.h" @@ -287,7 +288,7 @@ class WorkerTest : public InProcessBrowserTest { const std::string& query) { GURL url = GetTestURL(test_case, query); const string16 expected_title = ASCIIToUTF16("OK"); - ui_test_utils::TitleWatcher title_watcher( + content::TitleWatcher title_watcher( chrome::GetActiveWebContents(browser), expected_title); ui_test_utils::NavigateToURL(browser, url); string16 final_title = title_watcher.WaitAndGetTitle(); diff --git a/content/public/test/browser_test_utils.h b/content/public/test/browser_test_utils.h index e295dd2..0e0bac8 100644 --- a/content/public/test/browser_test_utils.h +++ b/content/public/test/browser_test_utils.h @@ -5,7 +5,14 @@ #ifndef CONTENT_PUBLIC_TEST_BROWSER_TEST_UTILS_H_ #define CONTENT_PUBLIC_TEST_BROWSER_TEST_UTILS_H_ +#include <vector> + #include "base/callback_forward.h" +#include "base/compiler_specific.h" +#include "base/memory/ref_counted.h" +#include "base/string16.h" +#include "content/public/browser/notification_observer.h" +#include "content/public/browser/notification_registrar.h" #include "googleurl/src/gurl.h" namespace base { @@ -21,6 +28,47 @@ class RunLoop; namespace content { +class MessageLoopRunner; +class WebContents; + +// Watches title changes on a tab, blocking until an expected title is set. +class TitleWatcher : public NotificationObserver { + public: + // |web_contents| must be non-NULL and needs to stay alive for the + // entire lifetime of |this|. |expected_title| is the title that |this| + // will wait for. + TitleWatcher(WebContents* web_contents, + const string16& expected_title); + virtual ~TitleWatcher(); + + // Adds another title to watch for. + void AlsoWaitForTitle(const string16& expected_title); + + // Waits until the title matches either expected_title or one of the titles + // added with AlsoWaitForTitle. Returns the value of the most recently + // observed matching title. + const string16& WaitAndGetTitle() WARN_UNUSED_RESULT; + + private: + // NotificationObserver + virtual void Observe(int type, + const NotificationSource& source, + const NotificationDetails& details) OVERRIDE; + + WebContents* web_contents_; + std::vector<string16> expected_titles_; + NotificationRegistrar notification_registrar_; + scoped_refptr<MessageLoopRunner> message_loop_runner_; + + // The most recently observed expected title, if any. + string16 observed_title_; + + bool expected_title_observed_; + bool quit_loop_on_observation_; + + DISALLOW_COPY_AND_ASSIGN(TitleWatcher); +}; + } // namespace content #endif // CONTENT_PUBLIC_TEST_BROWSER_TEST_UTILS_H_ diff --git a/content/test/browser_test_utils.cc b/content/test/browser_test_utils.cc index 145c432..6c55430 100644 --- a/content/test/browser_test_utils.cc +++ b/content/test/browser_test_utils.cc @@ -4,6 +4,78 @@ #include "content/public/test/browser_test_utils.h" +#include "content/public/browser/notification_types.h" +#include "content/public/browser/web_contents.h" +#include "content/public/test/test_utils.h" +#include "testing/gtest/include/gtest/gtest.h" + namespace content { +TitleWatcher::TitleWatcher(WebContents* web_contents, + const string16& expected_title) + : web_contents_(web_contents), + expected_title_observed_(false), + quit_loop_on_observation_(false) { + EXPECT_TRUE(web_contents != NULL); + expected_titles_.push_back(expected_title); + notification_registrar_.Add(this, + NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED, + Source<WebContents>(web_contents)); + + // When navigating through the history, the restored NavigationEntry's title + // will be used. If the entry ends up having the same title after we return + // to it, as will usually be the case, the + // NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED will then be suppressed, since the + // NavigationEntry's title hasn't changed. + notification_registrar_.Add( + this, + NOTIFICATION_LOAD_STOP, + Source<NavigationController>(&web_contents->GetController())); +} + +void TitleWatcher::AlsoWaitForTitle(const string16& expected_title) { + expected_titles_.push_back(expected_title); +} + +TitleWatcher::~TitleWatcher() { +} + +const string16& TitleWatcher::WaitAndGetTitle() { + if (expected_title_observed_) + return observed_title_; + quit_loop_on_observation_ = true; + message_loop_runner_ = new MessageLoopRunner; + message_loop_runner_->Run(); + return observed_title_; +} + +void TitleWatcher::Observe(int type, + const NotificationSource& source, + const NotificationDetails& details) { + if (type == NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED) { + WebContents* source_contents = Source<WebContents>(source).ptr(); + ASSERT_EQ(web_contents_, source_contents); + } else if (type == NOTIFICATION_LOAD_STOP) { + NavigationController* controller = + Source<NavigationController>(source).ptr(); + ASSERT_EQ(&web_contents_->GetController(), controller); + } else { + FAIL() << "Unexpected notification received."; + } + + std::vector<string16>::const_iterator it = + std::find(expected_titles_.begin(), + expected_titles_.end(), + web_contents_->GetTitle()); + if (it == expected_titles_.end()) + return; + observed_title_ = *it; + expected_title_observed_ = true; + if (quit_loop_on_observation_) { + // Only call Quit once, on first Observe: + quit_loop_on_observation_ = false; + message_loop_runner_->Quit(); + } +} + } // namespace content diff --git a/content/test/layout_browsertest.cc b/content/test/layout_browsertest.cc index 9ccd9db..b45477a 100644 --- a/content/test/layout_browsertest.cc +++ b/content/test/layout_browsertest.cc @@ -19,6 +19,7 @@ #include "chrome/test/base/ui_test_utils.h" #include "content/browser/web_contents/web_contents_impl.h" #include "content/public/common/content_paths.h" +#include "content/public/test/browser_test_utils.h" #include "net/base/net_util.h" #if defined(OS_WIN) @@ -185,7 +186,7 @@ void InProcessBrowserLayoutTest::RunLayoutTestInternal( const std::string& test_case_file_name, const GURL& url) { LOG(INFO) << "Navigating to URL " << url << " and blocking."; const string16 expected_title = ASCIIToUTF16("done"); - ui_test_utils::TitleWatcher title_watcher( + content::TitleWatcher title_watcher( chrome::GetActiveWebContents(browser()), expected_title); ui_test_utils::NavigateToURL(browser(), url); LOG(INFO) << "Navigation completed, now waiting for title."; |