Chromium Code Reviews| Index: blimp/engine/feature/geolocation/engine_geolocation_feature_unittest.cc |
| diff --git a/blimp/engine/feature/geolocation/engine_geolocation_feature_unittest.cc b/blimp/engine/feature/geolocation/engine_geolocation_feature_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e50ac8f79088bdda2077acc48533adb06f62a06b |
| --- /dev/null |
| +++ b/blimp/engine/feature/geolocation/engine_geolocation_feature_unittest.cc |
| @@ -0,0 +1,161 @@ |
| +// 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/engine/feature/geolocation/engine_geolocation_feature.h" |
| + |
| +#include <string> |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "blimp/common/create_blimp_message.h" |
| +#include "blimp/common/proto/blimp_message.pb.h" |
| +#include "blimp/common/proto/geolocation.pb.h" |
| +#include "blimp/net/test_common.h" |
| +#include "content/public/browser/location_provider.h" |
| +#include "content/public/common/geoposition.h" |
| +#include "net/base/test_completion_callback.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +using testing::_; |
| + |
| +namespace blimp { |
| +namespace engine { |
| + |
| +void SendMockLocationMessage(BlimpMessageProcessor* processor) { |
| + GeolocationMessage* geolocation_message; |
| + std::unique_ptr<BlimpMessage> message = |
| + CreateBlimpMessage(&geolocation_message); |
| + |
| + GeolocationCoordinatesMessage* coordinates_message = |
| + geolocation_message->mutable_coordinates(); |
| + coordinates_message->set_latitude(-42.0); |
| + coordinates_message->set_longitude(17.3); |
| + coordinates_message->set_altitude(123.4); |
| + coordinates_message->set_accuracy(73.7); |
| + |
| + net::TestCompletionCallback cb; |
| + processor->ProcessMessage(std::move(message), cb.callback()); |
| + EXPECT_EQ(net::OK, cb.WaitForResult()); |
| +} |
| + |
| +void SendMockErrorMessage(BlimpMessageProcessor* processor, |
| + GeolocationErrorMessage::ErrorCode error, |
| + std::string error_message) { |
| + GeolocationMessage* geolocation_message; |
| + std::unique_ptr<BlimpMessage> message = |
| + CreateBlimpMessage(&geolocation_message); |
| + GeolocationErrorMessage* geolocation_error_message = |
| + geolocation_message->mutable_error(); |
| + geolocation_error_message->set_error_code(error); |
| + geolocation_error_message->set_error_message(error_message); |
| + |
| + net::TestCompletionCallback cb; |
| + processor->ProcessMessage(std::move(message), cb.callback()); |
| + EXPECT_EQ(net::OK, cb.WaitForResult()); |
| +} |
| + |
| +MATCHER_P(EqualsUpdatedListenState, accuracy, "") { |
|
Wez
2016/07/15 01:46:17
Looks like some of the naming in the tests need up
CJ
2016/07/18 21:11:56
Done.
|
| + return arg.feature_case() == BlimpMessage::kGeolocation && |
| + arg.geolocation().type_case() == |
| + GeolocationMessage::kSetInterestLevel && |
| + arg.geolocation().set_interest_level().level() == accuracy; |
| +} |
| + |
| +MATCHER(EqualsStoppedListenState, "") { |
| + return arg.feature_case() == BlimpMessage::kGeolocation && |
| + arg.geolocation().type_case() == |
| + GeolocationMessage::kSetInterestLevel && |
| + arg.geolocation().set_interest_level().level() == |
| + GeolocationSetInterestLevelMessage::NO_INTEREST; |
| +} |
| + |
| +MATCHER(EqualsRequestRefresh, "") { |
| + return arg.feature_case() == BlimpMessage::kGeolocation && |
| + arg.geolocation().type_case() == GeolocationMessage::kRequestRefresh; |
| +} |
| + |
| +class EngineGeolocationFeatureTest : public testing::Test { |
| + public: |
| + EngineGeolocationFeatureTest() |
| + : out_processor_(nullptr), |
| + mock_callback_( |
| + base::Bind(&EngineGeolocationFeatureTest::OnLocationUpdate, |
| + base::Unretained(this))) {} |
| + |
| + void SetUp() override { |
| + out_processor_ = new MockBlimpMessageProcessor(); |
| + feature_.set_outgoing_message_processor(base::WrapUnique(out_processor_)); |
| + feature_.SetUpdateCallback(mock_callback_); |
| + } |
| + |
| + void OnLocationUpdate(const content::Geoposition& geoposition) { |
| + received_position = geoposition; |
| + } |
| + |
| + protected: |
| + // This is a raw pointer to a class that is owned by the NavigationFeature. |
| + MockBlimpMessageProcessor* out_processor_; |
| + |
| + EngineGeolocationFeature feature_; |
| + base::Callback<void(const content::Geoposition&)> mock_callback_; |
| + content::Geoposition received_position; |
| +}; |
| + |
| +TEST_F(EngineGeolocationFeatureTest, TestLocationReceived) { |
|
Wez
2016/07/15 01:46:18
nit: Don't prefix your test names with Test - the
CJ
2016/07/18 21:11:56
Done.
|
| + SendMockLocationMessage(&feature_); |
| + EXPECT_EQ(content::Geoposition::ERROR_CODE_NONE, |
| + received_position.error_code); |
| + EXPECT_EQ(-42.0, received_position.latitude); |
| + EXPECT_EQ(17.3, received_position.longitude); |
| + EXPECT_EQ(123.4, received_position.altitude); |
| + EXPECT_EQ(73.7, received_position.accuracy); |
| +} |
| + |
| +TEST_F(EngineGeolocationFeatureTest, TestErrorRecieved) { |
| + SendMockErrorMessage(&feature_, GeolocationErrorMessage::PERMISSION_DENIED, |
| + "PERMISSION_DENIED"); |
| + EXPECT_EQ(content::Geoposition::ERROR_CODE_PERMISSION_DENIED, |
| + received_position.error_code); |
| + EXPECT_EQ("PERMISSION_DENIED", received_position.error_message); |
| + |
| + SendMockErrorMessage(&feature_, GeolocationErrorMessage::POSITION_UNAVAILABLE, |
| + "POSITION_UNAVAILABLE"); |
| + EXPECT_EQ(content::Geoposition::ERROR_CODE_POSITION_UNAVAILABLE, |
| + received_position.error_code); |
| + EXPECT_EQ("POSITION_UNAVAILABLE", received_position.error_message); |
| +} |
| + |
| +TEST_F(EngineGeolocationFeatureTest, TestSendUpdateListenStateMessage) { |
| + EXPECT_CALL(*out_processor_, |
| + MockableProcessMessage( |
| + EqualsUpdatedListenState( |
| + GeolocationSetInterestLevelMessage::HIGH_ACCURACY), |
| + _)) |
| + .Times(1); |
| + EXPECT_CALL(*out_processor_, |
| + MockableProcessMessage( |
| + EqualsUpdatedListenState( |
| + GeolocationSetInterestLevelMessage::LOW_ACCURACY), |
| + _)) |
| + .Times(1); |
| + feature_.RequestAccuracy(GeolocationSetInterestLevelMessage::HIGH_ACCURACY); |
| + feature_.RequestAccuracy(GeolocationSetInterestLevelMessage::LOW_ACCURACY); |
| +} |
| + |
| +TEST_F(EngineGeolocationFeatureTest, TestSendStopUpdateListenStateMessage) { |
| + EXPECT_CALL(*out_processor_, |
| + MockableProcessMessage(EqualsStoppedListenState(), _)) |
| + .Times(1); |
| + feature_.RequestAccuracy(GeolocationSetInterestLevelMessage::NO_INTEREST); |
|
Wez
2016/07/15 01:46:18
You could fold this into the preceding test.
CJ
2016/07/18 21:11:56
Done.
|
| +} |
| + |
| +TEST_F(EngineGeolocationFeatureTest, TestSendRequestRefreshMessage) { |
|
Wez
2016/07/15 01:46:18
How about a test for EngineGeolocationFeature seei
CJ
2016/07/18 21:11:56
I ended up just trying one of the unexpected Geolo
Wez
2016/07/18 23:55:21
Acknowledged.
|
| + EXPECT_CALL(*out_processor_, |
| + MockableProcessMessage(EqualsRequestRefresh(), _)) |
| + .Times(1); |
| + feature_.RequestRefresh(); |
| +} |
| + |
| +} // namespace engine |
| +} // namespace blimp |