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

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

Issue 2947403004: [MediaRouter] Replace RouteMessage with PresentationConnectionMessage (Closed)
Patch Set: fix android Created 3 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>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/bind_helpers.h" 13 #include "base/bind_helpers.h"
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
16 #include "base/memory/ref_counted.h" 16 #include "base/memory/ref_counted.h"
17 #include "base/run_loop.h" 17 #include "base/run_loop.h"
18 #include "base/test/histogram_tester.h" 18 #include "base/test/histogram_tester.h"
19 #include "base/test/mock_callback.h" 19 #include "base/test/mock_callback.h"
20 #include "base/threading/thread_task_runner_handle.h" 20 #include "base/threading/thread_task_runner_handle.h"
21 #include "chrome/browser/media/router/mock_media_router.h" 21 #include "chrome/browser/media/router/mock_media_router.h"
22 #include "chrome/browser/media/router/mojo/media_router_mojo_metrics.h" 22 #include "chrome/browser/media/router/mojo/media_router_mojo_metrics.h"
23 #include "chrome/browser/media/router/mojo/media_router_mojo_test.h" 23 #include "chrome/browser/media/router/mojo/media_router_mojo_test.h"
24 #include "chrome/browser/media/router/route_message_observer.h" 24 #include "chrome/browser/media/router/route_message_observer.h"
25 #include "chrome/browser/media/router/test_helper.h" 25 #include "chrome/browser/media/router/test_helper.h"
26 #include "chrome/common/media_router/issue.h" 26 #include "chrome/common/media_router/issue.h"
27 #include "chrome/common/media_router/media_route.h" 27 #include "chrome/common/media_router/media_route.h"
28 #include "chrome/common/media_router/media_source_helper.h" 28 #include "chrome/common/media_router/media_source_helper.h"
29 #include "chrome/common/media_router/route_message.h"
30 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 29 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
31 #include "chrome/test/base/testing_browser_process.h" 30 #include "chrome/test/base/testing_browser_process.h"
32 #include "chrome/test/base/testing_profile.h" 31 #include "chrome/test/base/testing_profile.h"
33 #include "components/version_info/version_info.h" 32 #include "components/version_info/version_info.h"
34 #include "content/public/test/test_browser_thread_bundle.h" 33 #include "content/public/test/test_browser_thread_bundle.h"
35 #include "extensions/browser/extension_registry.h" 34 #include "extensions/browser/extension_registry.h"
36 #include "extensions/browser/process_manager.h" 35 #include "extensions/browser/process_manager.h"
37 #include "extensions/browser/process_manager_factory.h" 36 #include "extensions/browser/process_manager_factory.h"
38 #include "extensions/common/extension.h" 37 #include "extensions/common/extension.h"
39 #include "extensions/common/extension_builder.h" 38 #include "extensions/common/extension_builder.h"
(...skipping 954 matching lines...) Expand 10 before | Expand all | Expand 10 after
994 kRouteId, std::move(expected_binary_data), 993 kRouteId, std::move(expected_binary_data),
995 base::Bind(&SendMessageCallbackHandler::Invoke, 994 base::Bind(&SendMessageCallbackHandler::Invoke,
996 base::Unretained(&handler))); 995 base::Unretained(&handler)));
997 run_loop.Run(); 996 run_loop.Run();
998 } 997 }
999 998
1000 namespace { 999 namespace {
1001 1000
1002 // Used in the RouteMessages* tests to populate the messages that will be 1001 // Used in the RouteMessages* tests to populate the messages that will be
1003 // processed and dispatched to RouteMessageObservers. 1002 // processed and dispatched to RouteMessageObservers.
1004 void PopulateRouteMessages(std::vector<RouteMessage>* batch1, 1003 void PopulateRouteMessages(
1005 std::vector<RouteMessage>* batch2, 1004 std::vector<content::PresentationConnectionMessage>* batch1,
1006 std::vector<RouteMessage>* batch3) { 1005 std::vector<content::PresentationConnectionMessage>* batch2,
1007 batch1->resize(1); 1006 std::vector<content::PresentationConnectionMessage>* batch3,
1008 batch1->at(0).type = RouteMessage::TEXT; 1007 std::vector<content::PresentationConnectionMessage>* all_messages) {
1009 batch1->at(0).text = std::string("text1"); 1008 batch1->clear();
1010 batch2->resize(2); 1009 batch2->clear();
1011 batch2->at(0).type = RouteMessage::BINARY; 1010 batch3->clear();
1012 batch2->at(0).binary = std::vector<uint8_t>(1, UINT8_C(1)); 1011 batch1->emplace_back("text1");
1013 batch2->at(1).type = RouteMessage::TEXT; 1012 batch2->emplace_back(std::vector<uint8_t>(1, UINT8_C(1)));
1014 batch2->at(1).text = std::string("text2"); 1013 batch2->emplace_back("text2");
1015 batch3->resize(3); 1014 batch3->emplace_back("text3");
1016 batch3->at(0).type = RouteMessage::TEXT; 1015 batch3->emplace_back(std::vector<uint8_t>(1, UINT8_C(2)));
1017 batch3->at(0).text = std::string("text3"); 1016 batch3->emplace_back(std::vector<uint8_t>(1, UINT8_C(3)));
1018 batch3->at(1).type = RouteMessage::BINARY; 1017 all_messages->clear();
1019 batch3->at(1).binary = std::vector<uint8_t>(1, UINT8_C(2)); 1018 all_messages->insert(all_messages->end(), batch1->begin(), batch1->end());
1020 batch3->at(2).type = RouteMessage::BINARY; 1019 all_messages->insert(all_messages->end(), batch2->begin(), batch2->end());
1021 batch3->at(2).binary = std::vector<uint8_t>(1, UINT8_C(3)); 1020 all_messages->insert(all_messages->end(), batch3->begin(), batch3->end());
1022 } 1021 }
1023 1022
1024 // Used in the RouteMessages* tests to observe and sanity-check that the 1023 // Used in the RouteMessages* tests to observe and sanity-check that the
1025 // messages being received from the router are correct and in-sequence. The 1024 // messages being received from the router are correct and in-sequence. The
1026 // checks here correspond to the expected messages in PopulateRouteMessages() 1025 // checks here correspond to the expected messages in PopulateRouteMessages()
1027 // above. 1026 // above.
1028 class ExpectedMessagesObserver : public RouteMessageObserver { 1027 class ExpectedMessagesObserver : public RouteMessageObserver {
1029 public: 1028 public:
1030 ExpectedMessagesObserver(MediaRouter* router, const MediaRoute::Id& route_id) 1029 ExpectedMessagesObserver(
1031 : RouteMessageObserver(router, route_id) {} 1030 MediaRouter* router,
1031 const MediaRoute::Id& route_id,
1032 const std::vector<content::PresentationConnectionMessage>&
1033 expected_messages)
1034 : RouteMessageObserver(router, route_id),
1035 expected_messages_(expected_messages) {}
1032 1036
1033 ~ExpectedMessagesObserver() final { 1037 ~ExpectedMessagesObserver() final {
1034 CheckReceivedMessages(); 1038 CheckReceivedMessages();
1035 } 1039 }
1036 1040
1037 private: 1041 private:
1038 void OnMessagesReceived( 1042 void OnMessagesReceived(
1039 const std::vector<RouteMessage>& messages) final { 1043 const std::vector<content::PresentationConnectionMessage>& messages)
1044 final {
1040 messages_.insert(messages_.end(), messages.begin(), messages.end()); 1045 messages_.insert(messages_.end(), messages.begin(), messages.end());
1041 } 1046 }
1042 1047
1043 void CheckReceivedMessages() { 1048 void CheckReceivedMessages() {
1044 ASSERT_EQ(6u, messages_.size()); 1049 ASSERT_EQ(expected_messages_.size(), messages_.size());
1045 EXPECT_EQ(RouteMessage::TEXT, messages_[0].type); 1050 for (size_t i = 0; i < expected_messages_.size(); i++) {
1046 ASSERT_TRUE(messages_[0].text); 1051 EXPECT_EQ(expected_messages_[i], messages_[i])
1047 EXPECT_EQ("text1", *messages_[0].text); 1052 << "Message mismatch at index " << i << ": expected: "
1048 EXPECT_EQ(RouteMessage::BINARY, messages_[1].type); 1053 << PresentationConnectionMessageToString(expected_messages_[i])
1049 ASSERT_TRUE(messages_[1].binary); 1054 << ", actual: "
1050 ASSERT_EQ(1u, messages_[1].binary->size()); 1055 << PresentationConnectionMessageToString(messages_[i]);
1051 EXPECT_EQ(UINT8_C(1), messages_[1].binary->front()); 1056 }
1052 EXPECT_EQ(RouteMessage::TEXT, messages_[2].type);
1053 ASSERT_TRUE(messages_[2].text);
1054 EXPECT_EQ("text2", *messages_[2].text);
1055 EXPECT_EQ(RouteMessage::TEXT, messages_[3].type);
1056 ASSERT_TRUE(messages_[3].text);
1057 EXPECT_EQ("text3", *messages_[3].text);
1058 EXPECT_EQ(RouteMessage::BINARY, messages_[4].type);
1059 ASSERT_TRUE(messages_[4].binary);
1060 ASSERT_EQ(1u, messages_[4].binary->size());
1061 EXPECT_EQ(UINT8_C(2), messages_[4].binary->front());
1062 EXPECT_EQ(RouteMessage::BINARY, messages_[5].type);
1063 ASSERT_TRUE(messages_[5].binary);
1064 ASSERT_EQ(1u, messages_[5].binary->size());
1065 EXPECT_EQ(UINT8_C(3), messages_[5].binary->front());
1066 } 1057 }
1067 1058
1068 std::vector<RouteMessage> messages_; 1059 std::vector<content::PresentationConnectionMessage> expected_messages_;
1060 std::vector<content::PresentationConnectionMessage> messages_;
1069 }; 1061 };
1070 1062
1071 class NullMessageObserver : public RouteMessageObserver { 1063 class NullMessageObserver : public RouteMessageObserver {
1072 public: 1064 public:
1073 NullMessageObserver(MediaRouter* router, const MediaRoute::Id& route_id) 1065 NullMessageObserver(MediaRouter* router, const MediaRoute::Id& route_id)
1074 : RouteMessageObserver(router, route_id) {} 1066 : RouteMessageObserver(router, route_id) {}
1075 ~NullMessageObserver() final {} 1067 ~NullMessageObserver() final {}
1076 1068
1077 void OnMessagesReceived(const std::vector<RouteMessage>& messages) final {} 1069 void OnMessagesReceived(
1070 const std::vector<content::PresentationConnectionMessage>& messages)
1071 final {}
1078 }; 1072 };
1079 1073
1080 } // namespace 1074 } // namespace
1081 1075
1082 TEST_F(MediaRouterMojoImplTest, RouteMessagesSingleObserver) { 1076 TEST_F(MediaRouterMojoImplTest, RouteMessagesSingleObserver) {
1083 std::vector<RouteMessage> incoming_batch1, incoming_batch2, incoming_batch3; 1077 std::vector<content::PresentationConnectionMessage> incoming_batch1,
1084 PopulateRouteMessages(&incoming_batch1, &incoming_batch2, &incoming_batch3); 1078 incoming_batch2, incoming_batch3, all_messages;
1079 PopulateRouteMessages(&incoming_batch1, &incoming_batch2, &incoming_batch3,
1080 &all_messages);
1085 1081
1086 base::RunLoop run_loop; 1082 base::RunLoop run_loop;
1087 MediaRoute::Id expected_route_id("foo"); 1083 MediaRoute::Id expected_route_id("foo");
1088 EXPECT_CALL(mock_media_route_provider_, 1084 EXPECT_CALL(mock_media_route_provider_,
1089 StartListeningForRouteMessages(Eq(expected_route_id))) 1085 StartListeningForRouteMessages(Eq(expected_route_id)))
1090 .WillOnce(InvokeWithoutArgs([&run_loop]() { run_loop.Quit(); })); 1086 .WillOnce(InvokeWithoutArgs([&run_loop]() { run_loop.Quit(); }));
1091 1087
1092 // Creating ExpectedMessagesObserver will register itself to the 1088 // Creating ExpectedMessagesObserver will register itself to the
1093 // MediaRouter, which in turn will start listening for route messages. 1089 // MediaRouter, which in turn will start listening for route messages.
1094 ExpectedMessagesObserver observer(router(), expected_route_id); 1090 ExpectedMessagesObserver observer(router(), expected_route_id, all_messages);
1095 run_loop.Run(); // Will quit when StartListeningForRouteMessages() is called. 1091 run_loop.Run(); // Will quit when StartListeningForRouteMessages() is called.
1096 router()->OnRouteMessagesReceived(expected_route_id, incoming_batch1); 1092 router()->OnRouteMessagesReceived(expected_route_id, incoming_batch1);
1097 router()->OnRouteMessagesReceived(expected_route_id, incoming_batch2); 1093 router()->OnRouteMessagesReceived(expected_route_id, incoming_batch2);
1098 router()->OnRouteMessagesReceived(expected_route_id, incoming_batch3); 1094 router()->OnRouteMessagesReceived(expected_route_id, incoming_batch3);
1099 // When |observer| goes out-of-scope, its destructor will ensure all expected 1095 // When |observer| goes out-of-scope, its destructor will ensure all expected
1100 // messages have been received. 1096 // messages have been received.
1101 } 1097 }
1102 1098
1103 TEST_F(MediaRouterMojoImplTest, RouteMessagesMultipleObservers) { 1099 TEST_F(MediaRouterMojoImplTest, RouteMessagesMultipleObservers) {
1104 std::vector<RouteMessage> incoming_batch1, incoming_batch2, incoming_batch3; 1100 std::vector<content::PresentationConnectionMessage> incoming_batch1,
1105 PopulateRouteMessages(&incoming_batch1, &incoming_batch2, &incoming_batch3); 1101 incoming_batch2, incoming_batch3, all_messages;
1102 PopulateRouteMessages(&incoming_batch1, &incoming_batch2, &incoming_batch3,
1103 &all_messages);
1106 1104
1107 base::RunLoop run_loop; 1105 base::RunLoop run_loop;
1108 MediaRoute::Id expected_route_id("foo"); 1106 MediaRoute::Id expected_route_id("foo");
1109 EXPECT_CALL(mock_media_route_provider_, 1107 EXPECT_CALL(mock_media_route_provider_,
1110 StartListeningForRouteMessages(Eq(expected_route_id))) 1108 StartListeningForRouteMessages(Eq(expected_route_id)))
1111 .WillOnce(InvokeWithoutArgs([&run_loop]() { run_loop.Quit(); })); 1109 .WillOnce(InvokeWithoutArgs([&run_loop]() { run_loop.Quit(); }));
1112 1110
1113 // The ExpectedMessagesObservers will register themselves with the 1111 // The ExpectedMessagesObservers will register themselves with the
1114 // MediaRouter, which in turn will start listening for route messages. 1112 // MediaRouter, which in turn will start listening for route messages.
1115 ExpectedMessagesObserver observer1(router(), expected_route_id); 1113 ExpectedMessagesObserver observer1(router(), expected_route_id, all_messages);
1116 ExpectedMessagesObserver observer2(router(), expected_route_id); 1114 ExpectedMessagesObserver observer2(router(), expected_route_id, all_messages);
1117 run_loop.Run(); // Will quit when StartListeningForRouteMessages() is called. 1115 run_loop.Run(); // Will quit when StartListeningForRouteMessages() is called.
1118 router()->OnRouteMessagesReceived(expected_route_id, incoming_batch1); 1116 router()->OnRouteMessagesReceived(expected_route_id, incoming_batch1);
1119 router()->OnRouteMessagesReceived(expected_route_id, incoming_batch2); 1117 router()->OnRouteMessagesReceived(expected_route_id, incoming_batch2);
1120 router()->OnRouteMessagesReceived(expected_route_id, incoming_batch3); 1118 router()->OnRouteMessagesReceived(expected_route_id, incoming_batch3);
1121 // As each |observer| goes out-of-scope, its destructor will ensure all 1119 // As each |observer| goes out-of-scope, its destructor will ensure all
1122 // expected messages have been received. 1120 // expected messages have been received.
1123 } 1121 }
1124 1122
1125 TEST_F(MediaRouterMojoImplTest, PresentationConnectionStateChangedCallback) { 1123 TEST_F(MediaRouterMojoImplTest, PresentationConnectionStateChangedCallback) {
1126 MediaRoute::Id route_id("route-id"); 1124 MediaRoute::Id route_id("route-id");
(...skipping 698 matching lines...) Expand 10 before | Expand all | Expand 10 after
1825 EXPECT_CALL(mock_media_route_provider_, 1823 EXPECT_CALL(mock_media_route_provider_,
1826 StartListeningForRouteMessages(media_source.id())); 1824 StartListeningForRouteMessages(media_source.id()));
1827 media_router_->OnConnectionError(); 1825 media_router_->OnConnectionError();
1828 BindMediaRouteProvider(); 1826 BindMediaRouteProvider();
1829 RegisterMediaRouteProvider(); 1827 RegisterMediaRouteProvider();
1830 base::RunLoop().RunUntilIdle(); 1828 base::RunLoop().RunUntilIdle();
1831 } 1829 }
1832 } 1830 }
1833 1831
1834 } // namespace media_router 1832 } // namespace media_router
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698