Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "core/workers/WorkerThread.h" | 5 #include "core/workers/WorkerThread.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ScriptCallStack.h" | 7 #include "core/workers/WorkerThreadTestHelper.h" |
| 8 #include "bindings/core/v8/V8GCController.h" | |
| 9 #include "core/inspector/ConsoleMessage.h" | |
| 10 #include "core/workers/WorkerReportingProxy.h" | |
| 11 #include "core/workers/WorkerThreadStartupData.h" | |
|
tyoshino (SeeGerritForStatus)
2016/03/02 07:57:59
WorkerThreadStartupData is used in the code that r
hiroshige
2016/03/08 23:40:59
I moved the code to WorkerThreadTestHelper.h so Wo
| |
| 12 #include "platform/NotImplemented.h" | |
| 13 #include "platform/Task.h" | |
| 14 #include "platform/ThreadSafeFunctional.h" | 8 #include "platform/ThreadSafeFunctional.h" |
| 15 #include "platform/WaitableEvent.h" | |
| 16 #include "public/platform/WebScheduler.h" | 9 #include "public/platform/WebScheduler.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" | |
|
tyoshino (SeeGerritForStatus)
2016/03/02 07:57:59
EXPECT_CALL is used in the code that remains in th
hiroshige
2016/03/08 23:40:59
Done.
| |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 11 |
| 20 using testing::_; | 12 using testing::_; |
| 21 using testing::AtMost; | 13 using testing::AtMost; |
| 22 using testing::Invoke; | 14 using testing::Invoke; |
| 23 using testing::Return; | 15 using testing::Return; |
| 24 using testing::Mock; | 16 using testing::Mock; |
| 25 | 17 |
| 26 namespace blink { | 18 namespace blink { |
| 27 | 19 |
| 28 namespace { | |
| 29 | |
| 30 class MockWorkerLoaderProxyProvider : public WorkerLoaderProxyProvider { | |
| 31 public: | |
| 32 MockWorkerLoaderProxyProvider() { } | |
| 33 ~MockWorkerLoaderProxyProvider() override { } | |
| 34 | |
| 35 void postTaskToLoader(PassOwnPtr<ExecutionContextTask>) override | |
| 36 { | |
| 37 notImplemented(); | |
| 38 } | |
| 39 | |
| 40 bool postTaskToWorkerGlobalScope(PassOwnPtr<ExecutionContextTask>) override | |
| 41 { | |
| 42 notImplemented(); | |
| 43 return false; | |
| 44 } | |
| 45 }; | |
| 46 | |
| 47 class MockWorkerReportingProxy : public WorkerReportingProxy { | |
| 48 public: | |
| 49 MockWorkerReportingProxy() { } | |
| 50 ~MockWorkerReportingProxy() override { } | |
| 51 | |
| 52 MOCK_METHOD5(reportException, void(const String& errorMessage, int lineNumbe r, int columnNumber, const String& sourceURL, int exceptionId)); | |
| 53 MOCK_METHOD1(reportConsoleMessage, void(PassRefPtrWillBeRawPtr<ConsoleMessag e>)); | |
| 54 MOCK_METHOD1(postMessageToPageInspector, void(const String&)); | |
| 55 MOCK_METHOD0(postWorkerConsoleAgentEnabled, void()); | |
| 56 MOCK_METHOD1(didEvaluateWorkerScript, void(bool success)); | |
| 57 MOCK_METHOD1(workerGlobalScopeStarted, void(WorkerGlobalScope*)); | |
| 58 MOCK_METHOD0(workerGlobalScopeClosed, void()); | |
| 59 MOCK_METHOD0(workerThreadTerminated, void()); | |
| 60 MOCK_METHOD0(willDestroyWorkerGlobalScope, void()); | |
| 61 }; | |
| 62 | |
| 63 void notifyScriptLoadedEventToWorkerThreadForTest(WorkerThread*); | |
| 64 | |
| 65 class FakeWorkerGlobalScope : public WorkerGlobalScope { | |
| 66 public: | |
| 67 typedef WorkerGlobalScope Base; | |
| 68 | |
| 69 FakeWorkerGlobalScope(const KURL& url, const String& userAgent, WorkerThread * thread, PassOwnPtr<SecurityOrigin::PrivilegeData> starterOriginPrivilegeData, PassOwnPtrWillBeRawPtr<WorkerClients> workerClients) | |
| 70 : WorkerGlobalScope(url, userAgent, thread, monotonicallyIncreasingTime( ), starterOriginPrivilegeData, workerClients) | |
| 71 , m_thread(thread) | |
| 72 { | |
| 73 } | |
| 74 | |
| 75 ~FakeWorkerGlobalScope() override | |
| 76 { | |
| 77 } | |
| 78 | |
| 79 void scriptLoaded(size_t, size_t) override | |
| 80 { | |
| 81 notifyScriptLoadedEventToWorkerThreadForTest(m_thread); | |
| 82 } | |
| 83 | |
| 84 // EventTarget | |
| 85 const AtomicString& interfaceName() const override | |
| 86 { | |
| 87 return EventTargetNames::DedicatedWorkerGlobalScope; | |
| 88 } | |
| 89 | |
| 90 void logExceptionToConsole(const String&, int, const String&, int, int, Pass RefPtr<ScriptCallStack>) override | |
| 91 { | |
| 92 } | |
| 93 | |
| 94 private: | |
| 95 WorkerThread* m_thread; | |
| 96 }; | |
| 97 | |
| 98 class WorkerThreadForTest : public WorkerThread { | |
| 99 public: | |
| 100 WorkerThreadForTest( | |
| 101 WorkerLoaderProxyProvider* mockWorkerLoaderProxyProvider, | |
| 102 WorkerReportingProxy& mockWorkerReportingProxy) | |
| 103 : WorkerThread(WorkerLoaderProxy::create(mockWorkerLoaderProxyProvider), mockWorkerReportingProxy) | |
| 104 , m_thread(WebThreadSupportingGC::create("Test thread")) | |
| 105 , m_scriptLoadedEvent(adoptPtr(new WaitableEvent())) | |
| 106 { | |
| 107 } | |
| 108 | |
| 109 ~WorkerThreadForTest() override { } | |
| 110 | |
| 111 // WorkerThread implementation: | |
| 112 WebThreadSupportingGC& backingThread() override | |
| 113 { | |
| 114 return *m_thread; | |
| 115 } | |
| 116 void willDestroyIsolate() override | |
| 117 { | |
| 118 V8GCController::collectAllGarbageForTesting(v8::Isolate::GetCurrent()); | |
| 119 WorkerThread::willDestroyIsolate(); | |
| 120 } | |
| 121 | |
| 122 PassRefPtrWillBeRawPtr<WorkerGlobalScope> createWorkerGlobalScope(PassOwnPtr <WorkerThreadStartupData> startupData) override | |
| 123 { | |
| 124 return adoptRefWillBeNoop(new FakeWorkerGlobalScope(startupData->m_scrip tURL, startupData->m_userAgent, this, startupData->m_starterOriginPrivilegeData. release(), startupData->m_workerClients.release())); | |
| 125 } | |
| 126 | |
| 127 void waitUntilScriptLoaded() | |
| 128 { | |
| 129 m_scriptLoadedEvent->wait(); | |
| 130 } | |
| 131 | |
| 132 void scriptLoaded() | |
| 133 { | |
| 134 m_scriptLoadedEvent->signal(); | |
| 135 } | |
| 136 | |
| 137 private: | |
| 138 OwnPtr<WebThreadSupportingGC> m_thread; | |
| 139 OwnPtr<WaitableEvent> m_scriptLoadedEvent; | |
| 140 }; | |
| 141 | |
| 142 void notifyScriptLoadedEventToWorkerThreadForTest(WorkerThread* thread) | |
| 143 { | |
| 144 static_cast<WorkerThreadForTest*>(thread)->scriptLoaded(); | |
| 145 } | |
| 146 | |
| 147 } // namespace | |
| 148 | |
| 149 class WorkerThreadTest : public testing::Test { | 20 class WorkerThreadTest : public testing::Test { |
| 150 public: | 21 public: |
| 151 void SetUp() override | 22 void SetUp() override |
| 152 { | 23 { |
| 153 m_mockWorkerLoaderProxyProvider = adoptPtr(new MockWorkerLoaderProxyProv ider()); | 24 m_mockWorkerLoaderProxyProvider = adoptPtr(new MockWorkerLoaderProxyProv ider()); |
| 154 m_mockWorkerReportingProxy = adoptPtr(new MockWorkerReportingProxy()); | 25 m_mockWorkerReportingProxy = adoptPtr(new MockWorkerReportingProxy()); |
| 155 m_securityOrigin = SecurityOrigin::create(KURL(ParsedURLString, "http:// fake.url/")); | 26 m_securityOrigin = SecurityOrigin::create(KURL(ParsedURLString, "http:// fake.url/")); |
| 156 m_workerThread = adoptRef(new WorkerThreadForTest( | 27 m_workerThread = adoptRef(new WorkerThreadForTest( |
| 157 m_mockWorkerLoaderProxyProvider.get(), | 28 m_mockWorkerLoaderProxyProvider.get(), |
| 158 *m_mockWorkerReportingProxy)); | 29 *m_mockWorkerReportingProxy)); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 245 EXPECT_CALL(*m_mockWorkerReportingProxy, workerThreadTerminated()) | 116 EXPECT_CALL(*m_mockWorkerReportingProxy, workerThreadTerminated()) |
| 246 .Times(AtMost(1)); | 117 .Times(AtMost(1)); |
| 247 EXPECT_CALL(*m_mockWorkerReportingProxy, willDestroyWorkerGlobalScope()) | 118 EXPECT_CALL(*m_mockWorkerReportingProxy, willDestroyWorkerGlobalScope()) |
| 248 .Times(AtMost(1)); | 119 .Times(AtMost(1)); |
| 249 startWithSourceCode(source); | 120 startWithSourceCode(source); |
| 250 m_workerThread->waitUntilScriptLoaded(); | 121 m_workerThread->waitUntilScriptLoaded(); |
| 251 m_workerThread->terminateAndWait(); | 122 m_workerThread->terminateAndWait(); |
| 252 } | 123 } |
| 253 | 124 |
| 254 } // namespace blink | 125 } // namespace blink |
| OLD | NEW |