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 "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 using testing::_; |
| 21 |
| 22 namespace blimp { |
| 23 namespace engine { |
| 24 |
| 25 void SendMockLocationMessage(BlimpMessageProcessor* processor) { |
| 26 GeolocationMessage* geolocation_message; |
| 27 std::unique_ptr<BlimpMessage> message = |
| 28 CreateBlimpMessage(&geolocation_message); |
| 29 |
| 30 GeolocationPositionMessage* location_message = |
| 31 geolocation_message->mutable_location(); |
| 32 |
| 33 GeolocationCoordinates* coordinates = location_message->mutable_coordinates(); |
| 34 coordinates->set_latitude(-42.0); |
| 35 coordinates->set_longitude(17.3); |
| 36 coordinates->set_altitude(123.4); |
| 37 coordinates->set_accuracy(73.7); |
| 38 |
| 39 location_message->set_timestamp_millis(87); |
| 40 |
| 41 net::TestCompletionCallback cb; |
| 42 processor->ProcessMessage(std::move(message), cb.callback()); |
| 43 EXPECT_EQ(net::OK, cb.WaitForResult()); |
| 44 } |
| 45 |
| 46 void SendMockErrorMessage(BlimpMessageProcessor* processor, |
| 47 GeolocationErrorMessage::ErrorCode error, |
| 48 std::string error_message) { |
| 49 GeolocationMessage* geolocation_message; |
| 50 std::unique_ptr<BlimpMessage> message = |
| 51 CreateBlimpMessage(&geolocation_message); |
| 52 GeolocationErrorMessage* geolocation_error_message = |
| 53 geolocation_message->mutable_error(); |
| 54 geolocation_error_message->set_error_code(error); |
| 55 geolocation_error_message->set_error_message(error_message); |
| 56 |
| 57 net::TestCompletionCallback cb; |
| 58 processor->ProcessMessage(std::move(message), cb.callback()); |
| 59 EXPECT_EQ(net::OK, cb.WaitForResult()); |
| 60 } |
| 61 |
| 62 MATCHER_P(EqualsUpdatedListenState, accuracy, "") { |
| 63 return arg.feature_case() == BlimpMessage::kGeolocation && |
| 64 arg.geolocation().type_case() == |
| 65 GeolocationMessage::kUpdateListenState && |
| 66 arg.geolocation().update_listen_state().listen_state() == accuracy; |
| 67 } |
| 68 |
| 69 MATCHER(EqualsStoppedListenState, "") { |
| 70 return arg.feature_case() == BlimpMessage::kGeolocation && |
| 71 arg.geolocation().type_case() == |
| 72 GeolocationMessage::kUpdateListenState && |
| 73 arg.geolocation().update_listen_state().listen_state() == |
| 74 GeolocationInterestMessage::REQUEST_NONE; |
| 75 } |
| 76 |
| 77 MATCHER(EqualsRequestRefresh, "") { |
| 78 return arg.feature_case() == BlimpMessage::kGeolocation && |
| 79 arg.geolocation().type_case() == GeolocationMessage::kRequestRefresh; |
| 80 } |
| 81 |
| 82 class EngineGeolocationFeatureTest : public testing::Test { |
| 83 public: |
| 84 EngineGeolocationFeatureTest() |
| 85 : out_processor_(nullptr), |
| 86 mock_callback_( |
| 87 base::Bind(&EngineGeolocationFeatureTest::OnLocationUpdate, |
| 88 base::Unretained(this))) {} |
| 89 |
| 90 void SetUp() override { |
| 91 out_processor_ = new MockBlimpMessageProcessor(); |
| 92 feature_.set_outgoing_message_processor(base::WrapUnique(out_processor_)); |
| 93 feature_.SetUpdateCallback(mock_callback_); |
| 94 } |
| 95 |
| 96 void OnLocationUpdate(const content::Geoposition& geoposition) { |
| 97 received_position = geoposition; |
| 98 } |
| 99 |
| 100 protected: |
| 101 // This is a raw pointer to a class that is owned by the NavigationFeature. |
| 102 MockBlimpMessageProcessor* out_processor_; |
| 103 |
| 104 EngineGeolocationFeature feature_; |
| 105 base::Callback<void(const content::Geoposition&)> mock_callback_; |
| 106 content::Geoposition received_position; |
| 107 }; |
| 108 |
| 109 TEST_F(EngineGeolocationFeatureTest, TestLocationReceived) { |
| 110 SendMockLocationMessage(&feature_); |
| 111 EXPECT_EQ(content::Geoposition::ERROR_CODE_NONE, |
| 112 received_position.error_code); |
| 113 EXPECT_EQ(-42.0, received_position.latitude); |
| 114 EXPECT_EQ(17.3, received_position.longitude); |
| 115 EXPECT_EQ(123.4, received_position.altitude); |
| 116 EXPECT_EQ(73.7, received_position.accuracy); |
| 117 EXPECT_EQ(base::Time::FromJsTime(87), received_position.timestamp); |
| 118 } |
| 119 |
| 120 TEST_F(EngineGeolocationFeatureTest, TestErrorRecieved) { |
| 121 SendMockErrorMessage(&feature_, GeolocationErrorMessage::PERMISSION_DENIED, |
| 122 "PERMISSION_DENIED"); |
| 123 EXPECT_EQ(content::Geoposition::ERROR_CODE_PERMISSION_DENIED, |
| 124 received_position.error_code); |
| 125 EXPECT_EQ("PERMISSION_DENIED", received_position.error_message); |
| 126 |
| 127 SendMockErrorMessage(&feature_, GeolocationErrorMessage::POSITION_UNAVAILABLE, |
| 128 "POSITION_UNAVAILABLE"); |
| 129 EXPECT_EQ(content::Geoposition::ERROR_CODE_POSITION_UNAVAILABLE, |
| 130 received_position.error_code); |
| 131 EXPECT_EQ("POSITION_UNAVAILABLE", received_position.error_message); |
| 132 } |
| 133 |
| 134 TEST_F(EngineGeolocationFeatureTest, TestSendUpdateListenStateMessage) { |
| 135 EXPECT_CALL( |
| 136 *out_processor_, |
| 137 MockableProcessMessage( |
| 138 EqualsUpdatedListenState(GeolocationInterestMessage::ACCURACY_HIGH), |
| 139 _)) |
| 140 .Times(1); |
| 141 EXPECT_CALL( |
| 142 *out_processor_, |
| 143 MockableProcessMessage( |
| 144 EqualsUpdatedListenState(GeolocationInterestMessage::ACCURACY_LOW), |
| 145 _)) |
| 146 .Times(1); |
| 147 feature_.StartProvider(true); |
| 148 feature_.StartProvider(false); |
| 149 } |
| 150 |
| 151 TEST_F(EngineGeolocationFeatureTest, TestSendStopUpdateListenStateMessage) { |
| 152 EXPECT_CALL(*out_processor_, |
| 153 MockableProcessMessage(EqualsStoppedListenState(), _)) |
| 154 .Times(1); |
| 155 feature_.StopProvider(); |
| 156 } |
| 157 |
| 158 TEST_F(EngineGeolocationFeatureTest, TestSendRequestRefreshMessage) { |
| 159 EXPECT_CALL(*out_processor_, |
| 160 MockableProcessMessage(EqualsRequestRefresh(), _)) |
| 161 .Times(1); |
| 162 feature_.RequestRefresh(); |
| 163 } |
| 164 |
| 165 } // namespace engine |
| 166 } // namespace blimp |
OLD | NEW |