Chromium Code Reviews| Index: blimp/client/feature/geolocation_feature_unittest.cc |
| diff --git a/blimp/client/feature/geolocation_feature_unittest.cc b/blimp/client/feature/geolocation_feature_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a37cc4da562f5cfdc2a99e18df9d281a894b3001 |
| --- /dev/null |
| +++ b/blimp/client/feature/geolocation_feature_unittest.cc |
| @@ -0,0 +1,214 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "blimp/client/feature/geolocation_feature.h" |
| + |
| +#include <memory> |
| +#include <utility> |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "blimp/common/create_blimp_message.h" |
| +#include "blimp/common/proto/blimp_message.pb.h" |
| +#include "blimp/net/test_common.h" |
| +#include "device/geolocation/geoposition.h" |
| +#include "device/geolocation/location_provider.h" |
| +#include "device/geolocation/mock_location_provider.h" |
| +#include "net/base/net_errors.h" |
| +#include "net/base/test_completion_callback.h" |
| +#include "net/test/gtest_util.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using testing::Assign; |
| +using testing::InSequence; |
| +using testing::Invoke; |
| +using testing::Return; |
| +using testing::SaveArg; |
| +using testing::StrictMock; |
| +using testing::_; |
| + |
| +namespace blimp { |
| +namespace client { |
| + |
| +const double kLatitude = -42.0; |
| +const double kLongitude = 17.3; |
| +const double kAltitude = 123.4; |
| +const double kAccuracy = 73.7; |
| + |
| +MATCHER(EqualsGeoposition, "") { |
| + return arg.feature_case() == BlimpMessage::kGeolocation && |
| + arg.geolocation().type_case() == GeolocationMessage::kCoordinates && |
| + arg.geolocation().coordinates().latitude() == kLatitude && |
| + arg.geolocation().coordinates().longitude() == kLongitude && |
| + arg.geolocation().coordinates().altitude() == kAltitude && |
| + arg.geolocation().coordinates().accuracy() == kAccuracy; |
| +} |
| + |
| +MATCHER_P(EqualsError, error_code, "") { |
| + return arg.feature_case() == BlimpMessage::kGeolocation && |
| + arg.geolocation().type_case() == GeolocationMessage::kError && |
| + arg.geolocation().error().error_code() == error_code; |
| +} |
| + |
| +class GeolocationFeatureTest : public testing::Test { |
| + public: |
| + GeolocationFeatureTest() {} |
| + |
| + void SetUp() override { |
| + auto location_provider = |
| + base::MakeUnique<StrictMock<device::MockLocationProvider>>(); |
| + location_provider_ = location_provider.get(); |
| + EXPECT_CALL(*location_provider_, SetUpdateCallback(_)) |
| + .WillOnce(SaveArg<0>(&callback_)); |
| + feature_ = |
| + base::MakeUnique<GeolocationFeature>(std::move(location_provider)); |
| + |
| + auto out_processor = base::MakeUnique<MockBlimpMessageProcessor>(); |
|
Kevin M
2016/08/15 20:35:40
StrictMock?
Also remove Times(1)
CJ
2016/08/15 21:59:32
Done.
|
| + out_processor_ = out_processor.get(); |
| + feature_->set_outgoing_message_processor(std::move(out_processor)); |
| + |
| + position_.latitude = kLatitude; |
| + position_.longitude = kLongitude; |
| + position_.altitude = kAltitude; |
| + position_.accuracy = kAccuracy; |
| + } |
| + |
| + void GetPosition(device::Geoposition* position) { *position = position_; } |
| + |
| + protected: |
| + void SendMockSetInterestLevelMessage( |
| + GeolocationSetInterestLevelMessage::Level level) { |
| + GeolocationMessage* geolocation_message; |
| + std::unique_ptr<BlimpMessage> message = |
| + CreateBlimpMessage(&geolocation_message); |
| + |
| + GeolocationSetInterestLevelMessage* interest_message = |
| + geolocation_message->mutable_set_interest_level(); |
| + interest_message->set_level(level); |
| + |
| + net::TestCompletionCallback cb; |
| + feature_->ProcessMessage(std::move(message), cb.callback()); |
| + EXPECT_EQ(net::OK, cb.WaitForResult()); |
| + } |
| + |
| + void RunCallback(const BlimpMessage& blimp_message, |
| + const net::CompletionCallback& callback) { |
| + callback.Run(net::OK); |
| + } |
| + |
| + void RunErrorCallback(const BlimpMessage& blimp_message, |
| + const net::CompletionCallback& callback) { |
| + callback.Run(net::ERR_TIMED_OUT); |
| + } |
| + |
| + // These are raw pointers to classes that are owned by the |
| + // GeolocationFeature. |
| + MockBlimpMessageProcessor* out_processor_; |
| + StrictMock<device::MockLocationProvider>* location_provider_; |
| + |
| + std::unique_ptr<GeolocationFeature> feature_; |
| + device::LocationProvider::LocationProviderUpdateCallback callback_; |
| + device::Geoposition position_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(GeolocationFeatureTest); |
| +}; |
| + |
| +TEST_F(GeolocationFeatureTest, UpdateInterestLevelReceived) { |
| + InSequence s; |
| + |
| + EXPECT_CALL(*location_provider_, StartProvider(true)); |
| + EXPECT_CALL(*location_provider_, StopProvider()); |
| + EXPECT_CALL(*location_provider_, StartProvider(false)); |
| + |
| + SendMockSetInterestLevelMessage( |
| + GeolocationSetInterestLevelMessage::HIGH_ACCURACY); |
| + SendMockSetInterestLevelMessage( |
| + GeolocationSetInterestLevelMessage::NO_INTEREST); |
| + SendMockSetInterestLevelMessage( |
| + GeolocationSetInterestLevelMessage::LOW_ACCURACY); |
| +} |
| + |
| +TEST_F(GeolocationFeatureTest, UnexpectedMessageReceived) { |
| + GeolocationMessage* geolocation_message; |
| + std::unique_ptr<BlimpMessage> message = |
| + CreateBlimpMessage(&geolocation_message); |
| + |
| + GeolocationCoordinatesMessage* coordinates_message = |
| + geolocation_message->mutable_coordinates(); |
| + coordinates_message->set_latitude(1.0); |
| + |
| + net::TestCompletionCallback cb; |
| + feature_->ProcessMessage(std::move(message), cb.callback()); |
| + |
| + EXPECT_EQ(net::ERR_UNEXPECTED, cb.WaitForResult()); |
| +} |
| + |
| +TEST_F(GeolocationFeatureTest, RequestRefreshReceived) { |
| + EXPECT_CALL(*location_provider_, RequestRefresh()); |
| + |
| + GeolocationMessage* geolocation_message; |
| + std::unique_ptr<BlimpMessage> message = |
| + CreateBlimpMessage(&geolocation_message); |
| + geolocation_message->mutable_request_refresh(); |
| + |
| + net::TestCompletionCallback cb; |
| + feature_->ProcessMessage(std::move(message), cb.callback()); |
| + EXPECT_EQ(net::OK, cb.WaitForResult()); |
| +} |
| + |
| +TEST_F(GeolocationFeatureTest, LocationUpdateSendsCorrectMessage) { |
| + EXPECT_CALL(*out_processor_, MockableProcessMessage(EqualsGeoposition(), _)); |
| + callback_.Run(location_provider_, position_); |
| +} |
| + |
| +TEST_F(GeolocationFeatureTest, ErrorUpdateSendsCorrectMessage) { |
| + ON_CALL(*out_processor_, MockableProcessMessage(_, _)) |
|
Kevin M
2016/08/15 20:35:40
Use EXPECT_CALL with a specific Times()
CJ
2016/08/15 21:59:32
Are you saying to make this ON_CALL and the next 3
Kevin M
2016/08/17 00:55:01
I'm saying don't use ON_CALL; instead, be precise
CJ
2016/08/18 00:58:49
I'm using ON_CALL to specify the behavior of the n
|
| + .WillByDefault(Invoke( |
| + this, &GeolocationFeatureTest_ErrorUpdateSendsCorrectMessage_Test:: |
| + RunCallback)); |
| + EXPECT_CALL( |
| + *out_processor_, |
| + MockableProcessMessage( |
| + EqualsError(GeolocationErrorMessage::POSITION_UNAVAILABLE), _)); |
| + EXPECT_CALL(*out_processor_, |
| + MockableProcessMessage( |
| + EqualsError(GeolocationErrorMessage::PERMISSION_DENIED), _)); |
| + EXPECT_CALL( |
| + *out_processor_, |
| + MockableProcessMessage(EqualsError(GeolocationErrorMessage::TIMEOUT), _)); |
| + |
| + device::Geoposition err_position; |
| + err_position.error_code = |
| + device::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE; |
| + callback_.Run(location_provider_, err_position); |
| + |
| + err_position.error_code = |
| + device::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED; |
| + callback_.Run(location_provider_, err_position); |
| + |
| + err_position.error_code = device::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT; |
| + callback_.Run(location_provider_, err_position); |
| +} |
| + |
| +TEST_F(GeolocationFeatureTest, NoRepeatSendsWithMessagePending) { |
|
Kevin M
2016/08/15 20:35:40
Also verify that we can send messages again after
CJ
2016/08/15 21:59:32
I can't think of a way to do that without a delay
Kevin M
2016/08/17 00:55:02
Right, we don't like delays in our tests, but ther
CJ
2016/08/18 00:58:49
Done.
|
| + EXPECT_CALL(*out_processor_, MockableProcessMessage(EqualsGeoposition(), _)) |
| + .Times(1); |
| + callback_.Run(location_provider_, position_); |
| + callback_.Run(location_provider_, position_); |
| + callback_.Run(location_provider_, position_); |
| +} |
| + |
| +TEST_F(GeolocationFeatureTest, ProcessMessageHandlesNullCallback) { |
| + EXPECT_CALL(*location_provider_, RequestRefresh()); |
| + |
| + GeolocationMessage* geolocation_message; |
| + std::unique_ptr<BlimpMessage> message = |
| + CreateBlimpMessage(&geolocation_message); |
| + geolocation_message->mutable_request_refresh(); |
| + |
| + feature_->ProcessMessage(std::move(message), net::CompletionCallback()); |
| +} |
| + |
| +} // namespace client |
| +} // namespace blimp |