| OLD | NEW |
| (Empty) |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/media/router/discovery/dial/dial_media_sink_service.h" | |
| 6 #include "base/test/mock_callback.h" | |
| 7 #include "base/timer/mock_timer.h" | |
| 8 #include "chrome/browser/media/router/discovery/dial/dial_device_data.h" | |
| 9 #include "chrome/browser/media/router/discovery/dial/dial_registry.h" | |
| 10 #include "chrome/browser/media/router/test_helper.h" | |
| 11 #include "chrome/test/base/testing_profile.h" | |
| 12 #include "content/public/test/test_browser_thread_bundle.h" | |
| 13 #include "testing/gmock/include/gmock/gmock.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 | |
| 16 using ::testing::_; | |
| 17 using ::testing::Return; | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 media_router::DialSinkExtraData CreateDialSinkExtraData( | |
| 22 const std::string& model_name, | |
| 23 const std::string& ip_address, | |
| 24 const std::string& app_url) { | |
| 25 media_router::DialSinkExtraData dial_extra_data; | |
| 26 EXPECT_TRUE(dial_extra_data.ip_address.AssignFromIPLiteral(ip_address)); | |
| 27 dial_extra_data.model_name = model_name; | |
| 28 dial_extra_data.app_url = GURL(app_url); | |
| 29 return dial_extra_data; | |
| 30 } | |
| 31 | |
| 32 std::vector<media_router::MediaSinkInternal> CreateDialMediaSinks() { | |
| 33 media_router::MediaSink sink1("sink1", "sink_name_1", | |
| 34 media_router::MediaSink::IconType::CAST); | |
| 35 media_router::DialSinkExtraData extra_data1 = CreateDialSinkExtraData( | |
| 36 "model_name1", "192.168.1.1", "https://example1.com"); | |
| 37 | |
| 38 media_router::MediaSink sink2("sink2", "sink_name_2", | |
| 39 media_router::MediaSink::IconType::CAST); | |
| 40 media_router::DialSinkExtraData extra_data2 = CreateDialSinkExtraData( | |
| 41 "model_name2", "192.168.1.2", "https://example2.com"); | |
| 42 | |
| 43 std::vector<media_router::MediaSinkInternal> sinks; | |
| 44 sinks.push_back(media_router::MediaSinkInternal(sink1, extra_data1)); | |
| 45 sinks.push_back(media_router::MediaSinkInternal(sink2, extra_data2)); | |
| 46 return sinks; | |
| 47 } | |
| 48 | |
| 49 } // namespace | |
| 50 | |
| 51 namespace media_router { | |
| 52 | |
| 53 class TestDialRegistry : public DialRegistry { | |
| 54 public: | |
| 55 TestDialRegistry() {} | |
| 56 ~TestDialRegistry() {} | |
| 57 | |
| 58 MOCK_METHOD1(RegisterObserver, void(DialRegistry::Observer* observer)); | |
| 59 MOCK_METHOD1(UnregisterObserver, void(DialRegistry::Observer* observer)); | |
| 60 | |
| 61 MOCK_METHOD0(OnListenerAdded, void()); | |
| 62 MOCK_METHOD0(OnListenerRemoved, void()); | |
| 63 }; | |
| 64 | |
| 65 class MockDeviceDescriptionService : public DeviceDescriptionService { | |
| 66 public: | |
| 67 MockDeviceDescriptionService(DeviceDescriptionParseSuccessCallback success_cb, | |
| 68 DeviceDescriptionParseErrorCallback error_cb) | |
| 69 : DeviceDescriptionService(success_cb, error_cb) {} | |
| 70 ~MockDeviceDescriptionService() override {} | |
| 71 | |
| 72 MOCK_METHOD2(GetDeviceDescriptions, | |
| 73 void(const std::vector<DialDeviceData>& devices, | |
| 74 net::URLRequestContextGetter* request_context)); | |
| 75 }; | |
| 76 | |
| 77 class DialMediaSinkServiceTest : public ::testing::Test { | |
| 78 public: | |
| 79 DialMediaSinkServiceTest() | |
| 80 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | |
| 81 media_sink_service_( | |
| 82 new DialMediaSinkService(mock_sink_discovered_cb_.Get(), | |
| 83 profile_.GetRequestContext())) {} | |
| 84 | |
| 85 void SetUp() override { | |
| 86 EXPECT_CALL(test_dial_registry_, | |
| 87 RegisterObserver(media_sink_service_.get())); | |
| 88 EXPECT_CALL(test_dial_registry_, OnListenerAdded()); | |
| 89 | |
| 90 media_sink_service_->SetDialRegistryForTest(&test_dial_registry_); | |
| 91 | |
| 92 auto mock_description_service = | |
| 93 base::MakeUnique<MockDeviceDescriptionService>(mock_success_cb_.Get(), | |
| 94 mock_error_cb_.Get()); | |
| 95 mock_description_service_ = mock_description_service.get(); | |
| 96 media_sink_service_->SetDescriptionServiceForTest( | |
| 97 std::move(mock_description_service)); | |
| 98 mock_timer_ = | |
| 99 new base::MockTimer(true /*retain_user_task*/, false /*is_repeating*/); | |
| 100 media_sink_service_->SetTimerForTest(base::WrapUnique(mock_timer_)); | |
| 101 } | |
| 102 | |
| 103 void TearDown() override { | |
| 104 EXPECT_CALL(test_dial_registry_, | |
| 105 UnregisterObserver(media_sink_service_.get())); | |
| 106 EXPECT_CALL(test_dial_registry_, OnListenerRemoved()); | |
| 107 } | |
| 108 | |
| 109 void TestFetchCompleted(const std::vector<MediaSinkInternal>& old_sinks, | |
| 110 const std::vector<MediaSinkInternal>& new_sinks) { | |
| 111 media_sink_service_->mrp_sinks_ = | |
| 112 std::set<MediaSinkInternal>(old_sinks.begin(), old_sinks.end()); | |
| 113 media_sink_service_->current_sinks_ = | |
| 114 std::set<MediaSinkInternal>(new_sinks.begin(), new_sinks.end()); | |
| 115 EXPECT_CALL(mock_sink_discovered_cb_, Run(new_sinks)); | |
| 116 media_sink_service_->OnFetchCompleted(); | |
| 117 } | |
| 118 | |
| 119 protected: | |
| 120 const content::TestBrowserThreadBundle thread_bundle_; | |
| 121 TestingProfile profile_; | |
| 122 | |
| 123 base::MockCallback<MediaSinkService::OnSinksDiscoveredCallback> | |
| 124 mock_sink_discovered_cb_; | |
| 125 base::MockCallback< | |
| 126 MockDeviceDescriptionService::DeviceDescriptionParseSuccessCallback> | |
| 127 mock_success_cb_; | |
| 128 base::MockCallback< | |
| 129 MockDeviceDescriptionService::DeviceDescriptionParseErrorCallback> | |
| 130 mock_error_cb_; | |
| 131 | |
| 132 TestDialRegistry test_dial_registry_; | |
| 133 MockDeviceDescriptionService* mock_description_service_; | |
| 134 base::MockTimer* mock_timer_; | |
| 135 | |
| 136 std::unique_ptr<DialMediaSinkService> media_sink_service_; | |
| 137 | |
| 138 DISALLOW_COPY_AND_ASSIGN(DialMediaSinkServiceTest); | |
| 139 }; | |
| 140 | |
| 141 TEST_F(DialMediaSinkServiceTest, TestStart) { | |
| 142 media_sink_service_->Start(); | |
| 143 | |
| 144 DialRegistry::DeviceList deviceList; | |
| 145 DialDeviceData first_device("first", GURL("http://127.0.0.1/dd.xml"), | |
| 146 base::Time::Now()); | |
| 147 DialDeviceData second_device("second", GURL("http://127.0.0.2/dd.xml"), | |
| 148 base::Time::Now()); | |
| 149 DialDeviceData third_device("third", GURL("http://127.0.0.3/dd.xml"), | |
| 150 base::Time::Now()); | |
| 151 deviceList.push_back(first_device); | |
| 152 deviceList.push_back(second_device); | |
| 153 deviceList.push_back(third_device); | |
| 154 | |
| 155 EXPECT_CALL(*mock_description_service_, GetDeviceDescriptions(deviceList, _)); | |
| 156 | |
| 157 media_sink_service_->OnDialDeviceEvent(deviceList); | |
| 158 EXPECT_TRUE(media_sink_service_->finish_timer_->IsRunning()); | |
| 159 } | |
| 160 | |
| 161 TEST_F(DialMediaSinkServiceTest, TestOnDeviceDescriptionAvailable) { | |
| 162 DialDeviceData device_data("first", GURL("http://127.0.0.1/dd.xml"), | |
| 163 base::Time::Now()); | |
| 164 ParsedDialDeviceDescription device_description; | |
| 165 device_description.model_name = "model name"; | |
| 166 device_description.friendly_name = "friendly name"; | |
| 167 device_description.app_url = GURL("http://192.168.1.1/apps"); | |
| 168 device_description.unique_id = "unique id"; | |
| 169 | |
| 170 media_sink_service_->OnDeviceDescriptionAvailable(device_data, | |
| 171 device_description); | |
| 172 EXPECT_TRUE(media_sink_service_->current_sinks_.empty()); | |
| 173 | |
| 174 std::vector<DialDeviceData> deviceList{device_data}; | |
| 175 EXPECT_CALL(*mock_description_service_, GetDeviceDescriptions(deviceList, _)); | |
| 176 | |
| 177 media_sink_service_->OnDialDeviceEvent(deviceList); | |
| 178 media_sink_service_->OnDeviceDescriptionAvailable(device_data, | |
| 179 device_description); | |
| 180 | |
| 181 EXPECT_EQ(size_t(1), media_sink_service_->current_sinks_.size()); | |
| 182 } | |
| 183 | |
| 184 TEST_F(DialMediaSinkServiceTest, TestTimer) { | |
| 185 DialDeviceData device_data("first", GURL("http://127.0.0.1/dd.xml"), | |
| 186 base::Time::Now()); | |
| 187 ParsedDialDeviceDescription device_description; | |
| 188 device_description.model_name = "model name"; | |
| 189 device_description.friendly_name = "friendly name"; | |
| 190 device_description.app_url = GURL("http://192.168.1.1/apps"); | |
| 191 device_description.unique_id = "unique id"; | |
| 192 | |
| 193 std::vector<DialDeviceData> deviceList{device_data}; | |
| 194 EXPECT_CALL(*mock_description_service_, GetDeviceDescriptions(deviceList, _)); | |
| 195 | |
| 196 EXPECT_FALSE(mock_timer_->IsRunning()); | |
| 197 media_sink_service_->OnDialDeviceEvent(deviceList); | |
| 198 media_sink_service_->OnDeviceDescriptionAvailable(device_data, | |
| 199 device_description); | |
| 200 EXPECT_TRUE(mock_timer_->IsRunning()); | |
| 201 | |
| 202 EXPECT_CALL(mock_sink_discovered_cb_, Run(_)); | |
| 203 mock_timer_->Fire(); | |
| 204 | |
| 205 EXPECT_FALSE(mock_timer_->IsRunning()); | |
| 206 device_description.app_url = GURL("http://192.168.1.11/apps"); | |
| 207 media_sink_service_->OnDeviceDescriptionAvailable(device_data, | |
| 208 device_description); | |
| 209 EXPECT_TRUE(mock_timer_->IsRunning()); | |
| 210 } | |
| 211 | |
| 212 TEST_F(DialMediaSinkServiceTest, TestFetchCompleted_SameSink) { | |
| 213 std::vector<MediaSinkInternal> old_sinks; | |
| 214 std::vector<MediaSinkInternal> new_sinks = CreateDialMediaSinks(); | |
| 215 TestFetchCompleted(old_sinks, new_sinks); | |
| 216 | |
| 217 // Same sink | |
| 218 EXPECT_CALL(mock_sink_discovered_cb_, Run(new_sinks)).Times(0); | |
| 219 media_sink_service_->OnFetchCompleted(); | |
| 220 } | |
| 221 | |
| 222 TEST_F(DialMediaSinkServiceTest, TestFetchCompleted_OneNewSink) { | |
| 223 std::vector<MediaSinkInternal> old_sinks = CreateDialMediaSinks(); | |
| 224 std::vector<MediaSinkInternal> new_sinks = CreateDialMediaSinks(); | |
| 225 MediaSink sink3("sink3", "sink_name_3", MediaSink::IconType::CAST); | |
| 226 DialSinkExtraData extra_data3 = CreateDialSinkExtraData( | |
| 227 "model_name3", "192.168.1.3", "https://example3.com"); | |
| 228 new_sinks.push_back(MediaSinkInternal(sink3, extra_data3)); | |
| 229 TestFetchCompleted(old_sinks, new_sinks); | |
| 230 } | |
| 231 | |
| 232 TEST_F(DialMediaSinkServiceTest, TestFetchCompleted_RemovedOneSink) { | |
| 233 std::vector<MediaSinkInternal> old_sinks = CreateDialMediaSinks(); | |
| 234 std::vector<MediaSinkInternal> new_sinks = CreateDialMediaSinks(); | |
| 235 new_sinks.erase(new_sinks.begin()); | |
| 236 TestFetchCompleted(old_sinks, new_sinks); | |
| 237 } | |
| 238 | |
| 239 TEST_F(DialMediaSinkServiceTest, TestFetchCompleted_UpdatedOneSink) { | |
| 240 std::vector<MediaSinkInternal> old_sinks = CreateDialMediaSinks(); | |
| 241 std::vector<MediaSinkInternal> new_sinks = CreateDialMediaSinks(); | |
| 242 new_sinks[0].set_name("sink_name_4"); | |
| 243 TestFetchCompleted(old_sinks, new_sinks); | |
| 244 } | |
| 245 | |
| 246 TEST_F(DialMediaSinkServiceTest, TestFetchCompleted_Mixed) { | |
| 247 std::vector<MediaSinkInternal> old_sinks = CreateDialMediaSinks(); | |
| 248 | |
| 249 MediaSink sink1("sink1", "sink_name_1", MediaSink::IconType::CAST); | |
| 250 DialSinkExtraData extra_data2 = CreateDialSinkExtraData( | |
| 251 "model_name2", "192.168.1.2", "https://example2.com"); | |
| 252 | |
| 253 MediaSink sink3("sink3", "sink_name_3", MediaSink::IconType::CAST); | |
| 254 DialSinkExtraData extra_data3 = CreateDialSinkExtraData( | |
| 255 "model_name3", "192.168.1.3", "https://example3.com"); | |
| 256 | |
| 257 std::vector<MediaSinkInternal> new_sinks; | |
| 258 new_sinks.push_back(MediaSinkInternal(sink1, extra_data2)); | |
| 259 new_sinks.push_back(MediaSinkInternal(sink3, extra_data3)); | |
| 260 | |
| 261 TestFetchCompleted(old_sinks, new_sinks); | |
| 262 } | |
| 263 | |
| 264 } // namespace media_router | |
| OLD | NEW |