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..264b3d2438efac51c43dccf4abbcf6a1bc9d2daa |
| --- /dev/null |
| +++ b/blimp/client/feature/geolocation_feature_unittest.cc |
| @@ -0,0 +1,174 @@ |
| +// 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/geoposition.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::InSequence; |
| +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<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>(); |
| + out_processor_ = out_processor.get(); |
| + feature_->set_outgoing_message_processor(std::move(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 owned by the |
| + // GeolocationFeature. |
| + MockBlimpMessageProcessor* out_processor_; |
| + StrictMock<MockLocationProvider>* location_provider_; |
| + |
| + std::unique_ptr<GeolocationFeature> feature_; |
| + device::LocationProvider::LocationProviderUpdateCallback callback_; |
| + |
| + 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; |
| + EXPECT_DFATAL(feature_->ProcessMessage(std::move(message), cb.callback()), |
| + "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) { |
|
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.
|
| + 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 |