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