OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <stdint.h> | 5 #include <stdint.h> |
6 #include <tuple> | 6 #include <tuple> |
7 | 7 |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
111 switch (result) { | 111 switch (result) { |
112 case blink::kWebServiceWorkerEventResultCompleted: | 112 case blink::kWebServiceWorkerEventResultCompleted: |
113 return SERVICE_WORKER_OK; | 113 return SERVICE_WORKER_OK; |
114 case blink::kWebServiceWorkerEventResultRejected: | 114 case blink::kWebServiceWorkerEventResultRejected: |
115 return SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED; | 115 return SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED; |
116 } | 116 } |
117 NOTREACHED() << "Got invalid result: " << result; | 117 NOTREACHED() << "Got invalid result: " << result; |
118 return SERVICE_WORKER_ERROR_FAILED; | 118 return SERVICE_WORKER_ERROR_FAILED; |
119 } | 119 } |
120 | 120 |
121 // This is for the test of mojom::ServiceWorkerInstallEventMethods. | |
122 void RegisterForeignFetchScopes( | |
123 mojom::ServiceWorkerInstallEventMethodsAssociatedPtrInfo client) { | |
124 GURL valid_scope_1("http://www.example.com/test/subscope"); | |
125 GURL valid_scope_2("http://www.example.com/test/othersubscope"); | |
126 std::vector<GURL> valid_scopes; | |
127 valid_scopes.push_back(valid_scope_1); | |
128 valid_scopes.push_back(valid_scope_2); | |
129 | |
130 std::vector<url::Origin> all_origins; | |
131 url::Origin valid_origin(GURL("https://chromium.org/")); | |
132 std::vector<url::Origin> valid_origin_list(1, valid_origin); | |
133 | |
134 mojom::ServiceWorkerInstallEventMethodsAssociatedPtr install_event_methods; | |
135 install_event_methods.Bind(std::move(client)); | |
136 install_event_methods->RegisterForeignFetchScopes(valid_scopes, | |
137 valid_origin_list); | |
138 } | |
139 | |
121 } // namespace | 140 } // namespace |
122 | 141 |
123 class ServiceWorkerJobTest : public testing::Test { | 142 class ServiceWorkerJobTest : public testing::Test { |
124 public: | 143 public: |
125 ServiceWorkerJobTest() | 144 ServiceWorkerJobTest() |
126 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 145 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
127 | 146 |
128 void SetUp() override { | 147 void SetUp() override { |
129 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); | 148 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); |
130 } | 149 } |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
281 storage()->FindRegistrationForDocument( | 300 storage()->FindRegistrationForDocument( |
282 GURL("http://www.example.com/two/"), | 301 GURL("http://www.example.com/two/"), |
283 SaveFoundRegistration(SERVICE_WORKER_OK, &called2, ®istration2)); | 302 SaveFoundRegistration(SERVICE_WORKER_OK, &called2, ®istration2)); |
284 | 303 |
285 base::RunLoop().RunUntilIdle(); | 304 base::RunLoop().RunUntilIdle(); |
286 EXPECT_TRUE(called2); | 305 EXPECT_TRUE(called2); |
287 EXPECT_TRUE(called1); | 306 EXPECT_TRUE(called1); |
288 ASSERT_NE(registration1, registration2); | 307 ASSERT_NE(registration1, registration2); |
289 } | 308 } |
290 | 309 |
310 class RegisterForeignFetchTestHelper : public EmbeddedWorkerTestHelper { | |
311 public: | |
312 RegisterForeignFetchTestHelper() | |
313 : EmbeddedWorkerTestHelper(base::FilePath()) {} | |
314 | |
315 void OnInstallEvent( | |
316 mojom::ServiceWorkerInstallEventMethodsAssociatedPtrInfo client, | |
317 mojom::ServiceWorkerEventDispatcher::DispatchInstallEventCallback | |
318 callback) override { | |
319 RegisterForeignFetchScopes(std::move(client)); | |
320 dispatched_events()->push_back(Event::Install); | |
321 std::move(callback).Run(SERVICE_WORKER_OK, true /* has_fetch_handler */, | |
322 base::Time::Now()); | |
323 } | |
324 }; | |
325 | |
291 // Make sure basic registration is working. | 326 // Make sure basic registration is working. |
292 TEST_F(ServiceWorkerJobTest, Register) { | 327 TEST_F(ServiceWorkerJobTest, Register) { |
328 helper_.reset(new RegisterForeignFetchTestHelper); | |
329 | |
293 scoped_refptr<ServiceWorkerRegistration> registration = | 330 scoped_refptr<ServiceWorkerRegistration> registration = |
294 RunRegisterJob(GURL("http://www.example.com/"), | 331 RunRegisterJob(GURL("http://www.example.com/"), |
295 GURL("http://www.example.com/service_worker.js")); | 332 GURL("http://www.example.com/service_worker.js")); |
296 | 333 |
297 ASSERT_NE(scoped_refptr<ServiceWorkerRegistration>(NULL), registration); | 334 ASSERT_NE(scoped_refptr<ServiceWorkerRegistration>(NULL), registration); |
298 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching( | 335 EXPECT_EQ(EmbeddedWorkerTestHelper::Event::Install, |
299 ServiceWorkerMsg_InstallEvent::ID)); | 336 helper_->dispatched_events()->at(0)); |
337 EXPECT_EQ(EmbeddedWorkerTestHelper::Event::Activate, | |
338 helper_->dispatched_events()->at(1)); | |
339 | |
340 // Must be the same with the defination in embedded_worker_test_helper.cc. | |
xiaofengzhang
2017/04/28 08:20:17
This line will be deleted
| |
341 GURL valid_scope_1("http://www.example.com/test/subscope"); | |
342 GURL valid_scope_2("http://www.example.com/test/othersubscope"); | |
343 url::Origin valid_origin(GURL("https://chromium.org/")); | |
344 | |
345 ServiceWorkerVersion* version_ = registration->active_version(); | |
346 EXPECT_EQ(2u, version_->foreign_fetch_scopes_.size()); | |
347 EXPECT_EQ(valid_scope_1, version_->foreign_fetch_scopes_[0]); | |
348 EXPECT_EQ(valid_scope_2, version_->foreign_fetch_scopes_[1]); | |
349 EXPECT_EQ(1u, version_->foreign_fetch_origins_.size()); | |
350 EXPECT_EQ(valid_origin, version_->foreign_fetch_origins_[0]); | |
300 } | 351 } |
301 | 352 |
302 // Make sure registrations are cleaned up when they are unregistered. | 353 // Make sure registrations are cleaned up when they are unregistered. |
303 TEST_F(ServiceWorkerJobTest, Unregister) { | 354 TEST_F(ServiceWorkerJobTest, Unregister) { |
304 GURL pattern("http://www.example.com/"); | 355 GURL pattern("http://www.example.com/"); |
305 | 356 |
306 scoped_refptr<ServiceWorkerRegistration> registration = | 357 scoped_refptr<ServiceWorkerRegistration> registration = |
307 RunRegisterJob(pattern, GURL("http://www.example.com/service_worker.js")); | 358 RunRegisterJob(pattern, GURL("http://www.example.com/service_worker.js")); |
308 | 359 |
309 RunUnregisterJob(pattern); | 360 RunUnregisterJob(pattern); |
(...skipping 1154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1464 EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, third_version->status()); | 1515 EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, third_version->status()); |
1465 } | 1516 } |
1466 | 1517 |
1467 class EventCallbackHelper : public EmbeddedWorkerTestHelper { | 1518 class EventCallbackHelper : public EmbeddedWorkerTestHelper { |
1468 public: | 1519 public: |
1469 EventCallbackHelper() | 1520 EventCallbackHelper() |
1470 : EmbeddedWorkerTestHelper(base::FilePath()), | 1521 : EmbeddedWorkerTestHelper(base::FilePath()), |
1471 install_event_result_(blink::kWebServiceWorkerEventResultCompleted), | 1522 install_event_result_(blink::kWebServiceWorkerEventResultCompleted), |
1472 activate_event_result_(blink::kWebServiceWorkerEventResultCompleted) {} | 1523 activate_event_result_(blink::kWebServiceWorkerEventResultCompleted) {} |
1473 | 1524 |
1474 void OnInstallEvent(int embedded_worker_id, | 1525 void OnInstallEvent( |
1475 int request_id) override { | 1526 mojom::ServiceWorkerInstallEventMethodsAssociatedPtrInfo client, |
1527 mojom::ServiceWorkerEventDispatcher::DispatchInstallEventCallback | |
1528 callback) override { | |
1476 if (!install_callback_.is_null()) | 1529 if (!install_callback_.is_null()) |
1477 install_callback_.Run(); | 1530 install_callback_.Run(); |
1478 SimulateSend(new ServiceWorkerHostMsg_InstallEventFinished( | 1531 std::move(callback).Run(EventResultToStatus(install_event_result_), |
1479 embedded_worker_id, request_id, install_event_result_, | 1532 has_fetch_handler_, base::Time::Now()); |
1480 has_fetch_handler_, base::Time::Now())); | |
1481 } | 1533 } |
1482 | 1534 |
1483 void OnActivateEvent( | 1535 void OnActivateEvent( |
1484 mojom::ServiceWorkerEventDispatcher::DispatchActivateEventCallback | 1536 mojom::ServiceWorkerEventDispatcher::DispatchActivateEventCallback |
1485 callback) override { | 1537 callback) override { |
1486 std::move(callback).Run(EventResultToStatus(activate_event_result_), | 1538 std::move(callback).Run(EventResultToStatus(activate_event_result_), |
1487 base::Time::Now()); | 1539 base::Time::Now()); |
1488 } | 1540 } |
1489 | 1541 |
1490 void set_install_callback(const base::Closure& callback) { | 1542 void set_install_callback(const base::Closure& callback) { |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1749 // should not be promoted to ACTIVATED because failure occur | 1801 // should not be promoted to ACTIVATED because failure occur |
1750 // during shutdown. | 1802 // during shutdown. |
1751 runner->RunUntilIdle(); | 1803 runner->RunUntilIdle(); |
1752 base::RunLoop().RunUntilIdle(); | 1804 base::RunLoop().RunUntilIdle(); |
1753 EXPECT_EQ(new_version.get(), registration->active_version()); | 1805 EXPECT_EQ(new_version.get(), registration->active_version()); |
1754 EXPECT_EQ(ServiceWorkerVersion::ACTIVATING, new_version->status()); | 1806 EXPECT_EQ(ServiceWorkerVersion::ACTIVATING, new_version->status()); |
1755 registration->RemoveListener(update_helper); | 1807 registration->RemoveListener(update_helper); |
1756 } | 1808 } |
1757 | 1809 |
1758 } // namespace content | 1810 } // namespace content |
OLD | NEW |