Index: content/browser/service_worker/service_worker_dispatcher_host_unittest.cc |
diff --git a/content/browser/service_worker/service_worker_dispatcher_host_unittest.cc b/content/browser/service_worker/service_worker_dispatcher_host_unittest.cc |
index 462d093bae277e4b025794237f480e329178cbfa..6924814e7de6bd9f58cd123639bb24c38263cf2d 100644 |
--- a/content/browser/service_worker/service_worker_dispatcher_host_unittest.cc |
+++ b/content/browser/service_worker/service_worker_dispatcher_host_unittest.cc |
@@ -13,11 +13,13 @@ |
#include "content/browser/service_worker/embedded_worker_test_helper.h" |
#include "content/browser/service_worker/service_worker_context_core.h" |
#include "content/browser/service_worker/service_worker_context_wrapper.h" |
+#include "content/browser/service_worker/service_worker_test_utils.h" |
#include "content/common/service_worker/embedded_worker_messages.h" |
#include "content/common/service_worker/service_worker_messages.h" |
#include "content/public/common/content_switches.h" |
#include "content/public/test/mock_resource_context.h" |
#include "content/public/test/test_browser_thread_bundle.h" |
+#include "content/public/test/test_utils.h" |
#include "content/test/test_content_browser_client.h" |
#include "testing/gtest/include/gtest/gtest.h" |
@@ -32,9 +34,25 @@ static void SaveStatusCallback(bool* called, |
*out = status; |
} |
+class IPCWaiter : public IPC::Listener { |
+ public: |
+ explicit IPCWaiter(uint32 expected_message) |
+ : expected_message_(expected_message), runner_(new MessageLoopRunner) {} |
+ |
+ void Wait() { runner_->Run(); } |
+ |
+ bool OnMessageReceived(const IPC::Message& message) override { |
+ if (message.type() == expected_message_) |
+ runner_->Quit(); |
+ return false; |
+ } |
+ |
+ private: |
+ uint32 expected_message_; |
+ scoped_refptr<MessageLoopRunner> runner_; |
+}; |
} |
-static const int kRenderProcessId = 1; |
static const int kRenderFrameId = 1; |
class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { |
@@ -69,10 +87,10 @@ class ServiceWorkerDispatcherHostTest : public testing::Test { |
: browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
void SetUp() override { |
- helper_.reset( |
- new EmbeddedWorkerTestHelper(base::FilePath(), kRenderProcessId)); |
+ helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); |
dispatcher_host_ = new TestingServiceWorkerDispatcherHost( |
- kRenderProcessId, context_wrapper(), &resource_context_, helper_.get()); |
+ helper_->mock_render_process_id(), context_wrapper(), |
+ &resource_context_, helper_.get()); |
} |
void TearDown() override { helper_.reset(); } |
@@ -93,10 +111,14 @@ class ServiceWorkerDispatcherHostTest : public testing::Test { |
GURL pattern, |
GURL worker_url, |
uint32 expected_message) { |
+ IPCWaiter waiter(expected_message); |
+ dispatcher_host_->ipc_sink()->AddFilter(&waiter); |
SendRegister(provider_id, pattern, worker_url); |
+ waiter.Wait(); |
EXPECT_TRUE(dispatcher_host_->ipc_sink()->GetUniqueMessageMatching( |
expected_message)); |
dispatcher_host_->ipc_sink()->ClearMessages(); |
+ dispatcher_host_->ipc_sink()->RemoveFilter(&waiter); |
} |
void SendUnregister(int64 provider_id, int64 registration_id) { |
@@ -146,7 +168,7 @@ class ServiceWorkerDispatcherHostTest : public testing::Test { |
ServiceWorkerProviderHost* CreateServiceWorkerProviderHost(int provider_id) { |
return new ServiceWorkerProviderHost( |
- kRenderProcessId, kRenderFrameId, provider_id, |
+ helper_->mock_render_process_id(), kRenderFrameId, provider_id, |
SERVICE_WORKER_PROVIDER_FOR_WINDOW, context()->AsWeakPtr(), |
dispatcher_host_.get()); |
} |
@@ -375,7 +397,8 @@ TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { |
dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( |
kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW)); |
- EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
+ EXPECT_TRUE(context()->GetProviderHost(helper_->mock_render_process_id(), |
+ kProviderId)); |
// Two with the same ID should be seen as a bad message. |
dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( |
@@ -384,7 +407,8 @@ TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { |
dispatcher_host_->OnMessageReceived( |
ServiceWorkerHostMsg_ProviderDestroyed(kProviderId)); |
- EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
+ EXPECT_FALSE(context()->GetProviderHost(helper_->mock_render_process_id(), |
+ kProviderId)); |
// Destroying an ID that does not exist warrants a bad message. |
dispatcher_host_->OnMessageReceived( |
@@ -395,10 +419,12 @@ TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { |
// process to get deleted as well. |
dispatcher_host_->OnMessageReceived(ServiceWorkerHostMsg_ProviderCreated( |
kProviderId, MSG_ROUTING_NONE, SERVICE_WORKER_PROVIDER_FOR_WINDOW)); |
- EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
+ EXPECT_TRUE(context()->GetProviderHost(helper_->mock_render_process_id(), |
+ kProviderId)); |
EXPECT_TRUE(dispatcher_host_->HasOneRef()); |
dispatcher_host_ = NULL; |
- EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
+ EXPECT_FALSE(context()->GetProviderHost(helper_->mock_render_process_id(), |
+ kProviderId)); |
} |
TEST_F(ServiceWorkerDispatcherHostTest, GetRegistration_SameOrigin) { |
@@ -524,34 +550,37 @@ TEST_F(ServiceWorkerDispatcherHostTest, CleanupOnRendererCrash) { |
EXPECT_TRUE(called); |
ASSERT_EQ(SERVICE_WORKER_OK, status); |
- helper_->SimulateAddProcessToPattern(pattern, kRenderProcessId); |
+ helper_->SimulateAddProcessToPattern(pattern, |
+ helper_->mock_render_process_id()); |
// Start up the worker. |
status = SERVICE_WORKER_ERROR_ABORT; |
- version->StartWorker(base::Bind(&SaveStatusCallback, &called, &status)); |
- base::RunLoop().RunUntilIdle(); |
+ scoped_refptr<MessageLoopRunner> runner(new MessageLoopRunner); |
+ version->StartWorker( |
+ CreateReceiverOnCurrentThread(&status, runner->QuitClosure())); |
+ runner->Run(); |
- EXPECT_TRUE(called); |
EXPECT_EQ(SERVICE_WORKER_OK, status); |
- EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
+ EXPECT_TRUE(context()->GetProviderHost(helper_->mock_render_process_id(), |
+ kProviderId)); |
EXPECT_EQ(ServiceWorkerVersion::RUNNING, version->running_status()); |
// Simulate the render process crashing. |
dispatcher_host_->OnFilterRemoved(); |
// The dispatcher host should clean up the state from the process. |
- EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
+ EXPECT_FALSE(context()->GetProviderHost(helper_->mock_render_process_id(), |
+ kProviderId)); |
EXPECT_EQ(ServiceWorkerVersion::STOPPED, version->running_status()); |
// We should be able to hook up a new dispatcher host although the old object |
// is not yet destroyed. This is what the browser does when reusing a crashed |
// render process. |
scoped_refptr<TestingServiceWorkerDispatcherHost> new_dispatcher_host( |
- new TestingServiceWorkerDispatcherHost(kRenderProcessId, |
- context_wrapper(), |
- &resource_context_, |
- helper_.get())); |
+ new TestingServiceWorkerDispatcherHost( |
+ helper_->mock_render_process_id(), context_wrapper(), |
+ &resource_context_, helper_.get())); |
// To show the new dispatcher can operate, simulate provider creation. Since |
// the old dispatcher cleaned up the old provider host, the new one won't |