• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/core/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 core {
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 (!base::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 
~TestSupportImpl()46 TestSupportImpl::~TestSupportImpl() {}
47 
LogPerfResult(const char * test_name,const char * sub_test_name,double value,const char * units)48 void TestSupportImpl::LogPerfResult(const char* test_name,
49                                     const char* sub_test_name,
50                                     double value,
51                                     const char* units) {
52   DCHECK(test_name);
53   if (sub_test_name) {
54     std::string name = base::StringPrintf("%s/%s", test_name, sub_test_name);
55     base::LogPerfResult(name.c_str(), value, units);
56   } else {
57     base::LogPerfResult(test_name, value, units);
58   }
59 }
60 
OpenSourceRootRelativeFile(const char * relative_path)61 FILE* TestSupportImpl::OpenSourceRootRelativeFile(const char* relative_path) {
62   return base::OpenFile(ResolveSourceRootRelativePath(relative_path), "rb");
63 }
64 
EnumerateSourceRootRelativeDirectory(const char * relative_path)65 char** TestSupportImpl::EnumerateSourceRootRelativeDirectory(
66     const char* relative_path) {
67   std::vector<std::string> names;
68   base::FileEnumerator e(ResolveSourceRootRelativePath(relative_path), false,
69                          base::FileEnumerator::FILES);
70   for (base::FilePath name = e.Next(); !name.empty(); name = e.Next())
71     names.push_back(name.BaseName().AsUTF8Unsafe());
72 
73   // |names.size() + 1| for null terminator.
74   char** rv = static_cast<char**>(calloc(names.size() + 1, sizeof(char*)));
75   for (size_t i = 0; i < names.size(); ++i)
76     rv[i] = base::strdup(names[i].c_str());
77   return rv;
78 }
79 
80 }  // namespace test
81 }  // namespace core
82 }  // namespace mojo
83