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/dial_media_sink_cache_service.h" |
| 6 #include "chrome/browser/media/router/mock_device_description_service.h" |
| 7 #include "chrome/test/base/testing_profile.h" |
| 8 #include "content/public/test/test_browser_thread_bundle.h" |
| 9 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 using ::testing::_; |
| 13 |
| 14 namespace media_router { |
| 15 |
| 16 class DialMediaSinkCacheServiceTest : public ::testing::Test { |
| 17 public: |
| 18 void TearDown() override { cache_service_.cached_sinks_.clear(); } |
| 19 |
| 20 void AddTestDialMediaSinks() { |
| 21 MediaSink sink1("sink1", "sink_name_1", MediaSink::IconType::CAST); |
| 22 DialSinkExtraData extra_data1("192.168.1.1", "model_name1", |
| 23 "https://example1.com"); |
| 24 MediaSinkInternal dial_sink1(sink1, extra_data1); |
| 25 GURL gurl1 = GURL("https://device1"); |
| 26 base::Time last_update_time1 = |
| 27 base::Time::Now() - base::TimeDelta::FromSeconds(60); |
| 28 AddTestDialMediaSink(dial_sink1, gurl1, last_update_time1); |
| 29 |
| 30 MediaSink sink2("sink2", "sink_name_2", MediaSink::IconType::CAST); |
| 31 DialSinkExtraData extra_data2("192.168.1.2", "model_name2", |
| 32 "https://example2.com"); |
| 33 MediaSinkInternal dial_sink2(sink2, extra_data2); |
| 34 GURL gurl2 = GURL("https://device2"); |
| 35 base::Time last_update_time2 = base::Time::Now(); |
| 36 AddTestDialMediaSink(dial_sink2, gurl2, last_update_time2); |
| 37 } |
| 38 |
| 39 void AddTestDialMediaSink(const MediaSinkInternal& media_sink, |
| 40 const GURL& description_url, |
| 41 base::Time last_update_time) { |
| 42 CachedDialMediaSink dial_sink; |
| 43 dial_sink.sink = media_sink; |
| 44 dial_sink.description_url = description_url; |
| 45 dial_sink.last_update_time = last_update_time; |
| 46 cache_service_.cached_sinks_.insert( |
| 47 std::make_pair(media_sink.sink().id(), dial_sink)); |
| 48 } |
| 49 |
| 50 DialMediaSinkCacheService* cache_service() { return &cache_service_; } |
| 51 |
| 52 protected: |
| 53 const content::TestBrowserThreadBundle thread_bundle_; |
| 54 TestingProfile profile_; |
| 55 DialMediaSinkCacheService cache_service_; |
| 56 }; |
| 57 |
| 58 TEST_F(DialMediaSinkCacheServiceTest, GetAliveSinks) { |
| 59 auto sinks = cache_service()->GetAliveSinks(); |
| 60 ASSERT_TRUE(sinks.empty()); |
| 61 |
| 62 AddTestDialMediaSinks(); |
| 63 |
| 64 sinks = cache_service()->GetAliveSinks(); |
| 65 EXPECT_EQ(size_t(1), sinks.size()); |
| 66 } |
| 67 |
| 68 TEST_F(DialMediaSinkCacheServiceTest, TestPruneInactiveSinks) { |
| 69 AddTestDialMediaSinks(); |
| 70 |
| 71 MockDeviceDescriptionServiceObserver mock_observer; |
| 72 MockDeviceDescriptionService mock_description_service(&mock_observer); |
| 73 EXPECT_CALL(mock_description_service, CheckAccess(_, _, _)).Times(1); |
| 74 |
| 75 cache_service()->PruneInactiveSinks(true, &mock_description_service, |
| 76 profile_.GetRequestContext()); |
| 77 |
| 78 cache_service()->PruneInactiveSinks(false, &mock_description_service, |
| 79 profile_.GetRequestContext()); |
| 80 } |
| 81 |
| 82 TEST_F(DialMediaSinkCacheServiceTest, TestAddOrUpdateSink) { |
| 83 std::string sink_id = "sink_id"; |
| 84 std::string sink_name = "sink_name"; |
| 85 std::string sink_name_updated = "sink_name_updated"; |
| 86 |
| 87 DialDeviceDescription desc1; |
| 88 desc1.unique_id = sink_id; |
| 89 desc1.friendly_name = sink_name; |
| 90 desc1.ip_address = "192.168.1.1"; |
| 91 ASSERT_TRUE(cache_service()->AddOrUpdateSink(desc1)); |
| 92 |
| 93 auto sinks = cache_service()->GetAliveSinks(); |
| 94 EXPECT_EQ(size_t(1), sinks.size()); |
| 95 EXPECT_EQ(sink_id, sinks[0].sink().id()); |
| 96 EXPECT_EQ(sink_name, sinks[0].sink().name()); |
| 97 |
| 98 DialDeviceDescription desc2; |
| 99 desc2.unique_id = sink_id; |
| 100 desc2.friendly_name = sink_name_updated; |
| 101 desc2.ip_address = "192.168.1.2"; |
| 102 ASSERT_TRUE(cache_service()->AddOrUpdateSink(desc2)); |
| 103 |
| 104 sinks = cache_service()->GetAliveSinks(); |
| 105 EXPECT_EQ(size_t(1), sinks.size()); |
| 106 EXPECT_EQ(sink_id, sinks[0].sink().id()); |
| 107 EXPECT_EQ(sink_name_updated, sinks[0].sink().name()); |
| 108 |
| 109 DialDeviceDescription desc3; |
| 110 desc3.unique_id = sink_id; |
| 111 desc3.friendly_name = sink_name_updated; |
| 112 desc3.ip_address = "192.168.1.2"; |
| 113 ASSERT_FALSE(cache_service()->AddOrUpdateSink(desc3)); |
| 114 } |
| 115 |
| 116 } // namespace media_router |
OLD | NEW |