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/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::InSequence; | |
23 using testing::SaveArg; | |
24 using testing::StrictMock; | |
25 using testing::_; | |
26 | |
27 namespace blimp { | |
28 namespace client { | |
29 | |
30 const double kLatitude = -42.0; | |
31 const double kLongitude = 17.3; | |
32 const double kAltitude = 123.4; | |
33 const double kAccuracy = 73.7; | |
34 | |
35 MATCHER(EqualsGeoposition, "") { | |
36 return arg.feature_case() == BlimpMessage::kGeolocation && | |
37 arg.geolocation().type_case() == GeolocationMessage::kCoordinates && | |
38 arg.geolocation().coordinates().latitude() == kLatitude && | |
39 arg.geolocation().coordinates().longitude() == kLongitude && | |
40 arg.geolocation().coordinates().altitude() == kAltitude && | |
41 arg.geolocation().coordinates().accuracy() == kAccuracy; | |
42 } | |
43 | |
44 MATCHER_P(EqualsError, error_code, "") { | |
45 return arg.feature_case() == BlimpMessage::kGeolocation && | |
46 arg.geolocation().type_case() == GeolocationMessage::kError && | |
47 arg.geolocation().error().error_code() == error_code; | |
48 } | |
49 | |
50 class GeolocationFeatureTest : public testing::Test { | |
51 public: | |
52 GeolocationFeatureTest() {} | |
53 | |
54 void SetUp() override { | |
55 auto location_provider = | |
56 base::MakeUnique<StrictMock<MockLocationProvider>>(); | |
57 location_provider_ = location_provider.get(); | |
58 EXPECT_CALL(*location_provider_, SetUpdateCallback(_)) | |
59 .WillOnce(SaveArg<0>(&callback_)); | |
60 feature_ = base::MakeUnique<GeolocationFeature>( | |
61 std::move(location_provider)); | |
62 | |
63 auto out_processor = base::MakeUnique<MockBlimpMessageProcessor>(); | |
64 out_processor_ = out_processor.get(); | |
65 feature_->set_outgoing_message_processor(std::move(out_processor)); | |
66 } | |
67 | |
68 protected: | |
69 void SendMockSetInterestLevelMessage( | |
70 GeolocationSetInterestLevelMessage::Level level) { | |
71 GeolocationMessage* geolocation_message; | |
72 std::unique_ptr<BlimpMessage> message = | |
73 CreateBlimpMessage(&geolocation_message); | |
74 | |
75 GeolocationSetInterestLevelMessage* interest_message = | |
76 geolocation_message->mutable_set_interest_level(); | |
77 interest_message->set_level(level); | |
78 | |
79 net::TestCompletionCallback cb; | |
80 feature_->ProcessMessage(std::move(message), cb.callback()); | |
81 EXPECT_EQ(net::OK, cb.WaitForResult()); | |
82 } | |
83 | |
84 // These are raw pointers to classes that are owned by the | |
85 // GeolocationFeature. | |
86 MockBlimpMessageProcessor* out_processor_; | |
87 StrictMock<MockLocationProvider>* location_provider_; | |
88 | |
89 std::unique_ptr<GeolocationFeature> feature_; | |
90 device::LocationProvider::LocationProviderUpdateCallback callback_; | |
91 | |
92 private: | |
93 DISALLOW_COPY_AND_ASSIGN(GeolocationFeatureTest); | |
94 }; | |
95 | |
96 TEST_F(GeolocationFeatureTest, UpdateInterestLevelReceived) { | |
97 InSequence s; | |
98 | |
99 EXPECT_CALL(*location_provider_, StartProvider(true)); | |
100 EXPECT_CALL(*location_provider_, StopProvider()); | |
101 EXPECT_CALL(*location_provider_, StartProvider(false)); | |
102 | |
103 SendMockSetInterestLevelMessage( | |
104 GeolocationSetInterestLevelMessage::HIGH_ACCURACY); | |
105 SendMockSetInterestLevelMessage( | |
106 GeolocationSetInterestLevelMessage::NO_INTEREST); | |
107 SendMockSetInterestLevelMessage( | |
108 GeolocationSetInterestLevelMessage::LOW_ACCURACY); | |
109 } | |
110 | |
111 TEST_F(GeolocationFeatureTest, UnexpectedMessageReceived) { | |
112 GeolocationMessage* geolocation_message; | |
113 std::unique_ptr<BlimpMessage> message = | |
114 CreateBlimpMessage(&geolocation_message); | |
115 | |
116 GeolocationCoordinatesMessage* coordinates_message = | |
117 geolocation_message->mutable_coordinates(); | |
118 coordinates_message->set_latitude(1.0); | |
119 | |
120 net::TestCompletionCallback cb; | |
121 EXPECT_DFATAL(feature_->ProcessMessage(std::move(message), cb.callback()), | |
122 "Unsupported message type."); | |
123 } | |
124 | |
125 TEST_F(GeolocationFeatureTest, RequestRefreshReceived) { | |
126 EXPECT_CALL(*location_provider_, RequestRefresh()); | |
127 | |
128 GeolocationMessage* geolocation_message; | |
129 std::unique_ptr<BlimpMessage> message = | |
130 CreateBlimpMessage(&geolocation_message); | |
131 geolocation_message->mutable_request_refresh(); | |
132 | |
133 net::TestCompletionCallback cb; | |
134 feature_->ProcessMessage(std::move(message), cb.callback()); | |
135 EXPECT_EQ(net::OK, cb.WaitForResult()); | |
136 } | |
137 | |
138 TEST_F(GeolocationFeatureTest, LocationUpdateSendsCorrectMessage) { | |
139 EXPECT_CALL(*out_processor_, MockableProcessMessage(EqualsGeoposition(), _)); | |
140 device::Geoposition position; | |
141 position.latitude = kLatitude; | |
142 position.longitude = kLongitude; | |
143 position.altitude = kAltitude; | |
144 position.accuracy = kAccuracy; | |
145 callback_.Run(location_provider_, position); | |
146 } | |
147 | |
148 TEST_F(GeolocationFeatureTest, ErrorUpdateSendsCorrectMessage) { | |
Wez
2016/08/02 01:18:11
Add a test-case for the Feature handling a null Pr
CJ
2016/08/03 20:06:03
Done.
| |
149 EXPECT_CALL(*out_processor_, | |
150 MockableProcessMessage( | |
151 EqualsError(GeolocationErrorMessage::POSITION_UNAVAILABLE), | |
152 _)); | |
153 EXPECT_CALL(*out_processor_, | |
154 MockableProcessMessage( | |
155 EqualsError(GeolocationErrorMessage::PERMISSION_DENIED), _)); | |
156 EXPECT_CALL(*out_processor_, | |
157 MockableProcessMessage( | |
158 EqualsError(GeolocationErrorMessage::TIMEOUT), _)); | |
159 | |
160 device::Geoposition position; | |
161 position.error_code = | |
162 device::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE; | |
163 callback_.Run(location_provider_, position); | |
164 | |
165 position.error_code = | |
166 device::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED; | |
167 callback_.Run(location_provider_, position); | |
168 | |
169 position.error_code = device::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT; | |
170 callback_.Run(location_provider_, position); | |
171 } | |
172 | |
173 } // namespace client | |
174 } // namespace blimp | |
OLD | NEW |