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

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: Addressed Takumi's comments 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,
1006 std::vector<content::PresentationConnectionMessage>* batch3) {
1007 batch1->resize(1); 1007 batch1->resize(1);
mark a. foltz 2017/06/28 07:29:55 I would slightly prefer to use the PCM constructor
imcheng 2017/06/29 08:21:09 Done.
1008 batch1->at(0).type = RouteMessage::TEXT; 1008 batch1->at(0).message = std::string("text1");
1009 batch1->at(0).text = std::string("text1");
1010 batch2->resize(2); 1009 batch2->resize(2);
1011 batch2->at(0).type = RouteMessage::BINARY; 1010 batch2->at(0).data = std::vector<uint8_t>(1, UINT8_C(1));
1012 batch2->at(0).binary = std::vector<uint8_t>(1, UINT8_C(1)); 1011 batch2->at(1).message = std::string("text2");
1013 batch2->at(1).type = RouteMessage::TEXT;
1014 batch2->at(1).text = std::string("text2");
1015 batch3->resize(3); 1012 batch3->resize(3);
1016 batch3->at(0).type = RouteMessage::TEXT; 1013 batch3->at(0).message = std::string("text3");
1017 batch3->at(0).text = std::string("text3"); 1014 batch3->at(1).data = std::vector<uint8_t>(1, UINT8_C(2));
1018 batch3->at(1).type = RouteMessage::BINARY; 1015 batch3->at(2).data = std::vector<uint8_t>(1, UINT8_C(3));
1019 batch3->at(1).binary = std::vector<uint8_t>(1, UINT8_C(2));
1020 batch3->at(2).type = RouteMessage::BINARY;
1021 batch3->at(2).binary = std::vector<uint8_t>(1, UINT8_C(3));
1022 } 1016 }
1023 1017
1024 // Used in the RouteMessages* tests to observe and sanity-check that the 1018 // 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 1019 // messages being received from the router are correct and in-sequence. The
1026 // checks here correspond to the expected messages in PopulateRouteMessages() 1020 // checks here correspond to the expected messages in PopulateRouteMessages()
1027 // above. 1021 // above.
1028 class ExpectedMessagesObserver : public RouteMessageObserver { 1022 class ExpectedMessagesObserver : public RouteMessageObserver {
1029 public: 1023 public:
1030 ExpectedMessagesObserver(MediaRouter* router, const MediaRoute::Id& route_id) 1024 ExpectedMessagesObserver(MediaRouter* router, const MediaRoute::Id& route_id)
1031 : RouteMessageObserver(router, route_id) {} 1025 : RouteMessageObserver(router, route_id) {}
1032 1026
1033 ~ExpectedMessagesObserver() final { 1027 ~ExpectedMessagesObserver() final {
1034 CheckReceivedMessages(); 1028 CheckReceivedMessages();
1035 } 1029 }
1036 1030
1037 private: 1031 private:
1038 void OnMessagesReceived( 1032 void OnMessagesReceived(
1039 const std::vector<RouteMessage>& messages) final { 1033 const std::vector<content::PresentationConnectionMessage>& messages)
1034 final {
1040 messages_.insert(messages_.end(), messages.begin(), messages.end()); 1035 messages_.insert(messages_.end(), messages.begin(), messages.end());
mark a. foltz 2017/06/28 07:29:55 messages_ = messages?
imcheng 2017/06/29 08:21:09 This appends the contents of |messages| to |messag
1041 } 1036 }
1042 1037
1043 void CheckReceivedMessages() { 1038 void CheckReceivedMessages() {
1044 ASSERT_EQ(6u, messages_.size()); 1039 ASSERT_EQ(6u, messages_.size());
1045 EXPECT_EQ(RouteMessage::TEXT, messages_[0].type); 1040 ASSERT_TRUE(messages_[0].message);
mark a. foltz 2017/06/28 07:29:55 PCM implements == so you could store the messages
imcheng 2017/06/29 08:21:09 Done. Though I find that the problem with assertin
mark a. foltz 2017/06/29 17:38:57 Yeah. It would be nice if there were an ASSERT_CO
1046 ASSERT_TRUE(messages_[0].text); 1041 EXPECT_EQ("text1", *messages_[0].message);
1047 EXPECT_EQ("text1", *messages_[0].text); 1042 ASSERT_TRUE(messages_[1].data);
1048 EXPECT_EQ(RouteMessage::BINARY, messages_[1].type); 1043 ASSERT_EQ(1u, messages_[1].data->size());
1049 ASSERT_TRUE(messages_[1].binary); 1044 EXPECT_EQ(UINT8_C(1), messages_[1].data->front());
1050 ASSERT_EQ(1u, messages_[1].binary->size()); 1045 ASSERT_TRUE(messages_[2].message);
1051 EXPECT_EQ(UINT8_C(1), messages_[1].binary->front()); 1046 EXPECT_EQ("text2", *messages_[2].message);
1052 EXPECT_EQ(RouteMessage::TEXT, messages_[2].type); 1047 ASSERT_TRUE(messages_[3].message);
1053 ASSERT_TRUE(messages_[2].text); 1048 EXPECT_EQ("text3", *messages_[3].message);
1054 EXPECT_EQ("text2", *messages_[2].text); 1049 ASSERT_TRUE(messages_[4].data);
1055 EXPECT_EQ(RouteMessage::TEXT, messages_[3].type); 1050 ASSERT_EQ(1u, messages_[4].data->size());
1056 ASSERT_TRUE(messages_[3].text); 1051 EXPECT_EQ(UINT8_C(2), messages_[4].data->front());
1057 EXPECT_EQ("text3", *messages_[3].text); 1052 ASSERT_TRUE(messages_[5].data);
1058 EXPECT_EQ(RouteMessage::BINARY, messages_[4].type); 1053 ASSERT_EQ(1u, messages_[5].data->size());
1059 ASSERT_TRUE(messages_[4].binary); 1054 EXPECT_EQ(UINT8_C(3), messages_[5].data->front());
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 } 1055 }
1067 1056
1068 std::vector<RouteMessage> messages_; 1057 std::vector<content::PresentationConnectionMessage> messages_;
1069 }; 1058 };
1070 1059
1071 class NullMessageObserver : public RouteMessageObserver { 1060 class NullMessageObserver : public RouteMessageObserver {
1072 public: 1061 public:
1073 NullMessageObserver(MediaRouter* router, const MediaRoute::Id& route_id) 1062 NullMessageObserver(MediaRouter* router, const MediaRoute::Id& route_id)
1074 : RouteMessageObserver(router, route_id) {} 1063 : RouteMessageObserver(router, route_id) {}
1075 ~NullMessageObserver() final {} 1064 ~NullMessageObserver() final {}
1076 1065
1077 void OnMessagesReceived(const std::vector<RouteMessage>& messages) final {} 1066 void OnMessagesReceived(
1067 const std::vector<content::PresentationConnectionMessage>& messages)
1068 final {}
1078 }; 1069 };
1079 1070
1080 } // namespace 1071 } // namespace
1081 1072
1082 TEST_F(MediaRouterMojoImplTest, RouteMessagesSingleObserver) { 1073 TEST_F(MediaRouterMojoImplTest, RouteMessagesSingleObserver) {
1083 std::vector<RouteMessage> incoming_batch1, incoming_batch2, incoming_batch3; 1074 std::vector<content::PresentationConnectionMessage> incoming_batch1,
1075 incoming_batch2, incoming_batch3;
1084 PopulateRouteMessages(&incoming_batch1, &incoming_batch2, &incoming_batch3); 1076 PopulateRouteMessages(&incoming_batch1, &incoming_batch2, &incoming_batch3);
1085 1077
1086 base::RunLoop run_loop; 1078 base::RunLoop run_loop;
1087 MediaRoute::Id expected_route_id("foo"); 1079 MediaRoute::Id expected_route_id("foo");
1088 EXPECT_CALL(mock_media_route_provider_, 1080 EXPECT_CALL(mock_media_route_provider_,
1089 StartListeningForRouteMessages(Eq(expected_route_id))) 1081 StartListeningForRouteMessages(Eq(expected_route_id)))
1090 .WillOnce(InvokeWithoutArgs([&run_loop]() { run_loop.Quit(); })); 1082 .WillOnce(InvokeWithoutArgs([&run_loop]() { run_loop.Quit(); }));
1091 1083
1092 // Creating ExpectedMessagesObserver will register itself to the 1084 // Creating ExpectedMessagesObserver will register itself to the
1093 // MediaRouter, which in turn will start listening for route messages. 1085 // MediaRouter, which in turn will start listening for route messages.
1094 ExpectedMessagesObserver observer(router(), expected_route_id); 1086 ExpectedMessagesObserver observer(router(), expected_route_id);
1095 run_loop.Run(); // Will quit when StartListeningForRouteMessages() is called. 1087 run_loop.Run(); // Will quit when StartListeningForRouteMessages() is called.
1096 router()->OnRouteMessagesReceived(expected_route_id, incoming_batch1); 1088 router()->OnRouteMessagesReceived(expected_route_id, incoming_batch1);
1097 router()->OnRouteMessagesReceived(expected_route_id, incoming_batch2); 1089 router()->OnRouteMessagesReceived(expected_route_id, incoming_batch2);
1098 router()->OnRouteMessagesReceived(expected_route_id, incoming_batch3); 1090 router()->OnRouteMessagesReceived(expected_route_id, incoming_batch3);
1099 // When |observer| goes out-of-scope, its destructor will ensure all expected 1091 // When |observer| goes out-of-scope, its destructor will ensure all expected
1100 // messages have been received. 1092 // messages have been received.
1101 } 1093 }
1102 1094
1103 TEST_F(MediaRouterMojoImplTest, RouteMessagesMultipleObservers) { 1095 TEST_F(MediaRouterMojoImplTest, RouteMessagesMultipleObservers) {
1104 std::vector<RouteMessage> incoming_batch1, incoming_batch2, incoming_batch3; 1096 std::vector<content::PresentationConnectionMessage> incoming_batch1,
1097 incoming_batch2, incoming_batch3;
1105 PopulateRouteMessages(&incoming_batch1, &incoming_batch2, &incoming_batch3); 1098 PopulateRouteMessages(&incoming_batch1, &incoming_batch2, &incoming_batch3);
1106 1099
1107 base::RunLoop run_loop; 1100 base::RunLoop run_loop;
1108 MediaRoute::Id expected_route_id("foo"); 1101 MediaRoute::Id expected_route_id("foo");
1109 EXPECT_CALL(mock_media_route_provider_, 1102 EXPECT_CALL(mock_media_route_provider_,
1110 StartListeningForRouteMessages(Eq(expected_route_id))) 1103 StartListeningForRouteMessages(Eq(expected_route_id)))
1111 .WillOnce(InvokeWithoutArgs([&run_loop]() { run_loop.Quit(); })); 1104 .WillOnce(InvokeWithoutArgs([&run_loop]() { run_loop.Quit(); }));
1112 1105
1113 // The ExpectedMessagesObservers will register themselves with the 1106 // The ExpectedMessagesObservers will register themselves with the
1114 // MediaRouter, which in turn will start listening for route messages. 1107 // MediaRouter, which in turn will start listening for route messages.
(...skipping 710 matching lines...) Expand 10 before | Expand all | Expand 10 after
1825 EXPECT_CALL(mock_media_route_provider_, 1818 EXPECT_CALL(mock_media_route_provider_,
1826 StartListeningForRouteMessages(media_source.id())); 1819 StartListeningForRouteMessages(media_source.id()));
1827 media_router_->OnConnectionError(); 1820 media_router_->OnConnectionError();
1828 BindMediaRouteProvider(); 1821 BindMediaRouteProvider();
1829 RegisterMediaRouteProvider(); 1822 RegisterMediaRouteProvider();
1830 base::RunLoop().RunUntilIdle(); 1823 base::RunLoop().RunUntilIdle();
1831 } 1824 }
1832 } 1825 }
1833 1826
1834 } // namespace media_router 1827 } // namespace media_router
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698