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