summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorviettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-04-08 16:07:16 +0000
committerviettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-04-08 16:07:16 +0000
commit714786e1b9566ca76388497904d2ebdb7fd111f5 (patch)
tree01acf69cddaa411e91c0e8f1cb1c8ed3c30a0ba0
parenta5a00b1dd7a4950de617bb9bb9bc596d1f89965f (diff)
downloadchromium_src-714786e1b9566ca76388497904d2ebdb7fd111f5.zip
chromium_src-714786e1b9566ca76388497904d2ebdb7fd111f5.tar.gz
chromium_src-714786e1b9566ca76388497904d2ebdb7fd111f5.tar.bz2
Revert 43931 - [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 TBR=phajdan.jr@chromium.org Review URL: http://codereview.chromium.org/1520026 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@43950 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/autocomplete/autocomplete_browsertest.cc20
-rw-r--r--chrome/browser/browser_browsertest.cc7
-rw-r--r--chrome/browser/browser_init_browsertest.cc5
-rw-r--r--chrome/browser/crash_recovery_browsertest.cc36
-rw-r--r--chrome/browser/download/save_page_browsertest.cc52
-rw-r--r--chrome/browser/extensions/extension_apitest.cc15
-rw-r--r--chrome/browser/extensions/extension_browsertest.cc49
-rw-r--r--chrome/browser/extensions/extension_devtools_browsertest.h4
-rw-r--r--chrome/browser/extensions/extension_startup_unittest.cc42
-rw-r--r--chrome/test/ui_test_utils.cc116
-rw-r--r--chrome/test/ui_test_utils.h40
11 files changed, 252 insertions, 134 deletions
diff --git a/chrome/browser/autocomplete/autocomplete_browsertest.cc b/chrome/browser/autocomplete/autocomplete_browsertest.cc
index f54e535..29a9b30 100644
--- a/chrome/browser/autocomplete/autocomplete_browsertest.cc
+++ b/chrome/browser/autocomplete/autocomplete_browsertest.cc
@@ -12,6 +12,8 @@
#include "chrome/browser/location_bar.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/tab_contents/tab_contents.h"
+#include "chrome/common/notification_registrar.h"
+#include "chrome/common/notification_service.h"
#include "chrome/common/notification_type.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/in_process_browser_test.h"
@@ -34,7 +36,8 @@ std::wstring AutocompleteResultAsString(const AutocompleteResult& result) {
} // namespace
-class AutocompleteBrowserTest : public InProcessBrowserTest {
+class AutocompleteBrowserTest : public InProcessBrowserTest,
+ public NotificationObserver {
protected:
LocationBar* GetLocationBar() const {
return browser()->window()->GetLocationBar();
@@ -48,8 +51,19 @@ class AutocompleteBrowserTest : public InProcessBrowserTest {
void WaitForHistoryBackendToLoad() {
HistoryService* history_service =
browser()->profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
- if (!history_service->BackendLoaded())
- ui_test_utils::WaitForNotification(NotificationType::HISTORY_LOADED);
+ if (!history_service->BackendLoaded()) {
+ NotificationRegistrar registrar;
+ registrar.Add(this, NotificationType::HISTORY_LOADED,
+ NotificationService::AllSources());
+ ui_test_utils::RunMessageLoop();
+ }
+ }
+
+ virtual void Observe(NotificationType type,
+ const NotificationSource& source,
+ const NotificationDetails& details) {
+ DCHECK(type == NotificationType::HISTORY_LOADED);
+ MessageLoop::current()->Quit();
}
};
diff --git a/chrome/browser/browser_browsertest.cc b/chrome/browser/browser_browsertest.cc
index ee51d90..66922f5 100644
--- a/chrome/browser/browser_browsertest.cc
+++ b/chrome/browser/browser_browsertest.cc
@@ -364,7 +364,7 @@ IN_PROC_BROWSER_TEST_F(BrowserTest, CommandCreateAppShortcut) {
ui_test_utils::NavigateToURL(browser(), blank_url);
EXPECT_FALSE(command_updater->IsCommandEnabled(IDC_CREATE_SHORTCUTS));
}
-#endif // !defined(OS_MACOSX)
+#endif
// Test RenderView correctly send back favicon url for web page that redirects
// to an anchor in javascript body.onload handler.
@@ -452,7 +452,8 @@ IN_PROC_BROWSER_TEST_F(BrowserTest, AppTabRemovedWhenExtensionUninstalled) {
// The uninstall should have removed the tab.
ASSERT_EQ(1, browser()->tab_count());
}
-#endif // !defined(OS_MACOSX)
+
+#endif
// Tests that the CLD (Compact Language Detection) works properly.
IN_PROC_BROWSER_TEST_F(BrowserTest, PageLanguageDetection) {
@@ -550,4 +551,4 @@ IN_PROC_BROWSER_TEST_F(BrowserTest, RestorePinnedTabs) {
EXPECT_TRUE(new_model->GetTabContentsAt(0)->app_extension() ==
app_extension);
}
-#endif // !defined(OS_CHROMEOS)
+#endif
diff --git a/chrome/browser/browser_init_browsertest.cc b/chrome/browser/browser_init_browsertest.cc
index 1ee9e50..94aa2f2 100644
--- a/chrome/browser/browser_init_browsertest.cc
+++ b/chrome/browser/browser_init_browsertest.cc
@@ -58,10 +58,10 @@ IN_PROC_BROWSER_TEST_F(BrowserInitTest, OpenURLsPopup) {
// Test that we prevent openning potentially dangerous schemes from the
// command line.
-// TODO(jschuh): FLAKY because the process doesn't have sufficient time
+// jschuh: DISABLED because the process doesn't have sufficient time
// to start on most BuildBot runs and I don't want to add longer delays to
// the test. I'll circle back and make this work properly when i get a chance.
-IN_PROC_BROWSER_TEST_F(BrowserInitTest, FLAKY_BlockBadURLs) {
+IN_PROC_BROWSER_TEST_F(BrowserInitTest, DISABLED_BlockBadURLs) {
const std::wstring testurlstr(L"http://localhost/");
const GURL testurl(WideToUTF16Hack(testurlstr));
CommandLine cmdline(CommandLine::ARGUMENTS_ONLY);
@@ -85,4 +85,5 @@ IN_PROC_BROWSER_TEST_F(BrowserInitTest, FLAKY_BlockBadURLs) {
ASSERT_EQ(browser()->tab_count(), 3);
}
+
} // namespace
diff --git a/chrome/browser/crash_recovery_browsertest.cc b/chrome/browser/crash_recovery_browsertest.cc
index 1ee4cc4..846ddfe 100644
--- a/chrome/browser/crash_recovery_browsertest.cc
+++ b/chrome/browser/crash_recovery_browsertest.cc
@@ -6,7 +6,8 @@
#include "chrome/browser/browser.h"
#include "chrome/browser/tab_contents/navigation_entry.h"
#include "chrome/browser/tab_contents/tab_contents.h"
-#include "chrome/common/notification_type.h"
+#include "chrome/common/notification_registrar.h"
+#include "chrome/common/notification_service.h"
#include "chrome/common/page_transition_types.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/in_process_browser_test.h"
@@ -16,11 +17,40 @@
namespace {
+// Used to block until a navigation completes.
+class RendererCrashObserver : public NotificationObserver {
+ public:
+ RendererCrashObserver() {}
+
+ void WaitForRendererCrash() {
+ registrar_.Add(this, NotificationType::TAB_CONTENTS_DISCONNECTED,
+ NotificationService::AllSources());
+ ui_test_utils::RunMessageLoop();
+ }
+
+ virtual void Observe(NotificationType type,
+ const NotificationSource& source,
+ const NotificationDetails& details) {
+ if (type == NotificationType::TAB_CONTENTS_DISCONNECTED) {
+ registrar_.Remove(this, NotificationType::TAB_CONTENTS_DISCONNECTED,
+ NotificationService::AllSources());
+ MessageLoopForUI::current()->Quit();
+ } else {
+ NOTREACHED();
+ }
+ }
+
+ private:
+ NotificationRegistrar registrar_;
+
+ DISALLOW_COPY_AND_ASSIGN(RendererCrashObserver);
+};
+
void SimulateRendererCrash(Browser* browser) {
browser->OpenURL(GURL(chrome::kAboutCrashURL), GURL(), CURRENT_TAB,
PageTransition::TYPED);
- ui_test_utils::WaitForNotification(
- NotificationType::TAB_CONTENTS_DISCONNECTED);
+ RendererCrashObserver crash_observer;
+ crash_observer.WaitForRendererCrash();
}
} // namespace
diff --git a/chrome/browser/download/save_page_browsertest.cc b/chrome/browser/download/save_page_browsertest.cc
index 99648b4..70b1822 100644
--- a/chrome/browser/download/save_page_browsertest.cc
+++ b/chrome/browser/download/save_page_browsertest.cc
@@ -16,8 +16,6 @@
#include "chrome/test/ui_test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
-namespace {
-
static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("save_page");
static const char* kAppendedExtension =
@@ -27,6 +25,37 @@ static const char* kAppendedExtension =
".html";
#endif
+namespace {
+
+class SavePageFinishedObserver : public NotificationObserver {
+ public:
+ SavePageFinishedObserver() {
+ registrar_.Add(this, NotificationType::SAVE_PACKAGE_SUCCESSFULLY_FINISHED,
+ NotificationService::AllSources());
+ ui_test_utils::RunMessageLoop();
+ }
+
+ GURL page_url() const { return page_url_; }
+
+ virtual void Observe(NotificationType type,
+ const NotificationSource& source,
+ const NotificationDetails& details) {
+ if (type == NotificationType::SAVE_PACKAGE_SUCCESSFULLY_FINISHED) {
+ page_url_ = *Details<GURL>(details).ptr();
+ MessageLoopForUI::current()->Quit();
+ } else {
+ NOTREACHED();
+ }
+ }
+
+ private:
+ NotificationRegistrar registrar_;
+
+ GURL page_url_;
+
+ DISALLOW_COPY_AND_ASSIGN(SavePageFinishedObserver);
+};
+
class SavePageBrowserTest : public InProcessBrowserTest {
protected:
void SetUp() {
@@ -35,14 +64,6 @@ class SavePageBrowserTest : public InProcessBrowserTest {
InProcessBrowserTest::SetUp();
}
- GURL WaitForSavePackageToFinish() {
- ui_test_utils::TestNotificationObserver observer;
- ui_test_utils::RegisterAndWait(&observer,
- NotificationType::SAVE_PACKAGE_SUCCESSFULLY_FINISHED,
- NotificationService::AllSources());
- return *Details<GURL>(observer.details()).ptr();
- }
-
// Path to directory containing test data.
FilePath test_dir_;
@@ -64,7 +85,9 @@ IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) {
ASSERT_TRUE(current_tab->SavePage(full_file_name, dir,
SavePackage::SAVE_AS_ONLY_HTML));
- EXPECT_EQ(url, WaitForSavePackageToFinish());
+ SavePageFinishedObserver observer;
+
+ EXPECT_EQ(url, observer.page_url());
if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
@@ -119,7 +142,9 @@ IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) {
ASSERT_TRUE(current_tab->SavePage(full_file_name, dir,
SavePackage::SAVE_AS_COMPLETE_HTML));
- EXPECT_EQ(url, WaitForSavePackageToFinish());
+ SavePageFinishedObserver observer;
+
+ EXPECT_EQ(url, observer.page_url());
if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
@@ -160,8 +185,9 @@ IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) {
ASSERT_TRUE(current_tab->SavePage(full_file_name, dir,
SavePackage::SAVE_AS_COMPLETE_HTML));
+ SavePageFinishedObserver observer;
- EXPECT_EQ(url, WaitForSavePackageToFinish());
+ EXPECT_EQ(url, observer.page_url());
if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF))
EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible());
diff --git a/chrome/browser/extensions/extension_apitest.cc b/chrome/browser/extensions/extension_apitest.cc
index cc16957..081a092 100644
--- a/chrome/browser/extensions/extension_apitest.cc
+++ b/chrome/browser/extensions/extension_apitest.cc
@@ -10,6 +10,10 @@
#include "chrome/common/notification_registrar.h"
#include "chrome/test/ui_test_utils.h"
+namespace {
+static const int kTimeoutMs = 60 * 1000; // 1 minute
+};
+
ExtensionApiTest::ResultCatcher::ResultCatcher() {
registrar_.Add(this, NotificationType::EXTENSION_TEST_PASSED,
NotificationService::AllSources());
@@ -22,18 +26,19 @@ bool ExtensionApiTest::ResultCatcher::GetNextResult() {
// to RunMessageLoop(), so we maintain a queue of results and just pull them
// off as the test calls this, going to the run loop only when the queue is
// empty.
- if (results_.empty())
+ if (!results_.size()) {
+ MessageLoop::current()->PostDelayedTask(
+ FROM_HERE, new MessageLoop::QuitTask, kTimeoutMs);
ui_test_utils::RunMessageLoop();
-
- if (!results_.empty()) {
+ }
+ if (results_.size()) {
bool ret = results_.front();
results_.pop_front();
message_ = messages_.front();
messages_.pop_front();
return ret;
}
-
- NOTREACHED();
+ message_ = "No response from message loop.";
return false;
}
diff --git a/chrome/browser/extensions/extension_browsertest.cc b/chrome/browser/extensions/extension_browsertest.cc
index a6280a2..90cca84 100644
--- a/chrome/browser/extensions/extension_browsertest.cc
+++ b/chrome/browser/extensions/extension_browsertest.cc
@@ -157,9 +157,8 @@ bool ExtensionBrowserTest::InstallOrUpdateExtension(const std::string& id,
void ExtensionBrowserTest::ReloadExtension(const std::string& extension_id) {
ExtensionsService* service = browser()->profile()->GetExtensionsService();
service->ReloadExtension(extension_id);
- ui_test_utils::RegisterAndWait(this,
- NotificationType::EXTENSION_PROCESS_CREATED,
- NotificationService::AllSources());
+ ui_test_utils::RegisterAndWait(NotificationType::EXTENSION_PROCESS_CREATED,
+ this);
}
void ExtensionBrowserTest::UnloadExtension(const std::string& extension_id) {
@@ -183,26 +182,30 @@ void ExtensionBrowserTest::EnableExtension(const std::string& extension_id) {
}
bool ExtensionBrowserTest::WaitForPageActionCountChangeTo(int count) {
+ NotificationRegistrar registrar;
+ registrar.Add(this,
+ NotificationType::EXTENSION_PAGE_ACTION_COUNT_CHANGED,
+ NotificationService::AllSources());
+
+ target_page_action_count_ = count;
LocationBarTesting* location_bar =
browser()->window()->GetLocationBar()->GetLocationBarForTesting();
- if (location_bar->PageActionCount() != count) {
- target_page_action_count_ = count;
- ui_test_utils::RegisterAndWait(this,
- NotificationType::EXTENSION_PAGE_ACTION_COUNT_CHANGED,
- NotificationService::AllSources());
- }
+ if (location_bar->PageActionCount() != count)
+ ui_test_utils::RunMessageLoop();
return location_bar->PageActionCount() == count;
}
bool ExtensionBrowserTest::WaitForPageActionVisibilityChangeTo(int count) {
+ NotificationRegistrar registrar;
+ registrar.Add(this,
+ NotificationType::EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED,
+ NotificationService::AllSources());
+
+ target_visible_page_action_count_ = count;
LocationBarTesting* location_bar =
browser()->window()->GetLocationBar()->GetLocationBarForTesting();
- if (location_bar->PageActionVisibleCount() != count) {
- target_visible_page_action_count_ = count;
- ui_test_utils::RegisterAndWait(this,
- NotificationType::EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED,
- NotificationService::AllSources());
- }
+ if (location_bar->PageActionVisibleCount() != count)
+ ui_test_utils::RunMessageLoop();
return location_bar->PageActionVisibleCount() == count;
}
@@ -234,22 +237,19 @@ bool ExtensionBrowserTest::WaitForExtensionHostsToLoad() {
bool ExtensionBrowserTest::WaitForExtensionInstall() {
int before = extension_installs_observed_;
- ui_test_utils::RegisterAndWait(this, NotificationType::EXTENSION_INSTALLED,
- NotificationService::AllSources());
+ ui_test_utils::RegisterAndWait(NotificationType::EXTENSION_INSTALLED, this);
return extension_installs_observed_ == (before + 1);
}
bool ExtensionBrowserTest::WaitForExtensionInstallError() {
int before = extension_installs_observed_;
- ui_test_utils::RegisterAndWait(this,
- NotificationType::EXTENSION_INSTALL_ERROR,
- NotificationService::AllSources());
+ ui_test_utils::RegisterAndWait(NotificationType::EXTENSION_INSTALL_ERROR,
+ this);
return extension_installs_observed_ == before;
}
void ExtensionBrowserTest::WaitForExtensionLoad() {
- ui_test_utils::RegisterAndWait(this, NotificationType::EXTENSION_LOADED,
- NotificationService::AllSources());
+ ui_test_utils::RegisterAndWait(NotificationType::EXTENSION_LOADED, this);
WaitForExtensionHostsToLoad();
}
@@ -261,9 +261,8 @@ bool ExtensionBrowserTest::WaitForExtensionCrash(
// The extension is already unloaded, presumably due to a crash.
return true;
}
- ui_test_utils::RegisterAndWait(this,
- NotificationType::EXTENSION_PROCESS_TERMINATED,
- NotificationService::AllSources());
+ ui_test_utils::RegisterAndWait(NotificationType::EXTENSION_PROCESS_TERMINATED,
+ this);
return (service->GetExtensionById(extension_id, true) == NULL);
}
diff --git a/chrome/browser/extensions/extension_devtools_browsertest.h b/chrome/browser/extensions/extension_devtools_browsertest.h
index a29d3e5..7fa6435 100644
--- a/chrome/browser/extensions/extension_devtools_browsertest.h
+++ b/chrome/browser/extensions/extension_devtools_browsertest.h
@@ -13,6 +13,10 @@
class ExtensionDevToolsBrowserTest : public ExtensionBrowserTest {
protected:
virtual void SetUpCommandLine(CommandLine* command_line);
+
+ private:
+
+ NotificationRegistrar registrar_;
};
#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_DEVTOOLS_BROWSERTEST_H_
diff --git a/chrome/browser/extensions/extension_startup_unittest.cc b/chrome/browser/extensions/extension_startup_unittest.cc
index 1e1bbe3..4dc6efd 100644
--- a/chrome/browser/extensions/extension_startup_unittest.cc
+++ b/chrome/browser/extensions/extension_startup_unittest.cc
@@ -28,7 +28,8 @@
// had many silly bugs where command line flags did not get propagated correctly
// into the services, so we didn't start correctly.
-class ExtensionStartupTestBase : public InProcessBrowserTest {
+class ExtensionStartupTestBase
+ : public InProcessBrowserTest, public NotificationObserver {
public:
ExtensionStartupTestBase() : enable_extensions_(false) {
}
@@ -66,18 +67,36 @@ class ExtensionStartupTestBase : public InProcessBrowserTest {
}
}
+ // NotificationObserver
+ virtual void Observe(NotificationType type,
+ const NotificationSource& source,
+ const NotificationDetails& details) {
+ switch (type.value) {
+ case NotificationType::EXTENSIONS_READY:
+ case NotificationType::USER_SCRIPTS_UPDATED:
+ MessageLoopForUI::current()->Quit();
+ break;
+ default:
+ NOTREACHED();
+ }
+ }
+
virtual void TearDown() {
- EXPECT_TRUE(file_util::Delete(preferences_file_, false));
- EXPECT_TRUE(file_util::Delete(user_scripts_dir_, true));
- EXPECT_TRUE(file_util::Delete(extensions_dir_, true));
+ file_util::Delete(preferences_file_, false);
+ file_util::Delete(user_scripts_dir_, true);
+ file_util::Delete(extensions_dir_, true);
}
void WaitForServicesToStart(int num_expected_extensions,
bool expect_extensions_enabled) {
ExtensionsService* service = browser()->profile()->GetExtensionsService();
- if (!service->is_ready())
- ui_test_utils::WaitForNotification(NotificationType::EXTENSIONS_READY);
- ASSERT_TRUE(service->is_ready());
+ if (!service->is_ready()) {
+ registrar_.Add(this, NotificationType::EXTENSIONS_READY,
+ NotificationService::AllSources());
+ ui_test_utils::RunMessageLoop();
+ registrar_.Remove(this, NotificationType::EXTENSIONS_READY,
+ NotificationService::AllSources());
+ }
ASSERT_EQ(static_cast<uint32>(num_expected_extensions),
service->extensions()->size());
@@ -85,8 +104,12 @@ class ExtensionStartupTestBase : public InProcessBrowserTest {
UserScriptMaster* master = browser()->profile()->GetUserScriptMaster();
if (!master->ScriptsReady()) {
- ui_test_utils::WaitForNotification(
- NotificationType::USER_SCRIPTS_UPDATED);
+ // Wait for UserScriptMaster to finish its scan.
+ registrar_.Add(this, NotificationType::USER_SCRIPTS_UPDATED,
+ NotificationService::AllSources());
+ ui_test_utils::RunMessageLoop();
+ registrar_.Remove(this, NotificationType::USER_SCRIPTS_UPDATED,
+ NotificationService::AllSources());
}
ASSERT_TRUE(master->ScriptsReady());
}
@@ -122,6 +145,7 @@ class ExtensionStartupTestBase : public InProcessBrowserTest {
FilePath user_scripts_dir_;
bool enable_extensions_;
FilePath load_extension_;
+ NotificationRegistrar registrar_;
};
diff --git a/chrome/test/ui_test_utils.cc b/chrome/test/ui_test_utils.cc
index 612d921..b334e05 100644
--- a/chrome/test/ui_test_utils.cc
+++ b/chrome/test/ui_test_utils.cc
@@ -215,6 +215,62 @@ 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) {
@@ -382,28 +438,26 @@ void WaitForNavigations(NavigationController* controller,
}
void WaitForNewTab(Browser* browser) {
- TestNotificationObserver observer;
- RegisterAndWait(&observer, NotificationType::TAB_ADDED,
- Source<Browser>(browser));
+ SimpleNotificationObserver<Browser>
+ new_tab_observer(NotificationType::TAB_ADDED, browser);
}
void WaitForBrowserActionUpdated(ExtensionAction* browser_action) {
- TestNotificationObserver observer;
- RegisterAndWait(&observer, NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
- Source<ExtensionAction>(browser_action));
+ SimpleNotificationObserver<ExtensionAction>
+ observer(NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
+ browser_action);
}
void WaitForLoadStop(NavigationController* controller) {
- TestNotificationObserver observer;
- RegisterAndWait(&observer, NotificationType::LOAD_STOP,
- Source<NavigationController>(controller));
+ SimpleNotificationObserver<NavigationController>
+ new_tab_observer(NotificationType::LOAD_STOP, controller);
}
Browser* WaitForNewBrowser() {
- TestNotificationObserver observer;
- RegisterAndWait(&observer, NotificationType::BROWSER_WINDOW_READY,
- NotificationService::AllSources());
- return Source<Browser>(observer.source()).ptr();
+ SimpleNotificationObserverWithSource<Browser> observer(
+ NotificationType::BROWSER_WINDOW_READY,
+ NotificationService::AllSources());
+ return observer.source();
}
void OpenURLOffTheRecord(Profile* profile, const GURL& url) {
@@ -517,30 +571,28 @@ void WaitForDownloadCount(DownloadManager* download_manager, size_t count) {
}
AppModalDialog* WaitForAppModalDialog() {
- TestNotificationObserver observer;
- RegisterAndWait(&observer, NotificationType::APP_MODAL_DIALOG_SHOWN,
- NotificationService::AllSources());
- return Source<AppModalDialog>(observer.source()).ptr();
+ SimpleNotificationObserverWithSource<AppModalDialog> observer(
+ NotificationType::APP_MODAL_DIALOG_SHOWN,
+ NotificationService::AllSources());
+ return observer.source();
}
void CrashTab(TabContents* tab) {
RenderProcessHost* rph = tab->render_view_host()->process();
base::KillProcess(rph->GetHandle(), 0, false);
- TestNotificationObserver observer;
- RegisterAndWait(&observer, NotificationType::RENDERER_PROCESS_CLOSED,
- Source<RenderProcessHost>(rph));
+ SimpleNotificationObserver<RenderProcessHost>
+ crash_observer(NotificationType::RENDERER_PROCESS_CLOSED, rph);
}
void WaitForFocusChange(RenderViewHost* rvh) {
- TestNotificationObserver observer;
- RegisterAndWait(&observer, NotificationType::FOCUS_CHANGED_IN_PAGE,
- Source<RenderViewHost>(rvh));
+ SimpleNotificationObserver<RenderViewHost>
+ focus_observer(NotificationType::FOCUS_CHANGED_IN_PAGE, rvh);
}
void WaitForFocusInBrowser(Browser* browser) {
- TestNotificationObserver observer;
- RegisterAndWait(&observer, NotificationType::FOCUS_RETURNED_TO_BROWSER,
- Source<Browser>(browser));
+ SimpleNotificationObserver<Browser>
+ focus_observer(NotificationType::FOCUS_RETURNED_TO_BROWSER,
+ browser);
}
std::string WaitForLanguageDetection(TabContents* tab) {
@@ -557,16 +609,10 @@ int FindInPage(TabContents* tab_contents, const string16& search_string,
return observer.number_of_matches();
}
-void WaitForNotification(NotificationType::Type type) {
- TestNotificationObserver observer;
- RegisterAndWait(&observer, type, NotificationService::AllSources());
-}
-
-void RegisterAndWait(NotificationObserver* observer,
- NotificationType::Type type,
- const NotificationSource& source) {
+void RegisterAndWait(NotificationType::Type type,
+ NotificationObserver* observer) {
NotificationRegistrar registrar;
- registrar.Add(observer, type, source);
+ registrar.Add(observer, type, NotificationService::AllSources());
RunMessageLoop();
}
diff --git a/chrome/test/ui_test_utils.h b/chrome/test/ui_test_utils.h
index 9543f9d..b247a19 100644
--- a/chrome/test/ui_test_utils.h
+++ b/chrome/test/ui_test_utils.h
@@ -175,14 +175,10 @@ bool IsViewFocused(const Browser* browser, ViewID vid);
// Simulates a mouse click on a View in the browser.
void ClickOnView(const Browser* browser, ViewID vid);
-// 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);
+// 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);
// Run a message loop only for the specified amount of time.
class TimedMessageLoopRunner {
@@ -256,34 +252,6 @@ 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: