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

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: 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 "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 ServiceWorkerRegistration* registration1 =
85 context_->RegistrationForDocument(GURL("http://www.example.com/one"));
86 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 TEST_F(ServiceWorkerContextTest, Register) {
108 int64 registration_id = -1L;
109 bool called = false;
110 context_->RegisterServiceWorker(
111 GURL("http://www.example.com/*"),
112 GURL("http://www.example.com/service_worker.js"),
113 MakeRegisteredCallback(&called, &registration_id));
114
115 ASSERT_FALSE(called);
116 message_loop_.RunUntilIdle();
117 ASSERT_TRUE(called);
118
119 ASSERT_NE(-1L, registration_id);
120 }
121
122 TEST_F(ServiceWorkerContextTest, Unregister) {
123 GURL pattern("http://www.example.com/*");
124
125 bool called;
126 int64 registration_id = -1L;
127 context_->RegisterServiceWorker(
128 pattern,
129 GURL("http://www.example.com/service_worker.js"),
130 MakeRegisteredCallback(&called, &registration_id));
131
132 ASSERT_FALSE(called);
133 message_loop_.RunUntilIdle();
134 ASSERT_TRUE(called);
135
136 scoped_refptr<ServiceWorkerRegistration> registration(
137 context_->RegistrationForPattern(pattern));
138
139 called = false;
140 context_->UnregisterServiceWorker(pattern, MakeUnregisteredCallback(&called));
141
142 ASSERT_FALSE(called);
143 message_loop_.RunUntilIdle();
144 ASSERT_TRUE(called);
145
146 ASSERT_TRUE(registration->HasOneRef());
147
148 ASSERT_EQ(NULL, context_->RegistrationForPattern(pattern).get());
149 }
150
151 } // content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698