summaryrefslogtreecommitdiffstats
path: root/chrome/test/ui_test_utils.cc
diff options
context:
space:
mode:
authorsky@google.com <sky@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-01-30 00:40:43 +0000
committersky@google.com <sky@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-01-30 00:40:43 +0000
commitd4515eb94ad33ac2ab9bfd1094c8e1edd1eefa1b (patch)
treeadd4a98df5bcdbb3ca0e1f4471c77f453d52a1f4 /chrome/test/ui_test_utils.cc
parent0815731ac884b41853725e51e26b0697449747b4 (diff)
downloadchromium_src-d4515eb94ad33ac2ab9bfd1094c8e1edd1eefa1b.zip
chromium_src-d4515eb94ad33ac2ab9bfd1094c8e1edd1eefa1b.tar.gz
chromium_src-d4515eb94ad33ac2ab9bfd1094c8e1edd1eefa1b.tar.bz2
Provides the ability to write a unit test that brings up a browser. As
a proof of concept I converted FindInPageControllerTest.FindInPageFrames over to this. See the description in InProcessBrowserTest for how it all works. BUG=none TEST=none Review URL: http://codereview.chromium.org/19644 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8934 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/test/ui_test_utils.cc')
-rw-r--r--chrome/test/ui_test_utils.cc77
1 files changed, 77 insertions, 0 deletions
diff --git a/chrome/test/ui_test_utils.cc b/chrome/test/ui_test_utils.cc
new file mode 100644
index 0000000..07016f7
--- /dev/null
+++ b/chrome/test/ui_test_utils.cc
@@ -0,0 +1,77 @@
+// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/test/ui_test_utils.h"
+
+#include "base/message_loop.h"
+#include "chrome/browser/browser.h"
+#include "chrome/browser/tab_contents/navigation_controller.h"
+#include "chrome/browser/tab_contents/web_contents.h"
+#include "chrome/common/notification_registrar.h"
+#include "chrome/common/notification_service.h"
+#include "googleurl/src/gurl.h"
+
+namespace ui_test_utils {
+
+namespace {
+
+// Used to block until a navigation completes.
+class NavigationNotificationObserver : public NotificationObserver {
+ public:
+ explicit NavigationNotificationObserver(NavigationController* controller)
+ : navigation_started_(false) {
+ registrar_.Add(this, NOTIFY_NAV_ENTRY_COMMITTED,
+ Source<NavigationController>(controller));
+ registrar_.Add(this, NOTIFY_LOAD_START,
+ Source<NavigationController>(controller));
+ registrar_.Add(this, NOTIFY_LOAD_STOP,
+ Source<NavigationController>(controller));
+ RunMessageLoop();
+ }
+
+ virtual void Observe(NotificationType type,
+ const NotificationSource& source,
+ const NotificationDetails& details) {
+ if (type == NOTIFY_NAV_ENTRY_COMMITTED || type == NOTIFY_LOAD_START) {
+ navigation_started_ = true;
+ } else if (type == NOTIFY_LOAD_STOP) {
+ if (navigation_started_) {
+ navigation_started_ = false;
+ MessageLoopForUI::current()->Quit();
+ }
+ }
+ }
+
+ private:
+ NotificationRegistrar registrar_;
+
+ // If true the navigation has started.
+ bool navigation_started_;
+
+ DISALLOW_COPY_AND_ASSIGN(NavigationNotificationObserver);
+};
+
+} // namespace
+
+void RunMessageLoop() {
+ MessageLoopForUI* loop = MessageLoopForUI::current();
+ bool did_allow_task_nesting = loop->NestableTasksAllowed();
+ loop->SetNestableTasksAllowed(true);
+ loop->Run(NULL);
+ loop->SetNestableTasksAllowed(did_allow_task_nesting);
+}
+
+void WaitForNavigation(NavigationController* controller) {
+ NavigationNotificationObserver observer(controller);
+}
+
+void NavigateToURL(Browser* browser, const GURL& url) {
+ NavigationController* controller =
+ browser->GetSelectedTabContents()->controller();
+ browser->OpenURLFromTab(browser->GetSelectedTabContents(), url, GURL(),
+ CURRENT_TAB, PageTransition::TYPED);
+ WaitForNavigation(controller);
+}
+
+} // namespace ui_test_utils