Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / geofencing / geofencing_service_unittest.cc
blob7fbe0355c8a140e534e68940e5a1e26761798389
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.
5 #include "content/browser/geofencing/geofencing_provider.h"
6 #include "content/browser/geofencing/geofencing_registration_delegate.h"
7 #include "content/browser/geofencing/geofencing_service.h"
8 #include "content/public/test/test_browser_thread_bundle.h"
9 #include "content/public/test/test_utils.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h"
14 using blink::WebCircularGeofencingRegion;
16 namespace {
18 bool RegionsMatch(const WebCircularGeofencingRegion& expected,
19 const WebCircularGeofencingRegion& arg) {
20 return testing::Matches(expected.latitude)(arg.latitude) &&
21 testing::Matches(expected.longitude)(arg.longitude) &&
22 testing::Matches(expected.radius)(arg.radius);
25 } // namespace
27 namespace content {
29 class MockGeofencingRegistrationDelegate
30 : public GeofencingRegistrationDelegate {
31 public:
32 MOCK_METHOD2(RegistrationFinished,
33 void(int64 geofencing_registration_id, GeofencingStatus status));
34 MOCK_METHOD1(RegionEntered, void(int64 geofencing_registration_id));
35 MOCK_METHOD1(RegionExited, void(int64 geofencing_registration_id));
38 class MockGeofencingProvider : public GeofencingProvider {
39 public:
40 MOCK_METHOD3(RegisterRegion,
41 void(int64 geofencing_registration_id,
42 const blink::WebCircularGeofencingRegion& region,
43 const StatusCallback& callback));
44 MOCK_METHOD1(UnregisterRegion, void(int64 geofencing_registration_id));
47 ACTION_P(QuitRunner, runner) {
48 runner->Quit();
51 ACTION_P(SaveRegistrationId, geofencing_registration_id) {
52 *geofencing_registration_id = arg0;
55 ACTION_P(SaveStatusCallback, callback) {
56 *callback = arg2;
59 MATCHER_P(WebCircularGeofencingRegionEq, expected, "") {
60 return RegionsMatch(expected, arg);
63 class GeofencingServiceTest : public testing::Test {
64 public:
65 GeofencingServiceTest() : service_(nullptr) {
66 test_region_.latitude = 37.421999;
67 test_region_.longitude = -122.084015;
68 test_region_.radius = 100;
71 void SetUp() override { service_ = new GeofencingServiceImpl(); }
73 void TearDown() override { delete service_; }
75 void SetProviderForTests() {
76 provider_ = new MockGeofencingProvider();
77 service_->SetProviderForTesting(make_scoped_ptr(provider_));
80 int RegistrationCount() { return service_->RegistrationCountForTesting(); }
82 int64 RegisterRegionSync(const WebCircularGeofencingRegion& region,
83 GeofencingStatus provider_status) {
84 scoped_refptr<MessageLoopRunner> runner(new MessageLoopRunner());
86 // The registration ID that is passed to the provider.
87 int64 provider_registration_id = -1;
88 // The callback that is passed to the provider.
89 GeofencingProvider::StatusCallback callback;
91 EXPECT_CALL(
92 *provider_,
93 RegisterRegion(
94 testing::_, WebCircularGeofencingRegionEq(region), testing::_))
95 .WillOnce(testing::DoAll(SaveRegistrationId(&provider_registration_id),
96 SaveStatusCallback(&callback)));
98 int64 geofencing_registration_id =
99 service_->RegisterRegion(region, &delegate_);
101 // Service should have synchronously called the provider.
102 CHECK(!callback.is_null());
103 CHECK(provider_registration_id == geofencing_registration_id);
105 // Finish up registration by calling the callback and waiting for the
106 // delegate to be called.
107 EXPECT_CALL(
108 delegate_,
109 RegistrationFinished(geofencing_registration_id, provider_status))
110 .WillOnce(QuitRunner(runner));
111 callback.Run(provider_status);
112 runner->Run();
113 return geofencing_registration_id;
116 protected:
117 TestBrowserThreadBundle threads_;
118 GeofencingServiceImpl* service_;
119 MockGeofencingProvider* provider_;
120 MockGeofencingRegistrationDelegate delegate_;
122 WebCircularGeofencingRegion test_region_;
125 TEST_F(GeofencingServiceTest, RegisterRegion_NoProvider) {
126 scoped_refptr<MessageLoopRunner> runner(new MessageLoopRunner());
127 int64 geofencing_registration_id =
128 service_->RegisterRegion(test_region_, &delegate_);
129 EXPECT_CALL(delegate_,
130 RegistrationFinished(
131 geofencing_registration_id,
132 GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE))
133 .WillOnce(QuitRunner(runner));
134 runner->Run();
135 EXPECT_EQ(0, RegistrationCount());
138 TEST_F(GeofencingServiceTest, RegisterRegion_FailsInProvider) {
139 SetProviderForTests();
140 RegisterRegionSync(test_region_, GEOFENCING_STATUS_ERROR);
141 EXPECT_EQ(0, RegistrationCount());
144 TEST_F(GeofencingServiceTest, RegisterRegion_SucceedsInProvider) {
145 SetProviderForTests();
146 RegisterRegionSync(test_region_, GEOFENCING_STATUS_OK);
147 EXPECT_EQ(1, RegistrationCount());
150 TEST_F(GeofencingServiceTest, UnregisterRegion_AfterRegistration) {
151 SetProviderForTests();
152 int geofencing_registration_id =
153 RegisterRegionSync(test_region_, GEOFENCING_STATUS_OK);
154 EXPECT_EQ(1, RegistrationCount());
156 EXPECT_CALL(*provider_, UnregisterRegion(geofencing_registration_id));
157 service_->UnregisterRegion(geofencing_registration_id);
158 EXPECT_EQ(0, RegistrationCount());
161 TEST_F(GeofencingServiceTest, UnregisterRegion_DuringSuccesfullRegistration) {
162 SetProviderForTests();
163 scoped_refptr<MessageLoopRunner> runner(new MessageLoopRunner());
165 // The callback that is passed to the provider.
166 GeofencingProvider::StatusCallback callback;
168 EXPECT_CALL(
169 *provider_,
170 RegisterRegion(
171 testing::_, WebCircularGeofencingRegionEq(test_region_), testing::_))
172 .WillOnce(SaveStatusCallback(&callback));
174 int64 geofencing_registration_id =
175 service_->RegisterRegion(test_region_, &delegate_);
177 // Service should have synchronously called the provider.
178 CHECK(!callback.is_null());
180 // Call unregister before registration is finished.
181 service_->UnregisterRegion(geofencing_registration_id);
183 // Finish up registration by calling the callback and waiting for the
184 // provider to be called. The delegate should not be called in this case.
185 EXPECT_CALL(delegate_, RegistrationFinished(testing::_, testing::_)).Times(0);
186 EXPECT_CALL(*provider_, UnregisterRegion(geofencing_registration_id))
187 .WillOnce(QuitRunner(runner));
188 callback.Run(GEOFENCING_STATUS_OK);
189 runner->Run();
190 EXPECT_EQ(0, RegistrationCount());
193 } // namespace content