blob: 482b1874a7f0fd2f8655bef0cbaa1fe1b48b347c [file] [log] [blame]
kjellander@webrtc.org7951e812011-10-13 12:24:41 +00001/*
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
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000011#include <stdio.h>
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000012
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000013// File utilities for testing purposes.
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000014//
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000015// The ProjectRootPath() method is a convenient way of getting an absolute
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000016// 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:
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000025// const std::string kInputFile = webrtc::test::ProjectRootPath() +
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000026// "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
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000035// Then ProjectRootPath() will return /home/user/webrtc/trunk/ no matter if
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000036// 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
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000047// Then ProjectRootPath() will return C:\Users\user\webrtc\trunk\ when the
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000048// 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
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000057// Then ProjectRootPath() will return /Users/user/webrtc/trunk/ no matter if
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000058// 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
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000064#ifndef WEBRTC_TEST_TESTSUPPORT_FILEUTILS_H_
65#define WEBRTC_TEST_TESTSUPPORT_FILEUTILS_H_
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000066
67#include <string>
68
69namespace webrtc {
70namespace test {
71
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000072// This is the "directory" returned if the ProjectPath() function fails
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000073// to find the project root.
andrew@webrtc.org1e10bb32011-10-31 20:22:02 +000074extern const char* kCannotFindProjectRootDir;
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000075
76// Finds the root dir of the project, to be able to set correct paths to
77// resource files used by tests.
ehmaldonado8b28b802016-09-15 04:45:48 -070078// For desktop, we assume that the project root is two levels above (i.e. the
79// current working directory + /../../)
80// For Android, it is assumed to be /sdcard/chromium_tests_root/
81// For iOS, the resource files are assumed to be included in the test's .app
82// bundle.
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000083// If the current working directory is above the project root dir, it will not
84// be found.
85//
86// If symbolic links occur in the path they will be resolved and the actual
87// directory will be returned.
88//
89// Returns the absolute path to the project root dir (usually the trunk dir)
90// WITH a trailing path delimiter.
91// If the project root is not found, the string specified by
92// kCannotFindProjectRootDir is returned.
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000093std::string ProjectRootPath();
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000094
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000095// Creates and returns the absolute path to the output directory where log files
phoglund@webrtc.org4aa57b42012-03-22 12:56:54 +000096// and other test artifacts should be put. The output directory is generally a
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000097// directory named "out" at the top-level of the project, i.e. a subfolder to
phoglund@webrtc.org4aa57b42012-03-22 12:56:54 +000098// the path returned by ProjectRootPath(). The exception is Android where we use
99// /sdcard/ instead.
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000100//
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000101// Details described for ProjectRootPath() apply here too.
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000102//
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000103// Returns the path WITH a trailing path delimiter. If the project root is not
104// found, the current working directory ("./") is returned as a fallback.
105std::string OutputPath();
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000106
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000107// Generates an empty file with a unique name in the specified directory and
108// returns the file name and path.
109std::string TempFilename(const std::string &dir, const std::string &prefix);
110
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000111// Returns a path to a resource file for the currently executing platform.
112// Adapts to what filenames are currently present in the
113// [project-root]/resources/ dir.
114// Returns an absolute path according to this priority list (the directory
115// part of the path is left out for readability):
116// 1. [name]_[platform]_[architecture].[extension]
117// 2. [name]_[platform].[extension]
118// 3. [name]_[architecture].[extension]
119// 4. [name].[extension]
120// Where
121// * platform is either of "win", "mac" or "linux".
122// * architecture is either of "32" or "64".
123//
124// Arguments:
125// name - Name of the resource file. If a plain filename (no directory path)
126// is supplied, the file is assumed to be located in resources/
127// If a directory path is prepended to the filename, a subdirectory
128// hierarchy reflecting that path is assumed to be present.
129// extension - File extension, without the dot, i.e. "bmp" or "yuv".
130std::string ResourcePath(std::string name, std::string extension);
131
132// Gets the current working directory for the executing program.
133// Returns "./" if for some reason it is not possible to find the working
134// directory.
135std::string WorkingDir();
136
137// Creates a directory if it not already exists.
138// Returns true if successful. Will print an error message to stderr and return
139// false if a file with the same name already exists.
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000140bool CreateDir(std::string directory_name);
141
142// Checks if a file exists.
143bool FileExists(std::string& file_name);
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000144
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000145// File size of the supplied file in bytes. Will return 0 if the file is
146// empty or if the file does not exist/is readable.
147size_t GetFileSize(std::string filename);
148
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000149// Sets the executable path, i.e. the path to the executable that is being used
150// when launching it. This is usually the path relative to the working directory
151// but can also be an absolute path. The intention with this function is to pass
152// the argv[0] being sent into the main function to make it possible for
153// fileutils.h to find the correct project paths even when the working directory
154// is outside the project tree (which happens in some cases).
155void SetExecutablePath(const std::string& path_to_executable);
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000156
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000157} // namespace test
andrew@webrtc.org1e10bb32011-10-31 20:22:02 +0000158} // namespace webrtc
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000159
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000160#endif // WEBRTC_TEST_TESTSUPPORT_FILEUTILS_H_