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
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
6 #include "base/run_loop.h"
7 #include "content/browser/message_port_service.h"
8 #include "content/browser/service_worker/embedded_worker_registry.h"
9 #include "content/browser/service_worker/embedded_worker_test_helper.h"
10 #include "content/browser/service_worker/service_worker_context_core.h"
11 #include "content/browser/service_worker/service_worker_registration.h"
12 #include "content/browser/service_worker/service_worker_test_utils.h"
13 #include "content/browser/service_worker/service_worker_utils.h"
14 #include "content/browser/service_worker/service_worker_version.h"
15 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 // IPC messages for testing ---------------------------------------------------
20 #define IPC_MESSAGE_IMPL
21 #include "ipc/ipc_message_macros.h"
23 #define IPC_MESSAGE_START TestMsgStart
25 IPC_MESSAGE_CONTROL0(TestMsg_Message
);
26 IPC_MESSAGE_ROUTED1(TestMsg_MessageFromWorker
, int);
28 // ---------------------------------------------------------------------------
34 static const int kRenderProcessId
= 1;
36 class MessageReceiver
: public EmbeddedWorkerTestHelper
{
39 : EmbeddedWorkerTestHelper(base::FilePath(), kRenderProcessId
),
40 current_embedded_worker_id_(0) {}
41 ~MessageReceiver() override
{}
43 bool OnMessageToWorker(int thread_id
,
44 int embedded_worker_id
,
45 const IPC::Message
& message
) override
{
46 if (EmbeddedWorkerTestHelper::OnMessageToWorker(
47 thread_id
, embedded_worker_id
, message
)) {
50 current_embedded_worker_id_
= embedded_worker_id
;
52 IPC_BEGIN_MESSAGE_MAP(MessageReceiver
, message
)
53 IPC_MESSAGE_HANDLER(TestMsg_Message
, OnMessage
)
54 IPC_MESSAGE_UNHANDLED(handled
= false)
59 void SimulateSendValueToBrowser(int embedded_worker_id
, int value
) {
60 SimulateSend(new TestMsg_MessageFromWorker(embedded_worker_id
, value
));
68 int current_embedded_worker_id_
;
69 DISALLOW_COPY_AND_ASSIGN(MessageReceiver
);
72 void VerifyCalled(bool* called
) {
76 void ObserveStatusChanges(ServiceWorkerVersion
* version
,
77 std::vector
<ServiceWorkerVersion::Status
>* statuses
) {
78 statuses
->push_back(version
->status());
79 version
->RegisterStatusChangeCallback(
80 base::Bind(&ObserveStatusChanges
, base::Unretained(version
), statuses
));
83 void ReceiveFetchResult(ServiceWorkerStatusCode
* status
,
84 ServiceWorkerStatusCode actual_status
,
85 ServiceWorkerFetchEventResult actual_result
,
86 const ServiceWorkerResponse
& response
) {
87 *status
= actual_status
;
90 // A specialized listener class to receive test messages from a worker.
91 class MessageReceiverFromWorker
: public EmbeddedWorkerInstance::Listener
{
93 explicit MessageReceiverFromWorker(EmbeddedWorkerInstance
* instance
)
94 : instance_(instance
) {
95 instance_
->AddListener(this);
97 ~MessageReceiverFromWorker() override
{ instance_
->RemoveListener(this); }
99 void OnStarted() override
{ NOTREACHED(); }
100 void OnStopped(EmbeddedWorkerInstance::Status old_status
) override
{
103 bool OnMessageReceived(const IPC::Message
& message
) override
{
105 IPC_BEGIN_MESSAGE_MAP(MessageReceiverFromWorker
, message
)
106 IPC_MESSAGE_HANDLER(TestMsg_MessageFromWorker
, OnMessageFromWorker
)
107 IPC_MESSAGE_UNHANDLED(handled
= false)
108 IPC_END_MESSAGE_MAP()
112 void OnMessageFromWorker(int value
) { received_values_
.push_back(value
); }
113 const std::vector
<int>& received_values() const { return received_values_
; }
116 EmbeddedWorkerInstance
* instance_
;
117 std::vector
<int> received_values_
;
118 DISALLOW_COPY_AND_ASSIGN(MessageReceiverFromWorker
);
121 void SetUpDummyMessagePort(std::vector
<TransferredMessagePort
>* ports
) {
123 MessagePortService::GetInstance()->Create(MSG_ROUTING_NONE
, nullptr,
125 TransferredMessagePort dummy_port
;
126 dummy_port
.id
= port_id
;
127 ports
->push_back(dummy_port
);
132 class ServiceWorkerVersionTest
: public testing::Test
{
134 struct RunningStateListener
: public ServiceWorkerVersion::Listener
{
135 RunningStateListener() : last_status(ServiceWorkerVersion::STOPPED
) {}
136 ~RunningStateListener() override
{}
137 void OnRunningStateChanged(ServiceWorkerVersion
* version
) override
{
138 last_status
= version
->running_status();
140 ServiceWorkerVersion::RunningStatus last_status
;
143 ServiceWorkerVersionTest()
144 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP
) {}
146 void SetUp() override
{
147 helper_
= GetMessageReceiver();
149 pattern_
= GURL("http://www.example.com/");
150 registration_
= new ServiceWorkerRegistration(
153 helper_
->context()->AsWeakPtr());
154 version_
= new ServiceWorkerVersion(
156 GURL("http://www.example.com/service_worker.js"),
158 helper_
->context()->AsWeakPtr());
159 std::vector
<ServiceWorkerDatabase::ResourceRecord
> records
;
161 ServiceWorkerDatabase::ResourceRecord(10, version_
->script_url(), 100));
162 version_
->script_cache_map()->SetResources(records
);
164 // Make the registration findable via storage functions.
165 helper_
->context()->storage()->LazyInitialize(base::Bind(&base::DoNothing
));
166 base::RunLoop().RunUntilIdle();
167 ServiceWorkerStatusCode status
= SERVICE_WORKER_ERROR_FAILED
;
168 helper_
->context()->storage()->StoreRegistration(
171 CreateReceiverOnCurrentThread(&status
));
172 base::RunLoop().RunUntilIdle();
173 ASSERT_EQ(SERVICE_WORKER_OK
, status
);
175 // Simulate adding one process to the pattern.
176 helper_
->SimulateAddProcessToPattern(pattern_
, kRenderProcessId
);
177 ASSERT_TRUE(helper_
->context()->process_manager()
178 ->PatternHasProcessToRun(pattern_
));
181 virtual scoped_ptr
<MessageReceiver
> GetMessageReceiver() {
182 return make_scoped_ptr(new MessageReceiver());
185 void TearDown() override
{
191 TestBrowserThreadBundle thread_bundle_
;
192 scoped_ptr
<MessageReceiver
> helper_
;
193 scoped_refptr
<ServiceWorkerRegistration
> registration_
;
194 scoped_refptr
<ServiceWorkerVersion
> version_
;
198 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersionTest
);
201 class MessageReceiverDisallowStart
: public MessageReceiver
{
203 MessageReceiverDisallowStart()
204 : MessageReceiver() {}
205 ~MessageReceiverDisallowStart() override
{}
207 void OnStartWorker(int embedded_worker_id
,
208 int64 service_worker_version_id
,
210 const GURL
& script_url
,
211 bool pause_after_download
) override
{
216 DISALLOW_COPY_AND_ASSIGN(MessageReceiverDisallowStart
);
219 class ServiceWorkerFailToStartTest
: public ServiceWorkerVersionTest
{
221 ServiceWorkerFailToStartTest()
222 : ServiceWorkerVersionTest() {}
224 scoped_ptr
<MessageReceiver
> GetMessageReceiver() override
{
225 return make_scoped_ptr(new MessageReceiverDisallowStart());
229 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerFailToStartTest
);
232 class MessageReceiverDisallowFetch
: public MessageReceiver
{
234 MessageReceiverDisallowFetch() : MessageReceiver() {}
235 ~MessageReceiverDisallowFetch() override
{}
237 void OnFetchEvent(int embedded_worker_id
,
239 const ServiceWorkerFetchRequest
& request
) override
{
244 DISALLOW_COPY_AND_ASSIGN(MessageReceiverDisallowFetch
);
247 class ServiceWorkerWaitForeverInFetchTest
: public ServiceWorkerVersionTest
{
249 ServiceWorkerWaitForeverInFetchTest() : ServiceWorkerVersionTest() {}
251 scoped_ptr
<MessageReceiver
> GetMessageReceiver() override
{
252 return make_scoped_ptr(new MessageReceiverDisallowFetch());
256 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerWaitForeverInFetchTest
);
259 TEST_F(ServiceWorkerVersionTest
, ConcurrentStartAndStop
) {
260 // Call StartWorker() multiple times.
261 ServiceWorkerStatusCode status1
= SERVICE_WORKER_ERROR_FAILED
;
262 ServiceWorkerStatusCode status2
= SERVICE_WORKER_ERROR_FAILED
;
263 ServiceWorkerStatusCode status3
= SERVICE_WORKER_ERROR_FAILED
;
264 version_
->StartWorker(CreateReceiverOnCurrentThread(&status1
));
265 version_
->StartWorker(CreateReceiverOnCurrentThread(&status2
));
267 EXPECT_EQ(ServiceWorkerVersion::STARTING
, version_
->running_status());
268 base::RunLoop().RunUntilIdle();
269 EXPECT_EQ(ServiceWorkerVersion::RUNNING
, version_
->running_status());
271 // Call StartWorker() after it's started.
272 version_
->StartWorker(CreateReceiverOnCurrentThread(&status3
));
273 base::RunLoop().RunUntilIdle();
275 // All should just succeed.
276 EXPECT_EQ(SERVICE_WORKER_OK
, status1
);
277 EXPECT_EQ(SERVICE_WORKER_OK
, status2
);
278 EXPECT_EQ(SERVICE_WORKER_OK
, status3
);
280 // Call StopWorker() multiple times.
281 status1
= SERVICE_WORKER_ERROR_FAILED
;
282 status2
= SERVICE_WORKER_ERROR_FAILED
;
283 version_
->StopWorker(CreateReceiverOnCurrentThread(&status1
));
284 version_
->StopWorker(CreateReceiverOnCurrentThread(&status2
));
286 EXPECT_EQ(ServiceWorkerVersion::STOPPING
, version_
->running_status());
287 base::RunLoop().RunUntilIdle();
288 EXPECT_EQ(ServiceWorkerVersion::STOPPED
, version_
->running_status());
290 // All StopWorker should just succeed.
291 EXPECT_EQ(SERVICE_WORKER_OK
, status1
);
292 EXPECT_EQ(SERVICE_WORKER_OK
, status2
);
294 // Start worker again.
295 status1
= SERVICE_WORKER_ERROR_FAILED
;
296 status2
= SERVICE_WORKER_ERROR_FAILED
;
297 status3
= SERVICE_WORKER_ERROR_FAILED
;
299 version_
->StartWorker(CreateReceiverOnCurrentThread(&status1
));
301 EXPECT_EQ(ServiceWorkerVersion::STARTING
, version_
->running_status());
302 base::RunLoop().RunUntilIdle();
303 EXPECT_EQ(ServiceWorkerVersion::RUNNING
, version_
->running_status());
306 status2
= SERVICE_WORKER_ERROR_FAILED
;
307 version_
->StopWorker(CreateReceiverOnCurrentThread(&status2
));
309 // And try calling StartWorker while StopWorker is in queue.
310 version_
->StartWorker(CreateReceiverOnCurrentThread(&status3
));
312 EXPECT_EQ(ServiceWorkerVersion::STOPPING
, version_
->running_status());
313 base::RunLoop().RunUntilIdle();
314 EXPECT_EQ(ServiceWorkerVersion::RUNNING
, version_
->running_status());
316 // All should just succeed.
317 EXPECT_EQ(SERVICE_WORKER_OK
, status1
);
318 EXPECT_EQ(SERVICE_WORKER_OK
, status2
);
319 EXPECT_EQ(SERVICE_WORKER_OK
, status3
);
322 TEST_F(ServiceWorkerVersionTest
, DispatchEventToStoppedWorker
) {
323 EXPECT_EQ(ServiceWorkerVersion::STOPPED
, version_
->running_status());
325 // Dispatch an event without starting the worker.
326 ServiceWorkerStatusCode status
= SERVICE_WORKER_ERROR_FAILED
;
327 version_
->SetStatus(ServiceWorkerVersion::INSTALLING
);
328 version_
->DispatchInstallEvent(CreateReceiverOnCurrentThread(&status
));
329 base::RunLoop().RunUntilIdle();
330 EXPECT_EQ(SERVICE_WORKER_OK
, status
);
332 // The worker should be now started.
333 EXPECT_EQ(ServiceWorkerVersion::RUNNING
, version_
->running_status());
335 // Stop the worker, and then dispatch an event immediately after that.
336 status
= SERVICE_WORKER_ERROR_FAILED
;
337 ServiceWorkerStatusCode stop_status
= SERVICE_WORKER_ERROR_FAILED
;
338 version_
->StopWorker(CreateReceiverOnCurrentThread(&stop_status
));
339 version_
->DispatchInstallEvent(CreateReceiverOnCurrentThread(&status
));
340 base::RunLoop().RunUntilIdle();
341 EXPECT_EQ(SERVICE_WORKER_OK
, stop_status
);
343 // Dispatch an event should return SERVICE_WORKER_OK since the worker
344 // should have been restarted to dispatch the event.
345 EXPECT_EQ(SERVICE_WORKER_OK
, status
);
347 // The worker should be now started again.
348 EXPECT_EQ(ServiceWorkerVersion::RUNNING
, version_
->running_status());
351 TEST_F(ServiceWorkerVersionTest
, ReceiveMessageFromWorker
) {
353 ServiceWorkerStatusCode status
= SERVICE_WORKER_ERROR_FAILED
;
354 version_
->StartWorker(CreateReceiverOnCurrentThread(&status
));
355 EXPECT_EQ(ServiceWorkerVersion::STARTING
, version_
->running_status());
356 base::RunLoop().RunUntilIdle();
357 EXPECT_EQ(SERVICE_WORKER_OK
, status
);
358 EXPECT_EQ(ServiceWorkerVersion::RUNNING
, version_
->running_status());
360 MessageReceiverFromWorker
receiver(version_
->embedded_worker());
362 // Simulate sending some dummy values from the worker.
363 helper_
->SimulateSendValueToBrowser(
364 version_
->embedded_worker()->embedded_worker_id(), 555);
365 helper_
->SimulateSendValueToBrowser(
366 version_
->embedded_worker()->embedded_worker_id(), 777);
368 // Verify the receiver received the values.
369 ASSERT_EQ(2U, receiver
.received_values().size());
370 EXPECT_EQ(555, receiver
.received_values()[0]);
371 EXPECT_EQ(777, receiver
.received_values()[1]);
374 TEST_F(ServiceWorkerVersionTest
, InstallAndWaitCompletion
) {
375 version_
->SetStatus(ServiceWorkerVersion::INSTALLING
);
377 // Dispatch an install event.
378 ServiceWorkerStatusCode status
= SERVICE_WORKER_ERROR_FAILED
;
379 version_
->DispatchInstallEvent(CreateReceiverOnCurrentThread(&status
));
381 // Wait for the completion.
382 bool status_change_called
= false;
383 version_
->RegisterStatusChangeCallback(
384 base::Bind(&VerifyCalled
, &status_change_called
));
386 base::RunLoop().RunUntilIdle();
388 // Version's status must not have changed during installation.
389 EXPECT_EQ(SERVICE_WORKER_OK
, status
);
390 EXPECT_FALSE(status_change_called
);
391 EXPECT_EQ(ServiceWorkerVersion::INSTALLING
, version_
->status());
394 TEST_F(ServiceWorkerVersionTest
, ActivateAndWaitCompletion
) {
395 version_
->SetStatus(ServiceWorkerVersion::ACTIVATING
);
397 // Dispatch an activate event.
398 ServiceWorkerStatusCode status
= SERVICE_WORKER_ERROR_FAILED
;
399 version_
->DispatchActivateEvent(CreateReceiverOnCurrentThread(&status
));
401 // Wait for the completion.
402 bool status_change_called
= false;
403 version_
->RegisterStatusChangeCallback(
404 base::Bind(&VerifyCalled
, &status_change_called
));
406 base::RunLoop().RunUntilIdle();
408 // Version's status must not have changed during activation.
409 EXPECT_EQ(SERVICE_WORKER_OK
, status
);
410 EXPECT_FALSE(status_change_called
);
411 EXPECT_EQ(ServiceWorkerVersion::ACTIVATING
, version_
->status());
414 TEST_F(ServiceWorkerVersionTest
, RepeatedlyObserveStatusChanges
) {
415 EXPECT_EQ(ServiceWorkerVersion::NEW
, version_
->status());
417 // Repeatedly observe status changes (the callback re-registers itself).
418 std::vector
<ServiceWorkerVersion::Status
> statuses
;
419 version_
->RegisterStatusChangeCallback(
420 base::Bind(&ObserveStatusChanges
, version_
, &statuses
));
422 version_
->SetStatus(ServiceWorkerVersion::INSTALLING
);
423 version_
->SetStatus(ServiceWorkerVersion::INSTALLED
);
424 version_
->SetStatus(ServiceWorkerVersion::ACTIVATING
);
425 version_
->SetStatus(ServiceWorkerVersion::ACTIVATED
);
426 version_
->SetStatus(ServiceWorkerVersion::REDUNDANT
);
428 // Verify that we could successfully observe repeated status changes.
429 ASSERT_EQ(5U, statuses
.size());
430 ASSERT_EQ(ServiceWorkerVersion::INSTALLING
, statuses
[0]);
431 ASSERT_EQ(ServiceWorkerVersion::INSTALLED
, statuses
[1]);
432 ASSERT_EQ(ServiceWorkerVersion::ACTIVATING
, statuses
[2]);
433 ASSERT_EQ(ServiceWorkerVersion::ACTIVATED
, statuses
[3]);
434 ASSERT_EQ(ServiceWorkerVersion::REDUNDANT
, statuses
[4]);
437 TEST_F(ServiceWorkerVersionTest
, IdleTimeout
) {
438 // Used to reliably test when the idle time gets reset regardless of clock
440 const base::TimeDelta kOneSecond
= base::TimeDelta::FromSeconds(1);
442 // Verify the timer is not running when version initializes its status.
443 version_
->SetStatus(ServiceWorkerVersion::ACTIVATED
);
444 EXPECT_FALSE(version_
->timeout_timer_
.IsRunning());
446 // Verify the timer is running after the worker is started.
447 ServiceWorkerStatusCode status
= SERVICE_WORKER_ERROR_FAILED
;
448 version_
->StartWorker(CreateReceiverOnCurrentThread(&status
));
449 base::RunLoop().RunUntilIdle();
450 EXPECT_EQ(SERVICE_WORKER_OK
, status
);
451 EXPECT_TRUE(version_
->timeout_timer_
.IsRunning());
452 EXPECT_FALSE(version_
->idle_time_
.is_null());
454 // The idle time should be reset if the worker is restarted without
456 status
= SERVICE_WORKER_ERROR_FAILED
;
457 version_
->idle_time_
-= kOneSecond
;
458 base::TimeTicks idle_time
= version_
->idle_time_
;
459 version_
->StopWorker(CreateReceiverOnCurrentThread(&status
));
460 base::RunLoop().RunUntilIdle();
461 EXPECT_EQ(SERVICE_WORKER_OK
, status
);
462 status
= SERVICE_WORKER_ERROR_FAILED
;
463 version_
->StartWorker(CreateReceiverOnCurrentThread(&status
));
464 base::RunLoop().RunUntilIdle();
465 EXPECT_EQ(SERVICE_WORKER_OK
, status
);
466 EXPECT_TRUE(version_
->timeout_timer_
.IsRunning());
467 EXPECT_LT(idle_time
, version_
->idle_time_
);
469 // Adding a controllee resets the idle time.
470 version_
->idle_time_
-= kOneSecond
;
471 idle_time
= version_
->idle_time_
;
472 scoped_ptr
<ServiceWorkerProviderHost
> host(new ServiceWorkerProviderHost(
473 33 /* dummy render process id */, MSG_ROUTING_NONE
/* render_frame_id */,
474 1 /* dummy provider_id */, SERVICE_WORKER_PROVIDER_FOR_WINDOW
,
475 helper_
->context()->AsWeakPtr(), NULL
));
476 version_
->AddControllee(host
.get());
477 EXPECT_TRUE(version_
->timeout_timer_
.IsRunning());
478 EXPECT_LT(idle_time
, version_
->idle_time_
);
480 // Completing an event resets the idle time.
481 status
= SERVICE_WORKER_ERROR_FAILED
;
482 version_
->idle_time_
-= kOneSecond
;
483 idle_time
= version_
->idle_time_
;
484 version_
->DispatchFetchEvent(ServiceWorkerFetchRequest(),
485 base::Bind(&base::DoNothing
),
486 base::Bind(&ReceiveFetchResult
, &status
));
487 base::RunLoop().RunUntilIdle();
489 EXPECT_EQ(SERVICE_WORKER_OK
, status
);
490 EXPECT_LT(idle_time
, version_
->idle_time_
);
492 // Dispatching a message event resets the idle time.
493 std::vector
<TransferredMessagePort
> ports
;
494 SetUpDummyMessagePort(&ports
);
495 status
= SERVICE_WORKER_ERROR_FAILED
;
496 version_
->idle_time_
-= kOneSecond
;
497 idle_time
= version_
->idle_time_
;
498 version_
->DispatchMessageEvent(base::string16(), ports
,
499 CreateReceiverOnCurrentThread(&status
));
500 base::RunLoop().RunUntilIdle();
501 MessagePortService::GetInstance()->Destroy(ports
[0].id
);
503 EXPECT_EQ(SERVICE_WORKER_OK
, status
);
504 EXPECT_LT(idle_time
, version_
->idle_time_
);
507 TEST_F(ServiceWorkerVersionTest
, SetDevToolsAttached
) {
508 ServiceWorkerStatusCode status
= SERVICE_WORKER_ERROR_FAILED
;
509 version_
->StartWorker(CreateReceiverOnCurrentThread(&status
));
511 ASSERT_EQ(ServiceWorkerVersion::STARTING
, version_
->running_status());
513 ASSERT_TRUE(version_
->timeout_timer_
.IsRunning());
514 ASSERT_FALSE(version_
->start_time_
.is_null());
515 ASSERT_FALSE(version_
->skip_recording_startup_time_
);
517 // Simulate DevTools is attached. This should deactivate the timer for start
518 // timeout, but not stop the timer itself.
519 version_
->SetDevToolsAttached(true);
520 EXPECT_TRUE(version_
->timeout_timer_
.IsRunning());
521 EXPECT_TRUE(version_
->start_time_
.is_null());
522 EXPECT_TRUE(version_
->skip_recording_startup_time_
);
524 // Simulate DevTools is detached. This should reactivate the timer for start
526 version_
->SetDevToolsAttached(false);
527 EXPECT_TRUE(version_
->timeout_timer_
.IsRunning());
528 EXPECT_FALSE(version_
->start_time_
.is_null());
529 EXPECT_TRUE(version_
->skip_recording_startup_time_
);
531 base::RunLoop().RunUntilIdle();
532 EXPECT_EQ(SERVICE_WORKER_OK
, status
);
533 EXPECT_EQ(ServiceWorkerVersion::RUNNING
, version_
->running_status());
536 TEST_F(ServiceWorkerVersionTest
, StoppingBeforeDestruct
) {
537 RunningStateListener listener
;
538 version_
->AddListener(&listener
);
540 ServiceWorkerStatusCode status
= SERVICE_WORKER_ERROR_FAILED
;
541 version_
->StartWorker(CreateReceiverOnCurrentThread(&status
));
542 base::RunLoop().RunUntilIdle();
543 EXPECT_EQ(SERVICE_WORKER_OK
, status
);
544 EXPECT_EQ(ServiceWorkerVersion::RUNNING
, version_
->running_status());
545 EXPECT_EQ(ServiceWorkerVersion::RUNNING
, listener
.last_status
);
548 EXPECT_EQ(ServiceWorkerVersion::STOPPING
, listener
.last_status
);
551 TEST_F(ServiceWorkerWaitForeverInFetchTest
, RequestTimeout
) {
552 ServiceWorkerStatusCode status
= SERVICE_WORKER_ERROR_NETWORK
; // dummy value
554 version_
->SetStatus(ServiceWorkerVersion::ACTIVATED
);
555 version_
->DispatchFetchEvent(ServiceWorkerFetchRequest(),
556 base::Bind(&base::DoNothing
),
557 base::Bind(&ReceiveFetchResult
, &status
));
558 base::RunLoop().RunUntilIdle();
560 // Callback has not completed yet.
561 EXPECT_EQ(SERVICE_WORKER_ERROR_NETWORK
, status
);
562 EXPECT_EQ(ServiceWorkerVersion::RUNNING
, version_
->running_status());
565 EXPECT_TRUE(version_
->timeout_timer_
.IsRunning());
566 version_
->SetAllRequestTimes(
567 base::TimeTicks::Now() -
568 base::TimeDelta::FromMinutes(
569 ServiceWorkerVersion::kRequestTimeoutMinutes
+ 1));
570 version_
->timeout_timer_
.user_task().Run();
571 base::RunLoop().RunUntilIdle();
572 EXPECT_EQ(SERVICE_WORKER_ERROR_TIMEOUT
, status
);
573 EXPECT_EQ(ServiceWorkerVersion::STOPPED
, version_
->running_status());
576 TEST_F(ServiceWorkerFailToStartTest
, RendererCrash
) {
577 ServiceWorkerStatusCode status
= SERVICE_WORKER_ERROR_NETWORK
; // dummy value
578 version_
->StartWorker(
579 CreateReceiverOnCurrentThread(&status
));
580 base::RunLoop().RunUntilIdle();
582 // Callback has not completed yet.
583 EXPECT_EQ(SERVICE_WORKER_ERROR_NETWORK
, status
);
584 EXPECT_EQ(ServiceWorkerVersion::STARTING
, version_
->running_status());
586 // Simulate renderer crash: do what
587 // ServiceWorkerDispatcherHost::OnFilterRemoved does.
588 int process_id
= helper_
->mock_render_process_id();
589 helper_
->context()->RemoveAllProviderHostsForProcess(process_id
);
590 helper_
->context()->embedded_worker_registry()->RemoveChildProcessSender(
592 base::RunLoop().RunUntilIdle();
594 // Callback completed.
595 EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED
, status
);
596 EXPECT_EQ(ServiceWorkerVersion::STOPPED
, version_
->running_status());
599 TEST_F(ServiceWorkerFailToStartTest
, Timeout
) {
600 ServiceWorkerStatusCode status
= SERVICE_WORKER_ERROR_NETWORK
; // dummy value
602 // We could just call StartWorker but make it interesting and test
603 // starting the worker as part of dispatching an event.
604 version_
->SetStatus(ServiceWorkerVersion::ACTIVATING
);
605 version_
->DispatchActivateEvent(CreateReceiverOnCurrentThread(&status
));
606 base::RunLoop().RunUntilIdle();
608 // Callback has not completed yet.
609 EXPECT_EQ(SERVICE_WORKER_ERROR_NETWORK
, status
);
610 EXPECT_EQ(ServiceWorkerVersion::STARTING
, version_
->running_status());
613 EXPECT_TRUE(version_
->timeout_timer_
.IsRunning());
614 version_
->start_time_
=
615 base::TimeTicks::Now() -
616 base::TimeDelta::FromMinutes(
617 ServiceWorkerVersion::kStartWorkerTimeoutMinutes
+ 1);
618 version_
->timeout_timer_
.user_task().Run();
619 base::RunLoop().RunUntilIdle();
620 EXPECT_EQ(SERVICE_WORKER_ERROR_TIMEOUT
, status
);
621 EXPECT_EQ(ServiceWorkerVersion::STOPPED
, version_
->running_status());
624 } // namespace content