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/ModuleScript.h" |
| 9 #include "core/fetch/MockFetchContext.h" |
| 10 #include "core/fetch/ResourceFetcher.h" |
| 11 #include "core/loader/modulescript/ModuleScriptLoaderClient.h" |
| 12 #include "core/loader/modulescript/ModuleScriptLoaderRegistry.h" |
| 13 #include "core/testing/DummyPageHolder.h" |
| 14 #include "platform/heap/Handle.h" |
| 15 #include "platform/testing/TestingPlatformSupport.h" |
| 16 #include "public/platform/Platform.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 namespace blink { |
| 20 |
| 21 namespace { |
| 22 |
| 23 class TestModuleScriptLoaderClient |
| 24 : public GarbageCollected<TestModuleScriptLoaderClient>, |
| 25 public ModuleScriptLoaderClient { |
| 26 USING_GARBAGE_COLLECTED_MIXIN(TestModuleScriptLoaderClient); |
| 27 |
| 28 public: |
| 29 TestModuleScriptLoaderClient() = default; |
| 30 virtual ~TestModuleScriptLoaderClient() = default; |
| 31 |
| 32 DEFINE_INLINE_TRACE() { visitor->trace(m_moduleScript); } |
| 33 |
| 34 void notifyFinishedNewSingleModule(ModuleScript* moduleScript) override { |
| 35 m_wasNotifyFinished = true; |
| 36 m_moduleScript = moduleScript; |
| 37 } |
| 38 |
| 39 bool wasNotifyFinished() const { return m_wasNotifyFinished; } |
| 40 ModuleScript* moduleScript() { return m_moduleScript; } |
| 41 |
| 42 private: |
| 43 bool m_wasNotifyFinished = false; |
| 44 Member<ModuleScript> m_moduleScript; |
| 45 }; |
| 46 |
| 47 } // namespace |
| 48 |
| 49 class ModuleScriptLoaderTest : public testing::Test { |
| 50 public: |
| 51 void SetUp() override; |
| 52 |
| 53 LocalFrame& frame() { return m_dummyPageHolder->frame(); } |
| 54 Document& document() { return m_dummyPageHolder->document(); } |
| 55 ResourceFetcher* fetcher() { return m_fetcher.get(); } |
| 56 Modulator* modulator() { return document().ensureModulator(); } |
| 57 |
| 58 protected: |
| 59 TestingPlatformSupportWithMockScheduler m_platform; |
| 60 std::unique_ptr<DummyPageHolder> m_dummyPageHolder; |
| 61 Persistent<ResourceFetcher> m_fetcher; |
| 62 }; |
| 63 |
| 64 void ModuleScriptLoaderTest::SetUp() { |
| 65 m_platform.advanceClockSeconds(1.); // For non-zero DocumentParserTimings |
| 66 m_dummyPageHolder = DummyPageHolder::create(IntSize(500, 500)); |
| 67 document().setURL(KURL(KURL(), "https://example.test")); |
| 68 m_fetcher = ResourceFetcher::create( |
| 69 MockFetchContext::create(MockFetchContext::kShouldLoadNewResource)); |
| 70 } |
| 71 |
| 72 TEST_F(ModuleScriptLoaderTest, fetchDataURL) { |
| 73 ModuleScriptLoaderRegistry* registry = ModuleScriptLoaderRegistry::create(); |
| 74 KURL url(KURL(), "data:text/javascript,export default 'grapes';"); |
| 75 |
| 76 TestModuleScriptLoaderClient* client = new TestModuleScriptLoaderClient; |
| 77 registry->fetch(url, document().baseURL(), modulator(), fetcher(), client); |
| 78 |
| 79 m_platform.runUntilIdle(); |
| 80 |
| 81 EXPECT_TRUE(client->wasNotifyFinished()); |
| 82 EXPECT_TRUE(client->moduleScript()); |
| 83 EXPECT_EQ(client->moduleScript()->instantiationState(), |
| 84 InstantiationState::Uninstantiated); |
| 85 } |
| 86 |
| 87 } // namespace blink |
OLD | NEW |