| 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_service.h" | 8 #include "content/browser/geofencing/geofencing_service.h" |
| 9 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 9 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 10 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 10 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 11 #include "content/public/test/test_browser_thread_bundle.h" | 11 #include "content/public/test/test_browser_thread_bundle.h" |
| 12 #include "content/public/test/test_utils.h" | 12 #include "content/public/test/test_utils.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h" | 15 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h" |
| 16 | 16 |
| 17 using blink::WebCircularGeofencingRegion; | 17 using blink::WebCircularGeofencingRegion; |
| 18 typedef std::map<std::string, WebCircularGeofencingRegion> RegionMap; | 18 typedef std::map<std::string, WebCircularGeofencingRegion> RegionMap; |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 static const int kRenderProcessId = 99; | |
| 23 static const char* kTestRegionId = "region-id"; | 22 static const char* kTestRegionId = "region-id"; |
| 24 static const int64 kTestGeofencingRegistrationId = 42; | 23 static const int64 kTestGeofencingRegistrationId = 42; |
| 25 static const int64 kTestGeofencingRegistrationId2 = 43; | 24 static const int64 kTestGeofencingRegistrationId2 = 43; |
| 26 | 25 |
| 27 bool RegionsMatch(const WebCircularGeofencingRegion& expected, | 26 bool RegionsMatch(const WebCircularGeofencingRegion& expected, |
| 28 const WebCircularGeofencingRegion& arg) { | 27 const WebCircularGeofencingRegion& arg) { |
| 29 return testing::Matches(expected.latitude)(arg.latitude) && | 28 return testing::Matches(expected.latitude)(arg.latitude) && |
| 30 testing::Matches(expected.longitude)(arg.longitude) && | 29 testing::Matches(expected.longitude)(arg.longitude) && |
| 31 testing::Matches(expected.radius)(arg.radius); | 30 testing::Matches(expected.radius)(arg.radius); |
| 32 } | 31 } |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 CHECK(was_called_); | 80 CHECK(was_called_); |
| 82 return result_; | 81 return result_; |
| 83 } | 82 } |
| 84 | 83 |
| 85 private: | 84 private: |
| 86 bool was_called_; | 85 bool was_called_; |
| 87 GeofencingStatus result_; | 86 GeofencingStatus result_; |
| 88 scoped_refptr<MessageLoopRunner> runner_; | 87 scoped_refptr<MessageLoopRunner> runner_; |
| 89 }; | 88 }; |
| 90 | 89 |
| 91 void SaveResponseCallback(bool* called, | 90 void SaveResponseCallback(int64* store_registration_id, |
| 92 int64* store_registration_id, | 91 const scoped_refptr<MessageLoopRunner> runner, |
| 93 ServiceWorkerStatusCode status, | 92 ServiceWorkerStatusCode status, |
| 94 const std::string& status_message, | 93 const std::string& status_message, |
| 95 int64 registration_id) { | 94 int64 registration_id) { |
| 96 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); | 95 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); |
| 97 *called = true; | |
| 98 *store_registration_id = registration_id; | 96 *store_registration_id = registration_id; |
| 97 runner->Quit(); |
| 99 } | 98 } |
| 100 | 99 |
| 101 ServiceWorkerContextCore::RegistrationCallback MakeRegisteredCallback( | 100 ServiceWorkerContextCore::RegistrationCallback MakeRegisteredCallback( |
| 102 bool* called, | 101 int64* store_registration_id, |
| 103 int64* store_registration_id) { | 102 const scoped_refptr<MessageLoopRunner>& runner) { |
| 104 return base::Bind(&SaveResponseCallback, called, store_registration_id); | 103 return base::Bind(&SaveResponseCallback, store_registration_id, runner); |
| 105 } | 104 } |
| 106 | 105 |
| 107 void CallCompletedCallback(bool* called, ServiceWorkerStatusCode) { | 106 void CallCompletedCallback(bool* called, ServiceWorkerStatusCode) { |
| 108 *called = true; | 107 *called = true; |
| 109 } | 108 } |
| 110 | 109 |
| 111 ServiceWorkerContextCore::UnregistrationCallback MakeUnregisteredCallback( | 110 ServiceWorkerContextCore::UnregistrationCallback MakeUnregisteredCallback( |
| 112 bool* called) { | 111 bool* called) { |
| 113 return base::Bind(&CallCompletedCallback, called); | 112 return base::Bind(&CallCompletedCallback, called); |
| 114 } | 113 } |
| 115 | 114 |
| 116 class GeofencingManagerTest : public testing::Test { | 115 class GeofencingManagerTest : public testing::Test { |
| 117 public: | 116 public: |
| 118 GeofencingManagerTest() : service_(nullptr) { | 117 GeofencingManagerTest() : service_(nullptr) { |
| 119 test_region_.latitude = 37.421999; | 118 test_region_.latitude = 37.421999; |
| 120 test_region_.longitude = -122.084015; | 119 test_region_.longitude = -122.084015; |
| 121 test_region_.radius = 100; | 120 test_region_.radius = 100; |
| 122 expected_regions_[kTestRegionId] = test_region_; | 121 expected_regions_[kTestRegionId] = test_region_; |
| 123 } | 122 } |
| 124 | 123 |
| 125 void SetUp() override { | 124 void SetUp() override { |
| 126 helper_.reset( | 125 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); |
| 127 new EmbeddedWorkerTestHelper(base::FilePath(), kRenderProcessId)); | |
| 128 service_ = new TestGeofencingService(); | 126 service_ = new TestGeofencingService(); |
| 129 manager_ = new GeofencingManager(helper_->context_wrapper()); | 127 manager_ = new GeofencingManager(helper_->context_wrapper()); |
| 130 manager_->SetServiceForTesting(service_); | 128 manager_->SetServiceForTesting(service_); |
| 131 manager_->Init(); | 129 manager_->Init(); |
| 132 | 130 |
| 133 worker1_ = RegisterServiceWorker("1"); | 131 worker1_ = RegisterServiceWorker("1"); |
| 134 worker2_ = RegisterServiceWorker("2"); | 132 worker2_ = RegisterServiceWorker("2"); |
| 135 } | 133 } |
| 136 | 134 |
| 137 void TearDown() override { | 135 void TearDown() override { |
| 138 worker1_ = nullptr; | 136 worker1_ = nullptr; |
| 139 worker2_ = nullptr; | 137 worker2_ = nullptr; |
| 140 manager_ = nullptr; | 138 manager_ = nullptr; |
| 141 delete service_; | 139 delete service_; |
| 142 service_ = nullptr; | 140 service_ = nullptr; |
| 143 helper_.reset(); | 141 helper_.reset(); |
| 144 } | 142 } |
| 145 | 143 |
| 146 void SetHasProviderForTests() { service_->SetIsServiceAvailable(true); } | 144 void SetHasProviderForTests() { service_->SetIsServiceAvailable(true); } |
| 147 | 145 |
| 148 scoped_refptr<ServiceWorkerRegistration> RegisterServiceWorker( | 146 scoped_refptr<ServiceWorkerRegistration> RegisterServiceWorker( |
| 149 const std::string& name) { | 147 const std::string& name) { |
| 150 GURL pattern("http://www.example.com/" + name); | 148 GURL pattern("http://www.example.com/" + name); |
| 151 GURL script_url("http://www.example.com/service_worker.js"); | 149 GURL script_url("http://www.example.com/service_worker.js"); |
| 152 int64 registration_id = kInvalidServiceWorkerRegistrationId; | 150 int64 registration_id = kInvalidServiceWorkerRegistrationId; |
| 153 bool called = false; | 151 scoped_refptr<MessageLoopRunner> runner(new MessageLoopRunner); |
| 154 helper_->context()->RegisterServiceWorker( | 152 helper_->context()->RegisterServiceWorker( |
| 155 pattern, script_url, nullptr, | 153 pattern, script_url, nullptr, |
| 156 MakeRegisteredCallback(&called, ®istration_id)); | 154 MakeRegisteredCallback(®istration_id, runner)); |
| 157 | 155 |
| 158 EXPECT_FALSE(called); | 156 runner->Run(); |
| 159 base::RunLoop().RunUntilIdle(); | |
| 160 EXPECT_TRUE(called); | |
| 161 scoped_refptr<ServiceWorkerRegistration> worker( | 157 scoped_refptr<ServiceWorkerRegistration> worker( |
| 162 new ServiceWorkerRegistration(pattern, registration_id, | 158 new ServiceWorkerRegistration(pattern, registration_id, |
| 163 helper_->context()->AsWeakPtr())); | 159 helper_->context()->AsWeakPtr())); |
| 164 // ServiceWorkerRegistration posts a notification task on construction. | 160 // ServiceWorkerRegistration posts a notification task on construction. |
| 165 base::RunLoop().RunUntilIdle(); | 161 base::RunLoop().RunUntilIdle(); |
| 166 return worker; | 162 return worker; |
| 167 } | 163 } |
| 168 | 164 |
| 169 void UnregisterServiceWorker( | 165 void UnregisterServiceWorker( |
| 170 const scoped_refptr<ServiceWorkerRegistration>& registration) { | 166 const scoped_refptr<ServiceWorkerRegistration>& registration) { |
| (...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 508 GEOFENCING_STATUS_OK, kTestGeofencingRegistrationId)); | 504 GEOFENCING_STATUS_OK, kTestGeofencingRegistrationId)); |
| 509 VerifyRegions(worker1_->id(), expected_regions_); | 505 VerifyRegions(worker1_->id(), expected_regions_); |
| 510 | 506 |
| 511 EXPECT_CALL(*service_, UnregisterRegion(kTestGeofencingRegistrationId)); | 507 EXPECT_CALL(*service_, UnregisterRegion(kTestGeofencingRegistrationId)); |
| 512 | 508 |
| 513 manager_->SetMockProvider(GeofencingMockState::SERVICE_AVAILABLE); | 509 manager_->SetMockProvider(GeofencingMockState::SERVICE_AVAILABLE); |
| 514 VerifyRegions(worker1_->id(), RegionMap()); | 510 VerifyRegions(worker1_->id(), RegionMap()); |
| 515 } | 511 } |
| 516 | 512 |
| 517 } // namespace content | 513 } // namespace content |
| OLD | NEW |