1 // Copyright 2013 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.
7 #include "base/command_line.h"
8 #include "content/public/common/content_constants.h"
9 #include "content/public/common/content_switches.h"
10 #include "content/public/test/mock_render_process_host.h"
11 #include "content/test/test_render_view_host.h"
15 class RenderProcessHostUnitTest
: public RenderViewHostTestHarness
{};
17 // Tests that guest RenderProcessHosts are not considered suitable hosts when
18 // searching for RenderProcessHost.
19 TEST_F(RenderProcessHostUnitTest
, GuestsAreNotSuitableHosts
) {
20 GURL
test_url("http://foo.com");
22 MockRenderProcessHost
guest_host(browser_context());
23 guest_host
.set_is_isolated_guest(true);
25 EXPECT_FALSE(RenderProcessHostImpl::IsSuitableHost(
26 &guest_host
, browser_context(), test_url
));
27 EXPECT_TRUE(RenderProcessHostImpl::IsSuitableHost(
28 process(), browser_context(), test_url
));
31 RenderProcessHost::GetExistingProcessHost(browser_context(), test_url
));
34 #if !defined(OS_ANDROID)
35 TEST_F(RenderProcessHostUnitTest
, RendererProcessLimit
) {
36 // This test shouldn't run with --site-per-process or
37 // --enable-strict-site-isolation modes, since they don't allow renderer
38 // process reuse, which this test explicitly exercises.
39 const base::CommandLine
& command_line
=
40 *base::CommandLine::ForCurrentProcess();
41 if (command_line
.HasSwitch(switches::kSitePerProcess
) ||
42 command_line
.HasSwitch(switches::kEnableStrictSiteIsolation
))
45 // Disable any overrides.
46 RenderProcessHostImpl::SetMaxRendererProcessCount(0);
48 // Verify that the limit is between 1 and kMaxRendererProcessCount.
49 EXPECT_GT(RenderProcessHostImpl::GetMaxRendererProcessCount(), 0u);
50 EXPECT_LE(RenderProcessHostImpl::GetMaxRendererProcessCount(),
51 kMaxRendererProcessCount
);
53 // Add dummy process hosts to saturate the limit.
54 ASSERT_NE(0u, kMaxRendererProcessCount
);
55 ScopedVector
<MockRenderProcessHost
> hosts
;
56 for (size_t i
= 0; i
< kMaxRendererProcessCount
; ++i
) {
57 hosts
.push_back(new MockRenderProcessHost(browser_context()));
60 // Verify that the renderer sharing will happen.
61 GURL
test_url("http://foo.com");
62 EXPECT_TRUE(RenderProcessHostImpl::ShouldTryToUseExistingProcessHost(
63 browser_context(), test_url
));
67 #if defined(OS_ANDROID)
68 TEST_F(RenderProcessHostUnitTest
, NoRendererProcessLimitOnAndroid
) {
69 // Disable any overrides.
70 RenderProcessHostImpl::SetMaxRendererProcessCount(0);
72 // Verify that by default the limit on Android returns max size_t.
73 EXPECT_EQ(std::numeric_limits
<size_t>::max(),
74 RenderProcessHostImpl::GetMaxRendererProcessCount());
76 // Add a few dummy process hosts.
77 ASSERT_NE(0u, kMaxRendererProcessCount
);
78 ScopedVector
<MockRenderProcessHost
> hosts
;
79 for (size_t i
= 0; i
< kMaxRendererProcessCount
; ++i
) {
80 hosts
.push_back(new MockRenderProcessHost(browser_context()));
83 // Verify that the renderer sharing still won't happen.
84 GURL
test_url("http://foo.com");
85 EXPECT_FALSE(RenderProcessHostImpl::ShouldTryToUseExistingProcessHost(
86 browser_context(), test_url
));
90 } // namespace content