Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Side by Side Diff: blimp/engine/browser/engine_render_widget_message_processor_unittest.cc

Issue 1450423002: Add glue between the client and engine for Blimp (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@blimp_ipc2
Patch Set: Fix build break Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « blimp/engine/browser/engine_render_widget_message_processor.cc ('k') | blimp/net/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 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 "blimp/engine/browser/engine_render_widget_message_processor.h"
6
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/numerics/safe_conversions.h"
10 #include "blimp/common/proto/blimp_message.pb.h"
11 #include "blimp/common/proto/compositor.pb.h"
12 #include "blimp/common/proto/render_widget.pb.h"
13 #include "blimp/net/input_message_generator.h"
14 #include "net/base/net_errors.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/WebKit/public/web/WebInputEvent.h"
18
19 using testing::_;
20 using testing::InvokeArgument;
21 using testing::Ref;
22 using testing::Return;
23 using testing::SaveArg;
24
25 namespace blimp {
26
27 namespace {
28 class MockBlimpMessageProcessor : public BlimpMessageProcessor {
29 public:
30 MockBlimpMessageProcessor() {}
31
32 ~MockBlimpMessageProcessor() override {}
33
34 // Adapts calls from ProcessMessage to MockableProcessMessage by
35 // unboxing the |message| scoped_ptr for GMock compatibility.
36 void ProcessMessage(scoped_ptr<BlimpMessage> message,
37 const net::CompletionCallback& callback) {
38 MockableProcessMessage(*message);
39 if (!callback.is_null())
40 callback.Run(net::OK);
41 }
42
43 MOCK_METHOD1(MockableProcessMessage,
44 void(const BlimpMessage& message));
45 };
46
47 class MockHostRenderWidgetMessageDelegate
48 : public EngineRenderWidgetMessageProcessor::RenderWidgetMessageDelegate {
49 public:
50 // EngineRenderWidgetMessageProcessor implementation.
51 void OnWebInputEvent(scoped_ptr<blink::WebInputEvent> event) override {
52 MockableOnWebInputEvent();
53 }
54
55 void OnCompositorMessageReceived(
56 const std::vector<uint8_t>& message) override {
57 MockableOnCompositorMessageReceived(message);
58 }
59
60 MOCK_METHOD0(MockableOnWebInputEvent, void());
61 MOCK_METHOD1(MockableOnCompositorMessageReceived,
62 void(const std::vector<uint8_t>& message));
63 };
64
65 MATCHER_P(CompMsgEquals, contents, "") {
66 if (contents.size() != arg.size())
67 return false;
68
69 return memcmp(contents.data(), arg.data(), contents.size()) == 0;
70 }
71
72 MATCHER_P3(BlimpCompMsgEquals, tab_id, rw_id, contents, "") {
73 if (contents.size() != arg.compositor().payload().size())
74 return false;
75
76 if (memcmp(contents.data(),
77 arg.compositor().payload().data(),
78 contents.size()) != 0) {
79 return false;
80 }
81
82 return arg.compositor().render_widget_id() == rw_id &&
83 arg.target_tab_id() == tab_id;
84 }
85
86 MATCHER_P2(BlimpRWMsgEquals, tab_id, rw_id, "") {
87 return arg.render_widget().render_widget_id() == rw_id &&
88 arg.target_tab_id() == tab_id;
89 }
90
91 void SendInputMessage(BlimpMessageProcessor* processor,
92 int tab_id,
93 uint32_t rw_id) {
94 blink::WebGestureEvent input_event;
95 input_event.type = blink::WebInputEvent::Type::GestureTap;
96
97 InputMessageGenerator generator;
98 scoped_ptr<BlimpMessage> message = generator.GenerateMessage(input_event);
99 message->set_type(BlimpMessage::INPUT);
100 message->set_target_tab_id(tab_id);
101 message->mutable_input()->set_render_widget_id(rw_id);
102
103 processor->ProcessMessage(message.Pass(),
104 net::CompletionCallback());
105 }
106
107 void SendCompositorMessage(BlimpMessageProcessor* processor,
108 int tab_id,
109 uint32_t rw_id,
110 const std::vector<uint8_t>& payload) {
111 scoped_ptr<BlimpMessage> message(new BlimpMessage);
112 message->set_type(BlimpMessage::COMPOSITOR);
113 message->set_target_tab_id(tab_id);
114
115 CompositorMessage* details = message->mutable_compositor();
116 details->set_render_widget_id(rw_id);
117 details->set_payload(payload.data(), base::checked_cast<int>(payload.size()));
118 processor->ProcessMessage(message.Pass(),
119 net::CompletionCallback());
120 }
121
122 } // namespace
123
124 class EngineRenderWidgetMessageProcessorTest : public testing::Test {
125 public:
126 EngineRenderWidgetMessageProcessorTest()
127 : processor_(&out_processor_, &out_processor_) {}
128
129 void SetUp() override {
130 processor_.SetDelegate(1, &delegate1_);
131 processor_.SetDelegate(2, &delegate2_);
132
133 processor_.OnRenderWidgetInitialized(1);
134 processor_.OnRenderWidgetInitialized(2);
135 }
136
137 protected:
138 MockBlimpMessageProcessor out_processor_;
139 MockHostRenderWidgetMessageDelegate delegate1_;
140 MockHostRenderWidgetMessageDelegate delegate2_;
141 EngineRenderWidgetMessageProcessor processor_;
142 };
143
144 TEST_F(EngineRenderWidgetMessageProcessorTest, DelegateCallsOK) {
145 std::vector<uint8_t> payload = { 'd', 'a', 'v', 'i', 'd' };
146
147 EXPECT_CALL(delegate1_, MockableOnCompositorMessageReceived(
148 CompMsgEquals(payload))).Times(1);
149 SendCompositorMessage(&processor_, 1, 1U, payload);
150
151 EXPECT_CALL(delegate1_, MockableOnWebInputEvent()).Times(1);
152 SendInputMessage(&processor_, 1, 1U);
153
154 EXPECT_CALL(delegate2_, MockableOnCompositorMessageReceived(
155 CompMsgEquals(payload))).Times(1);
156 SendCompositorMessage(&processor_, 2, 1U, payload);
157
158 EXPECT_CALL(delegate2_, MockableOnWebInputEvent()).Times(1);
159 SendInputMessage(&processor_, 2, 1U);
160 }
161
162 TEST_F(EngineRenderWidgetMessageProcessorTest, DropsStaleMessages) {
163 std::vector<uint8_t> payload = { 'f', 'u', 'n' };
164
165 EXPECT_CALL(delegate1_, MockableOnCompositorMessageReceived(
166 CompMsgEquals(payload))).Times(1);
167 SendCompositorMessage(&processor_, 1, 1U, payload);
168
169 EXPECT_CALL(out_processor_,
170 MockableProcessMessage(BlimpRWMsgEquals(1, 2U))).Times(1);
171 processor_.OnRenderWidgetInitialized(1);
172
173 EXPECT_CALL(delegate1_, MockableOnCompositorMessageReceived(
174 CompMsgEquals(payload))).Times(0);
175 payload[0] = 'a';
176 SendCompositorMessage(&processor_, 1, 1U, payload);
177
178 EXPECT_CALL(delegate1_, MockableOnWebInputEvent()).Times(0);
179 SendInputMessage(&processor_, 1, 1U);
180
181 EXPECT_CALL(delegate1_, MockableOnCompositorMessageReceived(
182 CompMsgEquals(payload))).Times(1);
183 SendCompositorMessage(&processor_, 1, 2U, payload);
184
185 EXPECT_CALL(delegate1_, MockableOnWebInputEvent()).Times(1);
186 SendInputMessage(&processor_, 1, 2U);
187 }
188
189 TEST_F(EngineRenderWidgetMessageProcessorTest,
190 RepliesHaveCorrectRenderWidgetId) {
191 std::vector<uint8_t> payload = { 'a', 'b', 'c', 'd' };
192
193 EXPECT_CALL(out_processor_,
194 MockableProcessMessage(BlimpRWMsgEquals(1, 2U))).Times(1);
195 processor_.OnRenderWidgetInitialized(1);
196
197 EXPECT_CALL(out_processor_,
198 MockableProcessMessage(BlimpRWMsgEquals(2, 2U))).Times(1);
199 processor_.OnRenderWidgetInitialized(2);
200
201 EXPECT_CALL(out_processor_, MockableProcessMessage(
202 BlimpCompMsgEquals(1, 2U, payload))).Times(1);
203
204 processor_.SendCompositorMessage(1, payload);
205 }
206
207
208 } // namespace blimp
OLDNEW
« no previous file with comments | « blimp/engine/browser/engine_render_widget_message_processor.cc ('k') | blimp/net/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698