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