| OLD | NEW |
| 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/containers/hash_tables.h" | 6 #include "base/containers/hash_tables.h" |
| 7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "chrome/browser/media/router/media_sinks_observer.h" | 10 #include "chrome/browser/media/router/media_sinks_observer.h" |
| 11 #include "chrome/browser/media/router/media_source_helper.h" | 11 #include "chrome/browser/media/router/media_source_helper.h" |
| 12 #include "chrome/browser/media/router/mock_media_router.h" | 12 #include "chrome/browser/media/router/mock_media_router.h" |
| 13 #include "chrome/browser/ui/webui/media_router/query_result_manager.h" | 13 #include "chrome/browser/ui/webui/media_router/query_result_manager.h" |
| 14 #include "content/public/test/test_browser_thread_bundle.h" | 14 #include "content/public/test/test_browser_thread_bundle.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "url/gurl.h" |
| 17 | 18 |
| 18 using testing::Eq; | 19 using testing::Eq; |
| 19 using testing::IsEmpty; | 20 using testing::IsEmpty; |
| 20 using testing::Eq; | 21 using testing::Eq; |
| 21 using testing::Mock; | 22 using testing::Mock; |
| 22 using testing::Return; | 23 using testing::Return; |
| 23 using testing::_; | 24 using testing::_; |
| 24 | 25 |
| 25 namespace media_router { | 26 namespace media_router { |
| 26 | 27 |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 } | 109 } |
| 109 | 110 |
| 110 TEST_F(QueryResultManagerTest, StartStopSinksQuery) { | 111 TEST_F(QueryResultManagerTest, StartStopSinksQuery) { |
| 111 GURL origin(kOrigin); | 112 GURL origin(kOrigin); |
| 112 CastModeSet cast_modes = query_result_manager_.GetSupportedCastModes(); | 113 CastModeSet cast_modes = query_result_manager_.GetSupportedCastModes(); |
| 113 EXPECT_TRUE(cast_modes.empty()); | 114 EXPECT_TRUE(cast_modes.empty()); |
| 114 std::vector<MediaSource> actual_sources = | 115 std::vector<MediaSource> actual_sources = |
| 115 query_result_manager_.GetSourcesForCastMode(MediaCastMode::DEFAULT); | 116 query_result_manager_.GetSourcesForCastMode(MediaCastMode::DEFAULT); |
| 116 EXPECT_EQ(0u, actual_sources.size()); | 117 EXPECT_EQ(0u, actual_sources.size()); |
| 117 | 118 |
| 118 MediaSource source(MediaSourceForPresentationUrl("http://foo.com")); | 119 MediaSource source(MediaSourceForPresentationUrl(GURL("http://foo.com"))); |
| 119 EXPECT_CALL(mock_router_, RegisterMediaSinksObserver(_)) | 120 EXPECT_CALL(mock_router_, RegisterMediaSinksObserver(_)) |
| 120 .WillOnce(Return(true)); | 121 .WillOnce(Return(true)); |
| 121 query_result_manager_.SetSourcesForCastMode(MediaCastMode::DEFAULT, {source}, | 122 query_result_manager_.SetSourcesForCastMode(MediaCastMode::DEFAULT, {source}, |
| 122 origin); | 123 origin); |
| 123 | 124 |
| 124 cast_modes = query_result_manager_.GetSupportedCastModes(); | 125 cast_modes = query_result_manager_.GetSupportedCastModes(); |
| 125 EXPECT_EQ(1u, cast_modes.size()); | 126 EXPECT_EQ(1u, cast_modes.size()); |
| 126 EXPECT_TRUE(base::ContainsKey(cast_modes, MediaCastMode::DEFAULT)); | 127 EXPECT_TRUE(base::ContainsKey(cast_modes, MediaCastMode::DEFAULT)); |
| 127 actual_sources = | 128 actual_sources = |
| 128 query_result_manager_.GetSourcesForCastMode(MediaCastMode::DEFAULT); | 129 query_result_manager_.GetSourcesForCastMode(MediaCastMode::DEFAULT); |
| 129 EXPECT_EQ(1u, actual_sources.size()); | 130 EXPECT_EQ(1u, actual_sources.size()); |
| 130 EXPECT_EQ(source, actual_sources[0]); | 131 EXPECT_EQ(source, actual_sources[0]); |
| 131 | 132 |
| 132 // Register a different set of sources for the same cast mode. | 133 // Register a different set of sources for the same cast mode. |
| 133 MediaSource another_source(MediaSourceForPresentationUrl("http://bar.com")); | 134 MediaSource another_source( |
| 135 MediaSourceForPresentationUrl(GURL("http://bar.com"))); |
| 134 EXPECT_CALL(mock_router_, UnregisterMediaSinksObserver(_)).Times(1); | 136 EXPECT_CALL(mock_router_, UnregisterMediaSinksObserver(_)).Times(1); |
| 135 EXPECT_CALL(mock_router_, RegisterMediaSinksObserver(_)) | 137 EXPECT_CALL(mock_router_, RegisterMediaSinksObserver(_)) |
| 136 .WillOnce(Return(true)); | 138 .WillOnce(Return(true)); |
| 137 query_result_manager_.SetSourcesForCastMode(MediaCastMode::DEFAULT, | 139 query_result_manager_.SetSourcesForCastMode(MediaCastMode::DEFAULT, |
| 138 {another_source}, origin); | 140 {another_source}, origin); |
| 139 | 141 |
| 140 cast_modes = query_result_manager_.GetSupportedCastModes(); | 142 cast_modes = query_result_manager_.GetSupportedCastModes(); |
| 141 EXPECT_EQ(1u, cast_modes.size()); | 143 EXPECT_EQ(1u, cast_modes.size()); |
| 142 EXPECT_TRUE(base::ContainsKey(cast_modes, MediaCastMode::DEFAULT)); | 144 EXPECT_TRUE(base::ContainsKey(cast_modes, MediaCastMode::DEFAULT)); |
| 143 actual_sources = | 145 actual_sources = |
| (...skipping 10 matching lines...) Expand all Loading... |
| 154 query_result_manager_.GetSourcesForCastMode(MediaCastMode::DEFAULT); | 156 query_result_manager_.GetSourcesForCastMode(MediaCastMode::DEFAULT); |
| 155 EXPECT_EQ(0u, actual_sources.size()); | 157 EXPECT_EQ(0u, actual_sources.size()); |
| 156 } | 158 } |
| 157 | 159 |
| 158 TEST_F(QueryResultManagerTest, MultipleQueries) { | 160 TEST_F(QueryResultManagerTest, MultipleQueries) { |
| 159 MediaSink sink1("sinkId1", "Sink 1", MediaSink::IconType::CAST); | 161 MediaSink sink1("sinkId1", "Sink 1", MediaSink::IconType::CAST); |
| 160 MediaSink sink2("sinkId2", "Sink 2", MediaSink::IconType::CAST); | 162 MediaSink sink2("sinkId2", "Sink 2", MediaSink::IconType::CAST); |
| 161 MediaSink sink3("sinkId3", "Sink 3", MediaSink::IconType::CAST); | 163 MediaSink sink3("sinkId3", "Sink 3", MediaSink::IconType::CAST); |
| 162 MediaSink sink4("sinkId4", "Sink 4", MediaSink::IconType::CAST); | 164 MediaSink sink4("sinkId4", "Sink 4", MediaSink::IconType::CAST); |
| 163 MediaSink sink5("sinkId5", "Sink 5", MediaSink::IconType::CAST); | 165 MediaSink sink5("sinkId5", "Sink 5", MediaSink::IconType::CAST); |
| 164 MediaSource default_source1 = MediaSourceForPresentationUrl("http://bar.com"); | 166 MediaSource default_source1 = |
| 165 MediaSource default_source2 = MediaSourceForPresentationUrl("http://baz.com"); | 167 MediaSourceForPresentationUrl(GURL("http://bar.com")); |
| 168 MediaSource default_source2 = |
| 169 MediaSourceForPresentationUrl(GURL("http://baz.com")); |
| 166 MediaSource tab_source = MediaSourceForTab(123); | 170 MediaSource tab_source = MediaSourceForTab(123); |
| 167 GURL origin(kOrigin); | 171 GURL origin(kOrigin); |
| 168 | 172 |
| 169 query_result_manager_.AddObserver(&mock_observer_); | 173 query_result_manager_.AddObserver(&mock_observer_); |
| 170 DiscoverSinks(MediaCastMode::DEFAULT, default_source1); | 174 DiscoverSinks(MediaCastMode::DEFAULT, default_source1); |
| 171 DiscoverSinks(MediaCastMode::TAB_MIRROR, tab_source); | 175 DiscoverSinks(MediaCastMode::TAB_MIRROR, tab_source); |
| 172 | 176 |
| 173 // Scenario (results in this order): | 177 // Scenario (results in this order): |
| 174 // Action: DEFAULT -> [1, 2, 3] | 178 // Action: DEFAULT -> [1, 2, 3] |
| 175 // Expected result: | 179 // Expected result: |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 | 274 |
| 271 // Remaining observers: DEFAULT observer, which will be removed on destruction | 275 // Remaining observers: DEFAULT observer, which will be removed on destruction |
| 272 EXPECT_CALL(mock_router_, UnregisterMediaSinksObserver(_)).Times(1); | 276 EXPECT_CALL(mock_router_, UnregisterMediaSinksObserver(_)).Times(1); |
| 273 } | 277 } |
| 274 | 278 |
| 275 TEST_F(QueryResultManagerTest, MultipleUrls) { | 279 TEST_F(QueryResultManagerTest, MultipleUrls) { |
| 276 const MediaSink sink1("sinkId1", "Sink 1", MediaSink::IconType::CAST); | 280 const MediaSink sink1("sinkId1", "Sink 1", MediaSink::IconType::CAST); |
| 277 const MediaSink sink2("sinkId2", "Sink 2", MediaSink::IconType::CAST); | 281 const MediaSink sink2("sinkId2", "Sink 2", MediaSink::IconType::CAST); |
| 278 const MediaSink sink3("sinkId3", "Sink 3", MediaSink::IconType::CAST); | 282 const MediaSink sink3("sinkId3", "Sink 3", MediaSink::IconType::CAST); |
| 279 const MediaSink sink4("sinkId4", "Sink 4", MediaSink::IconType::CAST); | 283 const MediaSink sink4("sinkId4", "Sink 4", MediaSink::IconType::CAST); |
| 280 const MediaSource source_a(MediaSourceForPresentationUrl("http://urlA.com")); | 284 const MediaSource source_a( |
| 281 const MediaSource source_b(MediaSourceForPresentationUrl("http://urlB.com")); | 285 MediaSourceForPresentationUrl(GURL("http://urlA.com"))); |
| 282 const MediaSource source_c(MediaSourceForPresentationUrl("http://urlC.com")); | 286 const MediaSource source_b( |
| 287 MediaSourceForPresentationUrl(GURL("http://urlB.com"))); |
| 288 const MediaSource source_c( |
| 289 MediaSourceForPresentationUrl(GURL("http://urlC.com"))); |
| 283 const MediaSource source_tab(MediaSourceForTab(1)); | 290 const MediaSource source_tab(MediaSourceForTab(1)); |
| 284 // The sources are in decreasing order of priority. | 291 // The sources are in decreasing order of priority. |
| 285 const std::vector<MediaSource> default_sources = {source_a, source_b, | 292 const std::vector<MediaSource> default_sources = {source_a, source_b, |
| 286 source_c}; | 293 source_c}; |
| 287 const std::vector<MediaSource> tab_sources = {source_tab}; | 294 const std::vector<MediaSource> tab_sources = {source_tab}; |
| 288 const GURL origin(kOrigin); | 295 const GURL origin(kOrigin); |
| 289 const auto& sinks_observers = query_result_manager_.sinks_observers_; | 296 const auto& sinks_observers = query_result_manager_.sinks_observers_; |
| 290 | 297 |
| 291 // There should be one MediaSinksObserver per source. | 298 // There should be one MediaSinksObserver per source. |
| 292 EXPECT_CALL(mock_router_, RegisterMediaSinksObserver(_)) | 299 EXPECT_CALL(mock_router_, RegisterMediaSinksObserver(_)) |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 EXPECT_TRUE(IsTabSourceForSink(nullptr, sink3)); | 377 EXPECT_TRUE(IsTabSourceForSink(nullptr, sink3)); |
| 371 EXPECT_TRUE(IsTabSourceForSink(nullptr, sink4)); | 378 EXPECT_TRUE(IsTabSourceForSink(nullptr, sink4)); |
| 372 | 379 |
| 373 // The observers for the four sources should get unregistered. | 380 // The observers for the four sources should get unregistered. |
| 374 EXPECT_CALL(mock_router_, UnregisterMediaSinksObserver(_)).Times(4); | 381 EXPECT_CALL(mock_router_, UnregisterMediaSinksObserver(_)).Times(4); |
| 375 query_result_manager_.RemoveSourcesForCastMode(MediaCastMode::DEFAULT); | 382 query_result_manager_.RemoveSourcesForCastMode(MediaCastMode::DEFAULT); |
| 376 query_result_manager_.RemoveSourcesForCastMode(MediaCastMode::TAB_MIRROR); | 383 query_result_manager_.RemoveSourcesForCastMode(MediaCastMode::TAB_MIRROR); |
| 377 } | 384 } |
| 378 | 385 |
| 379 TEST_F(QueryResultManagerTest, AddInvalidSource) { | 386 TEST_F(QueryResultManagerTest, AddInvalidSource) { |
| 380 const MediaSource source(MediaSourceForPresentationUrl("http://url.com")); | 387 const MediaSource source( |
| 388 MediaSourceForPresentationUrl(GURL("http://url.com"))); |
| 381 const GURL origin(kOrigin); | 389 const GURL origin(kOrigin); |
| 382 | 390 |
| 383 EXPECT_CALL(mock_router_, RegisterMediaSinksObserver(_)) | 391 EXPECT_CALL(mock_router_, RegisterMediaSinksObserver(_)) |
| 384 .Times(1) | 392 .Times(1) |
| 385 .WillRepeatedly(Return(true)); | 393 .WillRepeatedly(Return(true)); |
| 386 query_result_manager_.SetSourcesForCastMode(MediaCastMode::DEFAULT, {source}, | 394 query_result_manager_.SetSourcesForCastMode(MediaCastMode::DEFAULT, {source}, |
| 387 origin); | 395 origin); |
| 388 // |source| has already been registered with the default cast mode, so it | 396 // |source| has already been registered with the default cast mode, so it |
| 389 // shouldn't get registered with tab mirroring. | 397 // shouldn't get registered with tab mirroring. |
| 390 query_result_manager_.SetSourcesForCastMode(MediaCastMode::TAB_MIRROR, | 398 query_result_manager_.SetSourcesForCastMode(MediaCastMode::TAB_MIRROR, |
| 391 {source}, origin); | 399 {source}, origin); |
| 392 | 400 |
| 393 const auto& cast_mode_sources = query_result_manager_.cast_mode_sources_; | 401 const auto& cast_mode_sources = query_result_manager_.cast_mode_sources_; |
| 394 const auto& default_sources = cast_mode_sources.at(MediaCastMode::DEFAULT); | 402 const auto& default_sources = cast_mode_sources.at(MediaCastMode::DEFAULT); |
| 395 EXPECT_TRUE(base::ContainsKey(cast_mode_sources, MediaCastMode::DEFAULT)); | 403 EXPECT_TRUE(base::ContainsKey(cast_mode_sources, MediaCastMode::DEFAULT)); |
| 396 EXPECT_EQ(default_sources.size(), 1u); | 404 EXPECT_EQ(default_sources.size(), 1u); |
| 397 EXPECT_EQ(default_sources.at(0), source); | 405 EXPECT_EQ(default_sources.at(0), source); |
| 398 EXPECT_FALSE(base::ContainsKey(cast_mode_sources, MediaCastMode::TAB_MIRROR)); | 406 EXPECT_FALSE(base::ContainsKey(cast_mode_sources, MediaCastMode::TAB_MIRROR)); |
| 399 } | 407 } |
| 400 | 408 |
| 401 } // namespace media_router | 409 } // namespace media_router |
| OLD | NEW |