0

The mojo JS message header validation tests load text files that encode binary messages. A simple gin file access module would simplify writing the tests:

file.getSourceRootDirectory() - return the path to the root src directory

file.getFilesInDirectory(path) - return an array of the filenames found in path. Do not return subdirectory names or links.

file.readFileToString(path) - return the contents of the file as a string.

BUG=397554

Review URL: https://codereview.chromium.org/419673004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@286484 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
hansmuller@chromium.org
2014-07-30 11:44:46 +00:00
parent b3e1607da7
commit 2c1c10ff99
7 changed files with 155 additions and 0 deletions

@ -82,6 +82,8 @@ executable("gin_shell") {
source_set("gin_test") {
sources = [
"test/file.cc",
"test/file.h",
"test/file_runner.cc",
"test/file_runner.h",
"test/gc.cc",

@ -105,6 +105,8 @@
'gin',
],
'sources': [
'test/file.cc',
'test/file.h',
'test/file_runner.cc',
'test/file_runner.h',
'test/gc.cc',

85
gin/test/file.cc Normal file

@ -0,0 +1,85 @@
// Copyright 2014 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 "gin/test/file.h"
#include <iostream>
#include "base/bind.h"
#include "base/file_util.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/path_service.h"
#include "gin/arguments.h"
#include "gin/converter.h"
#include "gin/object_template_builder.h"
#include "gin/per_isolate_data.h"
#include "gin/public/wrapper_info.h"
using v8::ObjectTemplate;
namespace gin {
namespace {
v8::Handle<v8::Value> ReadFileToString(gin::Arguments* args) {
std::string filename;
if (!args->GetNext(&filename))
return v8::Null(args->isolate());
const base::FilePath& path = base::FilePath::FromUTF8Unsafe(filename);
std::string contents;
if (!ReadFileToString(path, &contents))
return v8::Null(args->isolate());
return gin::Converter<std::string>::ToV8(args->isolate(), contents);
}
v8::Handle<v8::Value> GetSourceRootDirectory(gin::Arguments* args) {
base::FilePath path;
if (!PathService::Get(base::DIR_SOURCE_ROOT, &path))
return v8::Null(args->isolate());
return gin::Converter<std::string>::ToV8(args->isolate(),
path.AsUTF8Unsafe());
}
v8::Handle<v8::Value> GetFilesInDirectory(gin::Arguments* args) {
std::string filename;
if (!args->GetNext(&filename))
return v8::Null(args->isolate());
const base::FilePath& path = base::FilePath::FromUTF8Unsafe(filename);
if (!base::DirectoryExists(path))
return v8::Null(args->isolate());
std::vector<std::string> names;
base::FileEnumerator e(path, false, base::FileEnumerator::FILES);
for (base::FilePath name = e.Next(); !name.empty(); name = e.Next()) {
names.push_back(name.BaseName().AsUTF8Unsafe());
}
return gin::Converter<std::vector<std::string>>::ToV8(args->isolate(), names);
}
gin::WrapperInfo g_wrapper_info = { gin::kEmbedderNativeGin };
} // namespace
const char File::kModuleName[] = "file";
v8::Local<v8::Value> File::GetModule(v8::Isolate* isolate) {
gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
v8::Local<ObjectTemplate> templ = data->GetObjectTemplate(&g_wrapper_info);
if (templ.IsEmpty()) {
templ = gin::ObjectTemplateBuilder(isolate)
.SetMethod("readFileToString", ReadFileToString)
.SetMethod("getFilesInDirectory", GetFilesInDirectory)
.SetMethod("getSourceRootDirectory", GetSourceRootDirectory)
.Build();
data->SetObjectTemplate(&g_wrapper_info, templ);
}
return templ->NewInstance();
}
} // namespace gin

21
gin/test/file.h Normal file

@ -0,0 +1,21 @@
// Copyright 2014 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.
#ifndef GIN_TEST_FILE_H_
#define GIN_TEST_FILE_H_
#include "v8/include/v8.h"
namespace gin {
class File {
public:
static const char kModuleName[];
static v8::Local<v8::Value> GetModule(v8::Isolate* isolate);
};
} // namespace gin
#endif // GIN_TEST_FILE_H_

@ -12,6 +12,7 @@
#include "gin/modules/module_registry.h"
#include "gin/public/context_holder.h"
#include "gin/public/isolate_holder.h"
#include "gin/test/file.h"
#include "gin/test/gc.h"
#include "gin/test/gtest.h"
#include "gin/try_catch.h"
@ -36,6 +37,7 @@ FileRunnerDelegate::FileRunnerDelegate()
AddBuiltinModule(Console::kModuleName, Console::GetModule);
AddBuiltinModule(GTest::kModuleName, GTest::GetModule);
AddBuiltinModule(GC::kModuleName, GC::GetModule);
AddBuiltinModule(File::kModuleName, File::GetModule);
}
FileRunnerDelegate::~FileRunnerDelegate() {

@ -0,0 +1,37 @@
// Copyright 2014 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.
define([
"gin/test/expect",
"file"
], function(expect, file) {
function isString(x) {
return toString.call(x) === '[object String]'
}
var rootDir = file.getSourceRootDirectory();
expect(isString(rootDir)).toBeTruthy();
var noArgsNull = file.getFilesInDirectory();
expect(noArgsNull).toBeNull();
var files = file.getFilesInDirectory(rootDir);
expect(Array.isArray(files)).toBeTruthy();
var nsdNull = file.getFilesInDirectory(rootDir + "/no_such_dir");
expect(nsdNull).toBeNull();
var owners = file.readFileToString(rootDir + "/OWNERS");
expect(isString(owners)).toBeTruthy();
expect(owners.length).toBeGreaterThan(0);
noArgsNull = file.readFileToString();
expect(noArgsNull).toBeNull();
var nsfNull = file.readFileToString(rootDir + "/no_such_file");
expect(nsfNull).toBeNull();
this.result = "PASS";
});

@ -21,6 +21,12 @@ void RunTest(const base::FilePath& path) {
RunTestFromFile(path, &delegate);
}
TEST(JSTest, File) {
RunTest(BasePath()
.AppendASCII("test")
.AppendASCII("file_unittests.js"));
}
TEST(JSTest, GTest) {
RunTest(BasePath()
.AppendASCII("test")