1 // Copyright 2016 The Chromium Embedded Framework Authors. Postions copyright
2 // 2012 The Chromium Authors. All rights reserved. Use of this source code is
3 // governed by a BSD-style license that can be found in the LICENSE file.
4
5 #include "tests/ceftests/test_suite.h"
6
7 #include "include/cef_file_util.h"
8 #include "include/wrapper/cef_scoped_temp_dir.h"
9 #include "tests/gtest/include/gtest/gtest.h"
10 #include "tests/gtest/teamcity/include/teamcity_gtest.h"
11 #include "tests/shared/common/client_switches.h"
12
13 namespace {
14
15 CefTestSuite* g_test_suite = nullptr;
16
17 #if defined(OS_WIN)
18
19 // From base/process/launch_win.cc.
RouteStdioToConsole(bool create_console_if_not_found)20 void RouteStdioToConsole(bool create_console_if_not_found) {
21 // Don't change anything if stdout or stderr already point to a
22 // valid stream.
23 //
24 // If we are running under Buildbot or under Cygwin's default
25 // terminal (mintty), stderr and stderr will be pipe handles. In
26 // that case, we don't want to open CONOUT$, because its output
27 // likely does not go anywhere.
28 //
29 // We don't use GetStdHandle() to check stdout/stderr here because
30 // it can return dangling IDs of handles that were never inherited
31 // by this process. These IDs could have been reused by the time
32 // this function is called. The CRT checks the validity of
33 // stdout/stderr on startup (before the handle IDs can be reused).
34 // _fileno(stdout) will return -2 (_NO_CONSOLE_FILENO) if stdout was
35 // invalid.
36 if (_fileno(stdout) >= 0 || _fileno(stderr) >= 0) {
37 return;
38 }
39
40 if (!AttachConsole(ATTACH_PARENT_PROCESS)) {
41 unsigned int result = GetLastError();
42 // Was probably already attached.
43 if (result == ERROR_ACCESS_DENIED)
44 return;
45 // Don't bother creating a new console for each child process if the
46 // parent process is invalid (eg: crashed).
47 if (result == ERROR_GEN_FAILURE)
48 return;
49 if (create_console_if_not_found) {
50 // Make a new console if attaching to parent fails with any other error.
51 // It should be ERROR_INVALID_HANDLE at this point, which means the
52 // browser was likely not started from a console.
53 AllocConsole();
54 } else {
55 return;
56 }
57 }
58
59 // Arbitrary byte count to use when buffering output lines. More
60 // means potential waste, less means more risk of interleaved
61 // log-lines in output.
62 enum { kOutputBufferSize = 64 * 1024 };
63
64 if (freopen("CONOUT$", "w", stdout)) {
65 setvbuf(stdout, nullptr, _IOLBF, kOutputBufferSize);
66 // Overwrite FD 1 for the benefit of any code that uses this FD
67 // directly. This is safe because the CRT allocates FDs 0, 1 and
68 // 2 at startup even if they don't have valid underlying Windows
69 // handles. This means we won't be overwriting an FD created by
70 // _open() after startup.
71 _dup2(_fileno(stdout), 1);
72 }
73 if (freopen("CONOUT$", "w", stderr)) {
74 setvbuf(stderr, nullptr, _IOLBF, kOutputBufferSize);
75 _dup2(_fileno(stderr), 2);
76 }
77
78 // Fix all cout, wcout, cin, wcin, cerr, wcerr, clog and wclog.
79 std::ios::sync_with_stdio();
80 }
81
82 #endif // defined(OS_WIN)
83
84 } // namespace
85
CefTestSuite(int argc,char ** argv)86 CefTestSuite::CefTestSuite(int argc, char** argv)
87 : argc_(argc), argv_(argc, argv), retval_(0) {
88 g_test_suite = this;
89
90 // Keep a representation of the original command-line.
91 command_line_ = CefCommandLine::CreateCommandLine();
92 #if defined(OS_WIN)
93 command_line_->InitFromString(::GetCommandLineW());
94 #else
95 command_line_->InitFromArgv(argc, argv);
96 #endif
97
98 if (!command_line_->HasSwitch("type")) {
99 // Initialize in the main process only.
100 root_cache_path_ =
101 command_line_->GetSwitchValue(client::switches::kCachePath);
102 if (root_cache_path_.empty()) {
103 CefScopedTempDir temp_dir;
104 CHECK(temp_dir.CreateUniqueTempDir());
105 root_cache_path_ = temp_dir.Take();
106 RegisterTempDirectory(root_cache_path_);
107 }
108 }
109 }
110
~CefTestSuite()111 CefTestSuite::~CefTestSuite() {
112 g_test_suite = nullptr;
113 }
114
115 // static
GetInstance()116 CefTestSuite* CefTestSuite::GetInstance() {
117 return g_test_suite;
118 }
119
InitMainProcess()120 void CefTestSuite::InitMainProcess() {
121 PreInitialize();
122
123 // This will modify |argc_| and |argv_|.
124 testing::InitGoogleTest(&argc_, argv_.array());
125
126 if (jetbrains::teamcity::underTeamcity()) {
127 auto& listeners = ::testing::UnitTest::GetInstance()->listeners();
128 listeners.Append(
129 new jetbrains::teamcity::TeamcityGoogleTestEventListener());
130 }
131 }
132
133 // Don't add additional code to this method. Instead add it to Initialize().
Run()134 int CefTestSuite::Run() {
135 Initialize();
136 retval_ = RUN_ALL_TESTS();
137 Shutdown();
138 return retval_;
139 }
140
GetSettings(CefSettings & settings) const141 void CefTestSuite::GetSettings(CefSettings& settings) const {
142 // Enable the experimental Chrome runtime. See issue #2969 for details.
143 settings.chrome_runtime =
144 command_line_->HasSwitch(client::switches::kEnableChromeRuntime);
145
146 CefString(&settings.cache_path) = root_cache_path_;
147 CefString(&settings.root_cache_path) = root_cache_path_;
148 CefString(&settings.user_data_path) = root_cache_path_;
149
150 // Always expose the V8 gc() function to give tests finer-grained control over
151 // memory management.
152 std::string javascript_flags = "--expose-gc";
153 // Value of kJavascriptFlags switch.
154 std::string other_javascript_flags =
155 command_line_->GetSwitchValue("js-flags");
156 if (!other_javascript_flags.empty())
157 javascript_flags += " " + other_javascript_flags;
158 CefString(&settings.javascript_flags) = javascript_flags;
159
160 // Necessary for V8Test.OnUncaughtException tests.
161 settings.uncaught_exception_stack_size = 10;
162
163 // Necessary for the OSRTest tests.
164 settings.windowless_rendering_enabled = true;
165
166 // For Accept-Language test
167 CefString(&settings.accept_language_list) = CEF_SETTINGS_ACCEPT_LANGUAGE;
168 }
169
RegisterTempDirectory(const CefString & directory)170 void CefTestSuite::RegisterTempDirectory(const CefString& directory) {
171 base::AutoLock lock_scope(temp_directories_lock_);
172 temp_directories_.push_back(directory);
173 }
174
DeleteTempDirectories()175 void CefTestSuite::DeleteTempDirectories() {
176 base::AutoLock lock_scope(temp_directories_lock_);
177 for (size_t i = 0U; i < temp_directories_.size(); ++i) {
178 CefDeleteFile(temp_directories_[i], true);
179 }
180 temp_directories_.clear();
181 }
182
PreInitialize()183 void CefTestSuite::PreInitialize() {
184 #if defined(OS_WIN)
185 testing::GTEST_FLAG(catch_exceptions) = false;
186
187 // Enable termination on heap corruption.
188 // Ignore the result code. Supported starting with XP SP3 and Vista.
189 HeapSetInformation(nullptr, HeapEnableTerminationOnCorruption, nullptr, 0);
190 #endif
191
192 #if defined(OS_LINUX)
193 // When calling native char conversion functions (e.g wrctomb) we need to
194 // have the locale set. In the absence of such a call the "C" locale is the
195 // default. In the gtk code (below) gtk_init() implicitly sets a locale.
196 setlocale(LC_ALL, "");
197 #endif // defined(OS_LINUX)
198
199 // Don't add additional code to this function. Instead add it to Initialize().
200 }
201
Initialize()202 void CefTestSuite::Initialize() {
203 #if defined(OS_WIN)
204 RouteStdioToConsole(true);
205 #endif // defined(OS_WIN)
206 }
207
Shutdown()208 void CefTestSuite::Shutdown() {}
209