| 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 <stdint.h> |
| 6 |
| 5 #include "base/callback.h" | 7 #include "base/callback.h" |
| 6 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 7 #include "content/browser/geofencing/geofencing_manager.h" | 9 #include "content/browser/geofencing/geofencing_manager.h" |
| 8 #include "content/browser/geofencing/geofencing_service.h" | 10 #include "content/browser/geofencing/geofencing_service.h" |
| 9 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 11 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 10 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 12 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 11 #include "content/public/test/test_browser_thread_bundle.h" | 13 #include "content/public/test/test_browser_thread_bundle.h" |
| 12 #include "content/public/test/test_utils.h" | 14 #include "content/public/test/test_utils.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h" | 17 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h" |
| 16 | 18 |
| 17 using blink::WebCircularGeofencingRegion; | 19 using blink::WebCircularGeofencingRegion; |
| 18 typedef std::map<std::string, WebCircularGeofencingRegion> RegionMap; | 20 typedef std::map<std::string, WebCircularGeofencingRegion> RegionMap; |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 static const char* kTestRegionId = "region-id"; | 24 static const char* kTestRegionId = "region-id"; |
| 23 static const int64 kTestGeofencingRegistrationId = 42; | 25 static const int64_t kTestGeofencingRegistrationId = 42; |
| 24 static const int64 kTestGeofencingRegistrationId2 = 43; | 26 static const int64_t kTestGeofencingRegistrationId2 = 43; |
| 25 | 27 |
| 26 bool RegionsMatch(const WebCircularGeofencingRegion& expected, | 28 bool RegionsMatch(const WebCircularGeofencingRegion& expected, |
| 27 const WebCircularGeofencingRegion& arg) { | 29 const WebCircularGeofencingRegion& arg) { |
| 28 return testing::Matches(expected.latitude)(arg.latitude) && | 30 return testing::Matches(expected.latitude)(arg.latitude) && |
| 29 testing::Matches(expected.longitude)(arg.longitude) && | 31 testing::Matches(expected.longitude)(arg.longitude) && |
| 30 testing::Matches(expected.radius)(arg.radius); | 32 testing::Matches(expected.radius)(arg.radius); |
| 31 } | 33 } |
| 32 } | 34 } |
| 33 | 35 |
| 34 namespace content { | 36 namespace content { |
| 35 | 37 |
| 36 class TestGeofencingService : public GeofencingService { | 38 class TestGeofencingService : public GeofencingService { |
| 37 public: | 39 public: |
| 38 TestGeofencingService() : is_available_(false) {} | 40 TestGeofencingService() : is_available_(false) {} |
| 39 | 41 |
| 40 bool IsServiceAvailable() override { return is_available_; } | 42 bool IsServiceAvailable() override { return is_available_; } |
| 41 | 43 |
| 42 void SetIsServiceAvailable(bool is_available) { | 44 void SetIsServiceAvailable(bool is_available) { |
| 43 is_available_ = is_available; | 45 is_available_ = is_available; |
| 44 } | 46 } |
| 45 | 47 |
| 46 MOCK_METHOD2(RegisterRegion, | 48 MOCK_METHOD2(RegisterRegion, |
| 47 int64(const WebCircularGeofencingRegion& region, | 49 int64_t(const WebCircularGeofencingRegion& region, |
| 48 GeofencingRegistrationDelegate* delegate)); | 50 GeofencingRegistrationDelegate* delegate)); |
| 49 MOCK_METHOD1(UnregisterRegion, void(int64 geofencing_registration_id)); | 51 MOCK_METHOD1(UnregisterRegion, void(int64_t geofencing_registration_id)); |
| 50 | 52 |
| 51 private: | 53 private: |
| 52 bool is_available_; | 54 bool is_available_; |
| 53 }; | 55 }; |
| 54 | 56 |
| 55 ACTION_P(SaveDelegate, delegate) { | 57 ACTION_P(SaveDelegate, delegate) { |
| 56 *delegate = arg1; | 58 *delegate = arg1; |
| 57 } | 59 } |
| 58 | 60 |
| 59 ACTION_P(QuitRunner, runner) { | 61 ACTION_P(QuitRunner, runner) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 81 return result_; | 83 return result_; |
| 82 } | 84 } |
| 83 | 85 |
| 84 private: | 86 private: |
| 85 bool was_called_; | 87 bool was_called_; |
| 86 GeofencingStatus result_; | 88 GeofencingStatus result_; |
| 87 scoped_refptr<MessageLoopRunner> runner_; | 89 scoped_refptr<MessageLoopRunner> runner_; |
| 88 }; | 90 }; |
| 89 | 91 |
| 90 void SaveResponseCallback(bool* called, | 92 void SaveResponseCallback(bool* called, |
| 91 int64* store_registration_id, | 93 int64_t* store_registration_id, |
| 92 ServiceWorkerStatusCode status, | 94 ServiceWorkerStatusCode status, |
| 93 const std::string& status_message, | 95 const std::string& status_message, |
| 94 int64 registration_id) { | 96 int64_t registration_id) { |
| 95 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); | 97 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); |
| 96 *called = true; | 98 *called = true; |
| 97 *store_registration_id = registration_id; | 99 *store_registration_id = registration_id; |
| 98 } | 100 } |
| 99 | 101 |
| 100 ServiceWorkerContextCore::RegistrationCallback MakeRegisteredCallback( | 102 ServiceWorkerContextCore::RegistrationCallback MakeRegisteredCallback( |
| 101 bool* called, | 103 bool* called, |
| 102 int64* store_registration_id) { | 104 int64_t* store_registration_id) { |
| 103 return base::Bind(&SaveResponseCallback, called, store_registration_id); | 105 return base::Bind(&SaveResponseCallback, called, store_registration_id); |
| 104 } | 106 } |
| 105 | 107 |
| 106 void CallCompletedCallback(bool* called, ServiceWorkerStatusCode) { | 108 void CallCompletedCallback(bool* called, ServiceWorkerStatusCode) { |
| 107 *called = true; | 109 *called = true; |
| 108 } | 110 } |
| 109 | 111 |
| 110 ServiceWorkerContextCore::UnregistrationCallback MakeUnregisteredCallback( | 112 ServiceWorkerContextCore::UnregistrationCallback MakeUnregisteredCallback( |
| 111 bool* called) { | 113 bool* called) { |
| 112 return base::Bind(&CallCompletedCallback, called); | 114 return base::Bind(&CallCompletedCallback, called); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 140 service_ = nullptr; | 142 service_ = nullptr; |
| 141 helper_.reset(); | 143 helper_.reset(); |
| 142 } | 144 } |
| 143 | 145 |
| 144 void SetHasProviderForTests() { service_->SetIsServiceAvailable(true); } | 146 void SetHasProviderForTests() { service_->SetIsServiceAvailable(true); } |
| 145 | 147 |
| 146 scoped_refptr<ServiceWorkerRegistration> RegisterServiceWorker( | 148 scoped_refptr<ServiceWorkerRegistration> RegisterServiceWorker( |
| 147 const std::string& name) { | 149 const std::string& name) { |
| 148 GURL pattern("http://www.example.com/" + name); | 150 GURL pattern("http://www.example.com/" + name); |
| 149 GURL script_url("http://www.example.com/service_worker.js"); | 151 GURL script_url("http://www.example.com/service_worker.js"); |
| 150 int64 registration_id = kInvalidServiceWorkerRegistrationId; | 152 int64_t registration_id = kInvalidServiceWorkerRegistrationId; |
| 151 bool called = false; | 153 bool called = false; |
| 152 helper_->context()->RegisterServiceWorker( | 154 helper_->context()->RegisterServiceWorker( |
| 153 pattern, script_url, nullptr, | 155 pattern, script_url, nullptr, |
| 154 MakeRegisteredCallback(&called, ®istration_id)); | 156 MakeRegisteredCallback(&called, ®istration_id)); |
| 155 | 157 |
| 156 EXPECT_FALSE(called); | 158 EXPECT_FALSE(called); |
| 157 base::RunLoop().RunUntilIdle(); | 159 base::RunLoop().RunUntilIdle(); |
| 158 EXPECT_TRUE(called); | 160 EXPECT_TRUE(called); |
| 159 scoped_refptr<ServiceWorkerRegistration> worker( | 161 scoped_refptr<ServiceWorkerRegistration> worker( |
| 160 new ServiceWorkerRegistration(pattern, registration_id, | 162 new ServiceWorkerRegistration(pattern, registration_id, |
| 161 helper_->context()->AsWeakPtr())); | 163 helper_->context()->AsWeakPtr())); |
| 162 // ServiceWorkerRegistration posts a notification task on construction. | 164 // ServiceWorkerRegistration posts a notification task on construction. |
| 163 base::RunLoop().RunUntilIdle(); | 165 base::RunLoop().RunUntilIdle(); |
| 164 return worker; | 166 return worker; |
| 165 } | 167 } |
| 166 | 168 |
| 167 void UnregisterServiceWorker( | 169 void UnregisterServiceWorker( |
| 168 const scoped_refptr<ServiceWorkerRegistration>& registration) { | 170 const scoped_refptr<ServiceWorkerRegistration>& registration) { |
| 169 bool called = false; | 171 bool called = false; |
| 170 helper_->context()->UnregisterServiceWorker( | 172 helper_->context()->UnregisterServiceWorker( |
| 171 registration->pattern(), MakeUnregisteredCallback(&called)); | 173 registration->pattern(), MakeUnregisteredCallback(&called)); |
| 172 | 174 |
| 173 EXPECT_FALSE(called); | 175 EXPECT_FALSE(called); |
| 174 base::RunLoop().RunUntilIdle(); | 176 base::RunLoop().RunUntilIdle(); |
| 175 EXPECT_TRUE(called); | 177 EXPECT_TRUE(called); |
| 176 } | 178 } |
| 177 | 179 |
| 178 GeofencingStatus RegisterRegionSync( | 180 GeofencingStatus RegisterRegionSync( |
| 179 int64 service_worker_registration_id, | 181 int64_t service_worker_registration_id, |
| 180 const std::string& id, | 182 const std::string& id, |
| 181 const WebCircularGeofencingRegion& region) { | 183 const WebCircularGeofencingRegion& region) { |
| 182 StatusCatcher result; | 184 StatusCatcher result; |
| 183 manager_->RegisterRegion( | 185 manager_->RegisterRegion( |
| 184 service_worker_registration_id, | 186 service_worker_registration_id, |
| 185 id, | 187 id, |
| 186 region, | 188 region, |
| 187 base::Bind(&StatusCatcher::Done, base::Unretained(&result))); | 189 base::Bind(&StatusCatcher::Done, base::Unretained(&result))); |
| 188 return result.Wait(); | 190 return result.Wait(); |
| 189 } | 191 } |
| 190 | 192 |
| 191 GeofencingStatus RegisterRegionSyncWithServiceResult( | 193 GeofencingStatus RegisterRegionSyncWithServiceResult( |
| 192 int64 service_worker_registration_id, | 194 int64_t service_worker_registration_id, |
| 193 const std::string& id, | 195 const std::string& id, |
| 194 const WebCircularGeofencingRegion& region, | 196 const WebCircularGeofencingRegion& region, |
| 195 GeofencingStatus service_status, | 197 GeofencingStatus service_status, |
| 196 int64 geofencing_registration_id) { | 198 int64_t geofencing_registration_id) { |
| 197 StatusCatcher result; | 199 StatusCatcher result; |
| 198 GeofencingRegistrationDelegate* delegate = 0; | 200 GeofencingRegistrationDelegate* delegate = 0; |
| 199 EXPECT_CALL( | 201 EXPECT_CALL( |
| 200 *service_, | 202 *service_, |
| 201 RegisterRegion(WebCircularGeofencingRegionEq(region), testing::_)) | 203 RegisterRegion(WebCircularGeofencingRegionEq(region), testing::_)) |
| 202 .WillOnce(testing::DoAll(SaveDelegate(&delegate), | 204 .WillOnce(testing::DoAll(SaveDelegate(&delegate), |
| 203 testing::Return(geofencing_registration_id))); | 205 testing::Return(geofencing_registration_id))); |
| 204 manager_->RegisterRegion( | 206 manager_->RegisterRegion( |
| 205 service_worker_registration_id, | 207 service_worker_registration_id, |
| 206 id, | 208 id, |
| 207 region, | 209 region, |
| 208 base::Bind(&StatusCatcher::Done, base::Unretained(&result))); | 210 base::Bind(&StatusCatcher::Done, base::Unretained(&result))); |
| 209 CHECK(delegate); | 211 CHECK(delegate); |
| 210 delegate->RegistrationFinished(geofencing_registration_id, service_status); | 212 delegate->RegistrationFinished(geofencing_registration_id, service_status); |
| 211 return result.Wait(); | 213 return result.Wait(); |
| 212 } | 214 } |
| 213 | 215 |
| 214 GeofencingStatus UnregisterRegionSync(int64 service_worker_registration_id, | 216 GeofencingStatus UnregisterRegionSync( |
| 215 const std::string& id, | 217 int64_t service_worker_registration_id, |
| 216 bool should_call_service, | 218 const std::string& id, |
| 217 int64 geofencing_registration_id = 0) { | 219 bool should_call_service, |
| 220 int64_t geofencing_registration_id = 0) { |
| 218 StatusCatcher result; | 221 StatusCatcher result; |
| 219 if (should_call_service) { | 222 if (should_call_service) { |
| 220 EXPECT_CALL(*service_, UnregisterRegion(geofencing_registration_id)); | 223 EXPECT_CALL(*service_, UnregisterRegion(geofencing_registration_id)); |
| 221 } | 224 } |
| 222 manager_->UnregisterRegion( | 225 manager_->UnregisterRegion( |
| 223 service_worker_registration_id, | 226 service_worker_registration_id, |
| 224 id, | 227 id, |
| 225 base::Bind(&StatusCatcher::Done, base::Unretained(&result))); | 228 base::Bind(&StatusCatcher::Done, base::Unretained(&result))); |
| 226 return result.Wait(); | 229 return result.Wait(); |
| 227 } | 230 } |
| 228 | 231 |
| 229 void VerifyRegions(int64 service_worker_registration_id, | 232 void VerifyRegions(int64_t service_worker_registration_id, |
| 230 const RegionMap& expected_regions) { | 233 const RegionMap& expected_regions) { |
| 231 RegionMap regions; | 234 RegionMap regions; |
| 232 EXPECT_EQ(GEOFENCING_STATUS_OK, | 235 EXPECT_EQ(GEOFENCING_STATUS_OK, |
| 233 manager_->GetRegisteredRegions(service_worker_registration_id, | 236 manager_->GetRegisteredRegions(service_worker_registration_id, |
| 234 ®ions)); | 237 ®ions)); |
| 235 EXPECT_EQ(expected_regions.size(), regions.size()); | 238 EXPECT_EQ(expected_regions.size(), regions.size()); |
| 236 for (RegionMap::const_iterator it = expected_regions.begin(); | 239 for (RegionMap::const_iterator it = expected_regions.begin(); |
| 237 it != expected_regions.end(); | 240 it != expected_regions.end(); |
| 238 ++it) { | 241 ++it) { |
| 239 EXPECT_THAT(regions[it->first], | 242 EXPECT_THAT(regions[it->first], |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 GEOFENCING_STATUS_OK, kTestGeofencingRegistrationId)); | 509 GEOFENCING_STATUS_OK, kTestGeofencingRegistrationId)); |
| 507 VerifyRegions(worker1_->id(), expected_regions_); | 510 VerifyRegions(worker1_->id(), expected_regions_); |
| 508 | 511 |
| 509 EXPECT_CALL(*service_, UnregisterRegion(kTestGeofencingRegistrationId)); | 512 EXPECT_CALL(*service_, UnregisterRegion(kTestGeofencingRegistrationId)); |
| 510 | 513 |
| 511 manager_->SetMockProvider(GeofencingMockState::SERVICE_AVAILABLE); | 514 manager_->SetMockProvider(GeofencingMockState::SERVICE_AVAILABLE); |
| 512 VerifyRegions(worker1_->id(), RegionMap()); | 515 VerifyRegions(worker1_->id(), RegionMap()); |
| 513 } | 516 } |
| 514 | 517 |
| 515 } // namespace content | 518 } // namespace content |
| OLD | NEW |