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 "content/browser/geofencing/geofencing_service.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include "base/memory/ptr_util.h" | |
10 #include "content/browser/geofencing/geofencing_provider.h" | |
11 #include "content/browser/geofencing/geofencing_registration_delegate.h" | |
12 #include "content/public/test/test_browser_thread_bundle.h" | |
13 #include "content/public/test/test_utils.h" | |
14 #include "testing/gmock/include/gmock/gmock.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h" | |
17 | |
18 using blink::WebCircularGeofencingRegion; | |
19 | |
20 namespace { | |
21 | |
22 bool RegionsMatch(const WebCircularGeofencingRegion& expected, | |
23 const WebCircularGeofencingRegion& arg) { | |
24 return testing::Matches(expected.latitude)(arg.latitude) && | |
25 testing::Matches(expected.longitude)(arg.longitude) && | |
26 testing::Matches(expected.radius)(arg.radius); | |
27 } | |
28 | |
29 } // namespace | |
30 | |
31 namespace content { | |
32 | |
33 class MockGeofencingRegistrationDelegate | |
34 : public GeofencingRegistrationDelegate { | |
35 public: | |
36 MOCK_METHOD2(RegistrationFinished, | |
37 void(int64_t geofencing_registration_id, | |
38 GeofencingStatus status)); | |
39 MOCK_METHOD1(RegionEntered, void(int64_t geofencing_registration_id)); | |
40 MOCK_METHOD1(RegionExited, void(int64_t geofencing_registration_id)); | |
41 }; | |
42 | |
43 class MockGeofencingProvider : public GeofencingProvider { | |
44 public: | |
45 MOCK_METHOD3(RegisterRegion, | |
46 void(int64_t geofencing_registration_id, | |
47 const blink::WebCircularGeofencingRegion& region, | |
48 const StatusCallback& callback)); | |
49 MOCK_METHOD1(UnregisterRegion, void(int64_t geofencing_registration_id)); | |
50 }; | |
51 | |
52 ACTION_P(QuitRunner, runner) { | |
53 runner->Quit(); | |
54 } | |
55 | |
56 ACTION_P(SaveRegistrationId, geofencing_registration_id) { | |
57 *geofencing_registration_id = arg0; | |
58 } | |
59 | |
60 ACTION_P(SaveStatusCallback, callback) { | |
61 *callback = arg2; | |
62 } | |
63 | |
64 MATCHER_P(WebCircularGeofencingRegionEq, expected, "") { | |
65 return RegionsMatch(expected, arg); | |
66 } | |
67 | |
68 class GeofencingServiceTest : public testing::Test { | |
69 public: | |
70 GeofencingServiceTest() : service_(nullptr) { | |
71 test_region_.latitude = 37.421999; | |
72 test_region_.longitude = -122.084015; | |
73 test_region_.radius = 100; | |
74 } | |
75 | |
76 void SetUp() override { service_ = new GeofencingServiceImpl(); } | |
77 | |
78 void TearDown() override { delete service_; } | |
79 | |
80 void SetProviderForTests() { | |
81 provider_ = new MockGeofencingProvider(); | |
82 service_->SetProviderForTesting(base::WrapUnique(provider_)); | |
83 } | |
84 | |
85 int RegistrationCount() { return service_->RegistrationCountForTesting(); } | |
86 | |
87 int64_t RegisterRegionSync(const WebCircularGeofencingRegion& region, | |
88 GeofencingStatus provider_status) { | |
89 scoped_refptr<MessageLoopRunner> runner(new MessageLoopRunner()); | |
90 | |
91 // The registration ID that is passed to the provider. | |
92 int64_t provider_registration_id = -1; | |
93 // The callback that is passed to the provider. | |
94 GeofencingProvider::StatusCallback callback; | |
95 | |
96 EXPECT_CALL( | |
97 *provider_, | |
98 RegisterRegion( | |
99 testing::_, WebCircularGeofencingRegionEq(region), testing::_)) | |
100 .WillOnce(testing::DoAll(SaveRegistrationId(&provider_registration_id), | |
101 SaveStatusCallback(&callback))); | |
102 | |
103 int64_t geofencing_registration_id = | |
104 service_->RegisterRegion(region, &delegate_); | |
105 | |
106 // Service should have synchronously called the provider. | |
107 CHECK(!callback.is_null()); | |
108 CHECK(provider_registration_id == geofencing_registration_id); | |
109 | |
110 // Finish up registration by calling the callback and waiting for the | |
111 // delegate to be called. | |
112 EXPECT_CALL( | |
113 delegate_, | |
114 RegistrationFinished(geofencing_registration_id, provider_status)) | |
115 .WillOnce(QuitRunner(runner)); | |
116 callback.Run(provider_status); | |
117 runner->Run(); | |
118 return geofencing_registration_id; | |
119 } | |
120 | |
121 protected: | |
122 TestBrowserThreadBundle threads_; | |
123 GeofencingServiceImpl* service_; | |
124 MockGeofencingProvider* provider_; | |
125 MockGeofencingRegistrationDelegate delegate_; | |
126 | |
127 WebCircularGeofencingRegion test_region_; | |
128 }; | |
129 | |
130 TEST_F(GeofencingServiceTest, RegisterRegion_NoProvider) { | |
131 scoped_refptr<MessageLoopRunner> runner(new MessageLoopRunner()); | |
132 int64_t geofencing_registration_id = | |
133 service_->RegisterRegion(test_region_, &delegate_); | |
134 EXPECT_CALL(delegate_, | |
135 RegistrationFinished( | |
136 geofencing_registration_id, | |
137 GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE)) | |
138 .WillOnce(QuitRunner(runner)); | |
139 runner->Run(); | |
140 EXPECT_EQ(0, RegistrationCount()); | |
141 } | |
142 | |
143 TEST_F(GeofencingServiceTest, RegisterRegion_FailsInProvider) { | |
144 SetProviderForTests(); | |
145 RegisterRegionSync(test_region_, GEOFENCING_STATUS_ERROR); | |
146 EXPECT_EQ(0, RegistrationCount()); | |
147 } | |
148 | |
149 TEST_F(GeofencingServiceTest, RegisterRegion_SucceedsInProvider) { | |
150 SetProviderForTests(); | |
151 RegisterRegionSync(test_region_, GEOFENCING_STATUS_OK); | |
152 EXPECT_EQ(1, RegistrationCount()); | |
153 } | |
154 | |
155 TEST_F(GeofencingServiceTest, UnregisterRegion_AfterRegistration) { | |
156 SetProviderForTests(); | |
157 int geofencing_registration_id = | |
158 RegisterRegionSync(test_region_, GEOFENCING_STATUS_OK); | |
159 EXPECT_EQ(1, RegistrationCount()); | |
160 | |
161 EXPECT_CALL(*provider_, UnregisterRegion(geofencing_registration_id)); | |
162 service_->UnregisterRegion(geofencing_registration_id); | |
163 EXPECT_EQ(0, RegistrationCount()); | |
164 } | |
165 | |
166 TEST_F(GeofencingServiceTest, UnregisterRegion_DuringSuccesfullRegistration) { | |
167 SetProviderForTests(); | |
168 scoped_refptr<MessageLoopRunner> runner(new MessageLoopRunner()); | |
169 | |
170 // The callback that is passed to the provider. | |
171 GeofencingProvider::StatusCallback callback; | |
172 | |
173 EXPECT_CALL( | |
174 *provider_, | |
175 RegisterRegion( | |
176 testing::_, WebCircularGeofencingRegionEq(test_region_), testing::_)) | |
177 .WillOnce(SaveStatusCallback(&callback)); | |
178 | |
179 int64_t geofencing_registration_id = | |
180 service_->RegisterRegion(test_region_, &delegate_); | |
181 | |
182 // Service should have synchronously called the provider. | |
183 CHECK(!callback.is_null()); | |
184 | |
185 // Call unregister before registration is finished. | |
186 service_->UnregisterRegion(geofencing_registration_id); | |
187 | |
188 // Finish up registration by calling the callback and waiting for the | |
189 // provider to be called. The delegate should not be called in this case. | |
190 EXPECT_CALL(delegate_, RegistrationFinished(testing::_, testing::_)).Times(0); | |
191 EXPECT_CALL(*provider_, UnregisterRegion(geofencing_registration_id)) | |
192 .WillOnce(QuitRunner(runner)); | |
193 callback.Run(GEOFENCING_STATUS_OK); | |
194 runner->Run(); | |
195 EXPECT_EQ(0, RegistrationCount()); | |
196 } | |
197 | |
198 } // namespace content | |
OLD | NEW |