| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/renderer/module_system.h" | 5 #include "extensions/renderer/module_system.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> |
| 9 #include <utility> | 10 #include <utility> |
| 10 | 11 |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "extensions/renderer/module_system_test.h" | 12 #include "extensions/renderer/module_system_test.h" |
| 13 #include "gin/modules/module_registry.h" | 13 #include "gin/modules/module_registry.h" |
| 14 | 14 |
| 15 namespace extensions { | 15 namespace extensions { |
| 16 | 16 |
| 17 class CounterNatives : public ObjectBackedNativeHandler { | 17 class CounterNatives : public ObjectBackedNativeHandler { |
| 18 public: | 18 public: |
| 19 explicit CounterNatives(ScriptContext* context) | 19 explicit CounterNatives(ScriptContext* context) |
| 20 : ObjectBackedNativeHandler(context), counter_(0) { | 20 : ObjectBackedNativeHandler(context), counter_(0) { |
| 21 RouteFunction("Get", | 21 RouteFunction("Get", |
| (...skipping 27 matching lines...) Expand all Loading... |
| 49 bool handled_exception() const { return handled_exception_; } | 49 bool handled_exception() const { return handled_exception_; } |
| 50 | 50 |
| 51 private: | 51 private: |
| 52 bool handled_exception_; | 52 bool handled_exception_; |
| 53 }; | 53 }; |
| 54 | 54 |
| 55 TEST_F(ModuleSystemTest, TestExceptionHandling) { | 55 TEST_F(ModuleSystemTest, TestExceptionHandling) { |
| 56 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 56 ModuleSystem::NativesEnabledScope natives_enabled_scope( |
| 57 env()->module_system()); | 57 env()->module_system()); |
| 58 TestExceptionHandler* handler = new TestExceptionHandler; | 58 TestExceptionHandler* handler = new TestExceptionHandler; |
| 59 scoped_ptr<ModuleSystem::ExceptionHandler> scoped_handler(handler); | 59 std::unique_ptr<ModuleSystem::ExceptionHandler> scoped_handler(handler); |
| 60 ASSERT_FALSE(handler->handled_exception()); | 60 ASSERT_FALSE(handler->handled_exception()); |
| 61 env()->module_system()->SetExceptionHandlerForTest(std::move(scoped_handler)); | 61 env()->module_system()->SetExceptionHandlerForTest(std::move(scoped_handler)); |
| 62 | 62 |
| 63 env()->RegisterModule("test", "throw 'hi';"); | 63 env()->RegisterModule("test", "throw 'hi';"); |
| 64 env()->module_system()->Require("test"); | 64 env()->module_system()->Require("test"); |
| 65 ASSERT_TRUE(handler->handled_exception()); | 65 ASSERT_TRUE(handler->handled_exception()); |
| 66 | 66 |
| 67 ExpectNoAssertionsMade(); | 67 ExpectNoAssertionsMade(); |
| 68 } | 68 } |
| 69 | 69 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 "assert.AssertTrue(object.thing.y() == 10);" | 181 "assert.AssertTrue(object.thing.y() == 10);" |
| 182 "assert.AssertTrue(object.thing.z == 1);"); | 182 "assert.AssertTrue(object.thing.z == 1);"); |
| 183 env()->module_system()->Require("test"); | 183 env()->module_system()->Require("test"); |
| 184 } | 184 } |
| 185 | 185 |
| 186 TEST_F(ModuleSystemTest, TestLazyFieldIsOnlyEvaledOnce) { | 186 TEST_F(ModuleSystemTest, TestLazyFieldIsOnlyEvaledOnce) { |
| 187 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 187 ModuleSystem::NativesEnabledScope natives_enabled_scope( |
| 188 env()->module_system()); | 188 env()->module_system()); |
| 189 env()->module_system()->RegisterNativeHandler( | 189 env()->module_system()->RegisterNativeHandler( |
| 190 "counter", | 190 "counter", |
| 191 scoped_ptr<NativeHandler>(new CounterNatives(env()->context()))); | 191 std::unique_ptr<NativeHandler>(new CounterNatives(env()->context()))); |
| 192 env()->RegisterModule("lazy", | 192 env()->RegisterModule("lazy", |
| 193 "requireNative('counter').Increment();" | 193 "requireNative('counter').Increment();" |
| 194 "exports.$set('x', 5);"); | 194 "exports.$set('x', 5);"); |
| 195 | 195 |
| 196 v8::Local<v8::Object> object = env()->CreateGlobal("object"); | 196 v8::Local<v8::Object> object = env()->CreateGlobal("object"); |
| 197 | 197 |
| 198 env()->module_system()->SetLazyField(object, "x", "lazy", "x"); | 198 env()->module_system()->SetLazyField(object, "x", "lazy", "x"); |
| 199 | 199 |
| 200 env()->RegisterModule("test", | 200 env()->RegisterModule("test", |
| 201 "var assert = requireNative('assert');" | 201 "var assert = requireNative('assert');" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 "var assert = requireNative('assert');" | 236 "var assert = requireNative('assert');" |
| 237 "assert.AssertTrue(object.thing.x == 5);"); | 237 "assert.AssertTrue(object.thing.x == 5);"); |
| 238 env()->module_system()->Require("test"); | 238 env()->module_system()->Require("test"); |
| 239 } | 239 } |
| 240 | 240 |
| 241 TEST_F(ModuleSystemTest, TestModulesOnlyGetEvaledOnce) { | 241 TEST_F(ModuleSystemTest, TestModulesOnlyGetEvaledOnce) { |
| 242 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 242 ModuleSystem::NativesEnabledScope natives_enabled_scope( |
| 243 env()->module_system()); | 243 env()->module_system()); |
| 244 env()->module_system()->RegisterNativeHandler( | 244 env()->module_system()->RegisterNativeHandler( |
| 245 "counter", | 245 "counter", |
| 246 scoped_ptr<NativeHandler>(new CounterNatives(env()->context()))); | 246 std::unique_ptr<NativeHandler>(new CounterNatives(env()->context()))); |
| 247 | 247 |
| 248 env()->RegisterModule("incrementsWhenEvaled", | 248 env()->RegisterModule("incrementsWhenEvaled", |
| 249 "requireNative('counter').Increment();"); | 249 "requireNative('counter').Increment();"); |
| 250 env()->RegisterModule("test", | 250 env()->RegisterModule("test", |
| 251 "var assert = requireNative('assert');" | 251 "var assert = requireNative('assert');" |
| 252 "var counter = requireNative('counter');" | 252 "var counter = requireNative('counter');" |
| 253 "assert.AssertTrue(counter.Get() == 0);" | 253 "assert.AssertTrue(counter.Get() == 0);" |
| 254 "require('incrementsWhenEvaled');" | 254 "require('incrementsWhenEvaled');" |
| 255 "assert.AssertTrue(counter.Get() == 1);" | 255 "assert.AssertTrue(counter.Get() == 1);" |
| 256 "require('incrementsWhenEvaled');" | 256 "require('incrementsWhenEvaled');" |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 env()->module_system()); | 401 env()->module_system()); |
| 402 env()->RegisterModule( | 402 env()->RegisterModule( |
| 403 "test", | 403 "test", |
| 404 "requireAsync('natives').then(function(natives) {" | 404 "requireAsync('natives').then(function(natives) {" |
| 405 " natives.requireAsync('ping').then(function(ping) {" | 405 " natives.requireAsync('ping').then(function(ping) {" |
| 406 " return ping();" | 406 " return ping();" |
| 407 " }).then(function(result) {" | 407 " }).then(function(result) {" |
| 408 " requireNative('assert').AssertTrue(result == 'pong');" | 408 " requireNative('assert').AssertTrue(result == 'pong');" |
| 409 " });" | 409 " });" |
| 410 "});"); | 410 "});"); |
| 411 scoped_ptr<ModuleSystemTestEnvironment> other_env = CreateEnvironment(); | 411 std::unique_ptr<ModuleSystemTestEnvironment> other_env = CreateEnvironment(); |
| 412 other_env->RegisterModule("ping", | 412 other_env->RegisterModule("ping", |
| 413 "define('ping', ['natives'], function(natives) {" | 413 "define('ping', ['natives'], function(natives) {" |
| 414 " return function() {" | 414 " return function() {" |
| 415 " return 'pong';" | 415 " return 'pong';" |
| 416 " }" | 416 " }" |
| 417 "});"); | 417 "});"); |
| 418 gin::ModuleRegistry::From(env()->context()->v8_context()) | 418 gin::ModuleRegistry::From(env()->context()->v8_context()) |
| 419 ->AddBuiltinModule( | 419 ->AddBuiltinModule( |
| 420 env()->isolate(), "natives", | 420 env()->isolate(), "natives", |
| 421 other_env->module_system()->NewInstance()); | 421 other_env->module_system()->NewInstance()); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 438 "test", | 438 "test", |
| 439 "requireAsync('natives').then(function(natives) {" | 439 "requireAsync('natives').then(function(natives) {" |
| 440 " natives.requireAsync('ping').then(function(ping) {" | 440 " natives.requireAsync('ping').then(function(ping) {" |
| 441 " return ping();" | 441 " return ping();" |
| 442 " }).then(function(pong) {" | 442 " }).then(function(pong) {" |
| 443 " return pong();" | 443 " return pong();" |
| 444 " }).then(function(result) {" | 444 " }).then(function(result) {" |
| 445 " requireNative('assert').AssertTrue(result == 'done');" | 445 " requireNative('assert').AssertTrue(result == 'done');" |
| 446 " });" | 446 " });" |
| 447 "});"); | 447 "});"); |
| 448 scoped_ptr<ModuleSystemTestEnvironment> other_env = CreateEnvironment(); | 448 std::unique_ptr<ModuleSystemTestEnvironment> other_env = CreateEnvironment(); |
| 449 other_env->RegisterModule("ping", | 449 other_env->RegisterModule("ping", |
| 450 "define('ping', ['natives'], function(natives) {" | 450 "define('ping', ['natives'], function(natives) {" |
| 451 " return function() {" | 451 " return function() {" |
| 452 " return natives.requireAsync('pong');" | 452 " return natives.requireAsync('pong');" |
| 453 " }" | 453 " }" |
| 454 "});"); | 454 "});"); |
| 455 gin::ModuleRegistry::From(env()->context()->v8_context()) | 455 gin::ModuleRegistry::From(env()->context()->v8_context()) |
| 456 ->AddBuiltinModule( | 456 ->AddBuiltinModule( |
| 457 env()->isolate(), "natives", | 457 env()->isolate(), "natives", |
| 458 other_env->module_system()->NewInstance()); | 458 other_env->module_system()->NewInstance()); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 470 env()->RegisterModule("test", | 470 env()->RegisterModule("test", |
| 471 "requireAsync('natives').then(function(natives) {" | 471 "requireAsync('natives').then(function(natives) {" |
| 472 " var AssertTrue = requireNative('assert').AssertTrue;" | 472 " var AssertTrue = requireNative('assert').AssertTrue;" |
| 473 " natives.requireAsync('foo').then(function() {" | 473 " natives.requireAsync('foo').then(function() {" |
| 474 " AssertTrue(false);" | 474 " AssertTrue(false);" |
| 475 " }).catch(function(error) {" | 475 " }).catch(function(error) {" |
| 476 " AssertTrue(error.message == " | 476 " AssertTrue(error.message == " |
| 477 " 'Extension view no longer exists');" | 477 " 'Extension view no longer exists');" |
| 478 " });" | 478 " });" |
| 479 "});"); | 479 "});"); |
| 480 scoped_ptr<ModuleSystemTestEnvironment> other_env = CreateEnvironment(); | 480 std::unique_ptr<ModuleSystemTestEnvironment> other_env = CreateEnvironment(); |
| 481 gin::ModuleRegistry::From(env()->context()->v8_context()) | 481 gin::ModuleRegistry::From(env()->context()->v8_context()) |
| 482 ->AddBuiltinModule( | 482 ->AddBuiltinModule( |
| 483 env()->isolate(), "natives", | 483 env()->isolate(), "natives", |
| 484 other_env->module_system()->NewInstance()); | 484 other_env->module_system()->NewInstance()); |
| 485 other_env->ShutdownGin(); | 485 other_env->ShutdownGin(); |
| 486 env()->module_system()->Require("test"); | 486 env()->module_system()->Require("test"); |
| 487 RunResolvedPromises(); | 487 RunResolvedPromises(); |
| 488 } | 488 } |
| 489 | 489 |
| 490 TEST_F(ModuleSystemTest, TestRequireAsyncFromContextWithNoModuleSystem) { | 490 TEST_F(ModuleSystemTest, TestRequireAsyncFromContextWithNoModuleSystem) { |
| 491 ModuleSystem::NativesEnabledScope natives_enabled_scope( | 491 ModuleSystem::NativesEnabledScope natives_enabled_scope( |
| 492 env()->module_system()); | 492 env()->module_system()); |
| 493 env()->RegisterModule("test", | 493 env()->RegisterModule("test", |
| 494 "requireAsync('natives').then(function(natives) {" | 494 "requireAsync('natives').then(function(natives) {" |
| 495 " requireNative('assert').AssertTrue(" | 495 " requireNative('assert').AssertTrue(" |
| 496 " natives.requireAsync('foo') === undefined);" | 496 " natives.requireAsync('foo') === undefined);" |
| 497 "});"); | 497 "});"); |
| 498 scoped_ptr<ModuleSystemTestEnvironment> other_env = CreateEnvironment(); | 498 std::unique_ptr<ModuleSystemTestEnvironment> other_env = CreateEnvironment(); |
| 499 gin::ModuleRegistry::From(env()->context()->v8_context()) | 499 gin::ModuleRegistry::From(env()->context()->v8_context()) |
| 500 ->AddBuiltinModule( | 500 ->AddBuiltinModule( |
| 501 env()->isolate(), "natives", | 501 env()->isolate(), "natives", |
| 502 other_env->module_system()->NewInstance()); | 502 other_env->module_system()->NewInstance()); |
| 503 other_env->ShutdownModuleSystem(); | 503 other_env->ShutdownModuleSystem(); |
| 504 env()->module_system()->Require("test"); | 504 env()->module_system()->Require("test"); |
| 505 RunResolvedPromises(); | 505 RunResolvedPromises(); |
| 506 } | 506 } |
| 507 | 507 |
| 508 } // namespace extensions | 508 } // namespace extensions |
| OLD | NEW |