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