summaryrefslogtreecommitdiffstats
path: root/chrome/browser
diff options
context:
space:
mode:
authoryurys@google.com <yurys@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-03-17 12:05:48 +0000
committeryurys@google.com <yurys@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-03-17 12:05:48 +0000
commitf85d541dfff6b40ede739d3c0c3094ba420bafa2 (patch)
tree05fdc391e65feef0bb971230da16e5cc65ed1d51 /chrome/browser
parentea29a8a5bebf9c4fcf2448c6e4ddeafa62eba1d8 (diff)
downloadchromium_src-f85d541dfff6b40ede739d3c0c3094ba420bafa2.zip
chromium_src-f85d541dfff6b40ede739d3c0c3094ba420bafa2.tar.gz
chromium_src-f85d541dfff6b40ede739d3c0c3094ba420bafa2.tar.bz2
Initial unit tests implementation.
Review URL: http://codereview.chromium.org/42233 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@11855 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r--chrome/browser/browser_process_impl.cc3
-rw-r--r--chrome/browser/debugger/devtools_manager.cc30
-rw-r--r--chrome/browser/debugger/devtools_manager.h12
-rw-r--r--chrome/browser/debugger/devtools_manager_unittest.cc110
-rw-r--r--chrome/browser/debugger/devtools_window.h9
5 files changed, 151 insertions, 13 deletions
diff --git a/chrome/browser/browser_process_impl.cc b/chrome/browser/browser_process_impl.cc
index 5e1d9f3..1e4d0aa 100644
--- a/chrome/browser/browser_process_impl.cc
+++ b/chrome/browser/browser_process_impl.cc
@@ -363,7 +363,8 @@ void BrowserProcessImpl::CreateDebuggerWrapper(int port) {
void BrowserProcessImpl::CreateDevToolsManager() {
DCHECK(!devtools_manager_.get());
created_devtools_manager_ = true;
- devtools_manager_.reset(new DevToolsManager());
+ devtools_manager_.reset(new DevToolsManager(
+ NULL /*Create DevToolsWindows directly*/));
}
void BrowserProcessImpl::CreateAcceleratorHandler() {
diff --git a/chrome/browser/debugger/devtools_manager.cc b/chrome/browser/debugger/devtools_manager.cc
index 41d0f7a..8a4edf4 100644
--- a/chrome/browser/debugger/devtools_manager.cc
+++ b/chrome/browser/debugger/devtools_manager.cc
@@ -4,10 +4,8 @@
#include "chrome/browser/debugger/devtools_manager.h"
-#include "chrome/browser/browser_process.h"
#include "chrome/browser/debugger/devtools_window.h"
#include "chrome/browser/renderer_host/render_view_host.h"
-#include "chrome/browser/tab_contents/render_view_host_manager.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/tab_contents/web_contents.h"
#include "chrome/common/notification_registrar.h"
@@ -17,8 +15,10 @@
class DevToolsInstanceDescriptorImpl : public DevToolsInstanceDescriptor {
public:
explicit DevToolsInstanceDescriptorImpl(
+ DevToolsManager* manager,
NavigationController* navigation_controller)
- : navigation_controller_(navigation_controller),
+ : manager_(manager),
+ navigation_controller_(navigation_controller),
devtools_host_(NULL),
devtools_window_(NULL) {
}
@@ -33,11 +33,7 @@ class DevToolsInstanceDescriptorImpl : public DevToolsInstanceDescriptor {
}
virtual void Destroy() {
- DevToolsManager* manager = g_browser_process->devtools_manager();
- DCHECK(manager);
- if (manager) {
- manager->RemoveDescriptor(this);
- }
+ manager_->RemoveDescriptor(this);
delete this;
}
@@ -54,6 +50,7 @@ class DevToolsInstanceDescriptorImpl : public DevToolsInstanceDescriptor {
}
private:
+ DevToolsManager* manager_;
NavigationController* navigation_controller_;
RenderViewHost* devtools_host_;
DevToolsWindow* devtools_window_;
@@ -61,7 +58,9 @@ class DevToolsInstanceDescriptorImpl : public DevToolsInstanceDescriptor {
DISALLOW_COPY_AND_ASSIGN(DevToolsInstanceDescriptorImpl);
};
-DevToolsManager::DevToolsManager() : web_contents_listeners_(NULL) {
+DevToolsManager::DevToolsManager(DevToolsWindowFactory* factory)
+ : web_contents_listeners_(NULL),
+ devtools_window_factory_(factory) {
}
DevToolsManager::~DevToolsManager() {
@@ -69,6 +68,15 @@ DevToolsManager::~DevToolsManager() {
"All devtools windows must alredy have been closed.";
}
+DevToolsWindow* DevToolsManager::CreateDevToolsWindow(
+ DevToolsInstanceDescriptor* descriptor) {
+ if (devtools_window_factory_) {
+ return devtools_window_factory_->CreateDevToolsWindow(descriptor);
+ } else {
+ return DevToolsWindow::Create(descriptor);
+ }
+}
+
void DevToolsManager::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
@@ -100,12 +108,12 @@ void DevToolsManager::ShowDevToolsForWebContents(WebContents* web_contents) {
desc = it->second;
window = desc->devtools_window();
} else {
- desc = new DevToolsInstanceDescriptorImpl(navigation_controller);
+ desc = new DevToolsInstanceDescriptorImpl(this, navigation_controller);
navcontroller_to_descriptor_[navigation_controller] = desc;
StartListening(navigation_controller);
- window = DevToolsWindow::Create(desc);
+ window = CreateDevToolsWindow(desc);
}
window->Show();
diff --git a/chrome/browser/debugger/devtools_manager.h b/chrome/browser/debugger/devtools_manager.h
index ebacc5e..ce6ee73 100644
--- a/chrome/browser/debugger/devtools_manager.h
+++ b/chrome/browser/debugger/devtools_manager.h
@@ -15,8 +15,10 @@ namespace IPC {
class Message;
}
+class DevToolsInstanceDescriptor;
class DevToolsInstanceDescriptorImpl;
class DevToolsWindow;
+class DevToolsWindowFactory;
class NavigationController;
class NotificationRegistrar;
class RenderViewHost;
@@ -26,7 +28,9 @@ class WebContents;
// messages between developer tools clients and agents.
class DevToolsManager : public NotificationObserver {
public:
- DevToolsManager();
+ // If the factory is NULL, this will create DevToolsWindow objects using
+ // DevToolsWindow::Create static method.
+ DevToolsManager(DevToolsWindowFactory* factory);
virtual ~DevToolsManager();
// Opend developer tools window for |web_contents|. If there is already
@@ -43,6 +47,11 @@ class DevToolsManager : public NotificationObserver {
const IPC::Message& message);
private:
+ // Creates a DevToolsWindow using devtools_window_factory_ or by calling
+ // DevToolsWindow::Create, if the factory is NULL. All DevToolsWindows should
+ // be created by means of this method.
+ DevToolsWindow* CreateDevToolsWindow(DevToolsInstanceDescriptor* descriptor);
+
// NotificationObserver override.
virtual void Observe(NotificationType type,
const NotificationSource& source,
@@ -72,6 +81,7 @@ class DevToolsManager : public NotificationObserver {
typedef std::map<NavigationController*,
DevToolsInstanceDescriptorImpl*> DescriptorMap;
DescriptorMap navcontroller_to_descriptor_;
+ DevToolsWindowFactory* devtools_window_factory_;
DISALLOW_COPY_AND_ASSIGN(DevToolsManager);
};
diff --git a/chrome/browser/debugger/devtools_manager_unittest.cc b/chrome/browser/debugger/devtools_manager_unittest.cc
new file mode 100644
index 0000000..a43f95a
--- /dev/null
+++ b/chrome/browser/debugger/devtools_manager_unittest.cc
@@ -0,0 +1,110 @@
+// Copyright (c) 2009 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 "base/basictypes.h"
+#include "chrome/browser/debugger/devtools_manager.h"
+#include "chrome/browser/debugger/devtools_window.h"
+#include "chrome/browser/renderer_host/test_render_view_host.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "chrome/common/render_messages.h"
+
+namespace {
+
+class TestDevToolsWindow : public DevToolsWindow {
+ public:
+ TestDevToolsWindow(DevToolsInstanceDescriptor* descriptor)
+ : descriptor_(descriptor),
+ shown_(false),
+ closed_(false) {
+ descriptor->SetDevToolsWindow(this);
+ }
+
+ virtual ~TestDevToolsWindow() {
+ EXPECT_EQ(shown_, closed_);
+ }
+
+ virtual void Show() {
+ open_counter++;
+ shown_ = true;
+ }
+
+ virtual void Close() {
+ EXPECT_TRUE(shown_);
+ close_counter++;
+ descriptor_->Destroy();
+ closed_ = true;
+ }
+
+ static void ResetCounters() {
+ open_counter = 0;
+ close_counter = 0;
+ }
+
+ static int open_counter;
+ static int close_counter;
+
+ private:
+ DevToolsInstanceDescriptor* descriptor_;
+ bool shown_;
+ bool closed_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestDevToolsWindow);
+};
+
+int TestDevToolsWindow::open_counter = 0;
+int TestDevToolsWindow::close_counter = 0;
+
+
+class TestDevToolsWindowFactory : public DevToolsWindowFactory {
+ public:
+ TestDevToolsWindowFactory() : DevToolsWindowFactory(),
+ last_created_window(NULL) {}
+ virtual ~TestDevToolsWindowFactory() {}
+
+ virtual DevToolsWindow* CreateDevToolsWindow(
+ DevToolsInstanceDescriptor* descriptor) {
+ last_created_window = new TestDevToolsWindow(descriptor);
+ return last_created_window;
+ }
+
+ DevToolsWindow* last_created_window;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(TestDevToolsWindowFactory);
+};
+
+} // namespace
+
+class DevToolsManagerTest : public RenderViewHostTestHarness {
+ public:
+ DevToolsManagerTest() : RenderViewHostTestHarness() {
+ }
+
+ protected:
+ virtual void SetUp() {
+ RenderViewHostTestHarness::SetUp();
+ TestDevToolsWindow::ResetCounters();
+ }
+};
+
+TEST_F(DevToolsManagerTest, OpenAndManuallyCloseDevToolsWindow) {
+ TestDevToolsWindowFactory window_factory;
+ DevToolsManager manager(&window_factory);
+
+ manager.ShowDevToolsForWebContents(contents());
+ EXPECT_EQ(TestDevToolsWindow::open_counter, 1);
+ EXPECT_EQ(TestDevToolsWindow::close_counter, 0);
+
+ DevToolsWindow* window = window_factory.last_created_window;
+ // Test that same devtools window is used.
+ manager.ShowDevToolsForWebContents(contents());
+ // Check that no new windows were created.
+ EXPECT_TRUE(window == window_factory.last_created_window);
+ EXPECT_EQ(TestDevToolsWindow::open_counter, 2);
+ EXPECT_EQ(TestDevToolsWindow::close_counter, 0);
+
+ window->Close();
+ EXPECT_EQ(TestDevToolsWindow::open_counter, 2);
+ EXPECT_EQ(TestDevToolsWindow::close_counter, 1);
+}
diff --git a/chrome/browser/debugger/devtools_window.h b/chrome/browser/debugger/devtools_window.h
index fb6379bca..257c44d 100644
--- a/chrome/browser/debugger/devtools_window.h
+++ b/chrome/browser/debugger/devtools_window.h
@@ -25,4 +25,13 @@ class DevToolsWindow {
DISALLOW_COPY_AND_ASSIGN(DevToolsWindow);
};
+// Factory for creating DevToolsWindows. Useful for unit tests.
+class DevToolsWindowFactory {
+ public:
+ virtual ~DevToolsWindowFactory() {}
+ virtual DevToolsWindow* CreateDevToolsWindow(
+ DevToolsInstanceDescriptor* descriptor) = 0;
+};
+
+
#endif // CHROME_BROWSER_DEBUGGER_DEV_TOOLS_WINDOW_H_