Chromium Code Reviews| 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" | |
|
mark a. foltz
2017/04/25 21:06:26
Are these #includes in the right order?
zhaobin
2017/04/26 18:50:05
Done.
| |
| 7 #include "chrome/browser/media/router/discovery/dial/dial_device_data.h" | |
| 8 #include "chrome/browser/media/router/discovery/dial/dial_registry.h" | |
| 9 #include "chrome/browser/media/router/discovery/dial/mock_device_description_ser vice.h" | |
| 10 #include "chrome/browser/media/router/test_helper.h" | |
| 11 #include "chrome/common/media_router/discovery/media_sink_internal_unittest.h" | |
| 12 #include "chrome/test/base/testing_profile.h" | |
| 13 #include "content/public/test/test_browser_thread_bundle.h" | |
| 14 #include "testing/gmock/include/gmock/gmock.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 using ::testing::_; | |
| 18 using ::testing::Return; | |
| 19 | |
| 20 namespace media_router { | |
| 21 | |
| 22 class TestDialRegistry : public DialRegistry { | |
| 23 public: | |
| 24 TestDialRegistry() {} | |
| 25 ~TestDialRegistry() {} | |
| 26 | |
| 27 MOCK_METHOD1(RegisterObserver, void(DialRegistry::Observer* observer)); | |
| 28 MOCK_METHOD1(UnregisterObserver, void(DialRegistry::Observer* observer)); | |
| 29 }; | |
| 30 | |
| 31 class TestDialMediaSinkService : public DialMediaSinkService { | |
| 32 public: | |
| 33 TestDialMediaSinkService( | |
| 34 const MediaSinkService::OnSinksDiscoveredCallback& callback, | |
| 35 content::BrowserContext* browser_context, | |
| 36 TestDialRegistry* test_dial_registry, | |
| 37 MockDeviceDescriptionService* mock_description_service) | |
| 38 : DialMediaSinkService(callback, browser_context), | |
| 39 test_dial_registry_(test_dial_registry), | |
| 40 mock_description_service_(mock_description_service) {} | |
| 41 | |
| 42 DialRegistry* dial_registry() override { return test_dial_registry_; } | |
| 43 | |
| 44 DeviceDescriptionService* GetDescriptionService() override { | |
| 45 return mock_description_service_; | |
| 46 } | |
| 47 | |
| 48 private: | |
| 49 TestDialRegistry* test_dial_registry_; | |
| 50 MockDeviceDescriptionService* mock_description_service_; | |
| 51 }; | |
| 52 | |
| 53 class DialMediaSinkServiceTest : public ::testing::Test { | |
| 54 public: | |
| 55 DialMediaSinkServiceTest() | |
| 56 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | |
| 57 mock_description_service_(mock_success_cb_.Get(), mock_error_cb_.Get()), | |
| 58 media_sink_service_(mock_sink_discovered_cb_.Get(), | |
| 59 &profile_, | |
| 60 &test_dial_registry_, | |
| 61 &mock_description_service_) {} | |
| 62 | |
| 63 DialMediaSinkService* media_sink_service() { return &media_sink_service_; } | |
| 64 | |
| 65 std::vector<MediaSinkInternal> CreateDialMediaSinks() { | |
|
mark a. foltz
2017/04/25 21:06:26
Similar comment about moving test data factory cod
zhaobin
2017/04/26 18:50:05
Done.
| |
| 66 MediaSink sink1("sink1", "sink_name_1", MediaSink::IconType::CAST); | |
| 67 DialSinkExtraData extra_data1 = | |
| 68 MediaSinkInternalTest::CreateDialSinkExtraData( | |
| 69 "model_name1", "192.168.1.1", "https://example1.com"); | |
| 70 | |
| 71 MediaSink sink2("sink2", "sink_name_2", MediaSink::IconType::CAST); | |
| 72 DialSinkExtraData extra_data2 = | |
| 73 MediaSinkInternalTest::CreateDialSinkExtraData( | |
| 74 "model_name2", "192.168.1.2", "https://example2.com"); | |
| 75 | |
| 76 std::vector<MediaSinkInternal> sinks; | |
| 77 sinks.push_back(MediaSinkInternal(sink1, extra_data1)); | |
| 78 sinks.push_back(MediaSinkInternal(sink2, extra_data2)); | |
| 79 return sinks; | |
| 80 } | |
| 81 | |
| 82 void TestFetchCompleted(const std::vector<MediaSinkInternal>& old_sinks, | |
| 83 const std::vector<MediaSinkInternal>& new_sinks) { | |
| 84 media_sink_service()->mrp_sinks_ = | |
| 85 std::set<MediaSinkInternal>(old_sinks.begin(), old_sinks.end()); | |
| 86 media_sink_service()->current_sinks_ = | |
| 87 std::set<MediaSinkInternal>(new_sinks.begin(), new_sinks.end()); | |
| 88 EXPECT_CALL(mock_sink_discovered_cb_, Run(new_sinks)); | |
| 89 media_sink_service()->OnFetchCompleted(); | |
| 90 } | |
| 91 | |
| 92 protected: | |
| 93 const content::TestBrowserThreadBundle thread_bundle_; | |
| 94 TestingProfile profile_; | |
| 95 | |
| 96 base::MockCallback<MediaSinkService::OnSinksDiscoveredCallback> | |
| 97 mock_sink_discovered_cb_; | |
| 98 base::MockCallback< | |
| 99 MockDeviceDescriptionService::DeviceDescriptionParseSuccessCallback> | |
| 100 mock_success_cb_; | |
| 101 base::MockCallback< | |
| 102 MockDeviceDescriptionService::DeviceDescriptionParseErrorCallback> | |
| 103 mock_error_cb_; | |
| 104 | |
| 105 TestDialRegistry test_dial_registry_; | |
| 106 MockDeviceDescriptionService mock_description_service_; | |
| 107 | |
| 108 TestDialMediaSinkService media_sink_service_; | |
| 109 }; | |
| 110 | |
| 111 TEST_F(DialMediaSinkServiceTest, TestStart) { | |
| 112 EXPECT_CALL(test_dial_registry_, RegisterObserver(&media_sink_service_)); | |
| 113 media_sink_service()->Start(); | |
| 114 | |
| 115 DialRegistry::DeviceList deviceList; | |
| 116 DialDeviceData first_device("first", GURL("http://127.0.0.1/dd.xml"), | |
| 117 base::Time::Now()); | |
| 118 DialDeviceData second_device("second", GURL("http://127.0.0.2/dd.xml"), | |
| 119 base::Time::Now()); | |
| 120 DialDeviceData third_device("third", GURL("http://127.0.0.3/dd.xml"), | |
| 121 base::Time::Now()); | |
| 122 deviceList.push_back(first_device); | |
| 123 deviceList.push_back(second_device); | |
| 124 deviceList.push_back(third_device); | |
| 125 | |
| 126 EXPECT_CALL(mock_description_service_, GetDeviceDescriptions(deviceList, _)); | |
| 127 | |
| 128 media_sink_service()->OnDialDeviceEvent(deviceList); | |
| 129 EXPECT_TRUE(media_sink_service_.finish_timer_->IsRunning()); | |
| 130 } | |
| 131 | |
| 132 TEST_F(DialMediaSinkServiceTest, TestFetchCompleted) { | |
| 133 std::vector<MediaSinkInternal> old_sinks; | |
| 134 std::vector<MediaSinkInternal> new_sinks = CreateDialMediaSinks(); | |
| 135 TestFetchCompleted(old_sinks, new_sinks); | |
| 136 | |
| 137 // Same sink | |
| 138 EXPECT_CALL(mock_sink_discovered_cb_, Run(new_sinks)).Times(0); | |
| 139 media_sink_service()->OnFetchCompleted(); | |
| 140 } | |
| 141 | |
| 142 TEST_F(DialMediaSinkServiceTest, TestFetchCompleted_OneNewSink) { | |
| 143 std::vector<MediaSinkInternal> old_sinks = CreateDialMediaSinks(); | |
| 144 std::vector<MediaSinkInternal> new_sinks = CreateDialMediaSinks(); | |
| 145 MediaSink sink3("sink3", "sink_name_3", MediaSink::IconType::CAST); | |
| 146 DialSinkExtraData extra_data3 = | |
| 147 MediaSinkInternalTest::CreateDialSinkExtraData( | |
| 148 "model_name3", "192.168.1.3", "https://example3.com"); | |
| 149 new_sinks.push_back(MediaSinkInternal(sink3, extra_data3)); | |
| 150 TestFetchCompleted(old_sinks, new_sinks); | |
| 151 } | |
| 152 | |
| 153 TEST_F(DialMediaSinkServiceTest, TestFetchCompleted_RemovedOneSink) { | |
| 154 std::vector<MediaSinkInternal> old_sinks = CreateDialMediaSinks(); | |
| 155 std::vector<MediaSinkInternal> new_sinks = CreateDialMediaSinks(); | |
| 156 new_sinks.erase(new_sinks.begin()); | |
| 157 TestFetchCompleted(old_sinks, new_sinks); | |
| 158 } | |
| 159 | |
| 160 TEST_F(DialMediaSinkServiceTest, TestFetchCompleted_UpdatedOneSink) { | |
| 161 std::vector<MediaSinkInternal> old_sinks = CreateDialMediaSinks(); | |
| 162 std::vector<MediaSinkInternal> new_sinks = CreateDialMediaSinks(); | |
| 163 new_sinks[0].set_name("sink_name_4"); | |
| 164 TestFetchCompleted(old_sinks, new_sinks); | |
| 165 } | |
| 166 | |
| 167 TEST_F(DialMediaSinkServiceTest, TestFetchCompleted_Mixed) { | |
| 168 std::vector<MediaSinkInternal> old_sinks = CreateDialMediaSinks(); | |
| 169 | |
| 170 MediaSink sink1("sink1", "sink_name_1", MediaSink::IconType::CAST); | |
| 171 DialSinkExtraData extra_data2 = | |
| 172 MediaSinkInternalTest::CreateDialSinkExtraData( | |
| 173 "model_name2", "192.168.1.2", "https://example2.com"); | |
| 174 | |
| 175 MediaSink sink3("sink3", "sink_name_3", MediaSink::IconType::CAST); | |
| 176 DialSinkExtraData extra_data3 = | |
| 177 MediaSinkInternalTest::CreateDialSinkExtraData( | |
| 178 "model_name3", "192.168.1.3", "https://example3.com"); | |
| 179 | |
| 180 std::vector<MediaSinkInternal> new_sinks; | |
| 181 new_sinks.push_back(MediaSinkInternal(sink1, extra_data2)); | |
| 182 new_sinks.push_back(MediaSinkInternal(sink3, extra_data3)); | |
| 183 | |
| 184 TestFetchCompleted(old_sinks, new_sinks); | |
| 185 } | |
| 186 | |
| 187 } // namespace media_router | |
| OLD | NEW |