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.
8 #include "base/bind_helpers.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/run_loop.h"
11 #include "base/synchronization/waitable_event.h"
12 #include "chrome/browser/media/router/issue.h"
13 #include "chrome/browser/media/router/media_route.h"
14 #include "chrome/browser/media/router/media_router_mojo_test.h"
15 #include "chrome/browser/media/router/media_router_type_converters.h"
16 #include "chrome/browser/media/router/mock_media_router.h"
17 #include "chrome/browser/media/router/presentation_session_messages_observer.h"
18 #include "chrome/browser/media/router/test_helper.h"
19 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
20 #include "chrome/test/base/testing_profile.h"
21 #include "extensions/browser/extension_registry.h"
22 #include "extensions/browser/process_manager.h"
23 #include "extensions/browser/process_manager_factory.h"
24 #include "media/base/gmock_callback_support.h"
25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h"
30 using testing::Invoke
;
33 using testing::Pointee
;
34 using testing::Return
;
35 using testing::ReturnRef
;
36 using testing::SaveArg
;
38 namespace media_router
{
42 const char kDescription
[] = "description";
43 const char kError
[] = "error";
44 const char kExtensionId
[] = "extension1234";
45 const char kMessage
[] = "message";
46 const char kSource
[] = "source1";
47 const char kSource2
[] = "source2";
48 const char kRouteId
[] = "routeId";
49 const char kRouteId2
[] = "routeId2";
50 const char kSinkId
[] = "sink";
51 const char kSinkId2
[] = "sink2";
52 const char kSinkName
[] = "sinkName";
53 const char kPresentationId
[] = "presentationId";
54 const char kOrigin
[] = "http://origin/";
55 const int kTabId
= 123;
56 const uint8 kBinaryMessage
[] = {0x01, 0x02, 0x03, 0x04};
58 bool ArePresentationSessionMessagesEqual(
59 const content::PresentationSessionMessage
* expected
,
60 const content::PresentationSessionMessage
* actual
) {
61 if (expected
->type
!= actual
->type
)
64 return expected
->is_binary() ? *expected
->data
== *actual
->data
65 : expected
->message
== actual
->message
;
68 interfaces::IssuePtr
CreateMojoIssue(const std::string
& title
) {
69 interfaces::IssuePtr mojoIssue
= interfaces::Issue::New();
70 mojoIssue
->title
= title
;
71 mojoIssue
->message
= "msg";
72 mojoIssue
->route_id
= "";
73 mojoIssue
->default_action
=
74 interfaces::Issue::ActionType::ACTION_TYPE_DISMISS
;
75 mojoIssue
->secondary_actions
=
76 mojo::Array
<interfaces::Issue::ActionType
>::New(0);
77 mojoIssue
->severity
= interfaces::Issue::Severity::SEVERITY_WARNING
;
78 mojoIssue
->is_blocking
= false;
79 mojoIssue
->help_url
= "";
80 return mojoIssue
.Pass();
85 class RouteResponseCallbackHandler
{
88 void(const MediaRoute
* route
,
89 const std::string
& presentation_id
,
90 const std::string
& error_text
));
93 class SendMessageCallbackHandler
{
95 MOCK_METHOD1(Invoke
, void(bool));
98 class ListenForMessagesCallbackHandler
{
100 ListenForMessagesCallbackHandler(
101 ScopedVector
<content::PresentationSessionMessage
> expected_messages
,
103 : expected_messages_(expected_messages
.Pass()),
104 pass_ownership_(pass_ownership
) {}
105 void Invoke(const ScopedVector
<content::PresentationSessionMessage
>& messages
,
106 bool pass_ownership
) {
108 EXPECT_EQ(pass_ownership_
, pass_ownership
);
109 EXPECT_EQ(messages
.size(), expected_messages_
.size());
110 for (size_t i
= 0; i
< expected_messages_
.size(); ++i
) {
111 EXPECT_TRUE(ArePresentationSessionMessagesEqual(expected_messages_
[i
],
116 MOCK_METHOD0(InvokeObserver
, void());
119 ScopedVector
<content::PresentationSessionMessage
> expected_messages_
;
120 bool pass_ownership_
;
123 template <typename T
>
124 void StoreAndRun(T
* result
, const base::Closure
& closure
, const T
& result_val
) {
125 *result
= result_val
;
129 class MediaRouterMojoImplTest
: public MediaRouterMojoTest
{
131 MediaRouterMojoImplTest() {}
132 ~MediaRouterMojoImplTest() override
{}
135 // ProcessManager with a mocked method subset, for testing extension suspend
137 class TestProcessManager
: public extensions::ProcessManager
{
139 explicit TestProcessManager(content::BrowserContext
* context
)
140 : extensions::ProcessManager(
143 extensions::ExtensionRegistry::Get(context
)) {}
144 ~TestProcessManager() override
{}
146 static scoped_ptr
<KeyedService
> Create(content::BrowserContext
* context
) {
147 return make_scoped_ptr(new TestProcessManager(context
));
150 MOCK_METHOD1(IsEventPageSuspended
, bool(const std::string
& ext_id
));
152 MOCK_METHOD2(WakeEventPage
,
153 bool(const std::string
& extension_id
,
154 const base::Callback
<void(bool)>& callback
));
157 DISALLOW_COPY_AND_ASSIGN(TestProcessManager
);
160 // Mockable class for awaiting RegisterMediaRouteProvider callbacks.
161 class RegisterMediaRouteProviderHandler
{
163 MOCK_METHOD1(Invoke
, void(const std::string
& instance_id
));
166 TEST_F(MediaRouterMojoImplTest
, CreateRoute
) {
167 MediaRoute
expected_route(kRouteId
, MediaSource(std::string(kSource
)),
168 kSinkId
, "", false, "", false);
169 interfaces::MediaRoutePtr route
= interfaces::MediaRoute::New();
170 route
->media_source
= kSource
;
171 route
->media_sink_id
= kSinkId
;
172 route
->media_route_id
= kRouteId
;
173 route
->description
= kDescription
;
175 // Use a lambda function as an invocation target here to work around
176 // a limitation with GMock::Invoke that prevents it from using move-only types
177 // in runnable parameter lists.
178 EXPECT_CALL(mock_media_route_provider_
,
179 CreateRoute(mojo::String(kSource
), mojo::String(kSinkId
), _
,
180 mojo::String(kOrigin
), kTabId
, _
))
181 .WillOnce(Invoke([&route
](
182 const mojo::String
& source
, const mojo::String
& sink
,
183 const mojo::String
& presentation_id
, const mojo::String
& origin
,
185 const interfaces::MediaRouteProvider::CreateRouteCallback
& cb
) {
186 cb
.Run(route
.Pass(), mojo::String());
189 RouteResponseCallbackHandler handler
;
190 EXPECT_CALL(handler
, Invoke(Pointee(Equals(expected_route
)), Not(""), ""));
191 std::vector
<MediaRouteResponseCallback
> route_response_callbacks
;
192 route_response_callbacks
.push_back(base::Bind(
193 &RouteResponseCallbackHandler::Invoke
, base::Unretained(&handler
)));
194 router()->CreateRoute(kSource
, kSinkId
, GURL(kOrigin
), kTabId
,
195 route_response_callbacks
);
199 TEST_F(MediaRouterMojoImplTest
, CreateRouteFails
) {
200 EXPECT_CALL(mock_media_route_provider_
,
201 CreateRoute(mojo::String(kSource
), mojo::String(kSinkId
), _
,
202 mojo::String(kOrigin
), kTabId
, _
))
204 [](const mojo::String
& source
, const mojo::String
& sink
,
205 const mojo::String
& presentation_id
, const mojo::String
& origin
,
207 const interfaces::MediaRouteProvider::CreateRouteCallback
& cb
) {
208 cb
.Run(interfaces::MediaRoutePtr(), mojo::String(kError
));
211 RouteResponseCallbackHandler handler
;
212 EXPECT_CALL(handler
, Invoke(nullptr, "", kError
));
213 std::vector
<MediaRouteResponseCallback
> route_response_callbacks
;
214 route_response_callbacks
.push_back(base::Bind(
215 &RouteResponseCallbackHandler::Invoke
, base::Unretained(&handler
)));
216 router()->CreateRoute(kSource
, kSinkId
, GURL(kOrigin
), kTabId
,
217 route_response_callbacks
);
221 TEST_F(MediaRouterMojoImplTest
, JoinRoute
) {
222 MediaRoute
expected_route(kRouteId
, MediaSource(std::string(kSource
)),
223 kSinkId
, "", false, "", false);
224 interfaces::MediaRoutePtr route
= interfaces::MediaRoute::New();
225 route
->media_source
= kSource
;
226 route
->media_sink_id
= kSinkId
;
227 route
->media_route_id
= kRouteId
;
228 route
->description
= kDescription
;
230 // Use a lambda function as an invocation target here to work around
231 // a limitation with GMock::Invoke that prevents it from using move-only types
232 // in runnable parameter lists.
233 EXPECT_CALL(mock_media_route_provider_
,
234 JoinRoute(mojo::String(kSource
), mojo::String(kPresentationId
),
235 mojo::String(kOrigin
), kTabId
, _
))
236 .WillOnce(Invoke([&route
](
237 const mojo::String
& source
, const mojo::String
& presentation_id
,
238 const mojo::String
& origin
, int tab_id
,
239 const interfaces::MediaRouteProvider::JoinRouteCallback
& cb
) {
240 cb
.Run(route
.Pass(), mojo::String());
243 RouteResponseCallbackHandler handler
;
244 EXPECT_CALL(handler
, Invoke(Pointee(Equals(expected_route
)), Not(""), ""));
245 std::vector
<MediaRouteResponseCallback
> route_response_callbacks
;
246 route_response_callbacks
.push_back(base::Bind(
247 &RouteResponseCallbackHandler::Invoke
, base::Unretained(&handler
)));
248 router()->JoinRoute(kSource
, kPresentationId
, GURL(kOrigin
), kTabId
,
249 route_response_callbacks
);
253 TEST_F(MediaRouterMojoImplTest
, JoinRouteFails
) {
254 EXPECT_CALL(mock_media_route_provider_
,
255 JoinRoute(mojo::String(kSource
), mojo::String(kPresentationId
),
256 mojo::String(kOrigin
), kTabId
, _
))
258 [](const mojo::String
& source
, const mojo::String
& presentation_id
,
259 const mojo::String
& origin
, int tab_id
,
260 const interfaces::MediaRouteProvider::JoinRouteCallback
& cb
) {
261 cb
.Run(interfaces::MediaRoutePtr(), mojo::String(kError
));
264 RouteResponseCallbackHandler handler
;
265 EXPECT_CALL(handler
, Invoke(nullptr, "", kError
));
266 std::vector
<MediaRouteResponseCallback
> route_response_callbacks
;
267 route_response_callbacks
.push_back(base::Bind(
268 &RouteResponseCallbackHandler::Invoke
, base::Unretained(&handler
)));
269 router()->JoinRoute(kSource
, kPresentationId
, GURL(kOrigin
), kTabId
,
270 route_response_callbacks
);
274 TEST_F(MediaRouterMojoImplTest
, CloseRoute
) {
275 EXPECT_CALL(mock_media_route_provider_
, CloseRoute(mojo::String(kRouteId
)));
276 router()->CloseRoute(kRouteId
);
280 TEST_F(MediaRouterMojoImplTest
, HandleIssue
) {
281 MockIssuesObserver
issue_observer1(router());
282 MockIssuesObserver
issue_observer2(router());
283 interfaces::IssuePtr mojo_issue1
= CreateMojoIssue("title 1");
284 const Issue
& expected_issue1
= mojo_issue1
.To
<Issue
>();
287 EXPECT_CALL(issue_observer1
,
288 OnIssueUpdated(Pointee(EqualsIssue(expected_issue1
))))
289 .WillOnce(SaveArg
<0>(&issue
));
290 EXPECT_CALL(issue_observer2
,
291 OnIssueUpdated(Pointee(EqualsIssue(expected_issue1
))));
292 media_router_proxy_
->OnIssue(mojo_issue1
.Pass());
295 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&issue_observer1
));
296 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&issue_observer2
));
298 EXPECT_CALL(issue_observer2
, OnIssueUpdated(nullptr));
299 router()->ClearIssue(issue
->id());
300 router()->UnregisterIssuesObserver(&issue_observer1
);
301 interfaces::IssuePtr mojo_issue2
= CreateMojoIssue("title 2");
302 const Issue
& expected_issue2
= mojo_issue2
.To
<Issue
>();
304 EXPECT_CALL(issue_observer2
,
305 OnIssueUpdated(Pointee(EqualsIssue(expected_issue2
))));
306 media_router_proxy_
->OnIssue(mojo_issue2
.Pass());
310 TEST_F(MediaRouterMojoImplTest
, RegisterAndUnregisterMediaSinksObserver
) {
311 MediaSource
media_source(kSource
);
313 MockMediaRouter mock_router
;
314 EXPECT_CALL(mock_media_route_provider_
,
315 StartObservingMediaSinks(mojo::String(kSource
))).Times(2);
316 EXPECT_CALL(mock_media_route_provider_
,
317 StartObservingMediaSinks(mojo::String(kSource2
)));
319 MediaSinksObserver
* captured_observer
;
320 EXPECT_CALL(mock_router
, RegisterMediaSinksObserver(_
))
322 .WillRepeatedly(SaveArg
<0>(&captured_observer
));
324 MockMediaSinksObserver
sinks_observer(&mock_router
, media_source
);
325 EXPECT_EQ(&sinks_observer
, captured_observer
);
326 router()->RegisterMediaSinksObserver(&sinks_observer
);
327 MockMediaSinksObserver
extra_sinks_observer(&mock_router
, media_source
);
328 EXPECT_EQ(&extra_sinks_observer
, captured_observer
);
329 router()->RegisterMediaSinksObserver(&extra_sinks_observer
);
330 MockMediaSinksObserver
unrelated_sinks_observer(&mock_router
,
331 MediaSource(kSource2
));
332 EXPECT_EQ(&unrelated_sinks_observer
, captured_observer
);
333 router()->RegisterMediaSinksObserver(&unrelated_sinks_observer
);
335 std::vector
<MediaSink
> expected_sinks
;
336 expected_sinks
.push_back(
337 MediaSink(kSinkId
, kSinkName
, MediaSink::IconType::CAST
));
338 expected_sinks
.push_back(
339 MediaSink(kSinkId2
, kSinkName
, MediaSink::IconType::CAST
));
341 mojo::Array
<interfaces::MediaSinkPtr
> mojo_sinks(2);
342 mojo_sinks
[0] = interfaces::MediaSink::New();
343 mojo_sinks
[0]->sink_id
= kSinkId
;
344 mojo_sinks
[0]->name
= kSinkName
;
345 mojo_sinks
[0]->icon_type
=
346 media_router::interfaces::MediaSink::IconType::ICON_TYPE_CAST
;
347 mojo_sinks
[1] = interfaces::MediaSink::New();
348 mojo_sinks
[1]->sink_id
= kSinkId2
;
349 mojo_sinks
[1]->name
= kSinkName
;
350 mojo_sinks
[1]->icon_type
=
351 media_router::interfaces::MediaSink::IconType::ICON_TYPE_CAST
;
353 EXPECT_CALL(sinks_observer
, OnSinksReceived(SequenceEquals(expected_sinks
)));
354 EXPECT_CALL(extra_sinks_observer
,
355 OnSinksReceived(SequenceEquals(expected_sinks
)));
356 media_router_proxy_
->OnSinksReceived(media_source
.id(), mojo_sinks
.Pass());
359 EXPECT_CALL(mock_router
, UnregisterMediaSinksObserver(&sinks_observer
));
360 EXPECT_CALL(mock_router
, UnregisterMediaSinksObserver(&extra_sinks_observer
));
361 EXPECT_CALL(mock_router
,
362 UnregisterMediaSinksObserver(&unrelated_sinks_observer
));
363 EXPECT_CALL(mock_media_route_provider_
,
364 StopObservingMediaSinks(mojo::String(kSource
)));
365 EXPECT_CALL(mock_media_route_provider_
,
366 StopObservingMediaSinks(mojo::String(kSource2
)));
367 router()->UnregisterMediaSinksObserver(&sinks_observer
);
368 router()->UnregisterMediaSinksObserver(&extra_sinks_observer
);
369 router()->UnregisterMediaSinksObserver(&unrelated_sinks_observer
);
373 TEST_F(MediaRouterMojoImplTest
, RegisterAndUnregisterMediaRoutesObserver
) {
374 MockMediaRouter mock_router
;
375 EXPECT_CALL(mock_media_route_provider_
, StartObservingMediaRoutes()).Times(2);
377 MediaRoutesObserver
* observer_captured
;
378 EXPECT_CALL(mock_router
, RegisterMediaRoutesObserver(_
))
380 .WillRepeatedly(SaveArg
<0>(&observer_captured
));
381 MockMediaRoutesObserver
routes_observer(&mock_router
);
382 EXPECT_EQ(observer_captured
, &routes_observer
);
383 MockMediaRoutesObserver
extra_routes_observer(&mock_router
);
384 EXPECT_EQ(observer_captured
, &extra_routes_observer
);
385 router()->RegisterMediaRoutesObserver(&routes_observer
);
386 router()->RegisterMediaRoutesObserver(&extra_routes_observer
);
388 std::vector
<MediaRoute
> expected_routes
;
389 expected_routes
.push_back(MediaRoute(kRouteId
, MediaSource(kSource
), kSinkId
,
390 kDescription
, false, "", false));
391 expected_routes
.push_back(MediaRoute(kRouteId2
, MediaSource(kSource
), kSinkId
,
392 kDescription
, false, "", false));
394 mojo::Array
<interfaces::MediaRoutePtr
> mojo_routes(2);
395 mojo_routes
[0] = interfaces::MediaRoute::New();
396 mojo_routes
[0]->media_route_id
= kRouteId
;
397 mojo_routes
[0]->media_source
= kSource
;
398 mojo_routes
[0]->media_sink_id
= kSinkId
;
399 mojo_routes
[0]->description
= kDescription
;
400 mojo_routes
[0]->is_local
= false;
401 mojo_routes
[1] = interfaces::MediaRoute::New();
402 mojo_routes
[1]->media_route_id
= kRouteId2
;
403 mojo_routes
[1]->media_source
= kSource
;
404 mojo_routes
[1]->media_sink_id
= kSinkId
;
405 mojo_routes
[1]->description
= kDescription
;
406 mojo_routes
[1]->is_local
= false;
408 EXPECT_CALL(routes_observer
,
409 OnRoutesUpdated(SequenceEquals(expected_routes
)));
410 EXPECT_CALL(extra_routes_observer
,
411 OnRoutesUpdated(SequenceEquals(expected_routes
)));
412 media_router_proxy_
->OnRoutesUpdated(mojo_routes
.Pass());
415 EXPECT_CALL(mock_router
, UnregisterMediaRoutesObserver(&routes_observer
));
416 EXPECT_CALL(mock_router
,
417 UnregisterMediaRoutesObserver(&extra_routes_observer
));
418 router()->UnregisterMediaRoutesObserver(&routes_observer
);
419 router()->UnregisterMediaRoutesObserver(&extra_routes_observer
);
420 EXPECT_CALL(mock_media_route_provider_
, StopObservingMediaRoutes());
424 TEST_F(MediaRouterMojoImplTest
, SendRouteMessage
) {
426 mock_media_route_provider_
,
427 SendRouteMessage(mojo::String(kRouteId
), mojo::String(kMessage
), _
))
429 const MediaRoute::Id
& route_id
, const std::string
& message
,
430 const interfaces::MediaRouteProvider::SendRouteMessageCallback
& cb
) {
434 SendMessageCallbackHandler handler
;
435 EXPECT_CALL(handler
, Invoke(true));
436 router()->SendRouteMessage(kRouteId
, kMessage
,
437 base::Bind(&SendMessageCallbackHandler::Invoke
,
438 base::Unretained(&handler
)));
442 TEST_F(MediaRouterMojoImplTest
, SendRouteBinaryMessage
) {
443 scoped_ptr
<std::vector
<uint8
>> expected_binary_data(new std::vector
<uint8
>(
444 kBinaryMessage
, kBinaryMessage
+ arraysize(kBinaryMessage
)));
446 EXPECT_CALL(mock_media_route_provider_
,
447 SendRouteBinaryMessageInternal(mojo::String(kRouteId
), _
, _
))
449 const MediaRoute::Id
& route_id
, const std::vector
<uint8
>& data
,
450 const interfaces::MediaRouteProvider::SendRouteMessageCallback
& cb
) {
452 0, memcmp(kBinaryMessage
, &(data
[0]), arraysize(kBinaryMessage
)));
456 SendMessageCallbackHandler handler
;
457 EXPECT_CALL(handler
, Invoke(true));
458 router()->SendRouteBinaryMessage(
459 kRouteId
, expected_binary_data
.Pass(),
460 base::Bind(&SendMessageCallbackHandler::Invoke
,
461 base::Unretained(&handler
)));
465 TEST_F(MediaRouterMojoImplTest
, PresentationSessionMessagesSingleObserver
) {
466 mojo::Array
<interfaces::RouteMessagePtr
> mojo_messages(2);
467 mojo_messages
[0] = interfaces::RouteMessage::New();
468 mojo_messages
[0]->type
= interfaces::RouteMessage::Type::TYPE_TEXT
;
469 mojo_messages
[0]->message
= "text";
470 mojo_messages
[1] = interfaces::RouteMessage::New();
471 mojo_messages
[1]->type
= interfaces::RouteMessage::Type::TYPE_BINARY
;
472 mojo_messages
[1]->data
.push_back(1);
474 ScopedVector
<content::PresentationSessionMessage
> expected_messages
;
475 scoped_ptr
<content::PresentationSessionMessage
> message
;
476 message
.reset(new content::PresentationSessionMessage(
477 content::PresentationMessageType::TEXT
));
478 message
->message
= "text";
479 expected_messages
.push_back(message
.Pass());
481 message
.reset(new content::PresentationSessionMessage(
482 content::PresentationMessageType::ARRAY_BUFFER
));
483 message
->data
.reset(new std::vector
<uint8_t>(1, 1));
484 expected_messages
.push_back(message
.Pass());
486 MediaRoute::Id
expected_route_id("foo");
487 interfaces::MediaRouteProvider::ListenForRouteMessagesCallback mojo_callback
;
488 EXPECT_CALL(mock_media_route_provider_
,
489 ListenForRouteMessages(Eq(expected_route_id
), _
))
490 .WillOnce(SaveArg
<1>(&mojo_callback
));
492 // |pass_ownership| param is "true" here because there is only one observer.
493 ListenForMessagesCallbackHandler
handler(expected_messages
.Pass(), true);
495 EXPECT_CALL(handler
, InvokeObserver());
496 // Creating PresentationSessionMessagesObserver will register itself to the
497 // MediaRouter, which in turn will start listening for route messages.
498 scoped_ptr
<PresentationSessionMessagesObserver
> observer(
499 new PresentationSessionMessagesObserver(
500 base::Bind(&ListenForMessagesCallbackHandler::Invoke
,
501 base::Unretained(&handler
)),
502 expected_route_id
, router()));
505 // Simulate messages by invoking the saved mojo callback.
506 // We expect one more ListenForRouteMessages call since |observer| was
507 // still registered when the first set of messages arrived.
508 mojo_callback
.Run(mojo_messages
.Pass(), false);
509 interfaces::MediaRouteProvider::ListenForRouteMessagesCallback
511 EXPECT_CALL(mock_media_route_provider_
, ListenForRouteMessages(_
, _
))
512 .WillOnce(SaveArg
<1>(&mojo_callback_2
));
515 // Stop listening for messages. In particular, MediaRouterMojoImpl will not
516 // call ListenForRouteMessages again when it sees there are no more observers.
517 mojo::Array
<interfaces::RouteMessagePtr
> mojo_messages_2(1);
518 mojo_messages_2
[0] = interfaces::RouteMessage::New();
519 mojo_messages_2
[0]->type
= interfaces::RouteMessage::Type::TYPE_TEXT
;
520 mojo_messages_2
[0]->message
= "foo";
522 mojo_callback_2
.Run(mojo_messages_2
.Pass(), false);
526 TEST_F(MediaRouterMojoImplTest
, PresentationSessionMessagesMultipleObservers
) {
527 mojo::Array
<interfaces::RouteMessagePtr
> mojo_messages(2);
528 mojo_messages
[0] = interfaces::RouteMessage::New();
529 mojo_messages
[0]->type
= interfaces::RouteMessage::Type::TYPE_TEXT
;
530 mojo_messages
[0]->message
= "text";
531 mojo_messages
[1] = interfaces::RouteMessage::New();
532 mojo_messages
[1]->type
= interfaces::RouteMessage::Type::TYPE_BINARY
;
533 mojo_messages
[1]->data
.push_back(1);
535 ScopedVector
<content::PresentationSessionMessage
> expected_messages
;
536 scoped_ptr
<content::PresentationSessionMessage
> message
;
537 message
.reset(new content::PresentationSessionMessage(
538 content::PresentationMessageType::TEXT
));
539 message
->message
= "text";
540 expected_messages
.push_back(message
.Pass());
542 message
.reset(new content::PresentationSessionMessage(
543 content::PresentationMessageType::ARRAY_BUFFER
));
544 message
->data
.reset(new std::vector
<uint8_t>(1, 1));
545 expected_messages
.push_back(message
.Pass());
547 MediaRoute::Id
expected_route_id("foo");
548 interfaces::MediaRouteProvider::ListenForRouteMessagesCallback mojo_callback
;
549 EXPECT_CALL(mock_media_route_provider_
,
550 ListenForRouteMessages(Eq(expected_route_id
), _
))
551 .WillOnce(SaveArg
<1>(&mojo_callback
));
553 // |pass_ownership| param is "false" here because there are more than one
555 ListenForMessagesCallbackHandler
handler(expected_messages
.Pass(), false);
557 EXPECT_CALL(handler
, InvokeObserver()).Times(2);
558 // Creating PresentationSessionMessagesObserver will register itself to the
559 // MediaRouter, which in turn will start listening for route messages.
560 scoped_ptr
<PresentationSessionMessagesObserver
> observer1(
561 new PresentationSessionMessagesObserver(
562 base::Bind(&ListenForMessagesCallbackHandler::Invoke
,
563 base::Unretained(&handler
)),
564 expected_route_id
, router()));
565 scoped_ptr
<PresentationSessionMessagesObserver
> observer2(
566 new PresentationSessionMessagesObserver(
567 base::Bind(&ListenForMessagesCallbackHandler::Invoke
,
568 base::Unretained(&handler
)),
569 expected_route_id
, router()));
572 // Simulate messages by invoking the saved mojo callback.
573 // We expect one more ListenForRouteMessages call since |observer| was
574 // still registered when the first set of messages arrived.
575 mojo_callback
.Run(mojo_messages
.Pass(), false);
576 interfaces::MediaRouteProvider::ListenForRouteMessagesCallback
578 EXPECT_CALL(mock_media_route_provider_
, ListenForRouteMessages(_
, _
))
579 .WillOnce(SaveArg
<1>(&mojo_callback_2
));
582 // Stop listening for messages. In particular, MediaRouterMojoImpl will not
583 // call ListenForRouteMessages again when it sees there are no more observers.
584 mojo::Array
<interfaces::RouteMessagePtr
> mojo_messages_2(1);
585 mojo_messages_2
[0] = interfaces::RouteMessage::New();
586 mojo_messages_2
[0]->type
= interfaces::RouteMessage::Type::TYPE_TEXT
;
587 mojo_messages_2
[0]->message
= "foo";
590 mojo_callback_2
.Run(mojo_messages_2
.Pass(), false);
594 TEST_F(MediaRouterMojoImplTest
, PresentationSessionMessagesError
) {
595 MediaRoute::Id
expected_route_id("foo");
596 interfaces::MediaRouteProvider::ListenForRouteMessagesCallback mojo_callback
;
597 EXPECT_CALL(mock_media_route_provider_
,
598 ListenForRouteMessages(Eq(expected_route_id
), _
))
599 .WillOnce(SaveArg
<1>(&mojo_callback
));
601 ListenForMessagesCallbackHandler
handler(
602 ScopedVector
<content::PresentationSessionMessage
>(), true);
604 // Creating PresentationSessionMessagesObserver will register itself to the
605 // MediaRouter, which in turn will start listening for route messages.
606 scoped_ptr
<PresentationSessionMessagesObserver
> observer1(
607 new PresentationSessionMessagesObserver(
608 base::Bind(&ListenForMessagesCallbackHandler::Invoke
,
609 base::Unretained(&handler
)),
610 expected_route_id
, router()));
613 mojo_callback
.Run(mojo::Array
<interfaces::RouteMessagePtr
>(0), true);
617 TEST_F(MediaRouterMojoImplTest
, QueuedWhileAsleep
) {
618 EXPECT_CALL(mock_event_page_tracker_
, IsEventPageSuspended(extension_id()))
620 .WillRepeatedly(Return(true));
621 EXPECT_CALL(mock_event_page_tracker_
, WakeEventPage(extension_id(), _
))
623 .WillRepeatedly(Return(true));
624 router()->CloseRoute(kRouteId
);
625 router()->CloseRoute(kRouteId2
);
627 EXPECT_CALL(mock_event_page_tracker_
, IsEventPageSuspended(extension_id()))
629 .WillRepeatedly(Return(false));
630 EXPECT_CALL(mock_media_route_provider_
, CloseRoute(mojo::String(kRouteId
)));
631 EXPECT_CALL(mock_media_route_provider_
, CloseRoute(mojo::String(kRouteId2
)));
632 ConnectProviderManagerService();
636 // Temporarily disabled until the issues with extension system teardown
638 // TODO(kmarshall): Re-enable this test. (http://crbug.com/490468)
639 TEST(MediaRouterMojoExtensionTest
, DISABLED_DeferredBindingAndSuspension
) {
640 base::MessageLoop
message_loop(mojo::common::MessagePumpMojo::Create());
642 // Set up a mock ProcessManager instance.
643 TestingProfile profile
;
644 extensions::ProcessManagerFactory::GetInstance()->SetTestingFactory(
645 &profile
, &TestProcessManager::Create
);
646 TestProcessManager
* process_manager
= static_cast<TestProcessManager
*>(
647 extensions::ProcessManager::Get(&profile
));
649 // Create MR and its proxy, so that it can be accessed through Mojo.
650 MediaRouterMojoImpl
media_router(process_manager
);
651 interfaces::MediaRouterPtr media_router_proxy
;
653 // Create a client object and its Mojo proxy.
654 testing::StrictMock
<MockMediaRouteProvider
> mock_media_route_provider
;
655 interfaces::MediaRouteProviderPtr media_route_provider_proxy
;
657 // CloseRoute is called before *any* extension has connected.
658 // It should be queued.
659 media_router
.CloseRoute(kRouteId
);
661 // Construct bindings so that |media_router| delegates calls to
662 // |mojo_media_router|, which are then handled by
663 // |mock_media_route_provider_service|.
664 scoped_ptr
<mojo::Binding
<interfaces::MediaRouteProvider
>> binding(
665 new mojo::Binding
<interfaces::MediaRouteProvider
>(
666 &mock_media_route_provider
,
667 mojo::GetProxy(&media_route_provider_proxy
)));
668 media_router
.BindToMojoRequest(mojo::GetProxy(&media_router_proxy
),
671 // |mojo_media_router| signals its readiness to the MR by registering
672 // itself via RegisterMediaRouteProvider().
673 // Now that the |media_router| and |mojo_media_router| are fully initialized,
674 // the queued CloseRoute() call should be executed.
675 RegisterMediaRouteProviderHandler provide_handler
;
676 EXPECT_CALL(provide_handler
, Invoke(testing::Not("")));
677 EXPECT_CALL(*process_manager
, IsEventPageSuspended(kExtensionId
))
678 .WillOnce(Return(false));
679 EXPECT_CALL(mock_media_route_provider
, CloseRoute(mojo::String(kRouteId
)));
680 media_router_proxy
->RegisterMediaRouteProvider(
681 media_route_provider_proxy
.Pass(),
682 base::Bind(&RegisterMediaRouteProviderHandler::Invoke
,
683 base::Unretained(&provide_handler
)));
684 message_loop
.RunUntilIdle();
686 // Extension is suspended and re-awoken.
688 media_router
.BindToMojoRequest(mojo::GetProxy(&media_router_proxy
),
690 EXPECT_CALL(*process_manager
, IsEventPageSuspended(kExtensionId
))
691 .WillOnce(Return(true));
692 EXPECT_CALL(*process_manager
, WakeEventPage(kExtensionId
, _
))
693 .WillOnce(testing::DoAll(media::RunCallback
<1>(true), Return(true)));
694 media_router
.CloseRoute(kRouteId2
);
695 message_loop
.RunUntilIdle();
697 // RegisterMediaRouteProvider() is called.
698 // The queued CloseRoute(kRouteId2) call should be executed.
699 EXPECT_CALL(provide_handler
, Invoke(testing::Not("")));
700 EXPECT_CALL(*process_manager
, IsEventPageSuspended(kExtensionId
))
701 .WillOnce(Return(false));
702 EXPECT_CALL(mock_media_route_provider
, CloseRoute(mojo::String(kRouteId2
)));
703 binding
.reset(new mojo::Binding
<interfaces::MediaRouteProvider
>(
704 &mock_media_route_provider
, mojo::GetProxy(&media_route_provider_proxy
)));
705 media_router_proxy
->RegisterMediaRouteProvider(
706 media_route_provider_proxy
.Pass(),
707 base::Bind(&RegisterMediaRouteProviderHandler::Invoke
,
708 base::Unretained(&provide_handler
)));
709 message_loop
.RunUntilIdle();
712 } // namespace media_router