Simplify ChildProcessLauncher
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_version_unittest.cc
blob5f3ec486a9fd9bf1a55a22af6a21daad6c6995cc
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/service_worker/embedded_worker_registry.h"
8 #include "content/browser/service_worker/embedded_worker_test_helper.h"
9 #include "content/browser/service_worker/service_worker_context_core.h"
10 #include "content/browser/service_worker/service_worker_registration.h"
11 #include "content/browser/service_worker/service_worker_test_utils.h"
12 #include "content/browser/service_worker/service_worker_utils.h"
13 #include "content/browser/service_worker/service_worker_version.h"
14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 // IPC messages for testing ---------------------------------------------------
19 #define IPC_MESSAGE_IMPL
20 #include "ipc/ipc_message_macros.h"
22 #define IPC_MESSAGE_START TestMsgStart
24 IPC_MESSAGE_CONTROL0(TestMsg_Message);
25 IPC_MESSAGE_ROUTED1(TestMsg_MessageFromWorker, int);
27 // ---------------------------------------------------------------------------
29 namespace content {
31 namespace {
33 static const int kRenderProcessId = 1;
35 class MessageReceiver : public EmbeddedWorkerTestHelper {
36 public:
37 MessageReceiver()
38 : EmbeddedWorkerTestHelper(base::FilePath(), kRenderProcessId),
39 current_embedded_worker_id_(0) {}
40 ~MessageReceiver() override {}
42 bool OnMessageToWorker(int thread_id,
43 int embedded_worker_id,
44 const IPC::Message& message) override {
45 if (EmbeddedWorkerTestHelper::OnMessageToWorker(
46 thread_id, embedded_worker_id, message)) {
47 return true;
49 current_embedded_worker_id_ = embedded_worker_id;
50 bool handled = true;
51 IPC_BEGIN_MESSAGE_MAP(MessageReceiver, message)
52 IPC_MESSAGE_HANDLER(TestMsg_Message, OnMessage)
53 IPC_MESSAGE_UNHANDLED(handled = false)
54 IPC_END_MESSAGE_MAP()
55 return handled;
58 void SimulateSendValueToBrowser(int embedded_worker_id, int value) {
59 SimulateSend(new TestMsg_MessageFromWorker(embedded_worker_id, value));
62 private:
63 void OnMessage() {
64 // Do nothing.
67 int current_embedded_worker_id_;
68 DISALLOW_COPY_AND_ASSIGN(MessageReceiver);
71 void VerifyCalled(bool* called) {
72 *called = true;
75 void ObserveStatusChanges(ServiceWorkerVersion* version,
76 std::vector<ServiceWorkerVersion::Status>* statuses) {
77 statuses->push_back(version->status());
78 version->RegisterStatusChangeCallback(
79 base::Bind(&ObserveStatusChanges, base::Unretained(version), statuses));
82 void ReceiveFetchResult(ServiceWorkerStatusCode* status,
83 ServiceWorkerStatusCode actual_status,
84 ServiceWorkerFetchEventResult actual_result,
85 const ServiceWorkerResponse& response) {
86 *status = actual_status;
89 // A specialized listener class to receive test messages from a worker.
90 class MessageReceiverFromWorker : public EmbeddedWorkerInstance::Listener {
91 public:
92 explicit MessageReceiverFromWorker(EmbeddedWorkerInstance* instance)
93 : instance_(instance) {
94 instance_->AddListener(this);
96 ~MessageReceiverFromWorker() override { instance_->RemoveListener(this); }
98 void OnStarted() override { NOTREACHED(); }
99 void OnStopped(EmbeddedWorkerInstance::Status old_status) override {
100 NOTREACHED();
102 bool OnMessageReceived(const IPC::Message& message) override {
103 bool handled = true;
104 IPC_BEGIN_MESSAGE_MAP(MessageReceiverFromWorker, message)
105 IPC_MESSAGE_HANDLER(TestMsg_MessageFromWorker, OnMessageFromWorker)
106 IPC_MESSAGE_UNHANDLED(handled = false)
107 IPC_END_MESSAGE_MAP()
108 return handled;
111 void OnMessageFromWorker(int value) { received_values_.push_back(value); }
112 const std::vector<int>& received_values() const { return received_values_; }
114 private:
115 EmbeddedWorkerInstance* instance_;
116 std::vector<int> received_values_;
117 DISALLOW_COPY_AND_ASSIGN(MessageReceiverFromWorker);
120 } // namespace
122 class ServiceWorkerVersionTest : public testing::Test {
123 protected:
124 ServiceWorkerVersionTest()
125 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
127 void SetUp() override {
128 helper_ = GetMessageReceiver();
130 pattern_ = GURL("http://www.example.com/");
131 registration_ = new ServiceWorkerRegistration(
132 pattern_,
134 helper_->context()->AsWeakPtr());
135 version_ = new ServiceWorkerVersion(
136 registration_.get(),
137 GURL("http://www.example.com/service_worker.js"),
139 helper_->context()->AsWeakPtr());
141 // Make the registration findable via storage functions.
142 helper_->context()->storage()->LazyInitialize(base::Bind(&base::DoNothing));
143 base::RunLoop().RunUntilIdle();
144 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
145 helper_->context()->storage()->StoreRegistration(
146 registration_.get(),
147 version_.get(),
148 CreateReceiverOnCurrentThread(&status));
149 base::RunLoop().RunUntilIdle();
150 ASSERT_EQ(SERVICE_WORKER_OK, status);
152 // Simulate adding one process to the pattern.
153 helper_->SimulateAddProcessToPattern(pattern_, kRenderProcessId);
154 ASSERT_TRUE(helper_->context()->process_manager()
155 ->PatternHasProcessToRun(pattern_));
158 virtual scoped_ptr<MessageReceiver> GetMessageReceiver() {
159 return make_scoped_ptr(new MessageReceiver());
162 void TearDown() override {
163 version_ = 0;
164 registration_ = 0;
165 helper_.reset();
168 TestBrowserThreadBundle thread_bundle_;
169 scoped_ptr<MessageReceiver> helper_;
170 scoped_refptr<ServiceWorkerRegistration> registration_;
171 scoped_refptr<ServiceWorkerVersion> version_;
172 GURL pattern_;
174 private:
175 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersionTest);
178 class MessageReceiverDisallowStart : public MessageReceiver {
179 public:
180 MessageReceiverDisallowStart()
181 : MessageReceiver() {}
182 ~MessageReceiverDisallowStart() override {}
184 void OnStartWorker(int embedded_worker_id,
185 int64 service_worker_version_id,
186 const GURL& scope,
187 const GURL& script_url,
188 bool pause_after_download) override {
189 // Do nothing.
192 private:
193 DISALLOW_COPY_AND_ASSIGN(MessageReceiverDisallowStart);
196 class ServiceWorkerFailToStartTest : public ServiceWorkerVersionTest {
197 protected:
198 ServiceWorkerFailToStartTest()
199 : ServiceWorkerVersionTest() {}
201 scoped_ptr<MessageReceiver> GetMessageReceiver() override {
202 return make_scoped_ptr(new MessageReceiverDisallowStart());
205 private:
206 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerFailToStartTest);
209 class MessageReceiverDisallowFetch : public MessageReceiver {
210 public:
211 MessageReceiverDisallowFetch() : MessageReceiver() {}
212 ~MessageReceiverDisallowFetch() override {}
214 void OnFetchEvent(int embedded_worker_id,
215 int request_id,
216 const ServiceWorkerFetchRequest& request) override {
217 // Do nothing.
220 private:
221 DISALLOW_COPY_AND_ASSIGN(MessageReceiverDisallowFetch);
224 class ServiceWorkerWaitForeverInFetchTest : public ServiceWorkerVersionTest {
225 protected:
226 ServiceWorkerWaitForeverInFetchTest() : ServiceWorkerVersionTest() {}
228 scoped_ptr<MessageReceiver> GetMessageReceiver() override {
229 return make_scoped_ptr(new MessageReceiverDisallowFetch());
232 private:
233 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerWaitForeverInFetchTest);
236 TEST_F(ServiceWorkerVersionTest, ConcurrentStartAndStop) {
237 // Call StartWorker() multiple times.
238 ServiceWorkerStatusCode status1 = SERVICE_WORKER_ERROR_FAILED;
239 ServiceWorkerStatusCode status2 = SERVICE_WORKER_ERROR_FAILED;
240 ServiceWorkerStatusCode status3 = SERVICE_WORKER_ERROR_FAILED;
241 version_->StartWorker(CreateReceiverOnCurrentThread(&status1));
242 version_->StartWorker(CreateReceiverOnCurrentThread(&status2));
244 EXPECT_EQ(ServiceWorkerVersion::STARTING, version_->running_status());
245 base::RunLoop().RunUntilIdle();
246 EXPECT_EQ(ServiceWorkerVersion::RUNNING, version_->running_status());
248 // Call StartWorker() after it's started.
249 version_->StartWorker(CreateReceiverOnCurrentThread(&status3));
250 base::RunLoop().RunUntilIdle();
252 // All should just succeed.
253 EXPECT_EQ(SERVICE_WORKER_OK, status1);
254 EXPECT_EQ(SERVICE_WORKER_OK, status2);
255 EXPECT_EQ(SERVICE_WORKER_OK, status3);
257 // Call StopWorker() multiple times.
258 status1 = SERVICE_WORKER_ERROR_FAILED;
259 status2 = SERVICE_WORKER_ERROR_FAILED;
260 version_->StopWorker(CreateReceiverOnCurrentThread(&status1));
261 version_->StopWorker(CreateReceiverOnCurrentThread(&status2));
263 EXPECT_EQ(ServiceWorkerVersion::STOPPING, version_->running_status());
264 base::RunLoop().RunUntilIdle();
265 EXPECT_EQ(ServiceWorkerVersion::STOPPED, version_->running_status());
267 // All StopWorker should just succeed.
268 EXPECT_EQ(SERVICE_WORKER_OK, status1);
269 EXPECT_EQ(SERVICE_WORKER_OK, status2);
271 // Start worker again.
272 status1 = SERVICE_WORKER_ERROR_FAILED;
273 status2 = SERVICE_WORKER_ERROR_FAILED;
274 status3 = SERVICE_WORKER_ERROR_FAILED;
276 version_->StartWorker(CreateReceiverOnCurrentThread(&status1));
278 EXPECT_EQ(ServiceWorkerVersion::STARTING, version_->running_status());
279 base::RunLoop().RunUntilIdle();
280 EXPECT_EQ(ServiceWorkerVersion::RUNNING, version_->running_status());
282 // Call StopWorker()
283 status2 = SERVICE_WORKER_ERROR_FAILED;
284 version_->StopWorker(CreateReceiverOnCurrentThread(&status2));
286 // And try calling StartWorker while StopWorker is in queue.
287 version_->StartWorker(CreateReceiverOnCurrentThread(&status3));
289 EXPECT_EQ(ServiceWorkerVersion::STOPPING, version_->running_status());
290 base::RunLoop().RunUntilIdle();
291 EXPECT_EQ(ServiceWorkerVersion::RUNNING, version_->running_status());
293 // All should just succeed.
294 EXPECT_EQ(SERVICE_WORKER_OK, status1);
295 EXPECT_EQ(SERVICE_WORKER_OK, status2);
296 EXPECT_EQ(SERVICE_WORKER_OK, status3);
299 TEST_F(ServiceWorkerVersionTest, DispatchEventToStoppedWorker) {
300 EXPECT_EQ(ServiceWorkerVersion::STOPPED, version_->running_status());
302 // Dispatch an event without starting the worker.
303 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
304 version_->SetStatus(ServiceWorkerVersion::INSTALLING);
305 version_->DispatchInstallEvent(CreateReceiverOnCurrentThread(&status));
306 base::RunLoop().RunUntilIdle();
307 EXPECT_EQ(SERVICE_WORKER_OK, status);
309 // The worker should be now started.
310 EXPECT_EQ(ServiceWorkerVersion::RUNNING, version_->running_status());
312 // Stop the worker, and then dispatch an event immediately after that.
313 status = SERVICE_WORKER_ERROR_FAILED;
314 ServiceWorkerStatusCode stop_status = SERVICE_WORKER_ERROR_FAILED;
315 version_->StopWorker(CreateReceiverOnCurrentThread(&stop_status));
316 version_->DispatchInstallEvent(CreateReceiverOnCurrentThread(&status));
317 base::RunLoop().RunUntilIdle();
318 EXPECT_EQ(SERVICE_WORKER_OK, stop_status);
320 // Dispatch an event should return SERVICE_WORKER_OK since the worker
321 // should have been restarted to dispatch the event.
322 EXPECT_EQ(SERVICE_WORKER_OK, status);
324 // The worker should be now started again.
325 EXPECT_EQ(ServiceWorkerVersion::RUNNING, version_->running_status());
328 TEST_F(ServiceWorkerVersionTest, ReceiveMessageFromWorker) {
329 // Start worker.
330 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
331 version_->StartWorker(CreateReceiverOnCurrentThread(&status));
332 EXPECT_EQ(ServiceWorkerVersion::STARTING, version_->running_status());
333 base::RunLoop().RunUntilIdle();
334 EXPECT_EQ(SERVICE_WORKER_OK, status);
335 EXPECT_EQ(ServiceWorkerVersion::RUNNING, version_->running_status());
337 MessageReceiverFromWorker receiver(version_->embedded_worker());
339 // Simulate sending some dummy values from the worker.
340 helper_->SimulateSendValueToBrowser(
341 version_->embedded_worker()->embedded_worker_id(), 555);
342 helper_->SimulateSendValueToBrowser(
343 version_->embedded_worker()->embedded_worker_id(), 777);
345 // Verify the receiver received the values.
346 ASSERT_EQ(2U, receiver.received_values().size());
347 EXPECT_EQ(555, receiver.received_values()[0]);
348 EXPECT_EQ(777, receiver.received_values()[1]);
351 TEST_F(ServiceWorkerVersionTest, InstallAndWaitCompletion) {
352 version_->SetStatus(ServiceWorkerVersion::INSTALLING);
354 // Dispatch an install event.
355 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
356 version_->DispatchInstallEvent(CreateReceiverOnCurrentThread(&status));
358 // Wait for the completion.
359 bool status_change_called = false;
360 version_->RegisterStatusChangeCallback(
361 base::Bind(&VerifyCalled, &status_change_called));
363 base::RunLoop().RunUntilIdle();
365 // Version's status must not have changed during installation.
366 EXPECT_EQ(SERVICE_WORKER_OK, status);
367 EXPECT_FALSE(status_change_called);
368 EXPECT_EQ(ServiceWorkerVersion::INSTALLING, version_->status());
371 TEST_F(ServiceWorkerVersionTest, ActivateAndWaitCompletion) {
372 version_->SetStatus(ServiceWorkerVersion::ACTIVATING);
374 // Dispatch an activate event.
375 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
376 version_->DispatchActivateEvent(CreateReceiverOnCurrentThread(&status));
378 // Wait for the completion.
379 bool status_change_called = false;
380 version_->RegisterStatusChangeCallback(
381 base::Bind(&VerifyCalled, &status_change_called));
383 base::RunLoop().RunUntilIdle();
385 // Version's status must not have changed during activation.
386 EXPECT_EQ(SERVICE_WORKER_OK, status);
387 EXPECT_FALSE(status_change_called);
388 EXPECT_EQ(ServiceWorkerVersion::ACTIVATING, version_->status());
391 TEST_F(ServiceWorkerVersionTest, RepeatedlyObserveStatusChanges) {
392 EXPECT_EQ(ServiceWorkerVersion::NEW, version_->status());
394 // Repeatedly observe status changes (the callback re-registers itself).
395 std::vector<ServiceWorkerVersion::Status> statuses;
396 version_->RegisterStatusChangeCallback(
397 base::Bind(&ObserveStatusChanges, version_, &statuses));
399 version_->SetStatus(ServiceWorkerVersion::INSTALLING);
400 version_->SetStatus(ServiceWorkerVersion::INSTALLED);
401 version_->SetStatus(ServiceWorkerVersion::ACTIVATING);
402 version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
403 version_->SetStatus(ServiceWorkerVersion::REDUNDANT);
405 // Verify that we could successfully observe repeated status changes.
406 ASSERT_EQ(5U, statuses.size());
407 ASSERT_EQ(ServiceWorkerVersion::INSTALLING, statuses[0]);
408 ASSERT_EQ(ServiceWorkerVersion::INSTALLED, statuses[1]);
409 ASSERT_EQ(ServiceWorkerVersion::ACTIVATING, statuses[2]);
410 ASSERT_EQ(ServiceWorkerVersion::ACTIVATED, statuses[3]);
411 ASSERT_EQ(ServiceWorkerVersion::REDUNDANT, statuses[4]);
414 TEST_F(ServiceWorkerVersionTest, IdleTimeout) {
415 // Used to reliably test when the idle time gets reset regardless of clock
416 // granularity.
417 const base::TimeDelta kOneSecond = base::TimeDelta::FromSeconds(1);
419 // Verify the timer is not running when version initializes its status.
420 version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
421 EXPECT_FALSE(version_->timeout_timer_.IsRunning());
423 // Verify the timer is running after the worker is started.
424 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
425 version_->StartWorker(CreateReceiverOnCurrentThread(&status));
426 base::RunLoop().RunUntilIdle();
427 EXPECT_EQ(SERVICE_WORKER_OK, status);
428 EXPECT_TRUE(version_->timeout_timer_.IsRunning());
429 EXPECT_FALSE(version_->idle_time_.is_null());
431 // The idle time should be reset if the worker is restarted without
432 // controllee.
433 status = SERVICE_WORKER_ERROR_FAILED;
434 version_->idle_time_ -= kOneSecond;
435 base::TimeTicks idle_time = version_->idle_time_;
436 version_->StopWorker(CreateReceiverOnCurrentThread(&status));
437 base::RunLoop().RunUntilIdle();
438 EXPECT_EQ(SERVICE_WORKER_OK, status);
439 status = SERVICE_WORKER_ERROR_FAILED;
440 version_->StartWorker(CreateReceiverOnCurrentThread(&status));
441 base::RunLoop().RunUntilIdle();
442 EXPECT_EQ(SERVICE_WORKER_OK, status);
443 EXPECT_TRUE(version_->timeout_timer_.IsRunning());
444 EXPECT_LT(idle_time, version_->idle_time_);
446 // Adding a controllee resets the idle time.
447 version_->idle_time_ -= kOneSecond;
448 idle_time = version_->idle_time_;
449 scoped_ptr<ServiceWorkerProviderHost> host(
450 new ServiceWorkerProviderHost(33 /* dummy render process id */,
451 MSG_ROUTING_NONE /* render_frame_id */,
452 1 /* dummy provider_id */,
453 SERVICE_WORKER_PROVIDER_FOR_CONTROLLEE,
454 helper_->context()->AsWeakPtr(),
455 NULL));
456 version_->AddControllee(host.get());
457 EXPECT_TRUE(version_->timeout_timer_.IsRunning());
458 EXPECT_LT(idle_time, version_->idle_time_);
460 // Completing an event resets the idle time.
461 status = SERVICE_WORKER_ERROR_FAILED;
462 version_->idle_time_ -= kOneSecond;
463 idle_time = version_->idle_time_;
464 version_->DispatchFetchEvent(ServiceWorkerFetchRequest(),
465 base::Bind(&base::DoNothing),
466 base::Bind(&ReceiveFetchResult, &status));
467 base::RunLoop().RunUntilIdle();
469 EXPECT_EQ(SERVICE_WORKER_OK, status);
470 EXPECT_LT(idle_time, version_->idle_time_);
474 TEST_F(ServiceWorkerVersionTest, SetDevToolsAttached) {
475 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
476 version_->StartWorker(CreateReceiverOnCurrentThread(&status));
478 ASSERT_EQ(ServiceWorkerVersion::STARTING, version_->running_status());
480 ASSERT_TRUE(version_->timeout_timer_.IsRunning());
481 ASSERT_FALSE(version_->start_time_.is_null());
482 ASSERT_FALSE(version_->skip_recording_startup_time_);
484 // Simulate DevTools is attached. This should deactivate the timer for start
485 // timeout, but not stop the timer itself.
486 version_->SetDevToolsAttached(true);
487 EXPECT_TRUE(version_->timeout_timer_.IsRunning());
488 EXPECT_TRUE(version_->start_time_.is_null());
489 EXPECT_TRUE(version_->skip_recording_startup_time_);
491 // Simulate DevTools is detached. This should reactivate the timer for start
492 // timeout.
493 version_->SetDevToolsAttached(false);
494 EXPECT_TRUE(version_->timeout_timer_.IsRunning());
495 EXPECT_FALSE(version_->start_time_.is_null());
496 EXPECT_TRUE(version_->skip_recording_startup_time_);
498 base::RunLoop().RunUntilIdle();
499 EXPECT_EQ(SERVICE_WORKER_OK, status);
500 EXPECT_EQ(ServiceWorkerVersion::RUNNING, version_->running_status());
503 TEST_F(ServiceWorkerWaitForeverInFetchTest, RequestTimeout) {
504 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_NETWORK; // dummy value
506 version_->SetStatus(ServiceWorkerVersion::ACTIVATED);
507 version_->DispatchFetchEvent(ServiceWorkerFetchRequest(),
508 base::Bind(&base::DoNothing),
509 base::Bind(&ReceiveFetchResult, &status));
510 base::RunLoop().RunUntilIdle();
512 // Callback has not completed yet.
513 EXPECT_EQ(SERVICE_WORKER_ERROR_NETWORK, status);
514 EXPECT_EQ(ServiceWorkerVersion::RUNNING, version_->running_status());
516 // Simulate timeout.
517 EXPECT_TRUE(version_->timeout_timer_.IsRunning());
518 version_->SetAllRequestTimes(
519 base::TimeTicks::Now() -
520 base::TimeDelta::FromMinutes(
521 ServiceWorkerVersion::kRequestTimeoutMinutes + 1));
522 version_->timeout_timer_.user_task().Run();
523 base::RunLoop().RunUntilIdle();
524 EXPECT_EQ(SERVICE_WORKER_ERROR_TIMEOUT, status);
525 EXPECT_EQ(ServiceWorkerVersion::STOPPED, version_->running_status());
528 TEST_F(ServiceWorkerFailToStartTest, RendererCrash) {
529 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_NETWORK; // dummy value
530 version_->StartWorker(
531 CreateReceiverOnCurrentThread(&status));
532 base::RunLoop().RunUntilIdle();
534 // Callback has not completed yet.
535 EXPECT_EQ(SERVICE_WORKER_ERROR_NETWORK, status);
536 EXPECT_EQ(ServiceWorkerVersion::STARTING, version_->running_status());
538 // Simulate renderer crash: do what
539 // ServiceWorkerDispatcherHost::OnFilterRemoved does.
540 int process_id = helper_->mock_render_process_id();
541 helper_->context()->RemoveAllProviderHostsForProcess(process_id);
542 helper_->context()->embedded_worker_registry()->RemoveChildProcessSender(
543 process_id);
544 base::RunLoop().RunUntilIdle();
546 // Callback completed.
547 EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status);
548 EXPECT_EQ(ServiceWorkerVersion::STOPPED, version_->running_status());
551 TEST_F(ServiceWorkerFailToStartTest, Timeout) {
552 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_NETWORK; // dummy value
554 // We could just call StartWorker but make it interesting and test
555 // starting the worker as part of dispatching an event.
556 version_->SetStatus(ServiceWorkerVersion::ACTIVATING);
557 version_->DispatchActivateEvent(CreateReceiverOnCurrentThread(&status));
558 base::RunLoop().RunUntilIdle();
560 // Callback has not completed yet.
561 EXPECT_EQ(SERVICE_WORKER_ERROR_NETWORK, status);
562 EXPECT_EQ(ServiceWorkerVersion::STARTING, version_->running_status());
564 // Simulate timeout.
565 EXPECT_TRUE(version_->timeout_timer_.IsRunning());
566 version_->start_time_ =
567 base::TimeTicks::Now() -
568 base::TimeDelta::FromMinutes(
569 ServiceWorkerVersion::kStartWorkerTimeoutMinutes + 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 } // namespace content