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