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_handle.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_version.h"
14 #include "content/common/service_worker/embedded_worker_messages.h"
15 #include "content/common/service_worker/service_worker_messages.h"
16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "ipc/ipc_message.h"
18 #include "ipc/ipc_test_sink.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "third_party/WebKit/public/platform/WebServiceWorkerState.h"
26 const int kRenderProcessId
= 88; // A dummy ID for testing.
28 void VerifyStateChangedMessage(int expected_handle_id
,
29 blink::WebServiceWorkerState expected_state
,
30 const IPC::Message
* message
) {
31 ASSERT_TRUE(message
!= NULL
);
32 ServiceWorkerMsg_ServiceWorkerStateChanged::Param param
;
33 ASSERT_TRUE(ServiceWorkerMsg_ServiceWorkerStateChanged::Read(
35 EXPECT_EQ(expected_handle_id
, param
.b
);
36 EXPECT_EQ(expected_state
, param
.c
);
41 class ServiceWorkerHandleTest
: public testing::Test
{
43 ServiceWorkerHandleTest()
44 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP
) {}
46 virtual void SetUp() OVERRIDE
{
47 helper_
.reset(new EmbeddedWorkerTestHelper(kRenderProcessId
));
49 registration_
= new ServiceWorkerRegistration(
50 GURL("http://www.example.com/"),
52 helper_
->context()->AsWeakPtr());
53 version_
= new ServiceWorkerVersion(
55 GURL("http://www.example.com/service_worker.js"),
57 helper_
->context()->AsWeakPtr());
59 // Simulate adding one process to the worker.
60 int embedded_worker_id
= version_
->embedded_worker()->embedded_worker_id();
61 helper_
->SimulateAddProcessToWorker(embedded_worker_id
, kRenderProcessId
);
64 virtual void TearDown() OVERRIDE
{
70 IPC::TestSink
* ipc_sink() { return helper_
->ipc_sink(); }
72 TestBrowserThreadBundle browser_thread_bundle_
;
73 scoped_ptr
<EmbeddedWorkerTestHelper
> helper_
;
74 scoped_refptr
<ServiceWorkerRegistration
> registration_
;
75 scoped_refptr
<ServiceWorkerVersion
> version_
;
78 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerHandleTest
);
81 TEST_F(ServiceWorkerHandleTest
, OnVersionStateChanged
) {
82 scoped_ptr
<ServiceWorkerHandle
> handle
=
83 ServiceWorkerHandle::Create(helper_
->context()->AsWeakPtr(),
89 // Start the worker, and then...
90 ServiceWorkerStatusCode status
= SERVICE_WORKER_ERROR_FAILED
;
91 version_
->StartWorker(CreateReceiverOnCurrentThread(&status
));
92 base::RunLoop().RunUntilIdle();
93 EXPECT_EQ(SERVICE_WORKER_OK
, status
);
95 // ...dispatch install event.
96 status
= SERVICE_WORKER_ERROR_FAILED
;
97 version_
->SetStatus(ServiceWorkerVersion::INSTALLING
);
98 version_
->DispatchInstallEvent(-1, CreateReceiverOnCurrentThread(&status
));
99 base::RunLoop().RunUntilIdle();
100 EXPECT_EQ(SERVICE_WORKER_OK
, status
);
102 version_
->SetStatus(ServiceWorkerVersion::INSTALLED
);
104 ASSERT_EQ(4UL, ipc_sink()->message_count());
106 // We should be sending 1. StartWorker,
107 EXPECT_EQ(EmbeddedWorkerMsg_StartWorker::ID
,
108 ipc_sink()->GetMessageAt(0)->type());
109 // 2. StateChanged (state == Installing),
110 VerifyStateChangedMessage(handle
->handle_id(),
111 blink::WebServiceWorkerStateInstalling
,
112 ipc_sink()->GetMessageAt(1));
113 // 3. SendMessageToWorker (to send InstallEvent), and
114 EXPECT_EQ(EmbeddedWorkerContextMsg_MessageToWorker::ID
,
115 ipc_sink()->GetMessageAt(2)->type());
116 // 4. StateChanged (state == Installed).
117 VerifyStateChangedMessage(handle
->handle_id(),
118 blink::WebServiceWorkerStateInstalled
,
119 ipc_sink()->GetMessageAt(3));
122 } // namespace content