summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authorkoz@chromium.org <koz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-05-09 06:39:48 +0000
committerkoz@chromium.org <koz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-05-09 06:39:48 +0000
commit635e2c12b9c2fbfbbb86d18454690dc39aedc607 (patch)
tree5fb169b2adca3de321962b9cfd3180088b78746a /chrome
parentb75cf235a0b3830b03649dc492a93dc4365b8bd9 (diff)
downloadchromium_src-635e2c12b9c2fbfbbb86d18454690dc39aedc607.zip
chromium_src-635e2c12b9c2fbfbbb86d18454690dc39aedc607.tar.gz
chromium_src-635e2c12b9c2fbfbbb86d18454690dc39aedc607.tar.bz2
Extract ModuleSystemTest into its own file.
BUG=121479 TEST=existing tests Review URL: https://chromiumcodereview.appspot.com/10349005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@135995 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/chrome_tests.gypi2
-rw-r--r--chrome/renderer/module_system_unittest.cc109
-rw-r--r--chrome/test/base/module_system_test.cc116
-rw-r--r--chrome/test/base/module_system_test.h54
4 files changed, 175 insertions, 106 deletions
diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi
index 604edc7..4f315bb 100644
--- a/chrome/chrome_tests.gypi
+++ b/chrome/chrome_tests.gypi
@@ -211,6 +211,8 @@
'test/base/in_process_browser_test.h',
'test/base/model_test_utils.cc',
'test/base/model_test_utils.h',
+ 'test/base/module_system_test.cc',
+ 'test/base/module_system_test.h',
'test/base/profile_mock.cc',
'test/base/profile_mock.h',
'test/base/test_browser_window.cc',
diff --git a/chrome/renderer/module_system_unittest.cc b/chrome/renderer/module_system_unittest.cc
index f02a74c..45e7e30 100644
--- a/chrome/renderer/module_system_unittest.cc
+++ b/chrome/renderer/module_system_unittest.cc
@@ -2,38 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "base/callback.h"
+#include "chrome/test/base/module_system_test.h"
#include "base/memory/scoped_ptr.h"
-#include "base/string_piece.h"
#include "chrome/renderer/module_system.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-#include <map>
-#include <string>
-
-// Native JS functions for doing asserts.
-class AssertNatives : public NativeHandler {
- public:
- AssertNatives()
- : assertion_made_(false),
- failed_(false) {
- RouteFunction("AssertTrue", base::Bind(&AssertNatives::AssertTrue,
- base::Unretained(this)));
- }
-
- bool assertion_made() { return assertion_made_; }
- bool failed() { return failed_; }
-
- v8::Handle<v8::Value> AssertTrue(const v8::Arguments& args) {
- assertion_made_ = true;
- failed_ = failed_ || !args[0]->ToBoolean()->Value();
- return v8::Undefined();
- }
-
- private:
- bool assertion_made_;
- bool failed_;
-};
class CounterNatives : public NativeHandler {
public:
@@ -57,84 +28,9 @@ class CounterNatives : public NativeHandler {
int counter_;
};
-class StringSourceMap : public ModuleSystem::SourceMap {
- public:
- StringSourceMap() {}
- virtual ~StringSourceMap() {}
-
- v8::Handle<v8::Value> GetSource(const std::string& name) OVERRIDE {
- if (source_map_.count(name) == 0)
- return v8::Undefined();
- return v8::String::New(source_map_[name].c_str());
- }
-
- bool Contains(const std::string& name) OVERRIDE {
- return source_map_.count(name);
- }
-
- void RegisterModule(const std::string& name, const std::string& source) {
- source_map_[name] = source;
- }
-
- private:
- std::map<std::string, std::string> source_map_;
-};
-
-class ModuleSystemTest : public testing::Test {
- public:
- ModuleSystemTest()
- : context_(v8::Context::New()),
- source_map_(new StringSourceMap()),
- should_assertions_be_made_(true) {
- context_->Enter();
- assert_natives_ = new AssertNatives();
- module_system_.reset(new ModuleSystem(context_, source_map_.get()));
- module_system_->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>(
- assert_natives_));
- RegisterModule("add", "exports.Add = function(x, y) { return x + y; };");
- }
-
- ~ModuleSystemTest() {
- module_system_.reset();
- context_->Exit();
- context_.Dispose();
- }
-
- void RegisterModule(const std::string& name, const std::string& code) {
- source_map_->RegisterModule(name, code);
- }
-
- virtual void TearDown() {
- ASSERT_FALSE(try_catch_.HasCaught());
- // All tests must assert at least once unless otherwise specified.
- ASSERT_EQ(should_assertions_be_made_,
- assert_natives_->assertion_made());
- ASSERT_FALSE(assert_natives_->failed());
- }
-
- void ExpectNoAssertionsMade() {
- should_assertions_be_made_ = false;
- }
-
- v8::Handle<v8::Object> CreateGlobal(const std::string& name) {
- v8::HandleScope handle_scope;
- v8::Handle<v8::Object> object = v8::Object::New();
- v8::Context::GetCurrent()->Global()->Set(v8::String::New(name.c_str()),
- object);
- return handle_scope.Close(object);
- }
-
- v8::Persistent<v8::Context> context_;
- v8::HandleScope handle_scope_;
- v8::TryCatch try_catch_;
- AssertNatives* assert_natives_;
- scoped_ptr<StringSourceMap> source_map_;
- scoped_ptr<ModuleSystem> module_system_;
- bool should_assertions_be_made_;
-};
-
TEST_F(ModuleSystemTest, TestRequire) {
ModuleSystem::NativesEnabledScope natives_enabled_scope(module_system_.get());
+ RegisterModule("add", "exports.Add = function(x, y) { return x + y; };");
RegisterModule("test",
"var Add = require('add').Add;"
"requireNative('assert').AssertTrue(Add(3, 5) == 8);");
@@ -143,6 +39,7 @@ TEST_F(ModuleSystemTest, TestRequire) {
TEST_F(ModuleSystemTest, TestNestedRequire) {
ModuleSystem::NativesEnabledScope natives_enabled_scope(module_system_.get());
+ RegisterModule("add", "exports.Add = function(x, y) { return x + y; };");
RegisterModule("double",
"var Add = require('add').Add;"
"exports.Double = function(x) { return Add(x, x); };");
diff --git a/chrome/test/base/module_system_test.cc b/chrome/test/base/module_system_test.cc
new file mode 100644
index 0000000..2a4e529
--- /dev/null
+++ b/chrome/test/base/module_system_test.cc
@@ -0,0 +1,116 @@
+// Copyright (c) 2012 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 "chrome/test/base/module_system_test.h"
+
+#include "base/callback.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/string_piece.h"
+#include "chrome/renderer/native_handler.h"
+
+#include <map>
+#include <string>
+
+// Native JS functions for doing asserts.
+class AssertNatives : public NativeHandler {
+ public:
+ AssertNatives()
+ : assertion_made_(false),
+ failed_(false) {
+ RouteFunction("AssertTrue", base::Bind(&AssertNatives::AssertTrue,
+ base::Unretained(this)));
+ RouteFunction("AssertFalse", base::Bind(&AssertNatives::AssertFalse,
+ base::Unretained(this)));
+ }
+
+ bool assertion_made() { return assertion_made_; }
+ bool failed() { return failed_; }
+
+ v8::Handle<v8::Value> AssertTrue(const v8::Arguments& args) {
+ CHECK_EQ(1, args.Length());
+ assertion_made_ = true;
+ failed_ = failed_ || !args[0]->ToBoolean()->Value();
+ return v8::Undefined();
+ }
+
+ v8::Handle<v8::Value> AssertFalse(const v8::Arguments& args) {
+ CHECK_EQ(1, args.Length());
+ assertion_made_ = true;
+ failed_ = failed_ || args[0]->ToBoolean()->Value();
+ return v8::Undefined();
+ }
+
+ private:
+ bool assertion_made_;
+ bool failed_;
+};
+
+// Source map that operates on std::strings.
+class StringSourceMap : public ModuleSystem::SourceMap {
+ public:
+ StringSourceMap() {}
+ virtual ~StringSourceMap() {}
+
+ v8::Handle<v8::Value> GetSource(const std::string& name) OVERRIDE {
+ if (source_map_.count(name) == 0)
+ return v8::Undefined();
+ return v8::String::New(source_map_[name].c_str());
+ }
+
+ bool Contains(const std::string& name) OVERRIDE {
+ return source_map_.count(name);
+ }
+
+ void RegisterModule(const std::string& name, const std::string& source) {
+ CHECK_EQ(0u, source_map_.count(name));
+ source_map_[name] = source;
+ }
+
+ private:
+ std::map<std::string, std::string> source_map_;
+};
+
+ModuleSystemTest::ModuleSystemTest()
+ : context_(v8::Context::New()),
+ source_map_(new StringSourceMap()),
+ should_assertions_be_made_(true) {
+ context_->Enter();
+ assert_natives_ = new AssertNatives();
+ module_system_.reset(new ModuleSystem(context_, source_map_.get()));
+ module_system_->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>(
+ assert_natives_));
+ try_catch_.SetCaptureMessage(true);
+}
+
+ModuleSystemTest::~ModuleSystemTest() {
+ module_system_.reset();
+ context_->Exit();
+ context_.Dispose();
+}
+
+void ModuleSystemTest::RegisterModule(const std::string& name,
+ const std::string& code) {
+ source_map_->RegisterModule(name, code);
+}
+
+void ModuleSystemTest::TearDown() {
+ EXPECT_FALSE(try_catch_.HasCaught())
+ << *v8::String::Utf8Value(try_catch_.Message()->Get());
+ // All tests must assert at least once unless otherwise specified.
+ EXPECT_EQ(should_assertions_be_made_,
+ assert_natives_->assertion_made());
+ EXPECT_FALSE(assert_natives_->failed());
+}
+
+void ModuleSystemTest::ExpectNoAssertionsMade() {
+ should_assertions_be_made_ = false;
+}
+
+v8::Handle<v8::Object> ModuleSystemTest::CreateGlobal(const std::string& name) {
+ v8::HandleScope handle_scope;
+ v8::Handle<v8::Object> object = v8::Object::New();
+ v8::Context::GetCurrent()->Global()->Set(v8::String::New(name.c_str()),
+ object);
+ return handle_scope.Close(object);
+}
diff --git a/chrome/test/base/module_system_test.h b/chrome/test/base/module_system_test.h
new file mode 100644
index 0000000..71e3aa8
--- /dev/null
+++ b/chrome/test/base/module_system_test.h
@@ -0,0 +1,54 @@
+// Copyright (c) 2012 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 CHROME_TEST_BASE_MODULE_SYSTEM_TEST_H_
+#define CHROME_TEST_BASE_MODULE_SYSTEM_TEST_H_
+#pragma once
+
+#include "chrome/renderer/module_system.h"
+#include "v8/include/v8.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+class AssertNatives;
+class StringSourceMap;
+
+// Test fixture for testing JS that makes use of the module system.
+//
+// Typically tests will look like:
+//
+// TEST_F(MyModuleSystemTest, TestStuff) {
+// RegisterModule("test", "requireNative('assert').AssertTrue(true);");
+// module_system_->Require("test");
+// }
+//
+// By default a test will fail if no method in the native module 'assert' is
+// called. This behaviour can be overridden by calling ExpectNoAssertionsMade().
+class ModuleSystemTest : public testing::Test {
+ public:
+ ModuleSystemTest();
+ virtual ~ModuleSystemTest();
+
+ virtual void TearDown() OVERRIDE;
+
+ // Register a named JS module in the module system.
+ void RegisterModule(const std::string& name, const std::string& code);
+
+ // Make the test fail if any asserts are called. By default a test will fail
+ // if no asserts are called.
+ void ExpectNoAssertionsMade();
+
+ // Create an empty object in the global scope with name |name|.
+ v8::Handle<v8::Object> CreateGlobal(const std::string& name);
+
+ protected:
+ v8::Persistent<v8::Context> context_;
+ v8::HandleScope handle_scope_;
+ v8::TryCatch try_catch_;
+ AssertNatives* assert_natives_;
+ scoped_ptr<StringSourceMap> source_map_;
+ scoped_ptr<ModuleSystem> module_system_;
+ bool should_assertions_be_made_;
+};
+
+#endif // CHROME_TEST_BASE_MODULE_SYSTEM_TEST_H_