Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1305)

Side by Side Diff: third_party/WebKit/Source/web/WebEmbeddedWorkerImplTest.cpp

Issue 2847983002: Use unique_ptr for Create{ServiceWorkerNetworkProvider,ApplicationCacheHost} (Closed)
Patch Set: addressed comments Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "public/web/WebEmbeddedWorker.h" 5 #include "public/web/WebEmbeddedWorker.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include "platform/testing/URLTestHelpers.h" 8 #include "platform/testing/URLTestHelpers.h"
9 #include "platform/testing/UnitTestHelpers.h" 9 #include "platform/testing/UnitTestHelpers.h"
10 #include "platform/wtf/PtrUtil.h" 10 #include "platform/wtf/PtrUtil.h"
11 #include "public/platform/Platform.h" 11 #include "public/platform/Platform.h"
12 #include "public/platform/WebURLLoaderMockFactory.h" 12 #include "public/platform/WebURLLoaderMockFactory.h"
13 #include "public/platform/WebURLResponse.h" 13 #include "public/platform/WebURLResponse.h"
14 #include "public/platform/modules/serviceworker/WebServiceWorkerNetworkProvider. h"
14 #include "public/platform/modules/serviceworker/WebServiceWorkerProvider.h" 15 #include "public/platform/modules/serviceworker/WebServiceWorkerProvider.h"
15 #include "public/web/WebEmbeddedWorkerStartData.h" 16 #include "public/web/WebEmbeddedWorkerStartData.h"
16 #include "public/web/WebSettings.h" 17 #include "public/web/WebSettings.h"
17 #include "public/web/modules/serviceworker/WebServiceWorkerContextClient.h" 18 #include "public/web/modules/serviceworker/WebServiceWorkerContextClient.h"
18 #include "testing/gmock/include/gmock/gmock.h" 19 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
20 21
21 namespace blink { 22 namespace blink {
22 namespace { 23 namespace {
23 24
24 class MockServiceWorkerContextClient : public WebServiceWorkerContextClient { 25 class MockServiceWorkerContextClient : public WebServiceWorkerContextClient {
25 public: 26 public:
26 MockServiceWorkerContextClient() : has_associated_registration_(true) {} 27 MockServiceWorkerContextClient() : has_associated_registration_(true) {}
27 ~MockServiceWorkerContextClient() override {} 28 ~MockServiceWorkerContextClient() override {}
28 MOCK_METHOD0(WorkerReadyForInspection, void()); 29 MOCK_METHOD0(WorkerReadyForInspection, void());
29 MOCK_METHOD0(WorkerContextFailedToStart, void()); 30 MOCK_METHOD0(WorkerContextFailedToStart, void());
30 MOCK_METHOD0(WorkerScriptLoaded, void()); 31 MOCK_METHOD0(WorkerScriptLoaded, void());
31 MOCK_METHOD0(CreateServiceWorkerNetworkProvider, 32
33 // Work-around for mocking a method that return unique_ptr.
34 MOCK_METHOD0(CreateServiceWorkerNetworkProviderProxy,
32 WebServiceWorkerNetworkProvider*()); 35 WebServiceWorkerNetworkProvider*());
33 MOCK_METHOD0(CreateServiceWorkerProvider, WebServiceWorkerProvider*()); 36 MOCK_METHOD0(CreateServiceWorkerProviderProxy, WebServiceWorkerProvider*());
37 std::unique_ptr<WebServiceWorkerNetworkProvider>
38 CreateServiceWorkerNetworkProvider() override {
39 return std::unique_ptr<WebServiceWorkerNetworkProvider>(
40 CreateServiceWorkerNetworkProviderProxy());
41 }
42 std::unique_ptr<WebServiceWorkerProvider> CreateServiceWorkerProvider()
43 override {
44 return std::unique_ptr<WebServiceWorkerProvider>(
45 CreateServiceWorkerProviderProxy());
46 }
47
34 bool HasAssociatedRegistration() override { 48 bool HasAssociatedRegistration() override {
35 return has_associated_registration_; 49 return has_associated_registration_;
36 } 50 }
37 void SetHasAssociatedRegistration(bool has_associated_registration) { 51 void SetHasAssociatedRegistration(bool has_associated_registration) {
38 has_associated_registration_ = has_associated_registration; 52 has_associated_registration_ = has_associated_registration;
39 } 53 }
40 void GetClient(const WebString&, 54 void GetClient(const WebString&,
41 std::unique_ptr<WebServiceWorkerClientCallbacks>) override { 55 std::unique_ptr<WebServiceWorkerClientCallbacks>) override {
42 NOTREACHED(); 56 NOTREACHED();
43 } 57 }
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 worker_->TerminateWorkerContext(); 150 worker_->TerminateWorkerContext();
137 ::testing::Mock::VerifyAndClearExpectations(mock_client_); 151 ::testing::Mock::VerifyAndClearExpectations(mock_client_);
138 } 152 }
139 153
140 TEST_F(WebEmbeddedWorkerImplTest, TerminateWhileLoadingScript) { 154 TEST_F(WebEmbeddedWorkerImplTest, TerminateWhileLoadingScript) {
141 EXPECT_CALL(*mock_client_, WorkerReadyForInspection()).Times(1); 155 EXPECT_CALL(*mock_client_, WorkerReadyForInspection()).Times(1);
142 worker_->StartWorkerContext(start_data_); 156 worker_->StartWorkerContext(start_data_);
143 ::testing::Mock::VerifyAndClearExpectations(mock_client_); 157 ::testing::Mock::VerifyAndClearExpectations(mock_client_);
144 158
145 // Load the shadow page. 159 // Load the shadow page.
146 EXPECT_CALL(*mock_client_, CreateServiceWorkerNetworkProvider()) 160 EXPECT_CALL(*mock_client_, CreateServiceWorkerNetworkProviderProxy())
147 .WillOnce(::testing::Return(nullptr)); 161 .WillOnce(::testing::Return(nullptr));
148 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests(); 162 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests();
149 ::testing::Mock::VerifyAndClearExpectations(mock_client_); 163 ::testing::Mock::VerifyAndClearExpectations(mock_client_);
150 164
151 // Terminate before loading the script. 165 // Terminate before loading the script.
152 EXPECT_CALL(*mock_client_, WorkerContextFailedToStart()).Times(1); 166 EXPECT_CALL(*mock_client_, WorkerContextFailedToStart()).Times(1);
153 worker_->TerminateWorkerContext(); 167 worker_->TerminateWorkerContext();
154 ::testing::Mock::VerifyAndClearExpectations(mock_client_); 168 ::testing::Mock::VerifyAndClearExpectations(mock_client_);
155 } 169 }
156 170
157 TEST_F(WebEmbeddedWorkerImplTest, TerminateWhilePausedAfterDownload) { 171 TEST_F(WebEmbeddedWorkerImplTest, TerminateWhilePausedAfterDownload) {
158 EXPECT_CALL(*mock_client_, WorkerReadyForInspection()).Times(1); 172 EXPECT_CALL(*mock_client_, WorkerReadyForInspection()).Times(1);
159 start_data_.pause_after_download_mode = 173 start_data_.pause_after_download_mode =
160 WebEmbeddedWorkerStartData::kPauseAfterDownload; 174 WebEmbeddedWorkerStartData::kPauseAfterDownload;
161 worker_->StartWorkerContext(start_data_); 175 worker_->StartWorkerContext(start_data_);
162 ::testing::Mock::VerifyAndClearExpectations(mock_client_); 176 ::testing::Mock::VerifyAndClearExpectations(mock_client_);
163 177
164 // Load the shadow page. 178 // Load the shadow page.
165 EXPECT_CALL(*mock_client_, CreateServiceWorkerNetworkProvider()) 179 EXPECT_CALL(*mock_client_, CreateServiceWorkerNetworkProviderProxy())
166 .WillOnce(::testing::Return(nullptr)); 180 .WillOnce(::testing::Return(nullptr));
167 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests(); 181 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests();
168 ::testing::Mock::VerifyAndClearExpectations(mock_client_); 182 ::testing::Mock::VerifyAndClearExpectations(mock_client_);
169 183
170 // Load the script. 184 // Load the script.
171 EXPECT_CALL(*mock_client_, WorkerScriptLoaded()).Times(1); 185 EXPECT_CALL(*mock_client_, WorkerScriptLoaded()).Times(1);
172 EXPECT_CALL(*mock_client_, CreateServiceWorkerProvider()).Times(0); 186 EXPECT_CALL(*mock_client_, CreateServiceWorkerProviderProxy()).Times(0);
173 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests(); 187 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests();
174 ::testing::Mock::VerifyAndClearExpectations(mock_client_); 188 ::testing::Mock::VerifyAndClearExpectations(mock_client_);
175 189
176 // Terminate before resuming after download. 190 // Terminate before resuming after download.
177 EXPECT_CALL(*mock_client_, CreateServiceWorkerProvider()).Times(0); 191 EXPECT_CALL(*mock_client_, CreateServiceWorkerProviderProxy()).Times(0);
178 EXPECT_CALL(*mock_client_, WorkerContextFailedToStart()).Times(1); 192 EXPECT_CALL(*mock_client_, WorkerContextFailedToStart()).Times(1);
179 worker_->TerminateWorkerContext(); 193 worker_->TerminateWorkerContext();
180 ::testing::Mock::VerifyAndClearExpectations(mock_client_); 194 ::testing::Mock::VerifyAndClearExpectations(mock_client_);
181 } 195 }
182 196
183 TEST_F(WebEmbeddedWorkerImplTest, ScriptNotFound) { 197 TEST_F(WebEmbeddedWorkerImplTest, ScriptNotFound) {
184 WebURL script_url = 198 WebURL script_url =
185 URLTestHelpers::ToKURL("https://www.example.com/sw-404.js"); 199 URLTestHelpers::ToKURL("https://www.example.com/sw-404.js");
186 WebURLResponse response; 200 WebURLResponse response;
187 response.SetMIMEType("text/javascript"); 201 response.SetMIMEType("text/javascript");
188 response.SetHTTPStatusCode(404); 202 response.SetHTTPStatusCode(404);
189 WebURLError error; 203 WebURLError error;
190 error.reason = 1010; 204 error.reason = 1010;
191 error.domain = "WebEmbeddedWorkerImplTest"; 205 error.domain = "WebEmbeddedWorkerImplTest";
192 Platform::Current()->GetURLLoaderMockFactory()->RegisterErrorURL( 206 Platform::Current()->GetURLLoaderMockFactory()->RegisterErrorURL(
193 script_url, response, error); 207 script_url, response, error);
194 start_data_.script_url = script_url; 208 start_data_.script_url = script_url;
195 209
196 EXPECT_CALL(*mock_client_, WorkerReadyForInspection()).Times(1); 210 EXPECT_CALL(*mock_client_, WorkerReadyForInspection()).Times(1);
197 worker_->StartWorkerContext(start_data_); 211 worker_->StartWorkerContext(start_data_);
198 ::testing::Mock::VerifyAndClearExpectations(mock_client_); 212 ::testing::Mock::VerifyAndClearExpectations(mock_client_);
199 213
200 // Load the shadow page. 214 // Load the shadow page.
201 EXPECT_CALL(*mock_client_, CreateServiceWorkerNetworkProvider()) 215 EXPECT_CALL(*mock_client_, CreateServiceWorkerNetworkProviderProxy())
202 .WillOnce(::testing::Return(nullptr)); 216 .WillOnce(::testing::Return(nullptr));
203 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests(); 217 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests();
204 ::testing::Mock::VerifyAndClearExpectations(mock_client_); 218 ::testing::Mock::VerifyAndClearExpectations(mock_client_);
205 219
206 // Load the script. 220 // Load the script.
207 EXPECT_CALL(*mock_client_, WorkerScriptLoaded()).Times(0); 221 EXPECT_CALL(*mock_client_, WorkerScriptLoaded()).Times(0);
208 EXPECT_CALL(*mock_client_, CreateServiceWorkerProvider()).Times(0); 222 EXPECT_CALL(*mock_client_, CreateServiceWorkerProviderProxy()).Times(0);
209 EXPECT_CALL(*mock_client_, WorkerContextFailedToStart()).Times(1); 223 EXPECT_CALL(*mock_client_, WorkerContextFailedToStart()).Times(1);
210 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests(); 224 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests();
211 ::testing::Mock::VerifyAndClearExpectations(mock_client_); 225 ::testing::Mock::VerifyAndClearExpectations(mock_client_);
212 } 226 }
213 227
214 TEST_F(WebEmbeddedWorkerImplTest, NoRegistration) { 228 TEST_F(WebEmbeddedWorkerImplTest, NoRegistration) {
215 EXPECT_CALL(*mock_client_, WorkerReadyForInspection()).Times(1); 229 EXPECT_CALL(*mock_client_, WorkerReadyForInspection()).Times(1);
216 start_data_.pause_after_download_mode = 230 start_data_.pause_after_download_mode =
217 WebEmbeddedWorkerStartData::kPauseAfterDownload; 231 WebEmbeddedWorkerStartData::kPauseAfterDownload;
218 worker_->StartWorkerContext(start_data_); 232 worker_->StartWorkerContext(start_data_);
219 ::testing::Mock::VerifyAndClearExpectations(mock_client_); 233 ::testing::Mock::VerifyAndClearExpectations(mock_client_);
220 234
221 // Load the shadow page. 235 // Load the shadow page.
222 EXPECT_CALL(*mock_client_, CreateServiceWorkerNetworkProvider()) 236 EXPECT_CALL(*mock_client_, CreateServiceWorkerNetworkProviderProxy())
223 .WillOnce(::testing::Return(nullptr)); 237 .WillOnce(::testing::Return(nullptr));
224 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests(); 238 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests();
225 ::testing::Mock::VerifyAndClearExpectations(mock_client_); 239 ::testing::Mock::VerifyAndClearExpectations(mock_client_);
226 240
227 // Load the script. 241 // Load the script.
228 mock_client_->SetHasAssociatedRegistration(false); 242 mock_client_->SetHasAssociatedRegistration(false);
229 EXPECT_CALL(*mock_client_, WorkerScriptLoaded()).Times(0); 243 EXPECT_CALL(*mock_client_, WorkerScriptLoaded()).Times(0);
230 EXPECT_CALL(*mock_client_, CreateServiceWorkerProvider()).Times(0); 244 EXPECT_CALL(*mock_client_, CreateServiceWorkerProviderProxy()).Times(0);
231 EXPECT_CALL(*mock_client_, WorkerContextFailedToStart()).Times(1); 245 EXPECT_CALL(*mock_client_, WorkerContextFailedToStart()).Times(1);
232 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests(); 246 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests();
233 ::testing::Mock::VerifyAndClearExpectations(mock_client_); 247 ::testing::Mock::VerifyAndClearExpectations(mock_client_);
234 } 248 }
235 249
236 // The running worker is detected as a memory leak. crbug.com/586897 250 // The running worker is detected as a memory leak. crbug.com/586897
237 #if defined(ADDRESS_SANITIZER) 251 #if defined(ADDRESS_SANITIZER)
238 #define MAYBE_DontPauseAfterDownload DISABLED_DontPauseAfterDownload 252 #define MAYBE_DontPauseAfterDownload DISABLED_DontPauseAfterDownload
239 #else 253 #else
240 #define MAYBE_DontPauseAfterDownload DontPauseAfterDownload 254 #define MAYBE_DontPauseAfterDownload DontPauseAfterDownload
241 #endif 255 #endif
242 256
243 TEST_F(WebEmbeddedWorkerImplTest, MAYBE_DontPauseAfterDownload) { 257 TEST_F(WebEmbeddedWorkerImplTest, MAYBE_DontPauseAfterDownload) {
244 EXPECT_CALL(*mock_client_, WorkerReadyForInspection()).Times(1); 258 EXPECT_CALL(*mock_client_, WorkerReadyForInspection()).Times(1);
245 worker_->StartWorkerContext(start_data_); 259 worker_->StartWorkerContext(start_data_);
246 ::testing::Mock::VerifyAndClearExpectations(mock_client_); 260 ::testing::Mock::VerifyAndClearExpectations(mock_client_);
247 261
248 // Load the shadow page. 262 // Load the shadow page.
249 EXPECT_CALL(*mock_client_, CreateServiceWorkerNetworkProvider()) 263 EXPECT_CALL(*mock_client_, CreateServiceWorkerNetworkProviderProxy())
250 .WillOnce(::testing::Return(nullptr)); 264 .WillOnce(::testing::Return(nullptr));
251 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests(); 265 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests();
252 ::testing::Mock::VerifyAndClearExpectations(mock_client_); 266 ::testing::Mock::VerifyAndClearExpectations(mock_client_);
253 267
254 // Load the script. 268 // Load the script.
255 EXPECT_CALL(*mock_client_, WorkerScriptLoaded()).Times(1); 269 EXPECT_CALL(*mock_client_, WorkerScriptLoaded()).Times(1);
256 EXPECT_CALL(*mock_client_, CreateServiceWorkerProvider()) 270 EXPECT_CALL(*mock_client_, CreateServiceWorkerProviderProxy())
257 .WillOnce(::testing::Return(nullptr)); 271 .WillOnce(::testing::Return(nullptr));
258 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests(); 272 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests();
259 ::testing::Mock::VerifyAndClearExpectations(mock_client_); 273 ::testing::Mock::VerifyAndClearExpectations(mock_client_);
260 } 274 }
261 275
262 // The running worker is detected as a memory leak. crbug.com/586897 276 // The running worker is detected as a memory leak. crbug.com/586897
263 #if defined(ADDRESS_SANITIZER) 277 #if defined(ADDRESS_SANITIZER)
264 #define MAYBE_PauseAfterDownload DISABLED_PauseAfterDownload 278 #define MAYBE_PauseAfterDownload DISABLED_PauseAfterDownload
265 #else 279 #else
266 #define MAYBE_PauseAfterDownload PauseAfterDownload 280 #define MAYBE_PauseAfterDownload PauseAfterDownload
267 #endif 281 #endif
268 282
269 TEST_F(WebEmbeddedWorkerImplTest, MAYBE_PauseAfterDownload) { 283 TEST_F(WebEmbeddedWorkerImplTest, MAYBE_PauseAfterDownload) {
270 EXPECT_CALL(*mock_client_, WorkerReadyForInspection()).Times(1); 284 EXPECT_CALL(*mock_client_, WorkerReadyForInspection()).Times(1);
271 start_data_.pause_after_download_mode = 285 start_data_.pause_after_download_mode =
272 WebEmbeddedWorkerStartData::kPauseAfterDownload; 286 WebEmbeddedWorkerStartData::kPauseAfterDownload;
273 worker_->StartWorkerContext(start_data_); 287 worker_->StartWorkerContext(start_data_);
274 ::testing::Mock::VerifyAndClearExpectations(mock_client_); 288 ::testing::Mock::VerifyAndClearExpectations(mock_client_);
275 289
276 // Load the shadow page. 290 // Load the shadow page.
277 EXPECT_CALL(*mock_client_, CreateServiceWorkerNetworkProvider()) 291 EXPECT_CALL(*mock_client_, CreateServiceWorkerNetworkProviderProxy())
278 .WillOnce(::testing::Return(nullptr)); 292 .WillOnce(::testing::Return(nullptr));
279 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests(); 293 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests();
280 ::testing::Mock::VerifyAndClearExpectations(mock_client_); 294 ::testing::Mock::VerifyAndClearExpectations(mock_client_);
281 295
282 // Load the script. 296 // Load the script.
283 EXPECT_CALL(*mock_client_, WorkerScriptLoaded()).Times(1); 297 EXPECT_CALL(*mock_client_, WorkerScriptLoaded()).Times(1);
284 EXPECT_CALL(*mock_client_, CreateServiceWorkerProvider()).Times(0); 298 EXPECT_CALL(*mock_client_, CreateServiceWorkerProviderProxy()).Times(0);
285 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests(); 299 Platform::Current()->GetURLLoaderMockFactory()->ServeAsynchronousRequests();
286 ::testing::Mock::VerifyAndClearExpectations(mock_client_); 300 ::testing::Mock::VerifyAndClearExpectations(mock_client_);
287 301
288 // Resume after download. 302 // Resume after download.
289 EXPECT_CALL(*mock_client_, CreateServiceWorkerProvider()) 303 EXPECT_CALL(*mock_client_, CreateServiceWorkerProviderProxy())
290 .WillOnce(::testing::Return(nullptr)); 304 .WillOnce(::testing::Return(nullptr));
291 worker_->ResumeAfterDownload(); 305 worker_->ResumeAfterDownload();
292 ::testing::Mock::VerifyAndClearExpectations(mock_client_); 306 ::testing::Mock::VerifyAndClearExpectations(mock_client_);
293 } 307 }
294 308
295 } // namespace blink 309 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/web/WebEmbeddedWorkerImpl.cpp ('k') | third_party/WebKit/Source/web/WebSharedWorkerImpl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698