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

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

Powered by Google App Engine
This is Rietveld 408576698