Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "core/loader/modulescript/ModuleScriptLoader.h" | |
| 6 | |
| 7 #include "core/dom/Document.h" | |
| 8 #include "core/dom/Modulator.h" | |
| 9 #include "core/dom/ModuleScript.h" | |
| 10 #include "core/loader/modulescript/ModuleScriptFetchRequest.h" | |
| 11 #include "core/loader/modulescript/ModuleScriptLoaderClient.h" | |
| 12 #include "core/loader/modulescript/ModuleScriptLoaderRegistry.h" | |
| 13 #include "core/testing/DummyModulator.h" | |
| 14 #include "core/testing/DummyPageHolder.h" | |
| 15 #include "platform/heap/Handle.h" | |
| 16 #include "platform/loader/fetch/MockFetchContext.h" | |
| 17 #include "platform/loader/fetch/ResourceFetcher.h" | |
| 18 #include "platform/testing/TestingPlatformSupport.h" | |
| 19 #include "public/platform/Platform.h" | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | |
| 21 | |
| 22 namespace blink { | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 class TestModuleScriptLoaderClient | |
|
yhirano
2017/02/17 04:59:15
+final
kouhei (in TOK)
2017/02/22 03:52:29
Done.
| |
| 27 : public GarbageCollectedFinalized<TestModuleScriptLoaderClient>, | |
| 28 public ModuleScriptLoaderClient { | |
| 29 USING_GARBAGE_COLLECTED_MIXIN(TestModuleScriptLoaderClient); | |
| 30 | |
| 31 public: | |
| 32 TestModuleScriptLoaderClient() = default; | |
| 33 virtual ~TestModuleScriptLoaderClient() {} | |
|
yhirano
2017/02/17 04:59:15
-virtual
+override
kouhei (in TOK)
2017/02/22 03:52:29
Done.
| |
| 34 | |
| 35 DEFINE_INLINE_TRACE() { visitor->trace(m_moduleScript); } | |
| 36 | |
| 37 void notifyNewSingleModuleFinished(ModuleScript* moduleScript) override { | |
|
hiroshige
2017/02/20 23:15:32
optional: I prefer using gmock and EXPECT_CALL() b
kouhei (in TOK)
2017/02/22 03:52:29
ok
| |
| 38 m_wasNotifyFinished = true; | |
| 39 m_moduleScript = moduleScript; | |
| 40 } | |
| 41 | |
| 42 bool wasNotifyFinished() const { return m_wasNotifyFinished; } | |
| 43 ModuleScript* moduleScript() { return m_moduleScript; } | |
| 44 | |
| 45 private: | |
| 46 bool m_wasNotifyFinished = false; | |
| 47 Member<ModuleScript> m_moduleScript; | |
| 48 }; | |
| 49 | |
| 50 class ModuleScriptLoaderTestModulator final : public DummyModulator { | |
| 51 public: | |
| 52 ModuleScriptLoaderTestModulator(RefPtr<ScriptState> scriptState) | |
| 53 : m_scriptState(std::move(scriptState)) {} | |
| 54 | |
| 55 ScriptModule compileModule(const String& script, | |
| 56 const String& urlStr) override { | |
| 57 ScriptState::Scope scope(m_scriptState.get()); | |
| 58 return ScriptModule::compile(m_scriptState->isolate(), | |
| 59 "export default 'foo';", ""); | |
| 60 } | |
| 61 | |
| 62 private: | |
| 63 RefPtr<ScriptState> m_scriptState; | |
| 64 }; | |
| 65 | |
| 66 } // namespace | |
| 67 | |
| 68 class ModuleScriptLoaderTest : public testing::Test { | |
| 69 public: | |
| 70 void SetUp() override; | |
| 71 | |
| 72 LocalFrame& frame() { return m_dummyPageHolder->frame(); } | |
| 73 Document& document() { return m_dummyPageHolder->document(); } | |
| 74 ResourceFetcher* fetcher() { return m_fetcher.get(); } | |
| 75 Modulator* modulator() { return m_modulator.get(); } | |
| 76 | |
| 77 protected: | |
| 78 std::unique_ptr<DummyPageHolder> m_dummyPageHolder; | |
| 79 Persistent<ResourceFetcher> m_fetcher; | |
| 80 Persistent<Modulator> m_modulator; | |
| 81 }; | |
| 82 | |
| 83 void ModuleScriptLoaderTest::SetUp() { | |
| 84 m_dummyPageHolder = DummyPageHolder::create(IntSize(500, 500)); | |
| 85 document().setURL(KURL(KURL(), "https://example.test")); | |
| 86 m_fetcher = ResourceFetcher::create( | |
| 87 MockFetchContext::create(MockFetchContext::kShouldLoadNewResource)); | |
| 88 m_modulator = | |
| 89 new ModuleScriptLoaderTestModulator(ScriptState::forMainWorld(&frame())); | |
| 90 } | |
| 91 | |
| 92 TEST_F(ModuleScriptLoaderTest, fetchDataURL) { | |
| 93 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler> | |
| 94 platform; | |
| 95 platform->advanceClockSeconds(1.); // For non-zero DocumentParserTimings | |
| 96 ModuleScriptLoaderRegistry* registry = ModuleScriptLoaderRegistry::create(); | |
| 97 KURL url(KURL(), "data:text/javascript,export default 'grapes';"); | |
| 98 ModuleScriptFetchRequest moduleRequest( | |
| 99 url, String(), ParserInserted, WebURLRequest::FetchCredentialsModeOmit); | |
| 100 TestModuleScriptLoaderClient* client = new TestModuleScriptLoaderClient; | |
| 101 registry->fetch(moduleRequest, ModuleGraphLevel::TopLevelModuleFetch, | |
| 102 modulator(), fetcher(), client); | |
| 103 | |
|
hiroshige
2017/02/20 23:15:32
Could you add EXPECT_FALSE(client->wasNotifyFinish
kouhei (in TOK)
2017/02/22 03:52:29
Actually this was checking sync use case. I'll add
| |
| 104 platform->runUntilIdle(); | |
| 105 | |
| 106 EXPECT_TRUE(client->wasNotifyFinished()); | |
| 107 EXPECT_TRUE(client->moduleScript()); | |
| 108 EXPECT_EQ(client->moduleScript()->instantiationState(), | |
| 109 ModuleInstantiationState::Uninstantiated); | |
| 110 } | |
| 111 | |
| 112 } // namespace blink | |
| OLD | NEW |