summaryrefslogtreecommitdiff
path: root/testing
diff options
context:
space:
mode:
authorWei Li <weili@chromium.org>2015-11-09 10:22:30 -0800
committerWei Li <weili@chromium.org>2015-11-09 10:22:30 -0800
commitc0e93a9a942fe7d99800502a61d2fbb58cf9276f (patch)
treed129c72f56be283d4436b9d493d590c6b3825500 /testing
parenta8f5b7b5e7501bdcfba585fc8af76c4a0fab22cb (diff)
downloadpdfium-c0e93a9a942fe7d99800502a61d2fbb58cf9276f.tar.xz
Add path service to retrieve test data directory at run time so tests can be run from any directory.
Previously the tests which read test files assume the current directory is under pdfium. Running from any other directory will break the build. R=thestig@chromium.org Review URL: https://codereview.chromium.org/1408003014 .
Diffstat (limited to 'testing')
-rw-r--r--testing/embedder_test.cpp10
-rw-r--r--testing/embedder_test.h2
-rw-r--r--testing/test_support.cpp8
-rw-r--r--testing/utils/path_service.cpp101
-rw-r--r--testing/utils/path_service.h37
5 files changed, 149 insertions, 9 deletions
diff --git a/testing/embedder_test.cpp b/testing/embedder_test.cpp
index 7bc7bdda7b..3664d92ce4 100644
--- a/testing/embedder_test.cpp
+++ b/testing/embedder_test.cpp
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "embedder_test.h"
+#include "testing/embedder_test.h"
#include <limits.h>
@@ -14,8 +14,9 @@
#include "public/fpdf_dataavail.h"
#include "public/fpdf_text.h"
#include "public/fpdfview.h"
-#include "test_support.h"
#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/test_support.h"
+#include "testing/utils/path_service.h"
#ifdef PDF_ENABLE_V8
#include "v8/include/v8.h"
@@ -94,7 +95,10 @@ void EmbedderTest::TearDown() {
bool EmbedderTest::OpenDocument(const std::string& filename,
bool must_linearize) {
- file_contents_ = GetFileContents(filename.c_str(), &file_length_);
+ std::string file_path;
+ if (!PathService::GetTestFilePath(filename, &file_path))
+ return false;
+ file_contents_ = GetFileContents(file_path.c_str(), &file_length_);
if (!file_contents_)
return false;
diff --git a/testing/embedder_test.h b/testing/embedder_test.h
index 3aa45dfcda..f68cb9df74 100644
--- a/testing/embedder_test.h
+++ b/testing/embedder_test.h
@@ -80,6 +80,8 @@ class EmbedderTest : public ::testing::Test,
// Open the document specified by |filename|, and create its form fill
// environment, or return false on failure.
+ // The filename is relative to the test data directory where we store all the
+ // test files.
virtual bool OpenDocument(const std::string& filename,
bool must_linearize = false);
diff --git a/testing/test_support.cpp b/testing/test_support.cpp
index 8ecda30bce..f71f6f8b4f 100644
--- a/testing/test_support.cpp
+++ b/testing/test_support.cpp
@@ -2,16 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "test_support.h"
+#include "testing/test_support.h"
#include <stdio.h>
#include <string.h>
-#ifdef _WIN32
-#define PATH_SEPARATOR '\\'
-#else
-#define PATH_SEPARATOR '/'
-#endif
+#include "testing/utils/path_service.h"
#ifdef PDF_ENABLE_V8
#include "v8/include/libplatform/libplatform.h"
diff --git a/testing/utils/path_service.cpp b/testing/utils/path_service.cpp
new file mode 100644
index 0000000000..1501a2da68
--- /dev/null
+++ b/testing/utils/path_service.cpp
@@ -0,0 +1,101 @@
+// Copyright 2015 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "testing/utils/path_service.h"
+
+#ifdef _WIN32
+#include <Windows.h>
+#elif defined(__APPLE__)
+#include <mach-o/dyld.h>
+#else // Linux
+#include <linux/limits.h>
+#include <unistd.h>
+#endif // _WIN32
+
+#include "core/include/fxcrt/fx_system.h"
+
+// static
+bool PathService::EndsWithSeparator(const std::string& path) {
+ return path.size() > 1 && path[path.size() - 1] == PATH_SEPARATOR;
+}
+
+// static
+bool PathService::GetExecutableDir(std::string* path) {
+// Get the current executable file path.
+#ifdef _WIN32
+ char path_buffer[MAX_PATH];
+ path_buffer[0] = 0;
+
+ if (GetModuleFileNameA(NULL, path_buffer, MAX_PATH) == 0)
+ return false;
+ *path = std::string(path_buffer);
+#elif defined(__APPLE__)
+ FXSYS_assert(path);
+ unsigned int path_length = 0;
+ _NSGetExecutablePath(NULL, &path_length);
+ if (path_length == 0)
+ return false;
+
+ path->reserve(path_length);
+ path->resize(path_length - 1);
+ if (_NSGetExecutablePath(&((*path)[0]), &path_length))
+ return false;
+#else // Linux
+ static const char kProcSelfExe[] = "/proc/self/exe";
+ char buf[PATH_MAX];
+ ssize_t count = ::readlink(kProcSelfExe, buf, PATH_MAX);
+ if (count <= 0)
+ return false;
+
+ *path = std::string(buf, count);
+#endif // _WIN32
+
+ // Get the directory path.
+ std::size_t pos = path->size() - 1;
+ if (EndsWithSeparator(*path))
+ pos--;
+ std::size_t found = path->find_last_of(PATH_SEPARATOR, pos);
+ if (found == std::string::npos)
+ return false;
+ path->resize(found);
+ return true;
+}
+
+// static
+bool PathService::GetSourceDir(std::string* path) {
+ if (!GetExecutableDir(path))
+ return false;
+
+ if (!EndsWithSeparator(*path))
+ path->push_back(PATH_SEPARATOR);
+ path->append("..");
+ path->push_back(PATH_SEPARATOR);
+ path->append("..");
+ return true;
+}
+
+// static
+bool PathService::GetTestDataDir(std::string* path) {
+ if (!GetSourceDir(path))
+ return false;
+
+ if (!EndsWithSeparator(*path))
+ path->push_back(PATH_SEPARATOR);
+ path->append("testing");
+ path->push_back(PATH_SEPARATOR);
+ path->append("resources");
+ return true;
+}
+
+// static
+bool PathService::GetTestFilePath(const std::string& file_name,
+ std::string* path) {
+ if (!GetTestDataDir(path))
+ return false;
+
+ if (!EndsWithSeparator(*path))
+ path->push_back(PATH_SEPARATOR);
+ path->append(file_name);
+ return true;
+}
diff --git a/testing/utils/path_service.h b/testing/utils/path_service.h
new file mode 100644
index 0000000000..96fd69e889
--- /dev/null
+++ b/testing/utils/path_service.h
@@ -0,0 +1,37 @@
+// Copyright 2015 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef TESTING_UTILS_PATH_SERVICE_H_
+#define TESTING_UTILS_PATH_SERVICE_H_
+
+#include <string>
+
+#ifdef _WIN32
+#define PATH_SEPARATOR '\\'
+#else
+#define PATH_SEPARATOR '/'
+#endif
+
+// Get the various file directory and path information.
+class PathService {
+ public:
+ // Return true when the path ends with a path separator.
+ static bool EndsWithSeparator(const std::string& path);
+
+ // Retrieve the directory where executables run from.
+ static bool GetExecutableDir(std::string* path);
+
+ // Retrieve the root directory of the source tree.
+ // Assume executables always run from out/<Debug|Release>/, the source
+ // directory is two levels above the executable directory.
+ static bool GetSourceDir(std::string* path);
+
+ // Retrieve the test data directory where test files are stored.
+ // Currently, the test dir is under <source_dir>/testing/resources/.
+ static bool GetTestDataDir(std::string* path);
+
+ // Get the full path for a test file under the test data directory.
+ static bool GetTestFilePath(const std::string& file_name, std::string* path);
+};
+#endif // TESTING_UTILS_PATH_SERVICE_H_