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 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 helper_.reset(new MessageReceiver()); | 120 helper_.reset(new MessageReceiver()); |
121 | 121 |
| 122 pattern_ = GURL("http://www.example.com/"); |
122 registration_ = new ServiceWorkerRegistration( | 123 registration_ = new ServiceWorkerRegistration( |
123 GURL("http://www.example.com/"), | 124 pattern_, |
124 1L, | 125 1L, |
125 helper_->context()->AsWeakPtr()); | 126 helper_->context()->AsWeakPtr()); |
126 version_ = new ServiceWorkerVersion( | 127 version_ = new ServiceWorkerVersion( |
127 registration_.get(), | 128 registration_.get(), |
128 GURL("http://www.example.com/service_worker.js"), | 129 GURL("http://www.example.com/service_worker.js"), |
129 1L, | 130 1L, |
130 helper_->context()->AsWeakPtr()); | 131 helper_->context()->AsWeakPtr()); |
131 | 132 |
132 // Simulate adding one process to the worker. | 133 // Simulate adding one process to the pattern. |
133 int embedded_worker_id = version_->embedded_worker()->embedded_worker_id(); | 134 helper_->SimulateAddProcessToPattern(pattern_, kRenderProcessId); |
134 helper_->SimulateAddProcessToWorker(embedded_worker_id, kRenderProcessId); | 135 ASSERT_TRUE(helper_->context()->process_manager() |
135 ASSERT_TRUE(version_->HasProcessToRun()); | 136 ->PatternHasProcessToRun(pattern_)); |
136 } | 137 } |
137 | 138 |
138 virtual void TearDown() OVERRIDE { | 139 virtual void TearDown() OVERRIDE { |
139 version_ = 0; | 140 version_ = 0; |
140 registration_ = 0; | 141 registration_ = 0; |
141 helper_.reset(); | 142 helper_.reset(); |
142 } | 143 } |
143 | 144 |
144 TestBrowserThreadBundle thread_bundle_; | 145 TestBrowserThreadBundle thread_bundle_; |
145 scoped_ptr<MessageReceiver> helper_; | 146 scoped_ptr<MessageReceiver> helper_; |
146 scoped_refptr<ServiceWorkerRegistration> registration_; | 147 scoped_refptr<ServiceWorkerRegistration> registration_; |
147 scoped_refptr<ServiceWorkerVersion> version_; | 148 scoped_refptr<ServiceWorkerVersion> version_; |
| 149 GURL pattern_; |
148 | 150 |
149 private: | 151 private: |
150 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersionTest); | 152 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersionTest); |
151 }; | 153 }; |
152 | 154 |
153 TEST_F(ServiceWorkerVersionTest, ConcurrentStartAndStop) { | 155 TEST_F(ServiceWorkerVersionTest, ConcurrentStartAndStop) { |
154 // Call StartWorker() multiple times. | 156 // Call StartWorker() multiple times. |
155 ServiceWorkerStatusCode status1 = SERVICE_WORKER_ERROR_FAILED; | 157 ServiceWorkerStatusCode status1 = SERVICE_WORKER_ERROR_FAILED; |
156 ServiceWorkerStatusCode status2 = SERVICE_WORKER_ERROR_FAILED; | 158 ServiceWorkerStatusCode status2 = SERVICE_WORKER_ERROR_FAILED; |
157 ServiceWorkerStatusCode status3 = SERVICE_WORKER_ERROR_FAILED; | 159 ServiceWorkerStatusCode status3 = SERVICE_WORKER_ERROR_FAILED; |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 | 322 |
321 // Verify that we could successfully observe repeated status changes. | 323 // Verify that we could successfully observe repeated status changes. |
322 ASSERT_EQ(5U, statuses.size()); | 324 ASSERT_EQ(5U, statuses.size()); |
323 ASSERT_EQ(ServiceWorkerVersion::INSTALLING, statuses[0]); | 325 ASSERT_EQ(ServiceWorkerVersion::INSTALLING, statuses[0]); |
324 ASSERT_EQ(ServiceWorkerVersion::INSTALLED, statuses[1]); | 326 ASSERT_EQ(ServiceWorkerVersion::INSTALLED, statuses[1]); |
325 ASSERT_EQ(ServiceWorkerVersion::ACTIVATING, statuses[2]); | 327 ASSERT_EQ(ServiceWorkerVersion::ACTIVATING, statuses[2]); |
326 ASSERT_EQ(ServiceWorkerVersion::ACTIVATED, statuses[3]); | 328 ASSERT_EQ(ServiceWorkerVersion::ACTIVATED, statuses[3]); |
327 ASSERT_EQ(ServiceWorkerVersion::REDUNDANT, statuses[4]); | 329 ASSERT_EQ(ServiceWorkerVersion::REDUNDANT, statuses[4]); |
328 } | 330 } |
329 | 331 |
330 TEST_F(ServiceWorkerVersionTest, AddAndRemoveProcesses) { | |
331 // Preparation (to reset the process count to 0). | |
332 ASSERT_TRUE(version_->HasProcessToRun()); | |
333 version_->RemoveProcessFromWorker(kRenderProcessId); | |
334 ASSERT_FALSE(version_->HasProcessToRun()); | |
335 | |
336 // Add another process to the worker twice, and then remove process once. | |
337 const int another_process_id = kRenderProcessId + 1; | |
338 version_->AddProcessToWorker(another_process_id); | |
339 version_->AddProcessToWorker(another_process_id); | |
340 version_->RemoveProcessFromWorker(another_process_id); | |
341 | |
342 // We're ref-counting the process internally, so adding the same process | |
343 // multiple times should be handled correctly. | |
344 ASSERT_TRUE(version_->HasProcessToRun()); | |
345 | |
346 // Removing the process again (so that # of AddProcess == # of RemoveProcess | |
347 // for the process) should remove all process references. | |
348 version_->RemoveProcessFromWorker(another_process_id); | |
349 ASSERT_FALSE(version_->HasProcessToRun()); | |
350 } | |
351 | |
352 TEST_F(ServiceWorkerVersionTest, ScheduleStopWorker) { | 332 TEST_F(ServiceWorkerVersionTest, ScheduleStopWorker) { |
353 // Verify the timer is not running when version initializes its status. | 333 // Verify the timer is not running when version initializes its status. |
354 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); | 334 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); |
355 EXPECT_FALSE(version_->stop_worker_timer_.IsRunning()); | 335 EXPECT_FALSE(version_->stop_worker_timer_.IsRunning()); |
356 | 336 |
357 // Verify the timer is running when version status changes frome ACTIVATING | 337 // Verify the timer is running when version status changes frome ACTIVATING |
358 // to ACTIVATED. | 338 // to ACTIVATED. |
359 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; | 339 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; |
360 version_->StartWorker(CreateReceiverOnCurrentThread(&status)); | 340 version_->StartWorker(CreateReceiverOnCurrentThread(&status)); |
361 base::RunLoop().RunUntilIdle(); | 341 base::RunLoop().RunUntilIdle(); |
(...skipping 21 matching lines...) Expand all Loading... |
383 NULL)); | 363 NULL)); |
384 version_->AddControllee(host.get()); | 364 version_->AddControllee(host.get()); |
385 EXPECT_FALSE(version_->stop_worker_timer_.IsRunning()); | 365 EXPECT_FALSE(version_->stop_worker_timer_.IsRunning()); |
386 | 366 |
387 // The timer should be running if the controllee is removed. | 367 // The timer should be running if the controllee is removed. |
388 version_->RemoveControllee(host.get()); | 368 version_->RemoveControllee(host.get()); |
389 EXPECT_TRUE(version_->stop_worker_timer_.IsRunning()); | 369 EXPECT_TRUE(version_->stop_worker_timer_.IsRunning()); |
390 } | 370 } |
391 | 371 |
392 } // namespace content | 372 } // namespace content |
OLD | NEW |