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..7622503150bf0fe541523df594289f5a9f2d65ce |
| --- /dev/null |
| +++ b/blimp/client/feature/geolocation_feature_unittest.cc |
| @@ -0,0 +1,173 @@ |
| +// 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/client/feature/mock_location_provider.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/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::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() |
| + : out_processor_(nullptr), |
| + location_provider_(nullptr) {} |
| + |
| + void SetUp() override { |
| + 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.
|
| + EXPECT_CALL(*location_provider_, SetUpdateCallback(_)) |
| + .WillOnce(SaveArg<0>(&callback_)); |
| + 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?
|
| + |
| + out_processor_ = new MockBlimpMessageProcessor(); |
| + feature_->set_outgoing_message_processor(base::WrapUnique(out_processor_)); |
| + } |
| + |
| + void TearDown() override { |
| + 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.
|
| + delete out_processor_; |
| + } |
| + |
| + 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()); |
| + } |
| + |
| + // 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.
|
| + // owned by the GeolocationFeature. |
| + MockBlimpMessageProcessor* out_processor_; |
| + StrictMock<MockLocationProvider>* location_provider_; |
| + |
| + GeolocationFeature* feature_; |
| + device::LocationProvider::LocationProviderUpdateCallback callback_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(GeolocationFeatureTest); |
| +}; |
| + |
| +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.
|
| + 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; |
| + 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.
|
| + "Unsupported message type."); |
| +} |
| + |
| +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(), _)); |
| + device::Geoposition position; |
| + position.latitude = kLatitude; |
| + position.longitude = kLongitude; |
| + position.altitude = kAltitude; |
| + position.accuracy = kAccuracy; |
| + callback_.Run(location_provider_, position); |
| +} |
| + |
| +TEST_F(GeolocationFeatureTest, ErrorUpdateSendsCorrectMessage) { |
| + 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 position; |
| + position.error_code = |
| + device::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE; |
| + callback_.Run(location_provider_, position); |
| + |
| + position.error_code = |
| + device::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED; |
| + callback_.Run(location_provider_, position); |
| + |
| + position.error_code = device::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT; |
| + callback_.Run(location_provider_, position); |
| +} |
| + |
| +} // namespace client |
| +} // namespace blimp |