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. 4 5 #include "content/browser/service_worker/embedded_worker_test_helper.h" 6 7 #include "base/bind.h" 8 #include "content/browser/service_worker/embedded_worker_instance.h" 9 #include "content/browser/service_worker/embedded_worker_registry.h" 10 #include "content/browser/service_worker/service_worker_context_core.h" 11 #include "content/browser/service_worker/service_worker_context_wrapper.h" 12 #include "content/common/service_worker/embedded_worker_messages.h" 13 #include "content/common/service_worker/service_worker_messages.h" 14 #include "testing/gtest/include/gtest/gtest.h" 15 16 namespace content { 17 18 EmbeddedWorkerTestHelper::EmbeddedWorkerTestHelper(int mock_render_process_id) 19 : wrapper_(new ServiceWorkerContextWrapper(NULL)), 20 next_thread_id_(0), 21 weak_factory_(this) { 22 wrapper_->InitInternal(base::FilePath(), 23 base::MessageLoopProxy::current(), 24 base::MessageLoopProxy::current(), 25 NULL); 26 wrapper_->process_manager()->SetProcessIdForTest(mock_render_process_id); 27 registry()->AddChildProcessSender(mock_render_process_id, this); 28 } 29 30 EmbeddedWorkerTestHelper::~EmbeddedWorkerTestHelper() { 31 if (wrapper_) 32 wrapper_->Shutdown(); 33 } 34 35 void EmbeddedWorkerTestHelper::SimulateAddProcessToWorker( 36 int embedded_worker_id, 37 int process_id) { 38 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); 39 ASSERT_TRUE(worker); 40 registry()->AddChildProcessSender(process_id, this); 41 worker->AddProcessReference(process_id); 42 } 43 44 bool EmbeddedWorkerTestHelper::Send(IPC::Message* message) { 45 OnMessageReceived(*message); 46 delete message; 47 return true; 48 } 49 50 bool EmbeddedWorkerTestHelper::OnMessageReceived(const IPC::Message& message) { 51 bool handled = true; 52 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerTestHelper, message) 53 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StartWorker, OnStartWorkerStub) 54 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StopWorker, OnStopWorkerStub) 55 IPC_MESSAGE_HANDLER(EmbeddedWorkerContextMsg_MessageToWorker, 56 OnMessageToWorkerStub) 57 IPC_MESSAGE_UNHANDLED(handled = false) 58 IPC_END_MESSAGE_MAP() 59 60 // IPC::TestSink only records messages that are not handled by filters, 61 // so we just forward all messages to the separate sink. 62 sink_.OnMessageReceived(message); 63 64 return handled; 65 } 66 67 ServiceWorkerContextCore* EmbeddedWorkerTestHelper::context() { 68 return wrapper_->context(); 69 } 70 71 void EmbeddedWorkerTestHelper::ShutdownContext() { 72 wrapper_->Shutdown(); 73 wrapper_ = NULL; 74 } 75 76 void EmbeddedWorkerTestHelper::OnStartWorker( 77 int embedded_worker_id, 78 int64 service_worker_version_id, 79 const GURL& scope, 80 const GURL& script_url) { 81 // By default just notify the sender that the worker is started. 82 SimulateWorkerStarted(next_thread_id_++, embedded_worker_id); 83 } 84 85 void EmbeddedWorkerTestHelper::OnStopWorker(int embedded_worker_id) { 86 // By default just notify the sender that the worker is stopped. 87 SimulateWorkerStopped(embedded_worker_id); 88 } 89 90 bool EmbeddedWorkerTestHelper::OnMessageToWorker( 91 int thread_id, 92 int embedded_worker_id, 93 const IPC::Message& message) { 94 bool handled = true; 95 current_embedded_worker_id_ = embedded_worker_id; 96 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerTestHelper, message) 97 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent, OnActivateEventStub) 98 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEventStub) 99 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FetchEvent, OnFetchEventStub) 100 IPC_MESSAGE_UNHANDLED(handled = false) 101 IPC_END_MESSAGE_MAP() 102 // Record all messages directed to inner script context. 103 inner_sink_.OnMessageReceived(message); 104 return handled; 105 } 106 107 void EmbeddedWorkerTestHelper::OnActivateEvent(int embedded_worker_id, 108 int request_id) { 109 SimulateSend( 110 new ServiceWorkerHostMsg_ActivateEventFinished( 111 embedded_worker_id, request_id, 112 blink::WebServiceWorkerEventResultCompleted)); 113 } 114 115 void EmbeddedWorkerTestHelper::OnInstallEvent(int embedded_worker_id, 116 int request_id, 117 int active_version_id) { 118 SimulateSend( 119 new ServiceWorkerHostMsg_InstallEventFinished( 120 embedded_worker_id, request_id, 121 blink::WebServiceWorkerEventResultCompleted)); 122 } 123 124 void EmbeddedWorkerTestHelper::OnFetchEvent( 125 int embedded_worker_id, 126 int request_id, 127 const ServiceWorkerFetchRequest& request) { 128 SimulateSend( 129 new ServiceWorkerHostMsg_FetchEventFinished( 130 embedded_worker_id, 131 request_id, 132 SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, 133 ServiceWorkerResponse(200, "OK", 134 std::map<std::string, std::string>(), 135 std::string()))); 136 } 137 138 void EmbeddedWorkerTestHelper::SimulateWorkerStarted( 139 int thread_id, int embedded_worker_id) { 140 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); 141 ASSERT_TRUE(worker != NULL); 142 registry()->OnWorkerStarted( 143 worker->process_id(), 144 thread_id, 145 embedded_worker_id); 146 } 147 148 void EmbeddedWorkerTestHelper::SimulateWorkerStopped( 149 int embedded_worker_id) { 150 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); 151 if (worker != NULL) 152 registry()->OnWorkerStopped(worker->process_id(), embedded_worker_id); 153 } 154 155 void EmbeddedWorkerTestHelper::SimulateSend( 156 IPC::Message* message) { 157 registry()->OnMessageReceived(*message); 158 delete message; 159 } 160 161 void EmbeddedWorkerTestHelper::OnStartWorkerStub( 162 const EmbeddedWorkerMsg_StartWorker_Params& params) { 163 EmbeddedWorkerInstance* worker = 164 registry()->GetWorker(params.embedded_worker_id); 165 ASSERT_TRUE(worker != NULL); 166 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status()); 167 base::MessageLoopProxy::current()->PostTask( 168 FROM_HERE, 169 base::Bind(&EmbeddedWorkerTestHelper::OnStartWorker, 170 weak_factory_.GetWeakPtr(), 171 params.embedded_worker_id, 172 params.service_worker_version_id, 173 params.scope, 174 params.script_url)); 175 } 176 177 void EmbeddedWorkerTestHelper::OnStopWorkerStub(int embedded_worker_id) { 178 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); 179 ASSERT_TRUE(worker != NULL); 180 base::MessageLoopProxy::current()->PostTask( 181 FROM_HERE, 182 base::Bind(&EmbeddedWorkerTestHelper::OnStopWorker, 183 weak_factory_.GetWeakPtr(), 184 embedded_worker_id)); 185 } 186 187 void EmbeddedWorkerTestHelper::OnMessageToWorkerStub( 188 int thread_id, 189 int embedded_worker_id, 190 const IPC::Message& message) { 191 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); 192 ASSERT_TRUE(worker != NULL); 193 EXPECT_EQ(worker->thread_id(), thread_id); 194 base::MessageLoopProxy::current()->PostTask( 195 FROM_HERE, 196 base::Bind( 197 base::IgnoreResult(&EmbeddedWorkerTestHelper::OnMessageToWorker), 198 weak_factory_.GetWeakPtr(), 199 thread_id, 200 embedded_worker_id, 201 message)); 202 } 203 204 void EmbeddedWorkerTestHelper::OnActivateEventStub(int request_id) { 205 base::MessageLoopProxy::current()->PostTask( 206 FROM_HERE, 207 base::Bind(&EmbeddedWorkerTestHelper::OnActivateEvent, 208 weak_factory_.GetWeakPtr(), 209 current_embedded_worker_id_, 210 request_id)); 211 } 212 213 void EmbeddedWorkerTestHelper::OnInstallEventStub(int request_id, 214 int active_version_id) { 215 base::MessageLoopProxy::current()->PostTask( 216 FROM_HERE, 217 base::Bind(&EmbeddedWorkerTestHelper::OnInstallEvent, 218 weak_factory_.GetWeakPtr(), 219 current_embedded_worker_id_, 220 request_id, 221 active_version_id)); 222 } 223 224 void EmbeddedWorkerTestHelper::OnFetchEventStub( 225 int request_id, 226 const ServiceWorkerFetchRequest& request) { 227 base::MessageLoopProxy::current()->PostTask( 228 FROM_HERE, 229 base::Bind(&EmbeddedWorkerTestHelper::OnFetchEvent, 230 weak_factory_.GetWeakPtr(), 231 current_embedded_worker_id_, 232 request_id, 233 request)); 234 } 235 236 EmbeddedWorkerRegistry* EmbeddedWorkerTestHelper::registry() { 237 DCHECK(context()); 238 return context()->embedded_worker_registry(); 239 } 240 241 } // namespace content 242