| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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_context.h" | 5 #include "content/browser/service_worker/service_worker_context.h" |
| 6 | 6 |
| 7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "content/browser/browser_thread_impl.h" | 10 #include "content/browser/browser_thread_impl.h" |
| 11 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 11 #include "content/browser/service_worker/service_worker_context_core.h" | 12 #include "content/browser/service_worker/service_worker_context_core.h" |
| 12 #include "content/browser/service_worker/service_worker_registration.h" | 13 #include "content/browser/service_worker/service_worker_registration.h" |
| 13 #include "content/public/test/test_browser_thread_bundle.h" | 14 #include "content/public/test/test_browser_thread_bundle.h" |
| 14 #include "content/public/test/test_utils.h" | 15 #include "content/public/test/test_utils.h" |
| 16 #include "ipc/ipc_test_sink.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 18 |
| 17 namespace content { | 19 namespace content { |
| 18 | 20 |
| 19 namespace { | 21 namespace { |
| 20 | 22 |
| 21 void SaveResponseCallback(bool* called, | 23 void SaveResponseCallback(bool* called, |
| 22 int64* store_result, | 24 int64* store_result, |
| 23 ServiceWorkerRegistrationStatus status, | 25 ServiceWorkerRegistrationStatus status, |
| 24 int64 result) { | 26 int64 result) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 43 | 45 |
| 44 } // namespace | 46 } // namespace |
| 45 | 47 |
| 46 class ServiceWorkerContextTest : public testing::Test { | 48 class ServiceWorkerContextTest : public testing::Test { |
| 47 public: | 49 public: |
| 48 ServiceWorkerContextTest() | 50 ServiceWorkerContextTest() |
| 49 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 51 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 50 | 52 |
| 51 virtual void SetUp() OVERRIDE { | 53 virtual void SetUp() OVERRIDE { |
| 52 context_.reset(new ServiceWorkerContextCore(base::FilePath(), NULL)); | 54 context_.reset(new ServiceWorkerContextCore(base::FilePath(), NULL)); |
| 55 |
| 56 int provider_id = 123; |
| 57 render_process_id_ = 99; |
| 58 context_->embedded_worker_registry()->AddChildProcessSender( |
| 59 render_process_id_, &sink_); |
| 60 scoped_ptr<ServiceWorkerProviderHost> provider_host( |
| 61 new ServiceWorkerProviderHost(render_process_id_, provider_id)); |
| 62 context_->AddProviderHost(provider_host.Pass()); |
| 63 |
| 64 provider_host_ = context_->GetProviderHost(render_process_id_, provider_id); |
| 53 } | 65 } |
| 54 | 66 |
| 55 virtual void TearDown() OVERRIDE { context_.reset(); } | 67 virtual void TearDown() OVERRIDE { |
| 68 context_.reset(); |
| 69 context_->embedded_worker_registry()->RemoveChildProcessSender( |
| 70 provider_host_->process_id()); |
| 71 context_->RemoveProviderHost(provider_host_->process_id(), |
| 72 provider_host_->provider_id()); |
| 73 } |
| 74 |
| 75 int GetWorkerInstanceId() { |
| 76 EXPECT_EQ(1U, |
| 77 context_->embedded_worker_registry()->worker_instances().size()); |
| 78 return context_->embedded_worker_registry() |
| 79 ->worker_instances() |
| 80 .begin() |
| 81 ->first; |
| 82 } |
| 56 | 83 |
| 57 protected: | 84 protected: |
| 58 TestBrowserThreadBundle browser_thread_bundle_; | 85 TestBrowserThreadBundle browser_thread_bundle_; |
| 86 IPC::TestSink sink_; |
| 59 scoped_ptr<ServiceWorkerContextCore> context_; | 87 scoped_ptr<ServiceWorkerContextCore> context_; |
| 88 ServiceWorkerProviderHost* provider_host_; |
| 89 int render_process_id_; |
| 60 }; | 90 }; |
| 61 | 91 |
| 62 void RegistrationCallback( | 92 void RegistrationCallback( |
| 63 scoped_refptr<ServiceWorkerRegistration>* registration, | 93 scoped_refptr<ServiceWorkerRegistration>* registration, |
| 64 const scoped_refptr<ServiceWorkerRegistration>& result) { | 94 const scoped_refptr<ServiceWorkerRegistration>& result) { |
| 65 *registration = result; | 95 *registration = result; |
| 66 } | 96 } |
| 67 | 97 |
| 68 // Make sure basic registration is working. | 98 // Make sure basic registration is working. |
| 69 TEST_F(ServiceWorkerContextTest, Register) { | 99 TEST_F(ServiceWorkerContextTest, Register) { |
| 70 int64 registration_id = -1L; | 100 int64 registration_id = -1L; |
| 71 bool called = false; | 101 bool called = false; |
| 72 context_->RegisterServiceWorker( | 102 context_->RegisterServiceWorker( |
| 73 GURL("http://www.example.com/*"), | 103 GURL("http://www.example.com/*"), |
| 74 GURL("http://www.example.com/service_worker.js"), | 104 GURL("http://www.example.com/service_worker.js"), |
| 105 provider_host_, |
| 75 MakeRegisteredCallback(&called, ®istration_id)); | 106 MakeRegisteredCallback(&called, ®istration_id)); |
| 76 | 107 |
| 108 LOG(ERROR) << "RunUntilIdle..."; |
| 77 ASSERT_FALSE(called); | 109 ASSERT_FALSE(called); |
| 78 base::RunLoop().RunUntilIdle(); | 110 base::RunLoop().RunUntilIdle(); |
| 79 ASSERT_TRUE(called); | |
| 80 | 111 |
| 81 ASSERT_NE(-1L, registration_id); | 112 EXPECT_EQ(1UL, sink_.message_count()); |
| 113 |
| 114 LOG(ERROR) << "Claiming that OnWorkerStarted()"; |
| 115 context_->embedded_worker_registry()->OnWorkerStarted( |
| 116 render_process_id_, -1, GetWorkerInstanceId()); |
| 117 EXPECT_TRUE(called); |
| 118 |
| 119 EXPECT_NE(-1L, registration_id); |
| 82 } | 120 } |
| 83 | 121 |
| 84 // Make sure registrations are cleaned up when they are unregistered. | 122 // Make sure registrations are cleaned up when they are unregistered. |
| 85 TEST_F(ServiceWorkerContextTest, Unregister) { | 123 TEST_F(ServiceWorkerContextTest, Unregister) { |
| 86 GURL pattern("http://www.example.com/*"); | 124 GURL pattern("http://www.example.com/*"); |
| 87 | 125 |
| 88 bool called = false; | 126 bool called = false; |
| 89 int64 registration_id = -1L; | 127 int64 registration_id = -1L; |
| 90 context_->RegisterServiceWorker( | 128 context_->RegisterServiceWorker( |
| 91 pattern, | 129 pattern, |
| 92 GURL("http://www.example.com/service_worker.js"), | 130 GURL("http://www.example.com/service_worker.js"), |
| 131 provider_host_, |
| 93 MakeRegisteredCallback(&called, ®istration_id)); | 132 MakeRegisteredCallback(&called, ®istration_id)); |
| 94 | 133 |
| 95 ASSERT_FALSE(called); | 134 ASSERT_FALSE(called); |
| 96 base::RunLoop().RunUntilIdle(); | 135 base::RunLoop().RunUntilIdle(); |
| 97 ASSERT_TRUE(called); | 136 ASSERT_TRUE(called); |
| 98 | 137 |
| 99 called = false; | 138 called = false; |
| 100 context_->UnregisterServiceWorker(pattern, MakeUnregisteredCallback(&called)); | 139 context_->UnregisterServiceWorker( |
| 140 pattern, provider_host_, MakeUnregisteredCallback(&called)); |
| 101 | 141 |
| 102 ASSERT_FALSE(called); | 142 ASSERT_FALSE(called); |
| 103 base::RunLoop().RunUntilIdle(); | 143 base::RunLoop().RunUntilIdle(); |
| 104 ASSERT_TRUE(called); | 144 ASSERT_TRUE(called); |
| 105 } | 145 } |
| 106 | 146 |
| 107 // Make sure that when a new registration replaces an existing | 147 // Make sure that when a new registration replaces an existing |
| 108 // registration, that the old one is cleaned up. | 148 // registration, that the old one is cleaned up. |
| 109 TEST_F(ServiceWorkerContextTest, RegisterNewScript) { | 149 TEST_F(ServiceWorkerContextTest, RegisterNewScript) { |
| 110 GURL pattern("http://www.example.com/*"); | 150 GURL pattern("http://www.example.com/*"); |
| 111 | 151 |
| 112 bool called = false; | 152 bool called = false; |
| 113 int64 old_registration_id = -1L; | 153 int64 old_registration_id = -1L; |
| 114 context_->RegisterServiceWorker( | 154 context_->RegisterServiceWorker( |
| 115 pattern, | 155 pattern, |
| 116 GURL("http://www.example.com/service_worker.js"), | 156 GURL("http://www.example.com/service_worker.js"), |
| 157 provider_host_, |
| 117 MakeRegisteredCallback(&called, &old_registration_id)); | 158 MakeRegisteredCallback(&called, &old_registration_id)); |
| 118 | 159 |
| 119 ASSERT_FALSE(called); | 160 ASSERT_FALSE(called); |
| 120 base::RunLoop().RunUntilIdle(); | 161 base::RunLoop().RunUntilIdle(); |
| 121 ASSERT_TRUE(called); | 162 ASSERT_TRUE(called); |
| 122 | 163 |
| 123 called = false; | 164 called = false; |
| 124 int64 new_registration_id = -1L; | 165 int64 new_registration_id = -1L; |
| 125 context_->RegisterServiceWorker( | 166 context_->RegisterServiceWorker( |
| 126 pattern, | 167 pattern, |
| 127 GURL("http://www.example.com/service_worker_new.js"), | 168 GURL("http://www.example.com/service_worker_new.js"), |
| 169 provider_host_, |
| 128 MakeRegisteredCallback(&called, &new_registration_id)); | 170 MakeRegisteredCallback(&called, &new_registration_id)); |
| 129 | 171 |
| 130 ASSERT_FALSE(called); | 172 ASSERT_FALSE(called); |
| 131 base::RunLoop().RunUntilIdle(); | 173 base::RunLoop().RunUntilIdle(); |
| 132 ASSERT_TRUE(called); | 174 ASSERT_TRUE(called); |
| 133 | 175 |
| 134 ASSERT_NE(old_registration_id, new_registration_id); | 176 ASSERT_NE(old_registration_id, new_registration_id); |
| 135 } | 177 } |
| 136 | 178 |
| 137 // Make sure that when registering a duplicate pattern+script_url | 179 // Make sure that when registering a duplicate pattern+script_url |
| 138 // combination, that the same registration is used. | 180 // combination, that the same registration is used. |
| 139 TEST_F(ServiceWorkerContextTest, RegisterDuplicateScript) { | 181 TEST_F(ServiceWorkerContextTest, RegisterDuplicateScript) { |
| 140 GURL pattern("http://www.example.com/*"); | 182 GURL pattern("http://www.example.com/*"); |
| 141 GURL script_url("http://www.example.com/service_worker.js"); | 183 GURL script_url("http://www.example.com/service_worker.js"); |
| 142 | 184 |
| 143 bool called = false; | 185 bool called = false; |
| 144 int64 old_registration_id = -1L; | 186 int64 old_registration_id = -1L; |
| 145 context_->RegisterServiceWorker( | 187 context_->RegisterServiceWorker( |
| 146 pattern, | 188 pattern, |
| 147 script_url, | 189 script_url, |
| 190 provider_host_, |
| 148 MakeRegisteredCallback(&called, &old_registration_id)); | 191 MakeRegisteredCallback(&called, &old_registration_id)); |
| 149 | 192 |
| 150 ASSERT_FALSE(called); | 193 ASSERT_FALSE(called); |
| 151 base::RunLoop().RunUntilIdle(); | 194 base::RunLoop().RunUntilIdle(); |
| 152 ASSERT_TRUE(called); | 195 ASSERT_TRUE(called); |
| 153 | 196 |
| 154 called = false; | 197 called = false; |
| 155 int64 new_registration_id = -1L; | 198 int64 new_registration_id = -1L; |
| 156 context_->RegisterServiceWorker( | 199 context_->RegisterServiceWorker( |
| 157 pattern, | 200 pattern, |
| 158 script_url, | 201 script_url, |
| 202 provider_host_, |
| 159 MakeRegisteredCallback(&called, &new_registration_id)); | 203 MakeRegisteredCallback(&called, &new_registration_id)); |
| 160 | 204 |
| 161 ASSERT_FALSE(called); | 205 ASSERT_FALSE(called); |
| 162 base::RunLoop().RunUntilIdle(); | 206 base::RunLoop().RunUntilIdle(); |
| 163 ASSERT_TRUE(called); | 207 ASSERT_TRUE(called); |
| 164 | 208 |
| 165 ASSERT_EQ(old_registration_id, new_registration_id); | 209 ASSERT_EQ(old_registration_id, new_registration_id); |
| 166 } | 210 } |
| 167 | 211 |
| 168 } // namespace content | 212 } // namespace content |
| OLD | NEW |