| OLD | NEW |
| 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/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" |
| 7 #include "content/browser/service_worker/embedded_worker_registry.h" | 7 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 8 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 8 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 9 #include "content/browser/service_worker/service_worker_context_core.h" | 9 #include "content/browser/service_worker/service_worker_context_core.h" |
| 10 #include "content/browser/service_worker/service_worker_registration.h" | 10 #include "content/browser/service_worker/service_worker_registration.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 // --------------------------------------------------------------------------- | 26 // --------------------------------------------------------------------------- |
| 27 | 27 |
| 28 namespace content { | 28 namespace content { |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 static const int kRenderProcessId = 1; | 32 static const int kRenderProcessId = 1; |
| 33 | 33 |
| 34 class MessageReceiver : public EmbeddedWorkerTestHelper { | 34 class MessageReceiver : public EmbeddedWorkerTestHelper { |
| 35 public: | 35 public: |
| 36 MessageReceiver(ServiceWorkerContextCore* context) | 36 MessageReceiver() |
| 37 : EmbeddedWorkerTestHelper(context, kRenderProcessId), | 37 : EmbeddedWorkerTestHelper(kRenderProcessId), |
| 38 current_embedded_worker_id_(0) {} | 38 current_embedded_worker_id_(0) {} |
| 39 virtual ~MessageReceiver() {} | 39 virtual ~MessageReceiver() {} |
| 40 | 40 |
| 41 virtual bool OnMessageToWorker(int thread_id, | 41 virtual bool OnMessageToWorker(int thread_id, |
| 42 int embedded_worker_id, | 42 int embedded_worker_id, |
| 43 const IPC::Message& message) OVERRIDE { | 43 const IPC::Message& message) OVERRIDE { |
| 44 if (EmbeddedWorkerTestHelper::OnMessageToWorker( | 44 if (EmbeddedWorkerTestHelper::OnMessageToWorker( |
| 45 thread_id, embedded_worker_id, message)) { | 45 thread_id, embedded_worker_id, message)) { |
| 46 return true; | 46 return true; |
| 47 } | 47 } |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 }; | 110 }; |
| 111 | 111 |
| 112 } // namespace | 112 } // namespace |
| 113 | 113 |
| 114 class ServiceWorkerVersionTest : public testing::Test { | 114 class ServiceWorkerVersionTest : public testing::Test { |
| 115 protected: | 115 protected: |
| 116 ServiceWorkerVersionTest() | 116 ServiceWorkerVersionTest() |
| 117 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 117 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 118 | 118 |
| 119 virtual void SetUp() OVERRIDE { | 119 virtual void SetUp() OVERRIDE { |
| 120 context_.reset(new ServiceWorkerContextCore(base::FilePath(), NULL, NULL)); | 120 helper_.reset(new MessageReceiver()); |
| 121 helper_.reset(new MessageReceiver(context_.get())); | |
| 122 | 121 |
| 123 registration_ = new ServiceWorkerRegistration( | 122 registration_ = new ServiceWorkerRegistration( |
| 124 GURL("http://www.example.com/*"), | 123 GURL("http://www.example.com/*"), |
| 125 GURL("http://www.example.com/service_worker.js"), | 124 GURL("http://www.example.com/service_worker.js"), |
| 126 1L, context_->AsWeakPtr()); | 125 1L, |
| 126 helper_->context()->AsWeakPtr()); |
| 127 version_ = new ServiceWorkerVersion( | 127 version_ = new ServiceWorkerVersion( |
| 128 registration_, | 128 registration_, 1L, helper_->context()->AsWeakPtr()); |
| 129 1L, context_->AsWeakPtr()); | |
| 130 | 129 |
| 131 // Simulate adding one process to the worker. | 130 // Simulate adding one process to the worker. |
| 132 int embedded_worker_id = version_->embedded_worker()->embedded_worker_id(); | 131 int embedded_worker_id = version_->embedded_worker()->embedded_worker_id(); |
| 133 helper_->SimulateAddProcessToWorker(embedded_worker_id, kRenderProcessId); | 132 helper_->SimulateAddProcessToWorker(embedded_worker_id, kRenderProcessId); |
| 134 ASSERT_TRUE(version_->HasProcessToRun()); | 133 ASSERT_TRUE(version_->HasProcessToRun()); |
| 135 } | 134 } |
| 136 | 135 |
| 137 virtual void TearDown() OVERRIDE { | 136 virtual void TearDown() OVERRIDE { |
| 138 version_ = 0; | 137 version_ = 0; |
| 139 registration_ = 0; | 138 registration_ = 0; |
| 140 helper_.reset(); | 139 helper_.reset(); |
| 141 context_.reset(); | |
| 142 } | 140 } |
| 143 | 141 |
| 144 TestBrowserThreadBundle thread_bundle_; | 142 TestBrowserThreadBundle thread_bundle_; |
| 145 scoped_ptr<ServiceWorkerContextCore> context_; | |
| 146 scoped_ptr<MessageReceiver> helper_; | 143 scoped_ptr<MessageReceiver> helper_; |
| 147 scoped_refptr<ServiceWorkerRegistration> registration_; | 144 scoped_refptr<ServiceWorkerRegistration> registration_; |
| 148 scoped_refptr<ServiceWorkerVersion> version_; | 145 scoped_refptr<ServiceWorkerVersion> version_; |
| 149 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersionTest); | 146 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersionTest); |
| 150 }; | 147 }; |
| 151 | 148 |
| 152 TEST_F(ServiceWorkerVersionTest, ConcurrentStartAndStop) { | 149 TEST_F(ServiceWorkerVersionTest, ConcurrentStartAndStop) { |
| 153 // Call StartWorker() multiple times. | 150 // Call StartWorker() multiple times. |
| 154 ServiceWorkerStatusCode status1 = SERVICE_WORKER_ERROR_FAILED; | 151 ServiceWorkerStatusCode status1 = SERVICE_WORKER_ERROR_FAILED; |
| 155 ServiceWorkerStatusCode status2 = SERVICE_WORKER_ERROR_FAILED; | 152 ServiceWorkerStatusCode status2 = SERVICE_WORKER_ERROR_FAILED; |
| 156 ServiceWorkerStatusCode status3 = SERVICE_WORKER_ERROR_FAILED; | 153 ServiceWorkerStatusCode status3 = SERVICE_WORKER_ERROR_FAILED; |
| 157 version_->StartWorker(CreateReceiverOnCurrentThread(&status1)); | 154 const std::vector<int> no_processes; |
| 158 version_->StartWorker(CreateReceiverOnCurrentThread(&status2)); | 155 version_->StartWorker(CreateReceiverOnCurrentThread(&status1), no_processes); |
| 156 version_->StartWorker(CreateReceiverOnCurrentThread(&status2), no_processes); |
| 159 | 157 |
| 160 EXPECT_EQ(ServiceWorkerVersion::STARTING, version_->running_status()); | 158 EXPECT_EQ(ServiceWorkerVersion::STARTING, version_->running_status()); |
| 161 base::RunLoop().RunUntilIdle(); | 159 base::RunLoop().RunUntilIdle(); |
| 162 EXPECT_EQ(ServiceWorkerVersion::RUNNING, version_->running_status()); | 160 EXPECT_EQ(ServiceWorkerVersion::RUNNING, version_->running_status()); |
| 163 | 161 |
| 164 // Call StartWorker() after it's started. | 162 // Call StartWorker() after it's started. |
| 165 version_->StartWorker(CreateReceiverOnCurrentThread(&status3)); | 163 version_->StartWorker(CreateReceiverOnCurrentThread(&status3), no_processes); |
| 166 base::RunLoop().RunUntilIdle(); | 164 base::RunLoop().RunUntilIdle(); |
| 167 | 165 |
| 168 // All should just succeed. | 166 // All should just succeed. |
| 169 EXPECT_EQ(SERVICE_WORKER_OK, status1); | 167 EXPECT_EQ(SERVICE_WORKER_OK, status1); |
| 170 EXPECT_EQ(SERVICE_WORKER_OK, status2); | 168 EXPECT_EQ(SERVICE_WORKER_OK, status2); |
| 171 EXPECT_EQ(SERVICE_WORKER_OK, status3); | 169 EXPECT_EQ(SERVICE_WORKER_OK, status3); |
| 172 | 170 |
| 173 // Call StopWorker() multiple times. | 171 // Call StopWorker() multiple times. |
| 174 status1 = SERVICE_WORKER_ERROR_FAILED; | 172 status1 = SERVICE_WORKER_ERROR_FAILED; |
| 175 status2 = SERVICE_WORKER_ERROR_FAILED; | 173 status2 = SERVICE_WORKER_ERROR_FAILED; |
| 176 status3 = SERVICE_WORKER_ERROR_FAILED; | 174 status3 = SERVICE_WORKER_ERROR_FAILED; |
| 177 version_->StopWorker(CreateReceiverOnCurrentThread(&status1)); | 175 version_->StopWorker(CreateReceiverOnCurrentThread(&status1)); |
| 178 version_->StopWorker(CreateReceiverOnCurrentThread(&status2)); | 176 version_->StopWorker(CreateReceiverOnCurrentThread(&status2)); |
| 179 | 177 |
| 180 // Also try calling StartWorker while StopWorker is in queue. | 178 // Also try calling StartWorker while StopWorker is in queue. |
| 181 version_->StartWorker(CreateReceiverOnCurrentThread(&status3)); | 179 version_->StartWorker(CreateReceiverOnCurrentThread(&status3), no_processes); |
| 182 | 180 |
| 183 EXPECT_EQ(ServiceWorkerVersion::STOPPING, version_->running_status()); | 181 EXPECT_EQ(ServiceWorkerVersion::STOPPING, version_->running_status()); |
| 184 base::RunLoop().RunUntilIdle(); | 182 base::RunLoop().RunUntilIdle(); |
| 185 EXPECT_EQ(ServiceWorkerVersion::STOPPED, version_->running_status()); | 183 EXPECT_EQ(ServiceWorkerVersion::STOPPED, version_->running_status()); |
| 186 | 184 |
| 187 // All StopWorker should just succeed, while StartWorker fails. | 185 // All StopWorker should just succeed, while StartWorker fails. |
| 188 EXPECT_EQ(SERVICE_WORKER_OK, status1); | 186 EXPECT_EQ(SERVICE_WORKER_OK, status1); |
| 189 EXPECT_EQ(SERVICE_WORKER_OK, status2); | 187 EXPECT_EQ(SERVICE_WORKER_OK, status2); |
| 190 EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status3); | 188 EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status3); |
| 191 } | 189 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 215 // SendMessage should return START_WORKER_FAILED error since it tried to | 213 // SendMessage should return START_WORKER_FAILED error since it tried to |
| 216 // start a worker while it was stopping. | 214 // start a worker while it was stopping. |
| 217 EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, msg_status); | 215 EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, msg_status); |
| 218 } | 216 } |
| 219 | 217 |
| 220 TEST_F(ServiceWorkerVersionTest, ReSendMessageAfterStop) { | 218 TEST_F(ServiceWorkerVersionTest, ReSendMessageAfterStop) { |
| 221 EXPECT_EQ(ServiceWorkerVersion::STOPPED, version_->running_status()); | 219 EXPECT_EQ(ServiceWorkerVersion::STOPPED, version_->running_status()); |
| 222 | 220 |
| 223 // Start the worker. | 221 // Start the worker. |
| 224 ServiceWorkerStatusCode start_status = SERVICE_WORKER_ERROR_FAILED; | 222 ServiceWorkerStatusCode start_status = SERVICE_WORKER_ERROR_FAILED; |
| 225 version_->StartWorker(CreateReceiverOnCurrentThread(&start_status)); | 223 version_->StartWorker(CreateReceiverOnCurrentThread(&start_status), |
| 224 std::vector<int>()); |
| 226 base::RunLoop().RunUntilIdle(); | 225 base::RunLoop().RunUntilIdle(); |
| 227 EXPECT_EQ(SERVICE_WORKER_OK, start_status); | 226 EXPECT_EQ(SERVICE_WORKER_OK, start_status); |
| 228 EXPECT_EQ(ServiceWorkerVersion::RUNNING, version_->running_status()); | 227 EXPECT_EQ(ServiceWorkerVersion::RUNNING, version_->running_status()); |
| 229 | 228 |
| 230 // Stop the worker, and then send the message immediately. | 229 // Stop the worker, and then send the message immediately. |
| 231 ServiceWorkerStatusCode msg_status = SERVICE_WORKER_ERROR_FAILED; | 230 ServiceWorkerStatusCode msg_status = SERVICE_WORKER_ERROR_FAILED; |
| 232 ServiceWorkerStatusCode stop_status = SERVICE_WORKER_ERROR_FAILED; | 231 ServiceWorkerStatusCode stop_status = SERVICE_WORKER_ERROR_FAILED; |
| 233 version_->StopWorker(CreateReceiverOnCurrentThread(&stop_status)); | 232 version_->StopWorker(CreateReceiverOnCurrentThread(&stop_status)); |
| 234 version_->SendMessage(TestMsg_Message(), | 233 version_->SendMessage(TestMsg_Message(), |
| 235 CreateReceiverOnCurrentThread(&msg_status)); | 234 CreateReceiverOnCurrentThread(&msg_status)); |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 // multiple times should be handled correctly. | 348 // multiple times should be handled correctly. |
| 350 ASSERT_TRUE(version_->HasProcessToRun()); | 349 ASSERT_TRUE(version_->HasProcessToRun()); |
| 351 | 350 |
| 352 // Removing the process again (so that # of AddProcess == # of RemoveProcess | 351 // Removing the process again (so that # of AddProcess == # of RemoveProcess |
| 353 // for the process) should remove all process references. | 352 // for the process) should remove all process references. |
| 354 version_->RemoveProcessFromWorker(another_process_id); | 353 version_->RemoveProcessFromWorker(another_process_id); |
| 355 ASSERT_FALSE(version_->HasProcessToRun()); | 354 ASSERT_FALSE(version_->HasProcessToRun()); |
| 356 } | 355 } |
| 357 | 356 |
| 358 } // namespace content | 357 } // namespace content |
| OLD | NEW |