summaryrefslogtreecommitdiffstats
path: root/chrome/test/ui_test_utils.cc
diff options
context:
space:
mode:
authorisherman@chromium.org <isherman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-09-28 22:28:30 +0000
committerisherman@chromium.org <isherman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-09-28 22:28:30 +0000
commita6e602f4041198222584e5f449f145d0b886dba5 (patch)
treeaf3ac167919a62663e53f52453f98a973edac9f5 /chrome/test/ui_test_utils.cc
parent25da2f324b3ff67bde74b4d339429c582e54147a (diff)
downloadchromium_src-a6e602f4041198222584e5f449f145d0b886dba5.zip
chromium_src-a6e602f4041198222584e5f449f145d0b886dba5.tar.gz
chromium_src-a6e602f4041198222584e5f449f145d0b886dba5.tar.bz2
Refactoring ui_test_utils code
BUG=none TEST=all tests that used to pass should continue to pass Review URL: http://codereview.chromium.org/3402030 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@60858 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/test/ui_test_utils.cc')
-rw-r--r--chrome/test/ui_test_utils.cc98
1 files changed, 93 insertions, 5 deletions
diff --git a/chrome/test/ui_test_utils.cc b/chrome/test/ui_test_utils.cc
index ad0d3a7..964ae60 100644
--- a/chrome/test/ui_test_utils.cc
+++ b/chrome/test/ui_test_utils.cc
@@ -17,6 +17,7 @@
#include "chrome/browser/automation/ui_controls.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/browser_list.h"
+#include "chrome/browser/browser_window.h"
#include "chrome/browser/dom_operation_notification_details.h"
#include "chrome/browser/download/download_item.h"
#include "chrome/browser/download/download_manager.h"
@@ -28,8 +29,7 @@
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/extensions/extension_action.h"
-#include "chrome/common/notification_registrar.h"
-#include "chrome/common/notification_service.h"
+#include "chrome/common/notification_type.h"
#include "chrome/test/automation/javascript_execution_controller.h"
#include "chrome/test/bookmark_load_observer.h"
#if defined(TOOLKIT_VIEWS)
@@ -527,13 +527,13 @@ int FindInPage(TabContents* tab_contents, const string16& search_string,
return observer.number_of_matches();
}
-void WaitForNotification(NotificationType::Type type) {
+void WaitForNotification(NotificationType type) {
TestNotificationObserver observer;
RegisterAndWait(&observer, type, NotificationService::AllSources());
}
void RegisterAndWait(NotificationObserver* observer,
- NotificationType::Type type,
+ NotificationType type,
const NotificationSource& source) {
NotificationRegistrar registrar;
registrar.Add(observer, type, source);
@@ -550,12 +550,34 @@ void WaitForBookmarkModelToLoad(BookmarkModel* model) {
ASSERT_TRUE(model->IsLoaded());
}
-bool SendKeyPressSync(gfx::NativeWindow window,
+bool GetNativeWindow(const Browser* browser, gfx::NativeWindow* native_window) {
+ BrowserWindow* window = browser->window();
+ if (!window)
+ return false;
+
+ *native_window = window->GetNativeHandle();
+ return *native_window;
+}
+
+bool BringBrowserWindowToFront(const Browser* browser) {
+ gfx::NativeWindow window = NULL;
+ if (!GetNativeWindow(browser, &window))
+ return false;
+
+ ui_test_utils::ShowAndFocusNativeWindow(window);
+ return true;
+}
+
+bool SendKeyPressSync(const Browser* browser,
app::KeyboardCode key,
bool control,
bool shift,
bool alt,
bool command) {
+ gfx::NativeWindow window = NULL;
+ if (!GetNativeWindow(browser, &window))
+ return false;
+
if (!ui_controls::SendKeyPressNotifyWhenDone(
window, key, control, shift, alt, command,
new MessageLoop::QuitTask())) {
@@ -569,6 +591,24 @@ bool SendKeyPressSync(gfx::NativeWindow window,
return !testing::Test::HasFatalFailure();
}
+bool SendKeyPressAndWait(const Browser* browser,
+ app::KeyboardCode key,
+ bool control,
+ bool shift,
+ bool alt,
+ bool command,
+ NotificationType type,
+ const NotificationSource& source) {
+ WindowedNotificationObserver observer(type, source);
+
+ if (!SendKeyPressSync(browser, key, control, shift, alt, command))
+ return false;
+
+ observer.Wait();
+ return !testing::Test::HasFatalFailure();
+}
+
+
TimedMessageLoopRunner::TimedMessageLoopRunner()
: loop_(new MessageLoopForUI()),
owned_(true),
@@ -681,4 +721,52 @@ TestWebSocketServer::~TestWebSocketServer() {
base::LaunchApp(*cmd_line.get(), true, false, NULL);
}
+WindowedNotificationObserver::WindowedNotificationObserver(
+ NotificationType notification_type,
+ const NotificationSource& source)
+ : seen_(false),
+ running_(false),
+ waiting_for_(source) {
+ registrar_.Add(this, notification_type, waiting_for_);
+}
+
+void WindowedNotificationObserver::Wait() {
+ if (waiting_for_ == NotificationService::AllSources()) {
+ LOG(FATAL) << "Wait called when monitoring all sources. You must use "
+ << "WaitFor in this case.";
+ }
+
+ if (seen_)
+ return;
+
+ running_ = true;
+ ui_test_utils::RunMessageLoop();
+}
+
+void WindowedNotificationObserver::WaitFor(const NotificationSource& source) {
+ if (waiting_for_ != NotificationService::AllSources()) {
+ LOG(FATAL) << "WaitFor called when already waiting on a specific source."
+ << "Use Wait in this case.";
+ }
+
+ waiting_for_ = source;
+ if (sources_seen_.count(waiting_for_.map_key()) > 0)
+ return;
+
+ running_ = true;
+ ui_test_utils::RunMessageLoop();
+}
+
+void WindowedNotificationObserver::Observe(NotificationType type,
+ const NotificationSource& source,
+ const NotificationDetails& details) {
+ if (waiting_for_ == source) {
+ seen_ = true;
+ if (running_)
+ MessageLoopForUI::current()->Quit();
+ } else {
+ sources_seen_.insert(source.map_key());
+ }
+}
+
} // namespace ui_test_utils