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

Side by Side Diff: content/browser/service_worker/service_worker_job_unittest.cc

Issue 2746783002: [ServiceWorker] Mojofy InstallEvent of Service Worker (Closed)
Patch Set: Just delete a useless line 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 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
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
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, &registration2)); 302 SaveFoundRegistration(SERVICE_WORKER_OK, &called2, &registration2));
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 GURL valid_scope_1("http://www.example.com/test/subscope");
341 GURL valid_scope_2("http://www.example.com/test/othersubscope");
342 url::Origin valid_origin(GURL("https://chromium.org/"));
343
344 ServiceWorkerVersion* version_ = registration->active_version();
345 EXPECT_EQ(2u, version_->foreign_fetch_scopes_.size());
346 EXPECT_EQ(valid_scope_1, version_->foreign_fetch_scopes_[0]);
347 EXPECT_EQ(valid_scope_2, version_->foreign_fetch_scopes_[1]);
348 EXPECT_EQ(1u, version_->foreign_fetch_origins_.size());
349 EXPECT_EQ(valid_origin, version_->foreign_fetch_origins_[0]);
300 } 350 }
301 351
302 // Make sure registrations are cleaned up when they are unregistered. 352 // Make sure registrations are cleaned up when they are unregistered.
303 TEST_F(ServiceWorkerJobTest, Unregister) { 353 TEST_F(ServiceWorkerJobTest, Unregister) {
304 GURL pattern("http://www.example.com/"); 354 GURL pattern("http://www.example.com/");
305 355
306 scoped_refptr<ServiceWorkerRegistration> registration = 356 scoped_refptr<ServiceWorkerRegistration> registration =
307 RunRegisterJob(pattern, GURL("http://www.example.com/service_worker.js")); 357 RunRegisterJob(pattern, GURL("http://www.example.com/service_worker.js"));
308 358
309 RunUnregisterJob(pattern); 359 RunUnregisterJob(pattern);
(...skipping 1154 matching lines...) Expand 10 before | Expand all | Expand 10 after
1464 EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, third_version->status()); 1514 EXPECT_EQ(ServiceWorkerVersion::ACTIVATED, third_version->status());
1465 } 1515 }
1466 1516
1467 class EventCallbackHelper : public EmbeddedWorkerTestHelper { 1517 class EventCallbackHelper : public EmbeddedWorkerTestHelper {
1468 public: 1518 public:
1469 EventCallbackHelper() 1519 EventCallbackHelper()
1470 : EmbeddedWorkerTestHelper(base::FilePath()), 1520 : EmbeddedWorkerTestHelper(base::FilePath()),
1471 install_event_result_(blink::kWebServiceWorkerEventResultCompleted), 1521 install_event_result_(blink::kWebServiceWorkerEventResultCompleted),
1472 activate_event_result_(blink::kWebServiceWorkerEventResultCompleted) {} 1522 activate_event_result_(blink::kWebServiceWorkerEventResultCompleted) {}
1473 1523
1474 void OnInstallEvent(int embedded_worker_id, 1524 void OnInstallEvent(
1475 int request_id) override { 1525 mojom::ServiceWorkerInstallEventMethodsAssociatedPtrInfo client,
1526 mojom::ServiceWorkerEventDispatcher::DispatchInstallEventCallback
1527 callback) override {
1476 if (!install_callback_.is_null()) 1528 if (!install_callback_.is_null())
1477 install_callback_.Run(); 1529 install_callback_.Run();
1478 SimulateSend(new ServiceWorkerHostMsg_InstallEventFinished( 1530 std::move(callback).Run(EventResultToStatus(install_event_result_),
1479 embedded_worker_id, request_id, install_event_result_, 1531 has_fetch_handler_, base::Time::Now());
1480 has_fetch_handler_, base::Time::Now()));
1481 } 1532 }
1482 1533
1483 void OnActivateEvent( 1534 void OnActivateEvent(
1484 mojom::ServiceWorkerEventDispatcher::DispatchActivateEventCallback 1535 mojom::ServiceWorkerEventDispatcher::DispatchActivateEventCallback
1485 callback) override { 1536 callback) override {
1486 std::move(callback).Run(EventResultToStatus(activate_event_result_), 1537 std::move(callback).Run(EventResultToStatus(activate_event_result_),
1487 base::Time::Now()); 1538 base::Time::Now());
1488 } 1539 }
1489 1540
1490 void set_install_callback(const base::Closure& callback) { 1541 void set_install_callback(const base::Closure& callback) {
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
1749 // should not be promoted to ACTIVATED because failure occur 1800 // should not be promoted to ACTIVATED because failure occur
1750 // during shutdown. 1801 // during shutdown.
1751 runner->RunUntilIdle(); 1802 runner->RunUntilIdle();
1752 base::RunLoop().RunUntilIdle(); 1803 base::RunLoop().RunUntilIdle();
1753 EXPECT_EQ(new_version.get(), registration->active_version()); 1804 EXPECT_EQ(new_version.get(), registration->active_version());
1754 EXPECT_EQ(ServiceWorkerVersion::ACTIVATING, new_version->status()); 1805 EXPECT_EQ(ServiceWorkerVersion::ACTIVATING, new_version->status());
1755 registration->RemoveListener(update_helper); 1806 registration->RemoveListener(update_helper);
1756 } 1807 }
1757 1808
1758 } // namespace content 1809 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698