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.
4
5#include "mojo/common/test/test_support_impl.h"
6
7#include <stdlib.h>
8#include <string.h>
9
10#include "base/files/file_enumerator.h"
11#include "base/files/file_path.h"
12#include "base/files/file_util.h"
13#include "base/path_service.h"
14#include "base/strings/string_split.h"
15#include "base/strings/string_util.h"
16#include "base/test/perf_log.h"
17
18namespace mojo {
19namespace test {
20namespace {
21
22base::FilePath ResolveSourceRootRelativePath(const char* relative_path) {
23  base::FilePath path;
24  if (!PathService::Get(base::DIR_SOURCE_ROOT, &path))
25    return base::FilePath();
26
27  std::vector<std::string> components;
28  base::SplitString(relative_path, '/', &components);
29
30  for (size_t i = 0; i < components.size(); ++i) {
31    if (!components[i].empty())
32      path = path.AppendASCII(components[i]);
33  }
34
35  return path;
36}
37
38}  // namespace
39
40TestSupportImpl::TestSupportImpl() {
41}
42
43TestSupportImpl::~TestSupportImpl() {
44}
45
46void TestSupportImpl::LogPerfResult(const char* test_name,
47                                    double value,
48                                    const char* units) {
49  base::LogPerfResult(test_name, value, units);
50}
51
52FILE* TestSupportImpl::OpenSourceRootRelativeFile(const char* relative_path) {
53  return base::OpenFile(ResolveSourceRootRelativePath(relative_path), "rb");
54}
55
56char** TestSupportImpl::EnumerateSourceRootRelativeDirectory(
57    const char* relative_path) {
58  std::vector<std::string> names;
59  base::FileEnumerator e(ResolveSourceRootRelativePath(relative_path), false,
60                         base::FileEnumerator::FILES);
61  for (base::FilePath name = e.Next(); !name.empty(); name = e.Next())
62    names.push_back(name.BaseName().AsUTF8Unsafe());
63
64  // |names.size() + 1| for null terminator.
65  char** rv = static_cast<char**>(calloc(names.size() + 1, sizeof(char*)));
66  for (size_t i = 0; i < names.size(); ++i)
67    rv[i] = base::strdup(names[i].c_str());
68  return rv;
69}
70
71}  // namespace test
72}  // namespace mojo
73