Move Webstore URL concepts to //extensions and out
[chromium-blink-merge.git] / chrome / browser / devtools / devtools_window_testing.cc
blob3df89b45df2d5d7c003771309d48924d4ab0fc50
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"
17 namespace {
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) {
27 DCHECK(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());
38 instances->erase(it);
39 if (!close_callback_.is_null()) {
40 close_callback_.Run();
41 close_callback_ = base::Closure();
45 // static
46 DevToolsWindowTesting* DevToolsWindowTesting::Get(DevToolsWindow* window) {
47 DevToolsWindowTesting* testing = DevToolsWindowTesting::Find(window);
48 if (!testing)
49 testing = new DevToolsWindowTesting(window);
50 return testing;
53 // static
54 DevToolsWindowTesting* DevToolsWindowTesting::Find(DevToolsWindow* window) {
55 if (g_instances == NULL)
56 return NULL;
57 DevToolsWindowTestings* instances = g_instances.Pointer();
58 for (DevToolsWindowTestings::iterator it(instances->begin());
59 it != instances->end();
60 ++it) {
61 if ((*it)->devtools_window_ == window)
62 return *it;
64 return NULL;
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;
87 // static
88 void DevToolsWindowTesting::WindowClosed(DevToolsWindow* window) {
89 DevToolsWindowTesting* testing = DevToolsWindowTesting::Find(window);
90 if (testing)
91 delete testing;
94 // static
95 void DevToolsWindowTesting::WaitForDevToolsWindowLoad(DevToolsWindow* window) {
96 scoped_refptr<content::MessageLoopRunner> runner =
97 new content::MessageLoopRunner;
98 window->SetLoadCompletedCallback(runner->QuitClosure());
99 runner->Run();
102 // static
103 DevToolsWindow* DevToolsWindowTesting::OpenDevToolsWindowSync(
104 content::WebContents* inspected_web_contents,
105 bool is_docked) {
106 std::string settings = is_docked ?
107 "{\"currentDockState\":\"\\\"bottom\\\"\"}" :
108 "{\"currentDockState\":\"\\\"undocked\\\"\"}";
109 DevToolsWindow* window = DevToolsWindow::ToggleDevToolsWindow(
110 inspected_web_contents, true, DevToolsToggleAction::Show(), settings);
111 WaitForDevToolsWindowLoad(window);
112 return window;
115 // static
116 DevToolsWindow* DevToolsWindowTesting::OpenDevToolsWindowSync(
117 Browser* browser,
118 bool is_docked) {
119 return OpenDevToolsWindowSync(
120 browser->tab_strip_model()->GetActiveWebContents(), is_docked);
123 // static
124 DevToolsWindow* DevToolsWindowTesting::OpenDevToolsWindowForWorkerSync(
125 Profile* profile, content::DevToolsAgentHost* worker_agent) {
126 DevToolsWindow* window = DevToolsWindow::OpenDevToolsWindowForWorker(
127 profile, worker_agent);
128 WaitForDevToolsWindowLoad(window);
129 return window;
132 // static
133 void DevToolsWindowTesting::CloseDevToolsWindow(
134 DevToolsWindow* window) {
135 if (window->is_docked_) {
136 window->CloseWindow();
137 } else {
138 window->browser_->window()->Close();
142 // static
143 void DevToolsWindowTesting::CloseDevToolsWindowSync(
144 DevToolsWindow* window) {
145 scoped_refptr<content::MessageLoopRunner> runner =
146 new content::MessageLoopRunner;
147 DevToolsWindowTesting::Get(window)->SetCloseCallback(runner->QuitClosure());
148 CloseDevToolsWindow(window);
149 runner->Run();