Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2749)

Unified Diff: blimp/engine/feature/geolocation/engine_geolocation_feature_unittest.cc

Issue 2091023006: Adds EngineGeolocationFeature for Blimp Geolocation project. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: In response to Wez's #23 comment Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..8977ea3de639077832f9857a4feea2c69e4a1ad1
--- /dev/null
+++ b/blimp/engine/feature/geolocation/engine_geolocation_feature_unittest.cc
@@ -0,0 +1,166 @@
+// 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);
+
+ GeolocationPositionMessage* location_message =
+ geolocation_message->mutable_location();
+
+ GeolocationCoordinates* coordinates = location_message->mutable_coordinates();
+ coordinates->set_latitude(-42.0);
+ coordinates->set_longitude(17.3);
+ coordinates->set_altitude(123.4);
+ coordinates->set_accuracy(73.7);
+
+ location_message->set_timestamp_millis(87);
+
+ 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, "") {
+ return arg.feature_case() == BlimpMessage::kGeolocation &&
+ arg.geolocation().type_case() ==
+ GeolocationMessage::kUpdateListenState &&
+ arg.geolocation().update_listen_state().listen_state() == accuracy;
+}
+
+MATCHER(EqualsStoppedListenState, "") {
+ return arg.feature_case() == BlimpMessage::kGeolocation &&
+ arg.geolocation().type_case() ==
+ GeolocationMessage::kUpdateListenState &&
+ arg.geolocation().update_listen_state().listen_state() ==
+ GeolocationInterestMessage::REQUEST_NONE;
+}
+
+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) {
+ 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);
+ EXPECT_EQ(base::Time::FromJsTime(87), received_position.timestamp);
+}
+
+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(GeolocationInterestMessage::ACCURACY_HIGH),
+ _))
+ .Times(1);
+ EXPECT_CALL(
+ *out_processor_,
+ MockableProcessMessage(
+ EqualsUpdatedListenState(GeolocationInterestMessage::ACCURACY_LOW),
+ _))
+ .Times(1);
+ feature_.StartProvider(true);
+ feature_.StartProvider(false);
+}
+
+TEST_F(EngineGeolocationFeatureTest, TestSendStopUpdateListenStateMessage) {
+ EXPECT_CALL(*out_processor_,
+ MockableProcessMessage(EqualsStoppedListenState(), _))
+ .Times(1);
+ feature_.StopProvider();
+}
+
+TEST_F(EngineGeolocationFeatureTest, TestSendRequestRefreshMessage) {
+ EXPECT_CALL(*out_processor_,
+ MockableProcessMessage(EqualsRequestRefresh(), _))
+ .Times(1);
+ feature_.RequestRefresh();
+}
+
+} // namespace engine
+} // namespace blimp

Powered by Google App Engine
This is Rietveld 408576698