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

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

Powered by Google App Engine
This is Rietveld 408576698