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/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/location_provider.h" | |
16 #include "net/base/net_errors.h" | |
17 #include "net/base/test_completion_callback.h" | |
18 #include "net/test/gtest_util.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | |
20 | |
21 using testing::SaveArg; | |
22 using testing::StrictMock; | |
23 using testing::_; | |
24 | |
25 namespace blimp { | |
26 namespace client { | |
27 | |
28 const double kLatitude = -42.0; | |
29 const double kLongitude = 17.3; | |
30 const double kAltitude = 123.4; | |
31 const double kAccuracy = 73.7; | |
32 | |
33 MATCHER(EqualsGeoposition, "") { | |
34 return arg.feature_case() == BlimpMessage::kGeolocation && | |
35 arg.geolocation().type_case() == GeolocationMessage::kCoordinates && | |
36 arg.geolocation().coordinates().latitude() == kLatitude && | |
37 arg.geolocation().coordinates().longitude() == kLongitude && | |
38 arg.geolocation().coordinates().altitude() == kAltitude && | |
39 arg.geolocation().coordinates().accuracy() == kAccuracy; | |
40 } | |
41 | |
42 MATCHER_P(EqualsError, error_code, "") { | |
43 return arg.feature_case() == BlimpMessage::kGeolocation && | |
44 arg.geolocation().type_case() == GeolocationMessage::kError && | |
45 arg.geolocation().error().error_code() == error_code; | |
46 } | |
47 | |
48 class GeolocationFeatureTest : public testing::Test { | |
49 public: | |
50 GeolocationFeatureTest() | |
51 : out_processor_(nullptr), | |
52 location_provider_(nullptr) {} | |
53 | |
54 void SetUp() override { | |
55 location_provider_ = new StrictMock<MockLocationProvider>(); | |
Kevin M
2016/08/01 20:51:04
This works but the ownership policy isn't as robus
CJ
2016/08/02 00:36:01
Done.
| |
56 EXPECT_CALL(*location_provider_, SetUpdateCallback(_)) | |
57 .WillOnce(SaveArg<0>(&callback_)); | |
58 feature_ = new GeolocationFeature(base::WrapUnique(location_provider_)); | |
Kevin M
2016/08/01 20:51:05
Doesn't this leak?
CJ
2016/08/02 00:36:01
Is it still a concern if this is now a unique_ptr?
| |
59 | |
60 out_processor_ = new MockBlimpMessageProcessor(); | |
61 feature_->set_outgoing_message_processor(base::WrapUnique(out_processor_)); | |
62 } | |
63 | |
64 void TearDown() override { | |
65 delete location_provider_; | |
Kevin M
2016/08/01 20:51:05
Use std::unique_ptr for these two fields, and remo
CJ
2016/08/02 00:36:01
Done.
| |
66 delete out_processor_; | |
67 } | |
68 | |
69 protected: | |
70 void SendMockSetInterestLevelMessage( | |
71 GeolocationSetInterestLevelMessage::Level level) { | |
72 GeolocationMessage* geolocation_message; | |
73 std::unique_ptr<BlimpMessage> message = | |
74 CreateBlimpMessage(&geolocation_message); | |
75 | |
76 GeolocationSetInterestLevelMessage* interest_message = | |
77 geolocation_message->mutable_set_interest_level(); | |
78 interest_message->set_level(level); | |
79 | |
80 net::TestCompletionCallback cb; | |
81 feature_->ProcessMessage(std::move(message), cb.callback()); | |
82 EXPECT_EQ(net::OK, cb.WaitForResult()); | |
83 } | |
84 | |
85 // These are raw pointers to classes that are | |
Kevin M
2016/08/01 20:51:04
nit: premature linewrap
CJ
2016/08/02 00:36:01
Done.
| |
86 // owned by the GeolocationFeature. | |
87 MockBlimpMessageProcessor* out_processor_; | |
88 StrictMock<MockLocationProvider>* location_provider_; | |
89 | |
90 GeolocationFeature* feature_; | |
91 device::LocationProvider::LocationProviderUpdateCallback callback_; | |
92 | |
93 private: | |
94 DISALLOW_COPY_AND_ASSIGN(GeolocationFeatureTest); | |
95 }; | |
96 | |
97 TEST_F(GeolocationFeatureTest, UpdateInterestLevelReceived) { | |
Kevin M
2016/08/01 20:51:04
Declaring an testing::InSequence object would be u
CJ
2016/08/02 00:36:01
Done.
| |
98 EXPECT_CALL(*location_provider_, StartProvider(true)); | |
99 EXPECT_CALL(*location_provider_, StopProvider()); | |
100 EXPECT_CALL(*location_provider_, StartProvider(false)); | |
101 | |
102 SendMockSetInterestLevelMessage( | |
103 GeolocationSetInterestLevelMessage::HIGH_ACCURACY); | |
104 SendMockSetInterestLevelMessage( | |
105 GeolocationSetInterestLevelMessage::NO_INTEREST); | |
106 SendMockSetInterestLevelMessage( | |
107 GeolocationSetInterestLevelMessage::LOW_ACCURACY); | |
108 } | |
109 | |
110 TEST_F(GeolocationFeatureTest, UnexpectedMessageReceived) { | |
111 GeolocationMessage* geolocation_message; | |
112 std::unique_ptr<BlimpMessage> message = | |
113 CreateBlimpMessage(&geolocation_message); | |
114 | |
115 GeolocationCoordinatesMessage* coordinates_message = | |
116 geolocation_message->mutable_coordinates(); | |
117 coordinates_message->set_latitude(1.0); | |
118 | |
119 net::TestCompletionCallback cb; | |
120 EXPECT_DFATAL(feature_->ProcessMessage(std::move(message), cb.callback()), | |
Kevin M
2016/08/01 20:51:05
This works??? Cool, I should use it.
CJ
2016/08/02 00:36:01
As far as I can see it does.
| |
121 "Unsupported message type."); | |
122 } | |
123 | |
124 TEST_F(GeolocationFeatureTest, RequestRefreshReceived) { | |
125 EXPECT_CALL(*location_provider_, RequestRefresh()); | |
126 | |
127 GeolocationMessage* geolocation_message; | |
128 std::unique_ptr<BlimpMessage> message = | |
129 CreateBlimpMessage(&geolocation_message); | |
130 geolocation_message->mutable_request_refresh(); | |
131 | |
132 net::TestCompletionCallback cb; | |
133 feature_->ProcessMessage(std::move(message), cb.callback()); | |
134 EXPECT_EQ(net::OK, cb.WaitForResult()); | |
135 } | |
136 | |
137 TEST_F(GeolocationFeatureTest, LocationUpdateSendsCorrectMessage) { | |
138 EXPECT_CALL(*out_processor_, MockableProcessMessage(EqualsGeoposition(), _)); | |
139 device::Geoposition position; | |
140 position.latitude = kLatitude; | |
141 position.longitude = kLongitude; | |
142 position.altitude = kAltitude; | |
143 position.accuracy = kAccuracy; | |
144 callback_.Run(location_provider_, position); | |
145 } | |
146 | |
147 TEST_F(GeolocationFeatureTest, ErrorUpdateSendsCorrectMessage) { | |
148 EXPECT_CALL(*out_processor_, | |
149 MockableProcessMessage( | |
150 EqualsError(GeolocationErrorMessage::POSITION_UNAVAILABLE), | |
151 _)); | |
152 EXPECT_CALL(*out_processor_, | |
153 MockableProcessMessage( | |
154 EqualsError(GeolocationErrorMessage::PERMISSION_DENIED), _)); | |
155 EXPECT_CALL(*out_processor_, | |
156 MockableProcessMessage( | |
157 EqualsError(GeolocationErrorMessage::TIMEOUT), _)); | |
158 | |
159 device::Geoposition position; | |
160 position.error_code = | |
161 device::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE; | |
162 callback_.Run(location_provider_, position); | |
163 | |
164 position.error_code = | |
165 device::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED; | |
166 callback_.Run(location_provider_, position); | |
167 | |
168 position.error_code = device::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT; | |
169 callback_.Run(location_provider_, position); | |
170 } | |
171 | |
172 } // namespace client | |
173 } // namespace blimp | |
OLD | NEW |