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

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

Powered by Google App Engine
This is Rietveld 408576698