OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "base/callback.h" | 5 #include "base/callback.h" |
6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
7 #include "content/browser/geofencing/geofencing_manager.h" | 7 #include "content/browser/geofencing/geofencing_manager.h" |
8 #include "content/browser/geofencing/geofencing_provider.h" | 8 #include "content/browser/geofencing/geofencing_service.h" |
9 #include "content/public/browser/browser_thread.h" | 9 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
10 #include "content/public/test/test_browser_thread.h" | 10 #include "content/public/test/test_browser_thread_bundle.h" |
11 #include "content/public/test/test_utils.h" | 11 #include "content/public/test/test_utils.h" |
12 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h" | 14 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h" |
15 #include "url/gurl.h" | |
16 | 15 |
17 using blink::WebCircularGeofencingRegion; | 16 using blink::WebCircularGeofencingRegion; |
18 typedef std::map<std::string, WebCircularGeofencingRegion> RegionMap; | 17 typedef std::map<std::string, WebCircularGeofencingRegion> RegionMap; |
19 | 18 |
20 namespace { | 19 namespace { |
21 | 20 |
22 static const char* kTestRegionId = "region-id"; | 21 static const char* kTestRegionId = "region-id"; |
23 static const int64 kTestServiceWorkerRegistrationId = 123; | 22 static const int64 kTestServiceWorkerRegistrationId = 123; |
24 static const int64 kTestServiceWorkerRegistrationId2 = 456; | 23 static const int64 kTestServiceWorkerRegistrationId2 = 456; |
| 24 static const int64 kTestGeofencingRegistrationId = 42; |
| 25 static const int64 kTestGeofencingRegistrationId2 = 43; |
25 | 26 |
26 bool RegionsMatch(const WebCircularGeofencingRegion& expected, | 27 bool RegionsMatch(const WebCircularGeofencingRegion& expected, |
27 const WebCircularGeofencingRegion& arg) { | 28 const WebCircularGeofencingRegion& arg) { |
28 return testing::Matches(expected.latitude)(arg.latitude) && | 29 return testing::Matches(expected.latitude)(arg.latitude) && |
29 testing::Matches(expected.longitude)(arg.longitude) && | 30 testing::Matches(expected.longitude)(arg.longitude) && |
30 testing::Matches(expected.radius)(arg.radius); | 31 testing::Matches(expected.radius)(arg.radius); |
31 } | 32 } |
32 } | 33 } |
33 | 34 |
34 namespace content { | 35 namespace content { |
35 | 36 |
36 class TestGeofencingProvider : public GeofencingProvider { | 37 class TestGeofencingService : public GeofencingServiceInterface { |
37 public: | 38 public: |
| 39 MOCK_METHOD0(IsServiceAvailable, bool()); |
38 MOCK_METHOD2(RegisterRegion, | 40 MOCK_METHOD2(RegisterRegion, |
39 void(const WebCircularGeofencingRegion& region, | 41 int64(const WebCircularGeofencingRegion& region, |
40 const RegisterCallback& callback)); | 42 GeofencingRegistrationDelegate* delegate)); |
41 MOCK_METHOD1(UnregisterRegion, void(int registration_id)); | 43 MOCK_METHOD1(UnregisterRegion, void(int64 geofencing_registration_id)); |
42 }; | 44 }; |
43 | 45 |
44 ACTION_P2(CallRegisterCallback, status, id) { | 46 ACTION_P(SaveDelegate, delegate) { |
45 arg1.Run(status, id); | 47 *delegate = arg1; |
46 } | 48 } |
47 | 49 |
48 ACTION_P(SaveRegisterCallback, callback) { | 50 ACTION_P(QuitRunner, runner) { |
49 *callback = arg1; | 51 runner->Quit(); |
50 } | 52 } |
51 | 53 |
52 MATCHER_P(WebCircularGeofencingRegionEq, expected, "") { | 54 MATCHER_P(WebCircularGeofencingRegionEq, expected, "") { |
53 return RegionsMatch(expected, arg); | 55 return RegionsMatch(expected, arg); |
54 } | 56 } |
55 | 57 |
56 class StatusCatcher { | 58 class StatusCatcher { |
57 public: | 59 public: |
58 StatusCatcher() : was_called_(false), runner_(new MessageLoopRunner()) {} | 60 StatusCatcher() : was_called_(false), runner_(new MessageLoopRunner()) {} |
59 | 61 |
(...skipping 11 matching lines...) Expand all Loading... |
71 } | 73 } |
72 | 74 |
73 private: | 75 private: |
74 bool was_called_; | 76 bool was_called_; |
75 GeofencingStatus result_; | 77 GeofencingStatus result_; |
76 scoped_refptr<MessageLoopRunner> runner_; | 78 scoped_refptr<MessageLoopRunner> runner_; |
77 }; | 79 }; |
78 | 80 |
79 class GeofencingManagerTest : public testing::Test { | 81 class GeofencingManagerTest : public testing::Test { |
80 public: | 82 public: |
81 GeofencingManagerTest() | 83 GeofencingManagerTest() : service_(nullptr) { |
82 : message_loop_(), | |
83 io_thread_(BrowserThread::IO, &message_loop_), | |
84 provider_(0), | |
85 manager_(0), | |
86 test_origin_("https://example.com/") { | |
87 test_region_.latitude = 37.421999; | 84 test_region_.latitude = 37.421999; |
88 test_region_.longitude = -122.084015; | 85 test_region_.longitude = -122.084015; |
89 test_region_.radius = 100; | 86 test_region_.radius = 100; |
90 expected_regions_[kTestRegionId] = test_region_; | 87 expected_regions_[kTestRegionId] = test_region_; |
91 } | 88 } |
92 | 89 |
93 virtual void SetUp() { manager_ = new GeofencingManager(); } | 90 virtual void SetUp() { |
| 91 service_ = new TestGeofencingService(); |
| 92 ON_CALL(*service_, IsServiceAvailable()) |
| 93 .WillByDefault(testing::Return(false)); |
| 94 manager_ = new GeofencingManager(nullptr /* ServiceWorkerContextWrapper */); |
| 95 manager_->SetServiceForTests(service_); |
| 96 } |
94 | 97 |
95 virtual void TearDown() { delete manager_; } | 98 virtual void TearDown() { |
| 99 manager_ = nullptr; |
| 100 delete service_; |
| 101 service_ = nullptr; |
| 102 } |
96 | 103 |
97 void SetProviderForTests() { | 104 void SetHasProviderForTests() { |
98 provider_ = new TestGeofencingProvider(); | 105 ON_CALL(*service_, IsServiceAvailable()) |
99 manager_->SetProviderForTests(scoped_ptr<GeofencingProvider>(provider_)); | 106 .WillByDefault(testing::Return(true)); |
100 } | 107 } |
101 | 108 |
102 GeofencingStatus RegisterRegionSync( | 109 GeofencingStatus RegisterRegionSync( |
103 int64 service_worker_registration_id, | 110 int64 service_worker_registration_id, |
104 const std::string& id, | 111 const std::string& id, |
105 const WebCircularGeofencingRegion& region) { | 112 const WebCircularGeofencingRegion& region) { |
106 StatusCatcher result; | 113 StatusCatcher result; |
107 manager_->RegisterRegion( | 114 manager_->RegisterRegion( |
108 nullptr, /* browser_context */ | |
109 service_worker_registration_id, | 115 service_worker_registration_id, |
110 test_origin_, | |
111 id, | 116 id, |
112 region, | 117 region, |
113 base::Bind(&StatusCatcher::Done, base::Unretained(&result))); | 118 base::Bind(&StatusCatcher::Done, base::Unretained(&result))); |
114 return result.Wait(); | 119 return result.Wait(); |
115 } | 120 } |
116 | 121 |
117 GeofencingStatus RegisterRegionSyncWithProviderResult( | 122 GeofencingStatus RegisterRegionSyncWithServiceResult( |
118 int64 service_worker_registration_id, | 123 int64 service_worker_registration_id, |
119 const std::string& id, | 124 const std::string& id, |
120 const WebCircularGeofencingRegion& region, | 125 const WebCircularGeofencingRegion& region, |
121 GeofencingStatus provider_status, | 126 GeofencingStatus service_status, |
122 int provider_result) { | 127 int64 geofencing_registration_id) { |
123 StatusCatcher result; | 128 StatusCatcher result; |
| 129 GeofencingRegistrationDelegate* delegate = 0; |
124 EXPECT_CALL( | 130 EXPECT_CALL( |
125 *provider_, | 131 *service_, |
126 RegisterRegion(WebCircularGeofencingRegionEq(region), testing::_)) | 132 RegisterRegion(WebCircularGeofencingRegionEq(region), testing::_)) |
127 .WillOnce(CallRegisterCallback(provider_status, provider_result)); | 133 .WillOnce(testing::DoAll(SaveDelegate(&delegate), |
| 134 testing::Return(geofencing_registration_id))); |
128 manager_->RegisterRegion( | 135 manager_->RegisterRegion( |
129 nullptr, /* browser_context */ | |
130 service_worker_registration_id, | 136 service_worker_registration_id, |
131 test_origin_, | |
132 id, | 137 id, |
133 region, | 138 region, |
134 base::Bind(&StatusCatcher::Done, base::Unretained(&result))); | 139 base::Bind(&StatusCatcher::Done, base::Unretained(&result))); |
| 140 CHECK(delegate); |
| 141 delegate->RegistrationFinished(geofencing_registration_id, service_status); |
135 return result.Wait(); | 142 return result.Wait(); |
136 } | 143 } |
137 | 144 |
138 GeofencingStatus UnregisterRegionSync(int64 service_worker_registration_id, | 145 GeofencingStatus UnregisterRegionSync(int64 service_worker_registration_id, |
139 const std::string& id, | 146 const std::string& id, |
140 bool should_call_provider, | 147 bool should_call_service, |
141 int provider_id = 0) { | 148 int64 geofencing_registration_id = 0) { |
142 StatusCatcher result; | 149 StatusCatcher result; |
143 if (should_call_provider) { | 150 if (should_call_service) { |
144 EXPECT_CALL(*provider_, UnregisterRegion(provider_id)); | 151 EXPECT_CALL(*service_, UnregisterRegion(geofencing_registration_id)); |
145 } | 152 } |
146 manager_->UnregisterRegion( | 153 manager_->UnregisterRegion( |
147 nullptr, /* browser_context */ | |
148 service_worker_registration_id, | 154 service_worker_registration_id, |
149 test_origin_, | |
150 id, | 155 id, |
151 base::Bind(&StatusCatcher::Done, base::Unretained(&result))); | 156 base::Bind(&StatusCatcher::Done, base::Unretained(&result))); |
152 return result.Wait(); | 157 return result.Wait(); |
153 } | 158 } |
154 | 159 |
155 void VerifyRegions(int64 service_worker_registration_id, | 160 void VerifyRegions(int64 service_worker_registration_id, |
156 const RegionMap& expected_regions) { | 161 const RegionMap& expected_regions) { |
157 RegionMap regions; | 162 RegionMap regions; |
158 EXPECT_EQ(GeofencingStatus::GEOFENCING_STATUS_OK, | 163 EXPECT_EQ(GEOFENCING_STATUS_OK, |
159 manager_->GetRegisteredRegions(nullptr, /* browser_context */ | 164 manager_->GetRegisteredRegions(service_worker_registration_id, |
160 service_worker_registration_id, | |
161 test_origin_, | |
162 ®ions)); | 165 ®ions)); |
163 EXPECT_EQ(expected_regions.size(), regions.size()); | 166 EXPECT_EQ(expected_regions.size(), regions.size()); |
164 for (RegionMap::const_iterator it = expected_regions.begin(); | 167 for (RegionMap::const_iterator it = expected_regions.begin(); |
165 it != expected_regions.end(); | 168 it != expected_regions.end(); |
166 ++it) { | 169 ++it) { |
167 EXPECT_THAT(regions[it->first], | 170 EXPECT_THAT(regions[it->first], |
168 WebCircularGeofencingRegionEq(it->second)); | 171 WebCircularGeofencingRegionEq(it->second)); |
169 } | 172 } |
170 } | 173 } |
171 | 174 |
172 protected: | 175 protected: |
173 base::MessageLoop message_loop_; | 176 TestBrowserThreadBundle threads_; |
174 TestBrowserThread io_thread_; | 177 TestGeofencingService* service_; |
175 TestGeofencingProvider* provider_; | 178 scoped_refptr<GeofencingManager> manager_; |
176 GeofencingManager* manager_; | |
177 | 179 |
178 WebCircularGeofencingRegion test_region_; | 180 WebCircularGeofencingRegion test_region_; |
179 RegionMap expected_regions_; | 181 RegionMap expected_regions_; |
180 GURL test_origin_; | |
181 }; | 182 }; |
182 | 183 |
183 TEST_F(GeofencingManagerTest, RegisterRegion_NoProvider) { | 184 TEST_F(GeofencingManagerTest, RegisterRegion_NoService) { |
184 EXPECT_EQ(GeofencingStatus:: | 185 EXPECT_EQ(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE, |
185 GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE, | |
186 RegisterRegionSync( | 186 RegisterRegionSync( |
187 kTestServiceWorkerRegistrationId, kTestRegionId, test_region_)); | 187 kTestServiceWorkerRegistrationId, kTestRegionId, test_region_)); |
188 } | 188 } |
189 | 189 |
190 TEST_F(GeofencingManagerTest, UnregisterRegion_NoProvider) { | 190 TEST_F(GeofencingManagerTest, UnregisterRegion_NoService) { |
191 EXPECT_EQ(GeofencingStatus:: | 191 EXPECT_EQ(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE, |
192 GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE, | |
193 UnregisterRegionSync( | 192 UnregisterRegionSync( |
194 kTestServiceWorkerRegistrationId, kTestRegionId, false)); | 193 kTestServiceWorkerRegistrationId, kTestRegionId, false)); |
195 } | 194 } |
196 | 195 |
197 TEST_F(GeofencingManagerTest, GetRegisteredRegions_NoProvider) { | 196 TEST_F(GeofencingManagerTest, GetRegisteredRegions_NoService) { |
198 RegionMap regions; | 197 RegionMap regions; |
199 EXPECT_EQ(GeofencingStatus:: | 198 EXPECT_EQ(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE, |
200 GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE, | 199 manager_->GetRegisteredRegions(kTestServiceWorkerRegistrationId, |
201 manager_->GetRegisteredRegions(nullptr, /* browser_context */ | |
202 kTestServiceWorkerRegistrationId, | |
203 test_origin_, | |
204 ®ions)); | 200 ®ions)); |
205 EXPECT_TRUE(regions.empty()); | 201 EXPECT_TRUE(regions.empty()); |
206 } | 202 } |
207 | 203 |
208 TEST_F(GeofencingManagerTest, RegisterRegion_FailsInProvider) { | 204 TEST_F(GeofencingManagerTest, RegisterRegion_FailsInService) { |
209 SetProviderForTests(); | 205 SetHasProviderForTests(); |
210 EXPECT_EQ( | 206 EXPECT_EQ( |
211 GeofencingStatus::GEOFENCING_STATUS_ERROR, | 207 GEOFENCING_STATUS_ERROR, |
212 RegisterRegionSyncWithProviderResult(kTestServiceWorkerRegistrationId, | 208 RegisterRegionSyncWithServiceResult(kTestServiceWorkerRegistrationId, |
213 kTestRegionId, | 209 kTestRegionId, |
214 test_region_, | 210 test_region_, |
215 GEOFENCING_STATUS_ERROR, | 211 GEOFENCING_STATUS_ERROR, |
216 -1)); | 212 -1)); |
217 } | 213 } |
218 | 214 |
219 TEST_F(GeofencingManagerTest, RegisterRegion_SucceedsInProvider) { | 215 TEST_F(GeofencingManagerTest, RegisterRegion_SucceedsInService) { |
220 SetProviderForTests(); | 216 SetHasProviderForTests(); |
221 EXPECT_EQ( | 217 EXPECT_EQ( |
222 GeofencingStatus::GEOFENCING_STATUS_OK, | 218 GEOFENCING_STATUS_OK, |
223 RegisterRegionSyncWithProviderResult(kTestServiceWorkerRegistrationId, | 219 RegisterRegionSyncWithServiceResult(kTestServiceWorkerRegistrationId, |
224 kTestRegionId, | 220 kTestRegionId, |
225 test_region_, | 221 test_region_, |
226 GEOFENCING_STATUS_OK, | 222 GEOFENCING_STATUS_OK, |
227 0)); | 223 kTestGeofencingRegistrationId)); |
228 VerifyRegions(kTestServiceWorkerRegistrationId, expected_regions_); | 224 VerifyRegions(kTestServiceWorkerRegistrationId, expected_regions_); |
229 } | 225 } |
230 | 226 |
231 TEST_F(GeofencingManagerTest, RegisterRegion_AlreadyRegistered) { | 227 TEST_F(GeofencingManagerTest, RegisterRegion_AlreadyRegistered) { |
232 SetProviderForTests(); | 228 SetHasProviderForTests(); |
233 EXPECT_EQ( | 229 EXPECT_EQ( |
234 GeofencingStatus::GEOFENCING_STATUS_OK, | 230 GEOFENCING_STATUS_OK, |
235 RegisterRegionSyncWithProviderResult(kTestServiceWorkerRegistrationId, | 231 RegisterRegionSyncWithServiceResult(kTestServiceWorkerRegistrationId, |
236 kTestRegionId, | 232 kTestRegionId, |
237 test_region_, | 233 test_region_, |
238 GEOFENCING_STATUS_OK, | 234 GEOFENCING_STATUS_OK, |
239 0)); | 235 kTestGeofencingRegistrationId)); |
240 VerifyRegions(kTestServiceWorkerRegistrationId, expected_regions_); | 236 VerifyRegions(kTestServiceWorkerRegistrationId, expected_regions_); |
241 | 237 |
242 WebCircularGeofencingRegion region2; | 238 WebCircularGeofencingRegion region2; |
243 region2.latitude = 43.2; | 239 region2.latitude = 43.2; |
244 region2.longitude = 1.45; | 240 region2.longitude = 1.45; |
245 region2.radius = 8.5; | 241 region2.radius = 8.5; |
246 EXPECT_EQ(GeofencingStatus::GEOFENCING_STATUS_ERROR, | 242 EXPECT_EQ(GEOFENCING_STATUS_ERROR, |
247 RegisterRegionSync( | 243 RegisterRegionSync( |
248 kTestServiceWorkerRegistrationId, kTestRegionId, region2)); | 244 kTestServiceWorkerRegistrationId, kTestRegionId, region2)); |
249 VerifyRegions(kTestServiceWorkerRegistrationId, expected_regions_); | 245 VerifyRegions(kTestServiceWorkerRegistrationId, expected_regions_); |
250 } | 246 } |
251 | 247 |
252 TEST_F(GeofencingManagerTest, UnregisterRegion_NotRegistered) { | 248 TEST_F(GeofencingManagerTest, UnregisterRegion_NotRegistered) { |
253 SetProviderForTests(); | 249 SetHasProviderForTests(); |
254 EXPECT_EQ(GeofencingStatus::GEOFENCING_STATUS_ERROR, | 250 EXPECT_EQ(GEOFENCING_STATUS_ERROR, |
255 UnregisterRegionSync( | 251 UnregisterRegionSync( |
256 kTestServiceWorkerRegistrationId, kTestRegionId, false)); | 252 kTestServiceWorkerRegistrationId, kTestRegionId, false)); |
257 } | 253 } |
258 | 254 |
259 TEST_F(GeofencingManagerTest, UnregisterRegion_Success) { | 255 TEST_F(GeofencingManagerTest, UnregisterRegion_Success) { |
260 SetProviderForTests(); | 256 SetHasProviderForTests(); |
261 int provider_id = 123; | 257 |
262 | 258 EXPECT_EQ( |
263 EXPECT_EQ( | 259 GEOFENCING_STATUS_OK, |
264 GeofencingStatus::GEOFENCING_STATUS_OK, | 260 RegisterRegionSyncWithServiceResult(kTestServiceWorkerRegistrationId, |
265 RegisterRegionSyncWithProviderResult(kTestServiceWorkerRegistrationId, | 261 kTestRegionId, |
266 kTestRegionId, | 262 test_region_, |
267 test_region_, | 263 GEOFENCING_STATUS_OK, |
268 GEOFENCING_STATUS_OK, | 264 kTestGeofencingRegistrationId)); |
269 provider_id)); | 265 |
270 | 266 EXPECT_EQ(GEOFENCING_STATUS_OK, |
271 EXPECT_EQ( | 267 UnregisterRegionSync(kTestServiceWorkerRegistrationId, |
272 GeofencingStatus::GEOFENCING_STATUS_OK, | 268 kTestRegionId, |
273 UnregisterRegionSync( | 269 true, |
274 kTestServiceWorkerRegistrationId, kTestRegionId, true, provider_id)); | 270 kTestGeofencingRegistrationId)); |
275 VerifyRegions(kTestServiceWorkerRegistrationId, RegionMap()); | 271 VerifyRegions(kTestServiceWorkerRegistrationId, RegionMap()); |
276 } | 272 } |
277 | 273 |
278 TEST_F(GeofencingManagerTest, GetRegisteredRegions_RegistrationInProgress) { | 274 TEST_F(GeofencingManagerTest, GetRegisteredRegions_RegistrationInProgress) { |
279 SetProviderForTests(); | 275 SetHasProviderForTests(); |
280 StatusCatcher result; | 276 StatusCatcher result; |
281 GeofencingProvider::RegisterCallback callback; | 277 GeofencingRegistrationDelegate* delegate = nullptr; |
282 | 278 |
283 EXPECT_CALL( | 279 EXPECT_CALL( |
284 *provider_, | 280 *service_, |
285 RegisterRegion(WebCircularGeofencingRegionEq(test_region_), testing::_)) | 281 RegisterRegion(WebCircularGeofencingRegionEq(test_region_), testing::_)) |
286 .WillOnce(SaveRegisterCallback(&callback)); | 282 .WillOnce(testing::DoAll(SaveDelegate(&delegate), |
| 283 testing::Return(kTestGeofencingRegistrationId))); |
287 manager_->RegisterRegion( | 284 manager_->RegisterRegion( |
288 nullptr, /* browser_context */ | |
289 kTestServiceWorkerRegistrationId, | 285 kTestServiceWorkerRegistrationId, |
290 test_origin_, | |
291 kTestRegionId, | 286 kTestRegionId, |
292 test_region_, | 287 test_region_, |
293 base::Bind(&StatusCatcher::Done, base::Unretained(&result))); | 288 base::Bind(&StatusCatcher::Done, base::Unretained(&result))); |
294 | 289 |
295 // At this point the manager should have tried registering the region with | 290 // At this point the manager should have tried registering the region with |
296 // the provider, resulting in |callback| being set. Until the callback is | 291 // the service, resulting in |delegate| being set. Until the callback is |
297 // called the registration is not complete though. | 292 // called the registration is not complete though. |
298 EXPECT_FALSE(callback.is_null()); | 293 EXPECT_NE(delegate, nullptr); |
299 VerifyRegions(kTestServiceWorkerRegistrationId, RegionMap()); | 294 VerifyRegions(kTestServiceWorkerRegistrationId, RegionMap()); |
300 | 295 |
301 // Now call the callback, and verify the registration completed succesfully. | 296 // Now call the callback, and verify the registration completed succesfully. |
302 callback.Run(GEOFENCING_STATUS_OK, 123); | 297 delegate->RegistrationFinished(kTestGeofencingRegistrationId, |
303 EXPECT_EQ(GeofencingStatus::GEOFENCING_STATUS_OK, result.Wait()); | 298 GEOFENCING_STATUS_OK); |
| 299 EXPECT_EQ(GEOFENCING_STATUS_OK, result.Wait()); |
304 VerifyRegions(kTestServiceWorkerRegistrationId, expected_regions_); | 300 VerifyRegions(kTestServiceWorkerRegistrationId, expected_regions_); |
305 } | 301 } |
306 | 302 |
307 TEST_F(GeofencingManagerTest, UnregisterRegion_RegistrationInProgress) { | 303 TEST_F(GeofencingManagerTest, UnregisterRegion_RegistrationInProgress) { |
308 SetProviderForTests(); | 304 SetHasProviderForTests(); |
309 StatusCatcher result; | 305 StatusCatcher result; |
310 GeofencingProvider::RegisterCallback callback; | 306 GeofencingRegistrationDelegate* delegate = nullptr; |
311 | 307 |
312 EXPECT_CALL( | 308 EXPECT_CALL( |
313 *provider_, | 309 *service_, |
314 RegisterRegion(WebCircularGeofencingRegionEq(test_region_), testing::_)) | 310 RegisterRegion(WebCircularGeofencingRegionEq(test_region_), testing::_)) |
315 .WillOnce(SaveRegisterCallback(&callback)); | 311 .WillOnce(testing::DoAll(SaveDelegate(&delegate), |
| 312 testing::Return(kTestGeofencingRegistrationId))); |
316 manager_->RegisterRegion( | 313 manager_->RegisterRegion( |
317 nullptr, /* browser_context */ | |
318 kTestServiceWorkerRegistrationId, | 314 kTestServiceWorkerRegistrationId, |
319 test_origin_, | |
320 kTestRegionId, | 315 kTestRegionId, |
321 test_region_, | 316 test_region_, |
322 base::Bind(&StatusCatcher::Done, base::Unretained(&result))); | 317 base::Bind(&StatusCatcher::Done, base::Unretained(&result))); |
323 | 318 |
324 // At this point the manager should have tried registering the region with | 319 // At this point the manager should have tried registering the region with |
325 // the provider, resulting in |callback| being set. Until the callback is | 320 // the service, resulting in |delegate| being set. Until the callback is |
326 // called the registration is not complete though. | 321 // called the registration is not complete though. |
327 EXPECT_FALSE(callback.is_null()); | 322 EXPECT_NE(delegate, nullptr); |
328 | 323 |
329 EXPECT_EQ(GeofencingStatus::GEOFENCING_STATUS_ERROR, | 324 EXPECT_EQ(GEOFENCING_STATUS_ERROR, |
330 UnregisterRegionSync( | 325 UnregisterRegionSync( |
331 kTestServiceWorkerRegistrationId, kTestRegionId, false)); | 326 kTestServiceWorkerRegistrationId, kTestRegionId, false)); |
332 } | 327 } |
333 | 328 |
334 TEST_F(GeofencingManagerTest, GetRegisteredRegions_NoRegions) { | 329 TEST_F(GeofencingManagerTest, GetRegisteredRegions_NoRegions) { |
335 SetProviderForTests(); | 330 SetHasProviderForTests(); |
336 VerifyRegions(kTestServiceWorkerRegistrationId, RegionMap()); | 331 VerifyRegions(kTestServiceWorkerRegistrationId, RegionMap()); |
337 } | 332 } |
338 | 333 |
339 TEST_F(GeofencingManagerTest, RegisterRegion_SeparateServiceWorkers) { | 334 TEST_F(GeofencingManagerTest, RegisterRegion_SeparateServiceWorkers) { |
340 SetProviderForTests(); | 335 SetHasProviderForTests(); |
341 int provider_id1 = 12; | 336 |
342 int provider_id2 = 34; | 337 EXPECT_EQ( |
343 | 338 GEOFENCING_STATUS_OK, |
344 EXPECT_EQ( | 339 RegisterRegionSyncWithServiceResult(kTestServiceWorkerRegistrationId, |
345 GeofencingStatus::GEOFENCING_STATUS_OK, | 340 kTestRegionId, |
346 RegisterRegionSyncWithProviderResult(kTestServiceWorkerRegistrationId, | 341 test_region_, |
347 kTestRegionId, | 342 GEOFENCING_STATUS_OK, |
348 test_region_, | 343 kTestGeofencingRegistrationId)); |
349 GEOFENCING_STATUS_OK, | |
350 provider_id1)); | |
351 | 344 |
352 VerifyRegions(kTestServiceWorkerRegistrationId, expected_regions_); | 345 VerifyRegions(kTestServiceWorkerRegistrationId, expected_regions_); |
353 VerifyRegions(kTestServiceWorkerRegistrationId2, RegionMap()); | 346 VerifyRegions(kTestServiceWorkerRegistrationId2, RegionMap()); |
354 | 347 |
355 EXPECT_EQ( | 348 EXPECT_EQ( |
356 GeofencingStatus::GEOFENCING_STATUS_OK, | 349 GEOFENCING_STATUS_OK, |
357 RegisterRegionSyncWithProviderResult(kTestServiceWorkerRegistrationId2, | 350 RegisterRegionSyncWithServiceResult(kTestServiceWorkerRegistrationId2, |
358 kTestRegionId, | 351 kTestRegionId, |
359 test_region_, | 352 test_region_, |
360 GEOFENCING_STATUS_OK, | 353 GEOFENCING_STATUS_OK, |
361 provider_id2)); | 354 kTestGeofencingRegistrationId2)); |
362 | 355 |
363 VerifyRegions(kTestServiceWorkerRegistrationId, expected_regions_); | 356 VerifyRegions(kTestServiceWorkerRegistrationId, expected_regions_); |
364 VerifyRegions(kTestServiceWorkerRegistrationId2, expected_regions_); | 357 VerifyRegions(kTestServiceWorkerRegistrationId2, expected_regions_); |
365 } | 358 } |
366 | 359 |
367 TEST_F(GeofencingManagerTest, UnregisterRegion_SeparateServiceWorkers) { | 360 TEST_F(GeofencingManagerTest, UnregisterRegion_SeparateServiceWorkers) { |
368 SetProviderForTests(); | 361 SetHasProviderForTests(); |
369 int provider_id1 = 12; | 362 |
370 int provider_id2 = 34; | 363 EXPECT_EQ( |
371 | 364 GEOFENCING_STATUS_OK, |
372 EXPECT_EQ( | 365 RegisterRegionSyncWithServiceResult(kTestServiceWorkerRegistrationId, |
373 GeofencingStatus::GEOFENCING_STATUS_OK, | 366 kTestRegionId, |
374 RegisterRegionSyncWithProviderResult(kTestServiceWorkerRegistrationId, | 367 test_region_, |
375 kTestRegionId, | 368 GEOFENCING_STATUS_OK, |
376 test_region_, | 369 kTestGeofencingRegistrationId)); |
377 GEOFENCING_STATUS_OK, | 370 EXPECT_EQ( |
378 provider_id1)); | 371 GEOFENCING_STATUS_OK, |
379 EXPECT_EQ( | 372 RegisterRegionSyncWithServiceResult(kTestServiceWorkerRegistrationId2, |
380 GeofencingStatus::GEOFENCING_STATUS_OK, | 373 kTestRegionId, |
381 RegisterRegionSyncWithProviderResult(kTestServiceWorkerRegistrationId2, | 374 test_region_, |
382 kTestRegionId, | 375 GEOFENCING_STATUS_OK, |
383 test_region_, | 376 kTestGeofencingRegistrationId2)); |
384 GEOFENCING_STATUS_OK, | 377 |
385 provider_id2)); | 378 EXPECT_EQ(GEOFENCING_STATUS_OK, |
386 | 379 UnregisterRegionSync(kTestServiceWorkerRegistrationId, |
387 EXPECT_EQ( | 380 kTestRegionId, |
388 GeofencingStatus::GEOFENCING_STATUS_OK, | 381 true, |
389 UnregisterRegionSync( | 382 kTestGeofencingRegistrationId)); |
390 kTestServiceWorkerRegistrationId, kTestRegionId, true, provider_id1)); | |
391 | 383 |
392 VerifyRegions(kTestServiceWorkerRegistrationId, RegionMap()); | 384 VerifyRegions(kTestServiceWorkerRegistrationId, RegionMap()); |
393 VerifyRegions(kTestServiceWorkerRegistrationId2, expected_regions_); | 385 VerifyRegions(kTestServiceWorkerRegistrationId2, expected_regions_); |
394 | 386 |
395 EXPECT_EQ(GeofencingStatus::GEOFENCING_STATUS_OK, | 387 EXPECT_EQ(GEOFENCING_STATUS_OK, |
396 UnregisterRegionSync(kTestServiceWorkerRegistrationId2, | 388 UnregisterRegionSync(kTestServiceWorkerRegistrationId2, |
397 kTestRegionId, | 389 kTestRegionId, |
398 true, | 390 true, |
399 provider_id2)); | 391 kTestGeofencingRegistrationId2)); |
400 | 392 |
401 VerifyRegions(kTestServiceWorkerRegistrationId, RegionMap()); | 393 VerifyRegions(kTestServiceWorkerRegistrationId, RegionMap()); |
402 VerifyRegions(kTestServiceWorkerRegistrationId2, RegionMap()); | 394 VerifyRegions(kTestServiceWorkerRegistrationId2, RegionMap()); |
403 } | 395 } |
404 | 396 |
| 397 TEST_F(GeofencingManagerTest, ShutdownCleansRegistrations) { |
| 398 SetHasProviderForTests(); |
| 399 scoped_refptr<MessageLoopRunner> runner(new MessageLoopRunner()); |
| 400 EXPECT_EQ( |
| 401 GEOFENCING_STATUS_OK, |
| 402 RegisterRegionSyncWithServiceResult(kTestServiceWorkerRegistrationId, |
| 403 kTestRegionId, |
| 404 test_region_, |
| 405 GEOFENCING_STATUS_OK, |
| 406 kTestGeofencingRegistrationId)); |
| 407 |
| 408 EXPECT_CALL(*service_, UnregisterRegion(kTestGeofencingRegistrationId)) |
| 409 .WillOnce(QuitRunner(runner)); |
| 410 manager_->Shutdown(); |
| 411 runner->Run(); |
| 412 } |
| 413 |
405 } // namespace content | 414 } // namespace content |
OLD | NEW |