Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(312)

Unified Diff: testing/android/native_test_util.cc

Issue 12213035: Android: Refactor native test setup in a util class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« testing/android/native_test_util.h ('K') | « testing/android/native_test_util.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: testing/android/native_test_util.cc
diff --git a/testing/android/native_test_util.cc b/testing/android/native_test_util.cc
new file mode 100644
index 0000000000000000000000000000000000000000..b5109b5aa6847c49b4ce7a1330ee2323635087da
--- /dev/null
+++ b/testing/android/native_test_util.cc
@@ -0,0 +1,93 @@
+// Copyright (c) 2013 The Chromium 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/android/native_test_util.h"
+
+#include <android/log.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "base/command_line.h"
+#include "base/file_path.h"
+#include "base/file_util.h"
+#include "base/string_util.h"
+#include "base/strings/string_tokenizer.h"
+
+namespace {
+
+const char kLogTag[] = "chromium";
+
+void AndroidLogError(const char* format, ...) {
+ va_list args;
+ va_start(args, format);
+ __android_log_vprint(ANDROID_LOG_ERROR, kLogTag, format, args);
+ va_end(args);
+}
+
+void ParseArgsFromString(const std::string& command_line,
+ std::vector<std::string>* args) {
+ base::StringTokenizer tokenizer(command_line, kWhitespaceASCII);
+ tokenizer.set_quote_chars("\"");
+ while (tokenizer.GetNext()) {
+ std::string token;
+ RemoveChars(tokenizer.token(), "\"", &token);
+ args->push_back(token);
+ }
+}
+
+} // namespace
+
+namespace testing {
+
+NativeTestUtil::NativeTestUtil() {}
+
+NativeTestUtil::~NativeTestUtil() {}
+
+void NativeTestUtil::CreateFIFO(const char* fifo_path) {
+ unlink(fifo_path);
+ // Default permissions for mkfifo is ignored, chmod is required.
+ if (mkfifo(fifo_path, 0666) || chmod(fifo_path, 0666)) {
+ AndroidLogError("Failed to create fifo %s: %s\n",
+ fifo_path, strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+}
+
+void NativeTestUtil::RedirectStream(
+ FILE* stream, const char* path, const char* mode) {
+ if (!freopen(path, mode, stream)) {
+ AndroidLogError("Failed to redirect stream to file: %s: %s\n",
+ path, strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+}
+
+void NativeTestUtil::ParseArgsFromCommandLineFile(
+ const char* path, std::vector<std::string>* args) {
+ FilePath command_line(path);
+ std::string command_line_string;
+ if (file_util::ReadFileToString(command_line, &command_line_string)) {
+ ParseArgsFromString(command_line_string, args);
+ }
+}
+
+
+int NativeTestUtil::ArgsToArgv(const std::vector<std::string>& args,
+ std::vector<char*>* argv) {
+ // We need to pass in a non-const char**.
+ int argc = args.size();
+
+ argv->resize(argc + 1);
+ for (int i = 0; i < argc; ++i) {
+ (*argv)[i] = const_cast<char*>(args[i].c_str());
+ }
+ (*argv)[argc] = NULL; // argv must be NULL terminated.
+
+ return argc;
+}
+
+} // namespace testing
« testing/android/native_test_util.h ('K') | « testing/android/native_test_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698