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

Side by Side Diff: chrome/browser/media/router/mojo/media_router_mojo_impl_unittest.cc

Issue 2111303003: [Media Router] Replace route messaging API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 4 years, 5 months 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <memory> 8 #include <memory>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
(...skipping 981 matching lines...) Expand 10 before | Expand all | Expand 10 after
992 message->message = "text"; 992 message->message = "text";
993 expected_messages.push_back(std::move(message)); 993 expected_messages.push_back(std::move(message));
994 994
995 message.reset(new content::PresentationSessionMessage( 995 message.reset(new content::PresentationSessionMessage(
996 content::PresentationMessageType::ARRAY_BUFFER)); 996 content::PresentationMessageType::ARRAY_BUFFER));
997 message->data.reset(new std::vector<uint8_t>(1, 1)); 997 message->data.reset(new std::vector<uint8_t>(1, 1));
998 expected_messages.push_back(std::move(message)); 998 expected_messages.push_back(std::move(message));
999 999
1000 base::RunLoop run_loop; 1000 base::RunLoop run_loop;
1001 MediaRoute::Id expected_route_id("foo"); 1001 MediaRoute::Id expected_route_id("foo");
1002 interfaces::MediaRouteProvider::ListenForRouteMessagesCallback mojo_callback;
1003 EXPECT_CALL(mock_media_route_provider_, 1002 EXPECT_CALL(mock_media_route_provider_,
1004 ListenForRouteMessages(Eq(expected_route_id), _)) 1003 StartListeningForRouteMessages(Eq(expected_route_id)))
1005 .WillOnce(DoAll(SaveArg<1>(&mojo_callback), 1004 .WillOnce(InvokeWithoutArgs([&run_loop]() { run_loop.Quit(); }));
1006 InvokeWithoutArgs([&run_loop]() { run_loop.Quit(); })));
1007 1005
1008 // |pass_ownership| param is "true" here because there is only one observer. 1006 // |pass_ownership| param is |true| here because there is only one observer.
1009 ListenForMessagesCallbackHandler handler(std::move(expected_messages), true); 1007 ListenForMessagesCallbackHandler handler(std::move(expected_messages), true);
1010 1008
1011 EXPECT_CALL(handler, InvokeObserver());
1012 // Creating PresentationSessionMessagesObserver will register itself to the 1009 // Creating PresentationSessionMessagesObserver will register itself to the
1013 // MediaRouter, which in turn will start listening for route messages. 1010 // MediaRouter, which in turn will start listening for route messages.
1014 std::unique_ptr<PresentationSessionMessagesObserver> observer( 1011 std::unique_ptr<PresentationSessionMessagesObserver> observer(
1015 new PresentationSessionMessagesObserver( 1012 new PresentationSessionMessagesObserver(
1016 base::Bind(&ListenForMessagesCallbackHandler::Invoke, 1013 base::Bind(&ListenForMessagesCallbackHandler::Invoke,
1017 base::Unretained(&handler)), 1014 base::Unretained(&handler)),
1018 expected_route_id, router())); 1015 expected_route_id, router()));
1019 run_loop.Run(); 1016 run_loop.Run();
1020 1017
1021 base::RunLoop run_loop2; 1018 EXPECT_CALL(handler, InvokeObserver());
1022 // Simulate messages by invoking the saved mojo callback. 1019 router()->OnRouteMessagesReceived(expected_route_id,
1023 // We expect one more ListenForRouteMessages call since |observer| was 1020 std::move(mojo_messages));
1024 // still registered when the first set of messages arrived.
1025 mojo_callback.Run(std::move(mojo_messages), false);
1026 interfaces::MediaRouteProvider::ListenForRouteMessagesCallback
1027 mojo_callback_2;
1028 EXPECT_CALL(mock_media_route_provider_, ListenForRouteMessages(_, _))
1029 .WillOnce(DoAll(SaveArg<1>(&mojo_callback_2),
1030 InvokeWithoutArgs([&run_loop2]() { run_loop2.Quit(); })));
1031 run_loop2.Run();
1032
1033 base::RunLoop run_loop3;
1034 // Stop listening for messages. In particular, MediaRouterMojoImpl will not
1035 // call ListenForRouteMessages again when it sees there are no more observers.
1036 mojo::Array<interfaces::RouteMessagePtr> mojo_messages_2(1);
1037 mojo_messages_2[0] = interfaces::RouteMessage::New();
1038 mojo_messages_2[0]->type = interfaces::RouteMessage::Type::TEXT;
1039 mojo_messages_2[0]->message = "foo";
1040 observer.reset();
1041 mojo_callback_2.Run(std::move(mojo_messages_2), false);
1042 EXPECT_CALL(mock_media_route_provider_, StopListeningForRouteMessages(_))
1043 .WillOnce(InvokeWithoutArgs([&run_loop3]() { run_loop3.Quit(); }));
1044 run_loop3.Run();
1045 } 1021 }
1046 1022
1047 TEST_F(MediaRouterMojoImplTest, PresentationSessionMessagesMultipleObservers) { 1023 TEST_F(MediaRouterMojoImplTest, PresentationSessionMessagesMultipleObservers) {
1048 mojo::Array<interfaces::RouteMessagePtr> mojo_messages(2); 1024 mojo::Array<interfaces::RouteMessagePtr> mojo_messages(2);
1049 mojo_messages[0] = interfaces::RouteMessage::New(); 1025 mojo_messages[0] = interfaces::RouteMessage::New();
1050 mojo_messages[0]->type = interfaces::RouteMessage::Type::TEXT; 1026 mojo_messages[0]->type = interfaces::RouteMessage::Type::TEXT;
1051 mojo_messages[0]->message = "text"; 1027 mojo_messages[0]->message = "text";
1052 mojo_messages[1] = interfaces::RouteMessage::New(); 1028 mojo_messages[1] = interfaces::RouteMessage::New();
1053 mojo_messages[1]->type = interfaces::RouteMessage::Type::BINARY; 1029 mojo_messages[1]->type = interfaces::RouteMessage::Type::BINARY;
1054 mojo_messages[1]->data.push_back(1); 1030 mojo_messages[1]->data.push_back(1);
1055 1031
1056 ScopedVector<content::PresentationSessionMessage> expected_messages; 1032 ScopedVector<content::PresentationSessionMessage> expected_messages;
1057 std::unique_ptr<content::PresentationSessionMessage> message; 1033 std::unique_ptr<content::PresentationSessionMessage> message;
1058 message.reset(new content::PresentationSessionMessage( 1034 message.reset(new content::PresentationSessionMessage(
1059 content::PresentationMessageType::TEXT)); 1035 content::PresentationMessageType::TEXT));
1060 message->message = "text"; 1036 message->message = "text";
1061 expected_messages.push_back(std::move(message)); 1037 expected_messages.push_back(std::move(message));
1062 1038
1063 message.reset(new content::PresentationSessionMessage( 1039 message.reset(new content::PresentationSessionMessage(
1064 content::PresentationMessageType::ARRAY_BUFFER)); 1040 content::PresentationMessageType::ARRAY_BUFFER));
1065 message->data.reset(new std::vector<uint8_t>(1, 1)); 1041 message->data.reset(new std::vector<uint8_t>(1, 1));
1066 expected_messages.push_back(std::move(message)); 1042 expected_messages.push_back(std::move(message));
1067 1043
1068 base::RunLoop run_loop; 1044 base::RunLoop run_loop;
1069 MediaRoute::Id expected_route_id("foo"); 1045 MediaRoute::Id expected_route_id("foo");
1070 interfaces::MediaRouteProvider::ListenForRouteMessagesCallback mojo_callback;
1071 EXPECT_CALL(mock_media_route_provider_, 1046 EXPECT_CALL(mock_media_route_provider_,
1072 ListenForRouteMessages(Eq(expected_route_id), _)) 1047 StartListeningForRouteMessages(Eq(expected_route_id)))
1073 .WillOnce(DoAll(SaveArg<1>(&mojo_callback), 1048 .WillOnce(InvokeWithoutArgs([&run_loop]() { run_loop.Quit(); }));
1074 InvokeWithoutArgs([&run_loop]() { run_loop.Quit(); })));
1075 1049
1076 // |pass_ownership| param is "false" here because there are more than one 1050 // |pass_ownership| param is |false| here because there are more than one
1077 // observers. 1051 // observers.
1078 ListenForMessagesCallbackHandler handler(std::move(expected_messages), false); 1052 ListenForMessagesCallbackHandler handler(std::move(expected_messages), false);
1079 1053
1080 EXPECT_CALL(handler, InvokeObserver()).Times(2);
1081 // Creating PresentationSessionMessagesObserver will register itself to the 1054 // Creating PresentationSessionMessagesObserver will register itself to the
1082 // MediaRouter, which in turn will start listening for route messages. 1055 // MediaRouter, which in turn will start listening for route messages.
1083 std::unique_ptr<PresentationSessionMessagesObserver> observer1( 1056 std::unique_ptr<PresentationSessionMessagesObserver> observer1(
1084 new PresentationSessionMessagesObserver( 1057 new PresentationSessionMessagesObserver(
1085 base::Bind(&ListenForMessagesCallbackHandler::Invoke, 1058 base::Bind(&ListenForMessagesCallbackHandler::Invoke,
1086 base::Unretained(&handler)), 1059 base::Unretained(&handler)),
1087 expected_route_id, router())); 1060 expected_route_id, router()));
1088 std::unique_ptr<PresentationSessionMessagesObserver> observer2( 1061 std::unique_ptr<PresentationSessionMessagesObserver> observer2(
1089 new PresentationSessionMessagesObserver( 1062 new PresentationSessionMessagesObserver(
1090 base::Bind(&ListenForMessagesCallbackHandler::Invoke, 1063 base::Bind(&ListenForMessagesCallbackHandler::Invoke,
1091 base::Unretained(&handler)), 1064 base::Unretained(&handler)),
1092 expected_route_id, router())); 1065 expected_route_id, router()));
1093 run_loop.Run(); 1066 run_loop.Run();
1094 1067
1095 base::RunLoop run_loop2; 1068 EXPECT_CALL(handler, InvokeObserver()).Times(2);
1096 // Simulate messages by invoking the saved mojo callback. 1069 router()->OnRouteMessagesReceived(expected_route_id,
1097 // We expect one more ListenForRouteMessages call since |observer| was 1070 std::move(mojo_messages));
1098 // still registered when the first set of messages arrived.
1099 mojo_callback.Run(std::move(mojo_messages), false);
1100 interfaces::MediaRouteProvider::ListenForRouteMessagesCallback
1101 mojo_callback_2;
1102 EXPECT_CALL(mock_media_route_provider_, ListenForRouteMessages(_, _))
1103 .WillOnce(DoAll(SaveArg<1>(&mojo_callback_2),
1104 InvokeWithoutArgs([&run_loop2]() { run_loop2.Quit(); })));
1105 run_loop2.Run();
1106
1107 base::RunLoop run_loop3;
1108 // Stop listening for messages. In particular, MediaRouterMojoImpl will not
1109 // call ListenForRouteMessages again when it sees there are no more observers.
1110 mojo::Array<interfaces::RouteMessagePtr> mojo_messages_2(1);
1111 mojo_messages_2[0] = interfaces::RouteMessage::New();
1112 mojo_messages_2[0]->type = interfaces::RouteMessage::Type::TEXT;
1113 mojo_messages_2[0]->message = "foo";
1114 observer1.reset();
1115 observer2.reset();
1116 mojo_callback_2.Run(std::move(mojo_messages_2), false);
1117 EXPECT_CALL(mock_media_route_provider_, StopListeningForRouteMessages(_))
1118 .WillOnce(InvokeWithoutArgs([&run_loop3]() { run_loop3.Quit(); }));
1119 run_loop3.Run();
1120 }
1121
1122 TEST_F(MediaRouterMojoImplTest, PresentationSessionMessagesError) {
1123 MediaRoute::Id expected_route_id("foo");
1124 interfaces::MediaRouteProvider::ListenForRouteMessagesCallback mojo_callback;
1125 base::RunLoop run_loop;
1126 EXPECT_CALL(mock_media_route_provider_,
1127 ListenForRouteMessages(Eq(expected_route_id), _))
1128 .WillOnce(DoAll(SaveArg<1>(&mojo_callback),
1129 InvokeWithoutArgs([&run_loop]() { run_loop.Quit(); })));
1130
1131 ListenForMessagesCallbackHandler handler(
1132 ScopedVector<content::PresentationSessionMessage>(), true);
1133
1134 // Creating PresentationSessionMessagesObserver will register itself to the
1135 // MediaRouter, which in turn will start listening for route messages.
1136 std::unique_ptr<PresentationSessionMessagesObserver> observer1(
1137 new PresentationSessionMessagesObserver(
1138 base::Bind(&ListenForMessagesCallbackHandler::Invoke,
1139 base::Unretained(&handler)),
1140 expected_route_id, router()));
1141 run_loop.Run();
1142
1143 mojo_callback.Run(mojo::Array<interfaces::RouteMessagePtr>(), true);
1144 ProcessEventLoop();
1145 } 1071 }
1146 1072
1147 TEST_F(MediaRouterMojoImplTest, PresentationConnectionStateChangedCallback) { 1073 TEST_F(MediaRouterMojoImplTest, PresentationConnectionStateChangedCallback) {
1148 MediaRoute::Id route_id("route-id"); 1074 MediaRoute::Id route_id("route-id");
1149 const std::string kPresentationUrl("http://foo.fakeUrl"); 1075 const std::string kPresentationUrl("http://foo.fakeUrl");
1150 const std::string kPresentationId("pid"); 1076 const std::string kPresentationId("pid");
1151 content::PresentationSessionInfo connection(kPresentationUrl, 1077 content::PresentationSessionInfo connection(kPresentationUrl,
1152 kPresentationId); 1078 kPresentationId);
1153 MockPresentationConnectionStateChangedCallback callback; 1079 MockPresentationConnectionStateChangedCallback callback;
1154 std::unique_ptr<PresentationConnectionStateSubscription> subscription = 1080 std::unique_ptr<PresentationConnectionStateSubscription> subscription =
(...skipping 475 matching lines...) Expand 10 before | Expand all | Expand 10 after
1630 EXPECT_CALL(mock_media_route_provider_, 1556 EXPECT_CALL(mock_media_route_provider_,
1631 UpdateMediaSinks(mojo::String(MediaSourceForDesktop().id()))) 1557 UpdateMediaSinks(mojo::String(MediaSourceForDesktop().id())))
1632 .WillOnce(InvokeWithoutArgs([&run_loop2]() { 1558 .WillOnce(InvokeWithoutArgs([&run_loop2]() {
1633 run_loop2.Quit(); 1559 run_loop2.Quit();
1634 })); 1560 }));
1635 1561
1636 run_loop2.Run(); 1562 run_loop2.Run();
1637 } 1563 }
1638 1564
1639 } // namespace media_router 1565 } // namespace media_router
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698