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 "base/command_line.h"
6 #include "chrome/browser/extensions/api/mdns/mdns_api.h"
7 #include "chrome/browser/extensions/extension_apitest.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/common/extensions/api/mdns.h"
10 #include "extensions/common/switches.h"
11 #include "extensions/test/result_catcher.h"
12 #include "testing/gmock/include/gmock/gmock.h"
14 using extensions::DnsSdRegistry
;
18 namespace api
= extensions::api
;
22 class MockDnsSdRegistry
: public DnsSdRegistry
{
24 explicit MockDnsSdRegistry(extensions::MDnsAPI
* api
) : api_(api
) {}
25 virtual ~MockDnsSdRegistry() {}
27 MOCK_METHOD1(AddObserver
, void(DnsSdObserver
* observer
));
28 MOCK_METHOD1(RemoveObserver
, void(DnsSdObserver
* observer
));
29 MOCK_METHOD1(RegisterDnsSdListener
, void(const std::string
& service_type
));
30 MOCK_METHOD1(UnregisterDnsSdListener
, void(const std::string
& service_type
));
31 MOCK_METHOD0(ForceDiscovery
, void(void));
33 void DispatchMDnsEvent(const std::string
& service_type
,
34 const DnsSdServiceList
& services
) {
35 api_
->OnDnsSdEvent(service_type
, services
);
39 extensions::DnsSdRegistry::DnsSdObserver
* api_
;
42 class MDnsAPITest
: public ExtensionApiTest
{
46 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
47 ExtensionApiTest::SetUpCommandLine(command_line
);
48 command_line
->AppendSwitchASCII(
49 extensions::switches::kWhitelistedExtensionID
,
50 "ddchlicdkolnonkihahngkmmmjnjlkkf");
53 void SetUpTestDnsSdRegistry() {
54 extensions::MDnsAPI
* api
= extensions::MDnsAPI::Get(profile());
55 dns_sd_registry_
= new MockDnsSdRegistry(api
);
56 EXPECT_CALL(*dns_sd_registry_
, AddObserver(api
))
58 // Transfers ownership of the registry instance.
59 api
->SetDnsSdRegistryForTesting(
60 make_scoped_ptr
<DnsSdRegistry
>(dns_sd_registry_
).Pass());
64 MockDnsSdRegistry
* dns_sd_registry_
;
69 // TODO(justinlin): Win Dbg has a workaround that makes RunExtensionSubtest
70 // always return true without actually running the test. Remove when fixed.
71 #if defined(OS_WIN) && !defined(NDEBUG)
72 #define MAYBE_RegisterListener DISABLED_RegisterListener
74 #define MAYBE_RegisterListener RegisterListener
76 // Test loading extension, registering an MDNS listener and dispatching events.
77 IN_PROC_BROWSER_TEST_F(MDnsAPITest
, MAYBE_RegisterListener
) {
78 const std::string
& service_type
= "_googlecast._tcp.local";
79 SetUpTestDnsSdRegistry();
81 EXPECT_CALL(*dns_sd_registry_
, RegisterDnsSdListener(service_type
))
83 EXPECT_CALL(*dns_sd_registry_
, UnregisterDnsSdListener(service_type
))
85 EXPECT_CALL(*dns_sd_registry_
,
86 RemoveObserver(A
<extensions::DnsSdRegistry::DnsSdObserver
*>()))
89 EXPECT_TRUE(RunExtensionSubtest("mdns/api", "register_listener.html"))
92 extensions::ResultCatcher catcher
;
93 // Dispatch 3 events, one of which should not be sent to the test extension.
94 DnsSdRegistry::DnsSdServiceList services
;
96 extensions::DnsSdService service
;
97 service
.service_name
= service_type
;
98 services
.push_back(service
);
100 dns_sd_registry_
->DispatchMDnsEvent(service_type
, services
);
101 dns_sd_registry_
->DispatchMDnsEvent("_uninteresting._tcp.local", services
);
102 dns_sd_registry_
->DispatchMDnsEvent(service_type
, services
);
103 EXPECT_TRUE(catcher
.GetNextResult()) << catcher
.message();
106 // TODO(justinlin): Win Dbg has a workaround that makes RunExtensionSubtest
107 // always return true without actually running the test. Remove when fixed.
108 #if defined(OS_WIN) && !defined(NDEBUG)
109 #define MAYBE_ForceDiscovery DISABLED_ForceDiscovery
111 #define MAYBE_ForceDiscovery ForceDiscovery
113 // Test loading extension, registering an MDNS listener and dispatching events.
114 IN_PROC_BROWSER_TEST_F(MDnsAPITest
, MAYBE_ForceDiscovery
) {
115 const std::string
& service_type
= "_googlecast._tcp.local";
116 SetUpTestDnsSdRegistry();
118 EXPECT_CALL(*dns_sd_registry_
, RegisterDnsSdListener(service_type
)).Times(1);
119 EXPECT_CALL(*dns_sd_registry_
, UnregisterDnsSdListener(service_type
))
121 EXPECT_CALL(*dns_sd_registry_
, ForceDiscovery()).Times(1);
122 EXPECT_CALL(*dns_sd_registry_
,
123 RemoveObserver(A
<extensions::DnsSdRegistry::DnsSdObserver
*>()))
126 EXPECT_TRUE(RunExtensionSubtest("mdns/api", "force_discovery.html"))
129 extensions::ResultCatcher catcher
;
130 DnsSdRegistry::DnsSdServiceList services
;
132 extensions::DnsSdService service
;
133 service
.service_name
= service_type
;
134 services
.push_back(service
);
136 dns_sd_registry_
->DispatchMDnsEvent(service_type
, services
);
137 EXPECT_TRUE(catcher
.GetNextResult()) << catcher
.message();
140 // TODO(justinlin): Win Dbg has a workaround that makes RunExtensionSubtest
141 // always return true without actually running the test. Remove when fixed.
142 #if defined(OS_WIN) && !defined(NDEBUG)
143 #define MAYBE_RegisterMultipleListeners DISABLED_RegisterMultipleListeners
145 #define MAYBE_RegisterMultipleListeners RegisterMultipleListeners
147 // Test loading extension and registering multiple listeners.
148 IN_PROC_BROWSER_TEST_F(MDnsAPITest
, MAYBE_RegisterMultipleListeners
) {
149 const std::string
& service_type
= "_googlecast._tcp.local";
150 const std::string
& test_service_type
= "_testing._tcp.local";
151 SetUpTestDnsSdRegistry();
153 EXPECT_CALL(*dns_sd_registry_
, RegisterDnsSdListener(service_type
))
155 EXPECT_CALL(*dns_sd_registry_
, UnregisterDnsSdListener(service_type
))
157 EXPECT_CALL(*dns_sd_registry_
, RegisterDnsSdListener(test_service_type
))
159 EXPECT_CALL(*dns_sd_registry_
, UnregisterDnsSdListener(test_service_type
))
161 EXPECT_CALL(*dns_sd_registry_
,
162 RemoveObserver(A
<extensions::DnsSdRegistry::DnsSdObserver
*>()))
165 EXPECT_TRUE(RunExtensionSubtest("mdns/api",
166 "register_multiple_listeners.html"))
169 extensions::ResultCatcher catcher
;
170 DnsSdRegistry::DnsSdServiceList services
;
172 extensions::DnsSdService service
;
173 service
.service_name
= service_type
;
174 services
.push_back(service
);
176 dns_sd_registry_
->DispatchMDnsEvent(service_type
, services
);
177 dns_sd_registry_
->DispatchMDnsEvent(test_service_type
, services
);
178 EXPECT_TRUE(catcher
.GetNextResult()) << catcher
.message();
181 // TODO(justinlin): Win Dbg has a workaround that makes RunExtensionSubtest
182 // always return true without actually running the test. Remove when fixed.
183 #if defined(OS_WIN) && !defined(NDEBUG)
184 #define MAYBE_RegisterTooManyListeners DISABLED_RegisterTooManyListeners
186 #define MAYBE_RegisterTooManyListeners RegisterTooManyListeners
188 // Test loading extension and registering multiple listeners.
189 IN_PROC_BROWSER_TEST_F(MDnsAPITest
, MAYBE_RegisterTooManyListeners
) {
190 SetUpTestDnsSdRegistry();
192 EXPECT_CALL(*dns_sd_registry_
, RegisterDnsSdListener(_
)).Times(10);
193 EXPECT_CALL(*dns_sd_registry_
, UnregisterDnsSdListener(_
)).Times(10);
194 EXPECT_CALL(*dns_sd_registry_
,
195 RemoveObserver(A
<extensions::DnsSdRegistry::DnsSdObserver
*>()))
198 EXPECT_TRUE(RunPlatformAppTest("mdns/api-packaged-apps"))
202 // TODO(justinlin): Win Dbg has a workaround that makes RunExtensionSubtest
203 // always return true without actually running the test. Remove when fixed.
204 #if defined(OS_WIN) && !defined(NDEBUG)
205 #define MAYBE_MaxServiceInstancesPerEventConst \
206 DISABLED_MaxServiceInstancesPerEventConst
208 #define MAYBE_MaxServiceInstancesPerEventConst MaxServiceInstancesPerEventConst
210 // Test loading extension and registering multiple listeners.
211 IN_PROC_BROWSER_TEST_F(MDnsAPITest
, MAYBE_MaxServiceInstancesPerEventConst
) {
212 EXPECT_TRUE(RunExtensionSubtest("mdns/api",
213 "get_max_service_instances.html"));