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

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: 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_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 "content/browser/service_worker/service_worker_context_core.h"
12 #include "content/browser/service_worker/service_worker_registration.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "content/public/test/test_utils.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace content {
18
19 namespace {
kinuko 2013/12/06 15:24:49 nit: please insert an empty line here
alecflett 2013/12/06 18:58:44 Done.
20 void SaveResponseCallback(bool* called,
21 int64* store_result,
22 ServiceWorkerRegistrationStatus status,
23 int64 result) {
24 *called = true;
25 *store_result = result;
26 }
27
28 ServiceWorkerContextCore::RegistrationCallback MakeRegisteredCallback(
29 bool* called,
30 int64* store_result) {
31 return base::Bind(&SaveResponseCallback, called, store_result);
32 }
33
34 void CallCompletedCallback(bool* called, ServiceWorkerRegistrationStatus) {
35 *called = true;
36 }
37
38 ServiceWorkerContextCore::UnregistrationCallback MakeUnregisteredCallback(
39 bool* called) {
40 return base::Bind(&CallCompletedCallback, called);
41 }
42
43 } // namespace
44
45 class ServiceWorkerContextTest : public testing::Test {
46 public:
47 ServiceWorkerContextTest()
48 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {}
49
50 virtual void SetUp() OVERRIDE {
51 context_.reset(new ServiceWorkerContextCore(base::FilePath(), NULL));
52 }
53
54 virtual void TearDown() OVERRIDE { context_.reset(); }
55
56 protected:
57 TestBrowserThreadBundle browser_thread_bundle_;
58 scoped_ptr<ServiceWorkerContextCore> context_;
59 };
60
61 void RegistrationCallback(
62 scoped_refptr<ServiceWorkerRegistration>* registration,
63 const scoped_refptr<ServiceWorkerRegistration>& result) {
64 *registration = result;
65 }
66
67 // Make sure basic registration is working.
68 TEST_F(ServiceWorkerContextTest, Register) {
69 int64 registration_id = -1L;
70 bool called = false;
71 context_->RegisterServiceWorker(
72 GURL("http://www.example.com/*"),
73 GURL("http://www.example.com/service_worker.js"),
74 MakeRegisteredCallback(&called, &registration_id));
75
76 ASSERT_FALSE(called);
77 base::RunLoop().RunUntilIdle();
78 ASSERT_TRUE(called);
79
80 ASSERT_NE(-1L, registration_id);
81 }
82
83 // Make sure registrations are cleaned up when they are unregistered.
84 TEST_F(ServiceWorkerContextTest, Unregister) {
85 GURL pattern("http://www.example.com/*");
86
87 bool called = false;
88 int64 registration_id = -1L;
89 context_->RegisterServiceWorker(
90 pattern,
91 GURL("http://www.example.com/service_worker.js"),
92 MakeRegisteredCallback(&called, &registration_id));
93
94 ASSERT_FALSE(called);
95 base::RunLoop().RunUntilIdle();
96 ASSERT_TRUE(called);
97
98 called = false;
99 context_->UnregisterServiceWorker(pattern, MakeUnregisteredCallback(&called));
100
101 ASSERT_FALSE(called);
102 base::RunLoop().RunUntilIdle();
103 ASSERT_TRUE(called);
104 }
105
106 // Make sure that when a new registration replaces an existing
107 // registration, that the old one is cleaned up.
108 TEST_F(ServiceWorkerContextTest, RegisterNewScript) {
109 GURL pattern("http://www.example.com/*");
110
111 bool called = false;
112 int64 old_registration_id = -1L;
113 context_->RegisterServiceWorker(
114 pattern,
115 GURL("http://www.example.com/service_worker.js"),
116 MakeRegisteredCallback(&called, &old_registration_id));
117
118 ASSERT_FALSE(called);
119 base::RunLoop().RunUntilIdle();
120 ASSERT_TRUE(called);
121
122 called = false;
123 int64 new_registration_id = -1L;
124 context_->RegisterServiceWorker(
125 pattern,
126 GURL("http://www.example.com/service_worker_new.js"),
127 MakeRegisteredCallback(&called, &new_registration_id));
128
129 ASSERT_FALSE(called);
130 base::RunLoop().RunUntilIdle();
131 ASSERT_TRUE(called);
132
133 ASSERT_NE(old_registration_id, new_registration_id);
134 }
135
136 // Make sure that when registering a duplicate pattern+script_url
137 // combination, that the same registration is used.
138 TEST_F(ServiceWorkerContextTest, RegisterDuplicateScript) {
139 GURL pattern("http://www.example.com/*");
140 GURL script_url("http://www.example.com/service_worker.js");
141
142 bool called = false;
143 int64 old_registration_id = -1L;
144 context_->RegisterServiceWorker(
145 pattern,
146 script_url,
147 MakeRegisteredCallback(&called, &old_registration_id));
148
149 ASSERT_FALSE(called);
150 base::RunLoop().RunUntilIdle();
151 ASSERT_TRUE(called);
152
153 called = false;
154 int64 new_registration_id = -1L;
155 context_->RegisterServiceWorker(
156 pattern,
157 script_url,
158 MakeRegisteredCallback(&called, &new_registration_id));
159
160 ASSERT_FALSE(called);
161 base::RunLoop().RunUntilIdle();
162 ASSERT_TRUE(called);
163
164 ASSERT_EQ(old_registration_id, new_registration_id);
165 }
166
167 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698