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

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: Addresses kmarshall's #66 comments 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/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;
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<device::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 =
67 base::MakeUnique<StrictMock<MockBlimpMessageProcessor>>();
68 out_processor_ = out_processor.get();
69 feature_->set_outgoing_message_processor(std::move(out_processor));
70
71 position_.latitude = kLatitude;
72 position_.longitude = kLongitude;
73 position_.altitude = kAltitude;
74 position_.accuracy = kAccuracy;
75 }
76
77 void GetPosition(device::Geoposition* position) { *position = position_; }
78
79 protected:
80 void SendMockSetInterestLevelMessage(
81 GeolocationSetInterestLevelMessage::Level level) {
82 GeolocationMessage* geolocation_message;
83 std::unique_ptr<BlimpMessage> message =
84 CreateBlimpMessage(&geolocation_message);
85
86 GeolocationSetInterestLevelMessage* interest_message =
87 geolocation_message->mutable_set_interest_level();
88 interest_message->set_level(level);
89
90 net::TestCompletionCallback cb;
91 feature_->ProcessMessage(std::move(message), cb.callback());
92 EXPECT_EQ(net::OK, cb.WaitForResult());
93 }
94
95 void RunCallback(const BlimpMessage& blimp_message,
96 const net::CompletionCallback& callback) {
97 callback.Run(net::OK);
98 }
99
100 void RunErrorCallback(const BlimpMessage& blimp_message,
101 const net::CompletionCallback& callback) {
102 callback.Run(net::ERR_TIMED_OUT);
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_, RequestRefresh());
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_, MockableProcessMessage(EqualsGeoposition(), _));
163 callback_.Run(location_provider_, position_);
164 }
165
166 TEST_F(GeolocationFeatureTest, ErrorUpdateSendsCorrectMessage) {
167 ON_CALL(*out_processor_, MockableProcessMessage(_, _))
168 .WillByDefault(Invoke(
169 this, &GeolocationFeatureTest_ErrorUpdateSendsCorrectMessage_Test::
170 RunCallback));
171 EXPECT_CALL(
172 *out_processor_,
173 MockableProcessMessage(
174 EqualsError(GeolocationErrorMessage::POSITION_UNAVAILABLE), _));
175 EXPECT_CALL(*out_processor_,
176 MockableProcessMessage(
177 EqualsError(GeolocationErrorMessage::PERMISSION_DENIED), _));
178 EXPECT_CALL(
179 *out_processor_,
180 MockableProcessMessage(EqualsError(GeolocationErrorMessage::TIMEOUT), _));
181
182 device::Geoposition err_position;
183 err_position.error_code =
184 device::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE;
185 callback_.Run(location_provider_, err_position);
186
187 err_position.error_code =
188 device::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED;
189 callback_.Run(location_provider_, err_position);
190
191 err_position.error_code = device::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT;
192 callback_.Run(location_provider_, err_position);
193 }
194
195 TEST_F(GeolocationFeatureTest, NoRepeatSendsWithMessagePending) {
196 EXPECT_CALL(*out_processor_, MockableProcessMessage(EqualsGeoposition(), _));
197 callback_.Run(location_provider_, position_);
198 callback_.Run(location_provider_, position_);
199 callback_.Run(location_provider_, position_);
200 }
201
202 TEST_F(GeolocationFeatureTest, ProcessMessageHandlesNullCallback) {
203 EXPECT_CALL(*location_provider_, RequestRefresh());
204
205 GeolocationMessage* geolocation_message;
206 std::unique_ptr<BlimpMessage> message =
207 CreateBlimpMessage(&geolocation_message);
208 geolocation_message->mutable_request_refresh();
209
210 feature_->ProcessMessage(std::move(message), net::CompletionCallback());
211 }
212
213 } // namespace client
214 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698