| 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..458b369613da74d939bb42a9b0f5b09d34354d15
|
| --- /dev/null
|
| +++ b/blimp/client/feature/geolocation_feature_unittest.cc
|
| @@ -0,0 +1,180 @@
|
| +// 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_geolocation_feature_delegate.h"
|
| +#include "blimp/common/create_blimp_message.h"
|
| +#include "blimp/common/proto/blimp_message.pb.h"
|
| +#include "blimp/net/test_common.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +#include "net/base/net_errors.h"
|
| +#include "net/base/test_completion_callback.h"
|
| +#include "net/test/gtest_util.h"
|
| +
|
| +using testing::_;
|
| +
|
| +namespace blimp {
|
| +namespace client {
|
| +
|
| +void SendMockSetInterestLevelMessage(
|
| + BlimpMessageProcessor* processor,
|
| + 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;
|
| + processor->ProcessMessage(std::move(message), cb.callback());
|
| + EXPECT_EQ(net::OK, cb.WaitForResult());
|
| +}
|
| +
|
| +void SendMockUnexpectedMessage(BlimpMessageProcessor* processor) {
|
| + 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;
|
| + processor->ProcessMessage(std::move(message), cb.callback());
|
| + EXPECT_EQ(net::ERR_UNEXPECTED, cb.WaitForResult());
|
| +}
|
| +
|
| +void SendMockRequestRefreshMessage(BlimpMessageProcessor* processor) {
|
| + GeolocationMessage* geolocation_message;
|
| + std::unique_ptr<BlimpMessage> message =
|
| + CreateBlimpMessage(&geolocation_message);
|
| + geolocation_message->mutable_request_refresh();
|
| +
|
| + net::TestCompletionCallback cb;
|
| + processor->ProcessMessage(std::move(message), cb.callback());
|
| + EXPECT_EQ(net::OK, cb.WaitForResult());
|
| +}
|
| +
|
| +MATCHER(EqualsGeoposition, "") {
|
| + return arg.feature_case() == BlimpMessage::kGeolocation &&
|
| + arg.geolocation().type_case() == GeolocationMessage::kCoordinates &&
|
| + arg.geolocation().coordinates().latitude() == -42.0 &&
|
| + arg.geolocation().coordinates().longitude() == 17.3 &&
|
| + arg.geolocation().coordinates().altitude() == 123.4 &&
|
| + arg.geolocation().coordinates().accuracy() == 73.7;
|
| +}
|
| +
|
| +MATCHER_P2(EqualsError, error_code, message, "") {
|
| + return arg.feature_case() == BlimpMessage::kGeolocation &&
|
| + arg.geolocation().type_case() == GeolocationMessage::kError &&
|
| + arg.geolocation().error().error_code() == error_code &&
|
| + arg.geolocation().error().error_message() == message;
|
| +}
|
| +
|
| +class GeolocationFeatureTest : public testing::Test {
|
| + public:
|
| + GeolocationFeatureTest()
|
| + : out_processor_(nullptr),
|
| + delegate_(base::WrapUnique(new MockGeolocationFeatureDelegate())),
|
| + feature_(new GeolocationFeature()) {}
|
| +
|
| + void SetUp() override {
|
| + out_processor_ = new MockBlimpMessageProcessor();
|
| + feature_->set_outgoing_message_processor(base::WrapUnique(out_processor_));
|
| + feature_->SetDelegate(delegate_.get());
|
| + }
|
| +
|
| + protected:
|
| + // This is a raw pointer to a class that is owned by the GeolocationFeature.
|
| + MockBlimpMessageProcessor* out_processor_;
|
| +
|
| + std::unique_ptr<MockGeolocationFeatureDelegate> delegate_;
|
| + std::unique_ptr<GeolocationFeature> feature_;
|
| +};
|
| +
|
| +TEST_F(GeolocationFeatureTest, UpdateInterestLevelReceived) {
|
| + EXPECT_CALL(*delegate_,
|
| + OnGeolocationInterestUpdate(
|
| + GeolocationSetInterestLevelMessage::HIGH_ACCURACY))
|
| + .Times(1);
|
| + EXPECT_CALL(*delegate_, OnGeolocationInterestUpdate(
|
| + GeolocationSetInterestLevelMessage::NO_INTEREST))
|
| + .Times(1);
|
| + EXPECT_CALL(*delegate_, OnGeolocationInterestUpdate(
|
| + GeolocationSetInterestLevelMessage::LOW_ACCURACY))
|
| + .Times(1);
|
| +
|
| + SendMockSetInterestLevelMessage(
|
| + feature_.get(), GeolocationSetInterestLevelMessage::HIGH_ACCURACY);
|
| + SendMockSetInterestLevelMessage(
|
| + feature_.get(), GeolocationSetInterestLevelMessage::NO_INTEREST);
|
| + SendMockSetInterestLevelMessage(
|
| + feature_.get(), GeolocationSetInterestLevelMessage::LOW_ACCURACY);
|
| +}
|
| +
|
| +TEST_F(GeolocationFeatureTest, UnexpectedMessageReceived) {
|
| + EXPECT_CALL(*delegate_, OnGeolocationInterestUpdate(_)).Times(0);
|
| + EXPECT_CALL(*delegate_, OnRequestRefresh()).Times(0);
|
| + SendMockUnexpectedMessage(feature_.get());
|
| +}
|
| +
|
| +TEST_F(GeolocationFeatureTest, RequestRefreshReceived) {
|
| + EXPECT_CALL(*delegate_, OnRequestRefresh()).Times(1);
|
| + SendMockRequestRefreshMessage(feature_.get());
|
| +}
|
| +
|
| +TEST_F(GeolocationFeatureTest, LocationUpdateSendsCorrectMessage) {
|
| + EXPECT_CALL(*out_processor_, MockableProcessMessage(EqualsGeoposition(), _))
|
| + .Times(1);
|
| + content::Geoposition position;
|
| + position.latitude = -42.0;
|
| + position.longitude = 17.3;
|
| + position.altitude = 123.4;
|
| + position.accuracy = 73.7;
|
| + feature_->OnLocationUpdate(position);
|
| +}
|
| +
|
| +TEST_F(GeolocationFeatureTest, ErrorUpdateSendsCorrectMessage) {
|
| + EXPECT_CALL(*out_processor_,
|
| + MockableProcessMessage(
|
| + EqualsError(GeolocationErrorMessage::POSITION_UNAVAILABLE,
|
| + "Position unavailable"),
|
| + _))
|
| + .Times(1);
|
| + EXPECT_CALL(*out_processor_,
|
| + MockableProcessMessage(
|
| + EqualsError(GeolocationErrorMessage::PERMISSION_DENIED,
|
| + "Permission denied"),
|
| + _))
|
| + .Times(1);
|
| + EXPECT_CALL(*out_processor_,
|
| + MockableProcessMessage(
|
| + EqualsError(GeolocationErrorMessage::TIMEOUT, "Timeout"), _))
|
| + .Times(1);
|
| + content::Geoposition position;
|
| +
|
| + position.error_code =
|
| + content::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE;
|
| + position.error_message = "Position unavailable";
|
| + feature_->OnLocationUpdate(position);
|
| +
|
| + position.error_code =
|
| + content::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED;
|
| + position.error_message = "Permission denied";
|
| + feature_->OnLocationUpdate(position);
|
| +
|
| + position.error_code = content::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT;
|
| + position.error_message = "Timeout";
|
| + feature_->OnLocationUpdate(position);
|
| +}
|
| +
|
| +} // namespace client
|
| +} // namespace blimp
|
|
|