Chromium Code Reviews| Index: content/browser/renderer_host/render_process_host_browsertest.cc | 
| diff --git a/content/browser/renderer_host/render_process_host_browsertest.cc b/content/browser/renderer_host/render_process_host_browsertest.cc | 
| index 866e829d1b74c93dad22fc5b77ee71e84cf19598..c7d8cda7c481f913a73cbf15c27475cff8421aa3 100644 | 
| --- a/content/browser/renderer_host/render_process_host_browsertest.cc | 
| +++ b/content/browser/renderer_host/render_process_host_browsertest.cc | 
| @@ -17,6 +17,8 @@ | 
| #include "content/public/test/content_browser_test_utils.h" | 
| #include "content/public/test/test_service.mojom.h" | 
| #include "content/shell/browser/shell.h" | 
| +#include "content/shell/browser/shell_browser_context.h" | 
| +#include "content/shell/browser/shell_content_browser_client.h" | 
| #include "media/base/bind_to_current_loop.h" | 
| #include "media/base/media_switches.h" | 
| #include "media/base/test_data_util.h" | 
| @@ -42,6 +44,15 @@ int RenderProcessHostCount() { | 
| return count; | 
| } | 
| +int32_t FirstRenderProcessHostKey() { | 
| + content::RenderProcessHost::iterator hosts = | 
| + content::RenderProcessHost::AllHostsIterator(); | 
| + if (!hosts.IsAtEnd()) { | 
| + return hosts.GetCurrentKey(); | 
| + } | 
| + return 0; | 
| +} | 
| + | 
| class RenderProcessHostTest : public ContentBrowserTest, | 
| public RenderProcessHostObserver { | 
| public: | 
| @@ -126,10 +137,87 @@ IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, | 
| another_url = another_url.ReplaceComponents(replace_host); | 
| NavigateToURL(CreateBrowser(), another_url); | 
| - // Expect that we got another process (the guest renderer was not reused). | 
| + // Expect that we got another process (the guest RenderProcessHost was not | 
| + // reused). | 
| EXPECT_EQ(2, RenderProcessHostCount()); | 
| } | 
| +IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, SpareRenderProcessHostTaken) { | 
| + ASSERT_TRUE(embedded_test_server()->Start()); | 
| + | 
| + RenderProcessHost::WarmupSpareRenderProcessHost( | 
| + ShellContentBrowserClient::Get()->browser_context()); | 
| + int32_t spare_key = FirstRenderProcessHostKey(); | 
| 
 
Benoit L
2017/06/19 11:30:35
nit:
EXPECT_NE(0, spare_key);
?
 
mattcary
2017/06/19 12:55:10
Done.
 
 | 
| + | 
| + GURL test_url = embedded_test_server()->GetURL("/simple_page.html"); | 
| + NavigateToURL(CreateBrowser(), test_url); | 
| + | 
| + EXPECT_EQ(spare_key, FirstRenderProcessHostKey()); | 
| + | 
| + // The spare render process host should no longer be available. | 
| + EXPECT_EQ(nullptr, | 
| + RenderProcessHostImpl::GetSpareRenderProcessHostForTesting()); | 
| +} | 
| + | 
| +IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, SpareRenderProcessHostNotTaken) { | 
| + ASSERT_TRUE(embedded_test_server()->Start()); | 
| + | 
| + RenderProcessHost::WarmupSpareRenderProcessHost( | 
| + ShellContentBrowserClient::Get()->off_the_record_browser_context()); | 
| + int32_t spare_key = FirstRenderProcessHostKey(); | 
| 
 
Benoit L
2017/06/19 11:30:35
Question: Actually, should we warmup a spare rende
 
mattcary
2017/06/19 12:55:10
That's a good question, although shouldn't that be
 
Benoit L
2017/06/19 13:20:42
Acknowledged.
 
 | 
| + GURL test_url = embedded_test_server()->GetURL("/simple_page.html"); | 
| + NavigateToURL(CreateBrowser(), test_url); | 
| + | 
| + // There should have been another process created for the navigation. | 
| + EXPECT_NE(spare_key, FirstRenderProcessHostKey()); | 
| + | 
| + // The spare RenderProcessHost should have been cleaned up. Note this | 
| + // behavior is identical to what would have happened if the RenderProcessHost | 
| + // were taken. | 
| + EXPECT_EQ(nullptr, | 
| + RenderProcessHostImpl::GetSpareRenderProcessHostForTesting()); | 
| +} | 
| + | 
| +IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, SpareRenderProcessHostKilled) { | 
| + RenderProcessHost::WarmupSpareRenderProcessHost( | 
| + ShellContentBrowserClient::Get()->browser_context()); | 
| + | 
| + RenderProcessHost* spare = | 
| + RenderProcessHostImpl::GetSpareRenderProcessHostForTesting(); | 
| + mojom::TestServicePtr service; | 
| + ASSERT_NE(nullptr, spare); | 
| + BindInterface(spare, &service); | 
| + | 
| + base::RunLoop run_loop; | 
| + set_process_exit_callback(run_loop.QuitClosure()); | 
| + spare->AddObserver(this); // For process_exit_callback. | 
| + | 
| + // Should reply with a bad message and cause process death. | 
| + service->DoSomething(base::Bind(&base::DoNothing)); | 
| + run_loop.Run(); | 
| + | 
| + // The spare RenderProcessHost should disappear when its process dies. | 
| + EXPECT_EQ(nullptr, | 
| + RenderProcessHostImpl::GetSpareRenderProcessHostForTesting()); | 
| +} | 
| + | 
| +IN_PROC_BROWSER_TEST_F(RenderProcessHostTest, | 
| + SpareRenderProcessHostStoragePartitionKilled) { | 
| + RenderProcessHost::WarmupSpareRenderProcessHost( | 
| + ShellContentBrowserClient::Get()->browser_context()); | 
| + EXPECT_NE(nullptr, | 
| + RenderProcessHostImpl::GetSpareRenderProcessHostForTesting()); | 
| + | 
| + ShellContentBrowserClient::Get() | 
| + ->browser_context() | 
| + ->ShutdownStoragePartitions(); | 
| + | 
| + // The spare RenderProcessHost should disappear along with the | 
| + // StoragePartitions. | 
| + EXPECT_EQ(nullptr, | 
| + RenderProcessHostImpl::GetSpareRenderProcessHostForTesting()); | 
| +} | 
| + | 
| class ShellCloser : public RenderProcessHostObserver { | 
| public: | 
| ShellCloser(Shell* shell, std::string* logging_string) |