OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/service_worker/service_worker_context.h" | 5 #include "content/browser/service_worker/service_worker_context.h" |
6 | 6 |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "content/browser/browser_thread_impl.h" | 10 #include "content/browser/browser_thread_impl.h" |
11 #include "content/browser/service_worker/embedded_worker_registry.h" | |
12 #include "content/browser/service_worker/embedded_worker_test_helper.h" | |
11 #include "content/browser/service_worker/service_worker_context_core.h" | 13 #include "content/browser/service_worker/service_worker_context_core.h" |
12 #include "content/browser/service_worker/service_worker_registration.h" | 14 #include "content/browser/service_worker/service_worker_registration.h" |
13 #include "content/public/test/test_browser_thread_bundle.h" | 15 #include "content/public/test/test_browser_thread_bundle.h" |
14 #include "content/public/test/test_utils.h" | 16 #include "content/public/test/test_utils.h" |
17 #include "ipc/ipc_test_sink.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
16 | 19 |
17 namespace content { | 20 namespace content { |
18 | 21 |
19 namespace { | 22 namespace { |
20 | 23 |
21 void SaveResponseCallback(bool* called, | 24 void SaveResponseCallback(bool* called, |
22 int64* store_result, | 25 int64* store_result, |
23 ServiceWorkerStatusCode status, | 26 ServiceWorkerStatusCode status, |
24 int64 result) { | 27 int64 result) { |
28 LOG(ERROR) << "SaveResponseCallback: " << status << ", " << result; | |
25 *called = true; | 29 *called = true; |
26 *store_result = result; | 30 *store_result = result; |
27 } | 31 } |
28 | 32 |
29 ServiceWorkerContextCore::RegistrationCallback MakeRegisteredCallback( | 33 ServiceWorkerContextCore::RegistrationCallback MakeRegisteredCallback( |
30 bool* called, | 34 bool* called, |
31 int64* store_result) { | 35 int64* store_result) { |
32 return base::Bind(&SaveResponseCallback, called, store_result); | 36 return base::Bind(&SaveResponseCallback, called, store_result); |
33 } | 37 } |
34 | 38 |
35 void CallCompletedCallback(bool* called, ServiceWorkerStatusCode) { | 39 void CallCompletedCallback(bool* called, ServiceWorkerStatusCode) { |
36 *called = true; | 40 *called = true; |
37 } | 41 } |
38 | 42 |
39 ServiceWorkerContextCore::UnregistrationCallback MakeUnregisteredCallback( | 43 ServiceWorkerContextCore::UnregistrationCallback MakeUnregisteredCallback( |
40 bool* called) { | 44 bool* called) { |
41 return base::Bind(&CallCompletedCallback, called); | 45 return base::Bind(&CallCompletedCallback, called); |
42 } | 46 } |
43 | 47 |
44 } // namespace | 48 } // namespace |
45 | 49 |
46 class ServiceWorkerContextTest : public testing::Test { | 50 class ServiceWorkerContextTest : public testing::Test { |
47 public: | 51 public: |
48 ServiceWorkerContextTest() | 52 ServiceWorkerContextTest() |
49 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 53 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
50 | 54 |
51 virtual void SetUp() OVERRIDE { | 55 virtual void SetUp() OVERRIDE { |
52 context_.reset(new ServiceWorkerContextCore(base::FilePath(), NULL)); | 56 context_.reset(new ServiceWorkerContextCore(base::FilePath(), NULL)); |
57 helper_.reset(new EmbeddedWorkerTestHelper(context_.get())); | |
58 scoped_ptr<EmbeddedWorkerInstance> worker = | |
59 context_->embedded_worker_registry()->CreateWorker(); | |
60 | |
61 render_process_id_ = 99; | |
62 embedded_worker_id_ = worker->embedded_worker_id(); | |
kinuko
2014/01/29 09:56:20
These lines for embedded_worker_id_ are not necess
alecflett
2014/01/30 01:51:13
They are for the code as it is now - where StartWo
| |
63 helper_->SimulateAddProcess(embedded_worker_id_, render_process_id_); | |
53 } | 64 } |
54 | 65 |
55 virtual void TearDown() OVERRIDE { context_.reset(); } | 66 virtual void TearDown() OVERRIDE { |
67 helper_.reset(); | |
68 context_.reset(); | |
69 } | |
56 | 70 |
57 protected: | 71 protected: |
58 TestBrowserThreadBundle browser_thread_bundle_; | 72 TestBrowserThreadBundle browser_thread_bundle_; |
59 scoped_ptr<ServiceWorkerContextCore> context_; | 73 scoped_ptr<ServiceWorkerContextCore> context_; |
74 scoped_ptr<EmbeddedWorkerTestHelper> helper_; | |
75 int render_process_id_; | |
76 int embedded_worker_id_; | |
60 }; | 77 }; |
61 | 78 |
62 void RegistrationCallback( | 79 void RegistrationCallback( |
63 scoped_refptr<ServiceWorkerRegistration>* registration, | 80 scoped_refptr<ServiceWorkerRegistration>* registration, |
64 const scoped_refptr<ServiceWorkerRegistration>& result) { | 81 const scoped_refptr<ServiceWorkerRegistration>& result) { |
65 *registration = result; | 82 *registration = result; |
66 } | 83 } |
67 | 84 |
68 // Make sure basic registration is working. | 85 // Make sure basic registration is working. |
69 TEST_F(ServiceWorkerContextTest, Register) { | 86 TEST_F(ServiceWorkerContextTest, Register) { |
70 int64 registration_id = -1L; | 87 int64 registration_id = -1L; |
71 bool called = false; | 88 bool called = false; |
72 context_->RegisterServiceWorker( | 89 context_->RegisterServiceWorker( |
73 GURL("http://www.example.com/*"), | 90 GURL("http://www.example.com/*"), |
74 GURL("http://www.example.com/service_worker.js"), | 91 GURL("http://www.example.com/service_worker.js"), |
92 render_process_id_, | |
75 MakeRegisteredCallback(&called, ®istration_id)); | 93 MakeRegisteredCallback(&called, ®istration_id)); |
76 | 94 |
95 LOG(ERROR) << "RunUntilIdle..."; | |
77 ASSERT_FALSE(called); | 96 ASSERT_FALSE(called); |
78 base::RunLoop().RunUntilIdle(); | 97 base::RunLoop().RunUntilIdle(); |
79 ASSERT_TRUE(called); | 98 EXPECT_TRUE(called); |
80 | 99 |
81 ASSERT_NE(-1L, registration_id); | 100 EXPECT_EQ(1UL, helper_->ipc_sink()->message_count()); |
101 EXPECT_NE(-1L, registration_id); | |
82 } | 102 } |
83 | 103 |
84 // Make sure registrations are cleaned up when they are unregistered. | 104 // Make sure registrations are cleaned up when they are unregistered. |
85 TEST_F(ServiceWorkerContextTest, Unregister) { | 105 TEST_F(ServiceWorkerContextTest, Unregister) { |
86 GURL pattern("http://www.example.com/*"); | 106 GURL pattern("http://www.example.com/*"); |
87 | 107 |
88 bool called = false; | 108 bool called = false; |
89 int64 registration_id = -1L; | 109 int64 registration_id = -1L; |
90 context_->RegisterServiceWorker( | 110 context_->RegisterServiceWorker( |
91 pattern, | 111 pattern, |
92 GURL("http://www.example.com/service_worker.js"), | 112 GURL("http://www.example.com/service_worker.js"), |
113 render_process_id_, | |
93 MakeRegisteredCallback(&called, ®istration_id)); | 114 MakeRegisteredCallback(&called, ®istration_id)); |
94 | 115 |
95 ASSERT_FALSE(called); | 116 ASSERT_FALSE(called); |
96 base::RunLoop().RunUntilIdle(); | 117 base::RunLoop().RunUntilIdle(); |
97 ASSERT_TRUE(called); | 118 ASSERT_TRUE(called); |
98 | 119 |
99 called = false; | 120 called = false; |
100 context_->UnregisterServiceWorker(pattern, MakeUnregisteredCallback(&called)); | 121 context_->UnregisterServiceWorker( |
122 pattern, render_process_id_, MakeUnregisteredCallback(&called)); | |
101 | 123 |
102 ASSERT_FALSE(called); | 124 ASSERT_FALSE(called); |
103 base::RunLoop().RunUntilIdle(); | 125 base::RunLoop().RunUntilIdle(); |
104 ASSERT_TRUE(called); | 126 ASSERT_TRUE(called); |
105 } | 127 } |
106 | 128 |
107 // Make sure that when a new registration replaces an existing | 129 // Make sure that when a new registration replaces an existing |
108 // registration, that the old one is cleaned up. | 130 // registration, that the old one is cleaned up. |
109 TEST_F(ServiceWorkerContextTest, RegisterNewScript) { | 131 TEST_F(ServiceWorkerContextTest, RegisterNewScript) { |
110 GURL pattern("http://www.example.com/*"); | 132 GURL pattern("http://www.example.com/*"); |
111 | 133 |
112 bool called = false; | 134 bool called = false; |
113 int64 old_registration_id = -1L; | 135 int64 old_registration_id = -1L; |
114 context_->RegisterServiceWorker( | 136 context_->RegisterServiceWorker( |
115 pattern, | 137 pattern, |
116 GURL("http://www.example.com/service_worker.js"), | 138 GURL("http://www.example.com/service_worker.js"), |
139 render_process_id_, | |
117 MakeRegisteredCallback(&called, &old_registration_id)); | 140 MakeRegisteredCallback(&called, &old_registration_id)); |
118 | 141 |
119 ASSERT_FALSE(called); | 142 ASSERT_FALSE(called); |
120 base::RunLoop().RunUntilIdle(); | 143 base::RunLoop().RunUntilIdle(); |
121 ASSERT_TRUE(called); | 144 ASSERT_TRUE(called); |
122 | 145 |
123 called = false; | 146 called = false; |
124 int64 new_registration_id = -1L; | 147 int64 new_registration_id = -1L; |
125 context_->RegisterServiceWorker( | 148 context_->RegisterServiceWorker( |
126 pattern, | 149 pattern, |
127 GURL("http://www.example.com/service_worker_new.js"), | 150 GURL("http://www.example.com/service_worker_new.js"), |
151 render_process_id_, | |
128 MakeRegisteredCallback(&called, &new_registration_id)); | 152 MakeRegisteredCallback(&called, &new_registration_id)); |
129 | 153 |
130 ASSERT_FALSE(called); | 154 ASSERT_FALSE(called); |
131 base::RunLoop().RunUntilIdle(); | 155 base::RunLoop().RunUntilIdle(); |
132 ASSERT_TRUE(called); | 156 ASSERT_TRUE(called); |
133 | 157 |
134 ASSERT_NE(old_registration_id, new_registration_id); | 158 ASSERT_NE(old_registration_id, new_registration_id); |
135 } | 159 } |
136 | 160 |
137 // Make sure that when registering a duplicate pattern+script_url | 161 // Make sure that when registering a duplicate pattern+script_url |
138 // combination, that the same registration is used. | 162 // combination, that the same registration is used. |
139 TEST_F(ServiceWorkerContextTest, RegisterDuplicateScript) { | 163 TEST_F(ServiceWorkerContextTest, RegisterDuplicateScript) { |
140 GURL pattern("http://www.example.com/*"); | 164 GURL pattern("http://www.example.com/*"); |
141 GURL script_url("http://www.example.com/service_worker.js"); | 165 GURL script_url("http://www.example.com/service_worker.js"); |
142 | 166 |
143 bool called = false; | 167 bool called = false; |
144 int64 old_registration_id = -1L; | 168 int64 old_registration_id = -1L; |
145 context_->RegisterServiceWorker( | 169 context_->RegisterServiceWorker( |
146 pattern, | 170 pattern, |
147 script_url, | 171 script_url, |
172 render_process_id_, | |
148 MakeRegisteredCallback(&called, &old_registration_id)); | 173 MakeRegisteredCallback(&called, &old_registration_id)); |
149 | 174 |
150 ASSERT_FALSE(called); | 175 ASSERT_FALSE(called); |
151 base::RunLoop().RunUntilIdle(); | 176 base::RunLoop().RunUntilIdle(); |
152 ASSERT_TRUE(called); | 177 ASSERT_TRUE(called); |
153 | 178 |
154 called = false; | 179 called = false; |
155 int64 new_registration_id = -1L; | 180 int64 new_registration_id = -1L; |
156 context_->RegisterServiceWorker( | 181 context_->RegisterServiceWorker( |
157 pattern, | 182 pattern, |
158 script_url, | 183 script_url, |
184 render_process_id_, | |
159 MakeRegisteredCallback(&called, &new_registration_id)); | 185 MakeRegisteredCallback(&called, &new_registration_id)); |
160 | 186 |
161 ASSERT_FALSE(called); | 187 ASSERT_FALSE(called); |
162 base::RunLoop().RunUntilIdle(); | 188 base::RunLoop().RunUntilIdle(); |
163 ASSERT_TRUE(called); | 189 ASSERT_TRUE(called); |
164 | 190 |
165 ASSERT_EQ(old_registration_id, new_registration_id); | 191 ASSERT_EQ(old_registration_id, new_registration_id); |
166 } | 192 } |
167 | 193 |
168 } // namespace content | 194 } // namespace content |
OLD | NEW |