Chromium Code Reviews| Index: third_party/WebKit/Source/core/workers/WorkerThreadTestHelper.h |
| diff --git a/third_party/WebKit/Source/core/workers/WorkerThreadTestHelper.h b/third_party/WebKit/Source/core/workers/WorkerThreadTestHelper.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6b6180323dbb7cf6536958b0e6fb8e731c6b3ad8 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/core/workers/WorkerThreadTestHelper.h |
| @@ -0,0 +1,136 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "bindings/core/v8/ScriptCallStack.h" |
| +#include "bindings/core/v8/V8GCController.h" |
| +#include "core/inspector/ConsoleMessage.h" |
| +#include "core/workers/WorkerReportingProxy.h" |
| +#include "core/workers/WorkerThread.h" |
| +#include "core/workers/WorkerThreadStartupData.h" |
| +#include "platform/NotImplemented.h" |
| +#include "platform/WaitableEvent.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
|
tyoshino (SeeGerritForStatus)
2016/03/02 07:57:59
add PassOwnPtr.h
add Heap.h
add WorkerLoaderProxy.
hiroshige
2016/03/08 23:40:59
Added.
tyoshino (SeeGerritForStatus)
2016/03/09 06:26:12
Right, sorry!
|
| + |
| +namespace blink { |
| + |
| +class MockWorkerLoaderProxyProvider : public WorkerLoaderProxyProvider { |
| +public: |
| + MockWorkerLoaderProxyProvider() { } |
| + ~MockWorkerLoaderProxyProvider() override { } |
| + |
| + void postTaskToLoader(PassOwnPtr<ExecutionContextTask>) override |
| + { |
| + notImplemented(); |
| + } |
| + |
| + bool postTaskToWorkerGlobalScope(PassOwnPtr<ExecutionContextTask>) override |
| + { |
| + notImplemented(); |
| + return false; |
| + } |
| +}; |
| + |
| +class MockWorkerReportingProxy : public WorkerReportingProxy { |
| +public: |
| + MockWorkerReportingProxy() { } |
| + ~MockWorkerReportingProxy() override { } |
| + |
| + MOCK_METHOD5(reportException, void(const String& errorMessage, int lineNumber, int columnNumber, const String& sourceURL, int exceptionId)); |
| + MOCK_METHOD1(reportConsoleMessage, void(PassRefPtrWillBeRawPtr<ConsoleMessage>)); |
| + MOCK_METHOD1(postMessageToPageInspector, void(const String&)); |
| + MOCK_METHOD0(postWorkerConsoleAgentEnabled, void()); |
| + MOCK_METHOD1(didEvaluateWorkerScript, void(bool success)); |
| + MOCK_METHOD1(workerGlobalScopeStarted, void(WorkerGlobalScope*)); |
| + MOCK_METHOD0(workerGlobalScopeClosed, void()); |
| + MOCK_METHOD0(workerThreadTerminated, void()); |
| + MOCK_METHOD0(willDestroyWorkerGlobalScope, void()); |
| +}; |
| + |
| +void notifyScriptLoadedEventToWorkerThreadForTest(WorkerThread*); |
| + |
| +class FakeWorkerGlobalScope : public WorkerGlobalScope { |
| +public: |
| + typedef WorkerGlobalScope Base; |
| + |
| + FakeWorkerGlobalScope(const KURL& url, const String& userAgent, WorkerThread* thread, PassOwnPtr<SecurityOrigin::PrivilegeData> starterOriginPrivilegeData, PassOwnPtrWillBeRawPtr<WorkerClients> workerClients) |
| + : WorkerGlobalScope(url, userAgent, thread, monotonicallyIncreasingTime(), starterOriginPrivilegeData, workerClients) |
| + , m_thread(thread) |
| + { |
| + } |
| + |
| + ~FakeWorkerGlobalScope() override |
| + { |
| + } |
| + |
| + void scriptLoaded(size_t, size_t) override |
| + { |
| + notifyScriptLoadedEventToWorkerThreadForTest(m_thread); |
| + } |
| + |
| + // EventTarget |
| + const AtomicString& interfaceName() const override |
| + { |
| + return EventTargetNames::DedicatedWorkerGlobalScope; |
| + } |
| + |
| + void logExceptionToConsole(const String&, int, const String&, int, int, PassRefPtr<ScriptCallStack>) override |
| + { |
| + } |
| + |
| +private: |
| + WorkerThread* m_thread; |
| +}; |
| + |
| +class WorkerThreadForTest : public WorkerThread { |
| +public: |
| + WorkerThreadForTest( |
| + WorkerLoaderProxyProvider* mockWorkerLoaderProxyProvider, |
| + WorkerReportingProxy& mockWorkerReportingProxy) |
| + : WorkerThread(WorkerLoaderProxy::create(mockWorkerLoaderProxyProvider), mockWorkerReportingProxy) |
| + , m_thread(WebThreadSupportingGC::create("Test thread")) |
| + , m_scriptLoadedEvent(adoptPtr(new WaitableEvent())) |
| + { |
| + ASSERT(m_thread); |
| + } |
| + |
| + ~WorkerThreadForTest() override { } |
| + |
| + // WorkerThread implementation: |
| + WebThreadSupportingGC& backingThread() override |
| + { |
| + ASSERT(m_thread); |
| + return *m_thread; |
| + } |
| + void willDestroyIsolate() override |
| + { |
| + V8GCController::collectAllGarbageForTesting(v8::Isolate::GetCurrent()); |
| + WorkerThread::willDestroyIsolate(); |
| + } |
| + |
| + PassRefPtrWillBeRawPtr<WorkerGlobalScope> createWorkerGlobalScope(PassOwnPtr<WorkerThreadStartupData> startupData) override |
| + { |
| + return adoptRefWillBeNoop(new FakeWorkerGlobalScope(startupData->m_scriptURL, startupData->m_userAgent, this, startupData->m_starterOriginPrivilegeData.release(), startupData->m_workerClients.release())); |
| + } |
| + |
| + void waitUntilScriptLoaded() |
| + { |
| + m_scriptLoadedEvent->wait(); |
| + } |
| + |
| + void scriptLoaded() |
| + { |
| + m_scriptLoadedEvent->signal(); |
| + } |
| + |
| +private: |
| + OwnPtr<WebThreadSupportingGC> m_thread; |
| + OwnPtr<WaitableEvent> m_scriptLoadedEvent; |
| +}; |
| + |
| +inline void notifyScriptLoadedEventToWorkerThreadForTest(WorkerThread* thread) |
| +{ |
| + static_cast<WorkerThreadForTest*>(thread)->scriptLoaded(); |
| +} |
| + |
| +} // namespace blink |