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

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

Issue 945413005: Add browser test for V8 code caching for ServiceWorker scripts. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: incorporated kinuko's comment Created 5 years, 10 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
« no previous file with comments | « no previous file | content/browser/service_worker/service_worker_version.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "base/bind.h" 5 #include "base/bind.h"
6 #include "base/callback.h" 6 #include "base/callback.h"
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "content/browser/fileapi/chrome_blob_storage_context.h" 10 #include "content/browser/fileapi/chrome_blob_storage_context.h"
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 268
269 ServiceWorkerVersion* version = 269 ServiceWorkerVersion* version =
270 wrapper->context()->GetLiveVersion(version_id); 270 wrapper->context()->GetLiveVersion(version_id);
271 *num_resources = static_cast<int>(version->script_cache_map()->size()); 271 *num_resources = static_cast<int>(version->script_cache_map()->size());
272 } 272 }
273 273
274 } // namespace 274 } // namespace
275 275
276 class ServiceWorkerBrowserTest : public ContentBrowserTest { 276 class ServiceWorkerBrowserTest : public ContentBrowserTest {
277 protected: 277 protected:
278 typedef ServiceWorkerBrowserTest self; 278 using self = ServiceWorkerBrowserTest;
279 279
280 void SetUpCommandLine(base::CommandLine* command_line) override { 280 void SetUpCommandLine(base::CommandLine* command_line) override {
281 command_line->AppendSwitch( 281 command_line->AppendSwitch(
282 switches::kEnableExperimentalWebPlatformFeatures); 282 switches::kEnableExperimentalWebPlatformFeatures);
283 } 283 }
284 284
285 void SetUpOnMainThread() override { 285 void SetUpOnMainThread() override {
286 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); 286 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
287 StoragePartition* partition = BrowserContext::GetDefaultStoragePartition( 287 StoragePartition* partition = BrowserContext::GetDefaultStoragePartition(
288 shell()->web_contents()->GetBrowserContext()); 288 shell()->web_contents()->GetBrowserContext());
(...skipping 25 matching lines...) Expand all
314 pattern, shell()->web_contents()->GetRenderProcessHost()->GetID()); 314 pattern, shell()->web_contents()->GetRenderProcessHost()->GetID());
315 } 315 }
316 316
317 private: 317 private:
318 scoped_refptr<ServiceWorkerContextWrapper> wrapper_; 318 scoped_refptr<ServiceWorkerContextWrapper> wrapper_;
319 }; 319 };
320 320
321 class EmbeddedWorkerBrowserTest : public ServiceWorkerBrowserTest, 321 class EmbeddedWorkerBrowserTest : public ServiceWorkerBrowserTest,
322 public EmbeddedWorkerInstance::Listener { 322 public EmbeddedWorkerInstance::Listener {
323 public: 323 public:
324 typedef EmbeddedWorkerBrowserTest self; 324 using self = EmbeddedWorkerBrowserTest;
325 325
326 EmbeddedWorkerBrowserTest() 326 EmbeddedWorkerBrowserTest()
327 : last_worker_status_(EmbeddedWorkerInstance::STOPPED), 327 : last_worker_status_(EmbeddedWorkerInstance::STOPPED),
328 pause_mode_(DONT_PAUSE) {} 328 pause_mode_(DONT_PAUSE) {}
329 ~EmbeddedWorkerBrowserTest() override {} 329 ~EmbeddedWorkerBrowserTest() override {}
330 330
331 void TearDownOnIOThread() override { 331 void TearDownOnIOThread() override {
332 if (worker_) { 332 if (worker_) {
333 worker_->RemoveListener(this); 333 worker_->RemoveListener(this);
334 worker_.reset(); 334 worker_.reset();
335 } 335 }
336 } 336 }
337 337
338 void StartOnIOThread() { 338 void StartOnIOThread() {
339 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); 339 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
340 worker_ = wrapper()->context()->embedded_worker_registry()->CreateWorker(); 340 worker_ = wrapper()->context()->embedded_worker_registry()->CreateWorker();
341 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker_->status()); 341 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker_->status());
342 worker_->AddListener(this); 342 worker_->AddListener(this);
343 343
344
345 const int64 service_worker_version_id = 33L; 344 const int64 service_worker_version_id = 33L;
346 const GURL pattern = embedded_test_server()->GetURL("/"); 345 const GURL pattern = embedded_test_server()->GetURL("/");
347 const GURL script_url = embedded_test_server()->GetURL( 346 const GURL script_url = embedded_test_server()->GetURL(
348 "/service_worker/worker.js"); 347 "/service_worker/worker.js");
349 AssociateRendererProcessToPattern(pattern); 348 AssociateRendererProcessToPattern(pattern);
350 int process_id = shell()->web_contents()->GetRenderProcessHost()->GetID(); 349 int process_id = shell()->web_contents()->GetRenderProcessHost()->GetID();
351 wrapper()->process_manager()->AddProcessReferenceToPattern( 350 wrapper()->process_manager()->AddProcessReferenceToPattern(
352 pattern, process_id); 351 pattern, process_id);
353 worker_->Start( 352 worker_->Start(
354 service_worker_version_id, 353 service_worker_version_id,
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 PAUSE_THEN_STOP, 421 PAUSE_THEN_STOP,
423 } pause_mode_; 422 } pause_mode_;
424 423
425 // Called by EmbeddedWorkerInstance::Observer overrides so that 424 // Called by EmbeddedWorkerInstance::Observer overrides so that
426 // test code can wait for the worker status notifications. 425 // test code can wait for the worker status notifications.
427 base::Closure done_closure_; 426 base::Closure done_closure_;
428 }; 427 };
429 428
430 class ServiceWorkerVersionBrowserTest : public ServiceWorkerBrowserTest { 429 class ServiceWorkerVersionBrowserTest : public ServiceWorkerBrowserTest {
431 public: 430 public:
432 typedef ServiceWorkerVersionBrowserTest self; 431 using self = ServiceWorkerVersionBrowserTest;
433 432
434 ~ServiceWorkerVersionBrowserTest() override {} 433 ~ServiceWorkerVersionBrowserTest() override {}
435 434
436 void TearDownOnIOThread() override { 435 void TearDownOnIOThread() override {
437 registration_ = NULL; 436 registration_ = NULL;
438 version_ = NULL; 437 version_ = NULL;
439 } 438 }
440 439
441 void InstallTestHelper(const std::string& worker_url, 440 void InstallTestHelper(const std::string& worker_url,
442 ServiceWorkerStatusCode expected_status) { 441 ServiceWorkerStatusCode expected_status) {
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
527 wrapper()->context()->AsWeakPtr()); 526 wrapper()->context()->AsWeakPtr());
528 AssociateRendererProcessToPattern(pattern); 527 AssociateRendererProcessToPattern(pattern);
529 } 528 }
530 529
531 void TimeoutWorkerOnIOThread() { 530 void TimeoutWorkerOnIOThread() {
532 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); 531 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
533 version_->PingWorker(); 532 version_->PingWorker();
534 version_->OnPingTimeout(); 533 version_->OnPingTimeout();
535 } 534 }
536 535
536 void StartWorker(ServiceWorkerStatusCode expected_status) {
537 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI));
538 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
539 base::RunLoop start_run_loop;
540 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
541 base::Bind(&self::StartOnIOThread, this,
542 start_run_loop.QuitClosure(),
543 &status));
544 start_run_loop.Run();
545 ASSERT_EQ(expected_status, status);
546 }
547
548 void StopWorker(ServiceWorkerStatusCode expected_status) {
549 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI));
550 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
551 base::RunLoop stop_run_loop;
552 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
553 base::Bind(&self::StopOnIOThread, this,
554 stop_run_loop.QuitClosure(),
555 &status));
556 stop_run_loop.Run();
557 ASSERT_EQ(expected_status, status);
558 }
559
537 void StartOnIOThread(const base::Closure& done, 560 void StartOnIOThread(const base::Closure& done,
538 ServiceWorkerStatusCode* result) { 561 ServiceWorkerStatusCode* result) {
539 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); 562 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
540 version_->StartWorker(CreateReceiver(BrowserThread::UI, done, result)); 563 version_->StartWorker(CreateReceiver(BrowserThread::UI, done, result));
541 } 564 }
542 565
543 void InstallOnIOThread(const base::Closure& done, 566 void InstallOnIOThread(const base::Closure& done,
544 ServiceWorkerStatusCode* result) { 567 ServiceWorkerStatusCode* result) {
545 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); 568 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
546 version_->SetStatus(ServiceWorkerVersion::INSTALLING); 569 version_->SetStatus(ServiceWorkerVersion::INSTALLING);
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
658 &status)); 681 &status));
659 stop_run_loop.Run(); 682 stop_run_loop.Run();
660 ASSERT_EQ(SERVICE_WORKER_OK, status); 683 ASSERT_EQ(SERVICE_WORKER_OK, status);
661 } 684 }
662 685
663 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, StartNotFound) { 686 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, StartNotFound) {
664 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this, 687 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this,
665 "/service_worker/nonexistent.js")); 688 "/service_worker/nonexistent.js"));
666 689
667 // Start a worker for nonexistent URL. 690 // Start a worker for nonexistent URL.
668 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; 691 StartWorker(SERVICE_WORKER_ERROR_START_WORKER_FAILED);
669 base::RunLoop start_run_loop;
670 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
671 base::Bind(&self::StartOnIOThread, this,
672 start_run_loop.QuitClosure(),
673 &status));
674 start_run_loop.Run();
675 ASSERT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status);
676 } 692 }
677 693
678 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, Install) { 694 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, Install) {
679 InstallTestHelper("/service_worker/worker.js", SERVICE_WORKER_OK); 695 InstallTestHelper("/service_worker/worker.js", SERVICE_WORKER_OK);
680 } 696 }
681 697
682 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, 698 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest,
683 InstallWithWaitUntil_Fulfilled) { 699 InstallWithWaitUntil_Fulfilled) {
684 InstallTestHelper("/service_worker/worker_install_fulfilled.js", 700 InstallTestHelper("/service_worker/worker_install_fulfilled.js",
685 SERVICE_WORKER_OK); 701 SERVICE_WORKER_OK);
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after
977 RunOnIOThread( 993 RunOnIOThread(
978 base::Bind(&CountScriptResources, 994 base::Bind(&CountScriptResources,
979 base::Unretained(wrapper()), 995 base::Unretained(wrapper()),
980 embedded_test_server()->GetURL(kScopeUrl), 996 embedded_test_server()->GetURL(kScopeUrl),
981 &num_resources)); 997 &num_resources));
982 EXPECT_EQ(kExpectedNumResources, num_resources); 998 EXPECT_EQ(kExpectedNumResources, num_resources);
983 } 999 }
984 1000
985 class ServiceWorkerBlackBoxBrowserTest : public ServiceWorkerBrowserTest { 1001 class ServiceWorkerBlackBoxBrowserTest : public ServiceWorkerBrowserTest {
986 public: 1002 public:
987 typedef ServiceWorkerBlackBoxBrowserTest self; 1003 using self = ServiceWorkerBlackBoxBrowserTest;
988 1004
989 void FindRegistrationOnIO(const GURL& document_url, 1005 void FindRegistrationOnIO(const GURL& document_url,
990 ServiceWorkerStatusCode* status, 1006 ServiceWorkerStatusCode* status,
991 const base::Closure& continuation) { 1007 const base::Closure& continuation) {
992 wrapper()->context()->storage()->FindRegistrationForDocument( 1008 wrapper()->context()->storage()->FindRegistrationForDocument(
993 document_url, 1009 document_url,
994 base::Bind(&ServiceWorkerBlackBoxBrowserTest::FindRegistrationOnIO2, 1010 base::Bind(&ServiceWorkerBlackBoxBrowserTest::FindRegistrationOnIO2,
995 this, 1011 this,
996 status, 1012 status,
997 continuation)); 1013 continuation));
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
1123 "cross_site_xfer_confirm_via_serviceworker.html"; 1139 "cross_site_xfer_confirm_via_serviceworker.html";
1124 const base::string16 kOKTitle2(base::ASCIIToUTF16("OK_2")); 1140 const base::string16 kOKTitle2(base::ASCIIToUTF16("OK_2"));
1125 const base::string16 kFailTitle2(base::ASCIIToUTF16("FAIL_2")); 1141 const base::string16 kFailTitle2(base::ASCIIToUTF16("FAIL_2"));
1126 content::TitleWatcher title_watcher2(shell()->web_contents(), kOKTitle2); 1142 content::TitleWatcher title_watcher2(shell()->web_contents(), kOKTitle2);
1127 title_watcher2.AlsoWaitForTitle(kFailTitle2); 1143 title_watcher2.AlsoWaitForTitle(kFailTitle2);
1128 1144
1129 NavigateToURL(shell(), embedded_test_server()->GetURL(kConfirmPageUrl)); 1145 NavigateToURL(shell(), embedded_test_server()->GetURL(kConfirmPageUrl));
1130 EXPECT_EQ(kOKTitle2, title_watcher2.WaitAndGetTitle()); 1146 EXPECT_EQ(kOKTitle2, title_watcher2.WaitAndGetTitle());
1131 } 1147 }
1132 1148
1149 class ServiceWorkerVersionBrowserV8CacheTest
1150 : public ServiceWorkerVersionBrowserTest,
1151 public ServiceWorkerVersion::Listener {
1152 public:
1153 using self = ServiceWorkerVersionBrowserV8CacheTest;
1154 ~ServiceWorkerVersionBrowserV8CacheTest() override {
1155 if (version_)
1156 version_->RemoveListener(this);
1157 }
1158 void SetUpCommandLine(base::CommandLine* command_line) override {
1159 ServiceWorkerBrowserTest::SetUpCommandLine(command_line);
1160 command_line->AppendSwitchASCII(switches::kV8CacheOptions, "code");
1161 }
1162 void SetUpRegistrationAndListenerOnIOThread(const std::string& worker_url) {
1163 SetUpRegistrationOnIOThread(worker_url);
1164 version_->AddListener(this);
1165 }
1166
1167 protected:
1168 // ServiceWorkerVersion::Listener overrides
1169 void OnCachedMetadataUpdated(ServiceWorkerVersion* version) override {
1170 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
1171 cache_updated_closure_);
1172 }
1173
1174 base::Closure cache_updated_closure_;
1175 };
1176
1177 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserV8CacheTest, Restart) {
1178 RunOnIOThread(base::Bind(&self::SetUpRegistrationAndListenerOnIOThread, this,
1179 "/service_worker/worker.js"));
1180
1181 base::RunLoop cached_metadata_run_loop;
1182 cache_updated_closure_ = cached_metadata_run_loop.QuitClosure();
1183
1184 // Start a worker.
1185 StartWorker(SERVICE_WORKER_OK);
1186
1187 // Wait for the matadata is stored. This run loop should finish when
1188 // OnCachedMetadataUpdated() is called.
1189 cached_metadata_run_loop.Run();
1190
1191 // Activate the worker.
1192 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
1193 base::RunLoop acrivate_run_loop;
1194 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
1195 base::Bind(&self::ActivateOnIOThread, this,
1196 acrivate_run_loop.QuitClosure(), &status));
1197 acrivate_run_loop.Run();
1198 ASSERT_EQ(SERVICE_WORKER_OK, status);
1199 // Stop the worker.
1200 StopWorker(SERVICE_WORKER_OK);
1201 // Restart the worker.
1202 StartWorker(SERVICE_WORKER_OK);
1203 // Stop the worker.
1204 StopWorker(SERVICE_WORKER_OK);
1205 }
1206
1133 } // namespace content 1207 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/browser/service_worker/service_worker_version.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698