Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: content/browser/service_worker/service_worker_version_unittest.cc

Issue 369063004: ServiceWorker: SWRegisterJob should manage status of SWVersion (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix ServiceWorkerVersionBrowserTest Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 helper_->SimulateSendValueToBrowser( 254 helper_->SimulateSendValueToBrowser(
255 version_->embedded_worker()->embedded_worker_id(), 777); 255 version_->embedded_worker()->embedded_worker_id(), 777);
256 256
257 // Verify the receiver received the values. 257 // Verify the receiver received the values.
258 ASSERT_EQ(2U, receiver.received_values().size()); 258 ASSERT_EQ(2U, receiver.received_values().size());
259 EXPECT_EQ(555, receiver.received_values()[0]); 259 EXPECT_EQ(555, receiver.received_values()[0]);
260 EXPECT_EQ(777, receiver.received_values()[1]); 260 EXPECT_EQ(777, receiver.received_values()[1]);
261 } 261 }
262 262
263 TEST_F(ServiceWorkerVersionTest, InstallAndWaitCompletion) { 263 TEST_F(ServiceWorkerVersionTest, InstallAndWaitCompletion) {
264 EXPECT_EQ(ServiceWorkerVersion::NEW, version_->status()); 264 version_->SetStatus(ServiceWorkerVersion::INSTALLING);
265 265
266 // Dispatch an install event. 266 // Dispatch an install event.
267 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; 267 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
268 version_->DispatchInstallEvent(-1, CreateReceiverOnCurrentThread(&status)); 268 version_->DispatchInstallEvent(-1, CreateReceiverOnCurrentThread(&status));
269 269
270 // Wait for the completion. 270 // Wait for the completion.
271 bool status_change_called = false; 271 bool status_change_called = false;
272 version_->RegisterStatusChangeCallback( 272 version_->RegisterStatusChangeCallback(
273 base::Bind(&VerifyCalled, &status_change_called)); 273 base::Bind(&VerifyCalled, &status_change_called));
274 274
275 base::RunLoop().RunUntilIdle(); 275 base::RunLoop().RunUntilIdle();
276 276
277 // After successful completion, version's status must be changed to 277 // Version's status must not have changed during installation.
278 // INSTALLED, and status change callback must have been fired.
279 EXPECT_EQ(SERVICE_WORKER_OK, status); 278 EXPECT_EQ(SERVICE_WORKER_OK, status);
280 EXPECT_TRUE(status_change_called); 279 EXPECT_FALSE(status_change_called);
281 EXPECT_EQ(ServiceWorkerVersion::INSTALLED, version_->status()); 280 EXPECT_EQ(ServiceWorkerVersion::INSTALLING, version_->status());
282 } 281 }
283 282
284 TEST_F(ServiceWorkerVersionTest, ActivateAndWaitCompletion) { 283 TEST_F(ServiceWorkerVersionTest, ActivateAndWaitCompletion) {
285 version_->SetStatus(ServiceWorkerVersion::INSTALLED); 284 version_->SetStatus(ServiceWorkerVersion::ACTIVATING);
286 EXPECT_EQ(ServiceWorkerVersion::INSTALLED, version_->status());
287 285
288 // Dispatch an activate event. 286 // Dispatch an activate event.
289 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; 287 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
290 version_->DispatchActivateEvent(CreateReceiverOnCurrentThread(&status)); 288 version_->DispatchActivateEvent(CreateReceiverOnCurrentThread(&status));
291 289
292 // Wait for the completion. 290 // Wait for the completion.
293 bool status_change_called = false; 291 bool status_change_called = false;
294 version_->RegisterStatusChangeCallback( 292 version_->RegisterStatusChangeCallback(
295 base::Bind(&VerifyCalled, &status_change_called)); 293 base::Bind(&VerifyCalled, &status_change_called));
296 294
297 base::RunLoop().RunUntilIdle(); 295 base::RunLoop().RunUntilIdle();
298 296
299 // After successful completion, version's status must be changed to 297 // Version's status must not have changed during activation.
300 // ACTIVE, and status change callback must have been fired.
301 EXPECT_EQ(SERVICE_WORKER_OK, status); 298 EXPECT_EQ(SERVICE_WORKER_OK, status);
302 EXPECT_TRUE(status_change_called); 299 EXPECT_FALSE(status_change_called);
303 EXPECT_EQ(ServiceWorkerVersion::ACTIVE, version_->status()); 300 EXPECT_EQ(ServiceWorkerVersion::ACTIVATING, version_->status());
304 }
305
306 TEST_F(ServiceWorkerVersionTest, RepeatedlyObserveStatusChanges) {
michaeln 2014/07/07 21:56:37 why remove this test? this test was added to veri
nhiroki 2014/07/07 23:07:40 Revived and updated this test. Let me confirm my
307 EXPECT_EQ(ServiceWorkerVersion::NEW, version_->status());
308
309 // Repeatedly observe status changes (the callback re-registers itself).
310 std::vector<ServiceWorkerVersion::Status> statuses;
311 version_->RegisterStatusChangeCallback(
312 base::Bind(&ObserveStatusChanges, version_, &statuses));
313
314 // Dispatch some events.
315 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
316 version_->DispatchInstallEvent(-1, CreateReceiverOnCurrentThread(&status));
317 base::RunLoop().RunUntilIdle();
318 EXPECT_EQ(SERVICE_WORKER_OK, status);
319
320 status = SERVICE_WORKER_ERROR_FAILED;
321 version_->DispatchActivateEvent(CreateReceiverOnCurrentThread(&status));
322 base::RunLoop().RunUntilIdle();
323 EXPECT_EQ(SERVICE_WORKER_OK, status);
324
325 // Verify that we could successfully observe repeated status changes.
326 ASSERT_EQ(4U, statuses.size());
327 ASSERT_EQ(ServiceWorkerVersion::INSTALLING, statuses[0]);
328 ASSERT_EQ(ServiceWorkerVersion::INSTALLED, statuses[1]);
329 ASSERT_EQ(ServiceWorkerVersion::ACTIVATING, statuses[2]);
330 ASSERT_EQ(ServiceWorkerVersion::ACTIVE, statuses[3]);
331 } 301 }
332 302
333 TEST_F(ServiceWorkerVersionTest, AddAndRemoveProcesses) { 303 TEST_F(ServiceWorkerVersionTest, AddAndRemoveProcesses) {
334 // Preparation (to reset the process count to 0). 304 // Preparation (to reset the process count to 0).
335 ASSERT_TRUE(version_->HasProcessToRun()); 305 ASSERT_TRUE(version_->HasProcessToRun());
336 version_->RemoveProcessFromWorker(kRenderProcessId); 306 version_->RemoveProcessFromWorker(kRenderProcessId);
337 ASSERT_FALSE(version_->HasProcessToRun()); 307 ASSERT_FALSE(version_->HasProcessToRun());
338 308
339 // Add another process to the worker twice, and then remove process once. 309 // Add another process to the worker twice, and then remove process once.
340 const int another_process_id = kRenderProcessId + 1; 310 const int another_process_id = kRenderProcessId + 1;
341 version_->AddProcessToWorker(another_process_id); 311 version_->AddProcessToWorker(another_process_id);
342 version_->AddProcessToWorker(another_process_id); 312 version_->AddProcessToWorker(another_process_id);
343 version_->RemoveProcessFromWorker(another_process_id); 313 version_->RemoveProcessFromWorker(another_process_id);
344 314
345 // We're ref-counting the process internally, so adding the same process 315 // We're ref-counting the process internally, so adding the same process
346 // multiple times should be handled correctly. 316 // multiple times should be handled correctly.
347 ASSERT_TRUE(version_->HasProcessToRun()); 317 ASSERT_TRUE(version_->HasProcessToRun());
348 318
349 // Removing the process again (so that # of AddProcess == # of RemoveProcess 319 // Removing the process again (so that # of AddProcess == # of RemoveProcess
350 // for the process) should remove all process references. 320 // for the process) should remove all process references.
351 version_->RemoveProcessFromWorker(another_process_id); 321 version_->RemoveProcessFromWorker(another_process_id);
352 ASSERT_FALSE(version_->HasProcessToRun()); 322 ASSERT_FALSE(version_->HasProcessToRun());
353 } 323 }
354 324
355 } // namespace content 325 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698