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::InSequence; | |
23 using testing::Invoke; | |
24 using testing::SaveArg; | |
25 using testing::StrictMock; | |
26 using testing::_; | |
27 | |
28 namespace blimp { | |
29 namespace client { | |
30 | |
31 const double kLatitude = -42.0; | |
32 const double kLongitude = 17.3; | |
33 const double kAltitude = 123.4; | |
34 const double kAccuracy = 73.7; | |
35 | |
36 MATCHER(EqualsDefaultGeoposition, "") { | |
37 return arg.feature_case() == BlimpMessage::kGeolocation && | |
38 arg.geolocation().type_case() == GeolocationMessage::kCoordinates && | |
39 arg.geolocation().coordinates().latitude() == kLatitude && | |
40 arg.geolocation().coordinates().longitude() == kLongitude && | |
41 arg.geolocation().coordinates().altitude() == kAltitude && | |
42 arg.geolocation().coordinates().accuracy() == kAccuracy; | |
43 } | |
44 | |
45 MATCHER_P4(EqualGeoposition, lat, lon, alt, acc, "") { | |
46 return arg.feature_case() == BlimpMessage::kGeolocation && | |
47 arg.geolocation().type_case() == GeolocationMessage::kCoordinates && | |
48 arg.geolocation().coordinates().latitude() == lat && | |
49 arg.geolocation().coordinates().longitude() == lon && | |
50 arg.geolocation().coordinates().altitude() == alt && | |
51 arg.geolocation().coordinates().accuracy() == acc; | |
52 } | |
53 | |
54 MATCHER_P(EqualsError, error_code, "") { | |
55 return arg.feature_case() == BlimpMessage::kGeolocation && | |
56 arg.geolocation().type_case() == GeolocationMessage::kError && | |
57 arg.geolocation().error().error_code() == error_code; | |
58 } | |
59 | |
60 class GeolocationFeatureTest : public testing::Test { | |
61 public: | |
62 GeolocationFeatureTest() {} | |
63 | |
64 void SetUp() override { | |
65 auto location_provider = | |
66 base::MakeUnique<StrictMock<device::MockLocationProvider>>(); | |
67 location_provider_ = location_provider.get(); | |
68 EXPECT_CALL(*location_provider_, SetUpdateCallback(_)) | |
69 .WillOnce(SaveArg<0>(&callback_)); | |
70 feature_ = | |
71 base::MakeUnique<GeolocationFeature>(std::move(location_provider)); | |
72 | |
73 auto out_processor = | |
74 base::MakeUnique<StrictMock<MockBlimpMessageProcessor>>(); | |
75 out_processor_ = out_processor.get(); | |
76 feature_->set_outgoing_message_processor(std::move(out_processor)); | |
77 | |
78 position_.latitude = kLatitude; | |
79 position_.longitude = kLongitude; | |
80 position_.altitude = kAltitude; | |
81 position_.accuracy = kAccuracy; | |
82 } | |
83 | |
84 protected: | |
85 void SendMockSetInterestLevelMessage( | |
86 GeolocationSetInterestLevelMessage::Level level) { | |
87 GeolocationMessage* geolocation_message; | |
88 std::unique_ptr<BlimpMessage> message = | |
89 CreateBlimpMessage(&geolocation_message); | |
90 | |
91 GeolocationSetInterestLevelMessage* interest_message = | |
92 geolocation_message->mutable_set_interest_level(); | |
93 interest_message->set_level(level); | |
94 | |
95 net::TestCompletionCallback cb; | |
96 feature_->ProcessMessage(std::move(message), cb.callback()); | |
97 EXPECT_EQ(net::OK, cb.WaitForResult()); | |
98 } | |
99 | |
100 void ReportProcessMessageSuccess(const BlimpMessage& blimp_message, | |
101 const net::CompletionCallback& callback) { | |
102 callback.Run(net::OK); | |
103 } | |
104 | |
105 // These are raw pointers to classes that are owned by the | |
106 // GeolocationFeature. | |
107 StrictMock<MockBlimpMessageProcessor>* out_processor_; | |
108 StrictMock<device::MockLocationProvider>* location_provider_; | |
109 | |
110 std::unique_ptr<GeolocationFeature> feature_; | |
111 device::LocationProvider::LocationProviderUpdateCallback callback_; | |
112 device::Geoposition position_; | |
113 | |
114 private: | |
115 DISALLOW_COPY_AND_ASSIGN(GeolocationFeatureTest); | |
116 }; | |
117 | |
118 TEST_F(GeolocationFeatureTest, UpdateInterestLevelReceived) { | |
119 InSequence s; | |
120 | |
121 EXPECT_CALL(*location_provider_, StartProvider(true)); | |
122 EXPECT_CALL(*location_provider_, StopProvider()); | |
123 EXPECT_CALL(*location_provider_, StartProvider(false)); | |
124 | |
125 SendMockSetInterestLevelMessage( | |
126 GeolocationSetInterestLevelMessage::HIGH_ACCURACY); | |
127 SendMockSetInterestLevelMessage( | |
128 GeolocationSetInterestLevelMessage::NO_INTEREST); | |
129 SendMockSetInterestLevelMessage( | |
130 GeolocationSetInterestLevelMessage::LOW_ACCURACY); | |
131 } | |
132 | |
133 TEST_F(GeolocationFeatureTest, UnexpectedMessageReceived) { | |
134 GeolocationMessage* geolocation_message; | |
135 std::unique_ptr<BlimpMessage> message = | |
136 CreateBlimpMessage(&geolocation_message); | |
137 | |
138 GeolocationCoordinatesMessage* coordinates_message = | |
139 geolocation_message->mutable_coordinates(); | |
140 coordinates_message->set_latitude(1.0); | |
141 | |
142 net::TestCompletionCallback cb; | |
143 feature_->ProcessMessage(std::move(message), cb.callback()); | |
144 | |
145 EXPECT_EQ(net::ERR_UNEXPECTED, cb.WaitForResult()); | |
146 } | |
147 | |
148 TEST_F(GeolocationFeatureTest, RequestRefreshReceived) { | |
149 EXPECT_CALL(*location_provider_, OnPermissionGranted()); | |
150 | |
151 GeolocationMessage* geolocation_message; | |
152 std::unique_ptr<BlimpMessage> message = | |
153 CreateBlimpMessage(&geolocation_message); | |
154 geolocation_message->mutable_request_refresh(); | |
155 | |
156 net::TestCompletionCallback cb; | |
157 feature_->ProcessMessage(std::move(message), cb.callback()); | |
158 EXPECT_EQ(net::OK, cb.WaitForResult()); | |
159 } | |
160 | |
161 TEST_F(GeolocationFeatureTest, LocationUpdateSendsCorrectMessage) { | |
162 EXPECT_CALL(*out_processor_, | |
163 MockableProcessMessage(EqualsDefaultGeoposition(), _)); | |
164 callback_.Run(location_provider_, position_); | |
165 } | |
166 | |
167 TEST_F(GeolocationFeatureTest, ErrorUpdateSendsCorrectMessage) { | |
168 ON_CALL(*out_processor_, MockableProcessMessage(_, _)) | |
169 .WillByDefault(Invoke( | |
170 this, &GeolocationFeatureTest_ErrorUpdateSendsCorrectMessage_Test:: | |
171 ReportProcessMessageSuccess)); | |
172 EXPECT_CALL( | |
173 *out_processor_, | |
174 MockableProcessMessage( | |
175 EqualsError(GeolocationErrorMessage::POSITION_UNAVAILABLE), _)); | |
176 EXPECT_CALL(*out_processor_, | |
177 MockableProcessMessage( | |
178 EqualsError(GeolocationErrorMessage::PERMISSION_DENIED), _)); | |
179 EXPECT_CALL( | |
180 *out_processor_, | |
181 MockableProcessMessage(EqualsError(GeolocationErrorMessage::TIMEOUT), _)); | |
182 | |
183 device::Geoposition err_position; | |
184 err_position.error_code = | |
185 device::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE; | |
186 callback_.Run(location_provider_, err_position); | |
187 | |
188 err_position.error_code = | |
189 device::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED; | |
190 callback_.Run(location_provider_, err_position); | |
191 | |
192 err_position.error_code = device::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT; | |
193 callback_.Run(location_provider_, err_position); | |
194 } | |
195 | |
196 TEST_F(GeolocationFeatureTest, NoRepeatSendsWithMessagePending) { | |
197 EXPECT_CALL(*out_processor_, | |
198 MockableProcessMessage(EqualsDefaultGeoposition(), _)); | |
199 callback_.Run(location_provider_, position_); | |
200 callback_.Run(location_provider_, position_); | |
201 callback_.Run(location_provider_, position_); | |
202 } | |
203 | |
204 TEST_F(GeolocationFeatureTest, MessageSendsAfterAcknowledgement) { | |
205 EXPECT_CALL(*out_processor_, | |
206 MockableProcessMessage(EqualsDefaultGeoposition(), _)) | |
207 .WillOnce(Invoke( | |
208 this, &GeolocationFeatureTest_MessageSendsAfterAcknowledgement_Test:: | |
209 ReportProcessMessageSuccess)); | |
210 | |
211 device::Geoposition position; | |
212 position.latitude = 1.0; | |
213 position.longitude = 1.0; | |
214 position.altitude = 1.0; | |
215 position.accuracy = 1.0; | |
216 position.timestamp = base::Time::Now(); | |
217 | |
218 EXPECT_CALL(*out_processor_, | |
219 MockableProcessMessage(EqualGeoposition(1.0, 1.0, 1.0, 1.0), _)); | |
220 callback_.Run(location_provider_, position_); | |
221 callback_.Run(location_provider_, position); | |
222 } | |
223 | |
224 TEST_F(GeolocationFeatureTest, ProcessMessageHandlesNullCallback) { | |
225 EXPECT_CALL(*location_provider_, OnPermissionGranted()); | |
226 | |
227 GeolocationMessage* geolocation_message; | |
228 std::unique_ptr<BlimpMessage> message = | |
229 CreateBlimpMessage(&geolocation_message); | |
230 geolocation_message->mutable_request_refresh(); | |
231 | |
232 feature_->ProcessMessage(std::move(message), net::CompletionCallback()); | |
233 } | |
234 | |
235 } // namespace client | |
236 } // namespace blimp | |
OLD | NEW |