Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/custom_handlers/protocol_handler_registry.h" |
| 6 | |
| 7 | #include <set> |
| 8 | |
| 9 | #include "base/memory/scoped_ptr.h" |
Ben Murdoch | 9ab5563 | 2013-07-18 11:57:30 +0100 | [diff] [blame] | 10 | #include "base/message_loop/message_loop.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 11 | #include "base/strings/utf_string_conversions.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 12 | #include "base/synchronization/waitable_event.h" |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 13 | #include "chrome/browser/chrome_notification_types.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 14 | #include "chrome/browser/prefs/pref_service_syncable.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 15 | #include "chrome/common/custom_handlers/protocol_handler.h" |
| 16 | #include "chrome/test/base/testing_browser_process.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 17 | #include "chrome/test/base/testing_profile.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 18 | #include "components/user_prefs/pref_registry_syncable.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 19 | #include "content/public/browser/notification_observer.h" |
| 20 | #include "content/public/browser/notification_registrar.h" |
| 21 | #include "content/public/browser/notification_source.h" |
| 22 | #include "content/public/test/test_browser_thread.h" |
| 23 | #include "content/public/test/test_renderer_host.h" |
| 24 | #include "net/url_request/url_request.h" |
| 25 | #include "net/url_request/url_request_context.h" |
| 26 | #include "testing/gtest/include/gtest/gtest.h" |
| 27 | |
| 28 | using content::BrowserThread; |
| 29 | |
| 30 | namespace { |
| 31 | |
| 32 | void AssertInterceptedIO( |
| 33 | const GURL& url, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 34 | net::URLRequestJobFactory* interceptor) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 35 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 36 | net::URLRequestContext context; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 37 | net::NetworkDelegate *network_delegate = NULL; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 38 | net::URLRequest request(url, NULL, &context); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 39 | scoped_refptr<net::URLRequestJob> job = |
| 40 | interceptor->MaybeCreateJobWithProtocolHandler( |
| 41 | url.scheme(), &request, network_delegate); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 42 | ASSERT_TRUE(job.get() != NULL); |
| 43 | } |
| 44 | |
| 45 | void AssertIntercepted( |
| 46 | const GURL& url, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 47 | net::URLRequestJobFactory* interceptor) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 48 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 49 | BrowserThread::PostTask(BrowserThread::IO, |
| 50 | FROM_HERE, |
| 51 | base::Bind(AssertInterceptedIO, |
| 52 | url, |
| 53 | base::Unretained(interceptor))); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 54 | base::MessageLoop::current()->RunUntilIdle(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 57 | // FakeURLRequestJobFactory returns NULL for all job creation requests and false |
| 58 | // for all IsHandled*() requests. FakeURLRequestJobFactory can be chained to |
| 59 | // ProtocolHandlerRegistry::JobInterceptorFactory so the result of |
| 60 | // MaybeCreateJobWithProtocolHandler() indicates whether the |
| 61 | // ProtocolHandlerRegistry properly handled a job creation request. |
| 62 | class FakeURLRequestJobFactory : public net::URLRequestJobFactory { |
| 63 | // net::URLRequestJobFactory implementation: |
| 64 | virtual net::URLRequestJob* MaybeCreateJobWithProtocolHandler( |
| 65 | const std::string& scheme, |
| 66 | net::URLRequest* request, |
| 67 | net::NetworkDelegate* network_delegate) const OVERRIDE { |
| 68 | return NULL; |
| 69 | } |
| 70 | virtual bool IsHandledProtocol(const std::string& scheme) const OVERRIDE { |
| 71 | return false; |
| 72 | } |
| 73 | virtual bool IsHandledURL(const GURL& url) const OVERRIDE { |
| 74 | return false; |
| 75 | } |
Torne (Richard Coles) | b2df76e | 2013-05-13 16:52:09 +0100 | [diff] [blame] | 76 | virtual bool IsSafeRedirectTarget(const GURL& location) const OVERRIDE { |
| 77 | return true; |
| 78 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 81 | void AssertWillHandleIO( |
| 82 | const std::string& scheme, |
| 83 | bool expected, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 84 | ProtocolHandlerRegistry::JobInterceptorFactory* interceptor) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 85 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 86 | interceptor->Chain(scoped_ptr<net::URLRequestJobFactory>( |
| 87 | new FakeURLRequestJobFactory())); |
| 88 | ASSERT_EQ(expected, interceptor->IsHandledProtocol(scheme)); |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 89 | interceptor->Chain(scoped_ptr<net::URLRequestJobFactory>()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void AssertWillHandle( |
| 93 | const std::string& scheme, |
| 94 | bool expected, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 95 | ProtocolHandlerRegistry::JobInterceptorFactory* interceptor) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 96 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 97 | BrowserThread::PostTask(BrowserThread::IO, |
| 98 | FROM_HERE, |
| 99 | base::Bind(AssertWillHandleIO, |
| 100 | scheme, |
| 101 | expected, |
| 102 | base::Unretained(interceptor))); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 103 | base::MessageLoop::current()->RunUntilIdle(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | class FakeDelegate : public ProtocolHandlerRegistry::Delegate { |
| 107 | public: |
| 108 | FakeDelegate() : force_os_failure_(false) {} |
| 109 | virtual ~FakeDelegate() { } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 110 | virtual void RegisterExternalHandler(const std::string& protocol) OVERRIDE { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 111 | ASSERT_TRUE( |
| 112 | registered_protocols_.find(protocol) == registered_protocols_.end()); |
| 113 | registered_protocols_.insert(protocol); |
| 114 | } |
| 115 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 116 | virtual void DeregisterExternalHandler(const std::string& protocol) OVERRIDE { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 117 | registered_protocols_.erase(protocol); |
| 118 | } |
| 119 | |
| 120 | virtual ShellIntegration::DefaultProtocolClientWorker* CreateShellWorker( |
| 121 | ShellIntegration::DefaultWebClientObserver* observer, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 122 | const std::string& protocol) OVERRIDE; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 123 | |
| 124 | virtual ProtocolHandlerRegistry::DefaultClientObserver* CreateShellObserver( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 125 | ProtocolHandlerRegistry* registry) OVERRIDE; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 126 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 127 | virtual void RegisterWithOSAsDefaultClient( |
| 128 | const std::string& protocol, |
| 129 | ProtocolHandlerRegistry* reg) OVERRIDE { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 130 | ProtocolHandlerRegistry::Delegate::RegisterWithOSAsDefaultClient(protocol, |
| 131 | reg); |
| 132 | ASSERT_FALSE(IsFakeRegisteredWithOS(protocol)); |
| 133 | } |
| 134 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 135 | virtual bool IsExternalHandlerRegistered( |
| 136 | const std::string& protocol) OVERRIDE { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 137 | return registered_protocols_.find(protocol) != registered_protocols_.end(); |
| 138 | } |
| 139 | |
| 140 | bool IsFakeRegisteredWithOS(const std::string& protocol) { |
| 141 | return os_registered_protocols_.find(protocol) != |
| 142 | os_registered_protocols_.end(); |
| 143 | } |
| 144 | |
| 145 | void FakeRegisterWithOS(const std::string& protocol) { |
| 146 | os_registered_protocols_.insert(protocol); |
| 147 | } |
| 148 | |
| 149 | void Reset() { |
| 150 | registered_protocols_.clear(); |
| 151 | os_registered_protocols_.clear(); |
| 152 | force_os_failure_ = false; |
| 153 | } |
| 154 | |
| 155 | void set_force_os_failure(bool force) { force_os_failure_ = force; } |
| 156 | |
| 157 | bool force_os_failure() { return force_os_failure_; } |
| 158 | |
| 159 | private: |
| 160 | std::set<std::string> registered_protocols_; |
| 161 | std::set<std::string> os_registered_protocols_; |
| 162 | bool force_os_failure_; |
| 163 | }; |
| 164 | |
| 165 | class FakeClientObserver |
| 166 | : public ProtocolHandlerRegistry::DefaultClientObserver { |
| 167 | public: |
| 168 | FakeClientObserver(ProtocolHandlerRegistry* registry, |
| 169 | FakeDelegate* registry_delegate) |
| 170 | : ProtocolHandlerRegistry::DefaultClientObserver(registry), |
| 171 | delegate_(registry_delegate) {} |
| 172 | |
| 173 | virtual void SetDefaultWebClientUIState( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 174 | ShellIntegration::DefaultWebClientUIState state) OVERRIDE { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 175 | ProtocolHandlerRegistry::DefaultClientObserver::SetDefaultWebClientUIState( |
| 176 | state); |
| 177 | if (state == ShellIntegration::STATE_IS_DEFAULT) { |
| 178 | delegate_->FakeRegisterWithOS(worker_->protocol()); |
| 179 | } |
| 180 | if (state != ShellIntegration::STATE_PROCESSING) { |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 181 | base::MessageLoop::current()->Quit(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | private: |
| 186 | FakeDelegate* delegate_; |
| 187 | }; |
| 188 | |
| 189 | class FakeProtocolClientWorker |
| 190 | : public ShellIntegration::DefaultProtocolClientWorker { |
| 191 | public: |
| 192 | FakeProtocolClientWorker(ShellIntegration::DefaultWebClientObserver* observer, |
| 193 | const std::string& protocol, |
| 194 | bool force_failure) |
| 195 | : ShellIntegration::DefaultProtocolClientWorker(observer, protocol), |
| 196 | force_failure_(force_failure) {} |
| 197 | |
| 198 | private: |
| 199 | virtual ~FakeProtocolClientWorker() {} |
| 200 | |
| 201 | virtual ShellIntegration::DefaultWebClientState CheckIsDefault() OVERRIDE { |
| 202 | if (force_failure_) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 203 | return ShellIntegration::NOT_DEFAULT; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 204 | } else { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 205 | return ShellIntegration::IS_DEFAULT; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
| 209 | virtual bool SetAsDefault(bool interactive_permitted) OVERRIDE { |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | private: |
| 214 | bool force_failure_; |
| 215 | }; |
| 216 | |
| 217 | ProtocolHandlerRegistry::DefaultClientObserver* |
| 218 | FakeDelegate::CreateShellObserver(ProtocolHandlerRegistry* registry) { |
| 219 | return new FakeClientObserver(registry, this); |
| 220 | } |
| 221 | |
| 222 | ShellIntegration::DefaultProtocolClientWorker* FakeDelegate::CreateShellWorker( |
| 223 | ShellIntegration::DefaultWebClientObserver* observer, |
| 224 | const std::string& protocol) { |
| 225 | return new FakeProtocolClientWorker(observer, protocol, force_os_failure_); |
| 226 | } |
| 227 | |
| 228 | class NotificationCounter : public content::NotificationObserver { |
| 229 | public: |
| 230 | explicit NotificationCounter(Profile* profile) |
| 231 | : events_(0), |
| 232 | notification_registrar_() { |
| 233 | notification_registrar_.Add(this, |
| 234 | chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED, |
| 235 | content::Source<Profile>(profile)); |
| 236 | } |
| 237 | |
| 238 | int events() { return events_; } |
| 239 | bool notified() { return events_ > 0; } |
| 240 | void Clear() { events_ = 0; } |
| 241 | virtual void Observe(int type, |
| 242 | const content::NotificationSource& source, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 243 | const content::NotificationDetails& details) OVERRIDE { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 244 | ++events_; |
| 245 | } |
| 246 | |
| 247 | int events_; |
| 248 | content::NotificationRegistrar notification_registrar_; |
| 249 | }; |
| 250 | |
| 251 | class QueryProtocolHandlerOnChange |
| 252 | : public content::NotificationObserver { |
| 253 | public: |
| 254 | QueryProtocolHandlerOnChange(Profile* profile, |
| 255 | ProtocolHandlerRegistry* registry) |
| 256 | : local_registry_(registry), |
| 257 | called_(false), |
| 258 | notification_registrar_() { |
| 259 | notification_registrar_.Add(this, |
| 260 | chrome::NOTIFICATION_PROTOCOL_HANDLER_REGISTRY_CHANGED, |
| 261 | content::Source<Profile>(profile)); |
| 262 | } |
| 263 | |
| 264 | virtual void Observe(int type, |
| 265 | const content::NotificationSource& source, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 266 | const content::NotificationDetails& details) OVERRIDE { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 267 | std::vector<std::string> output; |
| 268 | local_registry_->GetRegisteredProtocols(&output); |
| 269 | called_ = true; |
| 270 | } |
| 271 | |
| 272 | ProtocolHandlerRegistry* local_registry_; |
| 273 | bool called_; |
| 274 | content::NotificationRegistrar notification_registrar_; |
| 275 | }; |
| 276 | |
| 277 | // URLRequest DCHECKS that the current MessageLoop is IO. It does this because |
| 278 | // it can't check the thread id (since net can't depend on content.) We want |
| 279 | // to harness our tests so all threads use the same loop allowing us to |
| 280 | // guarantee all messages are processed.) By overriding the IsType method |
| 281 | // we basically ignore the supplied message loop type, and instead infer |
| 282 | // our type based on the current thread. GO DEPENDENCY INJECTION! |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 283 | class TestMessageLoop : public base::MessageLoop { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 284 | public: |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 285 | TestMessageLoop() : base::MessageLoop(base::MessageLoop::TYPE_DEFAULT) {} |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 286 | virtual ~TestMessageLoop() {} |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 287 | virtual bool IsType(base::MessageLoop::Type type) const OVERRIDE { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 288 | switch (type) { |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 289 | case base::MessageLoop::TYPE_UI: |
| 290 | return BrowserThread::CurrentlyOn(BrowserThread::UI); |
| 291 | case base::MessageLoop::TYPE_IO: |
| 292 | return BrowserThread::CurrentlyOn(BrowserThread::IO); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 293 | #if defined(OS_ANDROID) |
| 294 | case base::MessageLoop::TYPE_JAVA: // fall-through |
| 295 | #endif // defined(OS_ANDROID) |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 296 | case base::MessageLoop::TYPE_DEFAULT: |
| 297 | return !BrowserThread::CurrentlyOn(BrowserThread::UI) && |
| 298 | !BrowserThread::CurrentlyOn(BrowserThread::IO); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 299 | } |
| 300 | return false; |
| 301 | } |
| 302 | }; |
| 303 | |
| 304 | } // namespace |
| 305 | |
| 306 | class ProtocolHandlerRegistryTest : public testing::Test { |
| 307 | protected: |
| 308 | ProtocolHandlerRegistryTest() |
| 309 | : ui_thread_(BrowserThread::UI, &loop_), |
| 310 | file_thread_(BrowserThread::FILE, &loop_), |
| 311 | io_thread_(BrowserThread::IO, &loop_), |
| 312 | test_protocol_handler_(CreateProtocolHandler("test", "test")) {} |
| 313 | |
| 314 | FakeDelegate* delegate() const { return delegate_; } |
| 315 | ProtocolHandlerRegistry* registry() { return registry_.get(); } |
| 316 | TestingProfile* profile() const { return profile_.get(); } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 317 | const ProtocolHandler& test_protocol_handler() const { |
| 318 | return test_protocol_handler_; |
| 319 | } |
| 320 | |
| 321 | ProtocolHandler CreateProtocolHandler(const std::string& protocol, |
| 322 | const GURL& url, |
| 323 | const std::string& title) { |
| 324 | return ProtocolHandler::CreateProtocolHandler(protocol, url, |
| 325 | UTF8ToUTF16(title)); |
| 326 | } |
| 327 | |
| 328 | ProtocolHandler CreateProtocolHandler(const std::string& protocol, |
| 329 | const std::string& name) { |
| 330 | return CreateProtocolHandler(protocol, GURL("http://" + name + "/%s"), |
| 331 | name); |
| 332 | } |
| 333 | |
| 334 | void RecreateRegistry(bool initialize) { |
| 335 | TeadDownRegistry(); |
| 336 | SetUpRegistry(initialize); |
| 337 | } |
| 338 | |
| 339 | // Returns a new registry, initializing it if |initialize| is true. |
| 340 | // Caller assumes ownership for the object |
| 341 | void SetUpRegistry(bool initialize) { |
| 342 | delegate_ = new FakeDelegate(); |
| 343 | registry_.reset(new ProtocolHandlerRegistry(profile(), delegate())); |
| 344 | if (initialize) registry_->InitProtocolSettings(); |
| 345 | } |
| 346 | |
| 347 | void TeadDownRegistry() { |
| 348 | registry_->Shutdown(); |
| 349 | registry_.reset(); |
| 350 | // Registry owns the delegate_ it handles deletion of that object. |
| 351 | } |
| 352 | |
| 353 | virtual void SetUp() { |
| 354 | profile_.reset(new TestingProfile()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 355 | CHECK(profile_->GetPrefs()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 356 | SetUpRegistry(true); |
| 357 | test_protocol_handler_ = |
| 358 | CreateProtocolHandler("test", GURL("http://test.com/%s"), "Test"); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | virtual void TearDown() { |
| 362 | TeadDownRegistry(); |
| 363 | } |
| 364 | |
| 365 | TestMessageLoop loop_; |
| 366 | |
| 367 | private: |
| 368 | content::TestBrowserThread ui_thread_; |
| 369 | content::TestBrowserThread file_thread_; |
| 370 | content::TestBrowserThread io_thread_; |
| 371 | |
| 372 | scoped_ptr<TestingProfile> profile_; |
| 373 | FakeDelegate* delegate_; // Registry assumes ownership of delegate_. |
| 374 | scoped_ptr<ProtocolHandlerRegistry> registry_; |
| 375 | ProtocolHandler test_protocol_handler_; |
| 376 | }; |
| 377 | |
| 378 | // ProtocolHandlerRegistryTest tests are flaky on Linux & ChromeOS. |
| 379 | // http://crbug.com/133023 |
| 380 | #if defined(OS_LINUX) || defined(OS_CHROMEOS) |
| 381 | #define MAYBE_AcceptProtocolHandlerHandlesProtocol \ |
| 382 | DISABLED_AcceptProtocolHandlerHandlesProtocol |
| 383 | #define MAYBE_DeniedProtocolIsntHandledUntilAccepted \ |
| 384 | DISABLED_DeniedProtocolIsntHandledUntilAccepted |
| 385 | #define MAYBE_TestStartsAsDefault DISABLED_TestStartsAsDefault |
| 386 | #define MAYBE_TestRemoveHandlerRemovesDefault \ |
| 387 | DISABLED_TestRemoveHandlerRemovesDefault |
| 388 | #define MAYBE_TestClearDefaultGetsPropagatedToIO \ |
| 389 | DISABLED_TestClearDefaultGetsPropagatedToIO |
| 390 | #define MAYBE_TestIsHandledProtocolWorksOnIOThread \ |
| 391 | DISABLED_TestIsHandledProtocolWorksOnIOThread |
| 392 | #define MAYBE_TestInstallDefaultHandler \ |
| 393 | DISABLED_TestInstallDefaultHandler |
| 394 | #else |
| 395 | #define MAYBE_AcceptProtocolHandlerHandlesProtocol \ |
| 396 | AcceptProtocolHandlerHandlesProtocol |
| 397 | #define MAYBE_DeniedProtocolIsntHandledUntilAccepted \ |
| 398 | DeniedProtocolIsntHandledUntilAccepted |
| 399 | #define MAYBE_TestStartsAsDefault TestStartsAsDefault |
| 400 | #define MAYBE_TestRemoveHandlerRemovesDefault TestRemoveHandlerRemovesDefault |
| 401 | #define MAYBE_TestClearDefaultGetsPropagatedToIO \ |
| 402 | TestClearDefaultGetsPropagatedToIO |
| 403 | #define MAYBE_TestIsHandledProtocolWorksOnIOThread \ |
| 404 | TestIsHandledProtocolWorksOnIOThread |
| 405 | #define MAYBE_TestInstallDefaultHandler TestInstallDefaultHandler |
| 406 | #endif // defined(OS_CHROMEOS) |
| 407 | |
| 408 | TEST_F(ProtocolHandlerRegistryTest, |
| 409 | MAYBE_AcceptProtocolHandlerHandlesProtocol) { |
| 410 | ASSERT_FALSE(registry()->IsHandledProtocol("test")); |
| 411 | registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler()); |
| 412 | ASSERT_TRUE(registry()->IsHandledProtocol("test")); |
| 413 | } |
| 414 | |
| 415 | TEST_F(ProtocolHandlerRegistryTest, |
| 416 | MAYBE_DeniedProtocolIsntHandledUntilAccepted) { |
| 417 | registry()->OnDenyRegisterProtocolHandler(test_protocol_handler()); |
| 418 | ASSERT_FALSE(registry()->IsHandledProtocol("test")); |
| 419 | registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler()); |
| 420 | ASSERT_TRUE(registry()->IsHandledProtocol("test")); |
| 421 | } |
| 422 | |
| 423 | TEST_F(ProtocolHandlerRegistryTest, ClearDefaultMakesProtocolNotHandled) { |
| 424 | registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler()); |
| 425 | registry()->ClearDefault("test"); |
| 426 | ASSERT_FALSE(registry()->IsHandledProtocol("test")); |
| 427 | ASSERT_TRUE(registry()->GetHandlerFor("test").IsEmpty()); |
| 428 | } |
| 429 | |
| 430 | TEST_F(ProtocolHandlerRegistryTest, DisableDeregistersProtocolHandlers) { |
| 431 | ASSERT_FALSE(delegate()->IsExternalHandlerRegistered("test")); |
| 432 | registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler()); |
| 433 | ASSERT_TRUE(delegate()->IsExternalHandlerRegistered("test")); |
| 434 | |
| 435 | registry()->Disable(); |
| 436 | ASSERT_FALSE(delegate()->IsExternalHandlerRegistered("test")); |
| 437 | registry()->Enable(); |
| 438 | ASSERT_TRUE(delegate()->IsExternalHandlerRegistered("test")); |
| 439 | } |
| 440 | |
| 441 | TEST_F(ProtocolHandlerRegistryTest, IgnoreProtocolHandler) { |
| 442 | registry()->OnIgnoreRegisterProtocolHandler(test_protocol_handler()); |
| 443 | ASSERT_TRUE(registry()->IsIgnored(test_protocol_handler())); |
| 444 | |
| 445 | registry()->RemoveIgnoredHandler(test_protocol_handler()); |
| 446 | ASSERT_FALSE(registry()->IsIgnored(test_protocol_handler())); |
| 447 | } |
| 448 | |
| 449 | TEST_F(ProtocolHandlerRegistryTest, IgnoreEquivalentProtocolHandler) { |
| 450 | ProtocolHandler ph1 = CreateProtocolHandler("test", GURL("http://test/%s"), |
| 451 | "test1"); |
| 452 | ProtocolHandler ph2 = CreateProtocolHandler("test", GURL("http://test/%s"), |
| 453 | "test2"); |
| 454 | |
| 455 | registry()->OnIgnoreRegisterProtocolHandler(ph1); |
| 456 | ASSERT_TRUE(registry()->IsIgnored(ph1)); |
| 457 | ASSERT_TRUE(registry()->HasIgnoredEquivalent(ph2)); |
| 458 | |
| 459 | registry()->RemoveIgnoredHandler(ph1); |
| 460 | ASSERT_FALSE(registry()->IsIgnored(ph1)); |
| 461 | ASSERT_FALSE(registry()->HasIgnoredEquivalent(ph2)); |
| 462 | } |
| 463 | |
| 464 | TEST_F(ProtocolHandlerRegistryTest, SaveAndLoad) { |
| 465 | ProtocolHandler stuff_protocol_handler( |
| 466 | CreateProtocolHandler("stuff", "stuff")); |
| 467 | registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler()); |
| 468 | registry()->OnIgnoreRegisterProtocolHandler(stuff_protocol_handler); |
| 469 | |
| 470 | ASSERT_TRUE(registry()->IsHandledProtocol("test")); |
| 471 | ASSERT_TRUE(registry()->IsIgnored(stuff_protocol_handler)); |
| 472 | delegate()->Reset(); |
| 473 | RecreateRegistry(true); |
| 474 | ASSERT_TRUE(registry()->IsHandledProtocol("test")); |
| 475 | ASSERT_TRUE(registry()->IsIgnored(stuff_protocol_handler)); |
| 476 | } |
| 477 | |
| 478 | TEST_F(ProtocolHandlerRegistryTest, TestEnabledDisabled) { |
| 479 | registry()->Disable(); |
| 480 | ASSERT_FALSE(registry()->enabled()); |
| 481 | registry()->Enable(); |
| 482 | ASSERT_TRUE(registry()->enabled()); |
| 483 | } |
| 484 | |
| 485 | TEST_F(ProtocolHandlerRegistryTest, |
| 486 | DisallowRegisteringExternallyHandledProtocols) { |
| 487 | delegate()->RegisterExternalHandler("test"); |
| 488 | ASSERT_FALSE(registry()->CanSchemeBeOverridden("test")); |
| 489 | } |
| 490 | |
| 491 | TEST_F(ProtocolHandlerRegistryTest, RemovingHandlerMeansItCanBeAddedAgain) { |
| 492 | registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler()); |
| 493 | ASSERT_TRUE(registry()->CanSchemeBeOverridden("test")); |
| 494 | registry()->RemoveHandler(test_protocol_handler()); |
| 495 | ASSERT_TRUE(registry()->CanSchemeBeOverridden("test")); |
| 496 | } |
| 497 | |
| 498 | TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestStartsAsDefault) { |
| 499 | registry()->OnAcceptRegisterProtocolHandler(test_protocol_handler()); |
| 500 | ASSERT_TRUE(registry()->IsDefault(test_protocol_handler())); |
| 501 | } |
| 502 | |
| 503 | TEST_F(ProtocolHandlerRegistryTest, TestClearDefault) { |
| 504 | ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); |
| 505 | ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); |
| 506 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 507 | registry()->OnAcceptRegisterProtocolHandler(ph2); |
| 508 | |
| 509 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 510 | registry()->ClearDefault("test"); |
| 511 | ASSERT_FALSE(registry()->IsDefault(ph1)); |
| 512 | ASSERT_FALSE(registry()->IsDefault(ph2)); |
| 513 | } |
| 514 | |
| 515 | TEST_F(ProtocolHandlerRegistryTest, TestGetHandlerFor) { |
| 516 | ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); |
| 517 | ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); |
| 518 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 519 | registry()->OnAcceptRegisterProtocolHandler(ph2); |
| 520 | |
| 521 | registry()->OnAcceptRegisterProtocolHandler(ph2); |
| 522 | ASSERT_EQ(ph2, registry()->GetHandlerFor("test")); |
| 523 | ASSERT_TRUE(registry()->IsHandledProtocol("test")); |
| 524 | } |
| 525 | |
| 526 | TEST_F(ProtocolHandlerRegistryTest, TestMostRecentHandlerIsDefault) { |
| 527 | ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); |
| 528 | ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); |
| 529 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 530 | registry()->OnAcceptRegisterProtocolHandler(ph2); |
| 531 | ASSERT_FALSE(registry()->IsDefault(ph1)); |
| 532 | ASSERT_TRUE(registry()->IsDefault(ph2)); |
| 533 | } |
| 534 | |
| 535 | TEST_F(ProtocolHandlerRegistryTest, TestOnAcceptRegisterProtocolHandler) { |
| 536 | ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); |
| 537 | ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); |
| 538 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 539 | registry()->OnAcceptRegisterProtocolHandler(ph2); |
| 540 | |
| 541 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 542 | ASSERT_TRUE(registry()->IsDefault(ph1)); |
| 543 | ASSERT_FALSE(registry()->IsDefault(ph2)); |
| 544 | |
| 545 | registry()->OnAcceptRegisterProtocolHandler(ph2); |
| 546 | ASSERT_FALSE(registry()->IsDefault(ph1)); |
| 547 | ASSERT_TRUE(registry()->IsDefault(ph2)); |
| 548 | } |
| 549 | |
| 550 | TEST_F(ProtocolHandlerRegistryTest, TestDefaultSaveLoad) { |
| 551 | ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); |
| 552 | ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); |
| 553 | registry()->OnDenyRegisterProtocolHandler(ph1); |
| 554 | registry()->OnDenyRegisterProtocolHandler(ph2); |
| 555 | |
| 556 | registry()->OnAcceptRegisterProtocolHandler(ph2); |
| 557 | registry()->Disable(); |
| 558 | |
| 559 | RecreateRegistry(true); |
| 560 | |
| 561 | ASSERT_FALSE(registry()->enabled()); |
| 562 | registry()->Enable(); |
| 563 | ASSERT_FALSE(registry()->IsDefault(ph1)); |
| 564 | ASSERT_TRUE(registry()->IsDefault(ph2)); |
| 565 | |
| 566 | RecreateRegistry(true); |
| 567 | ASSERT_TRUE(registry()->enabled()); |
| 568 | } |
| 569 | |
| 570 | TEST_F(ProtocolHandlerRegistryTest, TestRemoveHandler) { |
| 571 | ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); |
| 572 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 573 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 574 | |
| 575 | registry()->RemoveHandler(ph1); |
| 576 | ASSERT_FALSE(registry()->IsRegistered(ph1)); |
| 577 | ASSERT_FALSE(registry()->IsHandledProtocol("test")); |
| 578 | } |
| 579 | |
| 580 | TEST_F(ProtocolHandlerRegistryTest, TestIsRegistered) { |
| 581 | ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); |
| 582 | ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); |
| 583 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 584 | registry()->OnAcceptRegisterProtocolHandler(ph2); |
| 585 | |
| 586 | ASSERT_TRUE(registry()->IsRegistered(ph1)); |
| 587 | } |
| 588 | |
| 589 | TEST_F(ProtocolHandlerRegistryTest, TestIsEquivalentRegistered) { |
| 590 | ProtocolHandler ph1 = CreateProtocolHandler("test", GURL("http://test/%s"), |
| 591 | "test1"); |
| 592 | ProtocolHandler ph2 = CreateProtocolHandler("test", GURL("http://test/%s"), |
| 593 | "test2"); |
| 594 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 595 | |
| 596 | ASSERT_TRUE(registry()->IsRegistered(ph1)); |
| 597 | ASSERT_TRUE(registry()->HasRegisteredEquivalent(ph2)); |
| 598 | } |
| 599 | |
| 600 | TEST_F(ProtocolHandlerRegistryTest, TestSilentlyRegisterHandler) { |
| 601 | ProtocolHandler ph1 = CreateProtocolHandler("test", GURL("http://test/%s"), |
| 602 | "test1"); |
| 603 | ProtocolHandler ph2 = CreateProtocolHandler("test", GURL("http://test/%s"), |
| 604 | "test2"); |
| 605 | ProtocolHandler ph3 = CreateProtocolHandler("ignore", GURL("http://test/%s"), |
| 606 | "ignore1"); |
| 607 | ProtocolHandler ph4 = CreateProtocolHandler("ignore", GURL("http://test/%s"), |
| 608 | "ignore2"); |
| 609 | |
| 610 | ASSERT_FALSE(registry()->SilentlyHandleRegisterHandlerRequest(ph1)); |
| 611 | ASSERT_FALSE(registry()->IsRegistered(ph1)); |
| 612 | |
| 613 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 614 | ASSERT_TRUE(registry()->IsRegistered(ph1)); |
| 615 | |
| 616 | ASSERT_TRUE(registry()->SilentlyHandleRegisterHandlerRequest(ph2)); |
| 617 | ASSERT_FALSE(registry()->IsRegistered(ph1)); |
| 618 | ASSERT_TRUE(registry()->IsRegistered(ph2)); |
| 619 | |
| 620 | ASSERT_FALSE(registry()->SilentlyHandleRegisterHandlerRequest(ph3)); |
| 621 | ASSERT_FALSE(registry()->IsRegistered(ph3)); |
| 622 | |
| 623 | registry()->OnIgnoreRegisterProtocolHandler(ph3); |
| 624 | ASSERT_FALSE(registry()->IsRegistered(ph3)); |
| 625 | ASSERT_TRUE(registry()->IsIgnored(ph3)); |
| 626 | |
| 627 | ASSERT_TRUE(registry()->SilentlyHandleRegisterHandlerRequest(ph4)); |
| 628 | ASSERT_FALSE(registry()->IsRegistered(ph4)); |
| 629 | ASSERT_TRUE(registry()->HasIgnoredEquivalent(ph4)); |
| 630 | } |
| 631 | |
| 632 | TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestRemoveHandlerRemovesDefault) { |
| 633 | ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); |
| 634 | ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); |
| 635 | ProtocolHandler ph3 = CreateProtocolHandler("test", "test3"); |
| 636 | |
| 637 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 638 | registry()->OnAcceptRegisterProtocolHandler(ph2); |
| 639 | registry()->OnAcceptRegisterProtocolHandler(ph3); |
| 640 | |
| 641 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 642 | registry()->RemoveHandler(ph1); |
| 643 | ASSERT_FALSE(registry()->IsDefault(ph1)); |
| 644 | } |
| 645 | |
| 646 | TEST_F(ProtocolHandlerRegistryTest, TestGetHandlersFor) { |
| 647 | ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); |
| 648 | ProtocolHandler ph2 = CreateProtocolHandler("test", "test2"); |
| 649 | ProtocolHandler ph3 = CreateProtocolHandler("test", "test3"); |
| 650 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 651 | registry()->OnAcceptRegisterProtocolHandler(ph2); |
| 652 | registry()->OnAcceptRegisterProtocolHandler(ph3); |
| 653 | |
| 654 | ProtocolHandlerRegistry::ProtocolHandlerList handlers = |
| 655 | registry()->GetHandlersFor("test"); |
| 656 | ASSERT_EQ(static_cast<size_t>(3), handlers.size()); |
| 657 | |
| 658 | ASSERT_EQ(ph3, handlers[0]); |
| 659 | ASSERT_EQ(ph2, handlers[1]); |
| 660 | ASSERT_EQ(ph1, handlers[2]); |
| 661 | } |
| 662 | |
| 663 | TEST_F(ProtocolHandlerRegistryTest, TestGetRegisteredProtocols) { |
| 664 | std::vector<std::string> protocols; |
| 665 | registry()->GetRegisteredProtocols(&protocols); |
| 666 | ASSERT_EQ(static_cast<size_t>(0), protocols.size()); |
| 667 | |
| 668 | registry()->GetHandlersFor("test"); |
| 669 | |
| 670 | protocols.clear(); |
| 671 | registry()->GetRegisteredProtocols(&protocols); |
| 672 | ASSERT_EQ(static_cast<size_t>(0), protocols.size()); |
| 673 | } |
| 674 | |
| 675 | TEST_F(ProtocolHandlerRegistryTest, TestIsHandledProtocol) { |
| 676 | registry()->GetHandlersFor("test"); |
| 677 | ASSERT_FALSE(registry()->IsHandledProtocol("test")); |
| 678 | } |
| 679 | |
| 680 | TEST_F(ProtocolHandlerRegistryTest, TestNotifications) { |
| 681 | ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); |
| 682 | NotificationCounter counter(profile()); |
| 683 | |
| 684 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 685 | ASSERT_TRUE(counter.notified()); |
| 686 | counter.Clear(); |
| 687 | |
| 688 | registry()->Disable(); |
| 689 | ASSERT_TRUE(counter.notified()); |
| 690 | counter.Clear(); |
| 691 | |
| 692 | registry()->Enable(); |
| 693 | ASSERT_TRUE(counter.notified()); |
| 694 | counter.Clear(); |
| 695 | |
| 696 | registry()->RemoveHandler(ph1); |
| 697 | ASSERT_TRUE(counter.notified()); |
| 698 | counter.Clear(); |
| 699 | } |
| 700 | |
| 701 | TEST_F(ProtocolHandlerRegistryTest, TestReentrantNotifications) { |
| 702 | QueryProtocolHandlerOnChange queryer(profile(), registry()); |
| 703 | ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); |
| 704 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 705 | ASSERT_TRUE(queryer.called_); |
| 706 | } |
| 707 | |
| 708 | TEST_F(ProtocolHandlerRegistryTest, TestProtocolsWithNoDefaultAreHandled) { |
| 709 | ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); |
| 710 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 711 | registry()->ClearDefault("test"); |
| 712 | std::vector<std::string> handled_protocols; |
| 713 | registry()->GetRegisteredProtocols(&handled_protocols); |
| 714 | ASSERT_EQ(static_cast<size_t>(1), handled_protocols.size()); |
| 715 | ASSERT_EQ("test", handled_protocols[0]); |
| 716 | } |
| 717 | |
| 718 | TEST_F(ProtocolHandlerRegistryTest, TestDisablePreventsHandling) { |
| 719 | ProtocolHandler ph1 = CreateProtocolHandler("test", "test1"); |
| 720 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 721 | ASSERT_TRUE(registry()->IsHandledProtocol("test")); |
| 722 | registry()->Disable(); |
| 723 | ASSERT_FALSE(registry()->IsHandledProtocol("test")); |
| 724 | } |
| 725 | |
| 726 | // TODO(smckay): This is much more appropriately an integration |
| 727 | // test. Make that so, then update the |
| 728 | // ShellIntegretion{Delegate,Observer,Worker} test classes we use to fully |
| 729 | // isolate this test from the FILE thread. |
| 730 | TEST_F(ProtocolHandlerRegistryTest, TestOSRegistration) { |
| 731 | ProtocolHandler ph_do1 = CreateProtocolHandler("do", "test1"); |
| 732 | ProtocolHandler ph_do2 = CreateProtocolHandler("do", "test2"); |
| 733 | ProtocolHandler ph_dont = CreateProtocolHandler("dont", "test"); |
| 734 | |
| 735 | ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("do")); |
| 736 | ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont")); |
| 737 | |
| 738 | registry()->OnAcceptRegisterProtocolHandler(ph_do1); |
| 739 | registry()->OnDenyRegisterProtocolHandler(ph_dont); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 740 | base::MessageLoop::current()->Run(); // FILE thread needs to run. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 741 | ASSERT_TRUE(delegate()->IsFakeRegisteredWithOS("do")); |
| 742 | ASSERT_FALSE(delegate()->IsFakeRegisteredWithOS("dont")); |
| 743 | |
| 744 | // This should not register with the OS, if it does the delegate |
| 745 | // will assert for us. We don't need to wait for the message loop |
| 746 | // as it should not go through to the shell worker. |
| 747 | registry()->OnAcceptRegisterProtocolHandler(ph_do2); |
| 748 | } |
| 749 | |
| 750 | #if defined(OS_LINUX) |
| 751 | // TODO(benwells): When Linux support is more reliable and |
| 752 | // http://crbut.com/88255 is fixed this test will pass. |
| 753 | #define MAYBE_TestOSRegistrationFailure DISABLED_TestOSRegistrationFailure |
| 754 | #else |
| 755 | #define MAYBE_TestOSRegistrationFailure TestOSRegistrationFailure |
| 756 | #endif |
| 757 | |
| 758 | // TODO(smckay): This is much more appropriately an integration |
| 759 | // test. Make that so, then update the |
| 760 | // ShellIntegretion{Delegate,Observer,Worker} test classes we use to fully |
| 761 | // isolate this test from the FILE thread. |
| 762 | TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestOSRegistrationFailure) { |
| 763 | ProtocolHandler ph_do = CreateProtocolHandler("do", "test1"); |
| 764 | ProtocolHandler ph_dont = CreateProtocolHandler("dont", "test"); |
| 765 | |
| 766 | ASSERT_FALSE(registry()->IsHandledProtocol("do")); |
| 767 | ASSERT_FALSE(registry()->IsHandledProtocol("dont")); |
| 768 | |
| 769 | registry()->OnAcceptRegisterProtocolHandler(ph_do); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 770 | base::MessageLoop::current()->Run(); // FILE thread needs to run. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 771 | delegate()->set_force_os_failure(true); |
| 772 | registry()->OnAcceptRegisterProtocolHandler(ph_dont); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 773 | base::MessageLoop::current()->Run(); // FILE thread needs to run. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 774 | ASSERT_TRUE(registry()->IsHandledProtocol("do")); |
| 775 | ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("do").size()); |
| 776 | ASSERT_FALSE(registry()->IsHandledProtocol("dont")); |
| 777 | ASSERT_EQ(static_cast<size_t>(1), registry()->GetHandlersFor("dont").size()); |
| 778 | } |
| 779 | |
| 780 | TEST_F(ProtocolHandlerRegistryTest, TestMaybeCreateTaskWorksFromIOThread) { |
| 781 | ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1"); |
| 782 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 783 | GURL url("mailto:someone@something.com"); |
| 784 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 785 | scoped_ptr<net::URLRequestJobFactory> interceptor( |
| 786 | registry()->CreateJobInterceptorFactory()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 787 | AssertIntercepted(url, interceptor.get()); |
| 788 | } |
| 789 | |
| 790 | TEST_F(ProtocolHandlerRegistryTest, |
| 791 | MAYBE_TestIsHandledProtocolWorksOnIOThread) { |
| 792 | std::string scheme("mailto"); |
| 793 | ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1"); |
| 794 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 795 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 796 | scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> interceptor( |
| 797 | registry()->CreateJobInterceptorFactory()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 798 | AssertWillHandle(scheme, true, interceptor.get()); |
| 799 | } |
| 800 | |
| 801 | TEST_F(ProtocolHandlerRegistryTest, TestRemovingDefaultFallsBackToOldDefault) { |
| 802 | ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1"); |
| 803 | ProtocolHandler ph2 = CreateProtocolHandler("mailto", "test2"); |
| 804 | ProtocolHandler ph3 = CreateProtocolHandler("mailto", "test3"); |
| 805 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 806 | registry()->OnAcceptRegisterProtocolHandler(ph2); |
| 807 | registry()->OnAcceptRegisterProtocolHandler(ph3); |
| 808 | |
| 809 | ASSERT_TRUE(registry()->IsDefault(ph3)); |
| 810 | registry()->RemoveHandler(ph3); |
| 811 | ASSERT_TRUE(registry()->IsDefault(ph2)); |
| 812 | registry()->OnAcceptRegisterProtocolHandler(ph3); |
| 813 | ASSERT_TRUE(registry()->IsDefault(ph3)); |
| 814 | registry()->RemoveHandler(ph2); |
| 815 | ASSERT_TRUE(registry()->IsDefault(ph3)); |
| 816 | registry()->RemoveHandler(ph3); |
| 817 | ASSERT_TRUE(registry()->IsDefault(ph1)); |
| 818 | } |
| 819 | |
| 820 | TEST_F(ProtocolHandlerRegistryTest, TestRemovingDefaultDoesntChangeHandlers) { |
| 821 | ProtocolHandler ph1 = CreateProtocolHandler("mailto", "test1"); |
| 822 | ProtocolHandler ph2 = CreateProtocolHandler("mailto", "test2"); |
| 823 | ProtocolHandler ph3 = CreateProtocolHandler("mailto", "test3"); |
| 824 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 825 | registry()->OnAcceptRegisterProtocolHandler(ph2); |
| 826 | registry()->OnAcceptRegisterProtocolHandler(ph3); |
| 827 | registry()->RemoveHandler(ph3); |
| 828 | |
| 829 | ProtocolHandlerRegistry::ProtocolHandlerList handlers = |
| 830 | registry()->GetHandlersFor("mailto"); |
| 831 | ASSERT_EQ(static_cast<size_t>(2), handlers.size()); |
| 832 | |
| 833 | ASSERT_EQ(ph2, handlers[0]); |
| 834 | ASSERT_EQ(ph1, handlers[1]); |
| 835 | } |
| 836 | |
| 837 | TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestClearDefaultGetsPropagatedToIO) { |
| 838 | std::string scheme("mailto"); |
| 839 | ProtocolHandler ph1 = CreateProtocolHandler(scheme, "test1"); |
| 840 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 841 | registry()->ClearDefault(scheme); |
| 842 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 843 | scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> interceptor( |
| 844 | registry()->CreateJobInterceptorFactory()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 845 | AssertWillHandle(scheme, false, interceptor.get()); |
| 846 | } |
| 847 | |
| 848 | TEST_F(ProtocolHandlerRegistryTest, TestLoadEnabledGetsPropogatedToIO) { |
| 849 | std::string mailto("mailto"); |
| 850 | ProtocolHandler ph1 = CreateProtocolHandler(mailto, "MailtoHandler"); |
| 851 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 852 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 853 | scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> interceptor( |
| 854 | registry()->CreateJobInterceptorFactory()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 855 | AssertWillHandle(mailto, true, interceptor.get()); |
| 856 | registry()->Disable(); |
| 857 | AssertWillHandle(mailto, false, interceptor.get()); |
| 858 | } |
| 859 | |
| 860 | TEST_F(ProtocolHandlerRegistryTest, TestReplaceHandler) { |
| 861 | ProtocolHandler ph1 = CreateProtocolHandler("mailto", |
| 862 | GURL("http://test.com/%s"), "test1"); |
| 863 | ProtocolHandler ph2 = CreateProtocolHandler("mailto", |
| 864 | GURL("http://test.com/updated-url/%s"), "test2"); |
| 865 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 866 | ASSERT_TRUE(registry()->AttemptReplace(ph2)); |
| 867 | const ProtocolHandler& handler(registry()->GetHandlerFor("mailto")); |
| 868 | ASSERT_EQ(handler.url(), ph2.url()); |
| 869 | } |
| 870 | |
| 871 | TEST_F(ProtocolHandlerRegistryTest, TestReplaceNonDefaultHandler) { |
| 872 | ProtocolHandler ph1 = CreateProtocolHandler("mailto", |
| 873 | GURL("http://test.com/%s"), "test1"); |
| 874 | ProtocolHandler ph2 = CreateProtocolHandler("mailto", |
| 875 | GURL("http://test.com/updated-url/%s"), "test2"); |
| 876 | ProtocolHandler ph3 = CreateProtocolHandler("mailto", |
| 877 | GURL("http://else.com/%s"), "test3"); |
| 878 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 879 | registry()->OnAcceptRegisterProtocolHandler(ph3); |
| 880 | ASSERT_TRUE(registry()->AttemptReplace(ph2)); |
| 881 | const ProtocolHandler& handler(registry()->GetHandlerFor("mailto")); |
| 882 | ASSERT_EQ(handler.url(), ph3.url()); |
| 883 | } |
| 884 | |
| 885 | TEST_F(ProtocolHandlerRegistryTest, TestReplaceRemovesStaleHandlers) { |
| 886 | ProtocolHandler ph1 = CreateProtocolHandler("mailto", |
| 887 | GURL("http://test.com/%s"), "test1"); |
| 888 | ProtocolHandler ph2 = CreateProtocolHandler("mailto", |
| 889 | GURL("http://test.com/updated-url/%s"), "test2"); |
| 890 | ProtocolHandler ph3 = CreateProtocolHandler("mailto", |
| 891 | GURL("http://test.com/third/%s"), "test"); |
| 892 | registry()->OnAcceptRegisterProtocolHandler(ph1); |
| 893 | registry()->OnAcceptRegisterProtocolHandler(ph2); |
| 894 | |
| 895 | // This should replace the previous two handlers. |
| 896 | ASSERT_TRUE(registry()->AttemptReplace(ph3)); |
| 897 | const ProtocolHandler& handler(registry()->GetHandlerFor("mailto")); |
| 898 | ASSERT_EQ(handler.url(), ph3.url()); |
| 899 | registry()->RemoveHandler(ph3); |
| 900 | ASSERT_TRUE(registry()->GetHandlerFor("mailto").IsEmpty()); |
| 901 | } |
| 902 | |
| 903 | TEST_F(ProtocolHandlerRegistryTest, TestIsSameOrigin) { |
| 904 | ProtocolHandler ph1 = CreateProtocolHandler("mailto", |
| 905 | GURL("http://test.com/%s"), "test1"); |
| 906 | ProtocolHandler ph2 = CreateProtocolHandler("mailto", |
| 907 | GURL("http://test.com/updated-url/%s"), "test2"); |
| 908 | ProtocolHandler ph3 = CreateProtocolHandler("mailto", |
| 909 | GURL("http://other.com/%s"), "test"); |
| 910 | ASSERT_EQ(ph1.url().GetOrigin() == ph2.url().GetOrigin(), |
| 911 | ph1.IsSameOrigin(ph2)); |
| 912 | ASSERT_EQ(ph1.url().GetOrigin() == ph2.url().GetOrigin(), |
| 913 | ph2.IsSameOrigin(ph1)); |
| 914 | ASSERT_EQ(ph2.url().GetOrigin() == ph3.url().GetOrigin(), |
| 915 | ph2.IsSameOrigin(ph3)); |
| 916 | ASSERT_EQ(ph3.url().GetOrigin() == ph2.url().GetOrigin(), |
| 917 | ph3.IsSameOrigin(ph2)); |
| 918 | } |
| 919 | |
| 920 | TEST_F(ProtocolHandlerRegistryTest, MAYBE_TestInstallDefaultHandler) { |
| 921 | RecreateRegistry(false); |
| 922 | registry()->AddPredefinedHandler(CreateProtocolHandler( |
| 923 | "test", GURL("http://test.com/%s"), "Test")); |
| 924 | registry()->InitProtocolSettings(); |
| 925 | std::vector<std::string> protocols; |
| 926 | registry()->GetRegisteredProtocols(&protocols); |
| 927 | ASSERT_EQ(static_cast<size_t>(1), protocols.size()); |
| 928 | } |