morrita | 7fd88cd | 2014-10-02 22:48:58 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "base/command_line.h" |
lfg | 06aac85 | 2015-03-23 22:36:10 | [diff] [blame] | 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" |
morrita | 7fd88cd | 2014-10-02 22:48:58 | [diff] [blame] | 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" |
lfg | 06aac85 | 2015-03-23 22:36:10 | [diff] [blame] | 13 | #include "content/public/test/test_navigation_observer.h" |
| 14 | #include "content/shell/browser/shell.h" |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | class MockChildProcessLauncherClient |
| 19 | : public content::ChildProcessLauncher::Client { |
| 20 | public: |
| 21 | MockChildProcessLauncherClient() |
| 22 | : client_(nullptr), simulate_failure_(false) {} |
| 23 | |
| 24 | void OnProcessLaunched() override { |
| 25 | if (simulate_failure_) |
| 26 | client_->OnProcessLaunchFailed(); |
| 27 | else |
| 28 | client_->OnProcessLaunched(); |
| 29 | }; |
| 30 | void OnProcessLaunchFailed() override { client_->OnProcessLaunchFailed(); }; |
| 31 | |
| 32 | content::ChildProcessLauncher::Client* client_; |
| 33 | bool simulate_failure_; |
| 34 | }; |
| 35 | |
| 36 | } |
morrita | 7fd88cd | 2014-10-02 22:48:58 | [diff] [blame] | 37 | |
| 38 | namespace content { |
| 39 | |
lfg | 06aac85 | 2015-03-23 22:36:10 | [diff] [blame] | 40 | class ChildProcessLauncherBrowserTest : public ContentBrowserTest {}; |
morrita | 7fd88cd | 2014-10-02 22:48:58 | [diff] [blame] | 41 | |
| 42 | class StatsTableBrowserTest : public ChildProcessLauncherBrowserTest { |
| 43 | public: |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 44 | void SetUpCommandLine(base::CommandLine* command_line) override { |
morrita | 7fd88cd | 2014-10-02 22:48:58 | [diff] [blame] | 45 | command_line->AppendSwitch(switches::kEnableStatsTable); |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | IN_PROC_BROWSER_TEST_F(StatsTableBrowserTest, StartWithStatTable) { |
| 50 | NavigateToURL(shell(), GURL("about:blank")); |
| 51 | } |
| 52 | |
lfg | 06aac85 | 2015-03-23 22:36:10 | [diff] [blame] | 53 | IN_PROC_BROWSER_TEST_F(ChildProcessLauncherBrowserTest, ChildSpawnFail) { |
| 54 | GURL url("about:blank"); |
| 55 | Shell* window = shell(); |
| 56 | MockChildProcessLauncherClient* client(nullptr); |
| 57 | |
| 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); |
| 72 | |
| 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); |
| 81 | |
| 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); |
| 91 | } |
| 92 | |
morrita | 7fd88cd | 2014-10-02 22:48:58 | [diff] [blame] | 93 | } // namespace content |