1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
content / renderer / media / inspector_media_event_handler_unittest.cc [blame]
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <string>
#include <utility>
#include "content/renderer/media/inspector_media_event_handler.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/web/web_media_inspector.h"
using ::testing::_;
namespace content {
class MockMediaInspectorContext : public blink::MediaInspectorContext {
public:
MockMediaInspectorContext() = default;
virtual ~MockMediaInspectorContext() = default;
blink::WebString CreatePlayer() override { return "TestPlayer"; }
void DestroyPlayer(const blink::WebString& playerId) override {
MockDestroyPlayer();
}
void NotifyPlayerEvents(blink::WebString id,
const blink::InspectorPlayerEvents& events) override {
MockNotifyPlayerEvents(events);
}
void SetPlayerProperties(
blink::WebString id,
const blink::InspectorPlayerProperties& props) override {
MockSetPlayerProperties(props);
}
void NotifyPlayerErrors(blink::WebString id,
const blink::InspectorPlayerErrors& errors) override {
MockNotifyPlayerErrors(errors);
}
void NotifyPlayerMessages(
blink::WebString id,
const blink::InspectorPlayerMessages& messages) override {
MockNotifyPlayerMessages(messages);
}
MOCK_METHOD1(MockNotifyPlayerEvents, void(blink::InspectorPlayerEvents));
MOCK_METHOD1(MockSetPlayerProperties, void(blink::InspectorPlayerProperties));
MOCK_METHOD1(MockNotifyPlayerErrors, void(blink::InspectorPlayerErrors));
MOCK_METHOD1(MockNotifyPlayerMessages, void(blink::InspectorPlayerMessages));
MOCK_METHOD0(MockDestroyPlayer, void());
MOCK_METHOD0(MockIncrementActiveSessionCount, void());
MOCK_METHOD0(MockDecrementActiveSessionCount, void());
};
class InspectorMediaEventHandlerTest : public testing::Test {
public:
InspectorMediaEventHandlerTest() {
mock_context_ = std::make_unique<MockMediaInspectorContext>();
handler_ =
std::make_unique<InspectorMediaEventHandler>(mock_context_.get());
}
InspectorMediaEventHandlerTest(const InspectorMediaEventHandlerTest&) =
delete;
InspectorMediaEventHandlerTest& operator=(
const InspectorMediaEventHandlerTest&) = delete;
protected:
std::unique_ptr<InspectorMediaEventHandler> handler_;
std::unique_ptr<MockMediaInspectorContext> mock_context_;
template <media::MediaLogEvent T>
media::MediaLogRecord CreateEvent() {
media::MediaLogRecord event;
event.id = 0;
event.type = media::MediaLogRecord::Type::kMediaEventTriggered;
event.time = base::TimeTicks();
event.params.Set("event", media::MediaLogEventTypeSupport<T>::TypeName());
return event;
}
media::MediaLogRecord CreatePropChange(
std::vector<std::pair<std::string, std::string>> props) {
media::MediaLogRecord event;
event.id = 0;
event.type = media::MediaLogRecord::Type::kMediaPropertyChange;
event.time = base::TimeTicks();
for (auto p : props) {
event.params.Set(std::get<0>(p), std::get<1>(p));
}
return event;
}
media::MediaLogRecord CreateMessage(std::string msg) {
media::MediaLogRecord event;
event.id = 0;
event.type = media::MediaLogRecord::Type::kMessage;
event.time = base::TimeTicks();
event.params.Set("warning", msg);
return event;
}
media::MediaLogRecord CreateError(int errorcode) {
media::MediaLogRecord error;
error.id = 0;
error.type = media::MediaLogRecord::Type::kMediaStatus;
error.time = base::TimeTicks();
error.params.Set(media::StatusConstants::kCodeKey, errorcode);
error.params.Set(media::StatusConstants::kGroupKey,
media::PipelineStatus::Traits::Group());
return error;
}
};
bool operator==(const blink::InspectorPlayerProperty& lhs,
const blink::InspectorPlayerProperty& rhs) {
return lhs.name == rhs.name && lhs.value == rhs.value;
}
bool operator!=(const blink::InspectorPlayerProperty& lhs,
const blink::InspectorPlayerProperty& rhs) {
return !(lhs == rhs);
}
bool operator==(const blink::InspectorPlayerEvent& lhs,
const blink::InspectorPlayerEvent& rhs) {
return lhs.timestamp == rhs.timestamp && lhs.value == rhs.value;
}
bool operator!=(const blink::InspectorPlayerEvent& lhs,
const blink::InspectorPlayerEvent& rhs) {
return !(lhs == rhs);
}
bool operator==(const blink::InspectorPlayerMessage& lhs,
const blink::InspectorPlayerMessage& rhs) {
return lhs.level == rhs.level && lhs.message == rhs.message;
}
bool operator!=(const blink::InspectorPlayerMessage& lhs,
const blink::InspectorPlayerMessage& rhs) {
return !(lhs == rhs);
}
bool operator==(const blink::InspectorPlayerError& lhs,
const blink::InspectorPlayerError& rhs) {
return lhs.group == rhs.group && lhs.code == rhs.code;
}
bool operator!=(const blink::InspectorPlayerError& lhs,
const blink::InspectorPlayerError& rhs) {
return !(lhs == rhs);
}
MATCHER_P(PropertiesEqualTo, props, "") {
if (props.size() != arg.size())
return false;
for (size_t i = 0; i < props.size(); i++)
if (props[i] != arg[i])
return false;
return true;
}
MATCHER_P(EventsEqualTo, events, "") {
if (events.size() != arg.size())
return false;
for (size_t i = 0; i < events.size(); i++)
if (events[i] != arg[i])
return false;
return true;
}
MATCHER_P(MessagesEqualTo, messages, "") {
if (messages.size() != arg.size())
return false;
for (size_t i = 0; i < messages.size(); i++)
if (messages[i] != arg[i])
return false;
return true;
}
MATCHER_P(ErrorsEqualTo, errors, "") {
if (errors.size() != arg.size())
return false;
for (size_t i = 0; i < errors.size(); i++)
if (errors[i] != arg[i])
return false;
return true;
}
TEST_F(InspectorMediaEventHandlerTest, ConvertsProperties) {
std::vector<media::MediaLogRecord> events = {
CreatePropChange({{"test_key", "test_value"}})};
blink::InspectorPlayerProperties expected;
blink::InspectorPlayerProperty prop = {
blink::WebString::FromUTF8("test_key"),
blink::WebString::FromUTF8("test_value")};
expected.emplace_back(prop);
EXPECT_CALL(*mock_context_,
MockSetPlayerProperties(PropertiesEqualTo(expected)))
.Times(1);
EXPECT_CALL(*mock_context_, MockNotifyPlayerEvents(_)).Times(0);
handler_->SendQueuedMediaEvents(events);
}
TEST_F(InspectorMediaEventHandlerTest, SplitsDoubleProperties) {
std::vector<media::MediaLogRecord> events = {
CreatePropChange({{"test_key", "test_value"}, {"foo", "bar"}})};
blink::InspectorPlayerProperties expected;
blink::InspectorPlayerProperty prop_test = {
blink::WebString::FromUTF8("test_key"),
blink::WebString::FromUTF8("test_value")};
blink::InspectorPlayerProperty prop_foo = {blink::WebString::FromUTF8("foo"),
blink::WebString::FromUTF8("bar")};
expected.emplace_back(prop_foo);
expected.emplace_back(prop_test);
EXPECT_CALL(*mock_context_,
MockSetPlayerProperties(PropertiesEqualTo(expected)))
.Times(1);
EXPECT_CALL(*mock_context_, MockNotifyPlayerEvents(_)).Times(0);
handler_->SendQueuedMediaEvents(events);
}
TEST_F(InspectorMediaEventHandlerTest, ConvertsMessageEvent) {
std::vector<media::MediaLogRecord> events = {
CreateMessage("Has Anyone Really Been Far Even as Decided to Use Even "
"Go Want to do Look More Like?")};
blink::InspectorPlayerMessages expected;
blink::InspectorPlayerMessage e = {
blink::InspectorPlayerMessage::Level::kWarning,
blink::WebString::FromUTF8("Has Anyone Really Been Far Even as Decided "
"to Use Even Go Want to do Look More Like?")};
expected.emplace_back(e);
EXPECT_CALL(*mock_context_, MockSetPlayerProperties(_)).Times(0);
EXPECT_CALL(*mock_context_,
MockNotifyPlayerMessages(MessagesEqualTo(expected)))
.Times(1);
handler_->SendQueuedMediaEvents(events);
}
TEST_F(InspectorMediaEventHandlerTest, ConvertsEventsAndProperties) {
std::vector<media::MediaLogRecord> events = {
CreateMessage("100% medically accurate"),
CreatePropChange(
{{"free_puppies", "all_taken"}, {"illuminati", "confirmed"}})};
blink::InspectorPlayerMessages expected_messages;
blink::InspectorPlayerMessage e = {
blink::InspectorPlayerMessage::Level::kWarning,
blink::WebString::FromUTF8("100% medically accurate")};
expected_messages.emplace_back(e);
blink::InspectorPlayerProperties expected_properties;
blink::InspectorPlayerProperty puppies = {
blink::WebString::FromUTF8("free_puppies"),
blink::WebString::FromUTF8("all_taken")};
blink::InspectorPlayerProperty illumanati = {
blink::WebString::FromUTF8("illuminati"),
blink::WebString::FromUTF8("confirmed")};
expected_properties.emplace_back(puppies);
expected_properties.emplace_back(illumanati);
EXPECT_CALL(*mock_context_,
MockSetPlayerProperties(PropertiesEqualTo(expected_properties)))
.Times(1);
EXPECT_CALL(*mock_context_,
MockNotifyPlayerMessages(MessagesEqualTo(expected_messages)))
.Times(1);
handler_->SendQueuedMediaEvents(events);
}
TEST_F(InspectorMediaEventHandlerTest, PassesPlayAndPauseEvents) {
std::vector<media::MediaLogRecord> events = {
CreateEvent<media::MediaLogEvent::kPlay>(),
CreateEvent<media::MediaLogEvent::kPause>()};
blink::InspectorPlayerEvents expected_events;
blink::InspectorPlayerEvent play = {
base::TimeTicks(), blink::WebString::FromUTF8("{\"event\":\"kPlay\"}")};
blink::InspectorPlayerEvent pause = {
base::TimeTicks(), blink::WebString::FromUTF8("{\"event\":\"kPause\"}")};
expected_events.emplace_back(play);
expected_events.emplace_back(pause);
EXPECT_CALL(*mock_context_,
MockNotifyPlayerEvents(EventsEqualTo(expected_events)))
.Times(1);
handler_->SendQueuedMediaEvents(events);
}
TEST_F(InspectorMediaEventHandlerTest, PassesErrorEvents) {
std::vector<media::MediaLogRecord> errors = {
CreateError(media::PIPELINE_ERROR_NETWORK),
CreateError(media::PIPELINE_ERROR_EXTERNAL_RENDERER_FAILED)};
blink::InspectorPlayerErrors expected_errors;
blink::InspectorPlayerError first = {"PipelineStatus", 2, "", {}, {}, {}};
blink::InspectorPlayerError second = {"PipelineStatus", 21, "", {}, {}, {}};
expected_errors.emplace_back(first);
expected_errors.emplace_back(second);
EXPECT_CALL(*mock_context_,
MockNotifyPlayerErrors(ErrorsEqualTo(expected_errors)))
.Times(1);
handler_->SendQueuedMediaEvents(errors);
}
} // namespace content