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/engine/feature/geolocation/engine_geolocation_feature.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <string> |
| 9 #include <utility> |
| 10 |
| 11 #include "base/memory/ptr_util.h" |
| 12 #include "blimp/common/create_blimp_message.h" |
| 13 #include "blimp/common/proto/blimp_message.pb.h" |
| 14 #include "blimp/common/proto/geolocation.pb.h" |
| 15 #include "blimp/net/test_common.h" |
| 16 #include "content/public/browser/location_provider.h" |
| 17 #include "content/public/common/geoposition.h" |
| 18 #include "net/base/test_completion_callback.h" |
| 19 #include "net/test/gtest_util.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 |
| 23 using testing::_; |
| 24 |
| 25 namespace blimp { |
| 26 namespace engine { |
| 27 |
| 28 void SendMockLocationMessage(BlimpMessageProcessor* processor) { |
| 29 GeolocationMessage* geolocation_message; |
| 30 std::unique_ptr<BlimpMessage> message = |
| 31 CreateBlimpMessage(&geolocation_message); |
| 32 |
| 33 GeolocationCoordinatesMessage* coordinates_message = |
| 34 geolocation_message->mutable_coordinates(); |
| 35 coordinates_message->set_latitude(-42.0); |
| 36 coordinates_message->set_longitude(17.3); |
| 37 coordinates_message->set_altitude(123.4); |
| 38 coordinates_message->set_accuracy(73.7); |
| 39 |
| 40 net::TestCompletionCallback cb; |
| 41 processor->ProcessMessage(std::move(message), cb.callback()); |
| 42 EXPECT_EQ(net::OK, cb.WaitForResult()); |
| 43 } |
| 44 |
| 45 void SendMockErrorMessage(BlimpMessageProcessor* processor, |
| 46 GeolocationErrorMessage::ErrorCode error, |
| 47 std::string error_message) { |
| 48 GeolocationMessage* geolocation_message; |
| 49 std::unique_ptr<BlimpMessage> message = |
| 50 CreateBlimpMessage(&geolocation_message); |
| 51 GeolocationErrorMessage* geolocation_error_message = |
| 52 geolocation_message->mutable_error(); |
| 53 geolocation_error_message->set_error_code(error); |
| 54 geolocation_error_message->set_error_message(error_message); |
| 55 |
| 56 net::TestCompletionCallback cb; |
| 57 processor->ProcessMessage(std::move(message), cb.callback()); |
| 58 EXPECT_EQ(net::OK, cb.WaitForResult()); |
| 59 } |
| 60 |
| 61 void SendMalformedMessage(BlimpMessageProcessor* processor) { |
| 62 GeolocationMessage* geolocation_message; |
| 63 std::unique_ptr<BlimpMessage> message = |
| 64 CreateBlimpMessage(&geolocation_message); |
| 65 |
| 66 net::TestCompletionCallback cb; |
| 67 processor->ProcessMessage(std::move(message), cb.callback()); |
| 68 EXPECT_EQ(net::OK, cb.WaitForResult()); |
| 69 } |
| 70 |
| 71 void SendUnexpectedMessage(BlimpMessageProcessor* processor) { |
| 72 GeolocationMessage* geolocation_message; |
| 73 std::unique_ptr<BlimpMessage> message = |
| 74 CreateBlimpMessage(&geolocation_message); |
| 75 GeolocationSetInterestLevelMessage* geolocation_set_interest_level = |
| 76 geolocation_message->mutable_set_interest_level(); |
| 77 geolocation_set_interest_level->set_level( |
| 78 GeolocationSetInterestLevelMessage::NO_INTEREST); |
| 79 |
| 80 net::TestCompletionCallback cb; |
| 81 processor->ProcessMessage(std::move(message), cb.callback()); |
| 82 EXPECT_EQ(net::OK, cb.WaitForResult()); |
| 83 } |
| 84 |
| 85 MATCHER_P(EqualsUpdatedRequestLevel, level, "") { |
| 86 return arg.feature_case() == BlimpMessage::kGeolocation && |
| 87 arg.geolocation().type_case() == |
| 88 GeolocationMessage::kSetInterestLevel && |
| 89 arg.geolocation().set_interest_level().level() == level; |
| 90 } |
| 91 |
| 92 MATCHER(EqualsRequestRefresh, "") { |
| 93 return arg.feature_case() == BlimpMessage::kGeolocation && |
| 94 arg.geolocation().type_case() == GeolocationMessage::kRequestRefresh; |
| 95 } |
| 96 |
| 97 class EngineGeolocationFeatureTest : public testing::Test { |
| 98 public: |
| 99 EngineGeolocationFeatureTest() |
| 100 : out_processor_(nullptr), |
| 101 location_provider_(feature_.CreateGeolocationDelegate() |
| 102 ->OverrideSystemLocationProvider()), |
| 103 mock_callback_( |
| 104 base::Bind(&EngineGeolocationFeatureTest::OnLocationUpdate, |
| 105 base::Unretained(this))) {} |
| 106 |
| 107 void SetUp() override { |
| 108 out_processor_ = new MockBlimpMessageProcessor(); |
| 109 feature_.set_outgoing_message_processor(base::WrapUnique(out_processor_)); |
| 110 location_provider_->SetUpdateCallback(mock_callback_); |
| 111 } |
| 112 |
| 113 void OnLocationUpdate(const content::LocationProvider* provider, |
| 114 const content::Geoposition& geoposition) { |
| 115 received_position = geoposition; |
| 116 } |
| 117 |
| 118 protected: |
| 119 // This is a raw pointer to a class that is owned by the NavigationFeature. |
| 120 MockBlimpMessageProcessor* out_processor_; |
| 121 |
| 122 EngineGeolocationFeature feature_; |
| 123 std::unique_ptr<content::LocationProvider> location_provider_; |
| 124 content::LocationProvider::LocationProviderUpdateCallback mock_callback_; |
| 125 content::Geoposition received_position; |
| 126 }; |
| 127 |
| 128 TEST_F(EngineGeolocationFeatureTest, LocationReceived) { |
| 129 SendMockLocationMessage(&feature_); |
| 130 EXPECT_EQ(content::Geoposition::ERROR_CODE_NONE, |
| 131 received_position.error_code); |
| 132 EXPECT_EQ(-42.0, received_position.latitude); |
| 133 EXPECT_EQ(17.3, received_position.longitude); |
| 134 EXPECT_EQ(123.4, received_position.altitude); |
| 135 EXPECT_EQ(73.7, received_position.accuracy); |
| 136 } |
| 137 |
| 138 TEST_F(EngineGeolocationFeatureTest, ErrorRecieved) { |
| 139 SendMockErrorMessage(&feature_, GeolocationErrorMessage::PERMISSION_DENIED, |
| 140 "PERMISSION_DENIED"); |
| 141 EXPECT_EQ(content::Geoposition::ERROR_CODE_PERMISSION_DENIED, |
| 142 received_position.error_code); |
| 143 EXPECT_EQ("PERMISSION_DENIED", received_position.error_message); |
| 144 |
| 145 SendMockErrorMessage(&feature_, GeolocationErrorMessage::POSITION_UNAVAILABLE, |
| 146 "POSITION_UNAVAILABLE"); |
| 147 EXPECT_EQ(content::Geoposition::ERROR_CODE_POSITION_UNAVAILABLE, |
| 148 received_position.error_code); |
| 149 EXPECT_EQ("POSITION_UNAVAILABLE", received_position.error_message); |
| 150 } |
| 151 |
| 152 TEST_F(EngineGeolocationFeatureTest, UpdateRequestLevel) { |
| 153 EXPECT_CALL(*out_processor_, |
| 154 MockableProcessMessage( |
| 155 EqualsUpdatedRequestLevel( |
| 156 GeolocationSetInterestLevelMessage::HIGH_ACCURACY), |
| 157 _)) |
| 158 .Times(1); |
| 159 EXPECT_CALL(*out_processor_, |
| 160 MockableProcessMessage( |
| 161 EqualsUpdatedRequestLevel( |
| 162 GeolocationSetInterestLevelMessage::LOW_ACCURACY), |
| 163 _)) |
| 164 .Times(1); |
| 165 EXPECT_CALL(*out_processor_, |
| 166 MockableProcessMessage( |
| 167 EqualsUpdatedRequestLevel( |
| 168 GeolocationSetInterestLevelMessage::NO_INTEREST), |
| 169 _)) |
| 170 .Times(1); |
| 171 |
| 172 location_provider_->StartProvider(true); |
| 173 location_provider_->StartProvider(false); |
| 174 location_provider_->StopProvider(); |
| 175 } |
| 176 |
| 177 TEST_F(EngineGeolocationFeatureTest, RequestRefresh) { |
| 178 EXPECT_CALL(*out_processor_, |
| 179 MockableProcessMessage( |
| 180 EqualsUpdatedRequestLevel( |
| 181 GeolocationSetInterestLevelMessage::HIGH_ACCURACY), |
| 182 _)) |
| 183 .Times(1); |
| 184 |
| 185 EXPECT_CALL(*out_processor_, |
| 186 MockableProcessMessage(EqualsRequestRefresh(), _)) |
| 187 .Times(1); |
| 188 EXPECT_CALL(*out_processor_, |
| 189 MockableProcessMessage( |
| 190 EqualsUpdatedRequestLevel( |
| 191 GeolocationSetInterestLevelMessage::NO_INTEREST), |
| 192 _)) |
| 193 .Times(1); |
| 194 |
| 195 location_provider_->StartProvider(true); |
| 196 location_provider_->RequestRefresh(); |
| 197 } |
| 198 |
| 199 } // namespace engine |
| 200 } // namespace blimp |
OLD | NEW |