1 // Copyright 2013 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.
5 #include "chrome/browser/extensions/api/mdns/dns_sd_registry.h"
6 #include "chrome/browser/extensions/api/mdns/dns_sd_delegate.h"
7 #include "chrome/browser/extensions/api/mdns/dns_sd_device_lister.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace extensions
{
13 class MockDnsSdDeviceLister
: public DnsSdDeviceLister
{
15 MockDnsSdDeviceLister() : DnsSdDeviceLister(NULL
, NULL
, "") {}
16 ~MockDnsSdDeviceLister() override
{}
18 MOCK_METHOD1(Discover
, void(bool force_update
));
21 class TestDnsSdRegistry
: public DnsSdRegistry
{
23 TestDnsSdRegistry() : DnsSdRegistry(NULL
), delegate_(NULL
) {}
24 ~TestDnsSdRegistry() override
{}
26 MockDnsSdDeviceLister
* GetListerForService(const std::string
& service_type
) {
27 return listers_
[service_type
];
30 int GetServiceListenerCount(const std::string
& service_type
) {
31 if (service_data_map_
.find(service_type
) == service_data_map_
.end())
34 return service_data_map_
[service_type
].get()->GetListenerCount();
37 MockDnsSdDeviceLister
* GetLister(const std::string
& service_type
) {
38 return listers_
.find(service_type
) != listers_
.end()
39 ? listers_
[service_type
]
43 DnsSdDelegate
* GetDelegate() {
48 DnsSdDeviceLister
* CreateDnsSdDeviceLister(
49 DnsSdDelegate
* delegate
,
50 const std::string
& service_type
,
51 local_discovery::ServiceDiscoverySharedClient
* discovery_client
)
54 MockDnsSdDeviceLister
* lister
= new MockDnsSdDeviceLister();
55 listers_
[service_type
] = lister
;
60 std::map
<std::string
, MockDnsSdDeviceLister
*> listers_
;
61 // The last delegate used or NULL.
62 DnsSdDelegate
* delegate_
;
65 class MockDnsSdObserver
: public DnsSdRegistry::DnsSdObserver
{
67 MOCK_METHOD2(OnDnsSdEvent
, void(const std::string
&,
68 const DnsSdRegistry::DnsSdServiceList
&));
71 class DnsSdRegistryTest
: public testing::Test
{
73 DnsSdRegistryTest() {}
74 ~DnsSdRegistryTest() override
{}
76 void SetUp() override
{
77 registry_
.reset(new TestDnsSdRegistry());
78 registry_
->AddObserver(&observer_
);
82 scoped_ptr
<TestDnsSdRegistry
> registry_
;
83 MockDnsSdObserver observer_
;
86 // Tests registering 2 listeners and removing one. The device lister should
88 TEST_F(DnsSdRegistryTest
, RegisterUnregisterListeners
) {
89 const std::string service_type
= "_testing._tcp.local";
91 EXPECT_CALL(observer_
, OnDnsSdEvent(service_type
,
92 DnsSdRegistry::DnsSdServiceList())).Times(2);
94 registry_
->RegisterDnsSdListener(service_type
);
95 registry_
->RegisterDnsSdListener(service_type
);
96 registry_
->UnregisterDnsSdListener(service_type
);
97 EXPECT_EQ(1, registry_
->GetServiceListenerCount(service_type
));
99 registry_
->UnregisterDnsSdListener(service_type
);
100 EXPECT_EQ(0, registry_
->GetServiceListenerCount(service_type
));
103 // Tests that verify calls to ForceDiscovery() are propagated to registered
105 TEST_F(DnsSdRegistryTest
, ForceDiscovery
) {
106 const std::string service_type1
= "_testing1._tcp.local";
107 const std::string service_type2
= "_testing2._tcp.local";
109 EXPECT_CALL(observer_
,
110 OnDnsSdEvent(service_type1
, DnsSdRegistry::DnsSdServiceList()));
111 EXPECT_CALL(observer_
,
112 OnDnsSdEvent(service_type2
, DnsSdRegistry::DnsSdServiceList()));
114 registry_
->RegisterDnsSdListener(service_type1
);
115 registry_
->RegisterDnsSdListener(service_type2
);
116 EXPECT_EQ(1, registry_
->GetServiceListenerCount(service_type1
));
117 EXPECT_EQ(1, registry_
->GetServiceListenerCount(service_type2
));
119 EXPECT_CALL(*registry_
->GetLister(service_type1
), Discover(false));
120 EXPECT_CALL(*registry_
->GetLister(service_type2
), Discover(false));
121 registry_
->ForceDiscovery();
124 // Tests registering a listener and receiving an added and updated event.
125 TEST_F(DnsSdRegistryTest
, AddAndUpdate
) {
126 const std::string service_type
= "_testing._tcp.local";
127 const std::string ip_address1
= "192.168.0.100";
128 const std::string ip_address2
= "192.168.0.101";
130 DnsSdService service
;
131 service
.service_name
= "_myDevice." + service_type
;
132 service
.ip_address
= ip_address1
;
134 DnsSdRegistry::DnsSdServiceList service_list
;
136 EXPECT_CALL(observer_
, OnDnsSdEvent(service_type
, service_list
));
138 // Add first service.
139 service_list
.push_back(service
);
140 EXPECT_CALL(observer_
, OnDnsSdEvent(service_type
, service_list
));
142 // Clear services and add same one with different IP address.
143 service_list
.clear();
144 service
.ip_address
= ip_address2
;
145 service_list
.push_back(service
);
146 EXPECT_CALL(observer_
, OnDnsSdEvent(service_type
, service_list
));
148 registry_
->RegisterDnsSdListener(service_type
);
149 service
.ip_address
= ip_address1
;
150 registry_
->GetDelegate()->ServiceChanged(service_type
, true, service
);
151 service
.ip_address
= ip_address2
;
152 registry_
->GetDelegate()->ServiceChanged(service_type
, false, service
);
155 // Tests registering a listener and receiving an added and removed event.
156 TEST_F(DnsSdRegistryTest
, AddAndRemove
) {
157 const std::string service_type
= "_testing._tcp.local";
159 DnsSdService service
;
160 service
.service_name
= "_myDevice." + service_type
;
161 service
.ip_address
= "192.168.0.100";
163 DnsSdRegistry::DnsSdServiceList service_list
;
164 // Expect to be called twice with empty list (once on register, once after
166 EXPECT_CALL(observer_
, OnDnsSdEvent(service_type
, service_list
)).Times(2);
167 service_list
.push_back(service
);
168 // Expect to be called twice with 1 item (once after adding, once after adding
169 // again after removal).
170 EXPECT_CALL(observer_
, OnDnsSdEvent(service_type
, service_list
)).Times(2);
172 registry_
->RegisterDnsSdListener(service_type
);
173 registry_
->GetDelegate()->ServiceChanged(service_type
, true, service
);
174 registry_
->GetDelegate()->ServiceRemoved(service_type
, service
.service_name
);
175 registry_
->GetDelegate()->ServiceChanged(service_type
, true, service
);
178 // Tests adding multiple services.
179 TEST_F(DnsSdRegistryTest
, AddMultipleServices
) {
180 const std::string service_type
= "_testing._tcp.local";
182 DnsSdService service
;
183 service
.service_name
= "_myDevice." + service_type
;
184 service
.ip_address
= "192.168.0.100";
186 DnsSdService service2
;
187 service
.service_name
= "_myDevice2." + service_type
;
188 service
.ip_address
= "192.168.0.101";
190 DnsSdRegistry::DnsSdServiceList service_list
;
191 EXPECT_CALL(observer_
, OnDnsSdEvent(service_type
, service_list
));
192 service_list
.push_back(service
);
193 EXPECT_CALL(observer_
, OnDnsSdEvent(service_type
, service_list
));
194 service_list
.push_back(service2
);
195 EXPECT_CALL(observer_
, OnDnsSdEvent(service_type
, service_list
));
197 registry_
->RegisterDnsSdListener(service_type
);
198 registry_
->GetDelegate()->ServiceChanged(service_type
, true, service
);
199 registry_
->GetDelegate()->ServiceChanged(service_type
, true, service2
);
202 // Tests adding multiple services and handling a flush event.
203 TEST_F(DnsSdRegistryTest
, FlushCache
) {
204 testing::InSequence s
;
205 const std::string service_type
= "_testing._tcp.local";
207 DnsSdService service
;
208 service
.service_name
= "_myDevice." + service_type
;
209 service
.ip_address
= "192.168.0.100";
211 DnsSdService service2
;
212 service
.service_name
= "_myDevice2." + service_type
;
213 service
.ip_address
= "192.168.0.101";
215 DnsSdRegistry::DnsSdServiceList service_list
;
216 EXPECT_CALL(observer_
, OnDnsSdEvent(service_type
, service_list
));
217 service_list
.push_back(service
);
218 EXPECT_CALL(observer_
, OnDnsSdEvent(service_type
, service_list
));
219 service_list
.push_back(service2
);
220 EXPECT_CALL(observer_
, OnDnsSdEvent(service_type
, service_list
));
221 service_list
.clear();
222 EXPECT_CALL(observer_
, OnDnsSdEvent(service_type
, service_list
));
224 registry_
->RegisterDnsSdListener(service_type
);
225 registry_
->GetDelegate()->ServiceChanged(service_type
, true, service
);
226 registry_
->GetDelegate()->ServiceChanged(service_type
, true, service2
);
227 registry_
->GetDelegate()->ServicesFlushed(service_type
);
230 // Tests receiving an update from the DnsSdDelegate that does not change the
231 // service object does not notify the observer.
232 TEST_F(DnsSdRegistryTest
, UpdateOnlyIfChanged
) {
233 const std::string service_type
= "_testing._tcp.local";
234 const std::string ip_address
= "192.168.0.100";
236 DnsSdService service
;
237 service
.service_name
= "_myDevice." + service_type
;
238 service
.ip_address
= ip_address
;
240 DnsSdRegistry::DnsSdServiceList service_list
;
241 EXPECT_CALL(observer_
, OnDnsSdEvent(service_type
, service_list
));
243 // Expect service_list with initial service.
244 service_list
.push_back(service
);
245 EXPECT_CALL(observer_
, OnDnsSdEvent(service_type
, service_list
));
247 // Expect service_list with updated service.
248 service_list
.clear();
249 service
.ip_address
= "192.168.0.101";
250 service_list
.push_back(service
);
251 EXPECT_CALL(observer_
, OnDnsSdEvent(service_type
, service_list
));
252 // No more calls to observer_.
254 registry_
->RegisterDnsSdListener(service_type
);
255 service
.ip_address
= "192.168.0.100";
256 registry_
->GetDelegate()->ServiceChanged(service_type
, true, service
);
257 // Update with changed ip address.
258 service
.ip_address
= "192.168.0.101";
259 registry_
->GetDelegate()->ServiceChanged(service_type
, false, service
);
260 // Update with no changes to the service.
261 registry_
->GetDelegate()->ServiceChanged(service_type
, false, service
);
264 } // namespace extensions