1c55a96383497a772a307b346368133960b02ad03Eric Laurent/*
2c55a96383497a772a307b346368133960b02ad03Eric Laurent *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3c55a96383497a772a307b346368133960b02ad03Eric Laurent *
4c55a96383497a772a307b346368133960b02ad03Eric Laurent *  Use of this source code is governed by a BSD-style license
5c55a96383497a772a307b346368133960b02ad03Eric Laurent *  that can be found in the LICENSE file in the root of the source
6c55a96383497a772a307b346368133960b02ad03Eric Laurent *  tree. An additional intellectual property rights grant can be found
7c55a96383497a772a307b346368133960b02ad03Eric Laurent *  in the file PATENTS.  All contributing project authors may
8c55a96383497a772a307b346368133960b02ad03Eric Laurent *  be found in the AUTHORS file in the root of the source tree.
9c55a96383497a772a307b346368133960b02ad03Eric Laurent */
10c55a96383497a772a307b346368133960b02ad03Eric Laurent
11c55a96383497a772a307b346368133960b02ad03Eric Laurent#include <cstdio>
12c55a96383497a772a307b346368133960b02ad03Eric Laurent
13c55a96383497a772a307b346368133960b02ad03Eric Laurent// File utilities for testing purposes.
14c55a96383497a772a307b346368133960b02ad03Eric Laurent//
15c55a96383497a772a307b346368133960b02ad03Eric Laurent// The ProjectRootPath() method is a convenient way of getting an absolute
16c55a96383497a772a307b346368133960b02ad03Eric Laurent// path to the project source tree root directory. Using this, it is easy to
17c55a96383497a772a307b346368133960b02ad03Eric Laurent// refer to test resource files in a portable way.
18c55a96383497a772a307b346368133960b02ad03Eric Laurent//
19c55a96383497a772a307b346368133960b02ad03Eric Laurent// Notice that even if Windows platforms use backslash as path delimiter, it is
20c55a96383497a772a307b346368133960b02ad03Eric Laurent// also supported to use slash, so there's no need for #ifdef checks in test
21c55a96383497a772a307b346368133960b02ad03Eric Laurent// code for setting up the paths to the resource files.
22c55a96383497a772a307b346368133960b02ad03Eric Laurent//
23c55a96383497a772a307b346368133960b02ad03Eric Laurent// Example use:
24c55a96383497a772a307b346368133960b02ad03Eric Laurent// Assume we have the following code being used in a test source file:
25c55a96383497a772a307b346368133960b02ad03Eric Laurent// const std::string kInputFile = webrtc::test::ProjectRootPath() +
26c55a96383497a772a307b346368133960b02ad03Eric Laurent//     "test/data/voice_engine/audio_long16.wav";
27c55a96383497a772a307b346368133960b02ad03Eric Laurent// // Use the kInputFile for the tests...
28c55a96383497a772a307b346368133960b02ad03Eric Laurent//
29c55a96383497a772a307b346368133960b02ad03Eric Laurent// Then here's some example outputs for different platforms:
30c55a96383497a772a307b346368133960b02ad03Eric Laurent// Linux:
31c55a96383497a772a307b346368133960b02ad03Eric Laurent// * Source tree located in /home/user/webrtc/trunk
32c55a96383497a772a307b346368133960b02ad03Eric Laurent// * Test project located in /home/user/webrtc/trunk/src/testproject
33c55a96383497a772a307b346368133960b02ad03Eric Laurent// * Test binary compiled as:
34c55a96383497a772a307b346368133960b02ad03Eric Laurent//   /home/user/webrtc/trunk/out/Debug/testproject_unittests
35c55a96383497a772a307b346368133960b02ad03Eric Laurent// Then ProjectRootPath() will return /home/user/webrtc/trunk/ no matter if
36c55a96383497a772a307b346368133960b02ad03Eric Laurent// the test binary is executed from standing in either of:
37c55a96383497a772a307b346368133960b02ad03Eric Laurent// /home/user/webrtc/trunk
38c55a96383497a772a307b346368133960b02ad03Eric Laurent// or
39c55a96383497a772a307b346368133960b02ad03Eric Laurent// /home/user/webrtc/trunk/out/Debug
40c55a96383497a772a307b346368133960b02ad03Eric Laurent// (or any other directory below the trunk for that matter).
41c55a96383497a772a307b346368133960b02ad03Eric Laurent//
42c55a96383497a772a307b346368133960b02ad03Eric Laurent// Windows:
43c55a96383497a772a307b346368133960b02ad03Eric Laurent// * Source tree located in C:\Users\user\webrtc\trunk
44c55a96383497a772a307b346368133960b02ad03Eric Laurent// * Test project located in C:\Users\user\webrtc\trunk\src\testproject
45c55a96383497a772a307b346368133960b02ad03Eric Laurent// * Test binary compiled as:
46c55a96383497a772a307b346368133960b02ad03Eric Laurent//   C:\Users\user\webrtc\trunk\src\testproject\Debug\testproject_unittests.exe
47c55a96383497a772a307b346368133960b02ad03Eric Laurent// Then ProjectRootPath() will return C:\Users\user\webrtc\trunk\ when the
48c55a96383497a772a307b346368133960b02ad03Eric Laurent// test binary is executed from inside Visual Studio.
49c55a96383497a772a307b346368133960b02ad03Eric Laurent// It will also return the same path if the test is executed from a command
50c55a96383497a772a307b346368133960b02ad03Eric Laurent// prompt standing in C:\Users\user\webrtc\trunk\src\testproject\Debug
51c55a96383497a772a307b346368133960b02ad03Eric Laurent//
52c55a96383497a772a307b346368133960b02ad03Eric Laurent// Mac:
53c55a96383497a772a307b346368133960b02ad03Eric Laurent// * Source tree located in /Users/user/webrtc/trunk
54c55a96383497a772a307b346368133960b02ad03Eric Laurent// * Test project located in /Users/user/webrtc/trunk/src/testproject
55c55a96383497a772a307b346368133960b02ad03Eric Laurent// * Test binary compiled as:
56c55a96383497a772a307b346368133960b02ad03Eric Laurent//   /Users/user/webrtc/trunk/xcodebuild/Debug/testproject_unittests
57c55a96383497a772a307b346368133960b02ad03Eric Laurent// Then ProjectRootPath() will return /Users/user/webrtc/trunk/ no matter if
58c55a96383497a772a307b346368133960b02ad03Eric Laurent// the test binary is executed from standing in either of:
59c55a96383497a772a307b346368133960b02ad03Eric Laurent// /Users/user/webrtc/trunk
60c55a96383497a772a307b346368133960b02ad03Eric Laurent// or
61c55a96383497a772a307b346368133960b02ad03Eric Laurent// /Users/user/webrtc/trunk/out/Debug
62c55a96383497a772a307b346368133960b02ad03Eric Laurent// (or any other directory below the trunk for that matter).
63c55a96383497a772a307b346368133960b02ad03Eric Laurent
64c55a96383497a772a307b346368133960b02ad03Eric Laurent#ifndef WEBRTC_TEST_TESTSUPPORT_FILEUTILS_H_
65c55a96383497a772a307b346368133960b02ad03Eric Laurent#define WEBRTC_TEST_TESTSUPPORT_FILEUTILS_H_
66c55a96383497a772a307b346368133960b02ad03Eric Laurent
67c55a96383497a772a307b346368133960b02ad03Eric Laurent#include <string>
68c55a96383497a772a307b346368133960b02ad03Eric Laurent
69c55a96383497a772a307b346368133960b02ad03Eric Laurentnamespace webrtc {
70c55a96383497a772a307b346368133960b02ad03Eric Laurentnamespace test {
71c55a96383497a772a307b346368133960b02ad03Eric Laurent
72c55a96383497a772a307b346368133960b02ad03Eric Laurent// This is the "directory" returned if the ProjectPath() function fails
73c55a96383497a772a307b346368133960b02ad03Eric Laurent// to find the project root.
74c55a96383497a772a307b346368133960b02ad03Eric Laurentextern const char* kCannotFindProjectRootDir;
75c55a96383497a772a307b346368133960b02ad03Eric Laurent
76c55a96383497a772a307b346368133960b02ad03Eric Laurent// Finds the root dir of the project, to be able to set correct paths to
77c55a96383497a772a307b346368133960b02ad03Eric Laurent// resource files used by tests.
78c55a96383497a772a307b346368133960b02ad03Eric Laurent// The implementation is simple: it just looks for the file defined by
79c55a96383497a772a307b346368133960b02ad03Eric Laurent// kProjectRootFileName, starting in the current directory (the working
80c55a96383497a772a307b346368133960b02ad03Eric Laurent// directory) and then steps upward until it is found (or it is at the root of
81c55a96383497a772a307b346368133960b02ad03Eric Laurent// the file system).
82c55a96383497a772a307b346368133960b02ad03Eric Laurent// If the current working directory is above the project root dir, it will not
83c55a96383497a772a307b346368133960b02ad03Eric Laurent// be found.
84c55a96383497a772a307b346368133960b02ad03Eric Laurent//
85c55a96383497a772a307b346368133960b02ad03Eric Laurent// If symbolic links occur in the path they will be resolved and the actual
86c55a96383497a772a307b346368133960b02ad03Eric Laurent// directory will be returned.
87c55a96383497a772a307b346368133960b02ad03Eric Laurent//
88c55a96383497a772a307b346368133960b02ad03Eric Laurent// Returns the absolute path to the project root dir (usually the trunk dir)
89c55a96383497a772a307b346368133960b02ad03Eric Laurent// WITH a trailing path delimiter.
90c55a96383497a772a307b346368133960b02ad03Eric Laurent// If the project root is not found, the string specified by
91c55a96383497a772a307b346368133960b02ad03Eric Laurent// kCannotFindProjectRootDir is returned.
92c55a96383497a772a307b346368133960b02ad03Eric Laurentstd::string ProjectRootPath();
93c55a96383497a772a307b346368133960b02ad03Eric Laurent
94c55a96383497a772a307b346368133960b02ad03Eric Laurent// Creates and returns the absolute path to the output directory where log files
95c55a96383497a772a307b346368133960b02ad03Eric Laurent// and other test artifacts should be put. The output directory is always a
96c55a96383497a772a307b346368133960b02ad03Eric Laurent// directory named "out" at the top-level of the project, i.e. a subfolder to
97c55a96383497a772a307b346368133960b02ad03Eric Laurent// the path returned by ProjectRootPath().
98c55a96383497a772a307b346368133960b02ad03Eric Laurent//
99c55a96383497a772a307b346368133960b02ad03Eric Laurent// Details described for ProjectRootPath() apply here too.
100c55a96383497a772a307b346368133960b02ad03Eric Laurent//
101c55a96383497a772a307b346368133960b02ad03Eric Laurent// Returns the path WITH a trailing path delimiter. If the project root is not
102c55a96383497a772a307b346368133960b02ad03Eric Laurent// found, the current working directory ("./") is returned as a fallback.
103c55a96383497a772a307b346368133960b02ad03Eric Laurentstd::string OutputPath();
104c55a96383497a772a307b346368133960b02ad03Eric Laurent
105c55a96383497a772a307b346368133960b02ad03Eric Laurent// Returns a path to a resource file for the currently executing platform.
106c55a96383497a772a307b346368133960b02ad03Eric Laurent// Adapts to what filenames are currently present in the
107c55a96383497a772a307b346368133960b02ad03Eric Laurent// [project-root]/resources/ dir.
108c55a96383497a772a307b346368133960b02ad03Eric Laurent// Returns an absolute path according to this priority list (the directory
109c55a96383497a772a307b346368133960b02ad03Eric Laurent// part of the path is left out for readability):
110c55a96383497a772a307b346368133960b02ad03Eric Laurent// 1. [name]_[platform]_[architecture].[extension]
111c55a96383497a772a307b346368133960b02ad03Eric Laurent// 2. [name]_[platform].[extension]
112c55a96383497a772a307b346368133960b02ad03Eric Laurent// 3. [name]_[architecture].[extension]
113c55a96383497a772a307b346368133960b02ad03Eric Laurent// 4. [name].[extension]
114c55a96383497a772a307b346368133960b02ad03Eric Laurent// Where
115c55a96383497a772a307b346368133960b02ad03Eric Laurent// * platform is either of "win", "mac" or "linux".
116c55a96383497a772a307b346368133960b02ad03Eric Laurent// * architecture is either of "32" or "64".
117c55a96383497a772a307b346368133960b02ad03Eric Laurent//
118c55a96383497a772a307b346368133960b02ad03Eric Laurent// Arguments:
119c55a96383497a772a307b346368133960b02ad03Eric Laurent//    name - Name of the resource file. If a plain filename (no directory path)
120c55a96383497a772a307b346368133960b02ad03Eric Laurent//           is supplied, the file is assumed to be located in resources/
121c55a96383497a772a307b346368133960b02ad03Eric Laurent//           If a directory path is prepended to the filename, a subdirectory
122c55a96383497a772a307b346368133960b02ad03Eric Laurent//           hierarchy reflecting that path is assumed to be present.
123c55a96383497a772a307b346368133960b02ad03Eric Laurent//    extension - File extension, without the dot, i.e. "bmp" or "yuv".
124c55a96383497a772a307b346368133960b02ad03Eric Laurentstd::string ResourcePath(std::string name, std::string extension);
125c55a96383497a772a307b346368133960b02ad03Eric Laurent
126c55a96383497a772a307b346368133960b02ad03Eric Laurent// Gets the current working directory for the executing program.
127c55a96383497a772a307b346368133960b02ad03Eric Laurent// Returns "./" if for some reason it is not possible to find the working
128c55a96383497a772a307b346368133960b02ad03Eric Laurent// directory.
129c55a96383497a772a307b346368133960b02ad03Eric Laurentstd::string WorkingDir();
130c55a96383497a772a307b346368133960b02ad03Eric Laurent
131c55a96383497a772a307b346368133960b02ad03Eric Laurent// Creates a directory if it not already exists.
132c55a96383497a772a307b346368133960b02ad03Eric Laurent// Returns true if successful. Will print an error message to stderr and return
133c55a96383497a772a307b346368133960b02ad03Eric Laurent// false if a file with the same name already exists.
134c55a96383497a772a307b346368133960b02ad03Eric Laurentbool CreateDirectory(std::string directory_name);
135c55a96383497a772a307b346368133960b02ad03Eric Laurent
136c55a96383497a772a307b346368133960b02ad03Eric Laurent// File size of the supplied file in bytes. Will return 0 if the file is
137c55a96383497a772a307b346368133960b02ad03Eric Laurent// empty or if the file does not exist/is readable.
138c55a96383497a772a307b346368133960b02ad03Eric Laurentsize_t GetFileSize(std::string filename);
139c55a96383497a772a307b346368133960b02ad03Eric Laurent
140c55a96383497a772a307b346368133960b02ad03Eric Laurent}  // namespace test
141c55a96383497a772a307b346368133960b02ad03Eric Laurent}  // namespace webrtc
142c55a96383497a772a307b346368133960b02ad03Eric Laurent
143c55a96383497a772a307b346368133960b02ad03Eric Laurent#endif  // WEBRTC_TEST_TESTSUPPORT_FILEUTILS_H_
144