OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "blimp/client/feature/geolocation_feature.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/memory/ptr_util.h" |
| 11 #include "blimp/client/feature/mock_geolocation_feature_delegate.h" |
| 12 #include "blimp/common/create_blimp_message.h" |
| 13 #include "blimp/common/proto/blimp_message.pb.h" |
| 14 #include "blimp/net/test_common.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "net/base/net_errors.h" |
| 17 #include "net/base/test_completion_callback.h" |
| 18 #include "net/test/gtest_util.h" |
| 19 |
| 20 using testing::_; |
| 21 |
| 22 namespace blimp { |
| 23 namespace client { |
| 24 |
| 25 void SendMockSetInterestLevelMessage( |
| 26 BlimpMessageProcessor* processor, |
| 27 GeolocationSetInterestLevelMessage::Level level) { |
| 28 GeolocationMessage* geolocation_message; |
| 29 std::unique_ptr<BlimpMessage> message = |
| 30 CreateBlimpMessage(&geolocation_message); |
| 31 |
| 32 GeolocationSetInterestLevelMessage* interest_message = |
| 33 geolocation_message->mutable_set_interest_level(); |
| 34 interest_message->set_level(level); |
| 35 |
| 36 net::TestCompletionCallback cb; |
| 37 processor->ProcessMessage(std::move(message), cb.callback()); |
| 38 EXPECT_EQ(net::OK, cb.WaitForResult()); |
| 39 } |
| 40 |
| 41 void SendMockUnexpectedMessage(BlimpMessageProcessor* processor) { |
| 42 GeolocationMessage* geolocation_message; |
| 43 std::unique_ptr<BlimpMessage> message = |
| 44 CreateBlimpMessage(&geolocation_message); |
| 45 |
| 46 GeolocationCoordinatesMessage* coordinates_message = |
| 47 geolocation_message->mutable_coordinates(); |
| 48 coordinates_message->set_latitude(1.0); |
| 49 |
| 50 net::TestCompletionCallback cb; |
| 51 processor->ProcessMessage(std::move(message), cb.callback()); |
| 52 EXPECT_EQ(net::ERR_UNEXPECTED, cb.WaitForResult()); |
| 53 } |
| 54 |
| 55 void SendMockRequestRefreshMessage(BlimpMessageProcessor* processor) { |
| 56 GeolocationMessage* geolocation_message; |
| 57 std::unique_ptr<BlimpMessage> message = |
| 58 CreateBlimpMessage(&geolocation_message); |
| 59 geolocation_message->mutable_request_refresh(); |
| 60 |
| 61 net::TestCompletionCallback cb; |
| 62 processor->ProcessMessage(std::move(message), cb.callback()); |
| 63 EXPECT_EQ(net::OK, cb.WaitForResult()); |
| 64 } |
| 65 |
| 66 MATCHER(EqualsGeoposition, "") { |
| 67 return arg.feature_case() == BlimpMessage::kGeolocation && |
| 68 arg.geolocation().type_case() == GeolocationMessage::kCoordinates && |
| 69 arg.geolocation().coordinates().latitude() == -42.0 && |
| 70 arg.geolocation().coordinates().longitude() == 17.3 && |
| 71 arg.geolocation().coordinates().altitude() == 123.4 && |
| 72 arg.geolocation().coordinates().accuracy() == 73.7; |
| 73 } |
| 74 |
| 75 MATCHER_P2(EqualsError, error_code, message, "") { |
| 76 return arg.feature_case() == BlimpMessage::kGeolocation && |
| 77 arg.geolocation().type_case() == GeolocationMessage::kError && |
| 78 arg.geolocation().error().error_code() == error_code && |
| 79 arg.geolocation().error().error_message() == message; |
| 80 } |
| 81 |
| 82 class GeolocationFeatureTest : public testing::Test { |
| 83 public: |
| 84 GeolocationFeatureTest() |
| 85 : out_processor_(nullptr), |
| 86 delegate_(base::WrapUnique(new MockGeolocationFeatureDelegate())), |
| 87 feature_(new GeolocationFeature()) {} |
| 88 |
| 89 void SetUp() override { |
| 90 out_processor_ = new MockBlimpMessageProcessor(); |
| 91 feature_->set_outgoing_message_processor(base::WrapUnique(out_processor_)); |
| 92 feature_->SetDelegate(delegate_.get()); |
| 93 } |
| 94 |
| 95 protected: |
| 96 // This is a raw pointer to a class that is owned by the GeolocationFeature. |
| 97 MockBlimpMessageProcessor* out_processor_; |
| 98 |
| 99 std::unique_ptr<MockGeolocationFeatureDelegate> delegate_; |
| 100 std::unique_ptr<GeolocationFeature> feature_; |
| 101 }; |
| 102 |
| 103 TEST_F(GeolocationFeatureTest, UpdateInterestLevelReceived) { |
| 104 EXPECT_CALL(*delegate_, |
| 105 OnGeolocationInterestUpdate( |
| 106 GeolocationSetInterestLevelMessage::HIGH_ACCURACY)) |
| 107 .Times(1); |
| 108 EXPECT_CALL(*delegate_, OnGeolocationInterestUpdate( |
| 109 GeolocationSetInterestLevelMessage::NO_INTEREST)) |
| 110 .Times(1); |
| 111 EXPECT_CALL(*delegate_, OnGeolocationInterestUpdate( |
| 112 GeolocationSetInterestLevelMessage::LOW_ACCURACY)) |
| 113 .Times(1); |
| 114 |
| 115 SendMockSetInterestLevelMessage( |
| 116 feature_.get(), GeolocationSetInterestLevelMessage::HIGH_ACCURACY); |
| 117 SendMockSetInterestLevelMessage( |
| 118 feature_.get(), GeolocationSetInterestLevelMessage::NO_INTEREST); |
| 119 SendMockSetInterestLevelMessage( |
| 120 feature_.get(), GeolocationSetInterestLevelMessage::LOW_ACCURACY); |
| 121 } |
| 122 |
| 123 TEST_F(GeolocationFeatureTest, UnexpectedMessageReceived) { |
| 124 EXPECT_CALL(*delegate_, OnGeolocationInterestUpdate(_)).Times(0); |
| 125 EXPECT_CALL(*delegate_, OnRequestRefresh()).Times(0); |
| 126 SendMockUnexpectedMessage(feature_.get()); |
| 127 } |
| 128 |
| 129 TEST_F(GeolocationFeatureTest, RequestRefreshReceived) { |
| 130 EXPECT_CALL(*delegate_, OnRequestRefresh()).Times(1); |
| 131 SendMockRequestRefreshMessage(feature_.get()); |
| 132 } |
| 133 |
| 134 TEST_F(GeolocationFeatureTest, LocationUpdateSendsCorrectMessage) { |
| 135 EXPECT_CALL(*out_processor_, MockableProcessMessage(EqualsGeoposition(), _)) |
| 136 .Times(1); |
| 137 content::Geoposition position; |
| 138 position.latitude = -42.0; |
| 139 position.longitude = 17.3; |
| 140 position.altitude = 123.4; |
| 141 position.accuracy = 73.7; |
| 142 feature_->OnLocationUpdate(position); |
| 143 } |
| 144 |
| 145 TEST_F(GeolocationFeatureTest, ErrorUpdateSendsCorrectMessage) { |
| 146 EXPECT_CALL(*out_processor_, |
| 147 MockableProcessMessage( |
| 148 EqualsError(GeolocationErrorMessage::POSITION_UNAVAILABLE, |
| 149 "Position unavailable"), |
| 150 _)) |
| 151 .Times(1); |
| 152 EXPECT_CALL(*out_processor_, |
| 153 MockableProcessMessage( |
| 154 EqualsError(GeolocationErrorMessage::PERMISSION_DENIED, |
| 155 "Permission denied"), |
| 156 _)) |
| 157 .Times(1); |
| 158 EXPECT_CALL(*out_processor_, |
| 159 MockableProcessMessage( |
| 160 EqualsError(GeolocationErrorMessage::TIMEOUT, "Timeout"), _)) |
| 161 .Times(1); |
| 162 content::Geoposition position; |
| 163 |
| 164 position.error_code = |
| 165 content::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE; |
| 166 position.error_message = "Position unavailable"; |
| 167 feature_->OnLocationUpdate(position); |
| 168 |
| 169 position.error_code = |
| 170 content::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED; |
| 171 position.error_message = "Permission denied"; |
| 172 feature_->OnLocationUpdate(position); |
| 173 |
| 174 position.error_code = content::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT; |
| 175 position.error_message = "Timeout"; |
| 176 feature_->OnLocationUpdate(position); |
| 177 } |
| 178 |
| 179 } // namespace client |
| 180 } // namespace blimp |
OLD | NEW |