Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/media/router/discovery/dial/dial_media_sink_service.h" | 5 #include "chrome/browser/media/router/discovery/dial/dial_media_sink_service.h" |
| 6 #include "base/run_loop.h" | |
| 6 #include "base/test/mock_callback.h" | 7 #include "base/test/mock_callback.h" |
| 7 #include "chrome/browser/media/router/discovery/dial/dial_device_data.h" | 8 #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/dial_registry.h" |
| 9 #include "chrome/browser/media/router/test_helper.h" | 10 #include "chrome/browser/media/router/test_helper.h" |
| 10 #include "chrome/test/base/testing_profile.h" | 11 #include "chrome/test/base/testing_profile.h" |
| 11 #include "content/public/test/test_browser_thread_bundle.h" | 12 #include "content/public/test/test_browser_thread_bundle.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| 15 using ::testing::_; | 16 using ::testing::_; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 49 | 50 |
| 50 namespace media_router { | 51 namespace media_router { |
| 51 | 52 |
| 52 class TestDialRegistry : public DialRegistry { | 53 class TestDialRegistry : public DialRegistry { |
| 53 public: | 54 public: |
| 54 TestDialRegistry() {} | 55 TestDialRegistry() {} |
| 55 ~TestDialRegistry() {} | 56 ~TestDialRegistry() {} |
| 56 | 57 |
| 57 MOCK_METHOD1(RegisterObserver, void(DialRegistry::Observer* observer)); | 58 MOCK_METHOD1(RegisterObserver, void(DialRegistry::Observer* observer)); |
| 58 MOCK_METHOD1(UnregisterObserver, void(DialRegistry::Observer* observer)); | 59 MOCK_METHOD1(UnregisterObserver, void(DialRegistry::Observer* observer)); |
| 60 | |
| 61 MOCK_METHOD0(StartPeriodicDiscovery, void()); | |
| 62 MOCK_METHOD0(StopPeriodicDiscovery, void()); | |
| 59 }; | 63 }; |
| 60 | 64 |
| 61 class MockDeviceDescriptionService : public DeviceDescriptionService { | 65 class MockDeviceDescriptionService : public DeviceDescriptionService { |
| 62 public: | 66 public: |
| 63 MockDeviceDescriptionService(DeviceDescriptionParseSuccessCallback success_cb, | 67 MockDeviceDescriptionService(DeviceDescriptionParseSuccessCallback success_cb, |
| 64 DeviceDescriptionParseErrorCallback error_cb) | 68 DeviceDescriptionParseErrorCallback error_cb) |
| 65 : DeviceDescriptionService(success_cb, error_cb) {} | 69 : DeviceDescriptionService(success_cb, error_cb) {} |
| 66 ~MockDeviceDescriptionService() override {} | 70 ~MockDeviceDescriptionService() override {} |
| 67 | 71 |
| 68 MOCK_METHOD2(GetDeviceDescriptions, | 72 MOCK_METHOD2(GetDeviceDescriptions, |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 81 test_dial_registry_(test_dial_registry), | 85 test_dial_registry_(test_dial_registry), |
| 82 mock_description_service_(mock_description_service) {} | 86 mock_description_service_(mock_description_service) {} |
| 83 | 87 |
| 84 DialRegistry* dial_registry() override { return test_dial_registry_; } | 88 DialRegistry* dial_registry() override { return test_dial_registry_; } |
| 85 | 89 |
| 86 DeviceDescriptionService* GetDescriptionService() override { | 90 DeviceDescriptionService* GetDescriptionService() override { |
| 87 return mock_description_service_; | 91 return mock_description_service_; |
| 88 } | 92 } |
| 89 | 93 |
| 90 private: | 94 private: |
| 95 ~TestDialMediaSinkService() override {} | |
|
Kevin M
2017/05/03 21:07:27
Is this necessary? The object will be deleted from
zhaobin
2017/05/04 23:52:36
Code removed.
| |
| 96 | |
| 91 TestDialRegistry* test_dial_registry_; | 97 TestDialRegistry* test_dial_registry_; |
| 92 MockDeviceDescriptionService* mock_description_service_; | 98 MockDeviceDescriptionService* mock_description_service_; |
| 93 }; | 99 }; |
| 94 | 100 |
| 95 class DialMediaSinkServiceTest : public ::testing::Test { | 101 class DialMediaSinkServiceTest : public ::testing::Test { |
| 96 public: | 102 public: |
| 97 DialMediaSinkServiceTest() | 103 DialMediaSinkServiceTest() |
| 98 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 104 : mock_description_service_(mock_success_cb_.Get(), mock_error_cb_.Get()), |
| 99 mock_description_service_(mock_success_cb_.Get(), mock_error_cb_.Get()), | 105 media_sink_service_( |
| 100 media_sink_service_(mock_sink_discovered_cb_.Get(), | 106 new TestDialMediaSinkService(mock_sink_discovered_cb_.Get(), |
| 101 profile_.GetRequestContext(), | 107 profile_.GetRequestContext(), |
| 102 &test_dial_registry_, | 108 &test_dial_registry_, |
| 103 &mock_description_service_) {} | 109 &mock_description_service_)) {} |
| 104 | 110 |
| 105 DialMediaSinkService* media_sink_service() { return &media_sink_service_; } | 111 DialMediaSinkService* media_sink_service() { |
|
Kevin M
2017/05/03 21:07:27
Getter method isn't necessary? Can just use arrow
zhaobin
2017/05/04 23:52:36
Done.
| |
| 112 return media_sink_service_.get(); | |
| 113 } | |
| 106 | 114 |
| 107 void TestFetchCompleted(const std::vector<MediaSinkInternal>& old_sinks, | 115 void TestFetchCompleted(const std::vector<MediaSinkInternal>& old_sinks, |
| 108 const std::vector<MediaSinkInternal>& new_sinks) { | 116 const std::vector<MediaSinkInternal>& new_sinks) { |
| 117 base::RunLoop run_loop; | |
|
Kevin M
2017/05/03 21:07:27
Nit..
*The runloop doesn't have to be in scope wh
zhaobin
2017/05/04 23:52:36
Done.
| |
| 109 media_sink_service()->mrp_sinks_ = | 118 media_sink_service()->mrp_sinks_ = |
| 110 std::set<MediaSinkInternal>(old_sinks.begin(), old_sinks.end()); | 119 std::set<MediaSinkInternal>(old_sinks.begin(), old_sinks.end()); |
| 111 media_sink_service()->current_sinks_ = | 120 media_sink_service()->current_sinks_ = |
| 112 std::set<MediaSinkInternal>(new_sinks.begin(), new_sinks.end()); | 121 std::set<MediaSinkInternal>(new_sinks.begin(), new_sinks.end()); |
| 113 EXPECT_CALL(mock_sink_discovered_cb_, Run(new_sinks)); | 122 EXPECT_CALL(mock_sink_discovered_cb_, Run(new_sinks)); |
| 114 media_sink_service()->OnFetchCompleted(); | 123 media_sink_service()->OnFetchCompleted(); |
|
Kevin M
2017/05/03 21:07:27
Also, see my previous comment about Timer dependen
zhaobin
2017/05/04 23:52:36
This aims to test OnFetchCompleted() function only
| |
| 124 run_loop.RunUntilIdle(); | |
| 115 } | 125 } |
| 116 | 126 |
| 117 protected: | 127 protected: |
| 118 const content::TestBrowserThreadBundle thread_bundle_; | 128 const content::TestBrowserThreadBundle thread_bundle_; |
| 119 TestingProfile profile_; | 129 TestingProfile profile_; |
| 120 | 130 |
| 121 base::MockCallback<MediaSinkService::OnSinksDiscoveredCallback> | 131 base::MockCallback<MediaSinkService::OnSinksDiscoveredCallback> |
| 122 mock_sink_discovered_cb_; | 132 mock_sink_discovered_cb_; |
| 123 base::MockCallback< | 133 base::MockCallback< |
| 124 MockDeviceDescriptionService::DeviceDescriptionParseSuccessCallback> | 134 MockDeviceDescriptionService::DeviceDescriptionParseSuccessCallback> |
| 125 mock_success_cb_; | 135 mock_success_cb_; |
| 126 base::MockCallback< | 136 base::MockCallback< |
| 127 MockDeviceDescriptionService::DeviceDescriptionParseErrorCallback> | 137 MockDeviceDescriptionService::DeviceDescriptionParseErrorCallback> |
| 128 mock_error_cb_; | 138 mock_error_cb_; |
| 129 | 139 |
| 130 TestDialRegistry test_dial_registry_; | 140 TestDialRegistry test_dial_registry_; |
| 131 MockDeviceDescriptionService mock_description_service_; | 141 MockDeviceDescriptionService mock_description_service_; |
| 132 | 142 |
| 133 TestDialMediaSinkService media_sink_service_; | 143 scoped_refptr<TestDialMediaSinkService> media_sink_service_; |
|
Kevin M
2017/05/03 21:07:27
DISALLOW_COPY_AND_ASSIGN(DialMediaSinkServiceTest)
zhaobin
2017/05/04 23:52:36
Done.
| |
| 134 }; | 144 }; |
| 135 | 145 |
| 136 TEST_F(DialMediaSinkServiceTest, TestStart) { | 146 TEST_F(DialMediaSinkServiceTest, TestStart) { |
| 137 EXPECT_CALL(test_dial_registry_, RegisterObserver(&media_sink_service_)); | 147 base::RunLoop run_loop; |
| 148 EXPECT_CALL(test_dial_registry_, RegisterObserver(media_sink_service())); | |
| 149 EXPECT_CALL(test_dial_registry_, StartPeriodicDiscovery()); | |
| 138 media_sink_service()->Start(); | 150 media_sink_service()->Start(); |
| 139 | 151 |
| 140 DialRegistry::DeviceList deviceList; | 152 DialRegistry::DeviceList deviceList; |
| 141 DialDeviceData first_device("first", GURL("http://127.0.0.1/dd.xml"), | 153 DialDeviceData first_device("first", GURL("http://127.0.0.1/dd.xml"), |
| 142 base::Time::Now()); | 154 base::Time::Now()); |
| 143 DialDeviceData second_device("second", GURL("http://127.0.0.2/dd.xml"), | 155 DialDeviceData second_device("second", GURL("http://127.0.0.2/dd.xml"), |
| 144 base::Time::Now()); | 156 base::Time::Now()); |
| 145 DialDeviceData third_device("third", GURL("http://127.0.0.3/dd.xml"), | 157 DialDeviceData third_device("third", GURL("http://127.0.0.3/dd.xml"), |
| 146 base::Time::Now()); | 158 base::Time::Now()); |
| 147 deviceList.push_back(first_device); | 159 deviceList.push_back(first_device); |
| 148 deviceList.push_back(second_device); | 160 deviceList.push_back(second_device); |
| 149 deviceList.push_back(third_device); | 161 deviceList.push_back(third_device); |
| 150 | 162 |
| 151 EXPECT_CALL(mock_description_service_, GetDeviceDescriptions(deviceList, _)); | 163 EXPECT_CALL(mock_description_service_, GetDeviceDescriptions(deviceList, _)); |
| 152 | 164 |
| 153 media_sink_service()->OnDialDeviceEvent(deviceList); | 165 media_sink_service()->OnDialDeviceEvent(deviceList); |
| 154 EXPECT_TRUE(media_sink_service_.finish_timer_->IsRunning()); | 166 EXPECT_TRUE(media_sink_service()->finish_timer_->IsRunning()); |
|
Kevin M
2017/05/03 21:07:27
No need to use a getter function; you can use the
zhaobin
2017/05/04 23:52:36
Done.
| |
| 167 run_loop.RunUntilIdle(); | |
| 155 } | 168 } |
| 156 | 169 |
| 157 TEST_F(DialMediaSinkServiceTest, TestOnDeviceDescriptionAvailable) { | 170 TEST_F(DialMediaSinkServiceTest, TestOnDeviceDescriptionAvailable) { |
| 158 DialDeviceData device_data("first", GURL("http://127.0.0.1/dd.xml"), | 171 DialDeviceData device_data("first", GURL("http://127.0.0.1/dd.xml"), |
| 159 base::Time::Now()); | 172 base::Time::Now()); |
| 160 ParsedDialDeviceDescription device_description; | 173 ParsedDialDeviceDescription device_description; |
| 161 device_description.model_name = "model name"; | 174 device_description.model_name = "model name"; |
| 162 device_description.friendly_name = "friendly name"; | 175 device_description.friendly_name = "friendly name"; |
| 163 device_description.app_url = GURL("http://192.168.1.1/apps"); | 176 device_description.app_url = GURL("http://192.168.1.1/apps"); |
| 164 device_description.unique_id = "unique id"; | 177 device_description.unique_id = "unique id"; |
| 165 | 178 |
| 166 media_sink_service()->OnDeviceDescriptionAvailable(device_data, | 179 media_sink_service()->OnDeviceDescriptionAvailable(device_data, |
| 167 device_description); | 180 device_description); |
| 168 EXPECT_TRUE(media_sink_service()->current_sinks_.empty()); | 181 EXPECT_TRUE(media_sink_service()->current_sinks_.empty()); |
| 169 | 182 |
| 170 std::vector<DialDeviceData> deviceList{device_data}; | 183 std::vector<DialDeviceData> deviceList{device_data}; |
| 171 EXPECT_CALL(mock_description_service_, GetDeviceDescriptions(deviceList, _)); | 184 EXPECT_CALL(mock_description_service_, GetDeviceDescriptions(deviceList, _)); |
| 172 | 185 |
| 173 media_sink_service()->OnDialDeviceEvent(deviceList); | 186 media_sink_service()->OnDialDeviceEvent(deviceList); |
| 174 media_sink_service()->OnDeviceDescriptionAvailable(device_data, | 187 media_sink_service()->OnDeviceDescriptionAvailable(device_data, |
| 175 device_description); | 188 device_description); |
| 176 | 189 |
| 177 EXPECT_EQ(size_t(1), media_sink_service()->current_sinks_.size()); | 190 EXPECT_EQ(size_t(1), media_sink_service()->current_sinks_.size()); |
| 178 } | 191 } |
| 179 | 192 |
| 180 TEST_F(DialMediaSinkServiceTest, TestFetchCompleted) { | 193 TEST_F(DialMediaSinkServiceTest, TestFetchCompleted_SameSink) { |
| 181 std::vector<MediaSinkInternal> old_sinks; | 194 std::vector<MediaSinkInternal> old_sinks; |
| 182 std::vector<MediaSinkInternal> new_sinks = CreateDialMediaSinks(); | 195 std::vector<MediaSinkInternal> new_sinks = CreateDialMediaSinks(); |
| 183 TestFetchCompleted(old_sinks, new_sinks); | 196 TestFetchCompleted(old_sinks, new_sinks); |
| 184 | 197 |
| 185 // Same sink | 198 // Same sink |
| 199 base::RunLoop run_loop; | |
| 186 EXPECT_CALL(mock_sink_discovered_cb_, Run(new_sinks)).Times(0); | 200 EXPECT_CALL(mock_sink_discovered_cb_, Run(new_sinks)).Times(0); |
| 187 media_sink_service()->OnFetchCompleted(); | 201 media_sink_service()->OnFetchCompleted(); |
| 202 run_loop.RunUntilIdle(); | |
| 188 } | 203 } |
| 189 | 204 |
| 190 TEST_F(DialMediaSinkServiceTest, TestFetchCompleted_OneNewSink) { | 205 TEST_F(DialMediaSinkServiceTest, TestFetchCompleted_OneNewSink) { |
| 191 std::vector<MediaSinkInternal> old_sinks = CreateDialMediaSinks(); | 206 std::vector<MediaSinkInternal> old_sinks = CreateDialMediaSinks(); |
| 192 std::vector<MediaSinkInternal> new_sinks = CreateDialMediaSinks(); | 207 std::vector<MediaSinkInternal> new_sinks = CreateDialMediaSinks(); |
| 193 MediaSink sink3("sink3", "sink_name_3", MediaSink::IconType::CAST); | 208 MediaSink sink3("sink3", "sink_name_3", MediaSink::IconType::CAST); |
| 194 DialSinkExtraData extra_data3 = CreateDialSinkExtraData( | 209 DialSinkExtraData extra_data3 = CreateDialSinkExtraData( |
| 195 "model_name3", "192.168.1.3", "https://example3.com"); | 210 "model_name3", "192.168.1.3", "https://example3.com"); |
| 196 new_sinks.push_back(MediaSinkInternal(sink3, extra_data3)); | 211 new_sinks.push_back(MediaSinkInternal(sink3, extra_data3)); |
| 197 TestFetchCompleted(old_sinks, new_sinks); | 212 TestFetchCompleted(old_sinks, new_sinks); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 223 "model_name3", "192.168.1.3", "https://example3.com"); | 238 "model_name3", "192.168.1.3", "https://example3.com"); |
| 224 | 239 |
| 225 std::vector<MediaSinkInternal> new_sinks; | 240 std::vector<MediaSinkInternal> new_sinks; |
| 226 new_sinks.push_back(MediaSinkInternal(sink1, extra_data2)); | 241 new_sinks.push_back(MediaSinkInternal(sink1, extra_data2)); |
| 227 new_sinks.push_back(MediaSinkInternal(sink3, extra_data3)); | 242 new_sinks.push_back(MediaSinkInternal(sink3, extra_data3)); |
| 228 | 243 |
| 229 TestFetchCompleted(old_sinks, new_sinks); | 244 TestFetchCompleted(old_sinks, new_sinks); |
| 230 } | 245 } |
| 231 | 246 |
| 232 } // namespace media_router | 247 } // namespace media_router |
| OLD | NEW |