Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Side by Side Diff: content/browser/service_worker/service_worker_storage_unittest.cc

Issue 62203007: Implement memory-persistent registration (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: More comments Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/browser/service_worker/service_worker_storage.h"
6
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "content/browser/browser_thread_impl.h"
11 #include "content/browser/service_worker/service_worker_registration.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace content {
15
16 class ServiceWorkerStorageTest : public testing::Test {
17 public:
18 ServiceWorkerStorageTest() : io_thread_(BrowserThread::IO, &message_loop_) {}
19
20 virtual void SetUp() OVERRIDE {
21 storage_ = new ServiceWorkerStorage(base::FilePath(), NULL);
22 }
23
24 virtual void TearDown() OVERRIDE {}
25
26 protected:
27 ServiceWorkerStorage* storage_;
kinuko 2013/11/27 03:45:23 scoped_ptr ? Looks like it's leaking
alecflett 2013/12/02 16:11:35 Done.
28
29 base::MessageLoopForIO message_loop_;
30 BrowserThreadImpl io_thread_;
31 };
32
33 TEST_F(ServiceWorkerStorageTest, RegisterMatch) {
kinuko 2013/11/27 03:45:23 RegisterMatch -> PatternMatches to make it clearer
alecflett 2013/12/02 16:11:35 Done.
34 ASSERT_TRUE(ServiceWorkerStorage::PatternMatches(
35 GURL("http://www.example.com/*"), GURL("http://www.example.com/")));
36 ASSERT_TRUE(ServiceWorkerStorage::PatternMatches(
37 GURL("http://www.example.com/*"),
38 GURL("http://www.example.com/page.html")));
39
40 ASSERT_FALSE(ServiceWorkerStorage::PatternMatches(
41 GURL("http://www.example.com/*"), GURL("https://www.example.com/")));
42 ASSERT_FALSE(ServiceWorkerStorage::PatternMatches(
43 GURL("http://www.example.com/*"),
44 GURL("https://www.example.com/page.html")));
45
46 ASSERT_FALSE(ServiceWorkerStorage::PatternMatches(
47 GURL("http://www.example.com/*"), GURL("http://www.foo.com/")));
48 ASSERT_FALSE(ServiceWorkerStorage::PatternMatches(
49 GURL("http://www.example.com/*"), GURL("https://www.foo.com/page.html")));
50 }
51
52 namespace {
kinuko 2013/11/27 03:45:23 Can we move this anon namespace to the top? (e.g.
alecflett 2013/12/02 16:11:35 Done.
53 void SaveRegistrationCallback(
54 ServiceWorkerStorage::RegistrationStatus expected_status,
55 bool* called,
56 scoped_refptr<ServiceWorkerRegistration>* registration,
57 ServiceWorkerStorage::RegistrationStatus status,
58 const scoped_refptr<ServiceWorkerRegistration>& result) {
59 EXPECT_EQ(expected_status, status);
60 *called = true;
61 *registration = result;
62 }
63
64 // Creates a callback which both keeps track of if it's been called,
65 // as well as the resulting registration. Whent the callback is fired,
66 // it ensures that the resulting status matches the expectation.
67 // 'called' is useful for making sure a sychronous callback is or
68 // isn't called.
69 ServiceWorkerStorage::RegistrationCallback SaveRegistration(
70 ServiceWorkerStorage::RegistrationStatus expected_status,
71 bool* called,
72 scoped_refptr<ServiceWorkerRegistration>* registration) {
73 *called = false;
74 return base::Bind(
75 &SaveRegistrationCallback, expected_status, called, registration);
76 }
77
78 void SaveUnregistrationCallback(
79 ServiceWorkerStorage::RegistrationStatus expected_status,
80 bool* called,
81 ServiceWorkerStorage::RegistrationStatus status) {
82 EXPECT_EQ(expected_status, status);
83 *called = true;
84 }
85
86 ServiceWorkerStorage::UnregistrationCallback SaveUnregistration(
87 ServiceWorkerStorage::RegistrationStatus expected_status,
88 bool* called) {
89 *called = false;
90 return base::Bind(&SaveUnregistrationCallback, expected_status, called);
91 }
92
93 } // namespace
94
95 TEST_F(ServiceWorkerStorageTest, SameDocumentSameRegistration) {
96 scoped_refptr<ServiceWorkerRegistration> original_registration;
97 bool called;
98 storage_->Register(GURL("http://www.example.com/*"),
99 GURL("http://www.example.com/service_worker.js"),
100 SaveRegistration(ServiceWorkerStorage::REGISTRATION_OK,
101 &called,
102 &original_registration));
103 EXPECT_FALSE(called);
104 message_loop_.RunUntilIdle();
kinuko 2013/11/27 03:45:23 It's deprecated, I think we prefer base::RunLoop()
alecflett 2013/12/02 16:11:35 I'm not sure how to hook up base::RunLoop() to the
alecflett 2013/12/03 22:34:09 Ah! I figured it out... TestBrowserThreadBundle
105 EXPECT_TRUE(called);
106
107 scoped_refptr<ServiceWorkerRegistration> registration1;
108 storage_->FindRegistrationForDocument(
109 GURL("http://www.example.com/"),
110 SaveRegistration(
111 ServiceWorkerStorage::REGISTRATION_OK, &called, &registration1));
112 scoped_refptr<ServiceWorkerRegistration> registration2;
113 storage_->FindRegistrationForDocument(
114 GURL("http://www.example.com/"),
115 SaveRegistration(
116 ServiceWorkerStorage::REGISTRATION_OK, &called, &registration2));
117
118 ServiceWorkerRegistration* null_registration(NULL);
119 ASSERT_EQ(null_registration, registration1);
120 ASSERT_EQ(null_registration, registration2);
121 EXPECT_FALSE(called);
kinuko 2013/11/27 03:45:23 If we assume FindRegistrationForDocument could ret
alecflett 2013/12/02 16:11:35 Well mostly I want to track that behavior - I don'
122 message_loop_.RunUntilIdle();
123 EXPECT_TRUE(called);
124 ASSERT_NE(null_registration, registration1);
125 ASSERT_NE(null_registration, registration2);
126
127 ASSERT_EQ(registration1, registration2);
128 }
129
130 TEST_F(ServiceWorkerStorageTest, SameMatchSameRegistration) {
131 bool called;
132 scoped_refptr<ServiceWorkerRegistration> original_registration;
133 storage_->Register(GURL("http://www.example.com/*"),
134 GURL("http://www.example.com/service_worker.js"),
135 SaveRegistration(ServiceWorkerStorage::REGISTRATION_OK,
136 &called,
137 &original_registration));
138 EXPECT_FALSE(called);
139 message_loop_.RunUntilIdle();
140 EXPECT_TRUE(called);
141 ASSERT_NE(static_cast<ServiceWorkerRegistration*>(NULL),
142 original_registration.get());
143
144 scoped_refptr<ServiceWorkerRegistration> registration1;
145 storage_->FindRegistrationForDocument(
146 GURL("http://www.example.com/one"),
147 SaveRegistration(
148 ServiceWorkerStorage::REGISTRATION_OK, &called, &registration1));
149
150 EXPECT_FALSE(called);
151 message_loop_.RunUntilIdle();
152 EXPECT_TRUE(called);
153
154 scoped_refptr<ServiceWorkerRegistration> registration2;
155 storage_->FindRegistrationForDocument(
156 GURL("http://www.example.com/two"),
157 SaveRegistration(
158 ServiceWorkerStorage::REGISTRATION_OK, &called, &registration2));
159 EXPECT_FALSE(called);
160 message_loop_.RunUntilIdle();
161 EXPECT_TRUE(called);
162
163 ASSERT_EQ(registration1, registration2);
164 }
165
166 TEST_F(ServiceWorkerStorageTest, DifferentMatchDifferentRegistration) {
167 bool called1;
168 scoped_refptr<ServiceWorkerRegistration> original_registration1;
169 storage_->Register(GURL("http://www.example.com/one/*"),
170 GURL("http://www.example.com/service_worker.js"),
171 SaveRegistration(ServiceWorkerStorage::REGISTRATION_OK,
172 &called1,
173 &original_registration1));
174
175 bool called2;
176 scoped_refptr<ServiceWorkerRegistration> original_registration2;
177 storage_->Register(GURL("http://www.example.com/two/*"),
178 GURL("http://www.example.com/service_worker.js"),
179 SaveRegistration(ServiceWorkerStorage::REGISTRATION_OK,
180 &called2,
181 &original_registration2));
182
183 EXPECT_FALSE(called1);
184 EXPECT_FALSE(called2);
185 message_loop_.RunUntilIdle();
186 EXPECT_TRUE(called2);
187 EXPECT_TRUE(called1);
188
189 scoped_refptr<ServiceWorkerRegistration> registration1;
190 storage_->FindRegistrationForDocument(
191 GURL("http://www.example.com/one/"),
192 SaveRegistration(
193 ServiceWorkerStorage::REGISTRATION_OK, &called1, &registration1));
194 scoped_refptr<ServiceWorkerRegistration> registration2;
195 storage_->FindRegistrationForDocument(
196 GURL("http://www.example.com/two/"),
197 SaveRegistration(
198 ServiceWorkerStorage::REGISTRATION_OK, &called2, &registration2));
199
200 EXPECT_FALSE(called1);
201 EXPECT_FALSE(called2);
202 message_loop_.RunUntilIdle();
203 EXPECT_TRUE(called2);
204 EXPECT_TRUE(called1);
205
206 ASSERT_NE(registration1, registration2);
207 }
208
209 // Make sure basic registration is working.
210 TEST_F(ServiceWorkerStorageTest, Register) {
211 bool called = false;
212 scoped_refptr<ServiceWorkerRegistration> registration;
213 storage_->Register(
214 GURL("http://www.example.com/*"),
215 GURL("http://www.example.com/service_worker.js"),
216 SaveRegistration(
217 ServiceWorkerStorage::REGISTRATION_OK, &called, &registration));
218
219 ASSERT_FALSE(called);
220 message_loop_.RunUntilIdle();
221 ASSERT_TRUE(called);
222
223 ASSERT_NE(scoped_refptr<ServiceWorkerRegistration>(NULL), registration);
kinuko 2013/11/27 03:45:23 Can we test FindRegistrationForPattern returns OK
alecflett 2013/12/02 16:11:35 oops, I think that was there before I shuffled thi
224 }
225
226 // Make sure registrations are cleaned up when they are unregistered.
227 TEST_F(ServiceWorkerStorageTest, Unregister) {
228 GURL pattern("http://www.example.com/*");
229
230 bool called;
231 scoped_refptr<ServiceWorkerRegistration> registration;
232 storage_->Register(
233 pattern,
234 GURL("http://www.example.com/service_worker.js"),
235 SaveRegistration(
236 ServiceWorkerStorage::REGISTRATION_OK, &called, &registration));
237
238 ASSERT_FALSE(called);
239 message_loop_.RunUntilIdle();
240 ASSERT_TRUE(called);
241
242 storage_->Unregister(
243 pattern,
244 SaveUnregistration(ServiceWorkerStorage::REGISTRATION_OK, &called));
245
246 ASSERT_FALSE(called);
247 message_loop_.RunUntilIdle();
248 ASSERT_TRUE(called);
249
250 ASSERT_TRUE(registration->HasOneRef());
251
252 storage_->FindRegistrationForPattern(
253 pattern,
254 SaveRegistration(ServiceWorkerStorage::REGISTRATION_NOT_FOUND,
255 &called,
256 &registration));
257
258 ASSERT_FALSE(called);
259 message_loop_.RunUntilIdle();
260 ASSERT_TRUE(called);
261
262 ASSERT_EQ(scoped_refptr<ServiceWorkerRegistration>(NULL), registration);
263 }
264
265 // Make sure that when a new registration replaces an existing
266 // registration, that the old one is cleaned up.
267 TEST_F(ServiceWorkerStorageTest, RegisterNewScript) {
268 GURL pattern("http://www.example.com/*");
269
270 bool called;
271 scoped_refptr<ServiceWorkerRegistration> old_registration;
272 storage_->Register(
273 pattern,
274 GURL("http://www.example.com/service_worker.js"),
275 SaveRegistration(
276 ServiceWorkerStorage::REGISTRATION_OK, &called, &old_registration));
277
278 ASSERT_FALSE(called);
279 message_loop_.RunUntilIdle();
280 ASSERT_TRUE(called);
281
282 scoped_refptr<ServiceWorkerRegistration> old_registration_by_pattern;
283 storage_->FindRegistrationForPattern(
284 pattern,
285 SaveRegistration(ServiceWorkerStorage::REGISTRATION_OK,
286 &called,
287 &old_registration_by_pattern));
288
289 ASSERT_FALSE(called);
290 message_loop_.RunUntilIdle();
291 ASSERT_TRUE(called);
292
293 ASSERT_EQ(old_registration, old_registration_by_pattern);
294 old_registration_by_pattern = NULL;
295
296 scoped_refptr<ServiceWorkerRegistration> new_registration;
297 storage_->Register(
298 pattern,
299 GURL("http://www.example.com/service_worker_new.js"),
300 SaveRegistration(
301 ServiceWorkerStorage::REGISTRATION_OK, &called, &new_registration));
302
303 ASSERT_FALSE(called);
304 message_loop_.RunUntilIdle();
305 ASSERT_TRUE(called);
306
307 ASSERT_TRUE(old_registration->HasOneRef());
308
309 ASSERT_NE(old_registration, new_registration);
310
311 scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern;
312 storage_->FindRegistrationForPattern(
313 pattern,
314 SaveRegistration(
315 ServiceWorkerStorage::REGISTRATION_OK, &called, &new_registration));
kinuko 2013/11/27 03:45:23 new_registration -> new_registration_by_pattern ?
alecflett 2013/12/02 16:11:35 oops! typo. Done.
316
317 ASSERT_FALSE(called);
318 message_loop_.RunUntilIdle();
319 ASSERT_TRUE(called);
320
321 ASSERT_NE(new_registration_by_pattern, old_registration);
322 }
323
324 // Make sure that when registering a duplicate pattern+script_url
325 // combination, that the same registration is used.
326 TEST_F(ServiceWorkerStorageTest, RegisterDuplicateScript) {
327 GURL pattern("http://www.example.com/*");
328 GURL script_url("http://www.example.com/service_worker.js");
329
330 bool called;
331 scoped_refptr<ServiceWorkerRegistration> old_registration;
332 storage_->Register(
333 pattern,
334 script_url,
335 SaveRegistration(
336 ServiceWorkerStorage::REGISTRATION_OK, &called, &old_registration));
337
338 ASSERT_FALSE(called);
339 message_loop_.RunUntilIdle();
340 ASSERT_TRUE(called);
341
342 scoped_refptr<ServiceWorkerRegistration> old_registration_by_pattern;
343 storage_->FindRegistrationForPattern(
344 pattern,
345 SaveRegistration(ServiceWorkerStorage::REGISTRATION_OK,
346 &called,
347 &old_registration_by_pattern));
348 ASSERT_FALSE(called);
349 message_loop_.RunUntilIdle();
350 ASSERT_TRUE(called);
351
352 ASSERT_TRUE(old_registration_by_pattern);
353
354 scoped_refptr<ServiceWorkerRegistration> new_registration;
355 storage_->Register(
356 pattern,
357 script_url,
358 SaveRegistration(
359 ServiceWorkerStorage::REGISTRATION_OK, &called, &new_registration));
360
361 ASSERT_FALSE(called);
362 message_loop_.RunUntilIdle();
363 ASSERT_TRUE(called);
364
365 ASSERT_EQ(old_registration, new_registration);
366
367 ASSERT_FALSE(old_registration->HasOneRef());
368
369 scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern;
370 storage_->FindRegistrationForPattern(
371 pattern,
372 SaveRegistration(ServiceWorkerStorage::REGISTRATION_OK,
373 &called,
374 &new_registration_by_pattern));
375
376 ASSERT_FALSE(called);
377 message_loop_.RunUntilIdle();
378 ASSERT_TRUE(called);
379
380 ASSERT_EQ(new_registration, old_registration);
kinuko 2013/11/27 03:45:23 new_registration -> new_registration_by_pattern ?
alecflett 2013/12/02 16:11:35 Done.
381 }
382
383 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698