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