| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/renderer_host/render_process_host_browsertest.h" | 5 #include "content/browser/renderer_host/render_process_host_browsertest.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/process.h" | 10 #include "base/process.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 | 48 |
| 49 // Ensure that the backgrounding / foregrounding gets a chance to run. | 49 // Ensure that the backgrounding / foregrounding gets a chance to run. |
| 50 content::BrowserThread::PostTaskAndReply( | 50 content::BrowserThread::PostTaskAndReply( |
| 51 content::BrowserThread::PROCESS_LAUNCHER, FROM_HERE, | 51 content::BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
| 52 base::Bind(&base::DoNothing), MessageLoop::QuitClosure()); | 52 base::Bind(&base::DoNothing), MessageLoop::QuitClosure()); |
| 53 MessageLoop::current()->Run(); | 53 MessageLoop::current()->Run(); |
| 54 | 54 |
| 55 return wc->GetRenderProcessHost()->GetHandle(); | 55 return wc->GetRenderProcessHost()->GetHandle(); |
| 56 } | 56 } |
| 57 | 57 |
| 58 void RenderProcessHostTestWithCommandLine::SetUpCommandLine( |
| 59 CommandLine* command_line) { |
| 60 InProcessBrowserTest::SetUpCommandLine(command_line); |
| 61 command_line->AppendSwitchASCII(switches::kRendererProcessLimit, "1"); |
| 62 } |
| 63 |
| 64 // When we hit the max number of renderers, verify that the way we do process |
| 65 // sharing behaves correctly. In particular, this test is verifying that even |
| 66 // when we hit the max process limit, that renderers of each type will wind up |
| 67 // in a process of that type, even if that means creating a new process. |
| 68 void RenderProcessHostTest::TestProcessOverflow() { |
| 69 int tab_count = 1; |
| 70 int host_count = 1; |
| 71 WebContents* tab1 = NULL; |
| 72 WebContents* tab2 = NULL; |
| 73 content::RenderProcessHost* rph1 = NULL; |
| 74 content::RenderProcessHost* rph2 = NULL; |
| 75 content::RenderProcessHost* rph3 = NULL; |
| 76 |
| 77 #if defined(USE_VIRTUAL_KEYBOARD) |
| 78 ++host_count; // For the virtual keyboard. |
| 79 #endif |
| 80 |
| 81 // Change the first tab to be the new tab page (TYPE_WEBUI). |
| 82 GURL newtab(chrome::kTestNewTabURL); |
| 83 ui_test_utils::NavigateToURL(browser(), newtab); |
| 84 EXPECT_EQ(tab_count, browser()->tab_count()); |
| 85 tab1 = browser()->GetWebContentsAt(tab_count - 1); |
| 86 rph1 = tab1->GetRenderProcessHost(); |
| 87 EXPECT_EQ(tab1->GetURL(), newtab); |
| 88 EXPECT_EQ(host_count, RenderProcessHostCount()); |
| 89 |
| 90 // Create a new TYPE_TABBED tab. It should be in its own process. |
| 91 GURL page1("data:text/html,hello world1"); |
| 92 browser()->ShowSingletonTab(page1); |
| 93 if (browser()->tab_count() == tab_count) |
| 94 ui_test_utils::WaitForNewTab(browser()); |
| 95 tab_count++; |
| 96 host_count++; |
| 97 EXPECT_EQ(tab_count, browser()->tab_count()); |
| 98 tab1 = browser()->GetWebContentsAt(tab_count - 1); |
| 99 rph2 = tab1->GetRenderProcessHost(); |
| 100 EXPECT_EQ(tab1->GetURL(), page1); |
| 101 EXPECT_EQ(host_count, RenderProcessHostCount()); |
| 102 EXPECT_NE(rph1, rph2); |
| 103 |
| 104 // Create another TYPE_TABBED tab. It should share the previous process. |
| 105 GURL page2("data:text/html,hello world2"); |
| 106 browser()->ShowSingletonTab(page2); |
| 107 if (browser()->tab_count() == tab_count) |
| 108 ui_test_utils::WaitForNewTab(browser()); |
| 109 tab_count++; |
| 110 EXPECT_EQ(tab_count, browser()->tab_count()); |
| 111 tab2 = browser()->GetWebContentsAt(tab_count - 1); |
| 112 EXPECT_EQ(tab2->GetURL(), page2); |
| 113 EXPECT_EQ(host_count, RenderProcessHostCount()); |
| 114 EXPECT_EQ(tab2->GetRenderProcessHost(), rph2); |
| 115 |
| 116 // Create another TYPE_WEBUI tab. It should share the process with newtab. |
| 117 // Note: intentionally create this tab after the TYPE_TABBED tabs to exercise |
| 118 // bug 43448 where extension and WebUI tabs could get combined into normal |
| 119 // renderers. |
| 120 GURL history(chrome::kTestHistoryURL); |
| 121 browser()->ShowSingletonTab(history); |
| 122 if (browser()->tab_count() == tab_count) |
| 123 ui_test_utils::WaitForNewTab(browser()); |
| 124 tab_count++; |
| 125 EXPECT_EQ(tab_count, browser()->tab_count()); |
| 126 tab2 = browser()->GetWebContentsAt(tab_count - 1); |
| 127 EXPECT_EQ(tab2->GetURL(), history); |
| 128 EXPECT_EQ(host_count, RenderProcessHostCount()); |
| 129 EXPECT_EQ(tab2->GetRenderProcessHost(), rph1); |
| 130 |
| 131 // Create a TYPE_EXTENSION tab. It should be in its own process. |
| 132 // (the bookmark manager is implemented as an extension) |
| 133 GURL bookmarks(chrome::kTestBookmarksURL); |
| 134 browser()->ShowSingletonTab(bookmarks); |
| 135 if (browser()->tab_count() == tab_count) |
| 136 ui_test_utils::WaitForNewTab(browser()); |
| 137 tab_count++; |
| 138 #if !defined(USE_VIRTUAL_KEYBOARD) |
| 139 // The virtual keyboard already creates an extension process. So this |
| 140 // should not increase the process count. |
| 141 host_count++; |
| 142 #endif |
| 143 EXPECT_EQ(tab_count, browser()->tab_count()); |
| 144 tab1 = browser()->GetWebContentsAt(tab_count - 1); |
| 145 rph3 = tab1->GetRenderProcessHost(); |
| 146 EXPECT_EQ(tab1->GetURL(), bookmarks); |
| 147 EXPECT_EQ(host_count, RenderProcessHostCount()); |
| 148 EXPECT_NE(rph1, rph3); |
| 149 EXPECT_NE(rph2, rph3); |
| 150 } |
| 151 |
| 58 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, ProcessPerTab) { | 152 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, ProcessPerTab) { |
| 59 // Set max renderers to 1 to force running out of processes. | 153 // Set max renderers to 1 to force running out of processes. |
| 60 content::RenderProcessHost::SetMaxRendererProcessCountForTest(1); | 154 content::RenderProcessHost::SetMaxRendererProcessCount(1); |
| 61 | 155 |
| 62 CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); | 156 CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); |
| 63 parsed_command_line.AppendSwitch(switches::kProcessPerTab); | 157 parsed_command_line.AppendSwitch(switches::kProcessPerTab); |
| 64 | 158 |
| 65 int tab_count = 1; | 159 int tab_count = 1; |
| 66 int host_count = 1; | 160 int host_count = 1; |
| 67 | 161 |
| 68 #if defined(USE_VIRTUAL_KEYBOARD) | 162 #if defined(USE_VIRTUAL_KEYBOARD) |
| 69 ++host_count; // For the virtual keyboard. | 163 ++host_count; // For the virtual keyboard. |
| 70 #endif | 164 #endif |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 EXPECT_FALSE(base::Process(pid2).IsProcessBackgrounded()); | 234 EXPECT_FALSE(base::Process(pid2).IsProcessBackgrounded()); |
| 141 | 235 |
| 142 // Navigate back to first page. It should be foreground again, and the second | 236 // Navigate back to first page. It should be foreground again, and the second |
| 143 // tab should be background. | 237 // tab should be background. |
| 144 EXPECT_EQ(pid1, ShowSingletonTab(page1)); | 238 EXPECT_EQ(pid1, ShowSingletonTab(page1)); |
| 145 EXPECT_FALSE(base::Process(pid1).IsProcessBackgrounded()); | 239 EXPECT_FALSE(base::Process(pid1).IsProcessBackgrounded()); |
| 146 EXPECT_TRUE(base::Process(pid2).IsProcessBackgrounded()); | 240 EXPECT_TRUE(base::Process(pid2).IsProcessBackgrounded()); |
| 147 } | 241 } |
| 148 #endif | 242 #endif |
| 149 | 243 |
| 150 // When we hit the max number of renderers, verify that the way we do process | |
| 151 // sharing behaves correctly. In particular, this test is verifying that even | |
| 152 // when we hit the max process limit, that renderers of each type will wind up | |
| 153 // in a process of that type, even if that means creating a new process. | |
| 154 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, ProcessOverflow) { | 244 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, ProcessOverflow) { |
| 155 // Set max renderers to 1 to force running out of processes. | 245 // Set max renderers to 1 to force running out of processes. |
| 156 content::RenderProcessHost::SetMaxRendererProcessCountForTest(1); | 246 content::RenderProcessHost::SetMaxRendererProcessCount(1); |
| 247 TestProcessOverflow(); |
| 248 } |
| 157 | 249 |
| 158 int tab_count = 1; | 250 // Variation of the ProcessOverflow test, which is driven through command line |
| 159 int host_count = 1; | 251 // parameter instead of direct function call into the class. |
| 160 WebContents* tab1 = NULL; | 252 IN_PROC_BROWSER_TEST_F(RenderProcessHostTestWithCommandLine, ProcessOverflow) { |
| 161 WebContents* tab2 = NULL; | 253 TestProcessOverflow(); |
| 162 content::RenderProcessHost* rph1 = NULL; | |
| 163 content::RenderProcessHost* rph2 = NULL; | |
| 164 content::RenderProcessHost* rph3 = NULL; | |
| 165 | |
| 166 #if defined(USE_VIRTUAL_KEYBOARD) | |
| 167 ++host_count; // For the virtual keyboard. | |
| 168 #endif | |
| 169 | |
| 170 // Change the first tab to be the new tab page (TYPE_WEBUI). | |
| 171 GURL newtab(chrome::kTestNewTabURL); | |
| 172 ui_test_utils::NavigateToURL(browser(), newtab); | |
| 173 EXPECT_EQ(tab_count, browser()->tab_count()); | |
| 174 tab1 = browser()->GetWebContentsAt(tab_count - 1); | |
| 175 rph1 = tab1->GetRenderProcessHost(); | |
| 176 EXPECT_EQ(tab1->GetURL(), newtab); | |
| 177 EXPECT_EQ(host_count, RenderProcessHostCount()); | |
| 178 | |
| 179 // Create a new TYPE_TABBED tab. It should be in its own process. | |
| 180 GURL page1("data:text/html,hello world1"); | |
| 181 browser()->ShowSingletonTab(page1); | |
| 182 if (browser()->tab_count() == tab_count) | |
| 183 ui_test_utils::WaitForNewTab(browser()); | |
| 184 tab_count++; | |
| 185 host_count++; | |
| 186 EXPECT_EQ(tab_count, browser()->tab_count()); | |
| 187 tab1 = browser()->GetWebContentsAt(tab_count - 1); | |
| 188 rph2 = tab1->GetRenderProcessHost(); | |
| 189 EXPECT_EQ(tab1->GetURL(), page1); | |
| 190 EXPECT_EQ(host_count, RenderProcessHostCount()); | |
| 191 EXPECT_NE(rph1, rph2); | |
| 192 | |
| 193 // Create another TYPE_TABBED tab. It should share the previous process. | |
| 194 GURL page2("data:text/html,hello world2"); | |
| 195 browser()->ShowSingletonTab(page2); | |
| 196 if (browser()->tab_count() == tab_count) | |
| 197 ui_test_utils::WaitForNewTab(browser()); | |
| 198 tab_count++; | |
| 199 EXPECT_EQ(tab_count, browser()->tab_count()); | |
| 200 tab2 = browser()->GetWebContentsAt(tab_count - 1); | |
| 201 EXPECT_EQ(tab2->GetURL(), page2); | |
| 202 EXPECT_EQ(host_count, RenderProcessHostCount()); | |
| 203 EXPECT_EQ(tab2->GetRenderProcessHost(), rph2); | |
| 204 | |
| 205 // Create another TYPE_WEBUI tab. It should share the process with newtab. | |
| 206 // Note: intentionally create this tab after the TYPE_TABBED tabs to exercise | |
| 207 // bug 43448 where extension and WebUI tabs could get combined into normal | |
| 208 // renderers. | |
| 209 GURL history(chrome::kTestHistoryURL); | |
| 210 browser()->ShowSingletonTab(history); | |
| 211 if (browser()->tab_count() == tab_count) | |
| 212 ui_test_utils::WaitForNewTab(browser()); | |
| 213 tab_count++; | |
| 214 EXPECT_EQ(tab_count, browser()->tab_count()); | |
| 215 tab2 = browser()->GetWebContentsAt(tab_count - 1); | |
| 216 EXPECT_EQ(tab2->GetURL(), history); | |
| 217 EXPECT_EQ(host_count, RenderProcessHostCount()); | |
| 218 EXPECT_EQ(tab2->GetRenderProcessHost(), rph1); | |
| 219 | |
| 220 // Create a TYPE_EXTENSION tab. It should be in its own process. | |
| 221 // (the bookmark manager is implemented as an extension) | |
| 222 GURL bookmarks(chrome::kTestBookmarksURL); | |
| 223 browser()->ShowSingletonTab(bookmarks); | |
| 224 if (browser()->tab_count() == tab_count) | |
| 225 ui_test_utils::WaitForNewTab(browser()); | |
| 226 tab_count++; | |
| 227 #if !defined(USE_VIRTUAL_KEYBOARD) | |
| 228 // The virtual keyboard already creates an extension process. So this | |
| 229 // should not increase the process count. | |
| 230 host_count++; | |
| 231 #endif | |
| 232 EXPECT_EQ(tab_count, browser()->tab_count()); | |
| 233 tab1 = browser()->GetWebContentsAt(tab_count - 1); | |
| 234 rph3 = tab1->GetRenderProcessHost(); | |
| 235 EXPECT_EQ(tab1->GetURL(), bookmarks); | |
| 236 EXPECT_EQ(host_count, RenderProcessHostCount()); | |
| 237 EXPECT_NE(rph1, rph3); | |
| 238 EXPECT_NE(rph2, rph3); | |
| 239 } | 254 } |
| OLD | NEW |