[Ozone-Gbm] Explicitly crash if trying software rendering on GBM
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_handle_unittest.cc
blob7b2d708180a9e2022ad3a7eb847fa636631df80c
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(new EmbeddedWorkerTestHelper(kRenderProcessId));
76 dispatcher_host_ = new TestingServiceWorkerDispatcherHost(
77 kRenderProcessId, helper_->context_wrapper(),
78 &resource_context_, helper_.get());
80 const GURL pattern("http://www.example.com/");
81 registration_ = new ServiceWorkerRegistration(
82 pattern,
83 1L,
84 helper_->context()->AsWeakPtr());
85 version_ = new ServiceWorkerVersion(
86 registration_.get(),
87 GURL("http://www.example.com/service_worker.js"),
88 1L,
89 helper_->context()->AsWeakPtr());
91 provider_host_.reset(new ServiceWorkerProviderHost(
92 kRenderProcessId, kRenderFrameId, 1,
93 SERVICE_WORKER_PROVIDER_FOR_CONTROLLEE,
94 helper_->context()->AsWeakPtr(), dispatcher_host_.get()));
96 helper_->SimulateAddProcessToPattern(pattern, kRenderProcessId);
99 void TearDown() override {
100 dispatcher_host_ = NULL;
101 registration_ = NULL;
102 version_ = NULL;
103 provider_host_.reset();
104 helper_.reset();
107 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); }
109 TestBrowserThreadBundle browser_thread_bundle_;
110 MockResourceContext resource_context_;
112 scoped_ptr<EmbeddedWorkerTestHelper> helper_;
113 scoped_ptr<ServiceWorkerProviderHost> provider_host_;
114 scoped_refptr<ServiceWorkerRegistration> registration_;
115 scoped_refptr<ServiceWorkerVersion> version_;
116 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host_;
118 private:
119 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerHandleTest);
122 TEST_F(ServiceWorkerHandleTest, OnVersionStateChanged) {
123 scoped_ptr<ServiceWorkerHandle> handle =
124 ServiceWorkerHandle::Create(helper_->context()->AsWeakPtr(),
125 provider_host_->AsWeakPtr(),
126 version_.get());
128 // Start the worker, and then...
129 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
130 version_->StartWorker(CreateReceiverOnCurrentThread(&status));
131 base::RunLoop().RunUntilIdle();
132 EXPECT_EQ(SERVICE_WORKER_OK, status);
134 // ...dispatch install event.
135 status = SERVICE_WORKER_ERROR_FAILED;
136 version_->SetStatus(ServiceWorkerVersion::INSTALLING);
137 version_->DispatchInstallEvent(-1, CreateReceiverOnCurrentThread(&status));
138 base::RunLoop().RunUntilIdle();
139 EXPECT_EQ(SERVICE_WORKER_OK, status);
141 version_->SetStatus(ServiceWorkerVersion::INSTALLED);
143 ASSERT_EQ(4UL, ipc_sink()->message_count());
144 ASSERT_EQ(0L, dispatcher_host_->bad_message_received_count_);
146 // We should be sending 1. StartWorker,
147 EXPECT_EQ(EmbeddedWorkerMsg_StartWorker::ID,
148 ipc_sink()->GetMessageAt(0)->type());
149 // 2. StateChanged (state == Installing),
150 VerifyStateChangedMessage(handle->handle_id(),
151 blink::WebServiceWorkerStateInstalling,
152 ipc_sink()->GetMessageAt(1));
153 // 3. SendMessageToWorker (to send InstallEvent), and
154 EXPECT_EQ(EmbeddedWorkerContextMsg_MessageToWorker::ID,
155 ipc_sink()->GetMessageAt(2)->type());
156 // 4. StateChanged (state == Installed).
157 VerifyStateChangedMessage(handle->handle_id(),
158 blink::WebServiceWorkerStateInstalled,
159 ipc_sink()->GetMessageAt(3));
162 } // namespace content