summaryrefslogtreecommitdiffstats
path: root/gin/test
diff options
context:
space:
mode:
authorabarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-11-17 17:46:07 +0000
committerabarth@chromium.org <abarth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-11-17 17:46:07 +0000
commit97f21cad04bb80834a8cc84bfc3dd24a96531a16 (patch)
tree4e49aa68b49f90e4e68c220cc9d9b04be95a6a5d /gin/test
parent00509a3ac0c1c11e97852d2331d116df6754eb5d (diff)
downloadchromium_src-97f21cad04bb80834a8cc84bfc3dd24a96531a16.zip
chromium_src-97f21cad04bb80834a8cc84bfc3dd24a96531a16.tar.gz
chromium_src-97f21cad04bb80834a8cc84bfc3dd24a96531a16.tar.bz2
This CL implements the Asynchronous Module Definition (AMD)
API, which we plan to use for JavaScript in Mojo. We don't yet implement every feature in the AMD spec <https://github.com/amdjs/amdjs-api/wiki/AMD>, but we implement the basic framework, which will let us get started writing and testing JavaScript modules in Mojo. The two other leading choices for a modules system are CommonJS and ES6 modules. We decided not to use CommonJS, despite its popularity, because it implies the ability to load modules synchronously. That works well in server environments like node.js, but it won't work well for Mojo where modules might be loaded across a network. I would really like to have used ES6 modules, but the spec isn't finalized yet and V8 doesn't yet implement them. It's likely that we'll replace this AMD module system with ES6 modules once ES6 modules are ready. Structurally, I've implemented AMD in the ModuleRegistry class in a new "modules" directory in Gin. Nothing else in Gin (except the tests) depends on ModuleRegistry, which means folks are free to use Gin without AMD. At the Mojo layer, I've added a dependency on AMD. BUG=317398 Review URL: https://codereview.chromium.org/62333018 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@235543 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'gin/test')
-rw-r--r--gin/test/file_runner.cc64
-rw-r--r--gin/test/file_runner.h30
-rw-r--r--gin/test/gtest_unittests.js11
-rw-r--r--gin/test/run_js_tests.cc37
4 files changed, 142 insertions, 0 deletions
diff --git a/gin/test/file_runner.cc b/gin/test/file_runner.cc
new file mode 100644
index 0000000..a8d0cc2
--- /dev/null
+++ b/gin/test/file_runner.cc
@@ -0,0 +1,64 @@
+// Copyright 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 "gin/test/file_runner.h"
+
+#include "base/file_util.h"
+#include "gin/converter.h"
+#include "gin/modules/module_registry.h"
+#include "gin/test/gtest.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace gin {
+
+namespace {
+
+std::string GetExceptionInfo(const v8::TryCatch& try_catch) {
+ std::string info;
+ ConvertFromV8(try_catch.Message()->Get(), &info);
+ return info;
+}
+
+} // namespace
+
+FileRunnerDelegate::FileRunnerDelegate() {
+}
+
+FileRunnerDelegate::~FileRunnerDelegate() {
+}
+
+v8::Handle<v8::ObjectTemplate> FileRunnerDelegate::GetGlobalTemplate(
+ Runner* runner) {
+ v8::Handle<v8::ObjectTemplate> templ = v8::ObjectTemplate::New();
+ ModuleRegistry::RegisterGlobals(runner->isolate(), templ);
+ return templ;
+}
+
+void FileRunnerDelegate::DidCreateContext(Runner* runner) {
+ v8::Handle<v8::Context> context = runner->context();
+ ModuleRegistry* registry = ModuleRegistry::From(context);
+
+ registry->AddBuiltinModule(runner->isolate(), "gtest",
+ GetGTestTemplate(runner->isolate()));
+}
+
+void RunTestFromFile(const base::FilePath& path, RunnerDelegate* delegate) {
+ ASSERT_TRUE(base::PathExists(path)) << path.LossyDisplayName();
+ std::string source;
+ ASSERT_TRUE(ReadFileToString(path, &source));
+ gin::Runner runner(delegate, v8::Isolate::GetCurrent());
+ gin::Runner::Scope scope(&runner);
+
+ v8::TryCatch try_catch;
+ runner.Run(source);
+ EXPECT_FALSE(try_catch.HasCaught()) << GetExceptionInfo(try_catch);
+
+ v8::Handle<v8::Value> result = runner.context()->Global()->Get(
+ StringToSymbol(runner.isolate(), "result"));
+ std::string result_string;
+ ASSERT_TRUE(ConvertFromV8(result, &result_string));
+ EXPECT_EQ("PASS", result_string);
+}
+
+} // namespace gin
diff --git a/gin/test/file_runner.h b/gin/test/file_runner.h
new file mode 100644
index 0000000..b4ca00e
--- /dev/null
+++ b/gin/test/file_runner.h
@@ -0,0 +1,30 @@
+// Copyright 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.
+
+#ifndef GIN_TEST_FILE_RUNNER_H_
+#define GIN_TEST_FILE_RUNNER_H_
+
+#include "base/compiler_specific.h"
+#include "base/files/file_path.h"
+#include "gin/runner.h"
+
+namespace gin {
+
+class FileRunnerDelegate : public RunnerDelegate {
+ public:
+ FileRunnerDelegate();
+ virtual ~FileRunnerDelegate();
+ virtual v8::Handle<v8::ObjectTemplate> GetGlobalTemplate(
+ Runner* runner) OVERRIDE;
+ virtual void DidCreateContext(Runner* runner) OVERRIDE;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(FileRunnerDelegate);
+};
+
+void RunTestFromFile(const base::FilePath& path, RunnerDelegate* delegate);
+
+} // namespace gin
+
+#endif // GIN_TEST_FILE_RUNNER_H_
diff --git a/gin/test/gtest_unittests.js b/gin/test/gtest_unittests.js
new file mode 100644
index 0000000..1d566d5
--- /dev/null
+++ b/gin/test/gtest_unittests.js
@@ -0,0 +1,11 @@
+// Copyright 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.
+
+define(["gtest"], function(gtest) {
+ gtest.expectTrue(true, "true is true");
+ gtest.expectFalse(false, "false is false");
+ gtest.expectTrue(this, "this is " + this);
+
+ this.result = "PASS";
+});
diff --git a/gin/test/run_js_tests.cc b/gin/test/run_js_tests.cc
new file mode 100644
index 0000000..0b7c41a
--- /dev/null
+++ b/gin/test/run_js_tests.cc
@@ -0,0 +1,37 @@
+// Copyright 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 "base/file_util.h"
+#include "base/path_service.h"
+#include "gin/test/file_runner.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace gin {
+namespace {
+
+base::FilePath BasePath() {
+ base::FilePath path;
+ PathService::Get(base::DIR_SOURCE_ROOT, &path);
+ return path.AppendASCII("gin");
+}
+
+void RunTest(const base::FilePath& path) {
+ FileRunnerDelegate delegate;
+ RunTestFromFile(path, &delegate);
+}
+
+TEST(JSTest, GTest) {
+ RunTest(BasePath()
+ .AppendASCII("test")
+ .AppendASCII("gtest_unittests.js"));
+}
+
+TEST(JSTest, ModuleRegistry) {
+ RunTest(BasePath()
+ .AppendASCII("modules")
+ .AppendASCII("module_registry_unittests.js"));
+}
+
+} // namespace
+} // gin