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

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

Issue 2204873002: ServiceWorker: Run functions in the browsertest on appropriate thread (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 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 | no next file » | 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 <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <map> 8 #include <map>
9 #include <memory> 9 #include <memory>
10 #include <mutex>
10 #include <utility> 11 #include <utility>
11 12
12 #include "base/bind.h" 13 #include "base/bind.h"
13 #include "base/callback.h" 14 #include "base/callback.h"
14 #include "base/command_line.h" 15 #include "base/command_line.h"
15 #include "base/macros.h" 16 #include "base/macros.h"
16 #include "base/memory/ptr_util.h" 17 #include "base/memory/ptr_util.h"
17 #include "base/memory/ref_counted.h" 18 #include "base/memory/ref_counted.h"
18 #include "base/run_loop.h" 19 #include "base/run_loop.h"
19 #include "base/single_thread_task_runner.h" 20 #include "base/single_thread_task_runner.h"
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 private: 376 private:
376 scoped_refptr<ServiceWorkerContextWrapper> wrapper_; 377 scoped_refptr<ServiceWorkerContextWrapper> wrapper_;
377 }; 378 };
378 379
379 class ConsoleListener : public EmbeddedWorkerInstance::Listener { 380 class ConsoleListener : public EmbeddedWorkerInstance::Listener {
380 public: 381 public:
381 void OnReportConsoleMessage(int source_identifier, 382 void OnReportConsoleMessage(int source_identifier,
382 int message_level, 383 int message_level,
383 const base::string16& message, 384 const base::string16& message,
384 int line_number, 385 int line_number,
385 const GURL& source_url) override { 386 const GURL& source_url) override {
nhiroki 2016/08/02 09:15:43 Can you add DCHECK_CURRENTLY_ON(BrowserThread::IO)
shimazu 2016/08/02 23:34:49 Done.
387 std::lock_guard<std::mutex> lock(mutex_);
386 messages_.push_back(message); 388 messages_.push_back(message);
387 if (!quit_.is_null() && messages_.size() == expected_message_count_) { 389 if (!quit_.is_null() && messages_.size() == expected_message_count_) {
388 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_); 390 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_);
389 quit_.Reset(); 391 quit_.Reset();
390 } 392 }
nhiroki 2016/08/02 09:15:43 How about running these operations on the UI threa
nhiroki 2016/08/02 09:16:32 BrowserThread::PostTask(BrowserThread::UI, FROM_HE
shimazu 2016/08/02 23:34:49 Done.
391 } 393 }
392 394
393 void WaitForConsoleMessages(size_t expected_message_count) { 395 void WaitForConsoleMessages(size_t expected_message_count) {
394 if (messages_.size() >= expected_message_count) 396 base::RunLoop console_run_loop;
395 return; 397 {
398 std::lock_guard<std::mutex> lock(mutex_);
399 if (messages_.size() >= expected_message_count)
400 return;
396 401
397 expected_message_count_ = expected_message_count; 402 expected_message_count_ = expected_message_count;
398 base::RunLoop console_run_loop; 403 quit_ = console_run_loop.QuitClosure();
399 quit_ = console_run_loop.QuitClosure(); 404 }
405
400 console_run_loop.Run(); 406 console_run_loop.Run();
401 407
402 ASSERT_EQ(messages_.size(), expected_message_count); 408 {
409 std::lock_guard<std::mutex> lock(mutex_);
410 ASSERT_EQ(messages_.size(), expected_message_count);
411 }
403 } 412 }
404 413
405 bool OnMessageReceived(const IPC::Message& message) override { return false; } 414 bool OnMessageReceived(const IPC::Message& message) override { return false; }
406 const std::vector<base::string16>& messages() const { return messages_; } 415 const std::vector<base::string16>& messages() const { return messages_; }
407 416
408 private: 417 private:
409 std::vector<base::string16> messages_; 418 std::vector<base::string16> messages_;
419 std::mutex mutex_;
410 size_t expected_message_count_; 420 size_t expected_message_count_;
411 base::Closure quit_; 421 base::Closure quit_;
nhiroki 2016/08/02 09:15:42 We may want to add a comment like "// These must b
shimazu 2016/08/02 23:34:49 Done.
412 }; 422 };
413 423
414 class ServiceWorkerVersionBrowserTest : public ServiceWorkerBrowserTest { 424 class ServiceWorkerVersionBrowserTest : public ServiceWorkerBrowserTest {
415 public: 425 public:
416 using self = ServiceWorkerVersionBrowserTest; 426 using self = ServiceWorkerVersionBrowserTest;
417 427
418 ~ServiceWorkerVersionBrowserTest() override {} 428 ~ServiceWorkerVersionBrowserTest() override {}
419 429
420 void TearDownOnIOThread() override { 430 void TearDownOnIOThread() override {
421 registration_ = NULL; 431 registration_ = NULL;
(...skipping 501 matching lines...) Expand 10 before | Expand all | Expand 10 after
923 ActivateTestHelper("/service_worker/worker_activate_rejected.js", 933 ActivateTestHelper("/service_worker/worker_activate_rejected.js",
924 SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED); 934 SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED);
925 } 935 }
926 936
927 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, 937 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest,
928 InstallWithWaitUntil_Rejected) { 938 InstallWithWaitUntil_Rejected) {
929 InstallTestHelper("/service_worker/worker_install_rejected.js", 939 InstallTestHelper("/service_worker/worker_install_rejected.js",
930 SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED); 940 SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED);
931 } 941 }
932 942
933 // Has errors under TSan. See https://crbug.com/631323.
934 #if defined(THREAD_SANITIZER)
935 #define MAYBE_InstallWithWaitUntil_RejectConsoleMessage \
936 DISABLED_InstallWithWaitUntil_RejectConsoleMessage
937 #else
938 #define MAYBE_InstallWithWaitUntil_RejectConsoleMessage \
939 InstallWithWaitUntil_RejectConsoleMessage
940 #endif
941 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, 943 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest,
942 MAYBE_InstallWithWaitUntil_RejectConsoleMessage) { 944 InstallWithWaitUntil_RejectConsoleMessage) {
943 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this, 945 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this,
944 "/service_worker/worker_install_rejected.js")); 946 "/service_worker/worker_install_rejected.js"));
945 947
946 ConsoleListener console_listener; 948 ConsoleListener console_listener;
947 version_->embedded_worker()->AddListener(&console_listener); 949 RunOnIOThread(base::Bind(&EmbeddedWorkerInstance::AddListener,
950 base::Unretained(version_->embedded_worker()),
951 &console_listener));
948 952
949 // Dispatch install on a worker. 953 // Dispatch install on a worker.
950 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; 954 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
951 base::RunLoop install_run_loop; 955 base::RunLoop install_run_loop;
952 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 956 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
953 base::Bind(&self::InstallOnIOThread, this, 957 base::Bind(&self::InstallOnIOThread, this,
954 install_run_loop.QuitClosure(), &status)); 958 install_run_loop.QuitClosure(), &status));
955 install_run_loop.Run(); 959 install_run_loop.Run();
956 ASSERT_EQ(SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED, status); 960 ASSERT_EQ(SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED, status);
957 961
958 const base::string16 expected = 962 const base::string16 expected =
959 base::ASCIIToUTF16("Rejecting oninstall event"); 963 base::ASCIIToUTF16("Rejecting oninstall event");
960 console_listener.WaitForConsoleMessages(1); 964 console_listener.WaitForConsoleMessages(1);
961 ASSERT_NE(base::string16::npos, 965 ASSERT_NE(base::string16::npos,
962 console_listener.messages()[0].find(expected)); 966 console_listener.messages()[0].find(expected));
963 version_->embedded_worker()->RemoveListener(&console_listener); 967 RunOnIOThread(base::Bind(&EmbeddedWorkerInstance::RemoveListener,
968 base::Unretained(version_->embedded_worker()),
969 &console_listener));
964 } 970 }
965 971
966 class WaitForLoaded : public EmbeddedWorkerInstance::Listener { 972 class WaitForLoaded : public EmbeddedWorkerInstance::Listener {
967 public: 973 public:
968 explicit WaitForLoaded(const base::Closure& quit) : quit_(quit) {} 974 explicit WaitForLoaded(const base::Closure& quit) : quit_(quit) {}
969 975
970 void OnThreadStarted() override { 976 void OnThreadStarted() override {
nhiroki 2016/08/02 09:15:43 DCHECK_CURRENTLY_ON(BrowserThread::IO)
shimazu 2016/08/02 23:34:49 Done.
971 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_); 977 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_);
972 } 978 }
973 bool OnMessageReceived(const IPC::Message& message) override { return false; } 979 bool OnMessageReceived(const IPC::Message& message) override { return false; }
974 980
975 private: 981 private:
976 base::Closure quit_; 982 base::Closure quit_;
977 }; 983 };
978 984
979 // Has errors under TSan. See https://crbug.com/631323. 985 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, TimeoutStartingWorker) {
980 #if defined(THREAD_SANITIZER)
981 #define MAYBE_TimeoutStartingWorker DISABLED_TimeoutStartingWorker
982 #else
983 #define MAYBE_TimeoutStartingWorker TimeoutStartingWorker
984 #endif
985 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest,
986 MAYBE_TimeoutStartingWorker) {
987 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this, 986 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this,
988 "/service_worker/while_true_worker.js")); 987 "/service_worker/while_true_worker.js"));
989 988
990 // Start a worker, waiting until the script is loaded. 989 // Start a worker, waiting until the script is loaded.
991 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; 990 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
992 base::RunLoop start_run_loop; 991 base::RunLoop start_run_loop;
993 base::RunLoop load_run_loop; 992 base::RunLoop load_run_loop;
994 WaitForLoaded wait_for_load(load_run_loop.QuitClosure()); 993 WaitForLoaded wait_for_load(load_run_loop.QuitClosure());
995 version_->embedded_worker()->AddListener(&wait_for_load); 994 RunOnIOThread(base::Bind(&EmbeddedWorkerInstance::AddListener,
995 base::Unretained(version_->embedded_worker()),
996 &wait_for_load));
996 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, 997 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
997 base::Bind(&self::StartOnIOThread, this, 998 base::Bind(&self::StartOnIOThread, this,
998 start_run_loop.QuitClosure(), &status)); 999 start_run_loop.QuitClosure(), &status));
999 load_run_loop.Run(); 1000 load_run_loop.Run();
1000 version_->embedded_worker()->RemoveListener(&wait_for_load); 1001 RunOnIOThread(base::Bind(&EmbeddedWorkerInstance::RemoveListener,
1002 base::Unretained(version_->embedded_worker()),
1003 &wait_for_load));
1001 1004
1002 // The script has loaded but start has not completed yet. 1005 // The script has loaded but start has not completed yet.
1003 ASSERT_EQ(SERVICE_WORKER_ERROR_FAILED, status); 1006 ASSERT_EQ(SERVICE_WORKER_ERROR_FAILED, status);
1004 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, version_->running_status()); 1007 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, version_->running_status());
1005 1008
1006 // Simulate execution timeout. Use a delay to prevent killing the worker 1009 // Simulate execution timeout. Use a delay to prevent killing the worker
1007 // before it's started execution. 1010 // before it's started execution.
1008 EXPECT_TRUE(version_->timeout_timer_.IsRunning()); 1011 EXPECT_TRUE(version_->timeout_timer_.IsRunning());
1009 RunOnIOThreadWithDelay(base::Bind(&self::TimeoutWorkerOnIOThread, this), 1012 RunOnIOThreadWithDelay(base::Bind(&self::TimeoutWorkerOnIOThread, this),
1010 base::TimeDelta::FromMilliseconds(100)); 1013 base::TimeDelta::FromMilliseconds(100));
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
1087 1090
1088 FetchOnRegisteredWorker(&result, &response2, &blob_data_handle); 1091 FetchOnRegisteredWorker(&result, &response2, &blob_data_handle);
1089 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, result); 1092 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, result);
1090 EXPECT_EQ(200, response2.status_code); 1093 EXPECT_EQ(200, response2.status_code);
1091 EXPECT_EQ("OK", response2.status_text); 1094 EXPECT_EQ("OK", response2.status_text);
1092 EXPECT_EQ(response1.response_time, response2.response_time); 1095 EXPECT_EQ(response1.response_time, response2.response_time);
1093 EXPECT_TRUE(response2.is_in_cache_storage); 1096 EXPECT_TRUE(response2.is_in_cache_storage);
1094 EXPECT_EQ("cache_name", response2.cache_storage_cache_name); 1097 EXPECT_EQ("cache_name", response2.cache_storage_cache_name);
1095 } 1098 }
1096 1099
1097 // Has errors under TSan. See https://crbug.com/631323.
1098 #if defined(THREAD_SANITIZER)
1099 #define MAYBE_FetchEvent_respondWithRejection \
1100 DISABLED_FetchEvent_respondWithRejection
1101 #else
1102 #define MAYBE_FetchEvent_respondWithRejection FetchEvent_respondWithRejection
1103 #endif
1104 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, 1100 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest,
1105 MAYBE_FetchEvent_respondWithRejection) { 1101 FetchEvent_respondWithRejection) {
1106 ServiceWorkerFetchEventResult result; 1102 ServiceWorkerFetchEventResult result;
1107 ServiceWorkerResponse response; 1103 ServiceWorkerResponse response;
1108 std::unique_ptr<storage::BlobDataHandle> blob_data_handle; 1104 std::unique_ptr<storage::BlobDataHandle> blob_data_handle;
1109 1105
1110 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this, 1106 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this,
1111 "/service_worker/fetch_event_rejected.js")); 1107 "/service_worker/fetch_event_rejected.js"));
1112 1108
1113 ConsoleListener console_listener; 1109 ConsoleListener console_listener;
1114 version_->embedded_worker()->AddListener(&console_listener); 1110 RunOnIOThread(base::Bind(&EmbeddedWorkerInstance::AddListener,
1111 base::Unretained(version_->embedded_worker()),
1112 &console_listener));
1115 1113
1116 FetchOnRegisteredWorker(&result, &response, &blob_data_handle); 1114 FetchOnRegisteredWorker(&result, &response, &blob_data_handle);
1117 const base::string16 expected1 = base::ASCIIToUTF16( 1115 const base::string16 expected1 = base::ASCIIToUTF16(
1118 "resulted in a network error response: the promise was rejected."); 1116 "resulted in a network error response: the promise was rejected.");
1119 const base::string16 expected2 = 1117 const base::string16 expected2 =
1120 base::ASCIIToUTF16("Uncaught (in promise) Rejecting respondWith promise"); 1118 base::ASCIIToUTF16("Uncaught (in promise) Rejecting respondWith promise");
1121 console_listener.WaitForConsoleMessages(2); 1119 console_listener.WaitForConsoleMessages(2);
1122 ASSERT_NE(base::string16::npos, 1120 ASSERT_NE(base::string16::npos,
1123 console_listener.messages()[0].find(expected1)); 1121 console_listener.messages()[0].find(expected1));
1124 ASSERT_EQ(0u, console_listener.messages()[1].find(expected2)); 1122 ASSERT_EQ(0u, console_listener.messages()[1].find(expected2));
1125 version_->embedded_worker()->RemoveListener(&console_listener); 1123 RunOnIOThread(base::Bind(&EmbeddedWorkerInstance::RemoveListener,
1124 base::Unretained(version_->embedded_worker()),
1125 &console_listener));
1126 1126
1127 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, result); 1127 ASSERT_EQ(SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, result);
1128 EXPECT_EQ(0, response.status_code); 1128 EXPECT_EQ(0, response.status_code);
1129 1129
1130 ASSERT_FALSE(blob_data_handle); 1130 ASSERT_FALSE(blob_data_handle);
1131 } 1131 }
1132 1132
1133 class MockContentBrowserClient : public TestContentBrowserClient { 1133 class MockContentBrowserClient : public TestContentBrowserClient {
1134 public: 1134 public:
1135 MockContentBrowserClient() 1135 MockContentBrowserClient()
(...skipping 852 matching lines...) Expand 10 before | Expand all | Expand 10 after
1988 1988
1989 IN_PROC_BROWSER_TEST_F(ServiceWorkerDisableWebSecurityTest, UpdateNoCrash) { 1989 IN_PROC_BROWSER_TEST_F(ServiceWorkerDisableWebSecurityTest, UpdateNoCrash) {
1990 const char kPageUrl[] = "/service_worker/disable_web_security_update.html"; 1990 const char kPageUrl[] = "/service_worker/disable_web_security_update.html";
1991 const char kScopeUrl[] = "/service_worker/scope/"; 1991 const char kScopeUrl[] = "/service_worker/scope/";
1992 const char kWorkerUrl[] = "/service_worker/fetch_event_blob.js"; 1992 const char kWorkerUrl[] = "/service_worker/fetch_event_blob.js";
1993 RegisterServiceWorkerOnCrossOriginServer(kScopeUrl, kWorkerUrl); 1993 RegisterServiceWorkerOnCrossOriginServer(kScopeUrl, kWorkerUrl);
1994 RunTestWithCrossOriginURL(kPageUrl, kScopeUrl); 1994 RunTestWithCrossOriginURL(kPageUrl, kScopeUrl);
1995 } 1995 }
1996 1996
1997 } // namespace content 1997 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698