Cast: Skip receiver log messages with time delta that can't be encoded.
[chromium-blink-merge.git] / content / browser / service_worker / embedded_worker_test_helper.cc
blobedc73b8951075b56fb35ef7e6fbfd090cffdb0f5
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 "content/browser/service_worker/embedded_worker_test_helper.h"
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/common/service_worker/embedded_worker_messages.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace content {
16 EmbeddedWorkerTestHelper::EmbeddedWorkerTestHelper(
17 ServiceWorkerContextCore* context,
18 int mock_render_process_id)
19 : context_(context->AsWeakPtr()),
20 next_thread_id_(0),
21 weak_factory_(this) {
22 registry()->AddChildProcessSender(mock_render_process_id, this);
25 EmbeddedWorkerTestHelper::~EmbeddedWorkerTestHelper() {
28 void EmbeddedWorkerTestHelper::SimulateAddProcessToWorker(
29 int embedded_worker_id,
30 int process_id) {
31 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
32 ASSERT_TRUE(worker);
33 registry()->AddChildProcessSender(process_id, this);
34 worker->AddProcessReference(process_id);
37 bool EmbeddedWorkerTestHelper::Send(IPC::Message* message) {
38 OnMessageReceived(*message);
39 delete message;
40 return true;
43 bool EmbeddedWorkerTestHelper::OnMessageReceived(const IPC::Message& message) {
44 bool handled = true;
45 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerTestHelper, message)
46 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StartWorker, OnStartWorkerStub)
47 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StopWorker, OnStopWorkerStub)
48 IPC_MESSAGE_HANDLER(EmbeddedWorkerContextMsg_SendMessageToWorker,
49 OnSendMessageToWorkerStub)
50 IPC_MESSAGE_UNHANDLED(handled = false)
51 IPC_END_MESSAGE_MAP()
53 // IPC::TestSink only records messages that are not handled by filters,
54 // so we just forward all messages to the separate sink.
55 sink_.OnMessageReceived(message);
57 return handled;
60 void EmbeddedWorkerTestHelper::OnStartWorker(
61 int embedded_worker_id,
62 int64 service_worker_version_id,
63 const GURL& script_url) {
64 // By default just notify the sender that the worker is started.
65 SimulateWorkerStarted(next_thread_id_++, embedded_worker_id);
68 void EmbeddedWorkerTestHelper::OnStopWorker(int embedded_worker_id) {
69 // By default just notify the sender that the worker is stopped.
70 SimulateWorkerStopped(embedded_worker_id);
73 void EmbeddedWorkerTestHelper::OnSendMessageToWorker(
74 int thread_id,
75 int embedded_worker_id,
76 int request_id,
77 const IPC::Message& message) {
78 // Do nothing on the message by default.
81 void EmbeddedWorkerTestHelper::SimulateWorkerStarted(
82 int thread_id, int embedded_worker_id) {
83 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
84 ASSERT_TRUE(worker != NULL);
85 registry()->OnWorkerStarted(
86 worker->process_id(),
87 thread_id,
88 embedded_worker_id);
91 void EmbeddedWorkerTestHelper::SimulateWorkerStopped(
92 int embedded_worker_id) {
93 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
94 ASSERT_TRUE(worker != NULL);
95 registry()->OnWorkerStopped(worker->process_id(), embedded_worker_id);
98 void EmbeddedWorkerTestHelper::SimulateSendMessageToBrowser(
99 int embedded_worker_id, int request_id, const IPC::Message& message) {
100 registry()->OnSendMessageToBrowser(embedded_worker_id, request_id, message);
103 void EmbeddedWorkerTestHelper::OnStartWorkerStub(
104 int embedded_worker_id,
105 int64 service_worker_version_id,
106 const GURL& script_url) {
107 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
108 ASSERT_TRUE(worker != NULL);
109 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status());
110 base::MessageLoopProxy::current()->PostTask(
111 FROM_HERE,
112 base::Bind(&EmbeddedWorkerTestHelper::OnStartWorker,
113 weak_factory_.GetWeakPtr(),
114 embedded_worker_id,
115 service_worker_version_id,
116 script_url));
119 void EmbeddedWorkerTestHelper::OnStopWorkerStub(int embedded_worker_id) {
120 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
121 ASSERT_TRUE(worker != NULL);
122 base::MessageLoopProxy::current()->PostTask(
123 FROM_HERE,
124 base::Bind(&EmbeddedWorkerTestHelper::OnStopWorker,
125 weak_factory_.GetWeakPtr(),
126 embedded_worker_id));
129 void EmbeddedWorkerTestHelper::OnSendMessageToWorkerStub(
130 int thread_id,
131 int embedded_worker_id,
132 int request_id,
133 const IPC::Message& message) {
134 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
135 ASSERT_TRUE(worker != NULL);
136 EXPECT_EQ(worker->thread_id(), thread_id);
137 base::MessageLoopProxy::current()->PostTask(
138 FROM_HERE,
139 base::Bind(&EmbeddedWorkerTestHelper::OnSendMessageToWorker,
140 weak_factory_.GetWeakPtr(),
141 thread_id,
142 embedded_worker_id,
143 request_id,
144 message));
147 EmbeddedWorkerRegistry* EmbeddedWorkerTestHelper::registry() {
148 DCHECK(context_);
149 return context_->embedded_worker_registry();
152 } // namespace content