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.
4
5#include <limits>
6
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"
12
13namespace content {
14
15class RenderProcessHostUnitTest : public RenderViewHostTestHarness {};
16
17// Tests that guest RenderProcessHosts are not considered suitable hosts when
18// searching for RenderProcessHost.
19TEST_F(RenderProcessHostUnitTest, GuestsAreNotSuitableHosts) {
20  GURL test_url("http://foo.com");
21
22  MockRenderProcessHost guest_host(browser_context());
23  guest_host.set_is_isolated_guest(true);
24
25  EXPECT_FALSE(RenderProcessHostImpl::IsSuitableHost(
26      &guest_host, browser_context(), test_url));
27  EXPECT_TRUE(RenderProcessHostImpl::IsSuitableHost(
28      process(), browser_context(), test_url));
29  EXPECT_EQ(
30      process(),
31      RenderProcessHost::GetExistingProcessHost(browser_context(), test_url));
32}
33
34#if !defined(OS_ANDROID)
35TEST_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))
43    return;
44
45  // Disable any overrides.
46  RenderProcessHostImpl::SetMaxRendererProcessCount(0);
47
48  // Verify that the limit is between 1 and kMaxRendererProcessCount.
49  EXPECT_GT(RenderProcessHostImpl::GetMaxRendererProcessCount(), 0u);
50  EXPECT_LE(RenderProcessHostImpl::GetMaxRendererProcessCount(),
51      kMaxRendererProcessCount);
52
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()));
58  }
59
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));
64}
65#endif
66
67#if defined(OS_ANDROID)
68TEST_F(RenderProcessHostUnitTest, NoRendererProcessLimitOnAndroid) {
69  // Disable any overrides.
70  RenderProcessHostImpl::SetMaxRendererProcessCount(0);
71
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());
75
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()));
81  }
82
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));
87}
88#endif
89
90}  // namespace content
91