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" | |
12 #include "platform/NotImplemented.h" | |
13 #include "platform/Task.h" | |
14 #include "platform/ThreadSafeFunctional.h" | |
15 #include "platform/WaitableEvent.h" | |
16 #include "public/platform/WebAddressSpace.h" | |
17 #include "public/platform/WebScheduler.h" | 8 #include "public/platform/WebScheduler.h" |
18 #include "testing/gmock/include/gmock/gmock.h" | 9 #include "testing/gmock/include/gmock/gmock.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
20 | 11 |
21 using testing::_; | 12 using testing::_; |
22 using testing::AtMost; | 13 using testing::AtMost; |
23 using testing::Invoke; | 14 using testing::Invoke; |
24 using testing::Return; | 15 using testing::Return; |
25 using testing::Mock; | 16 using testing::Mock; |
26 | 17 |
27 namespace blink { | 18 namespace blink { |
28 | 19 |
29 namespace { | |
30 | |
31 class MockWorkerLoaderProxyProvider : public WorkerLoaderProxyProvider { | |
32 public: | |
33 MockWorkerLoaderProxyProvider() { } | |
34 ~MockWorkerLoaderProxyProvider() override { } | |
35 | |
36 void postTaskToLoader(PassOwnPtr<ExecutionContextTask>) override | |
37 { | |
38 notImplemented(); | |
39 } | |
40 | |
41 bool postTaskToWorkerGlobalScope(PassOwnPtr<ExecutionContextTask>) override | |
42 { | |
43 notImplemented(); | |
44 return false; | |
45 } | |
46 }; | |
47 | |
48 class MockWorkerReportingProxy : public WorkerReportingProxy { | |
49 public: | |
50 MockWorkerReportingProxy() { } | |
51 ~MockWorkerReportingProxy() override { } | |
52 | |
53 MOCK_METHOD5(reportException, void(const String& errorMessage, int lineNumbe
r, int columnNumber, const String& sourceURL, int exceptionId)); | |
54 MOCK_METHOD1(reportConsoleMessage, void(PassRefPtrWillBeRawPtr<ConsoleMessag
e>)); | |
55 MOCK_METHOD1(postMessageToPageInspector, void(const String&)); | |
56 MOCK_METHOD0(postWorkerConsoleAgentEnabled, void()); | |
57 MOCK_METHOD1(didEvaluateWorkerScript, void(bool success)); | |
58 MOCK_METHOD1(workerGlobalScopeStarted, void(WorkerGlobalScope*)); | |
59 MOCK_METHOD0(workerGlobalScopeClosed, void()); | |
60 MOCK_METHOD0(workerThreadTerminated, void()); | |
61 MOCK_METHOD0(willDestroyWorkerGlobalScope, void()); | |
62 }; | |
63 | |
64 void notifyScriptLoadedEventToWorkerThreadForTest(WorkerThread*); | |
65 | |
66 class FakeWorkerGlobalScope : public WorkerGlobalScope { | |
67 public: | |
68 typedef WorkerGlobalScope Base; | |
69 | |
70 FakeWorkerGlobalScope(const KURL& url, const String& userAgent, WorkerThread
* thread, PassOwnPtr<SecurityOrigin::PrivilegeData> starterOriginPrivilegeData,
PassOwnPtrWillBeRawPtr<WorkerClients> workerClients) | |
71 : WorkerGlobalScope(url, userAgent, thread, monotonicallyIncreasingTime(
), starterOriginPrivilegeData, workerClients, true /* withInspector */) | |
72 , m_thread(thread) | |
73 { | |
74 } | |
75 | |
76 ~FakeWorkerGlobalScope() override | |
77 { | |
78 } | |
79 | |
80 void scriptLoaded(size_t, size_t) override | |
81 { | |
82 notifyScriptLoadedEventToWorkerThreadForTest(m_thread); | |
83 } | |
84 | |
85 // EventTarget | |
86 const AtomicString& interfaceName() const override | |
87 { | |
88 return EventTargetNames::DedicatedWorkerGlobalScope; | |
89 } | |
90 | |
91 void logExceptionToConsole(const String&, int, const String&, int, int, Pass
RefPtr<ScriptCallStack>) override | |
92 { | |
93 } | |
94 | |
95 private: | |
96 WorkerThread* m_thread; | |
97 }; | |
98 | |
99 class WorkerThreadForTest : public WorkerThread { | |
100 public: | |
101 WorkerThreadForTest( | |
102 WorkerLoaderProxyProvider* mockWorkerLoaderProxyProvider, | |
103 WorkerReportingProxy& mockWorkerReportingProxy) | |
104 : WorkerThread(WorkerLoaderProxy::create(mockWorkerLoaderProxyProvider),
mockWorkerReportingProxy) | |
105 , m_thread(WebThreadSupportingGC::create("Test thread")) | |
106 , m_scriptLoadedEvent(adoptPtr(new WaitableEvent())) | |
107 { | |
108 } | |
109 | |
110 ~WorkerThreadForTest() override { } | |
111 | |
112 // WorkerThread implementation: | |
113 WebThreadSupportingGC& backingThread() override | |
114 { | |
115 return *m_thread; | |
116 } | |
117 void willDestroyIsolate() override | |
118 { | |
119 V8GCController::collectAllGarbageForTesting(v8::Isolate::GetCurrent()); | |
120 WorkerThread::willDestroyIsolate(); | |
121 } | |
122 | |
123 PassRefPtrWillBeRawPtr<WorkerGlobalScope> createWorkerGlobalScope(PassOwnPtr
<WorkerThreadStartupData> startupData) override | |
124 { | |
125 return adoptRefWillBeNoop(new FakeWorkerGlobalScope(startupData->m_scrip
tURL, startupData->m_userAgent, this, startupData->m_starterOriginPrivilegeData.
release(), startupData->m_workerClients.release())); | |
126 } | |
127 | |
128 void waitUntilScriptLoaded() | |
129 { | |
130 m_scriptLoadedEvent->wait(); | |
131 } | |
132 | |
133 void scriptLoaded() | |
134 { | |
135 m_scriptLoadedEvent->signal(); | |
136 } | |
137 | |
138 private: | |
139 OwnPtr<WebThreadSupportingGC> m_thread; | |
140 OwnPtr<WaitableEvent> m_scriptLoadedEvent; | |
141 }; | |
142 | |
143 void notifyScriptLoadedEventToWorkerThreadForTest(WorkerThread* thread) | |
144 { | |
145 static_cast<WorkerThreadForTest*>(thread)->scriptLoaded(); | |
146 } | |
147 | |
148 } // namespace | |
149 | |
150 class WorkerThreadTest : public testing::Test { | 20 class WorkerThreadTest : public testing::Test { |
151 public: | 21 public: |
152 void SetUp() override | 22 void SetUp() override |
153 { | 23 { |
154 m_mockWorkerLoaderProxyProvider = adoptPtr(new MockWorkerLoaderProxyProv
ider()); | 24 m_mockWorkerLoaderProxyProvider = adoptPtr(new MockWorkerLoaderProxyProv
ider()); |
155 m_mockWorkerReportingProxy = adoptPtr(new MockWorkerReportingProxy()); | 25 m_mockWorkerReportingProxy = adoptPtr(new MockWorkerReportingProxy()); |
156 m_securityOrigin = SecurityOrigin::create(KURL(ParsedURLString, "http://
fake.url/")); | 26 m_securityOrigin = SecurityOrigin::create(KURL(ParsedURLString, "http://
fake.url/")); |
157 m_workerThread = adoptPtr(new WorkerThreadForTest( | 27 m_workerThread = adoptPtr(new WorkerThreadForTest( |
158 m_mockWorkerLoaderProxyProvider.get(), | 28 m_mockWorkerLoaderProxyProvider.get(), |
159 *m_mockWorkerReportingProxy)); | 29 *m_mockWorkerReportingProxy)); |
160 } | 30 } |
161 | 31 |
162 void TearDown() override | 32 void TearDown() override |
163 { | 33 { |
164 m_workerThread->workerLoaderProxy()->detachProvider(m_mockWorkerLoaderPr
oxyProvider.get()); | 34 m_workerThread->workerLoaderProxy()->detachProvider(m_mockWorkerLoaderPr
oxyProvider.get()); |
165 } | 35 } |
166 | 36 |
167 void start() | 37 void start() |
168 { | 38 { |
169 startWithSourceCode("//fake source code"); | 39 startWithSourceCode("//fake source code"); |
170 } | 40 } |
171 | 41 |
172 void startWithSourceCode(const String& source) | 42 void startWithSourceCode(const String& source) |
173 { | 43 { |
174 OwnPtr<Vector<CSPHeaderAndType>> headers = adoptPtr(new Vector<CSPHeader
AndType>()); | 44 m_workerThread->startWithSourceCode(m_securityOrigin.get(), source); |
175 CSPHeaderAndType headerAndType("contentSecurityPolicy", ContentSecurityP
olicyHeaderTypeReport); | |
176 headers->append(headerAndType); | |
177 | |
178 OwnPtrWillBeRawPtr<WorkerClients> clients = nullptr; | |
179 | |
180 m_workerThread->start(WorkerThreadStartupData::create( | |
181 KURL(ParsedURLString, "http://fake.url/"), | |
182 "fake user agent", | |
183 source, | |
184 nullptr, | |
185 DontPauseWorkerGlobalScopeOnStart, | |
186 headers.release(), | |
187 m_securityOrigin.get(), | |
188 clients.release(), | |
189 WebAddressSpaceLocal, | |
190 V8CacheOptionsDefault)); | |
191 } | 45 } |
192 | 46 |
193 void waitForInit() | 47 void waitForInit() |
194 { | 48 { |
195 OwnPtr<WaitableEvent> completionEvent = adoptPtr(new WaitableEvent()); | 49 m_workerThread->waitForInit(); |
196 m_workerThread->backingThread().postTask(BLINK_FROM_HERE, threadSafeBind
(&WaitableEvent::signal, AllowCrossThreadAccess(completionEvent.get()))); | |
197 completionEvent->wait(); | |
198 } | 50 } |
199 | 51 |
200 protected: | 52 protected: |
201 void expectWorkerLifetimeReportingCalls() | 53 void expectWorkerLifetimeReportingCalls() |
202 { | 54 { |
203 EXPECT_CALL(*m_mockWorkerReportingProxy, workerGlobalScopeStarted(_)).Ti
mes(1); | 55 EXPECT_CALL(*m_mockWorkerReportingProxy, workerGlobalScopeStarted(_)).Ti
mes(1); |
204 EXPECT_CALL(*m_mockWorkerReportingProxy, didEvaluateWorkerScript(true)).
Times(1); | 56 EXPECT_CALL(*m_mockWorkerReportingProxy, didEvaluateWorkerScript(true)).
Times(1); |
205 EXPECT_CALL(*m_mockWorkerReportingProxy, workerThreadTerminated()).Times
(1); | 57 EXPECT_CALL(*m_mockWorkerReportingProxy, workerThreadTerminated()).Times
(1); |
206 EXPECT_CALL(*m_mockWorkerReportingProxy, willDestroyWorkerGlobalScope())
.Times(1); | 58 EXPECT_CALL(*m_mockWorkerReportingProxy, willDestroyWorkerGlobalScope())
.Times(1); |
207 } | 59 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 EXPECT_CALL(*m_mockWorkerReportingProxy, workerThreadTerminated()) | 99 EXPECT_CALL(*m_mockWorkerReportingProxy, workerThreadTerminated()) |
248 .Times(AtMost(1)); | 100 .Times(AtMost(1)); |
249 EXPECT_CALL(*m_mockWorkerReportingProxy, willDestroyWorkerGlobalScope()) | 101 EXPECT_CALL(*m_mockWorkerReportingProxy, willDestroyWorkerGlobalScope()) |
250 .Times(AtMost(1)); | 102 .Times(AtMost(1)); |
251 startWithSourceCode(source); | 103 startWithSourceCode(source); |
252 m_workerThread->waitUntilScriptLoaded(); | 104 m_workerThread->waitUntilScriptLoaded(); |
253 m_workerThread->terminateAndWait(); | 105 m_workerThread->terminateAndWait(); |
254 } | 106 } |
255 | 107 |
256 } // namespace blink | 108 } // namespace blink |
OLD | NEW |