| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" |
| 7 #include "content/browser/service_worker/embedded_worker_registry.h" | 7 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 8 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 8 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 9 #include "content/browser/service_worker/service_worker_context_core.h" | 9 #include "content/browser/service_worker/service_worker_context_core.h" |
| 10 #include "content/browser/service_worker/service_worker_dispatcher_host.h" |
| 10 #include "content/browser/service_worker/service_worker_handle.h" | 11 #include "content/browser/service_worker/service_worker_handle.h" |
| 11 #include "content/browser/service_worker/service_worker_registration.h" | 12 #include "content/browser/service_worker/service_worker_registration.h" |
| 12 #include "content/browser/service_worker/service_worker_test_utils.h" | 13 #include "content/browser/service_worker/service_worker_test_utils.h" |
| 13 #include "content/browser/service_worker/service_worker_version.h" | 14 #include "content/browser/service_worker/service_worker_version.h" |
| 14 #include "content/common/service_worker/embedded_worker_messages.h" | 15 #include "content/common/service_worker/embedded_worker_messages.h" |
| 15 #include "content/common/service_worker/service_worker_messages.h" | 16 #include "content/common/service_worker/service_worker_messages.h" |
| 17 #include "content/public/test/mock_resource_context.h" |
| 16 #include "content/public/test/test_browser_thread_bundle.h" | 18 #include "content/public/test/test_browser_thread_bundle.h" |
| 17 #include "ipc/ipc_message.h" | 19 #include "ipc/ipc_message.h" |
| 18 #include "ipc/ipc_test_sink.h" | 20 #include "ipc/ipc_test_sink.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "third_party/WebKit/public/platform/WebServiceWorkerState.h" | 22 #include "third_party/WebKit/public/platform/WebServiceWorkerState.h" |
| 21 | 23 |
| 22 namespace content { | 24 namespace content { |
| 23 | 25 |
| 24 namespace { | 26 namespace { |
| 25 | 27 |
| 26 const int kRenderProcessId = 88; // A dummy ID for testing. | 28 const int kRenderProcessId = 88; // A dummy ID for testing. |
| 27 | 29 |
| 28 void VerifyStateChangedMessage(int expected_handle_id, | 30 void VerifyStateChangedMessage(int expected_handle_id, |
| 29 blink::WebServiceWorkerState expected_state, | 31 blink::WebServiceWorkerState expected_state, |
| 30 const IPC::Message* message) { | 32 const IPC::Message* message) { |
| 31 ASSERT_TRUE(message != NULL); | 33 ASSERT_TRUE(message != NULL); |
| 32 ServiceWorkerMsg_ServiceWorkerStateChanged::Param param; | 34 ServiceWorkerMsg_ServiceWorkerStateChanged::Param param; |
| 33 ASSERT_TRUE(ServiceWorkerMsg_ServiceWorkerStateChanged::Read( | 35 ASSERT_TRUE(ServiceWorkerMsg_ServiceWorkerStateChanged::Read( |
| 34 message, ¶m)); | 36 message, ¶m)); |
| 35 EXPECT_EQ(expected_handle_id, get<1>(param)); | 37 EXPECT_EQ(expected_handle_id, get<1>(param)); |
| 36 EXPECT_EQ(expected_state, get<2>(param)); | 38 EXPECT_EQ(expected_state, get<2>(param)); |
| 37 } | 39 } |
| 38 | 40 |
| 39 } // namespace | 41 } // namespace |
| 40 | 42 |
| 43 class TestingServiceWorkerDispatcherHost : public ServiceWorkerDispatcherHost { |
| 44 public: |
| 45 TestingServiceWorkerDispatcherHost( |
| 46 int process_id, |
| 47 ServiceWorkerContextWrapper* context_wrapper, |
| 48 ResourceContext* resource_context, |
| 49 EmbeddedWorkerTestHelper* helper) |
| 50 : ServiceWorkerDispatcherHost(process_id, nullptr, resource_context), |
| 51 bad_message_received_count_(0), |
| 52 helper_(helper) { |
| 53 Init(context_wrapper); |
| 54 } |
| 55 |
| 56 bool Send(IPC::Message* message) override { return helper_->Send(message); } |
| 57 |
| 58 void BadMessageReceived() override { ++bad_message_received_count_; } |
| 59 |
| 60 int bad_message_received_count_; |
| 61 |
| 62 protected: |
| 63 EmbeddedWorkerTestHelper* helper_; |
| 64 ~TestingServiceWorkerDispatcherHost() override {} |
| 65 }; |
| 66 |
| 41 class ServiceWorkerHandleTest : public testing::Test { | 67 class ServiceWorkerHandleTest : public testing::Test { |
| 42 public: | 68 public: |
| 43 ServiceWorkerHandleTest() | 69 ServiceWorkerHandleTest() |
| 44 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 70 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 45 | 71 |
| 46 void SetUp() override { | 72 void SetUp() override { |
| 47 helper_.reset(new EmbeddedWorkerTestHelper(kRenderProcessId)); | 73 helper_.reset(new EmbeddedWorkerTestHelper(kRenderProcessId)); |
| 48 | 74 |
| 75 dispatcher_host_ = new TestingServiceWorkerDispatcherHost( |
| 76 kRenderProcessId, helper_->context_wrapper(), |
| 77 &resource_context_, helper_.get()); |
| 78 |
| 49 const GURL pattern("http://www.example.com/"); | 79 const GURL pattern("http://www.example.com/"); |
| 50 registration_ = new ServiceWorkerRegistration( | 80 registration_ = new ServiceWorkerRegistration( |
| 51 pattern, | 81 pattern, |
| 52 1L, | 82 1L, |
| 53 helper_->context()->AsWeakPtr()); | 83 helper_->context()->AsWeakPtr()); |
| 54 version_ = new ServiceWorkerVersion( | 84 version_ = new ServiceWorkerVersion( |
| 55 registration_.get(), | 85 registration_.get(), |
| 56 GURL("http://www.example.com/service_worker.js"), | 86 GURL("http://www.example.com/service_worker.js"), |
| 57 1L, | 87 1L, |
| 58 helper_->context()->AsWeakPtr()); | 88 helper_->context()->AsWeakPtr()); |
| 59 | 89 |
| 90 provider_host_.reset(new ServiceWorkerProviderHost( |
| 91 kRenderProcessId, MSG_ROUTING_NONE, 1, |
| 92 helper_->context()->AsWeakPtr(), dispatcher_host_.get())); |
| 93 |
| 60 helper_->SimulateAddProcessToPattern(pattern, kRenderProcessId); | 94 helper_->SimulateAddProcessToPattern(pattern, kRenderProcessId); |
| 61 } | 95 } |
| 62 | 96 |
| 63 void TearDown() override { | 97 void TearDown() override { |
| 98 dispatcher_host_ = NULL; |
| 64 registration_ = NULL; | 99 registration_ = NULL; |
| 65 version_ = NULL; | 100 version_ = NULL; |
| 101 provider_host_.reset(); |
| 66 helper_.reset(); | 102 helper_.reset(); |
| 67 } | 103 } |
| 68 | 104 |
| 69 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); } | 105 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); } |
| 70 | 106 |
| 71 TestBrowserThreadBundle browser_thread_bundle_; | 107 TestBrowserThreadBundle browser_thread_bundle_; |
| 108 MockResourceContext resource_context_; |
| 109 |
| 72 scoped_ptr<EmbeddedWorkerTestHelper> helper_; | 110 scoped_ptr<EmbeddedWorkerTestHelper> helper_; |
| 111 scoped_ptr<ServiceWorkerProviderHost> provider_host_; |
| 73 scoped_refptr<ServiceWorkerRegistration> registration_; | 112 scoped_refptr<ServiceWorkerRegistration> registration_; |
| 74 scoped_refptr<ServiceWorkerVersion> version_; | 113 scoped_refptr<ServiceWorkerVersion> version_; |
| 114 scoped_refptr<TestingServiceWorkerDispatcherHost> dispatcher_host_; |
| 75 | 115 |
| 76 private: | 116 private: |
| 77 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerHandleTest); | 117 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerHandleTest); |
| 78 }; | 118 }; |
| 79 | 119 |
| 80 TEST_F(ServiceWorkerHandleTest, OnVersionStateChanged) { | 120 TEST_F(ServiceWorkerHandleTest, OnVersionStateChanged) { |
| 81 scoped_ptr<ServiceWorkerHandle> handle = | 121 scoped_ptr<ServiceWorkerHandle> handle = |
| 82 ServiceWorkerHandle::Create(helper_->context()->AsWeakPtr(), | 122 ServiceWorkerHandle::Create(helper_->context()->AsWeakPtr(), |
| 83 helper_.get(), | 123 provider_host_->AsWeakPtr(), |
| 84 version_.get()); | 124 version_.get()); |
| 85 | 125 |
| 86 // Start the worker, and then... | 126 // Start the worker, and then... |
| 87 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; | 127 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; |
| 88 version_->StartWorker(CreateReceiverOnCurrentThread(&status)); | 128 version_->StartWorker(CreateReceiverOnCurrentThread(&status)); |
| 89 base::RunLoop().RunUntilIdle(); | 129 base::RunLoop().RunUntilIdle(); |
| 90 EXPECT_EQ(SERVICE_WORKER_OK, status); | 130 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 91 | 131 |
| 92 // ...dispatch install event. | 132 // ...dispatch install event. |
| 93 status = SERVICE_WORKER_ERROR_FAILED; | 133 status = SERVICE_WORKER_ERROR_FAILED; |
| 94 version_->SetStatus(ServiceWorkerVersion::INSTALLING); | 134 version_->SetStatus(ServiceWorkerVersion::INSTALLING); |
| 95 version_->DispatchInstallEvent(-1, CreateReceiverOnCurrentThread(&status)); | 135 version_->DispatchInstallEvent(-1, CreateReceiverOnCurrentThread(&status)); |
| 96 base::RunLoop().RunUntilIdle(); | 136 base::RunLoop().RunUntilIdle(); |
| 97 EXPECT_EQ(SERVICE_WORKER_OK, status); | 137 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 98 | 138 |
| 99 version_->SetStatus(ServiceWorkerVersion::INSTALLED); | 139 version_->SetStatus(ServiceWorkerVersion::INSTALLED); |
| 100 | 140 |
| 101 ASSERT_EQ(4UL, ipc_sink()->message_count()); | 141 ASSERT_EQ(4UL, ipc_sink()->message_count()); |
| 142 ASSERT_EQ(0L, dispatcher_host_->bad_message_received_count_); |
| 102 | 143 |
| 103 // We should be sending 1. StartWorker, | 144 // We should be sending 1. StartWorker, |
| 104 EXPECT_EQ(EmbeddedWorkerMsg_StartWorker::ID, | 145 EXPECT_EQ(EmbeddedWorkerMsg_StartWorker::ID, |
| 105 ipc_sink()->GetMessageAt(0)->type()); | 146 ipc_sink()->GetMessageAt(0)->type()); |
| 106 // 2. StateChanged (state == Installing), | 147 // 2. StateChanged (state == Installing), |
| 107 VerifyStateChangedMessage(handle->handle_id(), | 148 VerifyStateChangedMessage(handle->handle_id(), |
| 108 blink::WebServiceWorkerStateInstalling, | 149 blink::WebServiceWorkerStateInstalling, |
| 109 ipc_sink()->GetMessageAt(1)); | 150 ipc_sink()->GetMessageAt(1)); |
| 110 // 3. SendMessageToWorker (to send InstallEvent), and | 151 // 3. SendMessageToWorker (to send InstallEvent), and |
| 111 EXPECT_EQ(EmbeddedWorkerContextMsg_MessageToWorker::ID, | 152 EXPECT_EQ(EmbeddedWorkerContextMsg_MessageToWorker::ID, |
| 112 ipc_sink()->GetMessageAt(2)->type()); | 153 ipc_sink()->GetMessageAt(2)->type()); |
| 113 // 4. StateChanged (state == Installed). | 154 // 4. StateChanged (state == Installed). |
| 114 VerifyStateChangedMessage(handle->handle_id(), | 155 VerifyStateChangedMessage(handle->handle_id(), |
| 115 blink::WebServiceWorkerStateInstalled, | 156 blink::WebServiceWorkerStateInstalled, |
| 116 ipc_sink()->GetMessageAt(3)); | 157 ipc_sink()->GetMessageAt(3)); |
| 117 } | 158 } |
| 118 | 159 |
| 119 } // namespace content | 160 } // namespace content |
| OLD | NEW |