Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(796)

Side by Side Diff: blimp/client/feature/geolocation_feature_unittest.cc

Issue 2161223003: Adds GeolocationFeature for Blimp Geolocation project. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@engine_feature_prep
Patch Set: Adding more deps to BUILD for device/geolocation. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/feature/geolocation_feature.h"
6
7 #include <memory>
8 #include <utility>
9
10 #include "base/memory/ptr_util.h"
11 #include "blimp/client/feature/mock_location_provider.h"
12 #include "blimp/common/create_blimp_message.h"
13 #include "blimp/common/proto/blimp_message.pb.h"
14 #include "blimp/net/test_common.h"
15 #include "device/geolocation/geoposition.h"
16 #include "device/geolocation/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;
23 using testing::InSequence;
24 using testing::Invoke;
25 using testing::Return;
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(EqualsGeoposition, "") {
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_P(EqualsError, error_code, "") {
48 return arg.feature_case() == BlimpMessage::kGeolocation &&
49 arg.geolocation().type_case() == GeolocationMessage::kError &&
50 arg.geolocation().error().error_code() == error_code;
51 }
52
53 class GeolocationFeatureTest : public testing::Test {
54 public:
55 GeolocationFeatureTest() {}
56
57 void SetUp() override {
58 auto location_provider =
59 base::MakeUnique<StrictMock<MockLocationProvider>>();
60 location_provider_ = location_provider.get();
61 EXPECT_CALL(*location_provider_, SetUpdateCallback(_))
62 .WillOnce(SaveArg<0>(&callback_));
63 feature_ =
64 base::MakeUnique<GeolocationFeature>(std::move(location_provider));
65
66 auto out_processor = base::MakeUnique<MockBlimpMessageProcessor>();
67 out_processor_ = out_processor.get();
68 feature_->set_outgoing_message_processor(std::move(out_processor));
69
70 position_.latitude = kLatitude;
71 position_.longitude = kLongitude;
72 position_.altitude = kAltitude;
73 position_.accuracy = kAccuracy;
74 }
75
76 void RunCallback(const BlimpMessage& blimp_message,
77 const net::CompletionCallback& callback) {
78 callback.Run(net::OK);
79 }
80
81 void RunErrorCallback(const BlimpMessage& blimp_message,
82 const net::CompletionCallback& callback) {
83 callback.Run(net::ERR_TIMED_OUT);
84 }
85
86 void GetPosition(device::Geoposition* position) { *position = position_; }
87
88 void SendMockSetInterestLevelMessage(
89 GeolocationSetInterestLevelMessage::Level level) {
90 GeolocationMessage* geolocation_message;
91 std::unique_ptr<BlimpMessage> message =
92 CreateBlimpMessage(&geolocation_message);
93
94 GeolocationSetInterestLevelMessage* interest_message =
95 geolocation_message->mutable_set_interest_level();
96 interest_message->set_level(level);
97
98 net::TestCompletionCallback cb;
99 feature_->ProcessMessage(std::move(message), cb.callback());
100 EXPECT_EQ(net::OK, cb.WaitForResult());
101 }
102
103 protected:
104 // These are raw pointers to classes that are owned by the
105 // GeolocationFeature.
106 MockBlimpMessageProcessor* out_processor_;
107 StrictMock<MockLocationProvider>* location_provider_;
108
109 std::unique_ptr<GeolocationFeature> feature_;
110 device::LocationProvider::LocationProviderUpdateCallback callback_;
111 device::Geoposition position_;
112
113 private:
114 DISALLOW_COPY_AND_ASSIGN(GeolocationFeatureTest);
115 };
116
117 TEST_F(GeolocationFeatureTest, UpdateInterestLevelReceived) {
118 InSequence s;
119
120 EXPECT_CALL(*location_provider_, StartProvider(true));
121 EXPECT_CALL(*location_provider_, StopProvider());
122 EXPECT_CALL(*location_provider_, StartProvider(false));
123
124 SendMockSetInterestLevelMessage(
125 GeolocationSetInterestLevelMessage::HIGH_ACCURACY);
126 SendMockSetInterestLevelMessage(
127 GeolocationSetInterestLevelMessage::NO_INTEREST);
128 SendMockSetInterestLevelMessage(
129 GeolocationSetInterestLevelMessage::LOW_ACCURACY);
130 }
131
132 TEST_F(GeolocationFeatureTest, UnexpectedMessageReceived) {
133 GeolocationMessage* geolocation_message;
134 std::unique_ptr<BlimpMessage> message =
135 CreateBlimpMessage(&geolocation_message);
136
137 GeolocationCoordinatesMessage* coordinates_message =
138 geolocation_message->mutable_coordinates();
139 coordinates_message->set_latitude(1.0);
140
141 net::TestCompletionCallback cb;
142 EXPECT_DFATAL(feature_->ProcessMessage(std::move(message), cb.callback()),
143 "Invalid message type: 3.");
144 }
145
146 TEST_F(GeolocationFeatureTest, RequestRefreshReceived) {
147 EXPECT_CALL(*location_provider_, RequestRefresh());
148
149 GeolocationMessage* geolocation_message;
150 std::unique_ptr<BlimpMessage> message =
151 CreateBlimpMessage(&geolocation_message);
152 geolocation_message->mutable_request_refresh();
153
154 net::TestCompletionCallback cb;
155 feature_->ProcessMessage(std::move(message), cb.callback());
156 EXPECT_EQ(net::OK, cb.WaitForResult());
157 }
158
159 TEST_F(GeolocationFeatureTest, LocationUpdateSendsCorrectMessage) {
160 EXPECT_CALL(*out_processor_, MockableProcessMessage(EqualsGeoposition(), _));
161 callback_.Run(location_provider_, position_);
162 }
163
164 TEST_F(GeolocationFeatureTest, ErrorUpdateSendsCorrectMessage) {
165 ON_CALL(*out_processor_, MockableProcessMessage(_, _))
166 .WillByDefault(Invoke(this, &GeolocationFeatureTest::RunCallback));
167 EXPECT_CALL(
168 *out_processor_,
169 MockableProcessMessage(
170 EqualsError(GeolocationErrorMessage::POSITION_UNAVAILABLE), _));
171 EXPECT_CALL(*out_processor_,
172 MockableProcessMessage(
173 EqualsError(GeolocationErrorMessage::PERMISSION_DENIED), _));
174 EXPECT_CALL(
175 *out_processor_,
176 MockableProcessMessage(EqualsError(GeolocationErrorMessage::TIMEOUT), _));
177
178 device::Geoposition err_position;
179 err_position.error_code =
180 device::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE;
181 callback_.Run(location_provider_, err_position);
182
183 err_position.error_code =
184 device::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED;
185 callback_.Run(location_provider_, err_position);
186
187 err_position.error_code = device::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT;
188 callback_.Run(location_provider_, err_position);
189 }
190
191 TEST_F(GeolocationFeatureTest, NoRepeatSendsWithMessagePending) {
192 EXPECT_CALL(*out_processor_, MockableProcessMessage(EqualsGeoposition(), _))
193 .Times(1);
194 callback_.Run(location_provider_, position_);
195 callback_.Run(location_provider_, position_);
196 callback_.Run(location_provider_, position_);
197 }
198
199 TEST_F(GeolocationFeatureTest, FailedSendResends) {
200 EXPECT_CALL(*out_processor_, MockableProcessMessage(EqualsGeoposition(), _))
201 .WillOnce(Invoke(this, &GeolocationFeatureTest::RunErrorCallback))
202 .WillOnce(Invoke(this, &GeolocationFeatureTest::RunCallback));
203 EXPECT_CALL(*location_provider_, GetPosition(_))
204 .WillOnce(Invoke(this, &GeolocationFeatureTest::GetPosition));
205
206 callback_.Run(location_provider_, position_);
207 }
208
209 TEST_F(GeolocationFeatureTest, ProcessMessageHandlesNullCallback) {
210 EXPECT_CALL(*location_provider_, RequestRefresh());
211
212 GeolocationMessage* geolocation_message;
213 std::unique_ptr<BlimpMessage> message =
214 CreateBlimpMessage(&geolocation_message);
215 geolocation_message->mutable_request_refresh();
216
217 feature_->ProcessMessage(std::move(message), net::CompletionCallback());
218 }
219
220 } // namespace client
221 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698