| 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 "public/web/WebEmbeddedWorker.h" | 5 #include "public/web/WebEmbeddedWorker.h" |
| 6 | 6 |
| 7 #include "platform/testing/URLTestHelpers.h" | 7 #include "platform/testing/URLTestHelpers.h" |
| 8 #include "platform/testing/UnitTestHelpers.h" | 8 #include "platform/testing/UnitTestHelpers.h" |
| 9 #include "public/platform/Platform.h" | 9 #include "public/platform/Platform.h" |
| 10 #include "public/platform/WebURLLoaderMockFactory.h" |
| 10 #include "public/platform/WebURLResponse.h" | 11 #include "public/platform/WebURLResponse.h" |
| 11 #include "public/platform/WebUnitTestSupport.h" | |
| 12 #include "public/platform/modules/serviceworker/WebServiceWorkerProvider.h" | 12 #include "public/platform/modules/serviceworker/WebServiceWorkerProvider.h" |
| 13 #include "public/web/WebCache.h" |
| 13 #include "public/web/WebEmbeddedWorkerStartData.h" | 14 #include "public/web/WebEmbeddedWorkerStartData.h" |
| 14 #include "public/web/WebSettings.h" | 15 #include "public/web/WebSettings.h" |
| 15 #include "public/web/modules/serviceworker/WebServiceWorkerContextClient.h" | 16 #include "public/web/modules/serviceworker/WebServiceWorkerContextClient.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| 19 namespace blink { | 20 namespace blink { |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 class MockServiceWorkerContextClient | 23 class MockServiceWorkerContextClient |
| (...skipping 13 matching lines...) Expand all Loading... |
| 36 void SetUp() override | 37 void SetUp() override |
| 37 { | 38 { |
| 38 m_mockClient = new MockServiceWorkerContextClient(); | 39 m_mockClient = new MockServiceWorkerContextClient(); |
| 39 m_worker = adoptPtr(WebEmbeddedWorker::create(m_mockClient, nullptr)); | 40 m_worker = adoptPtr(WebEmbeddedWorker::create(m_mockClient, nullptr)); |
| 40 | 41 |
| 41 WebURL scriptURL = URLTestHelpers::toKURL("https://www.example.com/sw.js
"); | 42 WebURL scriptURL = URLTestHelpers::toKURL("https://www.example.com/sw.js
"); |
| 42 WebURLResponse response; | 43 WebURLResponse response; |
| 43 response.initialize(); | 44 response.initialize(); |
| 44 response.setMIMEType("text/javascript"); | 45 response.setMIMEType("text/javascript"); |
| 45 response.setHTTPStatusCode(200); | 46 response.setHTTPStatusCode(200); |
| 46 Platform::current()->unitTestSupport()->registerMockedURL(scriptURL, res
ponse, ""); | 47 Platform::current()->getURLLoaderMockFactory()->registerURL(scriptURL, r
esponse, ""); |
| 47 | 48 |
| 48 m_startData.scriptURL = scriptURL; | 49 m_startData.scriptURL = scriptURL; |
| 49 m_startData.userAgent = WebString("dummy user agent"); | 50 m_startData.userAgent = WebString("dummy user agent"); |
| 50 m_startData.pauseAfterDownloadMode = WebEmbeddedWorkerStartData::DontPau
seAfterDownload; | 51 m_startData.pauseAfterDownloadMode = WebEmbeddedWorkerStartData::DontPau
seAfterDownload; |
| 51 m_startData.waitForDebuggerMode = WebEmbeddedWorkerStartData::DontWaitFo
rDebugger; | 52 m_startData.waitForDebuggerMode = WebEmbeddedWorkerStartData::DontWaitFo
rDebugger; |
| 52 m_startData.v8CacheOptions = WebSettings::V8CacheOptionsDefault; | 53 m_startData.v8CacheOptions = WebSettings::V8CacheOptionsDefault; |
| 53 } | 54 } |
| 54 | 55 |
| 55 void TearDown() override | 56 void TearDown() override |
| 56 { | 57 { |
| 57 Platform::current()->unitTestSupport()->unregisterAllMockedURLs(); | 58 Platform::current()->getURLLoaderMockFactory()->unregisterAllURLs(); |
| 59 WebCache::clear(); |
| 58 } | 60 } |
| 59 | 61 |
| 60 WebEmbeddedWorkerStartData m_startData; | 62 WebEmbeddedWorkerStartData m_startData; |
| 61 MockServiceWorkerContextClient* m_mockClient; | 63 MockServiceWorkerContextClient* m_mockClient; |
| 62 OwnPtr<WebEmbeddedWorker> m_worker; | 64 OwnPtr<WebEmbeddedWorker> m_worker; |
| 63 }; | 65 }; |
| 64 | 66 |
| 65 } // namespace | 67 } // namespace |
| 66 | 68 |
| 67 TEST_F(WebEmbeddedWorkerImplTest, TerminateSoonAfterStart) | 69 TEST_F(WebEmbeddedWorkerImplTest, TerminateSoonAfterStart) |
| (...skipping 20 matching lines...) Expand all Loading... |
| 88 } | 90 } |
| 89 | 91 |
| 90 TEST_F(WebEmbeddedWorkerImplTest, TerminateWhileLoadingScript) | 92 TEST_F(WebEmbeddedWorkerImplTest, TerminateWhileLoadingScript) |
| 91 { | 93 { |
| 92 EXPECT_CALL(*m_mockClient, workerReadyForInspection()).Times(1); | 94 EXPECT_CALL(*m_mockClient, workerReadyForInspection()).Times(1); |
| 93 m_worker->startWorkerContext(m_startData); | 95 m_worker->startWorkerContext(m_startData); |
| 94 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); | 96 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); |
| 95 | 97 |
| 96 // Load the shadow page. | 98 // Load the shadow page. |
| 97 EXPECT_CALL(*m_mockClient, createServiceWorkerNetworkProvider(::testing::_))
.WillOnce(::testing::Return(nullptr)); | 99 EXPECT_CALL(*m_mockClient, createServiceWorkerNetworkProvider(::testing::_))
.WillOnce(::testing::Return(nullptr)); |
| 98 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests(); | 100 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 99 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); | 101 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); |
| 100 | 102 |
| 101 // Terminate before loading the script. | 103 // Terminate before loading the script. |
| 102 EXPECT_CALL(*m_mockClient, workerContextFailedToStart()).Times(1); | 104 EXPECT_CALL(*m_mockClient, workerContextFailedToStart()).Times(1); |
| 103 m_worker->terminateWorkerContext(); | 105 m_worker->terminateWorkerContext(); |
| 104 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); | 106 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); |
| 105 } | 107 } |
| 106 | 108 |
| 107 TEST_F(WebEmbeddedWorkerImplTest, TerminateWhilePausedAfterDownload) | 109 TEST_F(WebEmbeddedWorkerImplTest, TerminateWhilePausedAfterDownload) |
| 108 { | 110 { |
| 109 EXPECT_CALL(*m_mockClient, workerReadyForInspection()) | 111 EXPECT_CALL(*m_mockClient, workerReadyForInspection()) |
| 110 .Times(1); | 112 .Times(1); |
| 111 m_startData.pauseAfterDownloadMode = WebEmbeddedWorkerStartData::PauseAfterD
ownload; | 113 m_startData.pauseAfterDownloadMode = WebEmbeddedWorkerStartData::PauseAfterD
ownload; |
| 112 m_worker->startWorkerContext(m_startData); | 114 m_worker->startWorkerContext(m_startData); |
| 113 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); | 115 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); |
| 114 | 116 |
| 115 // Load the shadow page. | 117 // Load the shadow page. |
| 116 EXPECT_CALL(*m_mockClient, createServiceWorkerNetworkProvider(::testing::_)) | 118 EXPECT_CALL(*m_mockClient, createServiceWorkerNetworkProvider(::testing::_)) |
| 117 .WillOnce(::testing::Return(nullptr)); | 119 .WillOnce(::testing::Return(nullptr)); |
| 118 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests(); | 120 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 119 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); | 121 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); |
| 120 | 122 |
| 121 // Load the script. | 123 // Load the script. |
| 122 EXPECT_CALL(*m_mockClient, workerScriptLoaded()) | 124 EXPECT_CALL(*m_mockClient, workerScriptLoaded()) |
| 123 .Times(1); | 125 .Times(1); |
| 124 EXPECT_CALL(*m_mockClient, createServiceWorkerProvider()) | 126 EXPECT_CALL(*m_mockClient, createServiceWorkerProvider()) |
| 125 .Times(0); | 127 .Times(0); |
| 126 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests(); | 128 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 127 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); | 129 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); |
| 128 | 130 |
| 129 // Terminate before resuming after download. | 131 // Terminate before resuming after download. |
| 130 EXPECT_CALL(*m_mockClient, createServiceWorkerProvider()) | 132 EXPECT_CALL(*m_mockClient, createServiceWorkerProvider()) |
| 131 .Times(0); | 133 .Times(0); |
| 132 EXPECT_CALL(*m_mockClient, workerContextFailedToStart()) | 134 EXPECT_CALL(*m_mockClient, workerContextFailedToStart()) |
| 133 .Times(1); | 135 .Times(1); |
| 134 m_worker->terminateWorkerContext(); | 136 m_worker->terminateWorkerContext(); |
| 135 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); | 137 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); |
| 136 } | 138 } |
| 137 | 139 |
| 138 TEST_F(WebEmbeddedWorkerImplTest, ScriptNotFound) | 140 TEST_F(WebEmbeddedWorkerImplTest, ScriptNotFound) |
| 139 { | 141 { |
| 140 WebURL scriptURL = URLTestHelpers::toKURL("https://www.example.com/sw-404.js
"); | 142 WebURL scriptURL = URLTestHelpers::toKURL("https://www.example.com/sw-404.js
"); |
| 141 WebURLResponse response; | 143 WebURLResponse response; |
| 142 response.initialize(); | 144 response.initialize(); |
| 143 response.setMIMEType("text/javascript"); | 145 response.setMIMEType("text/javascript"); |
| 144 response.setHTTPStatusCode(404); | 146 response.setHTTPStatusCode(404); |
| 145 WebURLError error; | 147 WebURLError error; |
| 146 error.reason = 1010; | 148 error.reason = 1010; |
| 147 error.domain = "WebEmbeddedWorkerImplTest"; | 149 error.domain = "WebEmbeddedWorkerImplTest"; |
| 148 Platform::current()->unitTestSupport()->registerMockedErrorURL(scriptURL, re
sponse, error); | 150 Platform::current()->getURLLoaderMockFactory()->registerErrorURL(scriptURL,
response, error); |
| 149 m_startData.scriptURL = scriptURL; | 151 m_startData.scriptURL = scriptURL; |
| 150 | 152 |
| 151 EXPECT_CALL(*m_mockClient, workerReadyForInspection()) | 153 EXPECT_CALL(*m_mockClient, workerReadyForInspection()) |
| 152 .Times(1); | 154 .Times(1); |
| 153 m_worker->startWorkerContext(m_startData); | 155 m_worker->startWorkerContext(m_startData); |
| 154 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); | 156 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); |
| 155 | 157 |
| 156 // Load the shadow page. | 158 // Load the shadow page. |
| 157 EXPECT_CALL(*m_mockClient, createServiceWorkerNetworkProvider(::testing::_)) | 159 EXPECT_CALL(*m_mockClient, createServiceWorkerNetworkProvider(::testing::_)) |
| 158 .WillOnce(::testing::Return(nullptr)); | 160 .WillOnce(::testing::Return(nullptr)); |
| 159 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests(); | 161 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 160 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); | 162 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); |
| 161 | 163 |
| 162 // Load the script. | 164 // Load the script. |
| 163 EXPECT_CALL(*m_mockClient, workerScriptLoaded()) | 165 EXPECT_CALL(*m_mockClient, workerScriptLoaded()) |
| 164 .Times(0); | 166 .Times(0); |
| 165 EXPECT_CALL(*m_mockClient, createServiceWorkerProvider()) | 167 EXPECT_CALL(*m_mockClient, createServiceWorkerProvider()) |
| 166 .Times(0); | 168 .Times(0); |
| 167 EXPECT_CALL(*m_mockClient, workerContextFailedToStart()) | 169 EXPECT_CALL(*m_mockClient, workerContextFailedToStart()) |
| 168 .Times(1); | 170 .Times(1); |
| 169 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests(); | 171 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 170 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); | 172 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); |
| 171 } | 173 } |
| 172 | 174 |
| 173 // The running worker is detected as a memory leak. crbug.com/586897 | 175 // The running worker is detected as a memory leak. crbug.com/586897 |
| 174 #if defined(ADDRESS_SANITIZER) | 176 #if defined(ADDRESS_SANITIZER) |
| 175 #define MAYBE_DontPauseAfterDownload DISABLED_DontPauseAfterDownload | 177 #define MAYBE_DontPauseAfterDownload DISABLED_DontPauseAfterDownload |
| 176 #else | 178 #else |
| 177 #define MAYBE_DontPauseAfterDownload DontPauseAfterDownload | 179 #define MAYBE_DontPauseAfterDownload DontPauseAfterDownload |
| 178 #endif | 180 #endif |
| 179 | 181 |
| 180 TEST_F(WebEmbeddedWorkerImplTest, MAYBE_DontPauseAfterDownload) | 182 TEST_F(WebEmbeddedWorkerImplTest, MAYBE_DontPauseAfterDownload) |
| 181 { | 183 { |
| 182 EXPECT_CALL(*m_mockClient, workerReadyForInspection()) | 184 EXPECT_CALL(*m_mockClient, workerReadyForInspection()) |
| 183 .Times(1); | 185 .Times(1); |
| 184 m_worker->startWorkerContext(m_startData); | 186 m_worker->startWorkerContext(m_startData); |
| 185 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); | 187 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); |
| 186 | 188 |
| 187 // Load the shadow page. | 189 // Load the shadow page. |
| 188 EXPECT_CALL(*m_mockClient, createServiceWorkerNetworkProvider(::testing::_)) | 190 EXPECT_CALL(*m_mockClient, createServiceWorkerNetworkProvider(::testing::_)) |
| 189 .WillOnce(::testing::Return(nullptr)); | 191 .WillOnce(::testing::Return(nullptr)); |
| 190 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests(); | 192 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 191 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); | 193 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); |
| 192 | 194 |
| 193 // Load the script. | 195 // Load the script. |
| 194 EXPECT_CALL(*m_mockClient, workerScriptLoaded()) | 196 EXPECT_CALL(*m_mockClient, workerScriptLoaded()) |
| 195 .Times(1); | 197 .Times(1); |
| 196 EXPECT_CALL(*m_mockClient, createServiceWorkerProvider()) | 198 EXPECT_CALL(*m_mockClient, createServiceWorkerProvider()) |
| 197 .WillOnce(::testing::Return(nullptr)); | 199 .WillOnce(::testing::Return(nullptr)); |
| 198 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests(); | 200 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 199 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); | 201 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); |
| 200 } | 202 } |
| 201 | 203 |
| 202 // The running worker is detected as a memory leak. crbug.com/586897 | 204 // The running worker is detected as a memory leak. crbug.com/586897 |
| 203 #if defined(ADDRESS_SANITIZER) | 205 #if defined(ADDRESS_SANITIZER) |
| 204 #define MAYBE_PauseAfterDownload DISABLED_PauseAfterDownload | 206 #define MAYBE_PauseAfterDownload DISABLED_PauseAfterDownload |
| 205 #else | 207 #else |
| 206 #define MAYBE_PauseAfterDownload PauseAfterDownload | 208 #define MAYBE_PauseAfterDownload PauseAfterDownload |
| 207 #endif | 209 #endif |
| 208 | 210 |
| 209 TEST_F(WebEmbeddedWorkerImplTest, MAYBE_PauseAfterDownload) | 211 TEST_F(WebEmbeddedWorkerImplTest, MAYBE_PauseAfterDownload) |
| 210 { | 212 { |
| 211 EXPECT_CALL(*m_mockClient, workerReadyForInspection()) | 213 EXPECT_CALL(*m_mockClient, workerReadyForInspection()) |
| 212 .Times(1); | 214 .Times(1); |
| 213 m_startData.pauseAfterDownloadMode = WebEmbeddedWorkerStartData::PauseAfterD
ownload; | 215 m_startData.pauseAfterDownloadMode = WebEmbeddedWorkerStartData::PauseAfterD
ownload; |
| 214 m_worker->startWorkerContext(m_startData); | 216 m_worker->startWorkerContext(m_startData); |
| 215 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); | 217 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); |
| 216 | 218 |
| 217 // Load the shadow page. | 219 // Load the shadow page. |
| 218 EXPECT_CALL(*m_mockClient, createServiceWorkerNetworkProvider(::testing::_)) | 220 EXPECT_CALL(*m_mockClient, createServiceWorkerNetworkProvider(::testing::_)) |
| 219 .WillOnce(::testing::Return(nullptr)); | 221 .WillOnce(::testing::Return(nullptr)); |
| 220 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests(); | 222 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 221 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); | 223 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); |
| 222 | 224 |
| 223 // Load the script. | 225 // Load the script. |
| 224 EXPECT_CALL(*m_mockClient, workerScriptLoaded()) | 226 EXPECT_CALL(*m_mockClient, workerScriptLoaded()) |
| 225 .Times(1); | 227 .Times(1); |
| 226 EXPECT_CALL(*m_mockClient, createServiceWorkerProvider()) | 228 EXPECT_CALL(*m_mockClient, createServiceWorkerProvider()) |
| 227 .Times(0); | 229 .Times(0); |
| 228 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests(); | 230 Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
| 229 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); | 231 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); |
| 230 | 232 |
| 231 // Resume after download. | 233 // Resume after download. |
| 232 EXPECT_CALL(*m_mockClient, createServiceWorkerProvider()) | 234 EXPECT_CALL(*m_mockClient, createServiceWorkerProvider()) |
| 233 .WillOnce(::testing::Return(nullptr)); | 235 .WillOnce(::testing::Return(nullptr)); |
| 234 m_worker->resumeAfterDownload(); | 236 m_worker->resumeAfterDownload(); |
| 235 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); | 237 ::testing::Mock::VerifyAndClearExpectations(m_mockClient); |
| 236 } | 238 } |
| 237 | 239 |
| 238 } // namespace blink | 240 } // namespace blink |
| OLD | NEW |