| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/callback.h" | 5 #include "base/callback.h" |
| 6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/string_piece.h" | |
| 8 #include "chrome/renderer/module_system.h" | 7 #include "chrome/renderer/module_system.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 9 |
| 11 #include <map> | 10 #include <map> |
| 12 #include <string> | 11 #include <string> |
| 13 | 12 |
| 14 // Native JS functions for doing asserts. | |
| 15 class AssertNatives : public NativeHandler { | 13 class AssertNatives : public NativeHandler { |
| 16 public: | 14 public: |
| 17 AssertNatives() | 15 AssertNatives() |
| 18 : native_function_called_(false), | 16 : native_function_called_(false), |
| 19 failed_(false) { | 17 failed_(false) { |
| 20 RouteFunction("AssertTrue", base::Bind(&AssertNatives::AssertTrue, | 18 RouteFunction("AssertTrue", base::Bind(&AssertNatives::AssertTrue, |
| 21 base::Unretained(this))); | 19 base::Unretained(this))); |
| 22 } | 20 } |
| 23 | 21 |
| 24 bool native_function_called() { return native_function_called_; } | 22 bool native_function_called() { return native_function_called_; } |
| 25 bool failed() { return failed_; } | 23 bool failed() { return failed_; } |
| 26 | 24 |
| 27 v8::Handle<v8::Value> AssertTrue(const v8::Arguments& args) { | 25 v8::Handle<v8::Value> AssertTrue(const v8::Arguments& args) { |
| 28 native_function_called_ = true; | 26 native_function_called_ = true; |
| 29 failed_ = failed_ || !args[0]->ToBoolean()->Value(); | 27 failed_ = failed_ || !args[0]->ToBoolean()->Value(); |
| 30 return v8::Undefined(); | 28 return v8::Undefined(); |
| 31 } | 29 } |
| 32 | 30 |
| 33 private: | 31 private: |
| 34 bool native_function_called_; | 32 bool native_function_called_; |
| 35 bool failed_; | 33 bool failed_; |
| 36 }; | 34 }; |
| 37 | 35 |
| 38 class StringSourceMap : public ModuleSystem::SourceMap { | |
| 39 public: | |
| 40 StringSourceMap() {} | |
| 41 | |
| 42 v8::Handle<v8::Value> GetSource(const std::string& name) OVERRIDE { | |
| 43 if (source_map_.count(name) == 0) | |
| 44 return v8::Undefined(); | |
| 45 return v8::String::New(source_map_[name].c_str()); | |
| 46 } | |
| 47 | |
| 48 bool Contains(const std::string& name) OVERRIDE { | |
| 49 return source_map_.count(name); | |
| 50 } | |
| 51 | |
| 52 void RegisterModule(const std::string& name, const std::string& source) { | |
| 53 source_map_[name] = source; | |
| 54 } | |
| 55 | |
| 56 private: | |
| 57 std::map<std::string, std::string> source_map_; | |
| 58 }; | |
| 59 | |
| 60 // Native JS functions for disabling injection in ModuleSystem. | |
| 61 class DisableNativesHandler : public NativeHandler { | |
| 62 public: | |
| 63 explicit DisableNativesHandler(ModuleSystem* module_system) | |
| 64 : module_system_(module_system) { | |
| 65 RouteFunction("DisableNatives", | |
| 66 base::Bind(&DisableNativesHandler::DisableNatives, | |
| 67 base::Unretained(this))); | |
| 68 } | |
| 69 | |
| 70 v8::Handle<v8::Value> DisableNatives(const v8::Arguments& args) { | |
| 71 module_system_->set_natives_enabled(false); | |
| 72 return v8::Undefined(); | |
| 73 } | |
| 74 | |
| 75 private: | |
| 76 ModuleSystem* module_system_; | |
| 77 }; | |
| 78 | |
| 79 class ModuleSystemTest : public testing::Test { | 36 class ModuleSystemTest : public testing::Test { |
| 80 public: | 37 public: |
| 81 ModuleSystemTest() | 38 ModuleSystemTest() |
| 82 : context_(v8::Context::New()), | 39 : context_(v8::Context::New()), |
| 83 handle_scope_(), | 40 assert_natives_(new AssertNatives()) { |
| 84 assert_natives_(new AssertNatives()), | |
| 85 source_map_(new StringSourceMap()), | |
| 86 module_system_(new ModuleSystem(source_map_)) { | |
| 87 context_->Enter(); | 41 context_->Enter(); |
| 42 source_map_["add"] = "exports.Add = function(x, y) { return x + y; };"; |
| 43 module_system_.reset(new ModuleSystem(&source_map_)); |
| 88 module_system_->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>( | 44 module_system_->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>( |
| 89 assert_natives_)); | 45 assert_natives_)); |
| 90 RegisterModule("add", "exports.Add = function(x, y) { return x + y; };"); | |
| 91 } | 46 } |
| 92 | 47 |
| 93 ~ModuleSystemTest() { | 48 ~ModuleSystemTest() { |
| 94 context_.Dispose(); | 49 context_.Dispose(); |
| 95 } | 50 } |
| 96 | 51 |
| 97 void RegisterModule(const std::string& name, const std::string& code) { | |
| 98 source_map_->RegisterModule(name, code); | |
| 99 } | |
| 100 | |
| 101 virtual void TearDown() { | 52 virtual void TearDown() { |
| 102 // All tests must call a native function at least once. | 53 // All tests must call a native function at least once. |
| 103 ASSERT_TRUE(assert_natives_->native_function_called()); | 54 ASSERT_TRUE(assert_natives_->native_function_called()); |
| 104 ASSERT_FALSE(assert_natives_->failed()); | 55 ASSERT_FALSE(assert_natives_->failed()); |
| 105 ASSERT_FALSE(try_catch_.HasCaught()); | 56 ASSERT_FALSE(try_catch_.HasCaught()); |
| 106 } | 57 } |
| 107 | 58 |
| 108 v8::Persistent<v8::Context> context_; | |
| 109 v8::HandleScope handle_scope_; | 59 v8::HandleScope handle_scope_; |
| 110 v8::TryCatch try_catch_; | 60 v8::TryCatch try_catch_; |
| 61 v8::Persistent<v8::Context> context_; |
| 111 AssertNatives* assert_natives_; | 62 AssertNatives* assert_natives_; |
| 112 StringSourceMap* source_map_; | 63 std::map<std::string, std::string> source_map_; |
| 113 scoped_ptr<ModuleSystem> module_system_; | 64 scoped_ptr<ModuleSystem> module_system_; |
| 114 }; | 65 }; |
| 115 | 66 |
| 116 TEST_F(ModuleSystemTest, TestRequire) { | 67 TEST_F(ModuleSystemTest, TestRequire) { |
| 117 RegisterModule("test", | 68 source_map_["test"] = |
| 118 "var Add = require('add').Add;" | 69 "var Add = require('add').Add;" |
| 119 "requireNative('assert').AssertTrue(Add(3, 5) == 8);"); | 70 "requireNative('assert').AssertTrue(Add(3, 5) == 8);"; |
| 120 module_system_->Require("test"); | 71 module_system_->Require("test"); |
| 121 } | 72 } |
| 122 | 73 |
| 123 TEST_F(ModuleSystemTest, TestNestedRequire) { | 74 TEST_F(ModuleSystemTest, TestNestedRequire) { |
| 124 RegisterModule("double", | 75 source_map_["double"] = |
| 125 "var Add = require('add').Add;" | 76 "var Add = require('add').Add;" |
| 126 "exports.Double = function(x) { return Add(x, x); };"); | 77 "exports.Double = function(x) { return Add(x, x); };"; |
| 127 RegisterModule("test", | 78 source_map_["test"] = |
| 128 "var Double = require('double').Double;" | 79 "var Double = require('double').Double;" |
| 129 "requireNative('assert').AssertTrue(Double(3) == 6);"); | 80 "requireNative('assert').AssertTrue(Double(3) == 6);"; |
| 130 module_system_->Require("test"); | 81 module_system_->Require("test"); |
| 131 } | 82 } |
| 132 | 83 |
| 133 TEST_F(ModuleSystemTest, TestModuleInsulation) { | 84 TEST_F(ModuleSystemTest, TestModuleInsulation) { |
| 134 RegisterModule("x", | 85 source_map_["x"] = |
| 135 "var x = 10;" | 86 "var x = 10;" |
| 136 "exports.X = function() { return x; };"); | 87 "exports.X = function() { return x; };"; |
| 137 RegisterModule("y", | 88 source_map_["y"] = |
| 138 "var x = 15;" | 89 "var x = 15;" |
| 139 "require('x');" | 90 "require('x');" |
| 140 "exports.Y = function() { return x; };"); | 91 "exports.Y = function() { return x; };"; |
| 141 RegisterModule("test", | 92 source_map_["test"] = |
| 142 "var Y = require('y').Y;" | 93 "var Y = require('y').Y;" |
| 143 "var X = require('x').X;" | 94 "var X = require('x').X;" |
| 144 "var assert = requireNative('assert');" | 95 "var assert = requireNative('assert');" |
| 145 "assert.AssertTrue(!this.hasOwnProperty('x'));" | 96 "assert.AssertTrue(!this.hasOwnProperty('x'));" |
| 146 "assert.AssertTrue(Y() == 15);" | 97 "assert.AssertTrue(Y() == 15);" |
| 147 "assert.AssertTrue(X() == 10);"); | 98 "assert.AssertTrue(X() == 10);"; |
| 148 module_system_->Require("test"); | 99 module_system_->Require("test"); |
| 149 } | 100 } |
| 150 | |
| 151 TEST_F(ModuleSystemTest, TestDisableNativesPreventsNativeModulesBeingLoaded) { | |
| 152 module_system_->RegisterNativeHandler("disable", | |
| 153 scoped_ptr<NativeHandler>( | |
| 154 new DisableNativesHandler(module_system_.get()))); | |
| 155 RegisterModule("test", | |
| 156 "var assert = requireNative('assert');" | |
| 157 "var disable = requireNative('disable');" | |
| 158 "disable.DisableNatives();" | |
| 159 "var caught = false;" | |
| 160 "try {" | |
| 161 " requireNative('assert');" | |
| 162 "} catch (e) {" | |
| 163 " caught = true;" | |
| 164 "}" | |
| 165 "assert.AssertTrue(caught);"); | |
| 166 module_system_->Require("test"); | |
| 167 } | |
| OLD | NEW |