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

Side by Side Diff: blimp/engine/feature/geolocation/engine_geolocation_feature_unittest.cc

Issue 2091023006: Adds EngineGeolocationFeature for Blimp Geolocation project. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Merge Created 4 years, 5 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/engine/feature/geolocation/engine_geolocation_feature.h"
6
7 #include <memory>
8 #include <string>
9 #include <utility>
10
11 #include "base/memory/ptr_util.h"
12 #include "blimp/common/create_blimp_message.h"
13 #include "blimp/common/proto/blimp_message.pb.h"
14 #include "blimp/common/proto/geolocation.pb.h"
15 #include "blimp/net/test_common.h"
16 #include "content/public/browser/location_provider.h"
17 #include "content/public/common/geoposition.h"
18 #include "net/base/test_completion_callback.h"
19 #include "net/test/gtest_util.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22
23 using testing::_;
24
25 namespace blimp {
26 namespace engine {
27
28 void SendMockLocationMessage(BlimpMessageProcessor* processor) {
29 GeolocationMessage* geolocation_message;
30 std::unique_ptr<BlimpMessage> message =
31 CreateBlimpMessage(&geolocation_message);
32
33 GeolocationCoordinatesMessage* coordinates_message =
34 geolocation_message->mutable_coordinates();
35 coordinates_message->set_latitude(-42.0);
36 coordinates_message->set_longitude(17.3);
37 coordinates_message->set_altitude(123.4);
38 coordinates_message->set_accuracy(73.7);
39
40 net::TestCompletionCallback cb;
41 processor->ProcessMessage(std::move(message), cb.callback());
42 EXPECT_EQ(net::OK, cb.WaitForResult());
43 }
44
45 void SendMockErrorMessage(BlimpMessageProcessor* processor,
46 GeolocationErrorMessage::ErrorCode error,
47 std::string error_message) {
48 GeolocationMessage* geolocation_message;
49 std::unique_ptr<BlimpMessage> message =
50 CreateBlimpMessage(&geolocation_message);
51 GeolocationErrorMessage* geolocation_error_message =
52 geolocation_message->mutable_error();
53 geolocation_error_message->set_error_code(error);
54 geolocation_error_message->set_error_message(error_message);
55
56 net::TestCompletionCallback cb;
57 processor->ProcessMessage(std::move(message), cb.callback());
58 EXPECT_EQ(net::OK, cb.WaitForResult());
59 }
60
61 void SendMalformedMessage(BlimpMessageProcessor* processor) {
62 GeolocationMessage* geolocation_message;
63 std::unique_ptr<BlimpMessage> message =
64 CreateBlimpMessage(&geolocation_message);
65
66 net::TestCompletionCallback cb;
67 processor->ProcessMessage(std::move(message), cb.callback());
68 EXPECT_EQ(net::OK, cb.WaitForResult());
69 }
70
71 std::unique_ptr<BlimpMessage> CreateUnexpectedMessage() {
72 GeolocationMessage* geolocation_message;
73 std::unique_ptr<BlimpMessage> message =
74 CreateBlimpMessage(&geolocation_message);
75 GeolocationSetInterestLevelMessage* geolocation_set_interest_level =
76 geolocation_message->mutable_set_interest_level();
77 geolocation_set_interest_level->set_level(
78 GeolocationSetInterestLevelMessage::NO_INTEREST);
79 return message;
80 }
81
82 MATCHER_P(EqualsUpdatedRequestLevel, level, "") {
83 return arg.feature_case() == BlimpMessage::kGeolocation &&
84 arg.geolocation().type_case() ==
85 GeolocationMessage::kSetInterestLevel &&
86 arg.geolocation().set_interest_level().level() == level;
87 }
88
89 MATCHER(EqualsRequestRefresh, "") {
90 return arg.feature_case() == BlimpMessage::kGeolocation &&
91 arg.geolocation().type_case() == GeolocationMessage::kRequestRefresh;
92 }
93
94 class EngineGeolocationFeatureTest : public testing::Test {
95 public:
96 EngineGeolocationFeatureTest()
97 : out_processor_(nullptr),
98 location_provider_(feature_.CreateGeolocationDelegate()
99 ->OverrideSystemLocationProvider()),
100 mock_callback_(
101 base::Bind(&EngineGeolocationFeatureTest::OnLocationUpdate,
102 base::Unretained(this))) {}
103
104 void SetUp() override {
105 out_processor_ = new MockBlimpMessageProcessor();
106 feature_.set_outgoing_message_processor(base::WrapUnique(out_processor_));
107 location_provider_->SetUpdateCallback(mock_callback_);
108 }
109
110 void OnLocationUpdate(const content::LocationProvider* provider,
111 const content::Geoposition& geoposition) {
112 received_position = geoposition;
113 }
114
115 protected:
116 // This is a raw pointer to a class that is owned by the GeolocationFeature.
117 MockBlimpMessageProcessor* out_processor_;
118
119 EngineGeolocationFeature feature_;
120 std::unique_ptr<content::LocationProvider> location_provider_;
121 content::LocationProvider::LocationProviderUpdateCallback mock_callback_;
122 content::Geoposition received_position;
123 };
124
125 TEST_F(EngineGeolocationFeatureTest, LocationReceived) {
126 SendMockLocationMessage(&feature_);
127 EXPECT_EQ(content::Geoposition::ERROR_CODE_NONE,
128 received_position.error_code);
129 EXPECT_EQ(-42.0, received_position.latitude);
130 EXPECT_EQ(17.3, received_position.longitude);
131 EXPECT_EQ(123.4, received_position.altitude);
132 EXPECT_EQ(73.7, received_position.accuracy);
133 }
134
135 TEST_F(EngineGeolocationFeatureTest, ErrorRecieved) {
136 SendMockErrorMessage(&feature_, GeolocationErrorMessage::PERMISSION_DENIED,
137 "PERMISSION_DENIED");
138 EXPECT_EQ(content::Geoposition::ERROR_CODE_PERMISSION_DENIED,
139 received_position.error_code);
140 EXPECT_EQ("PERMISSION_DENIED", received_position.error_message);
141
142 SendMockErrorMessage(&feature_, GeolocationErrorMessage::POSITION_UNAVAILABLE,
143 "POSITION_UNAVAILABLE");
144 EXPECT_EQ(content::Geoposition::ERROR_CODE_POSITION_UNAVAILABLE,
145 received_position.error_code);
146 EXPECT_EQ("POSITION_UNAVAILABLE", received_position.error_message);
147 }
148
149 TEST_F(EngineGeolocationFeatureTest, UpdateRequestLevel) {
150 EXPECT_CALL(*out_processor_,
151 MockableProcessMessage(
152 EqualsUpdatedRequestLevel(
153 GeolocationSetInterestLevelMessage::HIGH_ACCURACY),
154 _))
155 .Times(1);
156 EXPECT_CALL(*out_processor_,
157 MockableProcessMessage(
158 EqualsUpdatedRequestLevel(
159 GeolocationSetInterestLevelMessage::LOW_ACCURACY),
160 _))
161 .Times(1);
162 EXPECT_CALL(*out_processor_,
163 MockableProcessMessage(
164 EqualsUpdatedRequestLevel(
165 GeolocationSetInterestLevelMessage::NO_INTEREST),
166 _))
167 .Times(1);
168
169 location_provider_->StartProvider(true);
170 location_provider_->StartProvider(false);
171 location_provider_->StopProvider();
172 }
173
174 TEST_F(EngineGeolocationFeatureTest, UnexpectedMessageReceived) {
175 std::unique_ptr<BlimpMessage> message = CreateUnexpectedMessage();
176 net::TestCompletionCallback cb;
177 feature_.ProcessMessage(std::move(message), cb.callback());
178
179 EXPECT_EQ(net::ERR_UNEXPECTED, cb.WaitForResult());
180 }
181
182 TEST_F(EngineGeolocationFeatureTest, RequestRefresh) {
183 EXPECT_CALL(*out_processor_,
184 MockableProcessMessage(
185 EqualsUpdatedRequestLevel(
186 GeolocationSetInterestLevelMessage::HIGH_ACCURACY),
187 _))
188 .Times(1);
189
190 EXPECT_CALL(*out_processor_,
191 MockableProcessMessage(EqualsRequestRefresh(), _))
192 .Times(1);
193 EXPECT_CALL(*out_processor_,
194 MockableProcessMessage(
195 EqualsUpdatedRequestLevel(
196 GeolocationSetInterestLevelMessage::NO_INTEREST),
197 _))
198 .Times(1);
199
200 location_provider_->StartProvider(true);
201 location_provider_->RequestRefresh();
202 }
203
204 } // namespace engine
205 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698