1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "gin/modules/module_registry.h"
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "gin/modules/module_registry_observer.h"
#include "gin/modules/module_runner_delegate.h"
#include "gin/public/context_holder.h"
#include "gin/public/isolate_holder.h"
#include "gin/shell_runner.h"
#include "gin/test/v8_test.h"
#include "v8/include/v8.h"
namespace gin {
namespace {
struct TestHelper {
TestHelper(v8::Isolate* isolate)
: delegate(std::vector<base::FilePath>()),
runner(new ShellRunner(&delegate, isolate)),
scope(runner.get()) {
}
base::MessageLoop message_loop;
ModuleRunnerDelegate delegate;
scoped_ptr<ShellRunner> runner;
Runner::Scope scope;
};
class ModuleRegistryObserverImpl : public ModuleRegistryObserver {
public:
ModuleRegistryObserverImpl() : did_add_count_(0) {}
virtual void OnDidAddPendingModule(
const std::string& id,
const std::vector<std::string>& dependencies) override {
did_add_count_++;
id_ = id;
dependencies_ = dependencies;
}
int did_add_count() { return did_add_count_; }
const std::string& id() const { return id_; }
const std::vector<std::string>& dependencies() const { return dependencies_; }
private:
int did_add_count_;
std::string id_;
std::vector<std::string> dependencies_;
DISALLOW_COPY_AND_ASSIGN(ModuleRegistryObserverImpl);
};
void NestedCallback(v8::Handle<v8::Value> value) {
FAIL() << "Should not be called";
}
void OnModuleLoaded(TestHelper* helper,
v8::Isolate* isolate,
int64_t* counter,
v8::Handle<v8::Value> value) {
ASSERT_TRUE(value->IsNumber());
v8::Handle<v8::Integer> int_value = v8::Handle<v8::Integer>::Cast(value);
*counter += int_value->Value();
ModuleRegistry::From(helper->runner->GetContextHolder()->context())
->LoadModule(isolate, "two", base::Bind(NestedCallback));
}
} // namespace
typedef V8Test ModuleRegistryTest;
// Verifies ModuleRegistry is not available after ContextHolder has been
// deleted.
TEST_F(ModuleRegistryTest, DestroyedWithContext) {
v8::Isolate::Scope isolate_scope(instance_->isolate());
v8::HandleScope handle_scope(instance_->isolate());
v8::Handle<v8::Context> context = v8::Context::New(
instance_->isolate(), NULL, v8::Handle<v8::ObjectTemplate>());
{
ContextHolder context_holder(instance_->isolate());
context_holder.SetContext(context);
ModuleRegistry* registry = ModuleRegistry::From(context);
EXPECT_TRUE(registry != NULL);
}
ModuleRegistry* registry = ModuleRegistry::From(context);
EXPECT_TRUE(registry == NULL);
}
// Verifies ModuleRegistryObserver is notified appropriately.
TEST_F(ModuleRegistryTest, ModuleRegistryObserverTest) {
TestHelper helper(instance_->isolate());
std::string source =
"define('id', ['dep1', 'dep2'], function() {"
" return function() {};"
"});";
ModuleRegistryObserverImpl observer;
ModuleRegistry::From(helper.runner->GetContextHolder()->context())->
AddObserver(&observer);
helper.runner->Run(source, "script");
ModuleRegistry::From(helper.runner->GetContextHolder()->context())->
RemoveObserver(&observer);
EXPECT_EQ(1, observer.did_add_count());
EXPECT_EQ("id", observer.id());
ASSERT_EQ(2u, observer.dependencies().size());
EXPECT_EQ("dep1", observer.dependencies()[0]);
EXPECT_EQ("dep2", observer.dependencies()[1]);
}
// Verifies that multiple LoadModule calls for the same module are handled
// correctly.
TEST_F(ModuleRegistryTest, LoadModuleTest) {
TestHelper helper(instance_->isolate());
int64_t counter = 0;
std::string source =
"define('one', [], function() {"
" return 1;"
"});";
ModuleRegistry::LoadModuleCallback callback =
base::Bind(OnModuleLoaded, &helper, instance_->isolate(), &counter);
for (int i = 0; i < 3; i++) {
ModuleRegistry::From(helper.runner->GetContextHolder()->context())
->LoadModule(instance_->isolate(), "one", callback);
}
EXPECT_EQ(0, counter);
helper.runner->Run(source, "script");
EXPECT_EQ(3, counter);
}
} // namespace gin
|