| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "chrome/browser/media/router/mock_media_router.h" | 6 #include "chrome/browser/media/router/mock_media_router.h" |
| 7 #include "chrome/browser/media/router/test_helper.h" | 7 #include "chrome/browser/media/router/test_helper.h" |
| 8 #include "content/public/browser/presentation_session.h" | 8 #include "content/public/browser/presentation_session.h" |
| 9 #include "content/public/test/test_browser_thread_bundle.h" | 9 #include "content/public/test/test_browser_thread_bundle.h" |
| 10 #include "testing/gmock/include/gmock/gmock.h" | 10 #include "testing/gmock/include/gmock/gmock.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 12 |
| 13 using testing::_; | 13 using testing::_; |
| 14 using testing::SaveArg; |
| 14 | 15 |
| 15 namespace media_router { | 16 namespace media_router { |
| 16 | 17 |
| 17 // MockMediaRouter inherits from MediaRouterBase but overrides some of its | 18 // MockMediaRouter inherits from MediaRouterBase but overrides some of its |
| 18 // methods with mock methods, so we must override them again. | 19 // methods with mock methods, so we must override them again. |
| 19 class MockMediaRouterBase : public MockMediaRouter { | 20 class MockMediaRouterBase : public MockMediaRouter { |
| 20 public: | 21 public: |
| 21 MockMediaRouterBase() {} | 22 MockMediaRouterBase() {} |
| 22 ~MockMediaRouterBase() override {} | 23 ~MockMediaRouterBase() override {} |
| 23 | 24 |
| 24 std::unique_ptr<PresentationConnectionStateSubscription> | 25 std::unique_ptr<PresentationConnectionStateSubscription> |
| 25 AddPresentationConnectionStateChangedCallback( | 26 AddPresentationConnectionStateChangedCallback( |
| 26 const MediaRoute::Id& route_id, | 27 const MediaRoute::Id& route_id, |
| 27 const content::PresentationConnectionStateChangedCallback& callback) | 28 const content::PresentationConnectionStateChangedCallback& callback) |
| 28 override { | 29 override { |
| 29 return MediaRouterBase::AddPresentationConnectionStateChangedCallback( | 30 return MediaRouterBase::AddPresentationConnectionStateChangedCallback( |
| 30 route_id, callback); | 31 route_id, callback); |
| 31 } | 32 } |
| 32 | 33 |
| 33 void OnIncognitoProfileShutdown() override { | 34 void OnIncognitoProfileShutdown() override { |
| 34 MediaRouterBase::OnIncognitoProfileShutdown(); | 35 MediaRouterBase::OnIncognitoProfileShutdown(); |
| 35 } | 36 } |
| 37 |
| 38 std::vector<MediaRoute> GetCurrentRoutes() const override { |
| 39 return MediaRouterBase::GetCurrentRoutes(); |
| 40 } |
| 36 }; | 41 }; |
| 37 | 42 |
| 38 TEST(MediaRouterBaseTest, CreatePresentationIds) { | 43 class MediaRouterBaseTest : public testing::Test { |
| 44 public: |
| 45 void SetUp() override { |
| 46 EXPECT_CALL(router_, RegisterMediaRoutesObserver(_)) |
| 47 .WillOnce(SaveArg<0>(&routes_observer_)); |
| 48 router_.Initialize(); |
| 49 } |
| 50 |
| 51 void TearDown() override { router_.Shutdown(); } |
| 52 |
| 53 protected: |
| 54 content::TestBrowserThreadBundle thread_bundle_; |
| 55 MockMediaRouterBase router_; |
| 56 MediaRoutesObserver* routes_observer_; |
| 57 }; |
| 58 |
| 59 TEST_F(MediaRouterBaseTest, CreatePresentationIds) { |
| 39 std::string id1 = MediaRouterBase::CreatePresentationId(); | 60 std::string id1 = MediaRouterBase::CreatePresentationId(); |
| 40 std::string id2 = MediaRouterBase::CreatePresentationId(); | 61 std::string id2 = MediaRouterBase::CreatePresentationId(); |
| 41 EXPECT_NE(id1, ""); | 62 EXPECT_NE(id1, ""); |
| 42 EXPECT_NE(id2, ""); | 63 EXPECT_NE(id2, ""); |
| 43 EXPECT_NE(id1, id2); | 64 EXPECT_NE(id1, id2); |
| 44 } | 65 } |
| 45 | 66 |
| 46 TEST(MediaRouterBaseTest, NotifyCallbacks) { | 67 TEST_F(MediaRouterBaseTest, NotifyCallbacks) { |
| 47 content::TestBrowserThreadBundle thread_bundle_; | |
| 48 MockMediaRouterBase router; | |
| 49 router.Initialize(); | |
| 50 | |
| 51 MediaRoute::Id route_id1("id1"); | 68 MediaRoute::Id route_id1("id1"); |
| 52 MediaRoute::Id route_id2("id2"); | 69 MediaRoute::Id route_id2("id2"); |
| 53 MockPresentationConnectionStateChangedCallback callback1; | 70 MockPresentationConnectionStateChangedCallback callback1; |
| 54 MockPresentationConnectionStateChangedCallback callback2; | 71 MockPresentationConnectionStateChangedCallback callback2; |
| 55 std::unique_ptr<PresentationConnectionStateSubscription> subscription1 = | 72 std::unique_ptr<PresentationConnectionStateSubscription> subscription1 = |
| 56 router.AddPresentationConnectionStateChangedCallback( | 73 router_.AddPresentationConnectionStateChangedCallback( |
| 57 route_id1, | 74 route_id1, |
| 58 base::Bind(&MockPresentationConnectionStateChangedCallback::Run, | 75 base::Bind(&MockPresentationConnectionStateChangedCallback::Run, |
| 59 base::Unretained(&callback1))); | 76 base::Unretained(&callback1))); |
| 60 std::unique_ptr<PresentationConnectionStateSubscription> subscription2 = | 77 std::unique_ptr<PresentationConnectionStateSubscription> subscription2 = |
| 61 router.AddPresentationConnectionStateChangedCallback( | 78 router_.AddPresentationConnectionStateChangedCallback( |
| 62 route_id2, | 79 route_id2, |
| 63 base::Bind(&MockPresentationConnectionStateChangedCallback::Run, | 80 base::Bind(&MockPresentationConnectionStateChangedCallback::Run, |
| 64 base::Unretained(&callback2))); | 81 base::Unretained(&callback2))); |
| 65 | 82 |
| 66 content::PresentationConnectionStateChangeInfo change_info_connected( | 83 content::PresentationConnectionStateChangeInfo change_info_connected( |
| 67 content::PRESENTATION_CONNECTION_STATE_CONNECTED); | 84 content::PRESENTATION_CONNECTION_STATE_CONNECTED); |
| 68 content::PresentationConnectionStateChangeInfo change_info_terminated( | 85 content::PresentationConnectionStateChangeInfo change_info_terminated( |
| 69 content::PRESENTATION_CONNECTION_STATE_TERMINATED); | 86 content::PRESENTATION_CONNECTION_STATE_TERMINATED); |
| 70 content::PresentationConnectionStateChangeInfo change_info_closed( | 87 content::PresentationConnectionStateChangeInfo change_info_closed( |
| 71 content::PRESENTATION_CONNECTION_STATE_CLOSED); | 88 content::PRESENTATION_CONNECTION_STATE_CLOSED); |
| 72 change_info_closed.close_reason = | 89 change_info_closed.close_reason = |
| 73 content::PRESENTATION_CONNECTION_CLOSE_REASON_WENT_AWAY; | 90 content::PRESENTATION_CONNECTION_CLOSE_REASON_WENT_AWAY; |
| 74 change_info_closed.message = "Test message"; | 91 change_info_closed.message = "Test message"; |
| 75 | 92 |
| 76 EXPECT_CALL(callback1, Run(StateChangeInfoEquals(change_info_connected))); | 93 EXPECT_CALL(callback1, Run(StateChangeInfoEquals(change_info_connected))); |
| 77 router.NotifyPresentationConnectionStateChange( | 94 router_.NotifyPresentationConnectionStateChange( |
| 78 route_id1, content::PRESENTATION_CONNECTION_STATE_CONNECTED); | 95 route_id1, content::PRESENTATION_CONNECTION_STATE_CONNECTED); |
| 79 | 96 |
| 80 EXPECT_CALL(callback2, Run(StateChangeInfoEquals(change_info_connected))); | 97 EXPECT_CALL(callback2, Run(StateChangeInfoEquals(change_info_connected))); |
| 81 router.NotifyPresentationConnectionStateChange( | 98 router_.NotifyPresentationConnectionStateChange( |
| 82 route_id2, content::PRESENTATION_CONNECTION_STATE_CONNECTED); | 99 route_id2, content::PRESENTATION_CONNECTION_STATE_CONNECTED); |
| 83 | 100 |
| 84 EXPECT_CALL(callback1, Run(StateChangeInfoEquals(change_info_closed))); | 101 EXPECT_CALL(callback1, Run(StateChangeInfoEquals(change_info_closed))); |
| 85 router.NotifyPresentationConnectionClose( | 102 router_.NotifyPresentationConnectionClose( |
| 86 route_id1, change_info_closed.close_reason, change_info_closed.message); | 103 route_id1, change_info_closed.close_reason, change_info_closed.message); |
| 87 | 104 |
| 88 // After removing a subscription, the corresponding callback should no longer | 105 // After removing a subscription, the corresponding callback should no longer |
| 89 // be called. | 106 // be called. |
| 90 subscription1.reset(); | 107 subscription1.reset(); |
| 91 router.NotifyPresentationConnectionStateChange( | 108 router_.NotifyPresentationConnectionStateChange( |
| 92 route_id1, content::PRESENTATION_CONNECTION_STATE_TERMINATED); | 109 route_id1, content::PRESENTATION_CONNECTION_STATE_TERMINATED); |
| 93 | 110 |
| 94 EXPECT_CALL(callback2, Run(StateChangeInfoEquals(change_info_terminated))); | 111 EXPECT_CALL(callback2, Run(StateChangeInfoEquals(change_info_terminated))); |
| 95 router.NotifyPresentationConnectionStateChange( | 112 router_.NotifyPresentationConnectionStateChange( |
| 96 route_id2, content::PRESENTATION_CONNECTION_STATE_TERMINATED); | 113 route_id2, content::PRESENTATION_CONNECTION_STATE_TERMINATED); |
| 97 | 114 |
| 98 subscription2.reset(); | 115 subscription2.reset(); |
| 99 router.NotifyPresentationConnectionStateChange( | 116 router_.NotifyPresentationConnectionStateChange( |
| 100 route_id2, content::PRESENTATION_CONNECTION_STATE_TERMINATED); | 117 route_id2, content::PRESENTATION_CONNECTION_STATE_TERMINATED); |
| 118 } |
| 101 | 119 |
| 102 router.Shutdown(); | 120 TEST_F(MediaRouterBaseTest, GetCurrentRoutes) { |
| 121 MediaSource source1("source_1"); |
| 122 MediaSource source2("source_1"); |
| 123 MediaRoute route1("route_1", source1, "sink_1", "", false, "", false); |
| 124 MediaRoute route2("route_2", source2, "sink_2", "", true, "", false); |
| 125 std::vector<MediaRoute> routes = {route1, route2}; |
| 126 std::vector<MediaRoute::Id> joinable_route_ids = {"route_1"}; |
| 127 |
| 128 EXPECT_TRUE(router_.GetCurrentRoutes().empty()); |
| 129 routes_observer_->OnRoutesUpdated(routes, joinable_route_ids); |
| 130 std::vector<MediaRoute> current_routes = router_.GetCurrentRoutes(); |
| 131 ASSERT_EQ(current_routes.size(), 2u); |
| 132 EXPECT_TRUE(current_routes[0].Equals(route1)); |
| 133 EXPECT_TRUE(current_routes[1].Equals(route2)); |
| 134 |
| 135 routes_observer_->OnRoutesUpdated(std::vector<MediaRoute>(), |
| 136 std::vector<MediaRoute::Id>()); |
| 137 EXPECT_TRUE(router_.GetCurrentRoutes().empty()); |
| 103 } | 138 } |
| 104 | 139 |
| 105 } // namespace media_router | 140 } // namespace media_router |
| OLD | NEW |