summaryrefslogtreecommitdiffstats
path: root/chrome/test/browser/browser_tests_launcher.cc
blob: 34ec60cd3729577f6669eec9d2deb1a66b6478ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <windows.h>

#include <vector>

#include "base/command_line.h"
#include "base/logging.h"
#include "base/process_util.h"
#include "base/string_util.h"

namespace {

const wchar_t* const kBrowserTestDLLName = L"browser_tests.dll";
const wchar_t* const kGTestListTestsFlag = L"gtest_list_tests";
}

// TestEnvContext takes care of loading/unloading the DLL containing the tests.
class TestEnvContext {
 public:
  TestEnvContext()
      : module_(NULL),
        run_test_proc_(NULL) {
  }

  ~TestEnvContext() {
    if (!module_)
      return;
    BOOL r = ::FreeLibrary(module_);
    DCHECK(r);
    LOG(INFO) << "Unloaded " << kBrowserTestDLLName;
  }

  bool Init() {
    module_ = ::LoadLibrary(kBrowserTestDLLName);
    if (!module_) {
      LOG(ERROR) << "Failed to find " << kBrowserTestDLLName;
      return false;
    }

    run_test_proc_ = reinterpret_cast<RunTestProc>(
        ::GetProcAddress(module_, "RunTests"));
    if (!run_test_proc_) {
      LOG(ERROR) <<
          "Failed to find RunTest function in " << kBrowserTestDLLName;
      return false;
    }

    return true;
  }

  // Returns true if the test succeeded, false if it failed.
  bool RunTest(const std::string& test_name) {
    std::string filter_flag = StringPrintf("--gtest_filter=%s",
                                           test_name.c_str());
    char* argv[2];
    argv[0] = "";
    argv[1] = const_cast<char*>(filter_flag.c_str());
    return RunAsIs(2, argv) == 0;
  }

  // Calls-in to GTest with the arguments we were started with.
  int RunAsIs(int argc, char** argv) {
    return (run_test_proc_)(argc, argv);
  }

 private:
  typedef int (__cdecl *RunTestProc)(int, char**);

  HMODULE module_;
  RunTestProc run_test_proc_;
};

// Retrieves the list of tests to run.
// Simply uses the --gtest_list_tests option which honor the filter.
// Sadly there is no dry-run option (or willingness to get such an option) in
// GTest.  So we'll have to process disabled and repeat options ourselves.
bool GetTestList(const CommandLine& command_line,
                 std::vector<std::string>* test_list) {
  DCHECK(!command_line.HasSwitch(kGTestListTestsFlag));

  // Run ourselves with the --gtest_list_tests option and read the output.
  std::wstring new_command_line = command_line.command_line_string() + L" --" +
      kGTestListTestsFlag;
  std::string output;
  if (!base::GetAppOutput(new_command_line, &output))
    return false;

  // Now let's parse the returned output.
  // It looks like:
  // TestCase.
  //   Test1
  //   Test2
  // OtherTestCase.
  //   FooTest
  // ...
  std::vector<std::string> lines;
  SplitString(output, '\n', &lines);

  std::string test_case;
  for (std::vector<std::string>::const_iterator iter = lines.begin();
       iter != lines.end(); ++iter) {
    std::string line = *iter;
    if (line.empty())
      continue;  // Just ignore empty lines if any.

    if (line[line.size() - 1] == '.') {
      // This is a new test case.
      test_case = line;
      continue;
    }
    // We are dealing with a test.
    test_list->push_back(test_case + line);
  }
  return true;
}

int main(int argc, char** argv) {
  CommandLine::Init(argc, argv);
  const CommandLine* command_line = CommandLine::ForCurrentProcess();

  if (command_line->HasSwitch(kGTestListTestsFlag)) {
    TestEnvContext test_context;
    if (!test_context.Init())
      return 1;
    return test_context.RunAsIs(argc, argv);
  }

  // First let's get the list of tests we need to run.
  std::vector<std::string> test_list;
  if (!GetTestList(*command_line, &test_list)) {
    printf("Failed to retrieve the tests to run.\n");
    return 0;
  }

  if (test_list.empty()) {
    printf("No tests to run.\n");
    return 0;
  }

  // Run the tests.
  int test_run_count = 0;
  std::vector<std::string> failed_tests;
  for (std::vector<std::string>::const_iterator iter = test_list.begin();
       iter != test_list.end(); ++iter) {
    std::string test_name = *iter;
    TestEnvContext test_context;
    if (!test_context.Init())
      return 1;
    test_run_count++;
    if (!test_context.RunTest(test_name.c_str())) {
      if (std::find(failed_tests.begin(), failed_tests.end(), test_name) ==
          failed_tests.end()) {
        failed_tests.push_back(*iter);
      }
    }
  }

  printf("%d test%s run\n", test_run_count, test_run_count > 1 ? "s" : "");
  printf("%d test%s failed\n", failed_tests.size(),
                               failed_tests.size() > 1 ? "s" : "");
  if (failed_tests.empty())
    return 0;

  printf("Failing tests:\n");
  for (std::vector<std::string>::const_iterator iter = failed_tests.begin();
       iter != failed_tests.end(); ++iter) {
    printf("%s\n", iter->c_str());
  }

  return 1;
}