Chromium Code Reviews| 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::Invoke; | |
| 24 using testing::SaveArg; | |
| 25 using testing::StrictMock; | |
| 26 using testing::_; | |
| 27 | |
| 28 namespace blimp { | |
| 29 namespace client { | |
| 30 | |
| 31 const double kLatitude = -42.0; | |
| 32 const double kLongitude = 17.3; | |
| 33 const double kAltitude = 123.4; | |
| 34 const double kAccuracy = 73.7; | |
| 35 | |
| 36 MATCHER(EqualsGeoposition, "") { | |
| 37 return arg.feature_case() == BlimpMessage::kGeolocation && | |
| 38 arg.geolocation().type_case() == GeolocationMessage::kCoordinates && | |
| 39 arg.geolocation().coordinates().latitude() == kLatitude && | |
| 40 arg.geolocation().coordinates().longitude() == kLongitude && | |
| 41 arg.geolocation().coordinates().altitude() == kAltitude && | |
| 42 arg.geolocation().coordinates().accuracy() == kAccuracy; | |
| 43 } | |
| 44 | |
| 45 MATCHER_P(EqualsError, error_code, "") { | |
| 46 return arg.feature_case() == BlimpMessage::kGeolocation && | |
| 47 arg.geolocation().type_case() == GeolocationMessage::kError && | |
| 48 arg.geolocation().error().error_code() == error_code; | |
| 49 } | |
| 50 | |
| 51 class GeolocationFeatureTest : public testing::Test { | |
| 52 public: | |
| 53 GeolocationFeatureTest() {} | |
| 54 | |
| 55 void SetUp() override { | |
| 56 auto location_provider = | |
| 57 base::MakeUnique<StrictMock<MockLocationProvider>>(); | |
| 58 location_provider_ = location_provider.get(); | |
| 59 EXPECT_CALL(*location_provider_, SetUpdateCallback(_)) | |
| 60 .WillOnce(SaveArg<0>(&callback_)); | |
| 61 feature_ = | |
| 62 base::MakeUnique<GeolocationFeature>(std::move(location_provider)); | |
| 63 | |
| 64 auto out_processor = base::MakeUnique<MockBlimpMessageProcessor>(); | |
| 65 out_processor_ = out_processor.get(); | |
| 66 feature_->set_outgoing_message_processor(std::move(out_processor)); | |
| 67 } | |
| 68 | |
| 69 void RunCallback(const BlimpMessage& blimp_message, | |
| 70 const net::CompletionCallback& callback) { | |
| 71 callback.Run(net::OK); | |
| 72 } | |
| 73 | |
| 74 protected: | |
|
Kevin M
2016/08/09 22:08:55
Please revisit visibility. Not quite clear why som
CJ
2016/08/10 00:13:49
Did it, but don't we usually like things to be mor
Kevin M
2016/08/10 23:23:34
In general, the visibility should make good semant
CJ
2016/08/12 21:57:46
Done.
| |
| 75 void SendMockSetInterestLevelMessage( | |
| 76 GeolocationSetInterestLevelMessage::Level level) { | |
| 77 GeolocationMessage* geolocation_message; | |
| 78 std::unique_ptr<BlimpMessage> message = | |
| 79 CreateBlimpMessage(&geolocation_message); | |
| 80 | |
| 81 GeolocationSetInterestLevelMessage* interest_message = | |
| 82 geolocation_message->mutable_set_interest_level(); | |
| 83 interest_message->set_level(level); | |
| 84 | |
| 85 net::TestCompletionCallback cb; | |
| 86 feature_->ProcessMessage(std::move(message), cb.callback()); | |
| 87 EXPECT_EQ(net::OK, cb.WaitForResult()); | |
| 88 } | |
| 89 | |
| 90 // These are raw pointers to classes that are owned by the | |
| 91 // GeolocationFeature. | |
| 92 MockBlimpMessageProcessor* out_processor_; | |
| 93 StrictMock<MockLocationProvider>* location_provider_; | |
| 94 | |
| 95 std::unique_ptr<GeolocationFeature> feature_; | |
| 96 device::LocationProvider::LocationProviderUpdateCallback callback_; | |
| 97 | |
| 98 private: | |
| 99 DISALLOW_COPY_AND_ASSIGN(GeolocationFeatureTest); | |
| 100 }; | |
| 101 | |
| 102 TEST_F(GeolocationFeatureTest, UpdateInterestLevelReceived) { | |
| 103 InSequence s; | |
| 104 | |
| 105 EXPECT_CALL(*location_provider_, StartProvider(true)); | |
| 106 EXPECT_CALL(*location_provider_, StopProvider()); | |
| 107 EXPECT_CALL(*location_provider_, StartProvider(false)); | |
| 108 | |
| 109 SendMockSetInterestLevelMessage( | |
| 110 GeolocationSetInterestLevelMessage::HIGH_ACCURACY); | |
| 111 SendMockSetInterestLevelMessage( | |
| 112 GeolocationSetInterestLevelMessage::NO_INTEREST); | |
| 113 SendMockSetInterestLevelMessage( | |
| 114 GeolocationSetInterestLevelMessage::LOW_ACCURACY); | |
| 115 } | |
| 116 | |
| 117 TEST_F(GeolocationFeatureTest, UnexpectedMessageReceived) { | |
| 118 GeolocationMessage* geolocation_message; | |
| 119 std::unique_ptr<BlimpMessage> message = | |
| 120 CreateBlimpMessage(&geolocation_message); | |
| 121 | |
| 122 GeolocationCoordinatesMessage* coordinates_message = | |
| 123 geolocation_message->mutable_coordinates(); | |
| 124 coordinates_message->set_latitude(1.0); | |
| 125 | |
| 126 net::TestCompletionCallback cb; | |
| 127 EXPECT_DFATAL(feature_->ProcessMessage(std::move(message), cb.callback()), | |
| 128 "Invalid message type."); | |
| 129 } | |
| 130 | |
| 131 TEST_F(GeolocationFeatureTest, RequestRefreshReceived) { | |
| 132 EXPECT_CALL(*location_provider_, RequestRefresh()); | |
| 133 | |
| 134 GeolocationMessage* geolocation_message; | |
| 135 std::unique_ptr<BlimpMessage> message = | |
| 136 CreateBlimpMessage(&geolocation_message); | |
| 137 geolocation_message->mutable_request_refresh(); | |
| 138 | |
| 139 net::TestCompletionCallback cb; | |
| 140 feature_->ProcessMessage(std::move(message), cb.callback()); | |
| 141 EXPECT_EQ(net::OK, cb.WaitForResult()); | |
| 142 } | |
| 143 | |
| 144 TEST_F(GeolocationFeatureTest, LocationUpdateSendsCorrectMessage) { | |
| 145 EXPECT_CALL(*out_processor_, MockableProcessMessage(EqualsGeoposition(), _)); | |
| 146 device::Geoposition position; | |
| 147 position.latitude = kLatitude; | |
| 148 position.longitude = kLongitude; | |
| 149 position.altitude = kAltitude; | |
| 150 position.accuracy = kAccuracy; | |
| 151 callback_.Run(location_provider_, position); | |
| 152 } | |
| 153 | |
| 154 TEST_F(GeolocationFeatureTest, ErrorUpdateSendsCorrectMessage) { | |
| 155 ON_CALL(*out_processor_, MockableProcessMessage(_, _)) | |
| 156 .WillByDefault(Invoke(this, &GeolocationFeatureTest::RunCallback)); | |
| 157 EXPECT_CALL( | |
| 158 *out_processor_, | |
| 159 MockableProcessMessage( | |
| 160 EqualsError(GeolocationErrorMessage::POSITION_UNAVAILABLE), _)); | |
| 161 EXPECT_CALL(*out_processor_, | |
| 162 MockableProcessMessage( | |
| 163 EqualsError(GeolocationErrorMessage::PERMISSION_DENIED), _)); | |
| 164 EXPECT_CALL( | |
| 165 *out_processor_, | |
| 166 MockableProcessMessage(EqualsError(GeolocationErrorMessage::TIMEOUT), _)); | |
| 167 | |
| 168 device::Geoposition position; | |
| 169 position.error_code = | |
| 170 device::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE; | |
| 171 callback_.Run(location_provider_, position); | |
| 172 | |
| 173 position.error_code = | |
| 174 device::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED; | |
| 175 callback_.Run(location_provider_, position); | |
| 176 | |
| 177 position.error_code = device::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT; | |
| 178 callback_.Run(location_provider_, position); | |
| 179 } | |
| 180 | |
| 181 TEST_F(GeolocationFeatureTest, NoRepeatSendsWithMessagePending) { | |
| 182 EXPECT_CALL(*out_processor_, MockableProcessMessage(EqualsGeoposition(), _)) | |
| 183 .Times(1); | |
| 184 device::Geoposition position; | |
| 185 position.latitude = kLatitude; | |
| 186 position.longitude = kLongitude; | |
| 187 position.altitude = kAltitude; | |
| 188 position.accuracy = kAccuracy; | |
| 189 callback_.Run(location_provider_, position); | |
| 190 callback_.Run(location_provider_, position); | |
| 191 callback_.Run(location_provider_, position); | |
| 192 } | |
| 193 | |
| 194 TEST_F(GeolocationFeatureTest, ProcessMessageHandlesNullCallback) { | |
| 195 EXPECT_CALL(*location_provider_, RequestRefresh()); | |
| 196 | |
| 197 GeolocationMessage* geolocation_message; | |
| 198 std::unique_ptr<BlimpMessage> message = | |
| 199 CreateBlimpMessage(&geolocation_message); | |
| 200 geolocation_message->mutable_request_refresh(); | |
| 201 | |
| 202 feature_->ProcessMessage(std::move(message), net::CompletionCallback()); | |
| 203 } | |
| 204 | |
| 205 } // namespace client | |
| 206 } // namespace blimp | |
| OLD | NEW |