| 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/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/process.h" |
| 8 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
| 9 #include "chrome/test/base/ui_test_utils.h" | 11 #include "chrome/test/base/ui_test_utils.h" |
| 10 #include "content/browser/renderer_host/render_process_host_impl.h" | 12 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 11 #include "content/browser/tab_contents/tab_contents.h" | 13 #include "content/browser/tab_contents/tab_contents.h" |
| 12 #include "content/common/test_url_constants.h" | 14 #include "content/common/test_url_constants.h" |
| 15 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/common/content_switches.h" | 16 #include "content/public/common/content_switches.h" |
| 14 | 17 |
| 15 RenderProcessHostTest::RenderProcessHostTest() { | 18 RenderProcessHostTest::RenderProcessHostTest() { |
| 16 EnableDOMAutomation(); | 19 EnableDOMAutomation(); |
| 17 } | 20 } |
| 18 | 21 |
| 19 int RenderProcessHostTest::RenderProcessHostCount() { | 22 int RenderProcessHostTest::RenderProcessHostCount() { |
| 20 content::RenderProcessHost::iterator hosts = | 23 content::RenderProcessHost::iterator hosts = |
| 21 content::RenderProcessHost::AllHostsIterator(); | 24 content::RenderProcessHost::AllHostsIterator(); |
| 22 int count = 0; | 25 int count = 0; |
| 23 while (!hosts.IsAtEnd()) { | 26 while (!hosts.IsAtEnd()) { |
| 24 if (hosts.GetCurrentValue()->HasConnection()) | 27 if (hosts.GetCurrentValue()->HasConnection()) |
| 25 count++; | 28 count++; |
| 26 hosts.Advance(); | 29 hosts.Advance(); |
| 27 } | 30 } |
| 28 return count; | 31 return count; |
| 29 } | 32 } |
| 30 | 33 |
| 34 void PostQuit(MessageLoop* loop) { |
| 35 loop->PostTask(FROM_HERE, new MessageLoop::QuitTask); |
| 36 } |
| 37 |
| 38 void DoNothing() {} |
| 39 |
| 40 base::ProcessHandle RenderProcessHostTest::ShowSingletonTab(const GURL& page) { |
| 41 browser()->ShowSingletonTab(page); |
| 42 TabContents* tc = browser()->GetSelectedTabContents(); |
| 43 CHECK(tc->GetURL() == page); |
| 44 |
| 45 // Ensure that the backgrounding / foregrounding gets a chance to run. |
| 46 content::BrowserThread::PostTaskAndReply( |
| 47 content::BrowserThread::PROCESS_LAUNCHER, FROM_HERE, |
| 48 base::Bind(DoNothing), MessageLoop::QuitClosure()); |
| 49 MessageLoop::current()->Run(); |
| 50 |
| 51 return tc->GetRenderProcessHost()->GetHandle(); |
| 52 } |
| 53 |
| 31 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, ProcessPerTab) { | 54 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, ProcessPerTab) { |
| 32 // Set max renderers to 1 to force running out of processes. | 55 // Set max renderers to 1 to force running out of processes. |
| 33 content::RenderProcessHost::SetMaxRendererProcessCountForTest(1); | 56 content::RenderProcessHost::SetMaxRendererProcessCountForTest(1); |
| 34 | 57 |
| 35 CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); | 58 CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); |
| 36 parsed_command_line.AppendSwitch(switches::kProcessPerTab); | 59 parsed_command_line.AppendSwitch(switches::kProcessPerTab); |
| 37 | 60 |
| 38 int tab_count = 1; | 61 int tab_count = 1; |
| 39 int host_count = 1; | 62 int host_count = 1; |
| 40 | 63 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 | 100 |
| 78 // Create another new tab. It should share the process with the other WebUI. | 101 // Create another new tab. It should share the process with the other WebUI. |
| 79 browser()->NewTab(); | 102 browser()->NewTab(); |
| 80 if (browser()->tab_count() == tab_count) | 103 if (browser()->tab_count() == tab_count) |
| 81 ui_test_utils::WaitForNewTab(browser()); | 104 ui_test_utils::WaitForNewTab(browser()); |
| 82 tab_count++; | 105 tab_count++; |
| 83 EXPECT_EQ(tab_count, browser()->tab_count()); | 106 EXPECT_EQ(tab_count, browser()->tab_count()); |
| 84 EXPECT_EQ(host_count, RenderProcessHostCount()); | 107 EXPECT_EQ(host_count, RenderProcessHostCount()); |
| 85 } | 108 } |
| 86 | 109 |
| 110 // We don't change process priorities on Mac or Posix because the user lacks the |
| 111 // permission to raise a process' priority even after lowering it. |
| 112 #if defined(OS_WIN) || defined(OS_LINUX) |
| 113 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, Backgrounding) { |
| 114 if (!base::Process::CanBackgroundProcesses()) { |
| 115 LOG(ERROR) << "Can't background processes"; |
| 116 return; |
| 117 } |
| 118 CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); |
| 119 parsed_command_line.AppendSwitch(switches::kProcessPerTab); |
| 120 |
| 121 // Change the first tab to be the new tab page (TYPE_WEBUI). |
| 122 GURL newtab(chrome::kTestNewTabURL); |
| 123 ui_test_utils::NavigateToURL(browser(), newtab); |
| 124 |
| 125 // Create a new tab. It should be foreground. |
| 126 GURL page1("data:text/html,hello world1"); |
| 127 base::ProcessHandle pid1 = ShowSingletonTab(page1); |
| 128 EXPECT_FALSE(base::Process(pid1).IsProcessBackgrounded()); |
| 129 |
| 130 // Create another tab. It should be foreground, and the first tab should |
| 131 // now be background. |
| 132 GURL page2("data:text/html,hello world2"); |
| 133 base::ProcessHandle pid2 = ShowSingletonTab(page2); |
| 134 EXPECT_NE(pid1, pid2); |
| 135 EXPECT_TRUE(base::Process(pid1).IsProcessBackgrounded()); |
| 136 EXPECT_FALSE(base::Process(pid2).IsProcessBackgrounded()); |
| 137 |
| 138 // Navigate back to first page. It should be foreground again, and the second |
| 139 // tab should be background. |
| 140 EXPECT_EQ(pid1, ShowSingletonTab(page1)); |
| 141 EXPECT_FALSE(base::Process(pid1).IsProcessBackgrounded()); |
| 142 EXPECT_TRUE(base::Process(pid2).IsProcessBackgrounded()); |
| 143 } |
| 144 #endif |
| 145 |
| 87 // When we hit the max number of renderers, verify that the way we do process | 146 // When we hit the max number of renderers, verify that the way we do process |
| 88 // sharing behaves correctly. In particular, this test is verifying that even | 147 // sharing behaves correctly. In particular, this test is verifying that even |
| 89 // when we hit the max process limit, that renderers of each type will wind up | 148 // when we hit the max process limit, that renderers of each type will wind up |
| 90 // in a process of that type, even if that means creating a new process. | 149 // in a process of that type, even if that means creating a new process. |
| 91 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, ProcessOverflow) { | 150 IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, ProcessOverflow) { |
| 92 // Set max renderers to 1 to force running out of processes. | 151 // Set max renderers to 1 to force running out of processes. |
| 93 content::RenderProcessHost::SetMaxRendererProcessCountForTest(1); | 152 content::RenderProcessHost::SetMaxRendererProcessCountForTest(1); |
| 94 | 153 |
| 95 int tab_count = 1; | 154 int tab_count = 1; |
| 96 int host_count = 1; | 155 int host_count = 1; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 host_count++; | 226 host_count++; |
| 168 #endif | 227 #endif |
| 169 EXPECT_EQ(tab_count, browser()->tab_count()); | 228 EXPECT_EQ(tab_count, browser()->tab_count()); |
| 170 tab1 = browser()->GetTabContentsAt(tab_count - 1); | 229 tab1 = browser()->GetTabContentsAt(tab_count - 1); |
| 171 rph3 = tab1->GetRenderProcessHost(); | 230 rph3 = tab1->GetRenderProcessHost(); |
| 172 EXPECT_EQ(tab1->GetURL(), bookmarks); | 231 EXPECT_EQ(tab1->GetURL(), bookmarks); |
| 173 EXPECT_EQ(host_count, RenderProcessHostCount()); | 232 EXPECT_EQ(host_count, RenderProcessHostCount()); |
| 174 EXPECT_NE(rph1, rph3); | 233 EXPECT_NE(rph1, rph3); |
| 175 EXPECT_NE(rph2, rph3); | 234 EXPECT_NE(rph2, rph3); |
| 176 } | 235 } |
| OLD | NEW |