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

Side by Side Diff: chrome/browser/extensions/api/mdns/mdns_api_unittest.cc

Issue 2874243003: [mDns] Make DnsSdRegistry a leaky singleton (Closed)
Patch Set: Created 3 years, 7 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/extensions/api/mdns/mdns_api.h" 5 #include "chrome/browser/extensions/api/mdns/mdns_api.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 87
88 // For ExtensionService interface when it requires a path that is not used. 88 // For ExtensionService interface when it requires a path that is not used.
89 base::FilePath bogus_file_pathname(const std::string& name) { 89 base::FilePath bogus_file_pathname(const std::string& name) {
90 return base::FilePath(FILE_PATH_LITERAL("//foobar_nonexistent")) 90 return base::FilePath(FILE_PATH_LITERAL("//foobar_nonexistent"))
91 .AppendASCII(name); 91 .AppendASCII(name);
92 } 92 }
93 93
94 class MockDnsSdRegistry : public media_router::DnsSdRegistry { 94 class MockDnsSdRegistry : public media_router::DnsSdRegistry {
95 public: 95 public:
96 explicit MockDnsSdRegistry(extensions::MDnsAPI* api) : api_(api) {} 96 explicit MockDnsSdRegistry(extensions::MDnsAPI* api) : api_(api) {}
97 virtual ~MockDnsSdRegistry() {} 97
98 virtual ~MockDnsSdRegistry() {
99 RemoveObserver(api_);
100 api_->SetDnsSdRegistryForTesting(nullptr);
101 }
98 102
99 MOCK_METHOD1(AddObserver, void(DnsSdObserver* observer)); 103 MOCK_METHOD1(AddObserver, void(DnsSdObserver* observer));
100 MOCK_METHOD1(RemoveObserver, void(DnsSdObserver* observer)); 104 MOCK_METHOD1(RemoveObserver, void(DnsSdObserver* observer));
101 MOCK_METHOD1(RegisterDnsSdListener, void(const std::string& service_type)); 105 MOCK_METHOD1(RegisterDnsSdListener, void(const std::string& service_type));
102 MOCK_METHOD1(UnregisterDnsSdListener, void(const std::string& service_type)); 106 MOCK_METHOD1(UnregisterDnsSdListener, void(const std::string& service_type));
103 MOCK_METHOD1(Publish, void(const std::string&)); 107 MOCK_METHOD1(Publish, void(const std::string&));
104 MOCK_METHOD0(ForceDiscovery, void(void)); 108 MOCK_METHOD0(ForceDiscovery, void(void));
105 109
106 void DispatchMDnsEvent(const std::string& service_type, 110 void DispatchMDnsEvent(const std::string& service_type,
107 const DnsSdServiceList& services) { 111 const DnsSdServiceList& services) {
108 api_->OnDnsSdEvent(service_type, services); 112 auto* observer =
mark a. foltz 2017/05/11 23:22:24 MdnsAPI implements the DnsSdObserver interface. I
zhaobin 2017/05/12 17:35:00 Code removed.
113 static_cast<media_router::DnsSdRegistry::DnsSdObserver*>(api_);
114 observer->OnDnsSdEvent(service_type, services);
109 } 115 }
110 116
111 private: 117 private:
112 media_router::DnsSdRegistry::DnsSdObserver* api_; 118 extensions::MDnsAPI* api_;
113 }; 119 };
114 120
115 class MockEventRouter : public EventRouter { 121 class MockEventRouter : public EventRouter {
116 public: 122 public:
117 explicit MockEventRouter(content::BrowserContext* browser_context, 123 explicit MockEventRouter(content::BrowserContext* browser_context,
118 ExtensionPrefs* extension_prefs) 124 ExtensionPrefs* extension_prefs)
119 : EventRouter(browser_context, extension_prefs) {} 125 : EventRouter(browser_context, extension_prefs) {}
120 virtual ~MockEventRouter() {} 126 virtual ~MockEventRouter() {}
121 127
122 virtual void BroadcastEvent(std::unique_ptr<Event> event) { 128 virtual void BroadcastEvent(std::unique_ptr<Event> event) {
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 MDnsAPI::GetFactoryInstance()->SetTestingFactory(browser_context(), 203 MDnsAPI::GetFactoryInstance()->SetTestingFactory(browser_context(),
198 GetMDnsFactory()); 204 GetMDnsFactory());
199 205
200 EventRouterFactory::GetInstance()->SetTestingFactory(browser_context(), 206 EventRouterFactory::GetInstance()->SetTestingFactory(browser_context(),
201 &BuildEventRouter); 207 &BuildEventRouter);
202 208
203 // Do some sanity checking 209 // Do some sanity checking
204 ASSERT_TRUE(MDnsAPI::Get(browser_context())); // constructs MDnsAPI 210 ASSERT_TRUE(MDnsAPI::Get(browser_context())); // constructs MDnsAPI
205 ASSERT_TRUE(EventRouter::Get(browser_context())); // constructs EventRouter 211 ASSERT_TRUE(EventRouter::Get(browser_context())); // constructs EventRouter
206 212
207 registry_ = new MockDnsSdRegistry(MDnsAPI::Get(browser_context())); 213 registry_ =
214 base::MakeUnique<MockDnsSdRegistry>(MDnsAPI::Get(browser_context()));
208 EXPECT_CALL(*dns_sd_registry(), 215 EXPECT_CALL(*dns_sd_registry(),
209 AddObserver(MDnsAPI::Get(browser_context()))) 216 AddObserver(MDnsAPI::Get(browser_context())))
210 .Times(1); 217 .Times(1);
211 MDnsAPI::Get(browser_context()) 218 MDnsAPI::Get(browser_context())
212 ->SetDnsSdRegistryForTesting( 219 ->SetDnsSdRegistryForTesting(registry_.get());
213 std::unique_ptr<media_router::DnsSdRegistry>(registry_));
214 220
215 render_process_host_.reset( 221 render_process_host_.reset(
216 new content::MockRenderProcessHost(browser_context())); 222 new content::MockRenderProcessHost(browser_context()));
217 } 223 }
218 224
219 // Returns the mDNS API factory function (mock vs. real) to use for the test. 225 // Returns the mDNS API factory function (mock vs. real) to use for the test.
220 virtual BrowserContextKeyedServiceFactory::TestingFactoryFunction 226 virtual BrowserContextKeyedServiceFactory::TestingFactoryFunction
221 GetMDnsFactory() { 227 GetMDnsFactory() {
222 return MDnsAPITestingFactoryFunction; 228 return MDnsAPITestingFactoryFunction;
223 } 229 }
224 230
225 void TearDown() override { 231 void TearDown() override {
226 EXPECT_CALL(*dns_sd_registry(), 232 EXPECT_CALL(*dns_sd_registry(),
227 RemoveObserver(MDnsAPI::Get(browser_context()))) 233 RemoveObserver(MDnsAPI::Get(browser_context())))
228 .Times(1); 234 .Times(1);
229 render_process_host_.reset(); 235 render_process_host_.reset();
230 extensions::ExtensionServiceTestBase::TearDown(); 236 extensions::ExtensionServiceTestBase::TearDown();
231 } 237 }
232 238
233 virtual MockDnsSdRegistry* dns_sd_registry() { 239 virtual MockDnsSdRegistry* dns_sd_registry() { return registry_.get(); }
234 return registry_;
235 }
236 240
237 // Constructs an extension according to the parameters that matter most to 241 // Constructs an extension according to the parameters that matter most to
238 // MDnsAPI the local unit tests. 242 // MDnsAPI the local unit tests.
239 const scoped_refptr<extensions::Extension> CreateExtension( 243 const scoped_refptr<extensions::Extension> CreateExtension(
240 std::string name, 244 std::string name,
241 bool is_platform_app, 245 bool is_platform_app,
242 std::string extension_id) { 246 std::string extension_id) {
243 base::DictionaryValue manifest; 247 base::DictionaryValue manifest;
244 manifest.SetString(extensions::manifest_keys::kVersion, "1.0.0.0"); 248 manifest.SetString(extensions::manifest_keys::kVersion, "1.0.0.0");
245 manifest.SetString(extensions::manifest_keys::kName, name); 249 manifest.SetString(extensions::manifest_keys::kName, name);
(...skipping 12 matching lines...) Expand all
258 Extension::NO_FLAGS, 262 Extension::NO_FLAGS,
259 extension_id, 263 extension_id,
260 &error); 264 &error);
261 } 265 }
262 266
263 content::RenderProcessHost* render_process_host() const { 267 content::RenderProcessHost* render_process_host() const {
264 return render_process_host_.get(); 268 return render_process_host_.get();
265 } 269 }
266 270
267 private: 271 private:
268 // The registry is owned by MDnsAPI, but MDnsAPI does not have an accessor 272 std::unique_ptr<MockDnsSdRegistry> registry_;
269 // for it, so use a private member.
270 MockDnsSdRegistry* registry_;
271 273
272 std::unique_ptr<content::RenderProcessHost> render_process_host_; 274 std::unique_ptr<content::RenderProcessHost> render_process_host_;
273 }; 275 };
274 276
275 class MDnsAPIMaxServicesTest : public MDnsAPITest { 277 class MDnsAPIMaxServicesTest : public MDnsAPITest {
276 public: 278 public:
277 MockEventRouter* event_router() { 279 MockEventRouter* event_router() {
278 return static_cast<MockEventRouter*>(EventRouter::Get(browser_context())); 280 return static_cast<MockEventRouter*>(EventRouter::Get(browser_context()));
279 } 281 }
280 }; 282 };
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 render_process_host(), kExtId, filter, false); 440 render_process_host(), kExtId, filter, false);
439 441
440 EXPECT_CALL(*dns_sd_registry(), UnregisterDnsSdListener("_trex._tcp.local")); 442 EXPECT_CALL(*dns_sd_registry(), UnregisterDnsSdListener("_trex._tcp.local"));
441 EventRouter::Get(browser_context()) 443 EventRouter::Get(browser_context())
442 ->RemoveFilteredEventListener(api::mdns::OnServiceList::kEventName, 444 ->RemoveFilteredEventListener(api::mdns::OnServiceList::kEventName,
443 render_process_host(), kExtId, filter, 445 render_process_host(), kExtId, filter,
444 false); 446 false);
445 } 447 }
446 448
447 } // namespace extensions 449 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698