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_storage.h" | |
6 | |
7 #include "base/files/scoped_temp_dir.h" | 5 #include "base/files/scoped_temp_dir.h" |
8 #include "base/logging.h" | 6 #include "base/logging.h" |
9 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
10 #include "content/browser/browser_thread_impl.h" | 8 #include "content/browser/browser_thread_impl.h" |
| 9 #include "content/browser/service_worker/service_worker_job_coordinator.h" |
11 #include "content/browser/service_worker/service_worker_registration.h" | 10 #include "content/browser/service_worker/service_worker_registration.h" |
| 11 #include "content/browser/service_worker/service_worker_registration_status.h" |
12 #include "content/public/test/test_browser_thread_bundle.h" | 12 #include "content/public/test/test_browser_thread_bundle.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
| 15 // Unit tests for testing all job registration tasks. |
15 namespace content { | 16 namespace content { |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 void SaveRegistrationCallback( | 20 void SaveRegistrationCallback( |
20 ServiceWorkerRegistrationStatus expected_status, | 21 ServiceWorkerRegistrationStatus expected_status, |
21 bool* called, | 22 bool* called, |
22 scoped_refptr<ServiceWorkerRegistration>* registration, | 23 scoped_refptr<ServiceWorkerRegistration>* registration, |
23 ServiceWorkerRegistrationStatus status, | 24 ServiceWorkerRegistrationStatus status, |
24 const scoped_refptr<ServiceWorkerRegistration>& result) { | 25 const scoped_refptr<ServiceWorkerRegistration>& result) { |
(...skipping 14 matching lines...) Expand all Loading... |
39 EXPECT_EQ(expected_status, status); | 40 EXPECT_EQ(expected_status, status); |
40 *called = true; | 41 *called = true; |
41 *registration = result; | 42 *registration = result; |
42 } | 43 } |
43 | 44 |
44 // Creates a callback which both keeps track of if it's been called, | 45 // Creates a callback which both keeps track of if it's been called, |
45 // as well as the resulting registration. Whent the callback is fired, | 46 // as well as the resulting registration. Whent the callback is fired, |
46 // it ensures that the resulting status matches the expectation. | 47 // it ensures that the resulting status matches the expectation. |
47 // 'called' is useful for making sure a sychronous callback is or | 48 // 'called' is useful for making sure a sychronous callback is or |
48 // isn't called. | 49 // isn't called. |
49 ServiceWorkerStorage::RegistrationCallback SaveRegistration( | 50 ServiceWorkerRegisterJob::RegistrationCallback SaveRegistration( |
50 ServiceWorkerRegistrationStatus expected_status, | 51 ServiceWorkerRegistrationStatus expected_status, |
51 bool* called, | 52 bool* called, |
52 scoped_refptr<ServiceWorkerRegistration>* registration) { | 53 scoped_refptr<ServiceWorkerRegistration>* registration) { |
53 *called = false; | 54 *called = false; |
54 return base::Bind( | 55 return base::Bind( |
55 &SaveRegistrationCallback, expected_status, called, registration); | 56 &SaveRegistrationCallback, expected_status, called, registration); |
56 } | 57 } |
57 | 58 |
58 ServiceWorkerStorage::FindRegistrationCallback SaveFoundRegistration( | 59 ServiceWorkerStorage::FindRegistrationCallback SaveFoundRegistration( |
59 bool expected_found, | 60 bool expected_found, |
60 ServiceWorkerRegistrationStatus expected_status, | 61 ServiceWorkerRegistrationStatus expected_status, |
61 bool* called, | 62 bool* called, |
62 scoped_refptr<ServiceWorkerRegistration>* registration) { | 63 scoped_refptr<ServiceWorkerRegistration>* registration) { |
63 *called = false; | 64 *called = false; |
64 return base::Bind(&SaveFoundRegistrationCallback, | 65 return base::Bind(&SaveFoundRegistrationCallback, |
65 expected_found, | 66 expected_found, |
66 expected_status, | 67 expected_status, |
67 called, | 68 called, |
68 registration); | 69 registration); |
69 } | 70 } |
70 | 71 |
71 void SaveUnregistrationCallback(ServiceWorkerRegistrationStatus expected_status, | 72 void SaveUnregistrationCallback(ServiceWorkerRegistrationStatus expected_status, |
72 bool* called, | 73 bool* called, |
73 ServiceWorkerRegistrationStatus status) { | 74 ServiceWorkerRegistrationStatus status) { |
74 EXPECT_EQ(expected_status, status); | 75 EXPECT_EQ(expected_status, status); |
75 *called = true; | 76 *called = true; |
76 } | 77 } |
77 | 78 |
78 ServiceWorkerStorage::UnregistrationCallback SaveUnregistration( | 79 ServiceWorkerRegisterJob::UnregistrationCallback SaveUnregistration( |
79 ServiceWorkerRegistrationStatus expected_status, | 80 ServiceWorkerRegistrationStatus expected_status, |
80 bool* called) { | 81 bool* called) { |
81 *called = false; | 82 *called = false; |
82 return base::Bind(&SaveUnregistrationCallback, expected_status, called); | 83 return base::Bind(&SaveUnregistrationCallback, expected_status, called); |
83 } | 84 } |
84 | 85 |
85 } // namespace | 86 } // namespace |
86 | 87 |
87 class ServiceWorkerStorageTest : public testing::Test { | 88 class ServiceWorkerJobTest : public testing::Test { |
88 public: | 89 public: |
89 ServiceWorkerStorageTest() | 90 ServiceWorkerJobTest() |
90 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 91 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
91 | 92 |
92 virtual void SetUp() OVERRIDE { | 93 virtual void SetUp() OVERRIDE { |
93 storage_.reset(new ServiceWorkerStorage(base::FilePath(), NULL)); | 94 storage_.reset(new ServiceWorkerStorage(base::FilePath(), NULL)); |
| 95 job_coordinator_.reset(new ServiceWorkerJobCoordinator(storage_.get())); |
94 } | 96 } |
95 | 97 |
96 virtual void TearDown() OVERRIDE { storage_.reset(); } | 98 virtual void TearDown() OVERRIDE { storage_.reset(); } |
97 | 99 |
98 protected: | 100 protected: |
99 TestBrowserThreadBundle browser_thread_bundle_; | 101 TestBrowserThreadBundle browser_thread_bundle_; |
100 scoped_ptr<ServiceWorkerStorage> storage_; | 102 scoped_ptr<ServiceWorkerStorage> storage_; |
| 103 scoped_ptr<ServiceWorkerJobCoordinator> job_coordinator_; |
101 }; | 104 }; |
102 | 105 |
103 TEST_F(ServiceWorkerStorageTest, PatternMatches) { | 106 TEST_F(ServiceWorkerJobTest, SameDocumentSameRegistration) { |
104 ASSERT_TRUE(ServiceWorkerStorage::PatternMatches( | |
105 GURL("http://www.example.com/*"), GURL("http://www.example.com/"))); | |
106 ASSERT_TRUE(ServiceWorkerStorage::PatternMatches( | |
107 GURL("http://www.example.com/*"), | |
108 GURL("http://www.example.com/page.html"))); | |
109 | |
110 ASSERT_FALSE(ServiceWorkerStorage::PatternMatches( | |
111 GURL("http://www.example.com/*"), GURL("https://www.example.com/"))); | |
112 ASSERT_FALSE(ServiceWorkerStorage::PatternMatches( | |
113 GURL("http://www.example.com/*"), | |
114 GURL("https://www.example.com/page.html"))); | |
115 | |
116 ASSERT_FALSE(ServiceWorkerStorage::PatternMatches( | |
117 GURL("http://www.example.com/*"), GURL("http://www.foo.com/"))); | |
118 ASSERT_FALSE(ServiceWorkerStorage::PatternMatches( | |
119 GURL("http://www.example.com/*"), GURL("https://www.foo.com/page.html"))); | |
120 } | |
121 | |
122 TEST_F(ServiceWorkerStorageTest, SameDocumentSameRegistration) { | |
123 scoped_refptr<ServiceWorkerRegistration> original_registration; | 107 scoped_refptr<ServiceWorkerRegistration> original_registration; |
124 bool called; | 108 bool called; |
125 storage_->Register( | 109 job_coordinator_->Register( |
126 GURL("http://www.example.com/*"), | 110 GURL("http://www.example.com/*"), |
127 GURL("http://www.example.com/service_worker.js"), | 111 GURL("http://www.example.com/service_worker.js"), |
128 SaveRegistration(REGISTRATION_OK, &called, &original_registration)); | 112 SaveRegistration(REGISTRATION_OK, &called, &original_registration)); |
129 EXPECT_FALSE(called); | 113 EXPECT_FALSE(called); |
130 base::RunLoop().RunUntilIdle(); | 114 base::RunLoop().RunUntilIdle(); |
131 EXPECT_TRUE(called); | 115 EXPECT_TRUE(called); |
132 | 116 |
133 scoped_refptr<ServiceWorkerRegistration> registration1; | 117 scoped_refptr<ServiceWorkerRegistration> registration1; |
134 storage_->FindRegistrationForDocument( | 118 storage_->FindRegistrationForDocument( |
135 GURL("http://www.example.com/"), | 119 GURL("http://www.example.com/"), |
136 SaveFoundRegistration(true, REGISTRATION_OK, &called, ®istration1)); | 120 SaveFoundRegistration(true, REGISTRATION_OK, &called, ®istration1)); |
137 scoped_refptr<ServiceWorkerRegistration> registration2; | 121 scoped_refptr<ServiceWorkerRegistration> registration2; |
138 storage_->FindRegistrationForDocument( | 122 storage_->FindRegistrationForDocument( |
139 GURL("http://www.example.com/"), | 123 GURL("http://www.example.com/"), |
140 SaveFoundRegistration(true, REGISTRATION_OK, &called, ®istration2)); | 124 SaveFoundRegistration(true, REGISTRATION_OK, &called, ®istration2)); |
141 | 125 |
142 ServiceWorkerRegistration* null_registration(NULL); | 126 ServiceWorkerRegistration* null_registration(NULL); |
143 ASSERT_EQ(null_registration, registration1); | 127 ASSERT_EQ(null_registration, registration1); |
144 ASSERT_EQ(null_registration, registration2); | 128 ASSERT_EQ(null_registration, registration2); |
145 EXPECT_FALSE(called); | 129 EXPECT_FALSE(called); |
146 base::RunLoop().RunUntilIdle(); | 130 base::RunLoop().RunUntilIdle(); |
147 EXPECT_TRUE(called); | 131 EXPECT_TRUE(called); |
148 ASSERT_NE(null_registration, registration1); | 132 ASSERT_NE(null_registration, registration1); |
149 ASSERT_NE(null_registration, registration2); | 133 ASSERT_NE(null_registration, registration2); |
150 | 134 |
151 ASSERT_EQ(registration1, registration2); | 135 ASSERT_EQ(registration1, registration2); |
152 } | 136 } |
153 | 137 |
154 TEST_F(ServiceWorkerStorageTest, SameMatchSameRegistration) { | 138 TEST_F(ServiceWorkerJobTest, SameMatchSameRegistration) { |
155 bool called; | 139 bool called; |
156 scoped_refptr<ServiceWorkerRegistration> original_registration; | 140 scoped_refptr<ServiceWorkerRegistration> original_registration; |
157 storage_->Register( | 141 job_coordinator_->Register( |
158 GURL("http://www.example.com/*"), | 142 GURL("http://www.example.com/*"), |
159 GURL("http://www.example.com/service_worker.js"), | 143 GURL("http://www.example.com/service_worker.js"), |
160 SaveRegistration(REGISTRATION_OK, &called, &original_registration)); | 144 SaveRegistration(REGISTRATION_OK, &called, &original_registration)); |
161 EXPECT_FALSE(called); | 145 EXPECT_FALSE(called); |
162 base::RunLoop().RunUntilIdle(); | 146 base::RunLoop().RunUntilIdle(); |
163 EXPECT_TRUE(called); | 147 EXPECT_TRUE(called); |
164 ASSERT_NE(static_cast<ServiceWorkerRegistration*>(NULL), | 148 ASSERT_NE(static_cast<ServiceWorkerRegistration*>(NULL), |
165 original_registration.get()); | 149 original_registration.get()); |
166 | 150 |
167 scoped_refptr<ServiceWorkerRegistration> registration1; | 151 scoped_refptr<ServiceWorkerRegistration> registration1; |
168 storage_->FindRegistrationForDocument( | 152 storage_->FindRegistrationForDocument( |
169 GURL("http://www.example.com/one"), | 153 GURL("http://www.example.com/one"), |
170 SaveFoundRegistration(true, REGISTRATION_OK, &called, ®istration1)); | 154 SaveFoundRegistration(true, REGISTRATION_OK, &called, ®istration1)); |
171 | 155 |
172 EXPECT_FALSE(called); | 156 EXPECT_FALSE(called); |
173 base::RunLoop().RunUntilIdle(); | 157 base::RunLoop().RunUntilIdle(); |
174 EXPECT_TRUE(called); | 158 EXPECT_TRUE(called); |
175 | 159 |
176 scoped_refptr<ServiceWorkerRegistration> registration2; | 160 scoped_refptr<ServiceWorkerRegistration> registration2; |
177 storage_->FindRegistrationForDocument( | 161 storage_->FindRegistrationForDocument( |
178 GURL("http://www.example.com/two"), | 162 GURL("http://www.example.com/two"), |
179 SaveFoundRegistration(true, REGISTRATION_OK, &called, ®istration2)); | 163 SaveFoundRegistration(true, REGISTRATION_OK, &called, ®istration2)); |
180 EXPECT_FALSE(called); | 164 EXPECT_FALSE(called); |
181 base::RunLoop().RunUntilIdle(); | 165 base::RunLoop().RunUntilIdle(); |
182 EXPECT_TRUE(called); | 166 EXPECT_TRUE(called); |
183 | 167 |
184 ASSERT_EQ(registration1, registration2); | 168 ASSERT_EQ(registration1, registration2); |
185 } | 169 } |
186 | 170 |
187 TEST_F(ServiceWorkerStorageTest, DifferentMatchDifferentRegistration) { | 171 TEST_F(ServiceWorkerJobTest, DifferentMatchDifferentRegistration) { |
188 bool called1; | 172 bool called1; |
189 scoped_refptr<ServiceWorkerRegistration> original_registration1; | 173 scoped_refptr<ServiceWorkerRegistration> original_registration1; |
190 storage_->Register( | 174 job_coordinator_->Register( |
191 GURL("http://www.example.com/one/*"), | 175 GURL("http://www.example.com/one/*"), |
192 GURL("http://www.example.com/service_worker.js"), | 176 GURL("http://www.example.com/service_worker.js"), |
193 SaveRegistration(REGISTRATION_OK, &called1, &original_registration1)); | 177 SaveRegistration(REGISTRATION_OK, &called1, &original_registration1)); |
194 | 178 |
195 bool called2; | 179 bool called2; |
196 scoped_refptr<ServiceWorkerRegistration> original_registration2; | 180 scoped_refptr<ServiceWorkerRegistration> original_registration2; |
197 storage_->Register( | 181 job_coordinator_->Register( |
198 GURL("http://www.example.com/two/*"), | 182 GURL("http://www.example.com/two/*"), |
199 GURL("http://www.example.com/service_worker.js"), | 183 GURL("http://www.example.com/service_worker.js"), |
200 SaveRegistration(REGISTRATION_OK, &called2, &original_registration2)); | 184 SaveRegistration(REGISTRATION_OK, &called2, &original_registration2)); |
201 | 185 |
202 EXPECT_FALSE(called1); | 186 EXPECT_FALSE(called1); |
203 EXPECT_FALSE(called2); | 187 EXPECT_FALSE(called2); |
204 base::RunLoop().RunUntilIdle(); | 188 base::RunLoop().RunUntilIdle(); |
205 EXPECT_TRUE(called2); | 189 EXPECT_TRUE(called2); |
206 EXPECT_TRUE(called1); | 190 EXPECT_TRUE(called1); |
207 | 191 |
208 scoped_refptr<ServiceWorkerRegistration> registration1; | 192 scoped_refptr<ServiceWorkerRegistration> registration1; |
209 storage_->FindRegistrationForDocument( | 193 storage_->FindRegistrationForDocument( |
210 GURL("http://www.example.com/one/"), | 194 GURL("http://www.example.com/one/"), |
211 SaveFoundRegistration(true, REGISTRATION_OK, &called1, ®istration1)); | 195 SaveFoundRegistration(true, REGISTRATION_OK, &called1, ®istration1)); |
212 scoped_refptr<ServiceWorkerRegistration> registration2; | 196 scoped_refptr<ServiceWorkerRegistration> registration2; |
213 storage_->FindRegistrationForDocument( | 197 storage_->FindRegistrationForDocument( |
214 GURL("http://www.example.com/two/"), | 198 GURL("http://www.example.com/two/"), |
215 SaveFoundRegistration(true, REGISTRATION_OK, &called2, ®istration2)); | 199 SaveFoundRegistration(true, REGISTRATION_OK, &called2, ®istration2)); |
216 | 200 |
217 EXPECT_FALSE(called1); | 201 EXPECT_FALSE(called1); |
218 EXPECT_FALSE(called2); | 202 EXPECT_FALSE(called2); |
219 base::RunLoop().RunUntilIdle(); | 203 base::RunLoop().RunUntilIdle(); |
220 EXPECT_TRUE(called2); | 204 EXPECT_TRUE(called2); |
221 EXPECT_TRUE(called1); | 205 EXPECT_TRUE(called1); |
222 | 206 |
223 ASSERT_NE(registration1, registration2); | 207 ASSERT_NE(registration1, registration2); |
224 } | 208 } |
225 | 209 |
226 // Make sure basic registration is working. | 210 // Make sure basic registration is working. |
227 TEST_F(ServiceWorkerStorageTest, Register) { | 211 TEST_F(ServiceWorkerJobTest, Register) { |
228 bool called = false; | 212 bool called = false; |
229 scoped_refptr<ServiceWorkerRegistration> registration; | 213 scoped_refptr<ServiceWorkerRegistration> registration; |
230 storage_->Register(GURL("http://www.example.com/*"), | 214 job_coordinator_->Register( |
231 GURL("http://www.example.com/service_worker.js"), | 215 GURL("http://www.example.com/*"), |
232 SaveRegistration(REGISTRATION_OK, &called, ®istration)); | 216 GURL("http://www.example.com/service_worker.js"), |
| 217 SaveRegistration(REGISTRATION_OK, &called, ®istration)); |
233 | 218 |
234 ASSERT_FALSE(called); | 219 ASSERT_FALSE(called); |
235 base::RunLoop().RunUntilIdle(); | 220 base::RunLoop().RunUntilIdle(); |
236 ASSERT_TRUE(called); | 221 ASSERT_TRUE(called); |
237 | 222 |
238 ASSERT_NE(scoped_refptr<ServiceWorkerRegistration>(NULL), registration); | 223 ASSERT_NE(scoped_refptr<ServiceWorkerRegistration>(NULL), registration); |
239 } | 224 } |
240 | 225 |
241 // Make sure registrations are cleaned up when they are unregistered. | 226 // Make sure registrations are cleaned up when they are unregistered. |
242 TEST_F(ServiceWorkerStorageTest, Unregister) { | 227 TEST_F(ServiceWorkerJobTest, Unregister) { |
243 GURL pattern("http://www.example.com/*"); | 228 GURL pattern("http://www.example.com/*"); |
244 | 229 |
245 bool called; | 230 bool called; |
246 scoped_refptr<ServiceWorkerRegistration> registration; | 231 scoped_refptr<ServiceWorkerRegistration> registration; |
247 storage_->Register(pattern, | 232 job_coordinator_->Register( |
248 GURL("http://www.example.com/service_worker.js"), | 233 pattern, |
249 SaveRegistration(REGISTRATION_OK, &called, ®istration)); | 234 GURL("http://www.example.com/service_worker.js"), |
| 235 SaveRegistration(REGISTRATION_OK, &called, ®istration)); |
250 | 236 |
251 ASSERT_FALSE(called); | 237 ASSERT_FALSE(called); |
252 base::RunLoop().RunUntilIdle(); | 238 base::RunLoop().RunUntilIdle(); |
253 ASSERT_TRUE(called); | 239 ASSERT_TRUE(called); |
254 | 240 |
255 storage_->Unregister(pattern, SaveUnregistration(REGISTRATION_OK, &called)); | 241 job_coordinator_->Unregister(pattern, |
| 242 SaveUnregistration(REGISTRATION_OK, &called)); |
256 | 243 |
257 ASSERT_FALSE(called); | 244 ASSERT_FALSE(called); |
258 base::RunLoop().RunUntilIdle(); | 245 base::RunLoop().RunUntilIdle(); |
259 ASSERT_TRUE(called); | 246 ASSERT_TRUE(called); |
260 | 247 |
261 ASSERT_TRUE(registration->HasOneRef()); | 248 ASSERT_TRUE(registration->HasOneRef()); |
262 | 249 |
263 storage_->FindRegistrationForPattern( | 250 storage_->FindRegistrationForPattern( |
264 pattern, | 251 pattern, |
265 SaveFoundRegistration(false, REGISTRATION_OK, &called, ®istration)); | 252 SaveFoundRegistration(false, REGISTRATION_OK, &called, ®istration)); |
266 | 253 |
267 ASSERT_FALSE(called); | 254 ASSERT_FALSE(called); |
268 base::RunLoop().RunUntilIdle(); | 255 base::RunLoop().RunUntilIdle(); |
269 ASSERT_TRUE(called); | 256 ASSERT_TRUE(called); |
270 | 257 |
271 ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(NULL), registration); | 258 ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(NULL), registration); |
272 } | 259 } |
273 | 260 |
274 // Make sure that when a new registration replaces an existing | 261 // Make sure that when a new registration replaces an existing |
275 // registration, that the old one is cleaned up. | 262 // registration, that the old one is cleaned up. |
276 TEST_F(ServiceWorkerStorageTest, RegisterNewScript) { | 263 TEST_F(ServiceWorkerJobTest, RegisterNewScript) { |
277 GURL pattern("http://www.example.com/*"); | 264 GURL pattern("http://www.example.com/*"); |
278 | 265 |
279 bool called; | 266 bool called; |
280 scoped_refptr<ServiceWorkerRegistration> old_registration; | 267 scoped_refptr<ServiceWorkerRegistration> old_registration; |
281 storage_->Register( | 268 job_coordinator_->Register( |
282 pattern, | 269 pattern, |
283 GURL("http://www.example.com/service_worker.js"), | 270 GURL("http://www.example.com/service_worker.js"), |
284 SaveRegistration(REGISTRATION_OK, &called, &old_registration)); | 271 SaveRegistration(REGISTRATION_OK, &called, &old_registration)); |
285 | 272 |
286 ASSERT_FALSE(called); | 273 ASSERT_FALSE(called); |
287 base::RunLoop().RunUntilIdle(); | 274 base::RunLoop().RunUntilIdle(); |
288 ASSERT_TRUE(called); | 275 ASSERT_TRUE(called); |
289 | 276 |
290 scoped_refptr<ServiceWorkerRegistration> old_registration_by_pattern; | 277 scoped_refptr<ServiceWorkerRegistration> old_registration_by_pattern; |
291 storage_->FindRegistrationForPattern( | 278 storage_->FindRegistrationForPattern( |
292 pattern, | 279 pattern, |
293 SaveFoundRegistration( | 280 SaveFoundRegistration( |
294 true, REGISTRATION_OK, &called, &old_registration_by_pattern)); | 281 true, REGISTRATION_OK, &called, &old_registration_by_pattern)); |
295 | 282 |
296 ASSERT_FALSE(called); | 283 ASSERT_FALSE(called); |
297 base::RunLoop().RunUntilIdle(); | 284 base::RunLoop().RunUntilIdle(); |
298 ASSERT_TRUE(called); | 285 ASSERT_TRUE(called); |
299 | 286 |
300 ASSERT_EQ(old_registration, old_registration_by_pattern); | 287 ASSERT_EQ(old_registration, old_registration_by_pattern); |
301 old_registration_by_pattern = NULL; | 288 old_registration_by_pattern = NULL; |
302 | 289 |
303 scoped_refptr<ServiceWorkerRegistration> new_registration; | 290 scoped_refptr<ServiceWorkerRegistration> new_registration; |
304 storage_->Register( | 291 job_coordinator_->Register( |
305 pattern, | 292 pattern, |
306 GURL("http://www.example.com/service_worker_new.js"), | 293 GURL("http://www.example.com/service_worker_new.js"), |
307 SaveRegistration(REGISTRATION_OK, &called, &new_registration)); | 294 SaveRegistration(REGISTRATION_OK, &called, &new_registration)); |
308 | 295 |
309 ASSERT_FALSE(called); | 296 ASSERT_FALSE(called); |
310 base::RunLoop().RunUntilIdle(); | 297 base::RunLoop().RunUntilIdle(); |
311 ASSERT_TRUE(called); | 298 ASSERT_TRUE(called); |
312 | 299 |
313 ASSERT_TRUE(old_registration->HasOneRef()); | 300 ASSERT_TRUE(old_registration->HasOneRef()); |
314 | 301 |
315 ASSERT_NE(old_registration, new_registration); | 302 ASSERT_NE(old_registration, new_registration); |
316 | 303 |
317 scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern; | 304 scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern; |
318 storage_->FindRegistrationForPattern( | 305 storage_->FindRegistrationForPattern( |
319 pattern, | 306 pattern, |
320 SaveFoundRegistration(true, REGISTRATION_OK, &called, &new_registration)); | 307 SaveFoundRegistration(true, REGISTRATION_OK, &called, &new_registration)); |
321 | 308 |
322 ASSERT_FALSE(called); | 309 ASSERT_FALSE(called); |
323 base::RunLoop().RunUntilIdle(); | 310 base::RunLoop().RunUntilIdle(); |
324 ASSERT_TRUE(called); | 311 ASSERT_TRUE(called); |
325 | 312 |
326 ASSERT_NE(new_registration_by_pattern, old_registration); | 313 ASSERT_NE(new_registration_by_pattern, old_registration); |
327 } | 314 } |
328 | 315 |
329 // Make sure that when registering a duplicate pattern+script_url | 316 // Make sure that when registering a duplicate pattern+script_url |
330 // combination, that the same registration is used. | 317 // combination, that the same registration is used. |
331 TEST_F(ServiceWorkerStorageTest, RegisterDuplicateScript) { | 318 TEST_F(ServiceWorkerJobTest, RegisterDuplicateScript) { |
332 GURL pattern("http://www.example.com/*"); | 319 GURL pattern("http://www.example.com/*"); |
333 GURL script_url("http://www.example.com/service_worker.js"); | 320 GURL script_url("http://www.example.com/service_worker.js"); |
334 | 321 |
335 bool called; | 322 bool called; |
336 scoped_refptr<ServiceWorkerRegistration> old_registration; | 323 scoped_refptr<ServiceWorkerRegistration> old_registration; |
337 storage_->Register( | 324 job_coordinator_->Register( |
338 pattern, | 325 pattern, |
339 script_url, | 326 script_url, |
340 SaveRegistration(REGISTRATION_OK, &called, &old_registration)); | 327 SaveRegistration(REGISTRATION_OK, &called, &old_registration)); |
341 | 328 |
342 ASSERT_FALSE(called); | 329 ASSERT_FALSE(called); |
343 base::RunLoop().RunUntilIdle(); | 330 base::RunLoop().RunUntilIdle(); |
344 ASSERT_TRUE(called); | 331 ASSERT_TRUE(called); |
345 | 332 |
346 scoped_refptr<ServiceWorkerRegistration> old_registration_by_pattern; | 333 scoped_refptr<ServiceWorkerRegistration> old_registration_by_pattern; |
347 storage_->FindRegistrationForPattern( | 334 storage_->FindRegistrationForPattern( |
348 pattern, | 335 pattern, |
349 SaveFoundRegistration( | 336 SaveFoundRegistration( |
350 true, REGISTRATION_OK, &called, &old_registration_by_pattern)); | 337 true, REGISTRATION_OK, &called, &old_registration_by_pattern)); |
351 ASSERT_FALSE(called); | 338 ASSERT_FALSE(called); |
352 base::RunLoop().RunUntilIdle(); | 339 base::RunLoop().RunUntilIdle(); |
353 ASSERT_TRUE(called); | 340 ASSERT_TRUE(called); |
354 | 341 |
355 ASSERT_TRUE(old_registration_by_pattern); | 342 ASSERT_TRUE(old_registration_by_pattern); |
356 | 343 |
357 scoped_refptr<ServiceWorkerRegistration> new_registration; | 344 scoped_refptr<ServiceWorkerRegistration> new_registration; |
358 storage_->Register( | 345 job_coordinator_->Register( |
359 pattern, | 346 pattern, |
360 script_url, | 347 script_url, |
361 SaveRegistration(REGISTRATION_OK, &called, &new_registration)); | 348 SaveRegistration(REGISTRATION_OK, &called, &new_registration)); |
362 | 349 |
363 ASSERT_FALSE(called); | 350 ASSERT_FALSE(called); |
364 base::RunLoop().RunUntilIdle(); | 351 base::RunLoop().RunUntilIdle(); |
365 ASSERT_TRUE(called); | 352 ASSERT_TRUE(called); |
366 | 353 |
367 ASSERT_EQ(old_registration, new_registration); | 354 ASSERT_EQ(old_registration, new_registration); |
368 | 355 |
369 ASSERT_FALSE(old_registration->HasOneRef()); | 356 ASSERT_FALSE(old_registration->HasOneRef()); |
370 | 357 |
371 scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern; | 358 scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern; |
372 storage_->FindRegistrationForPattern( | 359 storage_->FindRegistrationForPattern( |
373 pattern, | 360 pattern, |
374 SaveFoundRegistration( | 361 SaveFoundRegistration( |
375 true, REGISTRATION_OK, &called, &new_registration_by_pattern)); | 362 true, REGISTRATION_OK, &called, &new_registration_by_pattern)); |
376 | 363 |
377 ASSERT_FALSE(called); | 364 ASSERT_FALSE(called); |
378 base::RunLoop().RunUntilIdle(); | 365 base::RunLoop().RunUntilIdle(); |
379 ASSERT_TRUE(called); | 366 ASSERT_TRUE(called); |
380 | 367 |
381 ASSERT_EQ(new_registration, old_registration); | 368 ASSERT_EQ(new_registration, old_registration); |
382 } | 369 } |
383 | 370 |
384 } // namespace content | 371 } // namespace content |
OLD | NEW |