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

Side by Side Diff: gin/modules/module_registry_unittest.cc

Issue 373973003: Gin: Allow multiple callers to wait for the same module in ModuleRegistry. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: callbacks.reserve() Created 6 years, 5 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 | « gin/modules/module_registry.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
8 #include "gin/modules/module_registry_observer.h" 9 #include "gin/modules/module_registry_observer.h"
9 #include "gin/modules/module_runner_delegate.h" 10 #include "gin/modules/module_runner_delegate.h"
10 #include "gin/public/context_holder.h" 11 #include "gin/public/context_holder.h"
11 #include "gin/public/isolate_holder.h" 12 #include "gin/public/isolate_holder.h"
12 #include "gin/shell_runner.h" 13 #include "gin/shell_runner.h"
13 #include "gin/test/v8_test.h" 14 #include "gin/test/v8_test.h"
14 #include "v8/include/v8.h" 15 #include "v8/include/v8.h"
15 16
16 namespace gin { 17 namespace gin {
(...skipping 30 matching lines...) Expand all
47 const std::vector<std::string>& dependencies() const { return dependencies_; } 48 const std::vector<std::string>& dependencies() const { return dependencies_; }
48 49
49 private: 50 private:
50 int did_add_count_; 51 int did_add_count_;
51 std::string id_; 52 std::string id_;
52 std::vector<std::string> dependencies_; 53 std::vector<std::string> dependencies_;
53 54
54 DISALLOW_COPY_AND_ASSIGN(ModuleRegistryObserverImpl); 55 DISALLOW_COPY_AND_ASSIGN(ModuleRegistryObserverImpl);
55 }; 56 };
56 57
58 void NestedCallback(v8::Handle<v8::Value> value) {
59 FAIL() << "Should not be called";
60 }
61
62 void OnModuleLoaded(TestHelper* helper,
63 v8::Isolate* isolate,
64 int64_t* counter,
65 v8::Handle<v8::Value> value) {
66 ASSERT_TRUE(value->IsNumber());
67 v8::Handle<v8::Integer> int_value = v8::Handle<v8::Integer>::Cast(value);
68 *counter += int_value->Value();
69 ModuleRegistry::From(helper->runner->GetContextHolder()->context())
70 ->LoadModule(isolate, "two", base::Bind(NestedCallback));
71 }
72
57 } // namespace 73 } // namespace
58 74
59 typedef V8Test ModuleRegistryTest; 75 typedef V8Test ModuleRegistryTest;
60 76
61 // Verifies ModuleRegistry is not available after ContextHolder has been 77 // Verifies ModuleRegistry is not available after ContextHolder has been
62 // deleted. 78 // deleted.
63 TEST_F(ModuleRegistryTest, DestroyedWithContext) { 79 TEST_F(ModuleRegistryTest, DestroyedWithContext) {
64 v8::Isolate::Scope isolate_scope(instance_->isolate()); 80 v8::Isolate::Scope isolate_scope(instance_->isolate());
65 v8::HandleScope handle_scope(instance_->isolate()); 81 v8::HandleScope handle_scope(instance_->isolate());
66 v8::Handle<v8::Context> context = v8::Context::New( 82 v8::Handle<v8::Context> context = v8::Context::New(
(...skipping 22 matching lines...) Expand all
89 helper.runner->Run(source, "script"); 105 helper.runner->Run(source, "script");
90 ModuleRegistry::From(helper.runner->GetContextHolder()->context())-> 106 ModuleRegistry::From(helper.runner->GetContextHolder()->context())->
91 RemoveObserver(&observer); 107 RemoveObserver(&observer);
92 EXPECT_EQ(1, observer.did_add_count()); 108 EXPECT_EQ(1, observer.did_add_count());
93 EXPECT_EQ("id", observer.id()); 109 EXPECT_EQ("id", observer.id());
94 ASSERT_EQ(2u, observer.dependencies().size()); 110 ASSERT_EQ(2u, observer.dependencies().size());
95 EXPECT_EQ("dep1", observer.dependencies()[0]); 111 EXPECT_EQ("dep1", observer.dependencies()[0]);
96 EXPECT_EQ("dep2", observer.dependencies()[1]); 112 EXPECT_EQ("dep2", observer.dependencies()[1]);
97 } 113 }
98 114
115 // Verifies that multiple LoadModule calls for the same module are handled
116 // correctly.
117 TEST_F(ModuleRegistryTest, LoadModuleTest) {
118 TestHelper helper(instance_->isolate());
119 int64_t counter = 0;
120 std::string source =
121 "define('one', [], function() {"
122 " return 1;"
123 "});";
124
125 ModuleRegistry::LoadModuleCallback callback =
126 base::Bind(OnModuleLoaded, &helper, instance_->isolate(), &counter);
127 for (int i = 0; i < 3; i++) {
128 ModuleRegistry::From(helper.runner->GetContextHolder()->context())
129 ->LoadModule(instance_->isolate(), "one", callback);
130 }
131 EXPECT_EQ(0, counter);
132 helper.runner->Run(source, "script");
133 EXPECT_EQ(3, counter);
134 }
135
99 } // namespace gin 136 } // namespace gin
OLDNEW
« no previous file with comments | « gin/modules/module_registry.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698