| 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 |
| 36 helper_->SimulateCreateWorker(kRenderProcessId); |
| 28 } | 37 } |
| 29 | 38 |
| 30 virtual void TearDown() { | 39 virtual void TearDown() { |
| 40 helper_.reset(); |
| 31 if (context_wrapper_) { | 41 if (context_wrapper_) { |
| 32 context_wrapper_->Shutdown(); | 42 context_wrapper_->Shutdown(); |
| 33 context_wrapper_ = NULL; | 43 context_wrapper_ = NULL; |
| 34 } | 44 } |
| 35 } | 45 } |
| 36 | 46 |
| 37 ServiceWorkerContextCore* context() { return context_wrapper_->context(); } | 47 ServiceWorkerContextCore* context() { return context_wrapper_->context(); } |
| 38 | 48 |
| 39 TestBrowserThreadBundle browser_thread_bundle_; | 49 TestBrowserThreadBundle browser_thread_bundle_; |
| 40 scoped_refptr<ServiceWorkerContextWrapper> context_wrapper_; | 50 scoped_refptr<ServiceWorkerContextWrapper> context_wrapper_; |
| 51 scoped_ptr<EmbeddedWorkerTestHelper> helper_; |
| 41 }; | 52 }; |
| 42 | 53 |
| 43 static const int kRenderProcessId = 1; | |
| 44 | 54 |
| 45 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { | 55 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { |
| 46 public: | 56 public: |
| 47 TestingServiceWorkerDispatcherHost( | 57 TestingServiceWorkerDispatcherHost( |
| 48 int process_id, | 58 int process_id, |
| 49 ServiceWorkerContextWrapper* context_wrapper) | 59 ServiceWorkerContextWrapper* context_wrapper, |
| 60 EmbeddedWorkerTestHelper* helper) |
| 50 : ServiceWorkerDispatcherHost(process_id), | 61 : ServiceWorkerDispatcherHost(process_id), |
| 51 bad_messages_received_count_(0) { | 62 bad_messages_received_count_(0), |
| 63 helper_(helper) { |
| 52 Init(context_wrapper); | 64 Init(context_wrapper); |
| 53 } | 65 } |
| 54 | 66 |
| 55 virtual bool Send(IPC::Message* message) OVERRIDE { | 67 virtual bool Send(IPC::Message* message) OVERRIDE { |
| 56 sent_messages_.push_back(message); | 68 return helper_->Send(message); |
| 57 return true; | |
| 58 } | 69 } |
| 59 | 70 |
| 71 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); } |
| 72 |
| 60 virtual void BadMessageReceived() OVERRIDE { | 73 virtual void BadMessageReceived() OVERRIDE { |
| 61 ++bad_messages_received_count_; | 74 ++bad_messages_received_count_; |
| 62 } | 75 } |
| 63 | 76 |
| 64 ScopedVector<IPC::Message> sent_messages_; | |
| 65 int bad_messages_received_count_; | 77 int bad_messages_received_count_; |
| 66 | 78 |
| 67 protected: | 79 protected: |
| 80 EmbeddedWorkerTestHelper* helper_; |
| 68 virtual ~TestingServiceWorkerDispatcherHost() {} | 81 virtual ~TestingServiceWorkerDispatcherHost() {} |
| 69 }; | 82 }; |
| 70 | 83 |
| 71 TEST_F(ServiceWorkerDispatcherHostTest, DisabledCausesError) { | 84 TEST_F(ServiceWorkerDispatcherHostTest, DisabledCausesError) { |
| 72 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( | 85 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( |
| 73 switches::kEnableServiceWorker)); | 86 switches::kEnableServiceWorker)); |
| 74 | 87 |
| 75 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | 88 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = |
| 76 new TestingServiceWorkerDispatcherHost(kRenderProcessId, | 89 new TestingServiceWorkerDispatcherHost( |
| 77 context_wrapper_.get()); | 90 kRenderProcessId, context_wrapper_.get(), helper_.get()); |
| 78 | 91 |
| 79 bool handled; | 92 bool handled; |
| 80 dispatcher_host->OnMessageReceived( | 93 dispatcher_host->OnMessageReceived( |
| 81 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), | 94 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), |
| 82 &handled); | 95 &handled); |
| 83 EXPECT_TRUE(handled); | 96 EXPECT_TRUE(handled); |
| 84 | 97 |
| 85 // TODO(alecflett): Pump the message loop when this becomes async. | 98 // TODO(alecflett): Pump the message loop when this becomes async. |
| 86 ASSERT_EQ(1UL, dispatcher_host->sent_messages_.size()); | 99 ASSERT_EQ(1UL, dispatcher_host->ipc_sink()->message_count()); |
| 87 EXPECT_EQ( | 100 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( |
| 88 static_cast<uint32>(ServiceWorkerMsg_ServiceWorkerRegistrationError::ID), | 101 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID)); |
| 89 dispatcher_host->sent_messages_[0]->type()); | |
| 90 } | 102 } |
| 91 | 103 |
| 92 TEST_F(ServiceWorkerDispatcherHostTest, Enabled) { | 104 TEST_F(ServiceWorkerDispatcherHostTest, Enabled) { |
| 93 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( | 105 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( |
| 94 switches::kEnableServiceWorker)); | 106 switches::kEnableServiceWorker)); |
| 95 CommandLine::ForCurrentProcess()->AppendSwitch( | 107 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 96 switches::kEnableServiceWorker); | 108 switches::kEnableServiceWorker); |
| 97 | 109 |
| 98 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | 110 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = |
| 99 new TestingServiceWorkerDispatcherHost(kRenderProcessId, | 111 new TestingServiceWorkerDispatcherHost( |
| 100 context_wrapper_.get()); | 112 kRenderProcessId, context_wrapper_.get(), helper_.get()); |
| 101 | 113 |
| 102 bool handled; | 114 bool handled; |
| 103 dispatcher_host->OnMessageReceived( | 115 dispatcher_host->OnMessageReceived( |
| 104 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), | 116 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), |
| 105 &handled); | 117 &handled); |
| 106 EXPECT_TRUE(handled); | 118 EXPECT_TRUE(handled); |
| 107 base::RunLoop().RunUntilIdle(); | 119 base::RunLoop().RunUntilIdle(); |
| 108 | 120 |
| 109 // TODO(alecflett): Pump the message loop when this becomes async. | 121 // TODO(alecflett): Pump the message loop when this becomes async. |
| 110 ASSERT_EQ(1UL, dispatcher_host->sent_messages_.size()); | 122 ASSERT_EQ(2UL, dispatcher_host->ipc_sink()->message_count()); |
| 111 EXPECT_EQ(static_cast<uint32>(ServiceWorkerMsg_ServiceWorkerRegistered::ID), | 123 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( |
| 112 dispatcher_host->sent_messages_[0]->type()); | 124 EmbeddedWorkerMsg_StartWorker::ID)); |
| 125 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( |
| 126 ServiceWorkerMsg_ServiceWorkerRegistered::ID)); |
| 113 } | 127 } |
| 114 | 128 |
| 115 TEST_F(ServiceWorkerDispatcherHostTest, EarlyContextDeletion) { | 129 TEST_F(ServiceWorkerDispatcherHostTest, EarlyContextDeletion) { |
| 116 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( | 130 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( |
| 117 switches::kEnableServiceWorker)); | 131 switches::kEnableServiceWorker)); |
| 118 CommandLine::ForCurrentProcess()->AppendSwitch( | 132 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 119 switches::kEnableServiceWorker); | 133 switches::kEnableServiceWorker); |
| 120 | 134 |
| 121 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | 135 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = |
| 122 new TestingServiceWorkerDispatcherHost(kRenderProcessId, | 136 new TestingServiceWorkerDispatcherHost( |
| 123 context_wrapper_.get()); | 137 kRenderProcessId, context_wrapper_.get(), helper_.get()); |
| 124 | 138 |
| 125 context_wrapper_->Shutdown(); | 139 context_wrapper_->Shutdown(); |
| 126 context_wrapper_ = NULL; | 140 context_wrapper_ = NULL; |
| 127 | 141 |
| 128 bool handled; | 142 bool handled; |
| 129 dispatcher_host->OnMessageReceived( | 143 dispatcher_host->OnMessageReceived( |
| 130 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), | 144 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), |
| 131 &handled); | 145 &handled); |
| 132 EXPECT_TRUE(handled); | 146 EXPECT_TRUE(handled); |
| 133 | 147 |
| 134 // TODO(alecflett): Pump the message loop when this becomes async. | 148 // TODO(alecflett): Pump the message loop when this becomes async. |
| 135 ASSERT_EQ(1UL, dispatcher_host->sent_messages_.size()); | 149 ASSERT_EQ(1UL, dispatcher_host->ipc_sink()->message_count()); |
| 136 EXPECT_EQ( | 150 EXPECT_TRUE(dispatcher_host->ipc_sink()->GetUniqueMessageMatching( |
| 137 static_cast<uint32>(ServiceWorkerMsg_ServiceWorkerRegistrationError::ID), | 151 ServiceWorkerMsg_ServiceWorkerRegistrationError::ID)); |
| 138 dispatcher_host->sent_messages_[0]->type()); | |
| 139 } | 152 } |
| 140 | 153 |
| 141 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { | 154 TEST_F(ServiceWorkerDispatcherHostTest, ProviderCreatedAndDestroyed) { |
| 142 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | 155 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = |
| 143 new TestingServiceWorkerDispatcherHost(kRenderProcessId, | 156 new TestingServiceWorkerDispatcherHost( |
| 144 context_wrapper_.get()); | 157 kRenderProcessId, context_wrapper_.get(), helper_.get()); |
| 145 | 158 |
| 146 const int kProviderId = 1001; // Test with a value != kRenderProcessId. | 159 const int kProviderId = 1001; // Test with a value != kRenderProcessId. |
| 147 | 160 |
| 148 bool handled = false; | 161 bool handled = false; |
| 149 dispatcher_host->OnMessageReceived( | 162 dispatcher_host->OnMessageReceived( |
| 150 ServiceWorkerHostMsg_ProviderCreated(kProviderId), | 163 ServiceWorkerHostMsg_ProviderCreated(kProviderId), |
| 151 &handled); | 164 &handled); |
| 152 EXPECT_TRUE(handled); | 165 EXPECT_TRUE(handled); |
| 153 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 166 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
| 154 | 167 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 181 ServiceWorkerHostMsg_ProviderCreated(kProviderId), | 194 ServiceWorkerHostMsg_ProviderCreated(kProviderId), |
| 182 &handled); | 195 &handled); |
| 183 EXPECT_TRUE(handled); | 196 EXPECT_TRUE(handled); |
| 184 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 197 EXPECT_TRUE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
| 185 EXPECT_TRUE(dispatcher_host->HasOneRef()); | 198 EXPECT_TRUE(dispatcher_host->HasOneRef()); |
| 186 dispatcher_host = NULL; | 199 dispatcher_host = NULL; |
| 187 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); | 200 EXPECT_FALSE(context()->GetProviderHost(kRenderProcessId, kProviderId)); |
| 188 } | 201 } |
| 189 | 202 |
| 190 } // namespace content | 203 } // namespace content |
| OLD | NEW |