ServiceWorker: Stop exposing ServiceWorkerContextCore
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_handle_unittest.cc
bloba3603aa4127e5dc1cbb5b5dc2bc7e12c8a4c8c08
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_dispatcher_host.h"
11 #include "content/browser/service_worker/service_worker_handle.h"
12 #include "content/browser/service_worker/service_worker_registration.h"
13 #include "content/browser/service_worker/service_worker_test_utils.h"
14 #include "content/browser/service_worker/service_worker_version.h"
15 #include "content/common/service_worker/embedded_worker_messages.h"
16 #include "content/common/service_worker/service_worker_messages.h"
17 #include "content/public/test/mock_resource_context.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "ipc/ipc_message.h"
20 #include "ipc/ipc_test_sink.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "third_party/WebKit/public/platform/WebServiceWorkerState.h"
24 namespace content {
26 namespace {
28 const int kRenderProcessId = 88; // A dummy ID for testing.
29 const int kRenderFrameId = 44; // A dummy ID for testing.
31 void VerifyStateChangedMessage(int expected_handle_id,
32 blink::WebServiceWorkerState expected_state,
33 const IPC::Message* message) {
34 ASSERT_TRUE(message != NULL);
35 ServiceWorkerMsg_ServiceWorkerStateChanged::Param param;
36 ASSERT_TRUE(ServiceWorkerMsg_ServiceWorkerStateChanged::Read(
37 message, &param));
38 EXPECT_EQ(expected_handle_id, get<1>(param));
39 EXPECT_EQ(expected_state, get<2>(param));
42 } // namespace
44 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost {
45 public:
46 TestingServiceWorkerDispatcherHost(
47 int process_id,
48 ServiceWorkerContextWrapper* context_wrapper,
49 ResourceContext* resource_context,
50 EmbeddedWorkerTestHelper* helper)
51 : ServiceWorkerDispatcherHost(process_id, nullptr, resource_context),
52 bad_message_received_count_(0),
53 helper_(helper) {
54 Init(context_wrapper);
57 bool Send(IPC::Message* message) override { return helper_->Send(message); }
59 void BadMessageReceived() override { ++bad_message_received_count_; }
61 int bad_message_received_count_;
63 protected:
64 EmbeddedWorkerTestHelper* helper_;
65 ~TestingServiceWorkerDispatcherHost() override {}
68 class ServiceWorkerHandleTest : public testing::Test {
69 public:
70 ServiceWorkerHandleTest()
71 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
73 void SetUp() override {
74 helper_.reset(
75 new EmbeddedWorkerTestHelper(base::FilePath(), kRenderProcessId));
77 dispatcher_host_ = new TestingServiceWorkerDispatcherHost(
78 kRenderProcessId, helper_->context_wrapper(),
79 &resource_context_, helper_.get());
81 const GURL pattern("http://www.example.com/");
82 registration_ = new ServiceWorkerRegistration(
83 pattern,
84 1L,
85 helper_->context()->AsWeakPtr());
86 version_ = new ServiceWorkerVersion(
87 registration_.get(),
88 GURL("http://www.example.com/service_worker.js"),
89 1L,
90 helper_->context()->AsWeakPtr());
92 // Make the registration findable via storage functions.
93 helper_->context()->storage()->LazyInitialize(base::Bind(&base::DoNothing));
94 base::RunLoop().RunUntilIdle();
95 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
96 helper_->context()->storage()->StoreRegistration(
97 registration_.get(),
98 version_.get(),
99 CreateReceiverOnCurrentThread(&status));
100 base::RunLoop().RunUntilIdle();
101 ASSERT_EQ(SERVICE_WORKER_OK, status);
103 provider_host_.reset(new ServiceWorkerProviderHost(
104 kRenderProcessId, kRenderFrameId, 1, SERVICE_WORKER_PROVIDER_FOR_WINDOW,
105 helper_->context()->AsWeakPtr(), dispatcher_host_.get()));
107 helper_->SimulateAddProcessToPattern(pattern, kRenderProcessId);
110 void TearDown() override {
111 dispatcher_host_ = NULL;
112 registration_ = NULL;
113 version_ = NULL;
114 provider_host_.reset();
115 helper_.reset();
118 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); }
120 TestBrowserThreadBundle browser_thread_bundle_;
121 MockResourceContext resource_context_;
123 scoped_ptr<EmbeddedWorkerTestHelper> helper_;
124 scoped_ptr<ServiceWorkerProviderHost> provider_host_;
125 scoped_refptr<ServiceWorkerRegistration> registration_;
126 scoped_refptr<ServiceWorkerVersion> version_;
127 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host_;
129 private:
130 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerHandleTest);
133 TEST_F(ServiceWorkerHandleTest, OnVersionStateChanged) {
134 scoped_ptr<ServiceWorkerHandle> handle =
135 ServiceWorkerHandle::Create(helper_->context()->AsWeakPtr(),
136 provider_host_->AsWeakPtr(),
137 version_.get());
139 // Start the worker, and then...
140 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
141 version_->StartWorker(CreateReceiverOnCurrentThread(&status));
142 base::RunLoop().RunUntilIdle();
143 EXPECT_EQ(SERVICE_WORKER_OK, status);
145 // ...dispatch install event.
146 status = SERVICE_WORKER_ERROR_FAILED;
147 version_->SetStatus(ServiceWorkerVersion::INSTALLING);
148 version_->DispatchInstallEvent(CreateReceiverOnCurrentThread(&status));
149 base::RunLoop().RunUntilIdle();
150 EXPECT_EQ(SERVICE_WORKER_OK, status);
152 version_->SetStatus(ServiceWorkerVersion::INSTALLED);
154 ASSERT_EQ(4UL, ipc_sink()->message_count());
155 ASSERT_EQ(0L, dispatcher_host_->bad_message_received_count_);
157 // We should be sending 1. StartWorker,
158 EXPECT_EQ(EmbeddedWorkerMsg_StartWorker::ID,
159 ipc_sink()->GetMessageAt(0)->type());
160 // 2. StateChanged (state == Installing),
161 VerifyStateChangedMessage(handle->handle_id(),
162 blink::WebServiceWorkerStateInstalling,
163 ipc_sink()->GetMessageAt(1));
164 // 3. SendMessageToWorker (to send InstallEvent), and
165 EXPECT_EQ(EmbeddedWorkerContextMsg_MessageToWorker::ID,
166 ipc_sink()->GetMessageAt(2)->type());
167 // 4. StateChanged (state == Installed).
168 VerifyStateChangedMessage(handle->handle_id(),
169 blink::WebServiceWorkerStateInstalled,
170 ipc_sink()->GetMessageAt(3));
173 } // namespace content