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

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

Powered by Google App Engine
This is Rietveld 408576698