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.
4
5#include "chrome/test/ui/ui_test_suite.h"
6
7#include <string>
8
9#include "base/command_line.h"
10#include "base/environment.h"
11#include "base/logging.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/path_service.h"
14#include "base/process/kill.h"
15#include "base/process/launch.h"
16#include "base/process/process_iterator.h"
17#include "chrome/common/chrome_switches.h"
18#include "chrome/common/env_vars.h"
19
20UITestSuite::UITestSuite(int argc, char** argv) : ChromeTestSuite(argc, argv) {
21#if defined(OS_WIN)
22  crash_service_ = NULL;
23#endif
24}
25
26void UITestSuite::Initialize() {
27  ChromeTestSuite::Initialize();
28#if defined(OS_WIN)
29  LoadCrashService();
30#endif
31}
32
33void UITestSuite::Shutdown() {
34#if defined(OS_WIN)
35  if (crash_service_)
36    base::KillProcess(crash_service_, 0, false);
37  job_handle_.Close();
38#endif
39  ChromeTestSuite::Shutdown();
40}
41
42#if defined(OS_WIN)
43void UITestSuite::LoadCrashService() {
44  scoped_ptr<base::Environment> env(base::Environment::Create());
45  if (env->HasVar(env_vars::kHeadless))
46    return;
47
48  if (base::GetProcessCount(L"crash_service.exe", NULL))
49    return;
50
51  job_handle_.Set(CreateJobObject(NULL, NULL));
52  if (!job_handle_.IsValid()) {
53    LOG(ERROR) << "Could not create JobObject.";
54    return;
55  }
56
57  if (!base::SetJobObjectAsKillOnJobClose(job_handle_.Get())) {
58    LOG(ERROR) << "Could not SetInformationJobObject.";
59    return;
60  }
61
62  base::FilePath exe_dir;
63  if (!PathService::Get(base::DIR_EXE, &exe_dir)) {
64    LOG(ERROR) << "Failed to get path to DIR_EXE, "
65               << "not starting crash_service.exe!";
66    return;
67  }
68
69  base::LaunchOptions launch_options;
70  launch_options.job_handle = job_handle_.Get();
71  base::FilePath crash_service = exe_dir.Append(L"crash_service.exe");
72  if (!base::LaunchProcess(crash_service.value(), base::LaunchOptions(),
73                           &crash_service_)) {
74    LOG(ERROR) << "Couldn't start crash_service.exe, so this ui_tests run "
75               << "won't tell you if any test crashes!";
76    return;
77  }
78}
79#endif
80