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..f5a20626f26cea824c7e51bee9b57aa50ccd8b4f |
--- /dev/null |
+++ b/blimp/client/feature/geolocation_feature_unittest.cc |
@@ -0,0 +1,206 @@ |
+// 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::Invoke; |
+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)); |
+ } |
+ |
+ void RunCallback(const BlimpMessage& blimp_message, |
+ const net::CompletionCallback& callback) { |
+ callback.Run(net::OK); |
+ } |
+ |
+ protected: |
Kevin M
2016/08/09 22:08:55
Please revisit visibility. Not quite clear why som
CJ
2016/08/10 00:13:49
Did it, but don't we usually like things to be mor
Kevin M
2016/08/10 23:23:34
In general, the visibility should make good semant
CJ
2016/08/12 21:57:46
Done.
|
+ 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()), |
+ "Invalid 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) { |
+ ON_CALL(*out_processor_, MockableProcessMessage(_, _)) |
+ .WillByDefault(Invoke(this, &GeolocationFeatureTest::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 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); |
+} |
+ |
+TEST_F(GeolocationFeatureTest, NoRepeatSendsWithMessagePending) { |
+ EXPECT_CALL(*out_processor_, MockableProcessMessage(EqualsGeoposition(), _)) |
+ .Times(1); |
+ device::Geoposition position; |
+ position.latitude = kLatitude; |
+ position.longitude = kLongitude; |
+ position.altitude = kAltitude; |
+ position.accuracy = kAccuracy; |
+ 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 |