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 GeolocationCoordinatesMessage* coordinates_message = | |
31 geolocation_message->mutable_coordinates(); | |
32 coordinates_message->set_latitude(-42.0); | |
33 coordinates_message->set_longitude(17.3); | |
34 coordinates_message->set_altitude(123.4); | |
35 coordinates_message->set_accuracy(73.7); | |
36 | |
37 net::TestCompletionCallback cb; | |
38 processor->ProcessMessage(std::move(message), cb.callback()); | |
39 EXPECT_EQ(net::OK, cb.WaitForResult()); | |
40 } | |
41 | |
42 void SendMockErrorMessage(BlimpMessageProcessor* processor, | |
43 GeolocationErrorMessage::ErrorCode error, | |
44 std::string error_message) { | |
45 GeolocationMessage* geolocation_message; | |
46 std::unique_ptr<BlimpMessage> message = | |
47 CreateBlimpMessage(&geolocation_message); | |
48 GeolocationErrorMessage* geolocation_error_message = | |
49 geolocation_message->mutable_error(); | |
50 geolocation_error_message->set_error_code(error); | |
51 geolocation_error_message->set_error_message(error_message); | |
52 | |
53 net::TestCompletionCallback cb; | |
54 processor->ProcessMessage(std::move(message), cb.callback()); | |
55 EXPECT_EQ(net::OK, cb.WaitForResult()); | |
56 } | |
57 | |
58 MATCHER_P(EqualsUpdatedListenState, accuracy, "") { | |
Wez
2016/07/15 01:46:17
Looks like some of the naming in the tests need up
CJ
2016/07/18 21:11:56
Done.
| |
59 return arg.feature_case() == BlimpMessage::kGeolocation && | |
60 arg.geolocation().type_case() == | |
61 GeolocationMessage::kSetInterestLevel && | |
62 arg.geolocation().set_interest_level().level() == accuracy; | |
63 } | |
64 | |
65 MATCHER(EqualsStoppedListenState, "") { | |
66 return arg.feature_case() == BlimpMessage::kGeolocation && | |
67 arg.geolocation().type_case() == | |
68 GeolocationMessage::kSetInterestLevel && | |
69 arg.geolocation().set_interest_level().level() == | |
70 GeolocationSetInterestLevelMessage::NO_INTEREST; | |
71 } | |
72 | |
73 MATCHER(EqualsRequestRefresh, "") { | |
74 return arg.feature_case() == BlimpMessage::kGeolocation && | |
75 arg.geolocation().type_case() == GeolocationMessage::kRequestRefresh; | |
76 } | |
77 | |
78 class EngineGeolocationFeatureTest : public testing::Test { | |
79 public: | |
80 EngineGeolocationFeatureTest() | |
81 : out_processor_(nullptr), | |
82 mock_callback_( | |
83 base::Bind(&EngineGeolocationFeatureTest::OnLocationUpdate, | |
84 base::Unretained(this))) {} | |
85 | |
86 void SetUp() override { | |
87 out_processor_ = new MockBlimpMessageProcessor(); | |
88 feature_.set_outgoing_message_processor(base::WrapUnique(out_processor_)); | |
89 feature_.SetUpdateCallback(mock_callback_); | |
90 } | |
91 | |
92 void OnLocationUpdate(const content::Geoposition& geoposition) { | |
93 received_position = geoposition; | |
94 } | |
95 | |
96 protected: | |
97 // This is a raw pointer to a class that is owned by the NavigationFeature. | |
98 MockBlimpMessageProcessor* out_processor_; | |
99 | |
100 EngineGeolocationFeature feature_; | |
101 base::Callback<void(const content::Geoposition&)> mock_callback_; | |
102 content::Geoposition received_position; | |
103 }; | |
104 | |
105 TEST_F(EngineGeolocationFeatureTest, TestLocationReceived) { | |
Wez
2016/07/15 01:46:18
nit: Don't prefix your test names with Test - the
CJ
2016/07/18 21:11:56
Done.
| |
106 SendMockLocationMessage(&feature_); | |
107 EXPECT_EQ(content::Geoposition::ERROR_CODE_NONE, | |
108 received_position.error_code); | |
109 EXPECT_EQ(-42.0, received_position.latitude); | |
110 EXPECT_EQ(17.3, received_position.longitude); | |
111 EXPECT_EQ(123.4, received_position.altitude); | |
112 EXPECT_EQ(73.7, received_position.accuracy); | |
113 } | |
114 | |
115 TEST_F(EngineGeolocationFeatureTest, TestErrorRecieved) { | |
116 SendMockErrorMessage(&feature_, GeolocationErrorMessage::PERMISSION_DENIED, | |
117 "PERMISSION_DENIED"); | |
118 EXPECT_EQ(content::Geoposition::ERROR_CODE_PERMISSION_DENIED, | |
119 received_position.error_code); | |
120 EXPECT_EQ("PERMISSION_DENIED", received_position.error_message); | |
121 | |
122 SendMockErrorMessage(&feature_, GeolocationErrorMessage::POSITION_UNAVAILABLE, | |
123 "POSITION_UNAVAILABLE"); | |
124 EXPECT_EQ(content::Geoposition::ERROR_CODE_POSITION_UNAVAILABLE, | |
125 received_position.error_code); | |
126 EXPECT_EQ("POSITION_UNAVAILABLE", received_position.error_message); | |
127 } | |
128 | |
129 TEST_F(EngineGeolocationFeatureTest, TestSendUpdateListenStateMessage) { | |
130 EXPECT_CALL(*out_processor_, | |
131 MockableProcessMessage( | |
132 EqualsUpdatedListenState( | |
133 GeolocationSetInterestLevelMessage::HIGH_ACCURACY), | |
134 _)) | |
135 .Times(1); | |
136 EXPECT_CALL(*out_processor_, | |
137 MockableProcessMessage( | |
138 EqualsUpdatedListenState( | |
139 GeolocationSetInterestLevelMessage::LOW_ACCURACY), | |
140 _)) | |
141 .Times(1); | |
142 feature_.RequestAccuracy(GeolocationSetInterestLevelMessage::HIGH_ACCURACY); | |
143 feature_.RequestAccuracy(GeolocationSetInterestLevelMessage::LOW_ACCURACY); | |
144 } | |
145 | |
146 TEST_F(EngineGeolocationFeatureTest, TestSendStopUpdateListenStateMessage) { | |
147 EXPECT_CALL(*out_processor_, | |
148 MockableProcessMessage(EqualsStoppedListenState(), _)) | |
149 .Times(1); | |
150 feature_.RequestAccuracy(GeolocationSetInterestLevelMessage::NO_INTEREST); | |
Wez
2016/07/15 01:46:18
You could fold this into the preceding test.
CJ
2016/07/18 21:11:56
Done.
| |
151 } | |
152 | |
153 TEST_F(EngineGeolocationFeatureTest, TestSendRequestRefreshMessage) { | |
Wez
2016/07/15 01:46:18
How about a test for EngineGeolocationFeature seei
CJ
2016/07/18 21:11:56
I ended up just trying one of the unexpected Geolo
Wez
2016/07/18 23:55:21
Acknowledged.
| |
154 EXPECT_CALL(*out_processor_, | |
155 MockableProcessMessage(EqualsRequestRefresh(), _)) | |
156 .Times(1); | |
157 feature_.RequestRefresh(); | |
158 } | |
159 | |
160 } // namespace engine | |
161 } // namespace blimp | |
OLD | NEW |