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 "gin/modules/module_registry.h" | 5 #include "gin/modules/module_registry.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "gin/modules/module_registry_observer.h" | 9 #include "gin/modules/module_registry_observer.h" |
10 #include "gin/modules/module_runner_delegate.h" | 10 #include "gin/modules/module_runner_delegate.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 v8::Isolate* isolate, | 63 v8::Isolate* isolate, |
64 int64_t* counter, | 64 int64_t* counter, |
65 v8::Handle<v8::Value> value) { | 65 v8::Handle<v8::Value> value) { |
66 ASSERT_TRUE(value->IsNumber()); | 66 ASSERT_TRUE(value->IsNumber()); |
67 v8::Handle<v8::Integer> int_value = v8::Handle<v8::Integer>::Cast(value); | 67 v8::Handle<v8::Integer> int_value = v8::Handle<v8::Integer>::Cast(value); |
68 *counter += int_value->Value(); | 68 *counter += int_value->Value(); |
69 ModuleRegistry::From(helper->runner->GetContextHolder()->context()) | 69 ModuleRegistry::From(helper->runner->GetContextHolder()->context()) |
70 ->LoadModule(isolate, "two", base::Bind(NestedCallback)); | 70 ->LoadModule(isolate, "two", base::Bind(NestedCallback)); |
71 } | 71 } |
72 | 72 |
| 73 void OnModuleLoadedNoOp(v8::Handle<v8::Value> value) { |
| 74 ASSERT_TRUE(value->IsNumber()); |
| 75 } |
| 76 |
73 } // namespace | 77 } // namespace |
74 | 78 |
75 typedef V8Test ModuleRegistryTest; | 79 typedef V8Test ModuleRegistryTest; |
76 | 80 |
77 // Verifies ModuleRegistry is not available after ContextHolder has been | 81 // Verifies ModuleRegistry is not available after ContextHolder has been |
78 // deleted. | 82 // deleted. |
79 TEST_F(ModuleRegistryTest, DestroyedWithContext) { | 83 TEST_F(ModuleRegistryTest, DestroyedWithContext) { |
80 v8::Isolate::Scope isolate_scope(instance_->isolate()); | 84 v8::Isolate::Scope isolate_scope(instance_->isolate()); |
81 v8::HandleScope handle_scope(instance_->isolate()); | 85 v8::HandleScope handle_scope(instance_->isolate()); |
82 v8::Handle<v8::Context> context = v8::Context::New( | 86 v8::Handle<v8::Context> context = v8::Context::New( |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 base::Bind(OnModuleLoaded, &helper, instance_->isolate(), &counter); | 130 base::Bind(OnModuleLoaded, &helper, instance_->isolate(), &counter); |
127 for (int i = 0; i < 3; i++) { | 131 for (int i = 0; i < 3; i++) { |
128 ModuleRegistry::From(helper.runner->GetContextHolder()->context()) | 132 ModuleRegistry::From(helper.runner->GetContextHolder()->context()) |
129 ->LoadModule(instance_->isolate(), "one", callback); | 133 ->LoadModule(instance_->isolate(), "one", callback); |
130 } | 134 } |
131 EXPECT_EQ(0, counter); | 135 EXPECT_EQ(0, counter); |
132 helper.runner->Run(source, "script"); | 136 helper.runner->Run(source, "script"); |
133 EXPECT_EQ(3, counter); | 137 EXPECT_EQ(3, counter); |
134 } | 138 } |
135 | 139 |
| 140 // Verifies that explicitly loading a module that's already pending does |
| 141 // not cause the ModuleRegistry's unsatisfied_dependency set to grow. |
| 142 TEST_F(ModuleRegistryTest, UnsatisfiedDependenciesTest) { |
| 143 TestHelper helper(instance_->isolate()); |
| 144 std::string source = |
| 145 "define('one', ['no_such_module'], function(nsm) {" |
| 146 " return 1;" |
| 147 "});"; |
| 148 ModuleRegistry* registry = |
| 149 ModuleRegistry::From(helper.runner->GetContextHolder()->context()); |
| 150 |
| 151 std::set<std::string> no_such_module_set; |
| 152 no_such_module_set.insert("no_such_module"); |
| 153 |
| 154 // Adds one unsatisfied dependency on "no-such-module". |
| 155 helper.runner->Run(source, "script"); |
| 156 EXPECT_EQ(no_such_module_set, registry->unsatisfied_dependencies()); |
| 157 |
| 158 // Should have no effect on the unsatisfied_dependencies set. |
| 159 ModuleRegistry::LoadModuleCallback callback = base::Bind(OnModuleLoadedNoOp); |
| 160 registry->LoadModule(instance_->isolate(), "one", callback); |
| 161 EXPECT_EQ(no_such_module_set, registry->unsatisfied_dependencies()); |
| 162 } |
| 163 |
136 } // namespace gin | 164 } // namespace gin |
OLD | NEW |