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

Side by Side Diff: chrome/renderer/module_system_unittest.cc

Issue 9386001: Implement a module system for the extension bindings JS. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: base files missing? Created 8 years, 9 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
7 #include "chrome/renderer/module_system.h" 8 #include "chrome/renderer/module_system.h"
8 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
9 10
10 #include <map> 11 #include <map>
11 #include <string> 12 #include <string>
12 13
14 // Native JS functions for doing asserts.
13 class AssertNatives : public NativeHandler { 15 class AssertNatives : public NativeHandler {
14 public: 16 public:
15 AssertNatives() 17 AssertNatives()
16 : native_function_called_(false), 18 : native_function_called_(false),
17 failed_(false) { 19 failed_(false) {
18 RouteFunction("AssertTrue", base::Bind(&AssertNatives::AssertTrue, 20 RouteFunction("AssertTrue", base::Bind(&AssertNatives::AssertTrue,
19 base::Unretained(this))); 21 base::Unretained(this)));
20 } 22 }
21 23
22 bool native_function_called() { return native_function_called_; } 24 bool native_function_called() { return native_function_called_; }
23 bool failed() { return failed_; } 25 bool failed() { return failed_; }
24 26
25 v8::Handle<v8::Value> AssertTrue(const v8::Arguments& args) { 27 v8::Handle<v8::Value> AssertTrue(const v8::Arguments& args) {
26 native_function_called_ = true; 28 native_function_called_ = true;
27 failed_ = failed_ || !args[0]->ToBoolean()->Value(); 29 failed_ = failed_ || !args[0]->ToBoolean()->Value();
28 return v8::Undefined(); 30 return v8::Undefined();
29 } 31 }
30 32
31 private: 33 private:
32 bool native_function_called_; 34 bool native_function_called_;
33 bool failed_; 35 bool failed_;
34 }; 36 };
35 37
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
36 class ModuleSystemTest : public testing::Test { 79 class ModuleSystemTest : public testing::Test {
37 public: 80 public:
38 ModuleSystemTest() 81 ModuleSystemTest()
39 : context_(v8::Context::New()), 82 : context_(v8::Context::New()),
40 assert_natives_(new AssertNatives()) { 83 handle_scope_(),
84 assert_natives_(new AssertNatives()),
85 source_map_(new StringSourceMap()),
86 module_system_(new ModuleSystem(source_map_)) {
41 context_->Enter(); 87 context_->Enter();
42 source_map_["add"] = "exports.Add = function(x, y) { return x + y; };";
43 module_system_.reset(new ModuleSystem(&source_map_));
44 module_system_->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>( 88 module_system_->RegisterNativeHandler("assert", scoped_ptr<NativeHandler>(
45 assert_natives_)); 89 assert_natives_));
90 RegisterModule("add", "exports.Add = function(x, y) { return x + y; };");
46 } 91 }
47 92
48 ~ModuleSystemTest() { 93 ~ModuleSystemTest() {
49 context_.Dispose(); 94 context_.Dispose();
50 } 95 }
51 96
97 void RegisterModule(const std::string& name, const std::string& code) {
98 source_map_->RegisterModule(name, code);
99 }
100
52 virtual void TearDown() { 101 virtual void TearDown() {
53 // All tests must call a native function at least once. 102 // All tests must call a native function at least once.
54 ASSERT_TRUE(assert_natives_->native_function_called()); 103 ASSERT_TRUE(assert_natives_->native_function_called());
55 ASSERT_FALSE(assert_natives_->failed()); 104 ASSERT_FALSE(assert_natives_->failed());
56 ASSERT_FALSE(try_catch_.HasCaught()); 105 ASSERT_FALSE(try_catch_.HasCaught());
57 } 106 }
58 107
108 v8::Persistent<v8::Context> context_;
59 v8::HandleScope handle_scope_; 109 v8::HandleScope handle_scope_;
60 v8::TryCatch try_catch_; 110 v8::TryCatch try_catch_;
61 v8::Persistent<v8::Context> context_;
62 AssertNatives* assert_natives_; 111 AssertNatives* assert_natives_;
63 std::map<std::string, std::string> source_map_; 112 StringSourceMap* source_map_;
64 scoped_ptr<ModuleSystem> module_system_; 113 scoped_ptr<ModuleSystem> module_system_;
65 }; 114 };
66 115
67 TEST_F(ModuleSystemTest, TestRequire) { 116 TEST_F(ModuleSystemTest, TestRequire) {
68 source_map_["test"] = 117 RegisterModule("test",
69 "var Add = require('add').Add;" 118 "var Add = require('add').Add;"
70 "requireNative('assert').AssertTrue(Add(3, 5) == 8);"; 119 "requireNative('assert').AssertTrue(Add(3, 5) == 8);");
71 module_system_->Require("test"); 120 module_system_->Require("test");
72 } 121 }
73 122
74 TEST_F(ModuleSystemTest, TestNestedRequire) { 123 TEST_F(ModuleSystemTest, TestNestedRequire) {
75 source_map_["double"] = 124 RegisterModule("double",
76 "var Add = require('add').Add;" 125 "var Add = require('add').Add;"
77 "exports.Double = function(x) { return Add(x, x); };"; 126 "exports.Double = function(x) { return Add(x, x); };");
78 source_map_["test"] = 127 RegisterModule("test",
79 "var Double = require('double').Double;" 128 "var Double = require('double').Double;"
80 "requireNative('assert').AssertTrue(Double(3) == 6);"; 129 "requireNative('assert').AssertTrue(Double(3) == 6);");
81 module_system_->Require("test"); 130 module_system_->Require("test");
82 } 131 }
83 132
84 TEST_F(ModuleSystemTest, TestModuleInsulation) { 133 TEST_F(ModuleSystemTest, TestModuleInsulation) {
85 source_map_["x"] = 134 RegisterModule("x",
86 "var x = 10;" 135 "var x = 10;"
87 "exports.X = function() { return x; };"; 136 "exports.X = function() { return x; };");
88 source_map_["y"] = 137 RegisterModule("y",
89 "var x = 15;" 138 "var x = 15;"
90 "require('x');" 139 "require('x');"
91 "exports.Y = function() { return x; };"; 140 "exports.Y = function() { return x; };");
92 source_map_["test"] = 141 RegisterModule("test",
93 "var Y = require('y').Y;" 142 "var Y = require('y').Y;"
94 "var X = require('x').X;" 143 "var X = require('x').X;"
95 "var assert = requireNative('assert');" 144 "var assert = requireNative('assert');"
96 "assert.AssertTrue(!this.hasOwnProperty('x'));" 145 "assert.AssertTrue(!this.hasOwnProperty('x'));"
97 "assert.AssertTrue(Y() == 15);" 146 "assert.AssertTrue(Y() == 15);"
98 "assert.AssertTrue(X() == 10);"; 147 "assert.AssertTrue(X() == 10);");
99 module_system_->Require("test"); 148 module_system_->Require("test");
100 } 149 }
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698