Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: chromeos/geolocation/simple_geolocation_unittest.cc

Issue 1819173002: SimpleGeolocation should support sending WiFi AP data. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update after review. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stddef.h> 5 #include <stddef.h>
6 6
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/stringprintf.h"
12 #include "chromeos/dbus/dbus_thread_manager.h"
13 #include "chromeos/dbus/shill_manager_client.h"
10 #include "chromeos/geolocation/simple_geolocation_provider.h" 14 #include "chromeos/geolocation/simple_geolocation_provider.h"
15 #include "chromeos/geolocation/simple_geolocation_request_test_monitor.h"
16 #include "chromeos/network/geolocation_handler.h"
17 #include "chromeos/network/network_handler.h"
11 #include "net/http/http_response_headers.h" 18 #include "net/http/http_response_headers.h"
12 #include "net/http/http_status_code.h" 19 #include "net/http/http_status_code.h"
13 #include "net/url_request/test_url_fetcher_factory.h" 20 #include "net/url_request/test_url_fetcher_factory.h"
14 #include "net/url_request/url_fetcher_impl.h" 21 #include "net/url_request/url_fetcher_impl.h"
15 #include "net/url_request/url_request_status.h" 22 #include "net/url_request/url_request_status.h"
16 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "third_party/cros_system_api/dbus/service_constants.h"
17 25
18 namespace { 26 namespace {
19 27
20 const int kRequestRetryIntervalMilliSeconds = 200; 28 const int kRequestRetryIntervalMilliSeconds = 200;
21 29
22 // This should be different from default to prevent SimpleGeolocationRequest 30 // This should be different from default to prevent SimpleGeolocationRequest
23 // from modifying it. 31 // from modifying it.
24 const char kTestGeolocationProviderUrl[] = 32 const char kTestGeolocationProviderUrl[] =
25 "https://localhost/geolocation/v1/geolocate?"; 33 "https://localhost/geolocation/v1/geolocate?";
26 34
27 const char kSimpleResponseBody[] = 35 const char kSimpleResponseBody[] =
28 "{\n" 36 "{\n"
29 " \"location\": {\n" 37 " \"location\": {\n"
30 " \"lat\": 51.0,\n" 38 " \"lat\": 51.0,\n"
31 " \"lng\": -0.1\n" 39 " \"lng\": -0.1\n"
32 " },\n" 40 " },\n"
33 " \"accuracy\": 1200.4\n" 41 " \"accuracy\": 1200.4\n"
34 "}"; 42 "}";
43 const char kIPOnlyRequestBody[] = "{\"considerIp\": \"true\"}";
44 const char kOneWiFiAPRequestBody[] =
45 "{"
46 "\"considerIp\":true,"
47 "\"wifiAccessPoints\":["
48 "{"
49 "\"channel\":1,"
50 "\"macAddress\":\"01:00:00:00:00:00\","
51 "\"signalStrength\":10,"
52 "\"signalToNoiseRatio\":0"
53 "}"
54 "]"
55 "}";
56 const char kExpectedPosition[] =
57 "latitude=51.000000, longitude=-0.100000, accuracy=1200.400000, "
58 "error_code=0, error_message='', status=1 (OK)";
59
60 const char kWiFiAP1MacAddress[] = "01:00:00:00:00:00";
35 } // anonymous namespace 61 } // anonymous namespace
36 62
37 namespace chromeos { 63 namespace chromeos {
38 64
39 // This is helper class for net::FakeURLFetcherFactory. 65 // This is helper class for net::FakeURLFetcherFactory.
40 class TestGeolocationAPIURLFetcherCallback { 66 class TestGeolocationAPIURLFetcherCallback {
41 public: 67 public:
42 TestGeolocationAPIURLFetcherCallback(const GURL& url, 68 TestGeolocationAPIURLFetcherCallback(const GURL& url,
43 const size_t require_retries, 69 const size_t require_retries,
44 const std::string& response, 70 const std::string& response,
45 SimpleGeolocationProvider* provider) 71 SimpleGeolocationProvider* provider)
46 : url_(url), 72 : url_(url),
47 require_retries_(require_retries), 73 require_retries_(require_retries),
48 response_(response), 74 response_(response),
49 factory_(NULL), 75 factory_(nullptr),
50 attempts_(0), 76 attempts_(0),
51 provider_(provider) {} 77 provider_(provider) {}
52 78
53 scoped_ptr<net::FakeURLFetcher> CreateURLFetcher( 79 scoped_ptr<net::FakeURLFetcher> CreateURLFetcher(
54 const GURL& url, 80 const GURL& url,
55 net::URLFetcherDelegate* delegate, 81 net::URLFetcherDelegate* delegate,
56 const std::string& response_data, 82 const std::string& response_data,
57 net::HttpStatusCode response_code, 83 net::HttpStatusCode response_code,
58 net::URLRequestStatus::Status status) { 84 net::URLRequestStatus::Status status) {
59 EXPECT_EQ(provider_->requests_.size(), 1U); 85 EXPECT_EQ(provider_->requests_.size(), 1U);
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 // Response data is served to SimpleGeolocationProvider via 134 // Response data is served to SimpleGeolocationProvider via
109 // net::FakeURLFetcher. 135 // net::FakeURLFetcher.
110 class GeolocationAPIFetcherFactory { 136 class GeolocationAPIFetcherFactory {
111 public: 137 public:
112 GeolocationAPIFetcherFactory(const GURL& url, 138 GeolocationAPIFetcherFactory(const GURL& url,
113 const std::string& response, 139 const std::string& response,
114 const size_t require_retries, 140 const size_t require_retries,
115 SimpleGeolocationProvider* provider) { 141 SimpleGeolocationProvider* provider) {
116 url_callback_.reset(new TestGeolocationAPIURLFetcherCallback( 142 url_callback_.reset(new TestGeolocationAPIURLFetcherCallback(
117 url, require_retries, response, provider)); 143 url, require_retries, response, provider));
118 net::URLFetcherImpl::set_factory(NULL); 144 net::URLFetcherImpl::set_factory(nullptr);
119 fetcher_factory_.reset(new net::FakeURLFetcherFactory( 145 fetcher_factory_.reset(new net::FakeURLFetcherFactory(
120 NULL, 146 nullptr,
121 base::Bind(&TestGeolocationAPIURLFetcherCallback::CreateURLFetcher, 147 base::Bind(&TestGeolocationAPIURLFetcherCallback::CreateURLFetcher,
122 base::Unretained(url_callback_.get())))); 148 base::Unretained(url_callback_.get()))));
123 url_callback_->Initialize(fetcher_factory_.get()); 149 url_callback_->Initialize(fetcher_factory_.get());
124 } 150 }
125 151
126 size_t attempts() const { return url_callback_->attempts(); } 152 size_t attempts() const { return url_callback_->attempts(); }
127 153
128 private: 154 private:
129 scoped_ptr<TestGeolocationAPIURLFetcherCallback> url_callback_; 155 scoped_ptr<TestGeolocationAPIURLFetcherCallback> url_callback_;
130 scoped_ptr<net::FakeURLFetcherFactory> fetcher_factory_; 156 scoped_ptr<net::FakeURLFetcherFactory> fetcher_factory_;
(...skipping 24 matching lines...) Expand all
155 bool server_error() const { return server_error_; } 181 bool server_error() const { return server_error_; }
156 base::TimeDelta elapsed() const { return elapsed_; } 182 base::TimeDelta elapsed() const { return elapsed_; }
157 183
158 private: 184 private:
159 Geoposition position_; 185 Geoposition position_;
160 bool server_error_; 186 bool server_error_;
161 base::TimeDelta elapsed_; 187 base::TimeDelta elapsed_;
162 scoped_ptr<base::RunLoop> message_loop_runner_; 188 scoped_ptr<base::RunLoop> message_loop_runner_;
163 }; 189 };
164 190
191 class WiFiTestMonitor : public SimpleGeolocationRequestTestMonitor {
192 public:
193 WiFiTestMonitor() {}
194
195 void OnRequestCreated(SimpleGeolocationRequest* request) override {}
196 void OnStart(SimpleGeolocationRequest* request) override {
197 last_request_body_ = request->FormatRequestBodyForTesting();
198 }
199
200 const std::string& last_request_body() const { return last_request_body_; }
201
202 private:
203 std::string last_request_body_;
204
205 DISALLOW_COPY_AND_ASSIGN(WiFiTestMonitor);
206 };
207
165 class SimpleGeolocationTest : public testing::Test { 208 class SimpleGeolocationTest : public testing::Test {
166 private: 209 private:
167 base::MessageLoop message_loop_; 210 base::MessageLoop message_loop_;
168 }; 211 };
169 212
170 TEST_F(SimpleGeolocationTest, ResponseOK) { 213 TEST_F(SimpleGeolocationTest, ResponseOK) {
171 SimpleGeolocationProvider provider(NULL, GURL(kTestGeolocationProviderUrl)); 214 SimpleGeolocationProvider provider(nullptr,
215 GURL(kTestGeolocationProviderUrl));
172 216
173 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl), 217 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl),
174 std::string(kSimpleResponseBody), 218 std::string(kSimpleResponseBody),
175 0 /* require_retries */, 219 0 /* require_retries */,
176 &provider); 220 &provider);
177 221
178 GeolocationReceiver receiver; 222 GeolocationReceiver receiver;
179 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), 223 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), false,
180 base::Bind(&GeolocationReceiver::OnRequestDone, 224 base::Bind(&GeolocationReceiver::OnRequestDone,
181 base::Unretained(&receiver))); 225 base::Unretained(&receiver)));
182 receiver.WaitUntilRequestDone(); 226 receiver.WaitUntilRequestDone();
183 227
184 EXPECT_EQ( 228 EXPECT_EQ(kExpectedPosition, receiver.position().ToString());
185 "latitude=51.000000, longitude=-0.100000, accuracy=1200.400000, "
186 "error_code=0, error_message='', status=1 (OK)",
187 receiver.position().ToString());
188 EXPECT_FALSE(receiver.server_error()); 229 EXPECT_FALSE(receiver.server_error());
189 EXPECT_EQ(1U, url_factory.attempts()); 230 EXPECT_EQ(1U, url_factory.attempts());
190 } 231 }
191 232
192 TEST_F(SimpleGeolocationTest, ResponseOKWithRetries) { 233 TEST_F(SimpleGeolocationTest, ResponseOKWithRetries) {
193 SimpleGeolocationProvider provider(NULL, GURL(kTestGeolocationProviderUrl)); 234 SimpleGeolocationProvider provider(nullptr,
235 GURL(kTestGeolocationProviderUrl));
194 236
195 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl), 237 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl),
196 std::string(kSimpleResponseBody), 238 std::string(kSimpleResponseBody),
197 3 /* require_retries */, 239 3 /* require_retries */,
198 &provider); 240 &provider);
199 241
200 GeolocationReceiver receiver; 242 GeolocationReceiver receiver;
201 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), 243 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), false,
202 base::Bind(&GeolocationReceiver::OnRequestDone, 244 base::Bind(&GeolocationReceiver::OnRequestDone,
203 base::Unretained(&receiver))); 245 base::Unretained(&receiver)));
204 receiver.WaitUntilRequestDone(); 246 receiver.WaitUntilRequestDone();
205 EXPECT_EQ( 247 EXPECT_EQ(kExpectedPosition, receiver.position().ToString());
206 "latitude=51.000000, longitude=-0.100000, accuracy=1200.400000, "
207 "error_code=0, error_message='', status=1 (OK)",
208 receiver.position().ToString());
209 EXPECT_FALSE(receiver.server_error()); 248 EXPECT_FALSE(receiver.server_error());
210 EXPECT_EQ(4U, url_factory.attempts()); 249 EXPECT_EQ(4U, url_factory.attempts());
211 } 250 }
212 251
213 TEST_F(SimpleGeolocationTest, InvalidResponse) { 252 TEST_F(SimpleGeolocationTest, InvalidResponse) {
214 SimpleGeolocationProvider provider(NULL, GURL(kTestGeolocationProviderUrl)); 253 SimpleGeolocationProvider provider(nullptr,
254 GURL(kTestGeolocationProviderUrl));
215 255
216 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl), 256 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl),
217 "invalid JSON string", 257 "invalid JSON string",
218 0 /* require_retries */, 258 0 /* require_retries */,
219 &provider); 259 &provider);
220 260
221 GeolocationReceiver receiver; 261 GeolocationReceiver receiver;
222 262
223 const int timeout_seconds = 1; 263 const int timeout_seconds = 1;
224 size_t expected_retries = static_cast<size_t>( 264 size_t expected_retries = static_cast<size_t>(
225 timeout_seconds * 1000 / kRequestRetryIntervalMilliSeconds); 265 timeout_seconds * 1000 / kRequestRetryIntervalMilliSeconds);
226 ASSERT_GE(expected_retries, 2U); 266 ASSERT_GE(expected_retries, 2U);
227 267
228 provider.RequestGeolocation(base::TimeDelta::FromSeconds(timeout_seconds), 268 provider.RequestGeolocation(base::TimeDelta::FromSeconds(timeout_seconds),
269 false,
229 base::Bind(&GeolocationReceiver::OnRequestDone, 270 base::Bind(&GeolocationReceiver::OnRequestDone,
230 base::Unretained(&receiver))); 271 base::Unretained(&receiver)));
231 receiver.WaitUntilRequestDone(); 272 receiver.WaitUntilRequestDone();
232 273
233 EXPECT_EQ( 274 EXPECT_EQ(
234 "latitude=200.000000, longitude=200.000000, accuracy=-1.000000, " 275 "latitude=200.000000, longitude=200.000000, accuracy=-1.000000, "
235 "error_code=0, error_message='SimpleGeolocation provider at " 276 "error_code=0, error_message='SimpleGeolocation provider at "
236 "'https://localhost/' : JSONReader failed: Line: 1, column: 1, " 277 "'https://localhost/' : JSONReader failed: Line: 1, column: 1, "
237 "Unexpected token..', status=4 (TIMEOUT)", 278 "Unexpected token..', status=4 (TIMEOUT)",
238 receiver.position().ToString()); 279 receiver.position().ToString());
239 EXPECT_TRUE(receiver.server_error()); 280 EXPECT_TRUE(receiver.server_error());
240 EXPECT_GE(url_factory.attempts(), 2U); 281 EXPECT_GE(url_factory.attempts(), 2U);
241 if (url_factory.attempts() > expected_retries + 1) { 282 if (url_factory.attempts() > expected_retries + 1) {
242 LOG(WARNING) 283 LOG(WARNING)
243 << "SimpleGeolocationTest::InvalidResponse: Too many attempts (" 284 << "SimpleGeolocationTest::InvalidResponse: Too many attempts ("
244 << url_factory.attempts() << "), no more then " << expected_retries + 1 285 << url_factory.attempts() << "), no more then " << expected_retries + 1
245 << " expected."; 286 << " expected.";
246 } 287 }
247 if (url_factory.attempts() < expected_retries - 1) { 288 if (url_factory.attempts() < expected_retries - 1) {
248 LOG(WARNING) 289 LOG(WARNING)
249 << "SimpleGeolocationTest::InvalidResponse: Too little attempts (" 290 << "SimpleGeolocationTest::InvalidResponse: Too little attempts ("
250 << url_factory.attempts() << "), greater then " << expected_retries - 1 291 << url_factory.attempts() << "), greater then " << expected_retries - 1
251 << " expected."; 292 << " expected.";
252 } 293 }
253 } 294 }
254 295
296 TEST_F(SimpleGeolocationTest, NoWiFi) {
297 // This initializes DBusThreadManager and markes it "for tests only".
298 DBusThreadManager::GetSetterForTesting();
299 NetworkHandler::Initialize();
300
301 WiFiTestMonitor requests_monitor;
302 SimpleGeolocationRequest::SetTestMonitor(&requests_monitor);
303
304 SimpleGeolocationProvider provider(nullptr,
305 GURL(kTestGeolocationProviderUrl));
306
307 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl),
308 std::string(kSimpleResponseBody),
309 0 /* require_retries */, &provider);
310
311 GeolocationReceiver receiver;
312 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), true,
313 base::Bind(&GeolocationReceiver::OnRequestDone,
314 base::Unretained(&receiver)));
315 receiver.WaitUntilRequestDone();
316 EXPECT_EQ(kIPOnlyRequestBody, requests_monitor.last_request_body());
317
318 EXPECT_EQ(kExpectedPosition, receiver.position().ToString());
319 EXPECT_FALSE(receiver.server_error());
320 EXPECT_EQ(1U, url_factory.attempts());
321
322 NetworkHandler::Shutdown();
323 DBusThreadManager::Shutdown();
324 }
325
326 // Test sending of WiFi Access points.
327 // (This is mostly derived from GeolocationHandlerTest.)
328 class SimpleGeolocationWiFiTest : public ::testing::TestWithParam<bool> {
329 public:
330 SimpleGeolocationWiFiTest() : manager_test_(nullptr) {}
331
332 ~SimpleGeolocationWiFiTest() override {}
333
334 void SetUp() override {
335 // This initializes DBusThreadManager and markes it "for tests only".
336 DBusThreadManager::GetSetterForTesting();
337 // Get the test interface for manager / device.
338 manager_test_ =
339 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface();
340 ASSERT_TRUE(manager_test_);
341 geolocation_handler_.reset(new GeolocationHandler());
342 geolocation_handler_->Init();
343 message_loop_.RunUntilIdle();
344 }
345
346 void TearDown() override {
347 geolocation_handler_.reset();
348 DBusThreadManager::Shutdown();
349 }
350
351 bool GetWifiAccessPoints() {
352 return geolocation_handler_->GetWifiAccessPoints(&wifi_access_points_,
353 nullptr);
354 }
355
356 void AddAccessPoint(int idx) {
357 base::DictionaryValue properties;
358 std::string mac_address =
359 base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X", idx, 0, 0, 0, 0, 0);
360 std::string channel = base::IntToString(idx);
361 std::string strength = base::IntToString(idx * 10);
362 properties.SetStringWithoutPathExpansion(shill::kGeoMacAddressProperty,
363 mac_address);
364 properties.SetStringWithoutPathExpansion(shill::kGeoChannelProperty,
365 channel);
366 properties.SetStringWithoutPathExpansion(shill::kGeoSignalStrengthProperty,
367 strength);
368 manager_test_->AddGeoNetwork(shill::kTypeWifi, properties);
369 message_loop_.RunUntilIdle();
370 }
371
372 protected:
373 base::MessageLoopForUI message_loop_;
374 scoped_ptr<GeolocationHandler> geolocation_handler_;
375 ShillManagerClient::TestInterface* manager_test_;
376 WifiAccessPointVector wifi_access_points_;
377
378 private:
379 DISALLOW_COPY_AND_ASSIGN(SimpleGeolocationWiFiTest);
380 };
381
382 // Parameter is enable/disable sending of WiFi data.
383 TEST_P(SimpleGeolocationWiFiTest, WiFiExists) {
384 NetworkHandler::Initialize();
385
386 WiFiTestMonitor requests_monitor;
387 SimpleGeolocationRequest::SetTestMonitor(&requests_monitor);
388
389 SimpleGeolocationProvider provider(nullptr,
390 GURL(kTestGeolocationProviderUrl));
391
392 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl),
393 std::string(kSimpleResponseBody),
394 0 /* require_retries */, &provider);
395 {
396 GeolocationReceiver receiver;
397 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), GetParam(),
398 base::Bind(&GeolocationReceiver::OnRequestDone,
399 base::Unretained(&receiver)));
400 receiver.WaitUntilRequestDone();
401 EXPECT_EQ(kIPOnlyRequestBody, requests_monitor.last_request_body());
402
403 EXPECT_EQ(kExpectedPosition, receiver.position().ToString());
404 EXPECT_FALSE(receiver.server_error());
405 EXPECT_EQ(1U, url_factory.attempts());
406 }
407
408 // Add an acces point.
409 AddAccessPoint(1);
410 message_loop_.RunUntilIdle();
411 // Inititial call should return false and request access points.
412 EXPECT_FALSE(GetWifiAccessPoints());
413 message_loop_.RunUntilIdle();
414 // Second call should return true since we have an access point.
415 EXPECT_TRUE(GetWifiAccessPoints());
416 ASSERT_EQ(1u, wifi_access_points_.size());
417 EXPECT_EQ(kWiFiAP1MacAddress, wifi_access_points_[0].mac_address);
418 EXPECT_EQ(1, wifi_access_points_[0].channel);
419
420 {
421 GeolocationReceiver receiver;
422 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), GetParam(),
423 base::Bind(&GeolocationReceiver::OnRequestDone,
424 base::Unretained(&receiver)));
425 receiver.WaitUntilRequestDone();
426 if (GetParam()) {
427 // Sending WiFi data is enabled.
428 EXPECT_EQ(kOneWiFiAPRequestBody, requests_monitor.last_request_body());
429 } else {
430 // Sending WiFi data is disabled.
431 EXPECT_EQ(kIPOnlyRequestBody, requests_monitor.last_request_body());
432 }
433
434 EXPECT_EQ(kExpectedPosition, receiver.position().ToString());
435 EXPECT_FALSE(receiver.server_error());
436 // This is total.
437 EXPECT_EQ(2U, url_factory.attempts());
438 }
439 NetworkHandler::Shutdown();
440 }
441
442 // This test verifies that WiFi data is sent only if sending was requested.
443 INSTANTIATE_TEST_CASE_P(EnableDisableSendingWifiData,
444 SimpleGeolocationWiFiTest,
445 testing::Bool());
446
255 } // namespace chromeos 447 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/geolocation/simple_geolocation_request_test_monitor.cc ('k') | chromeos/network/geolocation_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698