1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/devtools/devtools_window_testing.h"
7 #include "base/lazy_instance.h"
8 #include "chrome/browser/devtools/devtools_window.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/browser_window.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "content/public/browser/devtools_agent_host.h"
13 #include "content/public/browser/render_view_host.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/test/test_utils.h"
19 typedef std::vector
<DevToolsWindowTesting
*> DevToolsWindowTestings
;
20 base::LazyInstance
<DevToolsWindowTestings
>::Leaky g_instances
=
21 LAZY_INSTANCE_INITIALIZER
;
25 DevToolsWindowTesting::DevToolsWindowTesting(DevToolsWindow
* window
)
26 : devtools_window_(window
) {
28 window
->close_callback_
=
29 base::Bind(&DevToolsWindowTesting::WindowClosed
, window
);
30 g_instances
.Get().push_back(this);
33 DevToolsWindowTesting::~DevToolsWindowTesting() {
34 DevToolsWindowTestings
* instances
= g_instances
.Pointer();
35 DevToolsWindowTestings::iterator
it(
36 std::find(instances
->begin(), instances
->end(), this));
37 DCHECK(it
!= instances
->end());
39 if (!close_callback_
.is_null()) {
40 close_callback_
.Run();
41 close_callback_
= base::Closure();
46 DevToolsWindowTesting
* DevToolsWindowTesting::Get(DevToolsWindow
* window
) {
47 DevToolsWindowTesting
* testing
= DevToolsWindowTesting::Find(window
);
49 testing
= new DevToolsWindowTesting(window
);
54 DevToolsWindowTesting
* DevToolsWindowTesting::Find(DevToolsWindow
* window
) {
55 if (g_instances
== NULL
)
57 DevToolsWindowTestings
* instances
= g_instances
.Pointer();
58 for (DevToolsWindowTestings::iterator
it(instances
->begin());
59 it
!= instances
->end();
61 if ((*it
)->devtools_window_
== window
)
67 Browser
* DevToolsWindowTesting::browser() {
68 return devtools_window_
->browser_
;
71 content::WebContents
* DevToolsWindowTesting::main_web_contents() {
72 return devtools_window_
->main_web_contents_
;
75 content::WebContents
* DevToolsWindowTesting::toolbox_web_contents() {
76 return devtools_window_
->toolbox_web_contents_
;
79 void DevToolsWindowTesting::SetInspectedPageBounds(const gfx::Rect
& bounds
) {
80 devtools_window_
->SetInspectedPageBounds(bounds
);
83 void DevToolsWindowTesting::SetCloseCallback(const base::Closure
& closure
) {
84 close_callback_
= closure
;
88 void DevToolsWindowTesting::WindowClosed(DevToolsWindow
* window
) {
89 DevToolsWindowTesting
* testing
= DevToolsWindowTesting::Find(window
);
95 void DevToolsWindowTesting::WaitForDevToolsWindowLoad(DevToolsWindow
* window
) {
96 scoped_refptr
<content::MessageLoopRunner
> runner
=
97 new content::MessageLoopRunner
;
98 window
->SetLoadCompletedCallback(runner
->QuitClosure());
103 DevToolsWindow
* DevToolsWindowTesting::OpenDevToolsWindowSync(
104 content::WebContents
* inspected_web_contents
,
106 std::string settings
= is_docked
?
107 "{\"currentDockState\":\"\\\"bottom\\\"\"}" :
108 "{\"currentDockState\":\"\\\"undocked\\\"\"}";
109 scoped_refptr
<content::DevToolsAgentHost
> agent(
110 content::DevToolsAgentHost::GetOrCreateFor(inspected_web_contents
));
111 DevToolsWindow::ToggleDevToolsWindow(
112 inspected_web_contents
, true, DevToolsToggleAction::Show(), settings
);
113 DevToolsWindow
* window
= DevToolsWindow::FindDevToolsWindow(agent
.get());
114 WaitForDevToolsWindowLoad(window
);
119 DevToolsWindow
* DevToolsWindowTesting::OpenDevToolsWindowSync(
122 return OpenDevToolsWindowSync(
123 browser
->tab_strip_model()->GetActiveWebContents(), is_docked
);
127 DevToolsWindow
* DevToolsWindowTesting::OpenDevToolsWindowForWorkerSync(
128 Profile
* profile
, content::DevToolsAgentHost
* worker_agent
) {
129 DevToolsWindow::OpenDevToolsWindowForWorker(
130 profile
, worker_agent
);
131 DevToolsWindow
* window
= DevToolsWindow::FindDevToolsWindow(worker_agent
);
132 WaitForDevToolsWindowLoad(window
);
137 void DevToolsWindowTesting::CloseDevToolsWindow(
138 DevToolsWindow
* window
) {
139 if (window
->is_docked_
) {
140 window
->CloseWindow();
142 window
->browser_
->window()->Close();
147 void DevToolsWindowTesting::CloseDevToolsWindowSync(
148 DevToolsWindow
* window
) {
149 scoped_refptr
<content::MessageLoopRunner
> runner
=
150 new content::MessageLoopRunner
;
151 DevToolsWindowTesting::Get(window
)->SetCloseCallback(runner
->QuitClosure());
152 CloseDevToolsWindow(window
);