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

Unified Diff: blimp/client/feature/geolocation_feature_unittest.cc

Issue 2161223003: Adds GeolocationFeature for Blimp Geolocation project. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@engine_feature_prep
Patch Set: Addresses Wez's #12 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/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..28b7bee93d84d0cc0c9c2a2b9776375c1c7e6822
--- /dev/null
+++ b/blimp/client/feature/geolocation_feature_unittest.cc
@@ -0,0 +1,176 @@
+// 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 "content/public/browser/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::_;
+
+namespace blimp {
+namespace client {
+
+void SendMockSetInterestLevelMessage(
Kevin M 2016/07/28 21:32:43 If you make this a protected member of the test cl
CJ 2016/08/01 19:21:16 Done.
+ 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) {
Kevin M 2016/07/28 21:32:43 This only has one caller; no need to have a helper
CJ 2016/08/01 19:21:15 Done.
+ 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 &&
Kevin M 2016/07/28 21:32:42 Move these into kConstants.
CJ 2016/08/01 19:21:15 Done.
+ 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),
+ location_provider_(new MockLocationProvider()),
+ feature_(base::WrapUnique(new GeolocationFeature(
+ base::WrapUnique(location_provider_)))) {}
+
+ void SetUp() override {
+ out_processor_ = new MockBlimpMessageProcessor();
+ feature_->set_outgoing_message_processor(base::WrapUnique(out_processor_));
+ }
+
+ protected:
+ // These are a raw pointers to classes that are
Kevin M 2016/07/28 21:32:43 "these are raw pointers". Remove newline below ou
CJ 2016/08/01 19:21:15 Done.
+ // owned by the GeolocationFeature.
+ MockBlimpMessageProcessor* out_processor_;
+
+ MockLocationProvider* location_provider_;
+ std::unique_ptr<GeolocationFeature> feature_;
Kevin M 2016/07/28 21:32:42 Does this need to be heap-allocated? Can't it just
Kevin M 2016/07/28 21:32:43 DISALLOW_COPY_AND_ASSIGN
Kevin M 2016/07/28 21:32:43 Add newline above
CJ 2016/08/01 19:21:16 Done.
CJ 2016/08/01 19:21:16 Done.
CJ 2016/08/01 19:21:16 Done.
+};
+
+TEST_F(GeolocationFeatureTest, UpdateInterestLevelReceived) {
+ EXPECT_CALL(*location_provider_, StartProvider(true)).Times(1);
+ EXPECT_CALL(*location_provider_, StopProvider()).Times(1);
+ EXPECT_CALL(*location_provider_, StartProvider(false)).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(*location_provider_, StartProvider(_)).Times(0);
Kevin M 2016/07/28 21:32:43 Use StrictMock, then you don't have to specify Tim
CJ 2016/08/01 19:21:16 Done.
+ EXPECT_CALL(*location_provider_, StopProvider()).Times(0);
+ EXPECT_CALL(*location_provider_, RequestRefresh()).Times(0);
+ SendMockUnexpectedMessage(feature_.get());
+}
+
+TEST_F(GeolocationFeatureTest, RequestRefreshReceived) {
+ EXPECT_CALL(*location_provider_, RequestRefresh()).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;
+ location_provider_->callback_.Run(location_provider_, 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;
Kevin M 2016/07/28 21:32:42 add newline above, remove newline below
CJ 2016/08/01 19:21:16 Done.
+
+ position.error_code =
+ content::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE;
+ position.error_message = "Position unavailable";
Kevin M 2016/07/28 21:32:43 IMO checking for error message equality is too bri
CJ 2016/08/01 19:21:16 As for the DLOG FATAL, do we just want to make sur
+ location_provider_->callback_.Run(location_provider_, position);
+
+ position.error_code =
+ content::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED;
+ position.error_message = "Permission denied";
+ location_provider_->callback_.Run(location_provider_, position);
+
+ position.error_code = content::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT;
+ position.error_message = "Timeout";
+ location_provider_->callback_.Run(location_provider_, position);
+}
+
+} // namespace client
+} // namespace blimp

Powered by Google App Engine
This is Rietveld 408576698