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..bf7bd9d0edfc18b0c97e2f3461957a36fd7e0a7c |
--- /dev/null |
+++ b/content/browser/service_worker/service_worker_dispatcher_host_unittest.cc |
@@ -0,0 +1,106 @@ |
+// 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() { |
+ ASSERT_TRUE(dir_.CreateUniqueTempDir()); |
+ context_ = new ServiceWorkerContext(dir_.path(), nullptr); |
+ } |
+ |
+ 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 |