OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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. | |
4 | |
5 #include "base/command_line.h" | |
6 #include "chrome/browser/debugger/devtools_manager.h" | |
7 #include "chrome/browser/ui/browser.h" | |
8 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | |
9 #include "chrome/common/chrome_switches.h" | |
10 #include "chrome/common/url_constants.h" | |
11 #include "chrome/test/in_process_browser_test.h" | |
12 #include "chrome/test/ui_test_utils.h" | |
13 #include "content/browser/renderer_host/render_process_host.h" | |
14 #include "content/browser/renderer_host/render_view_host.h" | |
15 #include "content/browser/renderer_host/render_widget_host.h" | |
16 #include "content/browser/site_instance.h" | |
17 #include "content/browser/tab_contents/tab_contents.h" | |
18 | |
19 class RenderProcessHostTest : public InProcessBrowserTest { | |
20 public: | |
21 RenderProcessHostTest() { | |
22 EnableDOMAutomation(); | |
23 } | |
24 | |
25 int RenderProcessHostCount() { | |
26 RenderProcessHost::iterator hosts = RenderProcessHost::AllHostsIterator(); | |
27 int count = 0; | |
28 while (!hosts.IsAtEnd()) { | |
29 if (hosts.GetCurrentValue()->HasConnection()) | |
30 count++; | |
31 hosts.Advance(); | |
32 } | |
33 return count; | |
34 } | |
35 | |
36 RenderViewHost* FindFirstDevToolsHost() { | |
37 RenderProcessHost::iterator hosts = RenderProcessHost::AllHostsIterator(); | |
38 for (; !hosts.IsAtEnd(); hosts.Advance()) { | |
39 RenderProcessHost* render_process_host = hosts.GetCurrentValue(); | |
40 DCHECK(render_process_host); | |
41 if (!render_process_host->HasConnection()) | |
42 continue; | |
43 RenderProcessHost::listeners_iterator iter( | |
44 render_process_host->ListenersIterator()); | |
45 for (; !iter.IsAtEnd(); iter.Advance()) { | |
46 const RenderWidgetHost* widget = | |
47 static_cast<const RenderWidgetHost*>(iter.GetCurrentValue()); | |
48 DCHECK(widget); | |
49 if (!widget || !widget->IsRenderView()) | |
50 continue; | |
51 RenderViewHost* host = const_cast<RenderViewHost*>( | |
52 static_cast<const RenderViewHost*>(widget)); | |
53 RenderViewHostDelegate* host_delegate = host->delegate(); | |
54 GURL url = host_delegate->GetURL(); | |
55 if (url.SchemeIs(chrome::kChromeDevToolsScheme)) | |
56 return host; | |
57 } | |
58 } | |
59 return NULL; | |
60 } | |
61 }; | |
62 | |
63 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, ProcessPerTab) { | |
64 // Set max renderers to 1 to force running out of processes. | |
65 RenderProcessHost::SetMaxRendererProcessCount(1); | |
66 | |
67 CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); | |
68 parsed_command_line.AppendSwitch(switches::kProcessPerTab); | |
69 | |
70 int tab_count = 1; | |
71 int host_count = 1; | |
72 | |
73 // Change the first tab to be the new tab page (TYPE_WEBUI). | |
74 GURL newtab(chrome::kChromeUINewTabURL); | |
75 ui_test_utils::NavigateToURL(browser(), newtab); | |
76 EXPECT_EQ(tab_count, browser()->tab_count()); | |
77 EXPECT_EQ(host_count, RenderProcessHostCount()); | |
78 | |
79 // Create a new TYPE_TABBED tab. It should be in its own process. | |
80 GURL page1("data:text/html,hello world1"); | |
81 browser()->ShowSingletonTab(page1); | |
82 if (browser()->tab_count() == tab_count) | |
83 ui_test_utils::WaitForNewTab(browser()); | |
84 tab_count++; | |
85 host_count++; | |
86 EXPECT_EQ(tab_count, browser()->tab_count()); | |
87 EXPECT_EQ(host_count, RenderProcessHostCount()); | |
88 | |
89 // Create another TYPE_TABBED tab. It should share the previous process. | |
90 GURL page2("data:text/html,hello world2"); | |
91 browser()->ShowSingletonTab(page2); | |
92 if (browser()->tab_count() == tab_count) | |
93 ui_test_utils::WaitForNewTab(browser()); | |
94 tab_count++; | |
95 EXPECT_EQ(tab_count, browser()->tab_count()); | |
96 EXPECT_EQ(host_count, RenderProcessHostCount()); | |
97 | |
98 // Create another new tab. It should share the process with the other WebUI. | |
99 browser()->NewTab(); | |
100 if (browser()->tab_count() == tab_count) | |
101 ui_test_utils::WaitForNewTab(browser()); | |
102 tab_count++; | |
103 EXPECT_EQ(tab_count, browser()->tab_count()); | |
104 EXPECT_EQ(host_count, RenderProcessHostCount()); | |
105 | |
106 // Create another new tab. It should share the process with the other WebUI. | |
107 browser()->NewTab(); | |
108 if (browser()->tab_count() == tab_count) | |
109 ui_test_utils::WaitForNewTab(browser()); | |
110 tab_count++; | |
111 EXPECT_EQ(tab_count, browser()->tab_count()); | |
112 EXPECT_EQ(host_count, RenderProcessHostCount()); | |
113 } | |
114 | |
115 // Ensure that DevTools opened to debug DevTools is launched in a separate | |
116 // process when --process-per-tab is set. See crbug.com/69873. | |
117 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, DevToolsOnSelfInOwnProcessPPT) { | |
118 CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); | |
119 parsed_command_line.AppendSwitch(switches::kProcessPerTab); | |
120 | |
121 int tab_count = 1; | |
122 int host_count = 1; | |
123 | |
124 GURL page1("data:text/html,hello world1"); | |
125 browser()->ShowSingletonTab(page1); | |
126 if (browser()->tab_count() == tab_count) | |
127 ui_test_utils::WaitForNewTab(browser()); | |
128 tab_count++; | |
129 host_count++; | |
130 EXPECT_EQ(tab_count, browser()->tab_count()); | |
131 EXPECT_EQ(host_count, RenderProcessHostCount()); | |
132 | |
133 // DevTools start in docked mode (no new tab), in a separate process. | |
134 browser()->ToggleDevToolsWindow(DEVTOOLS_TOGGLE_ACTION_INSPECT); | |
135 host_count++; | |
136 EXPECT_EQ(tab_count, browser()->tab_count()); | |
137 EXPECT_EQ(host_count, RenderProcessHostCount()); | |
138 | |
139 RenderViewHost* devtools = FindFirstDevToolsHost(); | |
140 DCHECK(devtools); | |
141 | |
142 // DevTools start in a separate process. | |
143 DevToolsManager::GetInstance()->ToggleDevToolsWindow( | |
144 devtools, DEVTOOLS_TOGGLE_ACTION_INSPECT); | |
145 host_count++; | |
146 EXPECT_EQ(tab_count, browser()->tab_count()); | |
147 EXPECT_EQ(host_count, RenderProcessHostCount()); | |
148 } | |
149 | |
150 // Ensure that DevTools opened to debug DevTools is launched in a separate | |
151 // process. See crbug.com/69873. | |
152 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, DevToolsOnSelfInOwnProcess) { | |
153 int tab_count = 1; | |
154 int host_count = 1; | |
155 | |
156 GURL page1("data:text/html,hello world1"); | |
157 browser()->ShowSingletonTab(page1); | |
158 if (browser()->tab_count() == tab_count) | |
159 ui_test_utils::WaitForNewTab(browser()); | |
160 tab_count++; | |
161 host_count++; | |
162 EXPECT_EQ(tab_count, browser()->tab_count()); | |
163 EXPECT_EQ(host_count, RenderProcessHostCount()); | |
164 | |
165 // DevTools start in docked mode (no new tab), in a separate process. | |
166 browser()->ToggleDevToolsWindow(DEVTOOLS_TOGGLE_ACTION_INSPECT); | |
167 host_count++; | |
168 EXPECT_EQ(tab_count, browser()->tab_count()); | |
169 EXPECT_EQ(host_count, RenderProcessHostCount()); | |
170 | |
171 RenderViewHost* devtools = FindFirstDevToolsHost(); | |
172 DCHECK(devtools); | |
173 | |
174 // DevTools start in a separate process. | |
175 DevToolsManager::GetInstance()->ToggleDevToolsWindow( | |
176 devtools, DEVTOOLS_TOGGLE_ACTION_INSPECT); | |
177 host_count++; | |
178 EXPECT_EQ(tab_count, browser()->tab_count()); | |
179 EXPECT_EQ(host_count, RenderProcessHostCount()); | |
180 } | |
181 | |
182 // When we hit the max number of renderers, verify that the way we do process | |
183 // sharing behaves correctly. In particular, this test is verifying that even | |
184 // when we hit the max process limit, that renderers of each type will wind up | |
185 // in a process of that type, even if that means creating a new process. | |
186 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, ProcessOverflow) { | |
187 // Set max renderers to 1 to force running out of processes. | |
188 RenderProcessHost::SetMaxRendererProcessCount(1); | |
189 | |
190 int tab_count = 1; | |
191 int host_count = 1; | |
192 TabContents* tab1 = NULL; | |
193 TabContents* tab2 = NULL; | |
194 RenderProcessHost* rph1 = NULL; | |
195 RenderProcessHost* rph2 = NULL; | |
196 RenderProcessHost* rph3 = NULL; | |
197 | |
198 // Change the first tab to be the new tab page (TYPE_WEBUI). | |
199 GURL newtab(chrome::kChromeUINewTabURL); | |
200 ui_test_utils::NavigateToURL(browser(), newtab); | |
201 EXPECT_EQ(tab_count, browser()->tab_count()); | |
202 tab1 = browser()->GetTabContentsAt(tab_count - 1); | |
203 rph1 = tab1->GetRenderProcessHost(); | |
204 EXPECT_EQ(tab1->GetURL(), newtab); | |
205 EXPECT_EQ(host_count, RenderProcessHostCount()); | |
206 | |
207 // Create a new TYPE_TABBED tab. It should be in its own process. | |
208 GURL page1("data:text/html,hello world1"); | |
209 browser()->ShowSingletonTab(page1); | |
210 if (browser()->tab_count() == tab_count) | |
211 ui_test_utils::WaitForNewTab(browser()); | |
212 tab_count++; | |
213 host_count++; | |
214 EXPECT_EQ(tab_count, browser()->tab_count()); | |
215 tab1 = browser()->GetTabContentsAt(tab_count - 1); | |
216 rph2 = tab1->GetRenderProcessHost(); | |
217 EXPECT_EQ(tab1->GetURL(), page1); | |
218 EXPECT_EQ(host_count, RenderProcessHostCount()); | |
219 EXPECT_NE(rph1, rph2); | |
220 | |
221 // Create another TYPE_TABBED tab. It should share the previous process. | |
222 GURL page2("data:text/html,hello world2"); | |
223 browser()->ShowSingletonTab(page2); | |
224 if (browser()->tab_count() == tab_count) | |
225 ui_test_utils::WaitForNewTab(browser()); | |
226 tab_count++; | |
227 EXPECT_EQ(tab_count, browser()->tab_count()); | |
228 tab2 = browser()->GetTabContentsAt(tab_count - 1); | |
229 EXPECT_EQ(tab2->GetURL(), page2); | |
230 EXPECT_EQ(host_count, RenderProcessHostCount()); | |
231 EXPECT_EQ(tab2->GetRenderProcessHost(), rph2); | |
232 | |
233 // Create another TYPE_WEBUI tab. It should share the process with newtab. | |
234 // Note: intentionally create this tab after the TYPE_TABBED tabs to exercise | |
235 // bug 43448 where extension and WebUI tabs could get combined into normal | |
236 // renderers. | |
237 GURL history(chrome::kChromeUIHistoryURL); | |
238 browser()->ShowSingletonTab(history); | |
239 if (browser()->tab_count() == tab_count) | |
240 ui_test_utils::WaitForNewTab(browser()); | |
241 tab_count++; | |
242 EXPECT_EQ(tab_count, browser()->tab_count()); | |
243 tab2 = browser()->GetTabContentsAt(tab_count - 1); | |
244 EXPECT_EQ(tab2->GetURL(), history); | |
245 EXPECT_EQ(host_count, RenderProcessHostCount()); | |
246 EXPECT_EQ(tab2->GetRenderProcessHost(), rph1); | |
247 | |
248 // Create a TYPE_EXTENSION tab. It should be in its own process. | |
249 // (the bookmark manager is implemented as an extension) | |
250 GURL bookmarks(chrome::kChromeUIBookmarksURL); | |
251 browser()->ShowSingletonTab(bookmarks); | |
252 if (browser()->tab_count() == tab_count) | |
253 ui_test_utils::WaitForNewTab(browser()); | |
254 tab_count++; | |
255 host_count++; | |
256 EXPECT_EQ(tab_count, browser()->tab_count()); | |
257 tab1 = browser()->GetTabContentsAt(tab_count - 1); | |
258 rph3 = tab1->GetRenderProcessHost(); | |
259 EXPECT_EQ(tab1->GetURL(), bookmarks); | |
260 EXPECT_EQ(host_count, RenderProcessHostCount()); | |
261 EXPECT_NE(rph1, rph3); | |
262 EXPECT_NE(rph2, rph3); | |
263 } | |
OLD | NEW |