Index: content/browser/geofencing/geofencing_manager_unittest.cc |
diff --git a/content/browser/geofencing/geofencing_manager_unittest.cc b/content/browser/geofencing/geofencing_manager_unittest.cc |
index 78828acc93d67a398a65963ccf30ea1d2111d034..6970b7171ca488c18c515b77d5bcdc2c6c99f61b 100644 |
--- a/content/browser/geofencing/geofencing_manager_unittest.cc |
+++ b/content/browser/geofencing/geofencing_manager_unittest.cc |
@@ -5,14 +5,13 @@ |
#include "base/callback.h" |
#include "base/message_loop/message_loop.h" |
#include "content/browser/geofencing/geofencing_manager.h" |
-#include "content/browser/geofencing/geofencing_provider.h" |
-#include "content/public/browser/browser_thread.h" |
-#include "content/public/test/test_browser_thread.h" |
+#include "content/browser/geofencing/geofencing_service.h" |
+#include "content/browser/service_worker/service_worker_context_wrapper.h" |
+#include "content/public/test/test_browser_thread_bundle.h" |
#include "content/public/test/test_utils.h" |
#include "testing/gmock/include/gmock/gmock.h" |
#include "testing/gtest/include/gtest/gtest.h" |
#include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h" |
-#include "url/gurl.h" |
using blink::WebCircularGeofencingRegion; |
typedef std::map<std::string, WebCircularGeofencingRegion> RegionMap; |
@@ -22,6 +21,8 @@ namespace { |
static const char* kTestRegionId = "region-id"; |
static const int64 kTestServiceWorkerRegistrationId = 123; |
static const int64 kTestServiceWorkerRegistrationId2 = 456; |
+static const int64 kTestGeofencingRegistrationId = 42; |
+static const int64 kTestGeofencingRegistrationId2 = 43; |
bool RegionsMatch(const WebCircularGeofencingRegion& expected, |
const WebCircularGeofencingRegion& arg) { |
@@ -33,20 +34,21 @@ bool RegionsMatch(const WebCircularGeofencingRegion& expected, |
namespace content { |
-class TestGeofencingProvider : public GeofencingProvider { |
+class TestGeofencingService : public GeofencingService { |
public: |
+ MOCK_METHOD0(IsServiceAvailable, bool()); |
MOCK_METHOD2(RegisterRegion, |
- void(const WebCircularGeofencingRegion& region, |
- const RegisterCallback& callback)); |
- MOCK_METHOD1(UnregisterRegion, void(int registration_id)); |
+ int64(const WebCircularGeofencingRegion& region, |
+ GeofencingRegistrationDelegate* delegate)); |
+ MOCK_METHOD1(UnregisterRegion, void(int64 geofencing_registration_id)); |
}; |
-ACTION_P2(CallRegisterCallback, status, id) { |
- arg1.Run(status, id); |
+ACTION_P(SaveDelegate, delegate) { |
+ *delegate = arg1; |
} |
-ACTION_P(SaveRegisterCallback, callback) { |
- *callback = arg1; |
+ACTION_P(QuitRunner, runner) { |
+ runner->Quit(); |
} |
MATCHER_P(WebCircularGeofencingRegionEq, expected, "") { |
@@ -78,25 +80,30 @@ class StatusCatcher { |
class GeofencingManagerTest : public testing::Test { |
public: |
- GeofencingManagerTest() |
- : message_loop_(), |
- io_thread_(BrowserThread::IO, &message_loop_), |
- provider_(0), |
- manager_(0), |
- test_origin_("https://example.com/") { |
+ GeofencingManagerTest() : service_(nullptr) { |
test_region_.latitude = 37.421999; |
test_region_.longitude = -122.084015; |
test_region_.radius = 100; |
expected_regions_[kTestRegionId] = test_region_; |
} |
- virtual void SetUp() { manager_ = new GeofencingManager(); } |
+ virtual void SetUp() { |
+ service_ = new TestGeofencingService(); |
+ ON_CALL(*service_, IsServiceAvailable()) |
+ .WillByDefault(testing::Return(false)); |
+ manager_ = new GeofencingManager(nullptr /* ServiceWorkerContextWrapper */); |
+ manager_->SetServiceForTesting(service_); |
+ } |
- virtual void TearDown() { delete manager_; } |
+ virtual void TearDown() { |
+ manager_ = nullptr; |
+ delete service_; |
+ service_ = nullptr; |
+ } |
- void SetProviderForTests() { |
- provider_ = new TestGeofencingProvider(); |
- manager_->SetProviderForTests(scoped_ptr<GeofencingProvider>(provider_)); |
+ void SetHasProviderForTests() { |
+ ON_CALL(*service_, IsServiceAvailable()) |
+ .WillByDefault(testing::Return(true)); |
} |
GeofencingStatus RegisterRegionSync( |
@@ -105,48 +112,46 @@ class GeofencingManagerTest : public testing::Test { |
const WebCircularGeofencingRegion& region) { |
StatusCatcher result; |
manager_->RegisterRegion( |
- nullptr, /* browser_context */ |
service_worker_registration_id, |
- test_origin_, |
id, |
region, |
base::Bind(&StatusCatcher::Done, base::Unretained(&result))); |
return result.Wait(); |
} |
- GeofencingStatus RegisterRegionSyncWithProviderResult( |
+ GeofencingStatus RegisterRegionSyncWithServiceResult( |
int64 service_worker_registration_id, |
const std::string& id, |
const WebCircularGeofencingRegion& region, |
- GeofencingStatus provider_status, |
- int provider_result) { |
+ GeofencingStatus service_status, |
+ int64 geofencing_registration_id) { |
StatusCatcher result; |
+ GeofencingRegistrationDelegate* delegate = 0; |
EXPECT_CALL( |
- *provider_, |
+ *service_, |
RegisterRegion(WebCircularGeofencingRegionEq(region), testing::_)) |
- .WillOnce(CallRegisterCallback(provider_status, provider_result)); |
+ .WillOnce(testing::DoAll(SaveDelegate(&delegate), |
+ testing::Return(geofencing_registration_id))); |
manager_->RegisterRegion( |
- nullptr, /* browser_context */ |
service_worker_registration_id, |
- test_origin_, |
id, |
region, |
base::Bind(&StatusCatcher::Done, base::Unretained(&result))); |
+ CHECK(delegate); |
+ delegate->RegistrationFinished(geofencing_registration_id, service_status); |
return result.Wait(); |
} |
GeofencingStatus UnregisterRegionSync(int64 service_worker_registration_id, |
const std::string& id, |
- bool should_call_provider, |
- int provider_id = 0) { |
+ bool should_call_service, |
+ int64 geofencing_registration_id = 0) { |
StatusCatcher result; |
- if (should_call_provider) { |
- EXPECT_CALL(*provider_, UnregisterRegion(provider_id)); |
+ if (should_call_service) { |
+ EXPECT_CALL(*service_, UnregisterRegion(geofencing_registration_id)); |
} |
manager_->UnregisterRegion( |
- nullptr, /* browser_context */ |
service_worker_registration_id, |
- test_origin_, |
id, |
base::Bind(&StatusCatcher::Done, base::Unretained(&result))); |
return result.Wait(); |
@@ -155,10 +160,8 @@ class GeofencingManagerTest : public testing::Test { |
void VerifyRegions(int64 service_worker_registration_id, |
const RegionMap& expected_regions) { |
RegionMap regions; |
- EXPECT_EQ(GeofencingStatus::GEOFENCING_STATUS_OK, |
- manager_->GetRegisteredRegions(nullptr, /* browser_context */ |
- service_worker_registration_id, |
- test_origin_, |
+ EXPECT_EQ(GEOFENCING_STATUS_OK, |
+ manager_->GetRegisteredRegions(service_worker_registration_id, |
®ions)); |
EXPECT_EQ(expected_regions.size(), regions.size()); |
for (RegionMap::const_iterator it = expected_regions.begin(); |
@@ -170,236 +173,242 @@ class GeofencingManagerTest : public testing::Test { |
} |
protected: |
- base::MessageLoop message_loop_; |
- TestBrowserThread io_thread_; |
- TestGeofencingProvider* provider_; |
- GeofencingManager* manager_; |
+ TestBrowserThreadBundle threads_; |
+ TestGeofencingService* service_; |
+ scoped_refptr<GeofencingManager> manager_; |
WebCircularGeofencingRegion test_region_; |
RegionMap expected_regions_; |
- GURL test_origin_; |
}; |
-TEST_F(GeofencingManagerTest, RegisterRegion_NoProvider) { |
- EXPECT_EQ(GeofencingStatus:: |
- GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE, |
+TEST_F(GeofencingManagerTest, RegisterRegion_NoService) { |
+ EXPECT_EQ(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE, |
RegisterRegionSync( |
kTestServiceWorkerRegistrationId, kTestRegionId, test_region_)); |
} |
-TEST_F(GeofencingManagerTest, UnregisterRegion_NoProvider) { |
- EXPECT_EQ(GeofencingStatus:: |
- GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE, |
+TEST_F(GeofencingManagerTest, UnregisterRegion_NoService) { |
+ EXPECT_EQ(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE, |
UnregisterRegionSync( |
kTestServiceWorkerRegistrationId, kTestRegionId, false)); |
} |
-TEST_F(GeofencingManagerTest, GetRegisteredRegions_NoProvider) { |
+TEST_F(GeofencingManagerTest, GetRegisteredRegions_NoService) { |
RegionMap regions; |
- EXPECT_EQ(GeofencingStatus:: |
- GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE, |
- manager_->GetRegisteredRegions(nullptr, /* browser_context */ |
- kTestServiceWorkerRegistrationId, |
- test_origin_, |
+ EXPECT_EQ(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE, |
+ manager_->GetRegisteredRegions(kTestServiceWorkerRegistrationId, |
®ions)); |
EXPECT_TRUE(regions.empty()); |
} |
-TEST_F(GeofencingManagerTest, RegisterRegion_FailsInProvider) { |
- SetProviderForTests(); |
+TEST_F(GeofencingManagerTest, RegisterRegion_FailsInService) { |
+ SetHasProviderForTests(); |
EXPECT_EQ( |
- GeofencingStatus::GEOFENCING_STATUS_ERROR, |
- RegisterRegionSyncWithProviderResult(kTestServiceWorkerRegistrationId, |
- kTestRegionId, |
- test_region_, |
- GEOFENCING_STATUS_ERROR, |
- -1)); |
+ GEOFENCING_STATUS_ERROR, |
+ RegisterRegionSyncWithServiceResult(kTestServiceWorkerRegistrationId, |
+ kTestRegionId, |
+ test_region_, |
+ GEOFENCING_STATUS_ERROR, |
+ -1)); |
} |
-TEST_F(GeofencingManagerTest, RegisterRegion_SucceedsInProvider) { |
- SetProviderForTests(); |
+TEST_F(GeofencingManagerTest, RegisterRegion_SucceedsInService) { |
+ SetHasProviderForTests(); |
EXPECT_EQ( |
- GeofencingStatus::GEOFENCING_STATUS_OK, |
- RegisterRegionSyncWithProviderResult(kTestServiceWorkerRegistrationId, |
- kTestRegionId, |
- test_region_, |
- GEOFENCING_STATUS_OK, |
- 0)); |
+ GEOFENCING_STATUS_OK, |
+ RegisterRegionSyncWithServiceResult(kTestServiceWorkerRegistrationId, |
+ kTestRegionId, |
+ test_region_, |
+ GEOFENCING_STATUS_OK, |
+ kTestGeofencingRegistrationId)); |
VerifyRegions(kTestServiceWorkerRegistrationId, expected_regions_); |
} |
TEST_F(GeofencingManagerTest, RegisterRegion_AlreadyRegistered) { |
- SetProviderForTests(); |
+ SetHasProviderForTests(); |
EXPECT_EQ( |
- GeofencingStatus::GEOFENCING_STATUS_OK, |
- RegisterRegionSyncWithProviderResult(kTestServiceWorkerRegistrationId, |
- kTestRegionId, |
- test_region_, |
- GEOFENCING_STATUS_OK, |
- 0)); |
+ GEOFENCING_STATUS_OK, |
+ RegisterRegionSyncWithServiceResult(kTestServiceWorkerRegistrationId, |
+ kTestRegionId, |
+ test_region_, |
+ GEOFENCING_STATUS_OK, |
+ kTestGeofencingRegistrationId)); |
VerifyRegions(kTestServiceWorkerRegistrationId, expected_regions_); |
WebCircularGeofencingRegion region2; |
region2.latitude = 43.2; |
region2.longitude = 1.45; |
region2.radius = 8.5; |
- EXPECT_EQ(GeofencingStatus::GEOFENCING_STATUS_ERROR, |
+ EXPECT_EQ(GEOFENCING_STATUS_ERROR, |
RegisterRegionSync( |
kTestServiceWorkerRegistrationId, kTestRegionId, region2)); |
VerifyRegions(kTestServiceWorkerRegistrationId, expected_regions_); |
} |
TEST_F(GeofencingManagerTest, UnregisterRegion_NotRegistered) { |
- SetProviderForTests(); |
- EXPECT_EQ(GeofencingStatus::GEOFENCING_STATUS_ERROR, |
+ SetHasProviderForTests(); |
+ EXPECT_EQ(GEOFENCING_STATUS_UNREGISTRATION_FAILED_NOT_REGISTERED, |
UnregisterRegionSync( |
kTestServiceWorkerRegistrationId, kTestRegionId, false)); |
} |
TEST_F(GeofencingManagerTest, UnregisterRegion_Success) { |
- SetProviderForTests(); |
- int provider_id = 123; |
- |
- EXPECT_EQ( |
- GeofencingStatus::GEOFENCING_STATUS_OK, |
- RegisterRegionSyncWithProviderResult(kTestServiceWorkerRegistrationId, |
- kTestRegionId, |
- test_region_, |
- GEOFENCING_STATUS_OK, |
- provider_id)); |
+ SetHasProviderForTests(); |
EXPECT_EQ( |
- GeofencingStatus::GEOFENCING_STATUS_OK, |
- UnregisterRegionSync( |
- kTestServiceWorkerRegistrationId, kTestRegionId, true, provider_id)); |
+ GEOFENCING_STATUS_OK, |
+ RegisterRegionSyncWithServiceResult(kTestServiceWorkerRegistrationId, |
+ kTestRegionId, |
+ test_region_, |
+ GEOFENCING_STATUS_OK, |
+ kTestGeofencingRegistrationId)); |
+ |
+ EXPECT_EQ(GEOFENCING_STATUS_OK, |
+ UnregisterRegionSync(kTestServiceWorkerRegistrationId, |
+ kTestRegionId, |
+ true, |
+ kTestGeofencingRegistrationId)); |
VerifyRegions(kTestServiceWorkerRegistrationId, RegionMap()); |
} |
TEST_F(GeofencingManagerTest, GetRegisteredRegions_RegistrationInProgress) { |
- SetProviderForTests(); |
+ SetHasProviderForTests(); |
StatusCatcher result; |
- GeofencingProvider::RegisterCallback callback; |
+ GeofencingRegistrationDelegate* delegate = nullptr; |
EXPECT_CALL( |
- *provider_, |
+ *service_, |
RegisterRegion(WebCircularGeofencingRegionEq(test_region_), testing::_)) |
- .WillOnce(SaveRegisterCallback(&callback)); |
+ .WillOnce(testing::DoAll(SaveDelegate(&delegate), |
+ testing::Return(kTestGeofencingRegistrationId))); |
manager_->RegisterRegion( |
- nullptr, /* browser_context */ |
kTestServiceWorkerRegistrationId, |
- test_origin_, |
kTestRegionId, |
test_region_, |
base::Bind(&StatusCatcher::Done, base::Unretained(&result))); |
// At this point the manager should have tried registering the region with |
- // the provider, resulting in |callback| being set. Until the callback is |
+ // the service, resulting in |delegate| being set. Until the callback is |
// called the registration is not complete though. |
- EXPECT_FALSE(callback.is_null()); |
+ EXPECT_NE(delegate, nullptr); |
VerifyRegions(kTestServiceWorkerRegistrationId, RegionMap()); |
// Now call the callback, and verify the registration completed succesfully. |
- callback.Run(GEOFENCING_STATUS_OK, 123); |
- EXPECT_EQ(GeofencingStatus::GEOFENCING_STATUS_OK, result.Wait()); |
+ delegate->RegistrationFinished(kTestGeofencingRegistrationId, |
+ GEOFENCING_STATUS_OK); |
+ EXPECT_EQ(GEOFENCING_STATUS_OK, result.Wait()); |
VerifyRegions(kTestServiceWorkerRegistrationId, expected_regions_); |
} |
TEST_F(GeofencingManagerTest, UnregisterRegion_RegistrationInProgress) { |
- SetProviderForTests(); |
+ SetHasProviderForTests(); |
StatusCatcher result; |
- GeofencingProvider::RegisterCallback callback; |
+ GeofencingRegistrationDelegate* delegate = nullptr; |
EXPECT_CALL( |
- *provider_, |
+ *service_, |
RegisterRegion(WebCircularGeofencingRegionEq(test_region_), testing::_)) |
- .WillOnce(SaveRegisterCallback(&callback)); |
+ .WillOnce(testing::DoAll(SaveDelegate(&delegate), |
+ testing::Return(kTestGeofencingRegistrationId))); |
manager_->RegisterRegion( |
- nullptr, /* browser_context */ |
kTestServiceWorkerRegistrationId, |
- test_origin_, |
kTestRegionId, |
test_region_, |
base::Bind(&StatusCatcher::Done, base::Unretained(&result))); |
// At this point the manager should have tried registering the region with |
- // the provider, resulting in |callback| being set. Until the callback is |
+ // the service, resulting in |delegate| being set. Until the callback is |
// called the registration is not complete though. |
- EXPECT_FALSE(callback.is_null()); |
+ EXPECT_NE(delegate, nullptr); |
- EXPECT_EQ(GeofencingStatus::GEOFENCING_STATUS_ERROR, |
+ EXPECT_EQ(GEOFENCING_STATUS_UNREGISTRATION_FAILED_NOT_REGISTERED, |
UnregisterRegionSync( |
kTestServiceWorkerRegistrationId, kTestRegionId, false)); |
} |
TEST_F(GeofencingManagerTest, GetRegisteredRegions_NoRegions) { |
- SetProviderForTests(); |
+ SetHasProviderForTests(); |
VerifyRegions(kTestServiceWorkerRegistrationId, RegionMap()); |
} |
TEST_F(GeofencingManagerTest, RegisterRegion_SeparateServiceWorkers) { |
- SetProviderForTests(); |
- int provider_id1 = 12; |
- int provider_id2 = 34; |
+ SetHasProviderForTests(); |
EXPECT_EQ( |
- GeofencingStatus::GEOFENCING_STATUS_OK, |
- RegisterRegionSyncWithProviderResult(kTestServiceWorkerRegistrationId, |
- kTestRegionId, |
- test_region_, |
- GEOFENCING_STATUS_OK, |
- provider_id1)); |
+ GEOFENCING_STATUS_OK, |
+ RegisterRegionSyncWithServiceResult(kTestServiceWorkerRegistrationId, |
+ kTestRegionId, |
+ test_region_, |
+ GEOFENCING_STATUS_OK, |
+ kTestGeofencingRegistrationId)); |
VerifyRegions(kTestServiceWorkerRegistrationId, expected_regions_); |
VerifyRegions(kTestServiceWorkerRegistrationId2, RegionMap()); |
EXPECT_EQ( |
- GeofencingStatus::GEOFENCING_STATUS_OK, |
- RegisterRegionSyncWithProviderResult(kTestServiceWorkerRegistrationId2, |
- kTestRegionId, |
- test_region_, |
- GEOFENCING_STATUS_OK, |
- provider_id2)); |
+ GEOFENCING_STATUS_OK, |
+ RegisterRegionSyncWithServiceResult(kTestServiceWorkerRegistrationId2, |
+ kTestRegionId, |
+ test_region_, |
+ GEOFENCING_STATUS_OK, |
+ kTestGeofencingRegistrationId2)); |
VerifyRegions(kTestServiceWorkerRegistrationId, expected_regions_); |
VerifyRegions(kTestServiceWorkerRegistrationId2, expected_regions_); |
} |
TEST_F(GeofencingManagerTest, UnregisterRegion_SeparateServiceWorkers) { |
- SetProviderForTests(); |
- int provider_id1 = 12; |
- int provider_id2 = 34; |
+ SetHasProviderForTests(); |
EXPECT_EQ( |
- GeofencingStatus::GEOFENCING_STATUS_OK, |
- RegisterRegionSyncWithProviderResult(kTestServiceWorkerRegistrationId, |
- kTestRegionId, |
- test_region_, |
- GEOFENCING_STATUS_OK, |
- provider_id1)); |
+ GEOFENCING_STATUS_OK, |
+ RegisterRegionSyncWithServiceResult(kTestServiceWorkerRegistrationId, |
+ kTestRegionId, |
+ test_region_, |
+ GEOFENCING_STATUS_OK, |
+ kTestGeofencingRegistrationId)); |
EXPECT_EQ( |
- GeofencingStatus::GEOFENCING_STATUS_OK, |
- RegisterRegionSyncWithProviderResult(kTestServiceWorkerRegistrationId2, |
- kTestRegionId, |
- test_region_, |
- GEOFENCING_STATUS_OK, |
- provider_id2)); |
- |
- EXPECT_EQ( |
- GeofencingStatus::GEOFENCING_STATUS_OK, |
- UnregisterRegionSync( |
- kTestServiceWorkerRegistrationId, kTestRegionId, true, provider_id1)); |
+ GEOFENCING_STATUS_OK, |
+ RegisterRegionSyncWithServiceResult(kTestServiceWorkerRegistrationId2, |
+ kTestRegionId, |
+ test_region_, |
+ GEOFENCING_STATUS_OK, |
+ kTestGeofencingRegistrationId2)); |
+ |
+ EXPECT_EQ(GEOFENCING_STATUS_OK, |
+ UnregisterRegionSync(kTestServiceWorkerRegistrationId, |
+ kTestRegionId, |
+ true, |
+ kTestGeofencingRegistrationId)); |
VerifyRegions(kTestServiceWorkerRegistrationId, RegionMap()); |
VerifyRegions(kTestServiceWorkerRegistrationId2, expected_regions_); |
- EXPECT_EQ(GeofencingStatus::GEOFENCING_STATUS_OK, |
+ EXPECT_EQ(GEOFENCING_STATUS_OK, |
UnregisterRegionSync(kTestServiceWorkerRegistrationId2, |
kTestRegionId, |
true, |
- provider_id2)); |
+ kTestGeofencingRegistrationId2)); |
VerifyRegions(kTestServiceWorkerRegistrationId, RegionMap()); |
VerifyRegions(kTestServiceWorkerRegistrationId2, RegionMap()); |
} |
+TEST_F(GeofencingManagerTest, ShutdownCleansRegistrations) { |
+ SetHasProviderForTests(); |
+ scoped_refptr<MessageLoopRunner> runner(new MessageLoopRunner()); |
+ EXPECT_EQ( |
+ GEOFENCING_STATUS_OK, |
+ RegisterRegionSyncWithServiceResult(kTestServiceWorkerRegistrationId, |
+ kTestRegionId, |
+ test_region_, |
+ GEOFENCING_STATUS_OK, |
+ kTestGeofencingRegistrationId)); |
+ |
+ EXPECT_CALL(*service_, UnregisterRegion(kTestGeofencingRegistrationId)) |
+ .WillOnce(QuitRunner(runner)); |
+ manager_->Shutdown(); |
+ runner->Run(); |
+} |
+ |
} // namespace content |