Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/browser/service_worker/service_worker_dispatcher_host.h" | 5 #include "content/browser/service_worker/service_worker_dispatcher_host.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "content/browser/browser_thread_impl.h" | 10 #include "content/browser/browser_thread_impl.h" |
| 11 #include "content/browser/service_worker/embedded_worker_instance.h" | |
| 12 #include "content/browser/service_worker/embedded_worker_registry.h" | |
| 13 #include "content/browser/service_worker/embedded_worker_test_helper.h" | |
| 11 #include "content/browser/service_worker/service_worker_context_core.h" | 14 #include "content/browser/service_worker/service_worker_context_core.h" |
| 12 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 15 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 16 #include "content/common/service_worker/embedded_worker_messages.h" | |
| 13 #include "content/common/service_worker/service_worker_messages.h" | 17 #include "content/common/service_worker/service_worker_messages.h" |
| 14 #include "content/public/common/content_switches.h" | 18 #include "content/public/common/content_switches.h" |
| 15 #include "content/public/test/test_browser_thread_bundle.h" | 19 #include "content/public/test/test_browser_thread_bundle.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 21 |
| 18 namespace content { | 22 namespace content { |
| 19 | 23 |
| 24 static const int kRenderProcessId = 1; | |
| 25 | |
| 20 class ServiceWorkerDispatcherHostTest : public testing::Test { | 26 class ServiceWorkerDispatcherHostTest : public testing::Test { |
| 21 protected: | 27 protected: |
| 22 ServiceWorkerDispatcherHostTest() | 28 ServiceWorkerDispatcherHostTest() |
| 23 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 29 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 24 | 30 |
| 25 virtual void SetUp() { | 31 virtual void SetUp() { |
| 26 context_wrapper_ = new ServiceWorkerContextWrapper; | 32 context_wrapper_ = new ServiceWorkerContextWrapper; |
| 27 context_wrapper_->Init(base::FilePath(), NULL); | 33 context_wrapper_->Init(base::FilePath(), NULL); |
| 34 helper_.reset(new EmbeddedWorkerTestHelper(context())); | |
| 35 scoped_ptr<EmbeddedWorkerInstance> worker = | |
| 36 context()->embedded_worker_registry()->CreateWorker(); | |
| 37 | |
| 38 int render_process_id = kRenderProcessId; | |
| 39 int embedded_worker_id = worker->embedded_worker_id(); | |
| 40 helper_->SimulateAddProcess(embedded_worker_id, render_process_id); | |
|
kinuko
2014/02/04 11:41:31
Ditto. You can remove line 35-40 and add:
con
alecflett
2014/02/04 20:09:04
Done.
| |
| 28 } | 41 } |
| 29 | 42 |
| 30 virtual void TearDown() { | 43 virtual void TearDown() { |
| 44 helper_.reset(); | |
| 31 if (context_wrapper_) { | 45 if (context_wrapper_) { |
| 32 context_wrapper_->Shutdown(); | 46 context_wrapper_->Shutdown(); |
| 33 context_wrapper_ = NULL; | 47 context_wrapper_ = NULL; |
| 34 } | 48 } |
| 35 } | 49 } |
| 36 | 50 |
| 37 ServiceWorkerContextCore* context() { return context_wrapper_->context(); } | 51 ServiceWorkerContextCore* context() { return context_wrapper_->context(); } |
| 38 | 52 |
| 39 TestBrowserThreadBundle browser_thread_bundle_; | 53 TestBrowserThreadBundle browser_thread_bundle_; |
| 40 scoped_refptr<ServiceWorkerContextWrapper> context_wrapper_; | 54 scoped_refptr<ServiceWorkerContextWrapper> context_wrapper_; |
| 55 scoped_ptr<EmbeddedWorkerTestHelper> helper_; | |
| 41 }; | 56 }; |
| 42 | 57 |
| 43 static const int kRenderProcessId = 1; | |
| 44 | 58 |
| 45 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { | 59 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { |
| 46 public: | 60 public: |
| 47 TestingServiceWorkerDispatcherHost( | 61 TestingServiceWorkerDispatcherHost( |
| 48 int process_id, | 62 int process_id, |
| 49 ServiceWorkerContextWrapper* context_wrapper) | 63 ServiceWorkerContextWrapper* context_wrapper, |
| 64 EmbeddedWorkerTestHelper* helper) | |
| 50 : ServiceWorkerDispatcherHost(process_id), | 65 : ServiceWorkerDispatcherHost(process_id), |
| 51 bad_messages_received_count_(0) { | 66 bad_messages_received_count_(0), |
| 67 helper_(helper) { | |
| 52 Init(context_wrapper); | 68 Init(context_wrapper); |
| 53 } | 69 } |
| 54 | 70 |
| 55 virtual bool Send(IPC::Message* message) OVERRIDE { | 71 virtual bool Send(IPC::Message* message) OVERRIDE { |
| 56 sent_messages_.push_back(message); | 72 return helper_->Send(message); |
| 57 return true; | |
| 58 } | 73 } |
| 59 | 74 |
| 75 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); } | |
| 76 | |
| 60 virtual void BadMessageReceived() OVERRIDE { | 77 virtual void BadMessageReceived() OVERRIDE { |
| 61 ++bad_messages_received_count_; | 78 ++bad_messages_received_count_; |
| 62 } | 79 } |
| 63 | 80 |
| 64 ScopedVector<IPC::Message> sent_messages_; | |
| 65 int bad_messages_received_count_; | 81 int bad_messages_received_count_; |
| 66 | 82 |
| 67 protected: | 83 protected: |
| 84 EmbeddedWorkerTestHelper* helper_; | |
| 68 virtual ~TestingServiceWorkerDispatcherHost() {} | 85 virtual ~TestingServiceWorkerDispatcherHost() {} |
| 69 }; | 86 }; |
| 70 | 87 |
| 71 TEST_F(ServiceWorkerDispatcherHostTest, DisabledCausesError) { | 88 TEST_F(ServiceWorkerDispatcherHostTest, DisabledCausesError) { |
| 72 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( | 89 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( |
| 73 switches::kEnableServiceWorker)); | 90 switches::kEnableServiceWorker)); |
| 74 | 91 |
| 75 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | 92 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = |
| 76 new TestingServiceWorkerDispatcherHost(kRenderProcessId, | 93 new TestingServiceWorkerDispatcherHost( |
| 77 context_wrapper_.get()); | 94 kRenderProcessId, context_wrapper_.get(), helper_.get()); |
| 78 | 95 |
| 79 bool handled; | 96 bool handled; |
| 80 dispatcher_host->OnMessageReceived( | 97 dispatcher_host->OnMessageReceived( |
| 81 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), | 98 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), |
| 82 &handled); | 99 &handled); |
| 83 EXPECT_TRUE(handled); | 100 EXPECT_TRUE(handled); |
| 84 | 101 |
| 85 // TODO(alecflett): Pump the message loop when this becomes async. | 102 // TODO(alecflett): Pump the message loop when this becomes async. |
| 86 ASSERT_EQ(1UL, dispatcher_host->sent_messages_.size()); | 103 ASSERT_EQ(1UL, dispatcher_host->ipc_sink()->message_count()); |
| 87 EXPECT_EQ( | 104 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( |
| 88 static_cast<uint32>(ServiceWorkerMsg_ServiceWorkerRegistrationError::ID), | 105 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID)); |
| 89 dispatcher_host->sent_messages_[0]->type()); | |
| 90 } | 106 } |
| 91 | 107 |
| 92 TEST_F(ServiceWorkerDispatcherHostTest, Enabled) { | 108 TEST_F(ServiceWorkerDispatcherHostTest, Enabled) { |
| 93 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( | 109 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( |
| 94 switches::kEnableServiceWorker)); | 110 switches::kEnableServiceWorker)); |
| 95 CommandLine::ForCurrentProcess()->AppendSwitch( | 111 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 96 switches::kEnableServiceWorker); | 112 switches::kEnableServiceWorker); |
| 97 | 113 |
| 98 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | 114 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = |
| 99 new TestingServiceWorkerDispatcherHost(kRenderProcessId, | 115 new TestingServiceWorkerDispatcherHost( |
| 100 context_wrapper_.get()); | 116 kRenderProcessId, context_wrapper_.get(), helper_.get()); |
| 101 | 117 |
| 102 bool handled; | 118 bool handled; |
| 103 dispatcher_host->OnMessageReceived( | 119 dispatcher_host->OnMessageReceived( |
| 104 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), | 120 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), |
| 105 &handled); | 121 &handled); |
| 106 EXPECT_TRUE(handled); | 122 EXPECT_TRUE(handled); |
| 107 base::RunLoop().RunUntilIdle(); | 123 base::RunLoop().RunUntilIdle(); |
| 108 | 124 |
| 109 // TODO(alecflett): Pump the message loop when this becomes async. | 125 // TODO(alecflett): Pump the message loop when this becomes async. |
| 110 ASSERT_EQ(1UL, dispatcher_host->sent_messages_.size()); | 126 ASSERT_EQ(2UL, dispatcher_host->ipc_sink()->message_count()); |
| 111 EXPECT_EQ(static_cast<uint32>(ServiceWorkerMsg_ServiceWorkerRegistered::ID), | 127 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( |
| 112 dispatcher_host->sent_messages_[0]->type()); | 128 EmbeddedWorkerMsg_StartWorker::ID)); |
|
kinuko
2014/02/04 03:37:53
Could we have one more GetUniqueMessageMatching fo
alecflett
2014/02/04 20:09:04
Done.
| |
| 113 } | 129 } |
| 114 | 130 |
| 115 TEST_F(ServiceWorkerDispatcherHostTest, EarlyContextDeletion) { | 131 TEST_F(ServiceWorkerDispatcherHostTest, EarlyContextDeletion) { |
| 116 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( | 132 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( |
| 117 switches::kEnableServiceWorker)); | 133 switches::kEnableServiceWorker)); |
| 118 CommandLine::ForCurrentProcess()->AppendSwitch( | 134 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 119 switches::kEnableServiceWorker); | 135 switches::kEnableServiceWorker); |
| 120 | 136 |
| 121 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | 137 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = |
| 122 new TestingServiceWorkerDispatcherHost(kRenderProcessId, | 138 new TestingServiceWorkerDispatcherHost( |
| 123 context_wrapper_.get()); | 139 kRenderProcessId, context_wrapper_.get(), helper_.get()); |
| 124 | 140 |
| 125 context_wrapper_->Shutdown(); | 141 context_wrapper_->Shutdown(); |
| 126 context_wrapper_ = NULL; | 142 context_wrapper_ = NULL; |
| 127 | 143 |
| 128 bool handled; | 144 bool handled; |
| 129 dispatcher_host->OnMessageReceived( | 145 dispatcher_host->OnMessageReceived( |
| 130 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), | 146 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), |
| 131 &handled); | 147 &handled); |
| 132 EXPECT_TRUE(handled); | 148 EXPECT_TRUE(handled); |
| 133 | 149 |
| 134 // TODO(alecflett): Pump the message loop when this becomes async. | 150 // TODO(alecflett): Pump the message loop when this becomes async. |
| 135 ASSERT_EQ(1UL, dispatcher_host->sent_messages_.size()); | 151 ASSERT_EQ(1UL, dispatcher_host->ipc_sink()->message_count()); |
| 136 EXPECT_EQ( | 152 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( |
| 137 static_cast<uint32>(ServiceWorkerMsg_ServiceWorkerRegistrationError::ID), | 153 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID)); |
| 138 dispatcher_host->sent_messages_[0]->type()); | |
| 139 } | 154 } |
| 140 | 155 |
| 141 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { | 156 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { |
| 142 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | 157 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = |
| 143 new TestingServiceWorkerDispatcherHost(kRenderProcessId, | 158 new TestingServiceWorkerDispatcherHost( |
| 144 context_wrapper_.get()); | 159 kRenderProcessId, context_wrapper_.get(), helper_.get()); |
| 145 | 160 |
| 146 const int kProviderId = 1001; // Test with a value != kRenderProcessId. | 161 const int kProviderId = 1001; // Test with a value != kRenderProcessId. |
| 147 | 162 |
| 148 bool handled = false; | 163 bool handled = false; |
| 149 dispatcher_host->OnMessageReceived( | 164 dispatcher_host->OnMessageReceived( |
| 150 ServiceWorkerHostMsg_ProviderCreated(kProviderId), | 165 ServiceWorkerHostMsg_ProviderCreated(kProviderId), |
| 151 &handled); | 166 &handled); |
| 152 EXPECT_TRUE(handled); | 167 EXPECT_TRUE(handled); |
| 153 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 168 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
| 154 | 169 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 181 ServiceWorkerHostMsg_ProviderCreated(kProviderId), | 196 ServiceWorkerHostMsg_ProviderCreated(kProviderId), |
| 182 &handled); | 197 &handled); |
| 183 EXPECT_TRUE(handled); | 198 EXPECT_TRUE(handled); |
| 184 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 199 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
| 185 EXPECT_TRUE(dispatcher_host->HasOneRef()); | 200 EXPECT_TRUE(dispatcher_host->HasOneRef()); |
| 186 dispatcher_host = NULL; | 201 dispatcher_host = NULL; |
| 187 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 202 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
| 188 } | 203 } |
| 189 | 204 |
| 190 } // namespace content | 205 } // namespace content |
| OLD | NEW |