Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / child_process_launcher_browsertest.cc
blob9ef655c6fc6ef22dd3bdb12d12e67f58135872d6
1 // Copyright 2014 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.
5 #include "base/command_line.h"
6 #include "content/browser/child_process_launcher.h"
7 #include "content/browser/renderer_host/render_process_host_impl.h"
8 #include "content/public/browser/navigation_entry.h"
9 #include "content/public/browser/web_contents.h"
10 #include "content/public/common/content_switches.h"
11 #include "content/public/test/content_browser_test.h"
12 #include "content/public/test/content_browser_test_utils.h"
13 #include "content/public/test/test_navigation_observer.h"
14 #include "content/shell/browser/shell.h"
16 namespace {
18 class MockChildProcessLauncherClient
19 : public content::ChildProcessLauncher::Client {
20 public:
21 MockChildProcessLauncherClient()
22 : client_(nullptr), simulate_failure_(false) {}
24 void OnProcessLaunched() override {
25 if (simulate_failure_)
26 client_->OnProcessLaunchFailed();
27 else
28 client_->OnProcessLaunched();
30 void OnProcessLaunchFailed() override { client_->OnProcessLaunchFailed(); };
32 content::ChildProcessLauncher::Client* client_;
33 bool simulate_failure_;
38 namespace content {
40 class ChildProcessLauncherBrowserTest : public ContentBrowserTest {};
42 class StatsTableBrowserTest : public ChildProcessLauncherBrowserTest {
43 public:
44 void SetUpCommandLine(base::CommandLine* command_line) override {
45 command_line->AppendSwitch(switches::kEnableStatsTable);
49 IN_PROC_BROWSER_TEST_F(StatsTableBrowserTest, StartWithStatTable) {
50 NavigateToURL(shell(), GURL("about:blank"));
53 IN_PROC_BROWSER_TEST_F(ChildProcessLauncherBrowserTest, ChildSpawnFail) {
54 GURL url("about:blank");
55 Shell* window = shell();
56 MockChildProcessLauncherClient* client(nullptr);
58 // Navigate once and simulate a process failing to spawn.
59 TestNavigationObserver nav_observer1(window->web_contents(), 1);
60 client = new MockChildProcessLauncherClient;
61 window->LoadURL(url);
62 client->client_ = static_cast<RenderProcessHostImpl*>(
63 window->web_contents()->GetRenderProcessHost())
64 ->child_process_launcher_->ReplaceClientForTest(client);
65 client->simulate_failure_ = true;
66 nav_observer1.Wait();
67 delete client;
68 NavigationEntry* last_entry =
69 shell()->web_contents()->GetController().GetLastCommittedEntry();
70 // Make sure we didn't navigate.
71 CHECK(!last_entry);
73 // Navigate again and let the process spawn correctly.
74 TestNavigationObserver nav_observer2(window->web_contents(), 1);
75 window->LoadURL(url);
76 nav_observer2.Wait();
77 last_entry = shell()->web_contents()->GetController().GetLastCommittedEntry();
78 // Make sure that we navigated to the proper URL.
79 CHECK(last_entry && last_entry->GetPageType() == PAGE_TYPE_NORMAL);
80 CHECK(shell()->web_contents()->GetLastCommittedURL() == url);
82 // Navigate again, using the same renderer.
83 url = GURL("data:text/html,dataurl");
84 TestNavigationObserver nav_observer3(window->web_contents(), 1);
85 window->LoadURL(url);
86 nav_observer3.Wait();
87 last_entry = shell()->web_contents()->GetController().GetLastCommittedEntry();
88 // Make sure that we navigated to the proper URL.
89 CHECK(last_entry && last_entry->GetPageType() == PAGE_TYPE_NORMAL);
90 CHECK(shell()->web_contents()->GetLastCommittedURL() == url);
93 } // namespace content