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

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

Issue 62203007: Implement memory-persistent registration (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove some stray #includes 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_context.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 "testing/gtest/include/gtest/gtest.h"
12
13 namespace content {
14
15 class ServiceWorkerContextTest : public testing::Test {
16 public:
17 ServiceWorkerContextTest() : io_thread_(BrowserThread::IO, &message_loop_) {}
18
19 virtual void SetUp() OVERRIDE {
20 context_ = new ServiceWorkerContext(base::FilePath(), NULL);
21 }
22
23 static ServiceWorkerContext::ResponseCallback MakeRegisteredCallback(
24 bool* called,
25 int64* store_result) {
26 return base::Bind(SaveResponseCallback, called, store_result);
27 }
28
29 static base::Closure MakeUnregisteredCallback(bool* called) {
30 return base::Bind(CallCompletedCallback, called);
31 }
32
33 protected:
34 scoped_refptr<ServiceWorkerContext> context_;
35
36 static void SaveResponseCallback(bool* called,
37 int64* store_result,
38 int64 result) {
39 *called = true;
40 *store_result = result;
41 }
42
43 static void CallCompletedCallback(bool* called) { *called = true; }
44
45 base::MessageLoopForIO message_loop_;
46 BrowserThreadImpl io_thread_;
47 };
48
49 TEST_F(ServiceWorkerContextTest, RegisterMatch) {
50 ASSERT_TRUE(ServiceWorkerContext::PatternMatches(
51 GURL("http://www.example.com/*"), GURL("http://www.example.com/")));
52 ASSERT_TRUE(ServiceWorkerContext::PatternMatches(
53 GURL("http://www.example.com/*"),
54 GURL("http://www.example.com/page.html")));
55
56 ASSERT_FALSE(ServiceWorkerContext::PatternMatches(
57 GURL("http://www.example.com/*"), GURL("https://www.example.com/")));
58 ASSERT_FALSE(ServiceWorkerContext::PatternMatches(
59 GURL("http://www.example.com/*"),
60 GURL("https://www.example.com/page.html")));
61
62 ASSERT_FALSE(ServiceWorkerContext::PatternMatches(
63 GURL("http://www.example.com/*"), GURL("http://www.foo.com/")));
64 ASSERT_FALSE(ServiceWorkerContext::PatternMatches(
65 GURL("http://www.example.com/*"), GURL("https://www.foo.com/page.html")));
66 }
67
68 TEST_F(ServiceWorkerContextTest, SameDocumentSameRegistration) {
69 context_->Register(GURL("http://www.example.com/*"),
70 GURL("http://www.example.com/service_worker.js"));
71
72 scoped_refptr<ServiceWorkerRegistration> registration1 =
73 context_->RegistrationForDocument(GURL("http://www.example.com/"));
74 scoped_refptr<ServiceWorkerRegistration> registration2 =
75 context_->RegistrationForDocument(GURL("http://www.example.com/"));
76
77 ASSERT_EQ(registration1, registration2);
78 }
79
80 TEST_F(ServiceWorkerContextTest, SameMatchSameRegistration) {
81 context_->Register(GURL("http://www.example.com/*"),
82 GURL("http://www.example.com/service_worker.js"));
83
84 scoped_refptr<ServiceWorkerRegistration> registration1 =
85 context_->RegistrationForDocument(GURL("http://www.example.com/one"));
86 scoped_refptr<ServiceWorkerRegistration> registration2 =
87 context_->RegistrationForDocument(GURL("http://www.example.com/two"));
88
89 ASSERT_EQ(registration1, registration2);
90 }
91
92 TEST_F(ServiceWorkerContextTest, DifferentMatchDifferentRegistration) {
93 context_->Register(GURL("http://www.example.com/two/*"),
94 GURL("http://www.example.com/service_worker.js"));
95
96 context_->Register(GURL("http://www.example.com/one/*"),
97 GURL("http://www.example.com/service_worker.js"));
98
99 scoped_refptr<ServiceWorkerRegistration> registration1 =
100 context_->RegistrationForDocument(GURL("http://www.example.com/one/"));
101 scoped_refptr<ServiceWorkerRegistration> registration2 =
102 context_->RegistrationForDocument(GURL("http://www.example.com/two/"));
103
104 ASSERT_NE(registration1, registration2);
105 }
106
107 // Make sure basic registration is working.
108 TEST_F(ServiceWorkerContextTest, Register) {
109 int64 registration_id = -1L;
110 bool called = false;
111 context_->RegisterServiceWorker(
112 GURL("http://www.example.com/*"),
113 GURL("http://www.example.com/service_worker.js"),
114 MakeRegisteredCallback(&called, &registration_id));
115
116 ASSERT_FALSE(called);
117 message_loop_.RunUntilIdle();
118 ASSERT_TRUE(called);
119
120 ASSERT_NE(-1L, registration_id);
121 }
122
123 // Make sure registrations are cleaned up when they are unregistered.
124 TEST_F(ServiceWorkerContextTest, Unregister) {
125 GURL pattern("http://www.example.com/*");
126
127 bool called;
128 int64 registration_id = -1L;
129 context_->RegisterServiceWorker(
130 pattern,
131 GURL("http://www.example.com/service_worker.js"),
132 MakeRegisteredCallback(&called, &registration_id));
133
134 ASSERT_FALSE(called);
135 message_loop_.RunUntilIdle();
136 ASSERT_TRUE(called);
137
138 scoped_refptr<ServiceWorkerRegistration> registration(
139 context_->RegistrationForPattern(pattern));
140
141 called = false;
142 context_->UnregisterServiceWorker(pattern, MakeUnregisteredCallback(&called));
143
144 ASSERT_FALSE(called);
145 message_loop_.RunUntilIdle();
146 ASSERT_TRUE(called);
147
148 ASSERT_TRUE(registration->HasOneRef());
149
150 ASSERT_EQ(NULL, context_->RegistrationForPattern(pattern).get());
151 }
152
153 // Make sure that when a new registration replaces an existing
154 // registration, that the old one is cleaned up.
155 TEST_F(ServiceWorkerContextTest, RegisterNewScript) {
156 GURL pattern("http://www.example.com/*");
157
158 bool called;
159 int64 old_registration_id = -1L;
160 context_->RegisterServiceWorker(
161 pattern,
162 GURL("http://www.example.com/service_worker.js"),
163 MakeRegisteredCallback(&called, &old_registration_id));
164
165 ASSERT_FALSE(called);
166 message_loop_.RunUntilIdle();
167 ASSERT_TRUE(called);
168
169 scoped_refptr<ServiceWorkerRegistration> old_registration(
170 context_->RegistrationForPattern(pattern));
171
172 called = false;
173 int64 new_registration_id = -1L;
174 context_->RegisterServiceWorker(
175 pattern,
176 GURL("http://www.example.com/service_worker_new.js"),
177 MakeRegisteredCallback(&called, &new_registration_id));
178
179 ASSERT_FALSE(called);
180 message_loop_.RunUntilIdle();
181 ASSERT_TRUE(called);
182
183 ASSERT_TRUE(old_registration->HasOneRef());
184
185 ASSERT_NE(old_registration_id, new_registration_id);
186
187 scoped_refptr<ServiceWorkerRegistration> new_registration(
188 context_->RegistrationForPattern(pattern));
189 ASSERT_NE(new_registration, old_registration);
190 }
191
192 // Make sure that when registering a duplicate pattern+script_url
193 // combination, that the same registration is used.
194 TEST_F(ServiceWorkerContextTest, RegisterDuplicateScript) {
195 GURL pattern("http://www.example.com/*");
196 GURL script_url("http://www.example.com/service_worker.js");
197
198 bool called;
199 int64 old_registration_id = -1L;
200 context_->RegisterServiceWorker(
201 pattern,
202 script_url,
203 MakeRegisteredCallback(&called, &old_registration_id));
204
205 ASSERT_FALSE(called);
206 message_loop_.RunUntilIdle();
207 ASSERT_TRUE(called);
208
209 scoped_refptr<ServiceWorkerRegistration> old_registration(
210 context_->RegistrationForPattern(pattern));
211
212 called = false;
213 int64 new_registration_id = -1L;
214 context_->RegisterServiceWorker(
215 pattern,
216 script_url,
217 MakeRegisteredCallback(&called, &new_registration_id));
218
219 ASSERT_FALSE(called);
220 message_loop_.RunUntilIdle();
221 ASSERT_TRUE(called);
222
223 ASSERT_EQ(old_registration_id, new_registration_id);
224
225 ASSERT_FALSE(old_registration->HasOneRef());
226
227 scoped_refptr<ServiceWorkerRegistration> new_registration(
228 context_->RegistrationForPattern(pattern));
229 ASSERT_EQ(new_registration, old_registration);
230 }
231
232 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698