| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "content/browser/service_worker/service_worker_dispatcher_host.h" | 5 #include "content/browser/service_worker/service_worker_dispatcher_host.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "content/browser/service_worker/service_worker_context.h" | 9 #include "content/browser/service_worker/service_worker_context_core.h" |
| 10 #include "content/common/service_worker_messages.h" | 10 #include "content/common/service_worker_messages.h" |
| 11 #include "content/public/common/content_switches.h" | 11 #include "content/public/common/content_switches.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 using base::MessageLoop; | 14 using base::MessageLoop; |
| 15 | 15 |
| 16 namespace content { | 16 namespace content { |
| 17 | 17 |
| 18 class ServiceWorkerDispatcherHostTest : public testing::Test { | 18 class ServiceWorkerDispatcherHostTest : public testing::Test { |
| 19 protected: | 19 protected: |
| 20 virtual void SetUp() { | 20 virtual void SetUp() { |
| 21 context_ = new ServiceWorkerContext(base::FilePath(), NULL); | 21 context_.reset(new ServiceWorkerContextCore(base::FilePath(), NULL)); |
| 22 } | 22 } |
| 23 | 23 |
| 24 virtual void TearDown() { | 24 virtual void TearDown() { |
| 25 DCHECK(context_->HasOneRef()); | 25 context_.reset(); |
| 26 context_ = NULL; | |
| 27 } | 26 } |
| 28 | 27 |
| 29 scoped_refptr<ServiceWorkerContext> context_; | 28 scoped_ptr<ServiceWorkerContextCore> context_; |
| 30 }; | 29 }; |
| 31 | 30 |
| 32 namespace { | |
| 33 | |
| 34 static const int kRenderProcessId = 1; | 31 static const int kRenderProcessId = 1; |
| 35 | 32 |
| 36 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { | 33 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { |
| 37 public: | 34 public: |
| 38 TestingServiceWorkerDispatcherHost(int process_id, | 35 TestingServiceWorkerDispatcherHost(int process_id, |
| 39 ServiceWorkerContext* context) | 36 ServiceWorkerContextCore* context) |
| 40 : ServiceWorkerDispatcherHost(process_id, context) {} | 37 : ServiceWorkerDispatcherHost(process_id) { |
| 38 context_ = context->AsWeakPtr(); |
| 39 } |
| 41 | 40 |
| 42 virtual bool Send(IPC::Message* message) OVERRIDE { | 41 virtual bool Send(IPC::Message* message) OVERRIDE { |
| 43 sent_messages_.push_back(message); | 42 sent_messages_.push_back(message); |
| 44 return true; | 43 return true; |
| 45 } | 44 } |
| 46 | 45 |
| 47 ScopedVector<IPC::Message> sent_messages_; | 46 ScopedVector<IPC::Message> sent_messages_; |
| 48 | 47 |
| 49 protected: | 48 protected: |
| 50 virtual ~TestingServiceWorkerDispatcherHost() {} | 49 virtual ~TestingServiceWorkerDispatcherHost() {} |
| 51 }; | 50 }; |
| 52 | 51 |
| 53 } // namespace | |
| 54 | |
| 55 TEST_F(ServiceWorkerDispatcherHostTest, DisabledCausesError) { | 52 TEST_F(ServiceWorkerDispatcherHostTest, DisabledCausesError) { |
| 56 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( | 53 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( |
| 57 switches::kEnableServiceWorker)); | 54 switches::kEnableServiceWorker)); |
| 58 | 55 |
| 59 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | 56 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = |
| 60 new TestingServiceWorkerDispatcherHost(kRenderProcessId, context_); | 57 new TestingServiceWorkerDispatcherHost(kRenderProcessId, context_.get()); |
| 61 | 58 |
| 62 bool handled; | 59 bool handled; |
| 63 dispatcher_host->OnMessageReceived( | 60 dispatcher_host->OnMessageReceived( |
| 64 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), | 61 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), |
| 65 &handled); | 62 &handled); |
| 66 DCHECK(handled); | 63 DCHECK(handled); |
| 67 | 64 |
| 68 // TODO(alecflett): Pump the message loop when this becomes async. | 65 // TODO(alecflett): Pump the message loop when this becomes async. |
| 69 DCHECK_EQ(1UL, dispatcher_host->sent_messages_.size()); | 66 DCHECK_EQ(1UL, dispatcher_host->sent_messages_.size()); |
| 70 DCHECK_EQ( | 67 DCHECK_EQ( |
| 71 static_cast<uint32>(ServiceWorkerMsg_ServiceWorkerRegistrationError::ID), | 68 static_cast<uint32>(ServiceWorkerMsg_ServiceWorkerRegistrationError::ID), |
| 72 dispatcher_host->sent_messages_[0]->type()); | 69 dispatcher_host->sent_messages_[0]->type()); |
| 73 } | 70 } |
| 74 | 71 |
| 75 TEST_F(ServiceWorkerDispatcherHostTest, Enabled) { | 72 TEST_F(ServiceWorkerDispatcherHostTest, Enabled) { |
| 76 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( | 73 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( |
| 77 switches::kEnableServiceWorker)); | 74 switches::kEnableServiceWorker)); |
| 78 CommandLine::ForCurrentProcess()->AppendSwitch( | 75 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 79 switches::kEnableServiceWorker); | 76 switches::kEnableServiceWorker); |
| 80 | 77 |
| 81 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = | 78 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = |
| 82 new TestingServiceWorkerDispatcherHost(kRenderProcessId, context_); | 79 new TestingServiceWorkerDispatcherHost(kRenderProcessId, context_.get()); |
| 83 | 80 |
| 84 bool handled; | 81 bool handled; |
| 85 dispatcher_host->OnMessageReceived( | 82 dispatcher_host->OnMessageReceived( |
| 86 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), | 83 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), |
| 87 &handled); | 84 &handled); |
| 88 DCHECK(handled); | 85 DCHECK(handled); |
| 89 | 86 |
| 90 // TODO(alecflett): Pump the message loop when this becomes async. | 87 // TODO(alecflett): Pump the message loop when this becomes async. |
| 91 DCHECK_EQ(1UL, dispatcher_host->sent_messages_.size()); | 88 DCHECK_EQ(1UL, dispatcher_host->sent_messages_.size()); |
| 92 DCHECK_EQ(static_cast<uint32>(ServiceWorkerMsg_ServiceWorkerRegistered::ID), | 89 DCHECK_EQ(static_cast<uint32>(ServiceWorkerMsg_ServiceWorkerRegistered::ID), |
| 93 dispatcher_host->sent_messages_[0]->type()); | 90 dispatcher_host->sent_messages_[0]->type()); |
| 94 } | 91 } |
| 95 | 92 |
| 93 TEST_F(ServiceWorkerDispatcherHostTest, EarlyContextDeletion) { |
| 94 DCHECK(!CommandLine::ForCurrentProcess()->HasSwitch( |
| 95 switches::kEnableServiceWorker)); |
| 96 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 97 switches::kEnableServiceWorker); |
| 98 |
| 99 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host = |
| 100 new TestingServiceWorkerDispatcherHost(kRenderProcessId, context_.get()); |
| 101 |
| 102 context_.reset(); |
| 103 |
| 104 bool handled; |
| 105 dispatcher_host->OnMessageReceived( |
| 106 ServiceWorkerHostMsg_RegisterServiceWorker(-1, -1, GURL(), GURL()), |
| 107 &handled); |
| 108 DCHECK(handled); |
| 109 |
| 110 // TODO(alecflett): Pump the message loop when this becomes async. |
| 111 DCHECK_EQ(1UL, dispatcher_host->sent_messages_.size()); |
| 112 DCHECK_EQ( |
| 113 static_cast<uint32>(ServiceWorkerMsg_ServiceWorkerRegistrationError::ID), |
| 114 dispatcher_host->sent_messages_[0]->type()); |
| 115 } |
| 116 |
| 96 } // namespace content | 117 } // namespace content |
| OLD | NEW |