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

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

Powered by Google App Engine
This is Rietveld 408576698