| 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/testing/DummyPageHolder.h" |
| 6 #include "core/workers/ThreadedWorkletMessagingProxy.h" |
| 7 #include "core/workers/ThreadedWorkletObjectProxy.h" |
| 8 #include "core/workers/WorkerInspectorProxy.h" |
| 9 #include "core/workers/WorkerThread.h" |
| 10 #include "core/workers/WorkerThreadStartupData.h" |
| 11 #include "core/workers/WorkerThreadTestHelper.h" |
| 12 #include "core/workers/WorkletThreadHolder.h" |
| 13 #include "modules/webaudio/AudioWorkletGlobalScope.h" |
| 14 #include "platform/weborigin/SecurityOrigin.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace blink { |
| 18 |
| 19 class AudioWorkletThreadForTest : public WorkerThread { |
| 20 public: |
| 21 AudioWorkletThreadForTest( |
| 22 WorkerLoaderProxyProvider* workerLoaderProxyProvider, |
| 23 WorkerReportingProxy& workerReportingProxy) |
| 24 : WorkerThread(WorkerLoaderProxy::create(workerLoaderProxyProvider), |
| 25 workerReportingProxy) {} |
| 26 ~AudioWorkletThreadForTest() override{}; |
| 27 |
| 28 WorkerBackingThread& workerBackingThread() override { |
| 29 return *WorkletThreadHolder<AudioWorkletThreadForTest>::getInstance() |
| 30 ->thread(); |
| 31 } |
| 32 |
| 33 void clearWorkerBackingThread() override {} |
| 34 |
| 35 WorkerOrWorkletGlobalScope* createWorkerGlobalScope( |
| 36 std::unique_ptr<WorkerThreadStartupData> startupData) final { |
| 37 RefPtr<SecurityOrigin> securityOrigin = |
| 38 SecurityOrigin::create(startupData->m_scriptURL); |
| 39 return AudioWorkletGlobalScope::create( |
| 40 startupData->m_scriptURL, startupData->m_userAgent, |
| 41 securityOrigin.release(), this->isolate(), this); |
| 42 } |
| 43 |
| 44 bool isOwningBackingThread() const final { return false; } |
| 45 |
| 46 static void ensureSharedBackingThread() { |
| 47 DCHECK(isMainThread()); |
| 48 WorkletThreadHolder<AudioWorkletThreadForTest>::createForTest( |
| 49 "AudioWorkletThreadForTest"); |
| 50 } |
| 51 |
| 52 static void clearSharedBackingThread() { |
| 53 DCHECK(isMainThread()); |
| 54 WorkletThreadHolder<AudioWorkletThreadForTest>::clearInstance(); |
| 55 } |
| 56 }; |
| 57 |
| 58 class AudioWorkletMessagingProxyForTest : public ThreadedWorkletMessagingProxy { |
| 59 public: |
| 60 AudioWorkletMessagingProxyForTest(ExecutionContext* executionContext) |
| 61 : ThreadedWorkletMessagingProxy(executionContext) { |
| 62 m_mockWorkerLoaderProxyProvider = |
| 63 WTF::makeUnique<MockWorkerLoaderProxyProvider>(); |
| 64 setWorkerThreadForTest(WTF::makeUnique<AudioWorkletThreadForTest>( |
| 65 m_mockWorkerLoaderProxyProvider.get(), workletObjectProxy())); |
| 66 AudioWorkletThreadForTest::ensureSharedBackingThread(); |
| 67 } |
| 68 |
| 69 ~AudioWorkletMessagingProxyForTest() override { |
| 70 workerThread()->workerLoaderProxy()->detachProvider( |
| 71 m_mockWorkerLoaderProxyProvider.get()); |
| 72 workerThread()->terminateAndWait(); |
| 73 AudioWorkletThreadForTest::clearSharedBackingThread(); |
| 74 }; |
| 75 |
| 76 void start() { |
| 77 KURL scriptURL(ParsedURLString, "http://fake.url/"); |
| 78 std::unique_ptr<Vector<char>> cachedMetaData = nullptr; |
| 79 Vector<CSPHeaderAndType> contentSecurityPolicyHeaders; |
| 80 String referrerPolicy = ""; |
| 81 m_securityOrigin = SecurityOrigin::create(scriptURL); |
| 82 WorkerClients* workerClients = nullptr; |
| 83 Vector<String> originTrialTokens; |
| 84 std::unique_ptr<WorkerSettings> workerSettings = nullptr; |
| 85 workerThread()->start( |
| 86 WorkerThreadStartupData::create( |
| 87 scriptURL, "fake user agent", "// fake source code", |
| 88 std::move(cachedMetaData), DontPauseWorkerGlobalScopeOnStart, |
| 89 &contentSecurityPolicyHeaders, referrerPolicy, |
| 90 m_securityOrigin.get(), workerClients, WebAddressSpaceLocal, |
| 91 &originTrialTokens, std::move(workerSettings), |
| 92 WorkerV8Settings::Default()), |
| 93 getParentFrameTaskRunners()); |
| 94 workerInspectorProxy()->workerThreadCreated( |
| 95 toDocument(getExecutionContext()), workerThread(), scriptURL); |
| 96 } |
| 97 |
| 98 protected: |
| 99 std::unique_ptr<WorkerThread> createWorkerThread(double originTime) final { |
| 100 NOTREACHED(); |
| 101 return nullptr; |
| 102 } |
| 103 |
| 104 private: |
| 105 friend class AudioWorkletTest; |
| 106 |
| 107 std::unique_ptr<MockWorkerLoaderProxyProvider> |
| 108 m_mockWorkerLoaderProxyProvider; |
| 109 RefPtr<SecurityOrigin> m_securityOrigin; |
| 110 }; |
| 111 |
| 112 class AudioWorkletTest : public ::testing::Test { |
| 113 public: |
| 114 void SetUp() override { |
| 115 m_page = DummyPageHolder::create(); |
| 116 m_messagingProxy = |
| 117 WTF::makeUnique<AudioWorkletMessagingProxyForTest>(&m_page->document()); |
| 118 } |
| 119 |
| 120 AudioWorkletMessagingProxyForTest* messagingProxy() { |
| 121 return m_messagingProxy.get(); |
| 122 } |
| 123 |
| 124 AudioWorkletThreadForTest* workerThread() { |
| 125 return static_cast<AudioWorkletThreadForTest*>( |
| 126 m_messagingProxy->workerThread()); |
| 127 } |
| 128 |
| 129 Document& document() { return m_page->document(); } |
| 130 |
| 131 private: |
| 132 std::unique_ptr<DummyPageHolder> m_page; |
| 133 std::unique_ptr<AudioWorkletMessagingProxyForTest> m_messagingProxy; |
| 134 }; |
| 135 |
| 136 TEST_F(AudioWorkletTest, Basic) { |
| 137 messagingProxy()->start(); |
| 138 EXPECT_TRUE(workerThread()); |
| 139 |
| 140 // TODO(hongchan): |
| 141 // 1. Test instantiation of AudioWorkletThread |
| 142 // 2. Test instantiation of AudioWorkletGlobalScope |
| 143 // 3. Test registration of AudioWorkletProcessor |
| 144 // 4. Test instantiation of AudioWorkletProcessor |
| 145 } |
| 146 |
| 147 } // namespace blink |
| OLD | NEW |