Chromium Code Reviews| Index: content/browser/service_worker/service_worker_dispatcher_host_unittest.cc |
| diff --git a/content/browser/service_worker/service_worker_dispatcher_host_unittest.cc b/content/browser/service_worker/service_worker_dispatcher_host_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ebc7f6c03a3cbae9bd326648c72d03958fdbaf12 |
| --- /dev/null |
| +++ b/content/browser/service_worker/service_worker_dispatcher_host_unittest.cc |
| @@ -0,0 +1,105 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/service_worker/service_worker_dispatcher_host.h" |
| + |
| +#include "base/command_line.h" |
| +#include "base/files/scoped_temp_dir.h" |
| +#include "content/browser/child_process_security_policy_impl.h" |
| +#include "content/browser/service_worker/service_worker_context.h" |
| +#include "content/common/service_worker_messages.h" |
| +#include "content/public/common/content_switches.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using base::MessageLoop; |
| + |
| +namespace content { |
| + |
| +class ServiceWorkerDispatcherHostTest : public testing::Test { |
| + protected: |
| + virtual void SetUp() { |
| + context_ = new ServiceWorkerContext(dir_.path(), NULL); |
|
michaeln
2013/10/26 00:57:40
dir_ isn't needed now that there is no dir anymore
alecflett
2013/10/28 17:03:14
Done.
|
| + } |
| + |
| + virtual void TearDown() { |
| + DCHECK(context_->HasOneRef()); |
| + context_ = NULL; |
| + } |
| + |
| + scoped_refptr<ServiceWorkerContext> context_; |
| + base::ScopedTempDir dir_; |
| +}; |
| + |
| +namespace { |
| + |
| +static const int kRenderProcessId = 1; |
| + |
| +class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { |
| + public: |
| + TestingServiceWorkerDispatcherHost(int process_id, |
| + ServiceWorkerContext* context) |
| + : ServiceWorkerDispatcherHost(process_id, context) {} |
| + |
| + virtual bool Send(IPC::Message* message) OVERRIDE { |
| + sent_messages_.push_back(message); |
| + return true; |
| + } |
| + |
| + ScopedVector<IPC::Message> sent_messages_; |
| + |
| + protected: |
| + virtual ~TestingServiceWorkerDispatcherHost() {} |
| +}; |
| + |
| +} // namespace |
| + |
| +TEST_F(ServiceWorkerDispatcherHostTest, DisabledCausesError) { |
| + DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( |
| + switches::kEnableServiceWorker)); |
| + |
| + scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = |
| + new TestingServiceWorkerDispatcherHost(kRenderProcessId, context_); |
| + |
| + ChildProcessSecurityPolicyImpl* policy = |
| + ChildProcessSecurityPolicyImpl::GetInstance(); |
| + policy->Add(kRenderProcessId); |
| + |
| + bool handled; |
| + dispatcher_host->OnMessageReceived( |
| + ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), |
| + &handled); |
| + DCHECK(handled); |
| + |
| + // TODO(alecflett): Pump the message loop when this becomes async. |
| + DCHECK_EQ(1UL, dispatcher_host->sent_messages_.size()); |
| + DCHECK_EQ(ServiceWorkerMsg_ServiceWorkerRegistered::ID, |
| + dispatcher_host->sent_messages_[0]->type()); |
| +} |
| + |
| +TEST_F(ServiceWorkerDispatcherHostTest, Enabled) { |
| + DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( |
| + switches::kEnableServiceWorker)); |
| + CommandLine::ForCurrentProcess()->AppendSwitch( |
| + switches::kEnableServiceWorker); |
| + |
| + scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = |
| + new TestingServiceWorkerDispatcherHost(kRenderProcessId, context_); |
| + |
| + ChildProcessSecurityPolicyImpl* policy = |
| + ChildProcessSecurityPolicyImpl::GetInstance(); |
| + policy->Add(kRenderProcessId); |
| + |
| + bool handled; |
| + dispatcher_host->OnMessageReceived( |
| + ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), |
| + &handled); |
| + DCHECK(handled); |
| + |
| + // TODO(alecflett): Pump the message loop when this becomes async. |
| + DCHECK_EQ(1UL, dispatcher_host->sent_messages_.size()); |
| + DCHECK_EQ(ServiceWorkerMsg_ServiceWorkerRegistrationError::ID, |
| + dispatcher_host->sent_messages_[0]->type()); |
| +} |
| + |
| +} // namespace content |