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

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: rebase 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
« no previous file with comments | « chrome/renderer/module_system.cc ('k') | chrome/renderer/native_handler.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 source_map_(new StringSourceMap()) {
41 context_->Enter(); 85 context_->Enter();
42 source_map_["add"] = "exports.Add = function(x, y) { return x + y; };"; 86 assert_natives_ = new AssertNatives();
43 module_system_.reset(new ModuleSystem(&source_map_)); 87 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() {
94 context_->Exit();
49 context_.Dispose(); 95 context_.Dispose();
50 } 96 }
51 97
98 void RegisterModule(const std::string& name, const std::string& code) {
99 source_map_->RegisterModule(name, code);
100 }
101
52 virtual void TearDown() { 102 virtual void TearDown() {
53 // All tests must call a native function at least once. 103 // All tests must call a native function at least once.
54 ASSERT_TRUE(assert_natives_->native_function_called()); 104 ASSERT_TRUE(assert_natives_->native_function_called());
55 ASSERT_FALSE(assert_natives_->failed()); 105 ASSERT_FALSE(assert_natives_->failed());
56 ASSERT_FALSE(try_catch_.HasCaught()); 106 ASSERT_FALSE(try_catch_.HasCaught());
57 } 107 }
58 108
109 v8::Persistent<v8::Context> context_;
59 v8::HandleScope handle_scope_; 110 v8::HandleScope handle_scope_;
60 v8::TryCatch try_catch_; 111 v8::TryCatch try_catch_;
61 v8::Persistent<v8::Context> context_;
62 AssertNatives* assert_natives_; 112 AssertNatives* assert_natives_;
63 std::map<std::string, std::string> source_map_; 113 StringSourceMap* source_map_;
64 scoped_ptr<ModuleSystem> module_system_; 114 scoped_ptr<ModuleSystem> module_system_;
65 }; 115 };
66 116
67 TEST_F(ModuleSystemTest, TestRequire) { 117 TEST_F(ModuleSystemTest, TestRequire) {
68 source_map_["test"] = 118 RegisterModule("test",
69 "var Add = require('add').Add;" 119 "var Add = require('add').Add;"
70 "requireNative('assert').AssertTrue(Add(3, 5) == 8);"; 120 "requireNative('assert').AssertTrue(Add(3, 5) == 8);");
71 module_system_->Require("test"); 121 module_system_->Require("test");
72 } 122 }
73 123
74 TEST_F(ModuleSystemTest, TestNestedRequire) { 124 TEST_F(ModuleSystemTest, TestNestedRequire) {
75 source_map_["double"] = 125 RegisterModule("double",
76 "var Add = require('add').Add;" 126 "var Add = require('add').Add;"
77 "exports.Double = function(x) { return Add(x, x); };"; 127 "exports.Double = function(x) { return Add(x, x); };");
78 source_map_["test"] = 128 RegisterModule("test",
79 "var Double = require('double').Double;" 129 "var Double = require('double').Double;"
80 "requireNative('assert').AssertTrue(Double(3) == 6);"; 130 "requireNative('assert').AssertTrue(Double(3) == 6);");
81 module_system_->Require("test"); 131 module_system_->Require("test");
82 } 132 }
83 133
84 TEST_F(ModuleSystemTest, TestModuleInsulation) { 134 TEST_F(ModuleSystemTest, TestModuleInsulation) {
85 source_map_["x"] = 135 RegisterModule("x",
86 "var x = 10;" 136 "var x = 10;"
87 "exports.X = function() { return x; };"; 137 "exports.X = function() { return x; };");
88 source_map_["y"] = 138 RegisterModule("y",
89 "var x = 15;" 139 "var x = 15;"
90 "require('x');" 140 "require('x');"
91 "exports.Y = function() { return x; };"; 141 "exports.Y = function() { return x; };");
92 source_map_["test"] = 142 RegisterModule("test",
93 "var Y = require('y').Y;" 143 "var Y = require('y').Y;"
94 "var X = require('x').X;" 144 "var X = require('x').X;"
95 "var assert = requireNative('assert');" 145 "var assert = requireNative('assert');"
96 "assert.AssertTrue(!this.hasOwnProperty('x'));" 146 "assert.AssertTrue(!this.hasOwnProperty('x'));"
97 "assert.AssertTrue(Y() == 15);" 147 "assert.AssertTrue(Y() == 15);"
98 "assert.AssertTrue(X() == 10);"; 148 "assert.AssertTrue(X() == 10);");
99 module_system_->Require("test"); 149 module_system_->Require("test");
100 } 150 }
151
152 TEST_F(ModuleSystemTest, TestDisableNativesPreventsNativeModulesBeingLoaded) {
153 module_system_->RegisterNativeHandler("disable",
154 scoped_ptr<NativeHandler>(
155 new DisableNativesHandler(module_system_.get())));
156 RegisterModule("test",
157 "var assert = requireNative('assert');"
158 "var disable = requireNative('disable');"
159 "disable.DisableNatives();"
160 "var caught = false;"
161 "try {"
162 " requireNative('assert');"
163 "} catch (e) {"
164 " caught = true;"
165 "}"
166 "assert.AssertTrue(caught);");
167 module_system_->Require("test");
168 }
OLDNEW
« no previous file with comments | « chrome/renderer/module_system.cc ('k') | chrome/renderer/native_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698