Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / chrome / browser / devtools / devtools_window_testing.cc
blob26fa6e3d7ad74dbe0395c5a2e1573d9b05e5054f
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 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);
115 return window;
118 // static
119 DevToolsWindow* DevToolsWindowTesting::OpenDevToolsWindowSync(
120 Browser* browser,
121 bool is_docked) {
122 return OpenDevToolsWindowSync(
123 browser->tab_strip_model()->GetActiveWebContents(), is_docked);
126 // static
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);
133 return window;
136 // static
137 void DevToolsWindowTesting::CloseDevToolsWindow(
138 DevToolsWindow* window) {
139 if (window->is_docked_) {
140 window->CloseWindow();
141 } else {
142 window->browser_->window()->Close();
146 // static
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);
153 runner->Run();