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
3 // found in the LICENSE file.
5 #include "gin/modules/module_registry.h"
7 #include "base/message_loop/message_loop.h"
8 #include "gin/modules/module_registry_observer.h"
9 #include "gin/modules/module_runner_delegate.h"
10 #include "gin/public/context_holder.h"
11 #include "gin/public/isolate_holder.h"
12 #include "gin/shell_runner.h"
13 #include "gin/test/v8_test.h"
14 #include "v8/include/v8.h"
21 TestHelper(v8::Isolate
* isolate
)
22 : delegate(std::vector
<base::FilePath
>()),
23 runner(new ShellRunner(&delegate
, isolate
)),
27 base::MessageLoop message_loop
;
28 ModuleRunnerDelegate delegate
;
29 scoped_ptr
<ShellRunner
> runner
;
33 class ModuleRegistryObserverImpl
: public ModuleRegistryObserver
{
35 ModuleRegistryObserverImpl() : did_add_count_(0) {}
37 virtual void OnDidAddPendingModule(
38 const std::string
& id
,
39 const std::vector
<std::string
>& dependencies
) OVERRIDE
{
42 dependencies_
= dependencies
;
45 int did_add_count() { return did_add_count_
; }
46 const std::string
& id() const { return id_
; }
47 const std::vector
<std::string
>& dependencies() const { return dependencies_
; }
52 std::vector
<std::string
> dependencies_
;
54 DISALLOW_COPY_AND_ASSIGN(ModuleRegistryObserverImpl
);
59 typedef V8Test ModuleRegistryTest
;
61 // Verifies ModuleRegistry is not available after ContextHolder has been
63 TEST_F(ModuleRegistryTest
, DestroyedWithContext
) {
64 v8::Isolate::Scope
isolate_scope(instance_
->isolate());
65 v8::HandleScope
handle_scope(instance_
->isolate());
66 v8::Handle
<v8::Context
> context
= v8::Context::New(
67 instance_
->isolate(), NULL
, v8::Handle
<v8::ObjectTemplate
>());
69 ContextHolder
context_holder(instance_
->isolate());
70 context_holder
.SetContext(context
);
71 ModuleRegistry
* registry
= ModuleRegistry::From(context
);
72 EXPECT_TRUE(registry
!= NULL
);
74 ModuleRegistry
* registry
= ModuleRegistry::From(context
);
75 EXPECT_TRUE(registry
== NULL
);
78 // Verifies ModuleRegistryObserver is notified appropriately.
79 TEST_F(ModuleRegistryTest
, ModuleRegistryObserverTest
) {
80 TestHelper
helper(instance_
->isolate());
82 "define('id', ['dep1', 'dep2'], function() {"
83 " return function() {};"
86 ModuleRegistryObserverImpl observer
;
87 ModuleRegistry::From(helper
.runner
->GetContextHolder()->context())->
88 AddObserver(&observer
);
89 helper
.runner
->Run(source
, "script");
90 ModuleRegistry::From(helper
.runner
->GetContextHolder()->context())->
91 RemoveObserver(&observer
);
92 EXPECT_EQ(1, observer
.did_add_count());
93 EXPECT_EQ("id", observer
.id());
94 ASSERT_EQ(2u, observer
.dependencies().size());
95 EXPECT_EQ("dep1", observer
.dependencies()[0]);
96 EXPECT_EQ("dep2", observer
.dependencies()[1]);