Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / shell / browser / layout_test / layout_test_browser_main.cc
blobc8c73cc086843152d1d42c091ceac6bff31be256
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 "content/shell/browser/layout_test/layout_test_browser_main.h"
7 #include <iostream>
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/location.h"
14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/single_thread_task_runner.h"
17 #include "base/strings/sys_string_conversions.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "base/thread_task_runner_handle.h"
20 #include "base/threading/thread_restrictions.h"
21 #include "components/test_runner/test_info_extractor.h"
22 #include "content/public/browser/browser_main_runner.h"
23 #include "content/public/common/url_constants.h"
24 #include "content/shell/browser/blink_test_controller.h"
25 #include "content/shell/browser/shell.h"
26 #include "content/shell/common/shell_switches.h"
27 #include "content/shell/renderer/layout_test/blink_test_helpers.h"
28 #include "net/base/filename_util.h"
30 #if defined(OS_ANDROID)
31 #include "base/run_loop.h"
32 #include "content/shell/browser/layout_test/layout_test_android.h"
33 #endif
35 namespace {
37 bool RunOneTest(const test_runner::TestInfo& test_info,
38 bool* ran_at_least_once,
39 const scoped_ptr<content::BrowserMainRunner>& main_runner) {
40 if (!content::BlinkTestController::Get()->PrepareForLayoutTest(
41 test_info.url, test_info.current_working_directory,
42 test_info.enable_pixel_dumping, test_info.expected_pixel_hash)) {
43 return false;
46 *ran_at_least_once = true;
47 #if defined(OS_ANDROID)
48 // The message loop on Android is provided by the system, and does not
49 // offer a blocking Run() method. For layout tests, use a nested loop
50 // together with a base::RunLoop so it can block until a QuitClosure.
51 base::RunLoop run_loop;
52 run_loop.Run();
53 #else
54 main_runner->Run();
55 #endif
57 if (!content::BlinkTestController::Get()->ResetAfterLayoutTest())
58 return false;
60 #if defined(OS_ANDROID)
61 // There will be left-over tasks in the queue for Android because the
62 // main window is being destroyed. Run them before starting the next test.
63 base::MessageLoop::current()->RunUntilIdle();
64 #endif
65 return true;
68 int RunTests(const scoped_ptr<content::BrowserMainRunner>& main_runner) {
69 content::BlinkTestController test_controller;
71 // We're outside of the message loop here, and this is a test.
72 base::ThreadRestrictions::ScopedAllowIO allow_io;
73 base::FilePath temp_path;
74 base::GetTempDir(&temp_path);
75 test_controller.SetTempPath(temp_path);
78 std::cout << "#READY\n";
79 std::cout.flush();
81 base::CommandLine::StringVector args =
82 base::CommandLine::ForCurrentProcess()->GetArgs();
83 test_runner::TestInfoExtractor test_extractor(args);
84 bool ran_at_least_once = false;
85 scoped_ptr<test_runner::TestInfo> test_info;
86 while ((test_info = test_extractor.GetNextTest())) {
87 if (!RunOneTest(*test_info, &ran_at_least_once, main_runner))
88 break;
90 if (!ran_at_least_once) {
91 base::ThreadTaskRunnerHandle::Get()->PostTask(
92 FROM_HERE, base::MessageLoop::QuitClosure());
93 main_runner->Run();
96 #if defined(OS_ANDROID)
97 // We need to execute 'main_runner->Shutdown()' before the test_controller
98 // destructs when running on Android, and after it destructs when running
99 // anywhere else.
100 main_runner->Shutdown();
101 #endif
103 return 0;
106 } // namespace
108 // Main routine for running as the Browser process.
109 int LayoutTestBrowserMain(
110 const content::MainFunctionParams& parameters,
111 const scoped_ptr<content::BrowserMainRunner>& main_runner) {
112 base::ScopedTempDir browser_context_path_for_layout_tests;
114 CHECK(browser_context_path_for_layout_tests.CreateUniqueTempDir());
115 CHECK(!browser_context_path_for_layout_tests.path().MaybeAsASCII().empty());
116 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
117 switches::kContentShellDataPath,
118 browser_context_path_for_layout_tests.path().MaybeAsASCII());
120 #if defined(OS_ANDROID)
121 content::EnsureInitializeForAndroidLayoutTests();
122 #endif
124 int exit_code = main_runner->Initialize(parameters);
125 DCHECK_LT(exit_code, 0)
126 << "BrowserMainRunner::Initialize failed in LayoutTestBrowserMain";
128 if (exit_code >= 0)
129 return exit_code;
131 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
132 switches::kCheckLayoutTestSysDeps)) {
133 base::ThreadTaskRunnerHandle::Get()->PostTask(
134 FROM_HERE, base::MessageLoop::QuitClosure());
135 main_runner->Run();
136 content::Shell::CloseAllWindows();
137 main_runner->Shutdown();
138 return 0;
141 exit_code = RunTests(main_runner);
143 #if !defined(OS_ANDROID)
144 main_runner->Shutdown();
145 #endif
147 return exit_code;