Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: chrome/browser/media/router/discovery/dial/dial_registry_unittest.cc

Issue 2754703005: [Device Discovery] Make DialRegistry a Singleton (Closed)
Patch Set: fix mac and windows compile errors Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/media/router/discovery/dial/dial_registry.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "chrome/browser/media/router/discovery/dial/dial_device_data.h" 8 #include "chrome/browser/media/router/discovery/dial/dial_device_data.h"
9 #include "chrome/browser/media/router/discovery/dial/dial_registry.h" 9 #include "chrome/browser/media/router/discovery/dial/dial_registry.h"
10 #include "chrome/browser/media/router/discovery/dial/dial_service.h" 10 #include "chrome/browser/media/router/discovery/dial/dial_service.h"
(...skipping 26 matching lines...) Expand all
37 ~MockDialService() override {} 37 ~MockDialService() override {}
38 38
39 MOCK_METHOD0(Discover, bool()); 39 MOCK_METHOD0(Discover, bool());
40 MOCK_METHOD1(AddObserver, void(DialService::Observer*)); 40 MOCK_METHOD1(AddObserver, void(DialService::Observer*));
41 MOCK_METHOD1(RemoveObserver, void(DialService::Observer*)); 41 MOCK_METHOD1(RemoveObserver, void(DialService::Observer*));
42 MOCK_CONST_METHOD1(HasObserver, bool(const DialService::Observer*)); 42 MOCK_CONST_METHOD1(HasObserver, bool(const DialService::Observer*));
43 }; 43 };
44 44
45 class MockDialRegistry : public DialRegistry { 45 class MockDialRegistry : public DialRegistry {
46 public: 46 public:
47 MockDialRegistry(const base::TimeDelta& refresh_interval, 47 MockDialRegistry() : DialRegistry(), time_(Time::Now()) {}
48 const base::TimeDelta& expiration,
49 const size_t max_devices)
50 : DialRegistry(refresh_interval, expiration, max_devices),
51 time_(Time::Now()) {}
52 48
53 ~MockDialRegistry() override { 49 ~MockDialRegistry() override {
54 // Don't let the DialRegistry delete this. 50 // Don't let the DialRegistry delete this.
55 DialService* tmp = dial_.release(); 51 DialService* tmp = dial_.release();
56 if (tmp) 52 if (tmp)
57 CHECK_EQ(&mock_service_, tmp); 53 CHECK_EQ(&mock_service_, tmp);
58 } 54 }
59 55
60 // Returns the mock Dial service. 56 // Returns the mock Dial service.
61 MockDialService& mock_service() { return mock_service_; } 57 MockDialService& mock_service() { return mock_service_; }
(...skipping 19 matching lines...) Expand all
81 Time time_; 77 Time time_;
82 }; 78 };
83 79
84 class DialRegistryTest : public testing::Test { 80 class DialRegistryTest : public testing::Test {
85 public: 81 public:
86 DialRegistryTest() 82 DialRegistryTest()
87 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), 83 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
88 first_device_("first", GURL("http://127.0.0.1/dd.xml"), Time::Now()), 84 first_device_("first", GURL("http://127.0.0.1/dd.xml"), Time::Now()),
89 second_device_("second", GURL("http://127.0.0.2/dd.xml"), Time::Now()), 85 second_device_("second", GURL("http://127.0.0.2/dd.xml"), Time::Now()),
90 third_device_("third", GURL("http://127.0.0.3/dd.xml"), Time::Now()) { 86 third_device_("third", GURL("http://127.0.0.3/dd.xml"), Time::Now()) {
91 registry_ = base::MakeUnique<MockDialRegistry>( 87 registry_ = base::MakeUnique<MockDialRegistry>();
92 TimeDelta::FromSeconds(1000), TimeDelta::FromSeconds(10), 10);
93 registry_->RegisterObserver(&mock_observer_); 88 registry_->RegisterObserver(&mock_observer_);
94 list_with_first_device_.push_back(first_device_); 89 list_with_first_device_.push_back(first_device_);
95 list_with_second_device_.push_back(second_device_); 90 list_with_second_device_.push_back(second_device_);
96 } 91 }
97 92
98 protected: 93 protected:
99 void SetListenerExpectations() { 94 void SetListenerExpectations() {
100 EXPECT_CALL(registry_->mock_service(), 95 EXPECT_CALL(registry_->mock_service(),
101 AddObserver(A<DialService::Observer*>())); 96 AddObserver(A<DialService::Observer*>()));
102 EXPECT_CALL(registry_->mock_service(), 97 EXPECT_CALL(registry_->mock_service(),
(...skipping 10 matching lines...) Expand all
113 const DialRegistry::DeviceList empty_list_; 108 const DialRegistry::DeviceList empty_list_;
114 DialRegistry::DeviceList list_with_first_device_; 109 DialRegistry::DeviceList list_with_first_device_;
115 DialRegistry::DeviceList list_with_second_device_; 110 DialRegistry::DeviceList list_with_second_device_;
116 }; 111 };
117 112
118 TEST_F(DialRegistryTest, TestAddRemoveListeners) { 113 TEST_F(DialRegistryTest, TestAddRemoveListeners) {
119 SetListenerExpectations(); 114 SetListenerExpectations();
120 EXPECT_CALL(registry_->mock_service(), Discover()); 115 EXPECT_CALL(registry_->mock_service(), Discover());
121 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(empty_list_)).Times(2); 116 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(empty_list_)).Times(2);
122 117
123 EXPECT_FALSE(registry_->repeating_timer_.IsRunning()); 118 EXPECT_FALSE(registry_->repeating_timer_);
124 registry_->OnListenerAdded(); 119 registry_->OnListenerAdded();
125 EXPECT_TRUE(registry_->repeating_timer_.IsRunning()); 120 EXPECT_TRUE(registry_->repeating_timer_->IsRunning());
126 registry_->OnListenerAdded(); 121 registry_->OnListenerAdded();
127 EXPECT_TRUE(registry_->repeating_timer_.IsRunning()); 122 EXPECT_TRUE(registry_->repeating_timer_->IsRunning());
128 registry_->OnListenerRemoved(); 123 registry_->OnListenerRemoved();
129 EXPECT_TRUE(registry_->repeating_timer_.IsRunning()); 124 EXPECT_TRUE(registry_->repeating_timer_->IsRunning());
130 registry_->OnListenerRemoved(); 125 registry_->OnListenerRemoved();
131 EXPECT_FALSE(registry_->repeating_timer_.IsRunning()); 126 EXPECT_FALSE(registry_->repeating_timer_);
132 } 127 }
133 128
134 TEST_F(DialRegistryTest, TestNoDevicesDiscovered) { 129 TEST_F(DialRegistryTest, TestNoDevicesDiscovered) {
135 SetListenerExpectations(); 130 SetListenerExpectations();
136 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(empty_list_)); 131 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(empty_list_));
137 EXPECT_CALL(registry_->mock_service(), Discover()); 132 EXPECT_CALL(registry_->mock_service(), Discover());
138 133
139 registry_->OnListenerAdded(); 134 registry_->OnListenerAdded();
140 registry_->OnDiscoveryRequest(nullptr); 135 registry_->OnDiscoveryRequest(nullptr);
141 registry_->OnDiscoveryFinished(nullptr); 136 registry_->OnDiscoveryFinished(nullptr);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(empty_list_)); 198 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(empty_list_));
204 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(list_with_first_device_)); 199 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(list_with_first_device_));
205 EXPECT_CALL(registry_->mock_service(), Discover()); 200 EXPECT_CALL(registry_->mock_service(), Discover());
206 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(empty_list_)); 201 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(empty_list_));
207 202
208 registry_->OnListenerAdded(); 203 registry_->OnListenerAdded();
209 registry_->OnDiscoveryRequest(nullptr); 204 registry_->OnDiscoveryRequest(nullptr);
210 registry_->OnDeviceDiscovered(nullptr, first_device_); 205 registry_->OnDeviceDiscovered(nullptr, first_device_);
211 registry_->OnDiscoveryFinished(nullptr); 206 registry_->OnDiscoveryFinished(nullptr);
212 207
213 registry_->set_time(Time::Now() + TimeDelta::FromSeconds(30)); 208 registry_->set_time(Time::Now() + TimeDelta::FromSeconds(300));
214 209
215 registry_->DoDiscovery(); 210 registry_->DoDiscovery();
216 registry_->OnDiscoveryRequest(nullptr); 211 registry_->OnDiscoveryRequest(nullptr);
217 registry_->OnDiscoveryFinished(nullptr); 212 registry_->OnDiscoveryFinished(nullptr);
218 registry_->OnListenerRemoved(); 213 registry_->OnListenerRemoved();
219 } 214 }
220 215
221 TEST_F(DialRegistryTest, TestExpiredDeviceIsRediscovered) { 216 TEST_F(DialRegistryTest, TestExpiredDeviceIsRediscovered) {
222 std::vector<Time> discovery_times; 217 std::vector<Time> discovery_times;
223 discovery_times.push_back(Time::Now()); 218 discovery_times.push_back(Time::Now());
224 discovery_times.push_back(discovery_times[0] + TimeDelta::FromSeconds(30)); 219 discovery_times.push_back(discovery_times[0] + TimeDelta::FromSeconds(300));
225 discovery_times.push_back(discovery_times[1] + TimeDelta::FromSeconds(30)); 220 discovery_times.push_back(discovery_times[1] + TimeDelta::FromSeconds(300));
226 221
227 DialDeviceData rediscovered_device("first", GURL("http://127.0.0.1/dd.xml"), 222 DialDeviceData rediscovered_device("first", GURL("http://127.0.0.1/dd.xml"),
228 discovery_times[2]); 223 discovery_times[2]);
229 224
230 SetListenerExpectations(); 225 SetListenerExpectations();
231 226
232 InSequence s; 227 InSequence s;
233 EXPECT_CALL(registry_->mock_service(), Discover()); 228 EXPECT_CALL(registry_->mock_service(), Discover());
234 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(empty_list_)); 229 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(empty_list_));
235 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(list_with_first_device_)); 230 EXPECT_CALL(mock_observer_, OnDialDeviceEvent(list_with_first_device_));
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 registry_->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_ETHERNET); 361 registry_->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_ETHERNET);
367 362
368 registry_->OnDiscoveryRequest(nullptr); 363 registry_->OnDiscoveryRequest(nullptr);
369 registry_->OnDeviceDiscovered(nullptr, third_device_); 364 registry_->OnDeviceDiscovered(nullptr, third_device_);
370 registry_->OnDiscoveryFinished(nullptr); 365 registry_->OnDiscoveryFinished(nullptr);
371 366
372 registry_->OnListenerRemoved(); 367 registry_->OnListenerRemoved();
373 } 368 }
374 369
375 } // namespace media_router 370 } // namespace media_router
OLDNEW
« no previous file with comments | « chrome/browser/media/router/discovery/dial/dial_registry.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698