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

Side by Side Diff: chrome/browser/custom_handlers/protocol_handler_registry_unittest.cc

Issue 648653003: Standardize usage of virtual/override/final in chrome/browser/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 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 (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 "chrome/browser/custom_handlers/protocol_handler_registry.h" 5 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
6 6
7 #include <set> 7 #include <set>
8 8
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 base::MessageLoop::current()->RunUntilIdle(); 56 base::MessageLoop::current()->RunUntilIdle();
57 } 57 }
58 58
59 // FakeURLRequestJobFactory returns NULL for all job creation requests and false 59 // FakeURLRequestJobFactory returns NULL for all job creation requests and false
60 // for all IsHandled*() requests. FakeURLRequestJobFactory can be chained to 60 // for all IsHandled*() requests. FakeURLRequestJobFactory can be chained to
61 // ProtocolHandlerRegistry::JobInterceptorFactory so the result of 61 // ProtocolHandlerRegistry::JobInterceptorFactory so the result of
62 // MaybeCreateJobWithProtocolHandler() indicates whether the 62 // MaybeCreateJobWithProtocolHandler() indicates whether the
63 // ProtocolHandlerRegistry properly handled a job creation request. 63 // ProtocolHandlerRegistry properly handled a job creation request.
64 class FakeURLRequestJobFactory : public net::URLRequestJobFactory { 64 class FakeURLRequestJobFactory : public net::URLRequestJobFactory {
65 // net::URLRequestJobFactory implementation: 65 // net::URLRequestJobFactory implementation:
66 virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler( 66 net::URLRequestJob* MaybeCreateJobWithProtocolHandler(
67 const std::string& scheme, 67 const std::string& scheme,
68 net::URLRequest* request, 68 net::URLRequest* request,
69 net::NetworkDelegate* network_delegate) const override { 69 net::NetworkDelegate* network_delegate) const override {
70 return NULL; 70 return NULL;
71 } 71 }
72 virtual bool IsHandledProtocol(const std::string& scheme) const override { 72 bool IsHandledProtocol(const std::string& scheme) const override {
73 return false; 73 return false;
74 } 74 }
75 virtual bool IsHandledURL(const GURL& url) const override { 75 bool IsHandledURL(const GURL& url) const override { return false; }
76 return false; 76 bool IsSafeRedirectTarget(const GURL& location) const override {
77 }
78 virtual bool IsSafeRedirectTarget(const GURL& location) const override {
79 return true; 77 return true;
80 } 78 }
81 }; 79 };
82 80
83 void AssertWillHandleIO( 81 void AssertWillHandleIO(
84 const std::string& scheme, 82 const std::string& scheme,
85 bool expected, 83 bool expected,
86 ProtocolHandlerRegistry::JobInterceptorFactory* interceptor) { 84 ProtocolHandlerRegistry::JobInterceptorFactory* interceptor) {
87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
88 interceptor->Chain(scoped_ptr<net::URLRequestJobFactory>( 86 interceptor->Chain(scoped_ptr<net::URLRequestJobFactory>(
(...skipping 28 matching lines...) Expand all
117 std::string url, 115 std::string url,
118 bool is_default) { 116 bool is_default) {
119 base::DictionaryValue* value = GetProtocolHandlerValue(protocol, url); 117 base::DictionaryValue* value = GetProtocolHandlerValue(protocol, url);
120 value->SetBoolean("default", is_default); 118 value->SetBoolean("default", is_default);
121 return value; 119 return value;
122 } 120 }
123 121
124 class FakeDelegate : public ProtocolHandlerRegistry::Delegate { 122 class FakeDelegate : public ProtocolHandlerRegistry::Delegate {
125 public: 123 public:
126 FakeDelegate() : force_os_failure_(false) {} 124 FakeDelegate() : force_os_failure_(false) {}
127 virtual ~FakeDelegate() { } 125 ~FakeDelegate() override {}
128 virtual void RegisterExternalHandler(const std::string& protocol) override { 126 void RegisterExternalHandler(const std::string& protocol) override {
129 ASSERT_TRUE( 127 ASSERT_TRUE(
130 registered_protocols_.find(protocol) == registered_protocols_.end()); 128 registered_protocols_.find(protocol) == registered_protocols_.end());
131 registered_protocols_.insert(protocol); 129 registered_protocols_.insert(protocol);
132 } 130 }
133 131
134 virtual void DeregisterExternalHandler(const std::string& protocol) override { 132 void DeregisterExternalHandler(const std::string& protocol) override {
135 registered_protocols_.erase(protocol); 133 registered_protocols_.erase(protocol);
136 } 134 }
137 135
138 virtual ShellIntegration::DefaultProtocolClientWorker* CreateShellWorker( 136 ShellIntegration::DefaultProtocolClientWorker* CreateShellWorker(
139 ShellIntegration::DefaultWebClientObserver* observer, 137 ShellIntegration::DefaultWebClientObserver* observer,
140 const std::string& protocol) override; 138 const std::string& protocol) override;
141 139
142 virtual ProtocolHandlerRegistry::DefaultClientObserver* CreateShellObserver( 140 ProtocolHandlerRegistry::DefaultClientObserver* CreateShellObserver(
143 ProtocolHandlerRegistry* registry) override; 141 ProtocolHandlerRegistry* registry) override;
144 142
145 virtual void RegisterWithOSAsDefaultClient( 143 void RegisterWithOSAsDefaultClient(const std::string& protocol,
146 const std::string& protocol, 144 ProtocolHandlerRegistry* reg) override {
147 ProtocolHandlerRegistry* reg) override {
148 ProtocolHandlerRegistry::Delegate::RegisterWithOSAsDefaultClient(protocol, 145 ProtocolHandlerRegistry::Delegate::RegisterWithOSAsDefaultClient(protocol,
149 reg); 146 reg);
150 ASSERT_FALSE(IsFakeRegisteredWithOS(protocol)); 147 ASSERT_FALSE(IsFakeRegisteredWithOS(protocol));
151 } 148 }
152 149
153 virtual bool IsExternalHandlerRegistered( 150 bool IsExternalHandlerRegistered(const std::string& protocol) override {
154 const std::string& protocol) override {
155 return registered_protocols_.find(protocol) != registered_protocols_.end(); 151 return registered_protocols_.find(protocol) != registered_protocols_.end();
156 } 152 }
157 153
158 bool IsFakeRegisteredWithOS(const std::string& protocol) { 154 bool IsFakeRegisteredWithOS(const std::string& protocol) {
159 return os_registered_protocols_.find(protocol) != 155 return os_registered_protocols_.find(protocol) !=
160 os_registered_protocols_.end(); 156 os_registered_protocols_.end();
161 } 157 }
162 158
163 void FakeRegisterWithOS(const std::string& protocol) { 159 void FakeRegisterWithOS(const std::string& protocol) {
164 os_registered_protocols_.insert(protocol); 160 os_registered_protocols_.insert(protocol);
(...skipping 16 matching lines...) Expand all
181 }; 177 };
182 178
183 class FakeClientObserver 179 class FakeClientObserver
184 : public ProtocolHandlerRegistry::DefaultClientObserver { 180 : public ProtocolHandlerRegistry::DefaultClientObserver {
185 public: 181 public:
186 FakeClientObserver(ProtocolHandlerRegistry* registry, 182 FakeClientObserver(ProtocolHandlerRegistry* registry,
187 FakeDelegate* registry_delegate) 183 FakeDelegate* registry_delegate)
188 : ProtocolHandlerRegistry::DefaultClientObserver(registry), 184 : ProtocolHandlerRegistry::DefaultClientObserver(registry),
189 delegate_(registry_delegate) {} 185 delegate_(registry_delegate) {}
190 186
191 virtual void SetDefaultWebClientUIState( 187 void SetDefaultWebClientUIState(
192 ShellIntegration::DefaultWebClientUIState state) override { 188 ShellIntegration::DefaultWebClientUIState state) override {
193 ProtocolHandlerRegistry::DefaultClientObserver::SetDefaultWebClientUIState( 189 ProtocolHandlerRegistry::DefaultClientObserver::SetDefaultWebClientUIState(
194 state); 190 state);
195 if (state == ShellIntegration::STATE_IS_DEFAULT) { 191 if (state == ShellIntegration::STATE_IS_DEFAULT) {
196 delegate_->FakeRegisterWithOS(worker_->protocol()); 192 delegate_->FakeRegisterWithOS(worker_->protocol());
197 } 193 }
198 if (state != ShellIntegration::STATE_PROCESSING) { 194 if (state != ShellIntegration::STATE_PROCESSING) {
199 base::MessageLoop::current()->Quit(); 195 base::MessageLoop::current()->Quit();
200 } 196 }
201 } 197 }
202 198
203 private: 199 private:
204 FakeDelegate* delegate_; 200 FakeDelegate* delegate_;
205 }; 201 };
206 202
207 class FakeProtocolClientWorker 203 class FakeProtocolClientWorker
208 : public ShellIntegration::DefaultProtocolClientWorker { 204 : public ShellIntegration::DefaultProtocolClientWorker {
209 public: 205 public:
210 FakeProtocolClientWorker(ShellIntegration::DefaultWebClientObserver* observer, 206 FakeProtocolClientWorker(ShellIntegration::DefaultWebClientObserver* observer,
211 const std::string& protocol, 207 const std::string& protocol,
212 bool force_failure) 208 bool force_failure)
213 : ShellIntegration::DefaultProtocolClientWorker(observer, protocol), 209 : ShellIntegration::DefaultProtocolClientWorker(observer, protocol),
214 force_failure_(force_failure) {} 210 force_failure_(force_failure) {}
215 211
216 private: 212 private:
217 virtual ~FakeProtocolClientWorker() {} 213 ~FakeProtocolClientWorker() override {}
218 214
219 virtual ShellIntegration::DefaultWebClientState CheckIsDefault() override { 215 ShellIntegration::DefaultWebClientState CheckIsDefault() override {
220 if (force_failure_) { 216 if (force_failure_) {
221 return ShellIntegration::NOT_DEFAULT; 217 return ShellIntegration::NOT_DEFAULT;
222 } else { 218 } else {
223 return ShellIntegration::IS_DEFAULT; 219 return ShellIntegration::IS_DEFAULT;
224 } 220 }
225 } 221 }
226 222
227 virtual bool SetAsDefault(bool interactive_permitted) override { 223 bool SetAsDefault(bool interactive_permitted) override { return true; }
228 return true;
229 }
230 224
231 private: 225 private:
232 bool force_failure_; 226 bool force_failure_;
233 }; 227 };
234 228
235 ProtocolHandlerRegistry::DefaultClientObserver* 229 ProtocolHandlerRegistry::DefaultClientObserver*
236 FakeDelegate::CreateShellObserver(ProtocolHandlerRegistry* registry) { 230 FakeDelegate::CreateShellObserver(ProtocolHandlerRegistry* registry) {
237 return new FakeClientObserver(registry, this); 231 return new FakeClientObserver(registry, this);
238 } 232 }
239 233
240 ShellIntegration::DefaultProtocolClientWorker* FakeDelegate::CreateShellWorker( 234 ShellIntegration::DefaultProtocolClientWorker* FakeDelegate::CreateShellWorker(
241 ShellIntegration::DefaultWebClientObserver* observer, 235 ShellIntegration::DefaultWebClientObserver* observer,
242 const std::string& protocol) { 236 const std::string& protocol) {
243 return new FakeProtocolClientWorker(observer, protocol, force_os_failure_); 237 return new FakeProtocolClientWorker(observer, protocol, force_os_failure_);
244 } 238 }
245 239
246 class NotificationCounter : public content::NotificationObserver { 240 class NotificationCounter : public content::NotificationObserver {
247 public: 241 public:
248 explicit NotificationCounter(content::BrowserContext* context) 242 explicit NotificationCounter(content::BrowserContext* context)
249 : events_(0), 243 : events_(0),
250 notification_registrar_() { 244 notification_registrar_() {
251 notification_registrar_.Add(this, 245 notification_registrar_.Add(this,
252 chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED, 246 chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED,
253 content::Source<content::BrowserContext>(context)); 247 content::Source<content::BrowserContext>(context));
254 } 248 }
255 249
256 int events() { return events_; } 250 int events() { return events_; }
257 bool notified() { return events_ > 0; } 251 bool notified() { return events_ > 0; }
258 void Clear() { events_ = 0; } 252 void Clear() { events_ = 0; }
259 virtual void Observe(int type, 253 void Observe(int type,
260 const content::NotificationSource& source, 254 const content::NotificationSource& source,
261 const content::NotificationDetails& details) override { 255 const content::NotificationDetails& details) override {
262 ++events_; 256 ++events_;
263 } 257 }
264 258
265 int events_; 259 int events_;
266 content::NotificationRegistrar notification_registrar_; 260 content::NotificationRegistrar notification_registrar_;
267 }; 261 };
268 262
269 class QueryProtocolHandlerOnChange 263 class QueryProtocolHandlerOnChange
270 : public content::NotificationObserver { 264 : public content::NotificationObserver {
271 public: 265 public:
272 QueryProtocolHandlerOnChange(content::BrowserContext* context, 266 QueryProtocolHandlerOnChange(content::BrowserContext* context,
273 ProtocolHandlerRegistry* registry) 267 ProtocolHandlerRegistry* registry)
274 : local_registry_(registry), 268 : local_registry_(registry),
275 called_(false), 269 called_(false),
276 notification_registrar_() { 270 notification_registrar_() {
277 notification_registrar_.Add(this, 271 notification_registrar_.Add(this,
278 chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED, 272 chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED,
279 content::Source<content::BrowserContext>(context)); 273 content::Source<content::BrowserContext>(context));
280 } 274 }
281 275
282 virtual void Observe(int type, 276 void Observe(int type,
283 const content::NotificationSource& source, 277 const content::NotificationSource& source,
284 const content::NotificationDetails& details) override { 278 const content::NotificationDetails& details) override {
285 std::vector<std::string> output; 279 std::vector<std::string> output;
286 local_registry_->GetRegisteredProtocols(&output); 280 local_registry_->GetRegisteredProtocols(&output);
287 called_ = true; 281 called_ = true;
288 } 282 }
289 283
290 ProtocolHandlerRegistry* local_registry_; 284 ProtocolHandlerRegistry* local_registry_;
291 bool called_; 285 bool called_;
292 content::NotificationRegistrar notification_registrar_; 286 content::NotificationRegistrar notification_registrar_;
293 }; 287 };
294 288
295 // URLRequest DCHECKS that the current MessageLoop is IO. It does this because 289 // URLRequest DCHECKS that the current MessageLoop is IO. It does this because
296 // it can't check the thread id (since net can't depend on content.) We want 290 // it can't check the thread id (since net can't depend on content.) We want
297 // to harness our tests so all threads use the same loop allowing us to 291 // to harness our tests so all threads use the same loop allowing us to
298 // guarantee all messages are processed.) By overriding the IsType method 292 // guarantee all messages are processed.) By overriding the IsType method
299 // we basically ignore the supplied message loop type, and instead infer 293 // we basically ignore the supplied message loop type, and instead infer
300 // our type based on the current thread. GO DEPENDENCY INJECTION! 294 // our type based on the current thread. GO DEPENDENCY INJECTION!
301 class TestMessageLoop : public base::MessageLoop { 295 class TestMessageLoop : public base::MessageLoop {
302 public: 296 public:
303 TestMessageLoop() {} 297 TestMessageLoop() {}
304 virtual ~TestMessageLoop() {} 298 ~TestMessageLoop() override {}
305 virtual bool IsType(base::MessageLoop::Type type) const override { 299 bool IsType(base::MessageLoop::Type type) const override {
306 switch (type) { 300 switch (type) {
307 case base::MessageLoop::TYPE_UI: 301 case base::MessageLoop::TYPE_UI:
308 return BrowserThread::CurrentlyOn(BrowserThread::UI); 302 return BrowserThread::CurrentlyOn(BrowserThread::UI);
309 case base::MessageLoop::TYPE_IO: 303 case base::MessageLoop::TYPE_IO:
310 return BrowserThread::CurrentlyOn(BrowserThread::IO); 304 return BrowserThread::CurrentlyOn(BrowserThread::IO);
311 #if defined(OS_ANDROID) 305 #if defined(OS_ANDROID)
312 case base::MessageLoop::TYPE_JAVA: // fall-through 306 case base::MessageLoop::TYPE_JAVA: // fall-through
313 #endif // defined(OS_ANDROID) 307 #endif // defined(OS_ANDROID)
314 case base::MessageLoop::TYPE_CUSTOM: 308 case base::MessageLoop::TYPE_CUSTOM:
315 case base::MessageLoop::TYPE_DEFAULT: 309 case base::MessageLoop::TYPE_DEFAULT:
(...skipping 800 matching lines...) Expand 10 before | Expand all | Expand 10 after
1116 // added to pref. 1110 // added to pref.
1117 ASSERT_EQ(InPrefIgnoredHandlerCount(), 2); 1111 ASSERT_EQ(InPrefIgnoredHandlerCount(), 2);
1118 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 4); 1112 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 4);
1119 1113
1120 registry()->RemoveIgnoredHandler(p2u1); 1114 registry()->RemoveIgnoredHandler(p2u1);
1121 1115
1122 // p2u1 installed by user and policy, so it is removed from pref alone. 1116 // p2u1 installed by user and policy, so it is removed from pref alone.
1123 ASSERT_EQ(InPrefIgnoredHandlerCount(), 1); 1117 ASSERT_EQ(InPrefIgnoredHandlerCount(), 1);
1124 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 4); 1118 ASSERT_EQ(InMemoryIgnoredHandlerCount(), 4);
1125 } 1119 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698