| 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" |
| 12 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 11 #include "content/browser/service_worker/service_worker_context_core.h" | 13 #include "content/browser/service_worker/service_worker_context_core.h" |
| 12 #include "content/browser/service_worker/service_worker_registration.h" | 14 #include "content/browser/service_worker/service_worker_registration.h" |
| 13 #include "content/public/test/test_browser_thread_bundle.h" | 15 #include "content/public/test/test_browser_thread_bundle.h" |
| 14 #include "content/public/test/test_utils.h" | 16 #include "content/public/test/test_utils.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 |
| (...skipping 22 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 helper_.reset(new EmbeddedWorkerTestHelper(context_.get())); |
| 56 |
| 57 render_process_id_ = 99; |
| 58 helper_->SimulateCreateWorker(render_process_id_); |
| 53 } | 59 } |
| 54 | 60 |
| 55 virtual void TearDown() OVERRIDE { context_.reset(); } | 61 virtual void TearDown() OVERRIDE { |
| 62 helper_.reset(); |
| 63 context_.reset(); |
| 64 } |
| 56 | 65 |
| 57 protected: | 66 protected: |
| 58 TestBrowserThreadBundle browser_thread_bundle_; | 67 TestBrowserThreadBundle browser_thread_bundle_; |
| 59 scoped_ptr<ServiceWorkerContextCore> context_; | 68 scoped_ptr<ServiceWorkerContextCore> context_; |
| 69 scoped_ptr<EmbeddedWorkerTestHelper> helper_; |
| 70 int render_process_id_; |
| 60 }; | 71 }; |
| 61 | 72 |
| 62 void RegistrationCallback( | 73 void RegistrationCallback( |
| 63 scoped_refptr<ServiceWorkerRegistration>* registration, | 74 scoped_refptr<ServiceWorkerRegistration>* registration, |
| 64 const scoped_refptr<ServiceWorkerRegistration>& result) { | 75 const scoped_refptr<ServiceWorkerRegistration>& result) { |
| 65 *registration = result; | 76 *registration = result; |
| 66 } | 77 } |
| 67 | 78 |
| 68 // Make sure basic registration is working. | 79 // Make sure basic registration is working. |
| 69 TEST_F(ServiceWorkerContextTest, Register) { | 80 TEST_F(ServiceWorkerContextTest, Register) { |
| 70 int64 registration_id = -1L; | 81 int64 registration_id = -1L; |
| 71 bool called = false; | 82 bool called = false; |
| 72 context_->RegisterServiceWorker( | 83 context_->RegisterServiceWorker( |
| 73 GURL("http://www.example.com/*"), | 84 GURL("http://www.example.com/*"), |
| 74 GURL("http://www.example.com/service_worker.js"), | 85 GURL("http://www.example.com/service_worker.js"), |
| 86 render_process_id_, |
| 75 MakeRegisteredCallback(&called, ®istration_id)); | 87 MakeRegisteredCallback(&called, ®istration_id)); |
| 76 | 88 |
| 77 ASSERT_FALSE(called); | 89 ASSERT_FALSE(called); |
| 78 base::RunLoop().RunUntilIdle(); | 90 base::RunLoop().RunUntilIdle(); |
| 79 ASSERT_TRUE(called); | 91 EXPECT_TRUE(called); |
| 80 | 92 |
| 81 ASSERT_NE(-1L, registration_id); | 93 EXPECT_EQ(1UL, helper_->ipc_sink()->message_count()); |
| 94 EXPECT_NE(-1L, registration_id); |
| 82 } | 95 } |
| 83 | 96 |
| 84 // Make sure registrations are cleaned up when they are unregistered. | 97 // Make sure registrations are cleaned up when they are unregistered. |
| 85 TEST_F(ServiceWorkerContextTest, Unregister) { | 98 TEST_F(ServiceWorkerContextTest, Unregister) { |
| 86 GURL pattern("http://www.example.com/*"); | 99 GURL pattern("http://www.example.com/*"); |
| 87 | 100 |
| 88 bool called = false; | 101 bool called = false; |
| 89 int64 registration_id = -1L; | 102 int64 registration_id = -1L; |
| 90 context_->RegisterServiceWorker( | 103 context_->RegisterServiceWorker( |
| 91 pattern, | 104 pattern, |
| 92 GURL("http://www.example.com/service_worker.js"), | 105 GURL("http://www.example.com/service_worker.js"), |
| 106 render_process_id_, |
| 93 MakeRegisteredCallback(&called, ®istration_id)); | 107 MakeRegisteredCallback(&called, ®istration_id)); |
| 94 | 108 |
| 95 ASSERT_FALSE(called); | 109 ASSERT_FALSE(called); |
| 96 base::RunLoop().RunUntilIdle(); | 110 base::RunLoop().RunUntilIdle(); |
| 97 ASSERT_TRUE(called); | 111 ASSERT_TRUE(called); |
| 98 | 112 |
| 99 called = false; | 113 called = false; |
| 100 context_->UnregisterServiceWorker(pattern, MakeUnregisteredCallback(&called)); | 114 context_->UnregisterServiceWorker( |
| 115 pattern, render_process_id_, MakeUnregisteredCallback(&called)); |
| 101 | 116 |
| 102 ASSERT_FALSE(called); | 117 ASSERT_FALSE(called); |
| 103 base::RunLoop().RunUntilIdle(); | 118 base::RunLoop().RunUntilIdle(); |
| 104 ASSERT_TRUE(called); | 119 ASSERT_TRUE(called); |
| 105 } | 120 } |
| 106 | 121 |
| 107 // Make sure that when a new registration replaces an existing | 122 // Make sure that when a new registration replaces an existing |
| 108 // registration, that the old one is cleaned up. | 123 // registration, that the old one is cleaned up. |
| 109 TEST_F(ServiceWorkerContextTest, RegisterNewScript) { | 124 TEST_F(ServiceWorkerContextTest, RegisterNewScript) { |
| 110 GURL pattern("http://www.example.com/*"); | 125 GURL pattern("http://www.example.com/*"); |
| 111 | 126 |
| 112 bool called = false; | 127 bool called = false; |
| 113 int64 old_registration_id = -1L; | 128 int64 old_registration_id = -1L; |
| 114 context_->RegisterServiceWorker( | 129 context_->RegisterServiceWorker( |
| 115 pattern, | 130 pattern, |
| 116 GURL("http://www.example.com/service_worker.js"), | 131 GURL("http://www.example.com/service_worker.js"), |
| 132 render_process_id_, |
| 117 MakeRegisteredCallback(&called, &old_registration_id)); | 133 MakeRegisteredCallback(&called, &old_registration_id)); |
| 118 | 134 |
| 119 ASSERT_FALSE(called); | 135 ASSERT_FALSE(called); |
| 120 base::RunLoop().RunUntilIdle(); | 136 base::RunLoop().RunUntilIdle(); |
| 121 ASSERT_TRUE(called); | 137 ASSERT_TRUE(called); |
| 122 | 138 |
| 123 called = false; | 139 called = false; |
| 124 int64 new_registration_id = -1L; | 140 int64 new_registration_id = -1L; |
| 125 context_->RegisterServiceWorker( | 141 context_->RegisterServiceWorker( |
| 126 pattern, | 142 pattern, |
| 127 GURL("http://www.example.com/service_worker_new.js"), | 143 GURL("http://www.example.com/service_worker_new.js"), |
| 144 render_process_id_, |
| 128 MakeRegisteredCallback(&called, &new_registration_id)); | 145 MakeRegisteredCallback(&called, &new_registration_id)); |
| 129 | 146 |
| 130 ASSERT_FALSE(called); | 147 ASSERT_FALSE(called); |
| 131 base::RunLoop().RunUntilIdle(); | 148 base::RunLoop().RunUntilIdle(); |
| 132 ASSERT_TRUE(called); | 149 ASSERT_TRUE(called); |
| 133 | 150 |
| 134 ASSERT_NE(old_registration_id, new_registration_id); | 151 ASSERT_NE(old_registration_id, new_registration_id); |
| 135 } | 152 } |
| 136 | 153 |
| 137 // Make sure that when registering a duplicate pattern+script_url | 154 // Make sure that when registering a duplicate pattern+script_url |
| 138 // combination, that the same registration is used. | 155 // combination, that the same registration is used. |
| 139 TEST_F(ServiceWorkerContextTest, RegisterDuplicateScript) { | 156 TEST_F(ServiceWorkerContextTest, RegisterDuplicateScript) { |
| 140 GURL pattern("http://www.example.com/*"); | 157 GURL pattern("http://www.example.com/*"); |
| 141 GURL script_url("http://www.example.com/service_worker.js"); | 158 GURL script_url("http://www.example.com/service_worker.js"); |
| 142 | 159 |
| 143 bool called = false; | 160 bool called = false; |
| 144 int64 old_registration_id = -1L; | 161 int64 old_registration_id = -1L; |
| 145 context_->RegisterServiceWorker( | 162 context_->RegisterServiceWorker( |
| 146 pattern, | 163 pattern, |
| 147 script_url, | 164 script_url, |
| 165 render_process_id_, |
| 148 MakeRegisteredCallback(&called, &old_registration_id)); | 166 MakeRegisteredCallback(&called, &old_registration_id)); |
| 149 | 167 |
| 150 ASSERT_FALSE(called); | 168 ASSERT_FALSE(called); |
| 151 base::RunLoop().RunUntilIdle(); | 169 base::RunLoop().RunUntilIdle(); |
| 152 ASSERT_TRUE(called); | 170 ASSERT_TRUE(called); |
| 153 | 171 |
| 154 called = false; | 172 called = false; |
| 155 int64 new_registration_id = -1L; | 173 int64 new_registration_id = -1L; |
| 156 context_->RegisterServiceWorker( | 174 context_->RegisterServiceWorker( |
| 157 pattern, | 175 pattern, |
| 158 script_url, | 176 script_url, |
| 177 render_process_id_, |
| 159 MakeRegisteredCallback(&called, &new_registration_id)); | 178 MakeRegisteredCallback(&called, &new_registration_id)); |
| 160 | 179 |
| 161 ASSERT_FALSE(called); | 180 ASSERT_FALSE(called); |
| 162 base::RunLoop().RunUntilIdle(); | 181 base::RunLoop().RunUntilIdle(); |
| 163 ASSERT_TRUE(called); | 182 ASSERT_TRUE(called); |
| 164 | 183 |
| 165 ASSERT_EQ(old_registration_id, new_registration_id); | 184 ASSERT_EQ(old_registration_id, new_registration_id); |
| 166 } | 185 } |
| 167 | 186 |
| 168 } // namespace content | 187 } // namespace content |
| OLD | NEW |