OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 "blimp/client/core/geolocation/geolocation_feature.h" | |
6 | |
7 #include <memory> | |
8 #include <utility> | |
9 | |
10 #include "base/memory/ptr_util.h" | |
11 #include "blimp/common/create_blimp_message.h" | |
12 #include "blimp/common/proto/blimp_message.pb.h" | |
13 #include "blimp/net/test_common.h" | |
14 #include "device/geolocation/geoposition.h" | |
15 #include "device/geolocation/location_provider.h" | |
16 #include "device/geolocation/mock_location_provider.h" | |
17 #include "net/base/net_errors.h" | |
18 #include "net/base/test_completion_callback.h" | |
19 #include "net/test/gtest_util.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | |
21 | |
22 using testing::Assign; | |
Kevin M
2016/08/19 22:16:26
not used
CJ
2016/08/19 22:50:06
Done.
| |
23 using testing::InSequence; | |
24 using testing::Invoke; | |
25 using testing::Return; | |
Kevin M
2016/08/19 22:16:26
not used
CJ
2016/08/19 22:50:06
Done.
| |
26 using testing::SaveArg; | |
27 using testing::StrictMock; | |
28 using testing::_; | |
29 | |
30 namespace blimp { | |
31 namespace client { | |
32 | |
33 const double kLatitude = -42.0; | |
34 const double kLongitude = 17.3; | |
35 const double kAltitude = 123.4; | |
36 const double kAccuracy = 73.7; | |
37 | |
38 MATCHER(EqualsDefaultGeoposition, "") { | |
39 return arg.feature_case() == BlimpMessage::kGeolocation && | |
40 arg.geolocation().type_case() == GeolocationMessage::kCoordinates && | |
41 arg.geolocation().coordinates().latitude() == kLatitude && | |
42 arg.geolocation().coordinates().longitude() == kLongitude && | |
43 arg.geolocation().coordinates().altitude() == kAltitude && | |
44 arg.geolocation().coordinates().accuracy() == kAccuracy; | |
45 } | |
46 | |
47 MATCHER_P4(EqualGeoposition, lat, lon, alt, acc, "") { | |
48 return arg.feature_case() == BlimpMessage::kGeolocation && | |
49 arg.geolocation().type_case() == GeolocationMessage::kCoordinates && | |
50 arg.geolocation().coordinates().latitude() == lat && | |
51 arg.geolocation().coordinates().longitude() == lon && | |
52 arg.geolocation().coordinates().altitude() == alt && | |
53 arg.geolocation().coordinates().accuracy() == acc; | |
54 } | |
55 | |
56 MATCHER_P(EqualsError, error_code, "") { | |
57 return arg.feature_case() == BlimpMessage::kGeolocation && | |
58 arg.geolocation().type_case() == GeolocationMessage::kError && | |
59 arg.geolocation().error().error_code() == error_code; | |
60 } | |
61 | |
62 class GeolocationFeatureTest : public testing::Test { | |
63 public: | |
64 GeolocationFeatureTest() {} | |
65 | |
66 void SetUp() override { | |
67 auto location_provider = | |
68 base::MakeUnique<StrictMock<device::MockLocationProvider>>(); | |
69 location_provider_ = location_provider.get(); | |
70 EXPECT_CALL(*location_provider_, SetUpdateCallback(_)) | |
71 .WillOnce(SaveArg<0>(&callback_)); | |
72 feature_ = | |
73 base::MakeUnique<GeolocationFeature>(std::move(location_provider)); | |
74 | |
75 auto out_processor = | |
76 base::MakeUnique<StrictMock<MockBlimpMessageProcessor>>(); | |
77 out_processor_ = out_processor.get(); | |
78 feature_->set_outgoing_message_processor(std::move(out_processor)); | |
79 | |
80 position_.latitude = kLatitude; | |
81 position_.longitude = kLongitude; | |
82 position_.altitude = kAltitude; | |
83 position_.accuracy = kAccuracy; | |
84 } | |
85 | |
86 void GetPosition(device::Geoposition* position) { *position = position_; } | |
Wez
2016/08/19 18:50:25
What is this for?
CJ
2016/08/19 22:22:33
Seems like poor merging to me. Removed.
| |
87 | |
88 protected: | |
89 void SendMockSetInterestLevelMessage( | |
90 GeolocationSetInterestLevelMessage::Level level) { | |
91 GeolocationMessage* geolocation_message; | |
92 std::unique_ptr<BlimpMessage> message = | |
93 CreateBlimpMessage(&geolocation_message); | |
94 | |
95 GeolocationSetInterestLevelMessage* interest_message = | |
96 geolocation_message->mutable_set_interest_level(); | |
97 interest_message->set_level(level); | |
98 | |
99 net::TestCompletionCallback cb; | |
100 feature_->ProcessMessage(std::move(message), cb.callback()); | |
101 EXPECT_EQ(net::OK, cb.WaitForResult()); | |
102 } | |
103 | |
104 void RunCallback(const BlimpMessage& blimp_message, | |
105 const net::CompletionCallback& callback) { | |
Wez
2016/08/19 18:50:25
nit: Suggest calling this ReportProcessMessageSucc
CJ
2016/08/19 22:22:33
Done.
| |
106 callback.Run(net::OK); | |
107 } | |
108 | |
109 void RunErrorCallback(const BlimpMessage& blimp_message, | |
110 const net::CompletionCallback& callback) { | |
Wez
2016/08/19 18:50:25
This doesn't seem to be used anywhere?
CJ
2016/08/19 22:22:33
Gone.
| |
111 callback.Run(net::ERR_TIMED_OUT); | |
112 } | |
113 | |
114 void RunCompletionCallback(const net::CompletionCallback callback) { | |
115 callback.Run(net::OK); | |
Wez
2016/08/19 18:50:25
Nor does this?
CJ
2016/08/19 22:22:33
Gone.
| |
116 } | |
117 | |
118 // These are raw pointers to classes that are owned by the | |
119 // GeolocationFeature. | |
120 StrictMock<MockBlimpMessageProcessor>* out_processor_; | |
121 StrictMock<device::MockLocationProvider>* location_provider_; | |
122 | |
123 std::unique_ptr<GeolocationFeature> feature_; | |
124 device::LocationProvider::LocationProviderUpdateCallback callback_; | |
125 device::Geoposition position_; | |
126 | |
127 private: | |
128 DISALLOW_COPY_AND_ASSIGN(GeolocationFeatureTest); | |
129 }; | |
130 | |
131 TEST_F(GeolocationFeatureTest, UpdateInterestLevelReceived) { | |
132 InSequence s; | |
133 | |
134 EXPECT_CALL(*location_provider_, StartProvider(true)); | |
135 EXPECT_CALL(*location_provider_, StopProvider()); | |
136 EXPECT_CALL(*location_provider_, StartProvider(false)); | |
137 | |
138 SendMockSetInterestLevelMessage( | |
139 GeolocationSetInterestLevelMessage::HIGH_ACCURACY); | |
140 SendMockSetInterestLevelMessage( | |
141 GeolocationSetInterestLevelMessage::NO_INTEREST); | |
142 SendMockSetInterestLevelMessage( | |
143 GeolocationSetInterestLevelMessage::LOW_ACCURACY); | |
144 } | |
145 | |
146 TEST_F(GeolocationFeatureTest, UnexpectedMessageReceived) { | |
147 GeolocationMessage* geolocation_message; | |
148 std::unique_ptr<BlimpMessage> message = | |
149 CreateBlimpMessage(&geolocation_message); | |
150 | |
151 GeolocationCoordinatesMessage* coordinates_message = | |
152 geolocation_message->mutable_coordinates(); | |
153 coordinates_message->set_latitude(1.0); | |
154 | |
155 net::TestCompletionCallback cb; | |
156 feature_->ProcessMessage(std::move(message), cb.callback()); | |
157 | |
158 EXPECT_EQ(net::ERR_UNEXPECTED, cb.WaitForResult()); | |
159 } | |
160 | |
161 TEST_F(GeolocationFeatureTest, RequestRefreshReceived) { | |
162 EXPECT_CALL(*location_provider_, OnPermissionGranted()); | |
163 | |
164 GeolocationMessage* geolocation_message; | |
165 std::unique_ptr<BlimpMessage> message = | |
166 CreateBlimpMessage(&geolocation_message); | |
167 geolocation_message->mutable_request_refresh(); | |
168 | |
169 net::TestCompletionCallback cb; | |
170 feature_->ProcessMessage(std::move(message), cb.callback()); | |
171 EXPECT_EQ(net::OK, cb.WaitForResult()); | |
172 } | |
173 | |
174 TEST_F(GeolocationFeatureTest, LocationUpdateSendsCorrectMessage) { | |
175 EXPECT_CALL(*out_processor_, | |
176 MockableProcessMessage(EqualsDefaultGeoposition(), _)); | |
177 callback_.Run(location_provider_, position_); | |
178 } | |
179 | |
180 TEST_F(GeolocationFeatureTest, ErrorUpdateSendsCorrectMessage) { | |
181 ON_CALL(*out_processor_, MockableProcessMessage(_, _)) | |
Kevin M
2016/08/19 22:16:26
Wow, this works. Apparently EXPECT_CALL and ON_CAL
CJ
2016/08/19 22:50:06
Acknowledged.
| |
182 .WillByDefault(Invoke( | |
183 this, &GeolocationFeatureTest_ErrorUpdateSendsCorrectMessage_Test:: | |
184 RunCallback)); | |
185 EXPECT_CALL( | |
186 *out_processor_, | |
187 MockableProcessMessage( | |
188 EqualsError(GeolocationErrorMessage::POSITION_UNAVAILABLE), _)); | |
189 EXPECT_CALL(*out_processor_, | |
190 MockableProcessMessage( | |
191 EqualsError(GeolocationErrorMessage::PERMISSION_DENIED), _)); | |
192 EXPECT_CALL( | |
193 *out_processor_, | |
194 MockableProcessMessage(EqualsError(GeolocationErrorMessage::TIMEOUT), _)); | |
195 | |
196 device::Geoposition err_position; | |
197 err_position.error_code = | |
198 device::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE; | |
199 callback_.Run(location_provider_, err_position); | |
200 | |
201 err_position.error_code = | |
202 device::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED; | |
203 callback_.Run(location_provider_, err_position); | |
204 | |
205 err_position.error_code = device::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT; | |
206 callback_.Run(location_provider_, err_position); | |
207 } | |
208 | |
209 TEST_F(GeolocationFeatureTest, NoRepeatSendsWithMessagePending) { | |
210 EXPECT_CALL(*out_processor_, | |
211 MockableProcessMessage(EqualsDefaultGeoposition(), _)); | |
212 callback_.Run(location_provider_, position_); | |
213 callback_.Run(location_provider_, position_); | |
214 callback_.Run(location_provider_, position_); | |
215 } | |
216 | |
217 TEST_F(GeolocationFeatureTest, MessageSendsAfterAcknowledgement) { | |
218 EXPECT_CALL(*out_processor_, | |
219 MockableProcessMessage(EqualsDefaultGeoposition(), _)) | |
220 .WillOnce(Invoke( | |
221 this, &GeolocationFeatureTest_MessageSendsAfterAcknowledgement_Test:: | |
222 RunCallback)); | |
223 device::Geoposition position; | |
224 position.latitude = 1.0; | |
225 position.longitude = 1.0; | |
226 position.altitude = 1.0; | |
227 position.accuracy = 1.0; | |
228 EXPECT_CALL(*out_processor_, | |
229 MockableProcessMessage(EqualGeoposition(1.0, 1.0, 1.0, 1.0), _)); | |
230 callback_.Run(location_provider_, position_); | |
231 callback_.Run(location_provider_, position); | |
232 } | |
233 | |
234 TEST_F(GeolocationFeatureTest, ProcessMessageHandlesNullCallback) { | |
235 EXPECT_CALL(*location_provider_, OnPermissionGranted()); | |
236 | |
237 GeolocationMessage* geolocation_message; | |
238 std::unique_ptr<BlimpMessage> message = | |
239 CreateBlimpMessage(&geolocation_message); | |
240 geolocation_message->mutable_request_refresh(); | |
241 | |
242 feature_->ProcessMessage(std::move(message), net::CompletionCallback()); | |
243 } | |
244 | |
245 } // namespace client | |
246 } // namespace blimp | |
OLD | NEW |