Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / test / content_browser_test_test.cc
blob1ea3a016c05937d672ef6671d92145e7a77c8d41
1 // Copyright (c) 2011 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 "content/public/test/content_browser_test.h"
7 #include "base/command_line.h"
8 #include "base/location.h"
9 #include "base/process/launch.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/test/launcher/test_launcher.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/browser/render_process_host_observer.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/common/content_switches.h"
18 #include "content/public/test/browser_test_utils.h"
19 #include "content/public/test/content_browser_test_utils.h"
20 #include "content/public/test/test_launcher.h"
21 #include "content/public/test/test_utils.h"
22 #include "content/shell/browser/shell.h"
23 #include "content/shell/common/shell_switches.h"
24 #include "testing/gtest/include/gtest/gtest.h"
26 #if defined(OS_WIN)
27 #include "base/win/windows_version.h"
28 #endif
30 namespace content {
32 // Disabled on official builds because symbolization in sandboxes processes
33 // opens up security holes.
34 // On Android symbolization happens in one step after all the tests ran, so this
35 // test doesn't work there.
36 // TODO(mac): figure out why symbolization doesn't happen in the renderer.
37 // http://crbug.com/521456
38 // TODO(win): send PDB files for component build. http://crbug.com/521459
39 #if !defined(OFFICIAL_BUILD) && !defined(OS_ANDROID) && !defined(OS_MACOSX) && \
40 !(defined(COMPONENT_BUILD) && defined(OS_WIN))
42 IN_PROC_BROWSER_TEST_F(ContentBrowserTest, MANUAL_ShouldntRun) {
43 // Ensures that tests with MANUAL_ prefix don't run automatically.
44 ASSERT_TRUE(false);
47 class CrashObserver : public RenderProcessHostObserver {
48 public:
49 CrashObserver(const base::Closure& quit_closure)
50 : quit_closure_(quit_closure) {}
51 void RenderProcessExited(RenderProcessHost* host,
52 base::TerminationStatus status,
53 int exit_code) override {
54 ASSERT_TRUE(status == base::TERMINATION_STATUS_PROCESS_CRASHED ||
55 status == base::TERMINATION_STATUS_ABNORMAL_TERMINATION);
56 quit_closure_.Run();
59 private:
60 base::Closure quit_closure_;
63 IN_PROC_BROWSER_TEST_F(ContentBrowserTest, MANUAL_RendererCrash) {
64 scoped_refptr<MessageLoopRunner> message_loop_runner = new MessageLoopRunner;
65 CrashObserver crash_observer(message_loop_runner->QuitClosure());
66 shell()->web_contents()->GetRenderProcessHost()->AddObserver(&crash_observer);
68 NavigateToURL(shell(), GURL("chrome:crash"));
69 message_loop_runner->Run();
72 // Tests that browser tests print the callstack when a child process crashes.
73 IN_PROC_BROWSER_TEST_F(ContentBrowserTest, RendererCrashCallStack) {
74 #if defined(OS_WIN)
75 // Matches the same condition in RouteStdioToConsole, which makes this test
76 // fail on XP.
77 if (base::win::GetVersion() < base::win::VERSION_VISTA)
78 return;
79 #endif
81 base::ScopedTempDir temp_dir;
82 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
83 base::CommandLine new_test =
84 base::CommandLine(base::CommandLine::ForCurrentProcess()->GetProgram());
85 new_test.AppendSwitchASCII(base::kGTestFilterFlag,
86 "ContentBrowserTest.MANUAL_RendererCrash");
87 new_test.AppendSwitch(kRunManualTestsFlag);
88 new_test.AppendSwitch(kSingleProcessTestsFlag);
90 // Per https://www.chromium.org/developers/testing/addresssanitizer, there are
91 // ASAN bots that run without the sandbox which this test will pass for. The
92 // other ones pipe the output to a symbolizer script.
93 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoSandbox)) {
94 new_test.AppendSwitch(switches::kNoSandbox);
95 } else {
96 #if defined(ADDRESS_SANITIZER)
97 LOG(INFO) << "Couldn't run ContentBrowserTest.RendererCrashCallStack since "
98 << "sandbox is enabled and ASAN requires piping to an external "
99 << "script.";
100 return;
101 #endif
104 std::string output;
105 base::GetAppOutputAndError(new_test, &output);
107 std::string crash_string =
108 "content::RenderFrameImpl::PrepareRenderViewForNavigation";
110 if (output.find(crash_string) == std::string::npos) {
111 GTEST_FAIL() << "Couldn't find\n" << crash_string << "\n in output\n "
112 << output;
116 IN_PROC_BROWSER_TEST_F(ContentBrowserTest, MANUAL_BrowserCrash) {
117 CHECK(false);
120 // Tests that browser tests print the callstack on asserts.
121 IN_PROC_BROWSER_TEST_F(ContentBrowserTest, BrowserCrashCallStack) {
122 base::ScopedTempDir temp_dir;
123 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
124 base::CommandLine new_test =
125 base::CommandLine(base::CommandLine::ForCurrentProcess()->GetProgram());
126 new_test.AppendSwitchASCII(base::kGTestFilterFlag,
127 "ContentBrowserTest.MANUAL_BrowserCrash");
128 new_test.AppendSwitch(kRunManualTestsFlag);
129 new_test.AppendSwitch(kSingleProcessTestsFlag);
130 std::string output;
131 base::GetAppOutputAndError(new_test, &output);
133 std::string crash_string =
134 "content::ContentBrowserTest_MANUAL_BrowserCrash_Test::RunTestOnMainThread";
136 if (output.find(crash_string) == std::string::npos) {
137 GTEST_FAIL() << "Couldn't find\n" << crash_string << "\n in output\n "
138 << output;
142 #endif
144 class ContentBrowserTestSanityTest : public ContentBrowserTest {
145 public:
146 void SetUpCommandLine(base::CommandLine* command_line) override {
147 const testing::TestInfo* const test_info =
148 testing::UnitTest::GetInstance()->current_test_info();
149 if (std::string(test_info->name()) == "SingleProcess")
150 command_line->AppendSwitch(switches::kSingleProcess);
153 void Test() {
154 GURL url = GetTestUrl(".", "simple_page.html");
156 base::string16 expected_title(base::ASCIIToUTF16("OK"));
157 TitleWatcher title_watcher(shell()->web_contents(), expected_title);
158 NavigateToURL(shell(), url);
159 base::string16 title = title_watcher.WaitAndGetTitle();
160 EXPECT_EQ(expected_title, title);
164 IN_PROC_BROWSER_TEST_F(ContentBrowserTestSanityTest, Basic) {
165 Test();
168 IN_PROC_BROWSER_TEST_F(ContentBrowserTestSanityTest, SingleProcess) {
169 Test();
172 namespace {
174 void CallbackChecker(bool* non_nested_task_ran) {
175 *non_nested_task_ran = true;
178 } // namespace
180 IN_PROC_BROWSER_TEST_F(ContentBrowserTest, NonNestableTask) {
181 bool non_nested_task_ran = false;
182 base::ThreadTaskRunnerHandle::Get()->PostNonNestableTask(
183 FROM_HERE, base::Bind(&CallbackChecker, &non_nested_task_ran));
184 content::RunAllPendingInMessageLoop();
185 ASSERT_TRUE(non_nested_task_ran);
188 } // namespace content