OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "bindings/core/v8/ScriptModule.h" | 5 #include "bindings/core/v8/ScriptModule.h" |
6 | 6 |
| 7 #include "bindings/core/v8/ScriptController.h" |
| 8 #include "bindings/core/v8/ScriptSourceCode.h" |
| 9 #include "bindings/core/v8/V8Binding.h" |
7 #include "bindings/core/v8/V8BindingForTesting.h" | 10 #include "bindings/core/v8/V8BindingForTesting.h" |
8 #include "bindings/core/v8/V8PerContextData.h" | 11 #include "bindings/core/v8/V8PerContextData.h" |
9 #include "core/dom/ScriptModuleResolver.h" | 12 #include "core/dom/ScriptModuleResolver.h" |
| 13 #include "core/frame/LocalFrame.h" |
10 #include "core/testing/DummyModulator.h" | 14 #include "core/testing/DummyModulator.h" |
11 #include "testing/gmock/include/gmock/gmock-matchers.h" | 15 #include "testing/gmock/include/gmock/gmock-matchers.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "v8/include/v8.h" | 17 #include "v8/include/v8.h" |
14 | 18 |
15 namespace blink { | 19 namespace blink { |
16 | 20 |
17 namespace { | 21 namespace { |
18 | 22 |
19 class TestScriptModuleResolver final : public ScriptModuleResolver { | 23 class TestScriptModuleResolver final : public ScriptModuleResolver { |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 "c.js", kSharableCrossOrigin); | 191 "c.js", kSharableCrossOrigin); |
188 ASSERT_FALSE(module.IsNull()); | 192 ASSERT_FALSE(module.IsNull()); |
189 ScriptValue exception = module.Instantiate(scope.GetScriptState()); | 193 ScriptValue exception = module.Instantiate(scope.GetScriptState()); |
190 ASSERT_TRUE(exception.IsEmpty()); | 194 ASSERT_TRUE(exception.IsEmpty()); |
191 | 195 |
192 ASSERT_EQ(2u, resolver->ResolveCount()); | 196 ASSERT_EQ(2u, resolver->ResolveCount()); |
193 EXPECT_EQ("a", resolver->Specifiers()[0]); | 197 EXPECT_EQ("a", resolver->Specifiers()[0]); |
194 EXPECT_EQ("b", resolver->Specifiers()[1]); | 198 EXPECT_EQ("b", resolver->Specifiers()[1]); |
195 } | 199 } |
196 | 200 |
| 201 TEST(ScriptModuleTest, Evaluate) { |
| 202 V8TestingScope scope; |
| 203 |
| 204 auto modulator = new ScriptModuleTestModulator(); |
| 205 Modulator::SetModulator(scope.GetScriptState(), modulator); |
| 206 |
| 207 ScriptModule module = ScriptModule::Compile( |
| 208 scope.GetIsolate(), "export const a = 42; window.foo = 'bar';", "foo.js", |
| 209 kSharableCrossOrigin); |
| 210 ASSERT_FALSE(module.IsNull()); |
| 211 ScriptValue exception = module.Instantiate(scope.GetScriptState()); |
| 212 ASSERT_TRUE(exception.IsEmpty()); |
| 213 |
| 214 module.Evaluate(scope.GetScriptState()); |
| 215 v8::Local<v8::Value> value = scope.GetFrame() |
| 216 .GetScriptController() |
| 217 .ExecuteScriptInMainWorldAndReturnValue( |
| 218 ScriptSourceCode("window.foo")); |
| 219 ASSERT_TRUE(value->IsString()); |
| 220 EXPECT_EQ("bar", ToCoreString(v8::Local<v8::String>::Cast(value))); |
| 221 } |
| 222 |
197 } // namespace | 223 } // namespace |
198 | 224 |
199 } // namespace blink | 225 } // namespace blink |
OLD | NEW |