blob: 352a898dd7a5c3299344222aead08759cb234550 [file] [log] [blame]
[email protected]4e7fcd382014-05-19 22:00:331// Copyright 2014 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
avi6e1a22d2015-12-21 03:43:205#include <stddef.h>
6
Sebastien Marchand6d0558fd2019-01-25 16:49:377#include "base/bind.h"
avi6e1a22d2015-12-21 03:43:208#include "base/macros.h"
[email protected]4e7fcd382014-05-19 22:00:339#include "base/run_loop.h"
alemate9af1ade2016-03-23 23:21:4410#include "base/strings/string_number_conversions.h"
11#include "base/strings/stringprintf.h"
Carlos Caballerob8281a62019-06-10 20:12:2212#include "base/test/scoped_task_environment.h"
Steven Bennettsb4bf6cd2019-04-05 17:02:5713#include "chromeos/dbus/shill/shill_clients.h"
Steven Bennettsd11e440f2019-04-04 01:09:0014#include "chromeos/dbus/shill/shill_manager_client.h"
satoruxde6a5dc2014-12-12 22:30:3715#include "chromeos/geolocation/simple_geolocation_provider.h"
alemate9af1ade2016-03-23 23:21:4416#include "chromeos/geolocation/simple_geolocation_request_test_monitor.h"
17#include "chromeos/network/geolocation_handler.h"
18#include "chromeos/network/network_handler.h"
[email protected]4e7fcd382014-05-19 22:00:3319#include "net/http/http_response_headers.h"
20#include "net/http/http_status_code.h"
[email protected]4e7fcd382014-05-19 22:00:3321#include "net/url_request/url_request_status.h"
Sergio Villar Senin5a1ed4782018-08-21 19:26:4022#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
23#include "services/network/test/test_url_loader_factory.h"
[email protected]4e7fcd382014-05-19 22:00:3324#include "testing/gtest/include/gtest/gtest.h"
alemate9af1ade2016-03-23 23:21:4425#include "third_party/cros_system_api/dbus/service_constants.h"
[email protected]4e7fcd382014-05-19 22:00:3326
27namespace {
28
skylarceaf50cb2017-02-14 01:16:2829constexpr int kRequestRetryIntervalMilliSeconds = 200;
[email protected]4e7fcd382014-05-19 22:00:3330
31// This should be different from default to prevent SimpleGeolocationRequest
32// from modifying it.
skylarceaf50cb2017-02-14 01:16:2833constexpr char kTestGeolocationProviderUrl[] =
[email protected]4e7fcd382014-05-19 22:00:3334 "https://localhost/geolocation/v1/geolocate?";
35
skylarceaf50cb2017-02-14 01:16:2836constexpr char kSimpleResponseBody[] =
[email protected]4e7fcd382014-05-19 22:00:3337 "{\n"
38 " \"location\": {\n"
39 " \"lat\": 51.0,\n"
40 " \"lng\": -0.1\n"
41 " },\n"
42 " \"accuracy\": 1200.4\n"
43 "}";
skylarceaf50cb2017-02-14 01:16:2844constexpr char kIPOnlyRequestBody[] = "{\"considerIp\": \"true\"}";
45constexpr char kOneWiFiAPRequestBody[] =
alemate9af1ade2016-03-23 23:21:4446 "{"
skylarceaf50cb2017-02-14 01:16:2847 "\"considerIp\":true,"
48 "\"wifiAccessPoints\":["
49 "{"
50 "\"channel\":1,"
51 "\"macAddress\":\"01:00:00:00:00:00\","
52 "\"signalStrength\":10,"
53 "\"signalToNoiseRatio\":0"
54 "}"
55 "]"
alemate9af1ade2016-03-23 23:21:4456 "}";
skylarceaf50cb2017-02-14 01:16:2857constexpr char kOneCellTowerRequestBody[] =
58 "{"
59 "\"cellTowers\":["
60 "{"
61 "\"cellId\":\"1\","
Niranjan Kumar4c2ce9f2017-10-03 22:28:5162 "\"locationAreaCode\":\"3\","
skylarceaf50cb2017-02-14 01:16:2863 "\"mobileCountryCode\":\"100\","
64 "\"mobileNetworkCode\":\"101\""
65 "}"
66 "],"
67 "\"considerIp\":true"
68 "}";
69constexpr char kExpectedPosition[] =
alemate9af1ade2016-03-23 23:21:4470 "latitude=51.000000, longitude=-0.100000, accuracy=1200.400000, "
71 "error_code=0, error_message='', status=1 (OK)";
72
skylarceaf50cb2017-02-14 01:16:2873constexpr char kWiFiAP1MacAddress[] = "01:00:00:00:00:00";
74constexpr char kCellTower1MNC[] = "101";
[email protected]4e7fcd382014-05-19 22:00:3375} // anonymous namespace
76
77namespace chromeos {
78
Sergio Villar Senin5a1ed4782018-08-21 19:26:4079// This implements fake Google MAPS Geolocation API remote endpoint.
80class TestGeolocationAPILoaderFactory : public network::TestURLLoaderFactory {
[email protected]4e7fcd382014-05-19 22:00:3381 public:
Sergio Villar Senin5a1ed4782018-08-21 19:26:4082 TestGeolocationAPILoaderFactory(const GURL& url,
83 const std::string& response,
84 const size_t require_retries)
85 : url_(url), response_(response), require_retries_(require_retries) {
86 SetInterceptor(base::BindRepeating(
87 &TestGeolocationAPILoaderFactory::Intercept, base::Unretained(this)));
88 AddResponseWithCode(net::HTTP_INTERNAL_SERVER_ERROR);
89 }
[email protected]4e7fcd382014-05-19 22:00:3390
Sergio Villar Senin5a1ed4782018-08-21 19:26:4091 void Intercept(const network::ResourceRequest& request) {
92 EXPECT_EQ(url_, request.url);
93
94 EXPECT_NE(nullptr, provider_);
[email protected]4e7fcd382014-05-19 22:00:3395 EXPECT_EQ(provider_->requests_.size(), 1U);
96
benchan6f8ca9f2016-08-30 14:14:0997 SimpleGeolocationRequest* geolocation_request =
98 provider_->requests_[0].get();
[email protected]4e7fcd382014-05-19 22:00:3399
100 const base::TimeDelta base_retry_interval =
101 base::TimeDelta::FromMilliseconds(kRequestRetryIntervalMilliSeconds);
102 geolocation_request->set_retry_sleep_on_server_error_for_testing(
103 base_retry_interval);
104 geolocation_request->set_retry_sleep_on_bad_response_for_testing(
105 base_retry_interval);
106
Sergio Villar Senin5a1ed4782018-08-21 19:26:40107 if (++attempts_ > require_retries_)
108 AddResponseWithCode(net::OK);
[email protected]4e7fcd382014-05-19 22:00:33109 }
110
Sergio Villar Senin5a1ed4782018-08-21 19:26:40111 void SetSimpleGeolocationProvider(SimpleGeolocationProvider* provider) {
112 provider_ = provider;
[email protected]4e7fcd382014-05-19 22:00:33113 }
[email protected]4e7fcd382014-05-19 22:00:33114 size_t attempts() const { return attempts_; }
115
116 private:
Sergio Villar Senin5a1ed4782018-08-21 19:26:40117 void AddResponseWithCode(int error_code) {
118 network::ResourceResponseHead response_head;
119 response_head.headers = base::MakeRefCounted<net::HttpResponseHeaders>("");
120 response_head.headers->AddHeader("Content-Type: application/json");
121 // If AddResponse() is called multiple times for the same URL, the last
122 // one is the one used so there is no need for ClearResponses().
123 AddResponse(url_, response_head, response_,
124 network::URLLoaderCompletionStatus(error_code));
[email protected]4e7fcd382014-05-19 22:00:33125 }
126
Sergio Villar Senin5a1ed4782018-08-21 19:26:40127 GURL url_;
128 std::string response_;
129 const size_t require_retries_;
130 size_t attempts_ = 0;
131 SimpleGeolocationProvider* provider_;
[email protected]4e7fcd382014-05-19 22:00:33132
Sergio Villar Senin5a1ed4782018-08-21 19:26:40133 DISALLOW_COPY_AND_ASSIGN(TestGeolocationAPILoaderFactory);
[email protected]4e7fcd382014-05-19 22:00:33134};
135
136class GeolocationReceiver {
137 public:
138 GeolocationReceiver() : server_error_(false) {}
139
140 void OnRequestDone(const Geoposition& position,
141 bool server_error,
142 const base::TimeDelta elapsed) {
143 position_ = position;
144 server_error_ = server_error;
145 elapsed_ = elapsed;
146
147 message_loop_runner_->Quit();
148 }
149
150 void WaitUntilRequestDone() {
151 message_loop_runner_.reset(new base::RunLoop);
152 message_loop_runner_->Run();
153 }
154
155 const Geoposition& position() const { return position_; }
156 bool server_error() const { return server_error_; }
157 base::TimeDelta elapsed() const { return elapsed_; }
158
159 private:
160 Geoposition position_;
161 bool server_error_;
162 base::TimeDelta elapsed_;
dcheng0a6e80c2016-04-08 18:37:38163 std::unique_ptr<base::RunLoop> message_loop_runner_;
[email protected]4e7fcd382014-05-19 22:00:33164};
165
skylarceaf50cb2017-02-14 01:16:28166class WirelessTestMonitor : public SimpleGeolocationRequestTestMonitor {
alemate9af1ade2016-03-23 23:21:44167 public:
Chris Watkins2c529d62017-11-29 02:14:41168 WirelessTestMonitor() = default;
alemate9af1ade2016-03-23 23:21:44169
170 void OnRequestCreated(SimpleGeolocationRequest* request) override {}
171 void OnStart(SimpleGeolocationRequest* request) override {
172 last_request_body_ = request->FormatRequestBodyForTesting();
173 }
174
175 const std::string& last_request_body() const { return last_request_body_; }
176
177 private:
178 std::string last_request_body_;
179
skylarceaf50cb2017-02-14 01:16:28180 DISALLOW_COPY_AND_ASSIGN(WirelessTestMonitor);
alemate9af1ade2016-03-23 23:21:44181};
182
[email protected]4e7fcd382014-05-19 22:00:33183class SimpleGeolocationTest : public testing::Test {
184 private:
Carlos Caballerob8281a62019-06-10 20:12:22185 base::test::ScopedTaskEnvironment scoped_task_environment_;
[email protected]4e7fcd382014-05-19 22:00:33186};
187
188TEST_F(SimpleGeolocationTest, ResponseOK) {
Sergio Villar Senin5a1ed4782018-08-21 19:26:40189 TestGeolocationAPILoaderFactory url_factory(GURL(kTestGeolocationProviderUrl),
190 kSimpleResponseBody,
191 0 /* require_retries */);
192 SimpleGeolocationProvider provider(
193 base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
194 &url_factory),
195 GURL(kTestGeolocationProviderUrl));
196 url_factory.SetSimpleGeolocationProvider(&provider);
[email protected]4e7fcd382014-05-19 22:00:33197
198 GeolocationReceiver receiver;
skylarceaf50cb2017-02-14 01:16:28199 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), false, false,
[email protected]4e7fcd382014-05-19 22:00:33200 base::Bind(&GeolocationReceiver::OnRequestDone,
201 base::Unretained(&receiver)));
202 receiver.WaitUntilRequestDone();
203
alemate9af1ade2016-03-23 23:21:44204 EXPECT_EQ(kExpectedPosition, receiver.position().ToString());
[email protected]4e7fcd382014-05-19 22:00:33205 EXPECT_FALSE(receiver.server_error());
206 EXPECT_EQ(1U, url_factory.attempts());
207}
208
209TEST_F(SimpleGeolocationTest, ResponseOKWithRetries) {
Sergio Villar Senin5a1ed4782018-08-21 19:26:40210 TestGeolocationAPILoaderFactory url_factory(GURL(kTestGeolocationProviderUrl),
211 kSimpleResponseBody,
212 3 /* require_retries */);
[email protected]4e7fcd382014-05-19 22:00:33213
Sergio Villar Senin5a1ed4782018-08-21 19:26:40214 SimpleGeolocationProvider provider(
215 base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
216 &url_factory),
217 GURL(kTestGeolocationProviderUrl));
218 url_factory.SetSimpleGeolocationProvider(&provider);
[email protected]4e7fcd382014-05-19 22:00:33219
220 GeolocationReceiver receiver;
skylarceaf50cb2017-02-14 01:16:28221 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), false, false,
[email protected]4e7fcd382014-05-19 22:00:33222 base::Bind(&GeolocationReceiver::OnRequestDone,
223 base::Unretained(&receiver)));
224 receiver.WaitUntilRequestDone();
alemate9af1ade2016-03-23 23:21:44225 EXPECT_EQ(kExpectedPosition, receiver.position().ToString());
[email protected]4e7fcd382014-05-19 22:00:33226 EXPECT_FALSE(receiver.server_error());
227 EXPECT_EQ(4U, url_factory.attempts());
228}
229
230TEST_F(SimpleGeolocationTest, InvalidResponse) {
Sergio Villar Senin5a1ed4782018-08-21 19:26:40231 TestGeolocationAPILoaderFactory url_factory(GURL(kTestGeolocationProviderUrl),
232 "invalid JSON string",
233 0 /* require_retries */);
234 SimpleGeolocationProvider provider(
235 base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
236 &url_factory),
237 GURL(kTestGeolocationProviderUrl));
238 url_factory.SetSimpleGeolocationProvider(&provider);
[email protected]4e7fcd382014-05-19 22:00:33239
240 GeolocationReceiver receiver;
[email protected]8fafd872014-05-23 18:31:32241
[email protected]4e7fcd382014-05-19 22:00:33242 const int timeout_seconds = 1;
[email protected]8fafd872014-05-23 18:31:32243 size_t expected_retries = static_cast<size_t>(
244 timeout_seconds * 1000 / kRequestRetryIntervalMilliSeconds);
245 ASSERT_GE(expected_retries, 2U);
246
[email protected]4e7fcd382014-05-19 22:00:33247 provider.RequestGeolocation(base::TimeDelta::FromSeconds(timeout_seconds),
skylarceaf50cb2017-02-14 01:16:28248 false, false,
[email protected]4e7fcd382014-05-19 22:00:33249 base::Bind(&GeolocationReceiver::OnRequestDone,
250 base::Unretained(&receiver)));
251 receiver.WaitUntilRequestDone();
252
253 EXPECT_EQ(
254 "latitude=200.000000, longitude=200.000000, accuracy=-1.000000, "
255 "error_code=0, error_message='SimpleGeolocation provider at "
256 "'https://localhost/' : JSONReader failed: Line: 1, column: 1, "
257 "Unexpected token..', status=4 (TIMEOUT)",
258 receiver.position().ToString());
259 EXPECT_TRUE(receiver.server_error());
[email protected]8fafd872014-05-23 18:31:32260 EXPECT_GE(url_factory.attempts(), 2U);
261 if (url_factory.attempts() > expected_retries + 1) {
262 LOG(WARNING)
263 << "SimpleGeolocationTest::InvalidResponse: Too many attempts ("
thakise5b57862016-07-18 19:23:19264 << url_factory.attempts() << "), no more than " << expected_retries + 1
[email protected]8fafd872014-05-23 18:31:32265 << " expected.";
266 }
267 if (url_factory.attempts() < expected_retries - 1) {
268 LOG(WARNING)
269 << "SimpleGeolocationTest::InvalidResponse: Too little attempts ("
thakise5b57862016-07-18 19:23:19270 << url_factory.attempts() << "), greater than " << expected_retries - 1
[email protected]8fafd872014-05-23 18:31:32271 << " expected.";
272 }
[email protected]4e7fcd382014-05-19 22:00:33273}
274
alemate9af1ade2016-03-23 23:21:44275TEST_F(SimpleGeolocationTest, NoWiFi) {
Steven Bennettsb4bf6cd2019-04-05 17:02:57276 shill_clients::InitializeFakes();
alemate9af1ade2016-03-23 23:21:44277 NetworkHandler::Initialize();
278
skylarceaf50cb2017-02-14 01:16:28279 WirelessTestMonitor requests_monitor;
alemate9af1ade2016-03-23 23:21:44280 SimpleGeolocationRequest::SetTestMonitor(&requests_monitor);
281
Sergio Villar Senin5a1ed4782018-08-21 19:26:40282 TestGeolocationAPILoaderFactory url_factory(GURL(kTestGeolocationProviderUrl),
283 kSimpleResponseBody,
284 0 /* require_retries */);
285 SimpleGeolocationProvider provider(
286 base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
287 &url_factory),
288 GURL(kTestGeolocationProviderUrl));
289 url_factory.SetSimpleGeolocationProvider(&provider);
alemate9af1ade2016-03-23 23:21:44290
291 GeolocationReceiver receiver;
skylarceaf50cb2017-02-14 01:16:28292 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), true, false,
alemate9af1ade2016-03-23 23:21:44293 base::Bind(&GeolocationReceiver::OnRequestDone,
294 base::Unretained(&receiver)));
295 receiver.WaitUntilRequestDone();
296 EXPECT_EQ(kIPOnlyRequestBody, requests_monitor.last_request_body());
297
298 EXPECT_EQ(kExpectedPosition, receiver.position().ToString());
299 EXPECT_FALSE(receiver.server_error());
300 EXPECT_EQ(1U, url_factory.attempts());
301
302 NetworkHandler::Shutdown();
Steven Bennettsb4bf6cd2019-04-05 17:02:57303 shill_clients::Shutdown();
alemate9af1ade2016-03-23 23:21:44304}
305
skylarceaf50cb2017-02-14 01:16:28306// Test sending of WiFi Access points and Cell Towers.
alemate9af1ade2016-03-23 23:21:44307// (This is mostly derived from GeolocationHandlerTest.)
skylarceaf50cb2017-02-14 01:16:28308class SimpleGeolocationWirelessTest : public ::testing::TestWithParam<bool> {
alemate9af1ade2016-03-23 23:21:44309 public:
skylarceaf50cb2017-02-14 01:16:28310 SimpleGeolocationWirelessTest() : manager_test_(nullptr) {}
alemate9af1ade2016-03-23 23:21:44311
Chris Watkins2c529d62017-11-29 02:14:41312 ~SimpleGeolocationWirelessTest() override = default;
alemate9af1ade2016-03-23 23:21:44313
314 void SetUp() override {
Steven Bennettsb4bf6cd2019-04-05 17:02:57315 shill_clients::InitializeFakes();
alemate9af1ade2016-03-23 23:21:44316 // Get the test interface for manager / device.
Steven Bennettsb4bf6cd2019-04-05 17:02:57317 manager_test_ = ShillManagerClient::Get()->GetTestInterface();
alemate9af1ade2016-03-23 23:21:44318 ASSERT_TRUE(manager_test_);
319 geolocation_handler_.reset(new GeolocationHandler());
320 geolocation_handler_->Init();
fdorayf5b47fd12016-09-13 14:12:36321 base::RunLoop().RunUntilIdle();
alemate9af1ade2016-03-23 23:21:44322 }
323
324 void TearDown() override {
325 geolocation_handler_.reset();
Steven Bennettsb4bf6cd2019-04-05 17:02:57326 shill_clients::Shutdown();
alemate9af1ade2016-03-23 23:21:44327 }
328
329 bool GetWifiAccessPoints() {
330 return geolocation_handler_->GetWifiAccessPoints(&wifi_access_points_,
331 nullptr);
332 }
333
skylarceaf50cb2017-02-14 01:16:28334 bool GetCellTowers() {
335 return geolocation_handler_->GetNetworkInformation(nullptr, &cell_towers_);
336 }
337
338 // This should remain in sync with the format of shill (chromeos) dict entries
alemate9af1ade2016-03-23 23:21:44339 void AddAccessPoint(int idx) {
340 base::DictionaryValue properties;
341 std::string mac_address =
342 base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X", idx, 0, 0, 0, 0, 0);
Raul Tambred252a662019-02-11 20:29:42343 std::string channel = base::NumberToString(idx);
344 std::string strength = base::NumberToString(idx * 10);
jdoerriefbb03dd2017-08-17 14:43:26345 properties.SetKey(shill::kGeoMacAddressProperty, base::Value(mac_address));
346 properties.SetKey(shill::kGeoChannelProperty, base::Value(channel));
347 properties.SetKey(shill::kGeoSignalStrengthProperty, base::Value(strength));
skylarceaf50cb2017-02-14 01:16:28348 manager_test_->AddGeoNetwork(shill::kGeoWifiAccessPointsProperty,
349 properties);
350 base::RunLoop().RunUntilIdle();
351 }
352
353 // This should remain in sync with the format of shill (chromeos) dict entries
354 void AddCellTower(int idx) {
355 base::DictionaryValue properties;
Raul Tambred252a662019-02-11 20:29:42356 std::string ci = base::NumberToString(idx);
357 std::string lac = base::NumberToString(idx * 3);
358 std::string mcc = base::NumberToString(idx * 100);
359 std::string mnc = base::NumberToString(idx * 100 + 1);
skylarceaf50cb2017-02-14 01:16:28360
jdoerriefbb03dd2017-08-17 14:43:26361 properties.SetKey(shill::kGeoCellIdProperty, base::Value(ci));
362 properties.SetKey(shill::kGeoLocationAreaCodeProperty, base::Value(lac));
363 properties.SetKey(shill::kGeoMobileCountryCodeProperty, base::Value(mcc));
364 properties.SetKey(shill::kGeoMobileNetworkCodeProperty, base::Value(mnc));
skylarceaf50cb2017-02-14 01:16:28365
366 manager_test_->AddGeoNetwork(shill::kGeoCellTowersProperty, properties);
fdorayf5b47fd12016-09-13 14:12:36367 base::RunLoop().RunUntilIdle();
alemate9af1ade2016-03-23 23:21:44368 }
369
370 protected:
Carlos Caballerof17e6e22019-07-27 03:10:44371 base::test::ScopedTaskEnvironment scoped_task_environment_{
372 base::test::ScopedTaskEnvironment::MainThreadType::UI};
dcheng0a6e80c2016-04-08 18:37:38373 std::unique_ptr<GeolocationHandler> geolocation_handler_;
alemate9af1ade2016-03-23 23:21:44374 ShillManagerClient::TestInterface* manager_test_;
375 WifiAccessPointVector wifi_access_points_;
skylarceaf50cb2017-02-14 01:16:28376 CellTowerVector cell_towers_;
alemate9af1ade2016-03-23 23:21:44377
378 private:
skylarceaf50cb2017-02-14 01:16:28379 DISALLOW_COPY_AND_ASSIGN(SimpleGeolocationWirelessTest);
alemate9af1ade2016-03-23 23:21:44380};
381
382// Parameter is enable/disable sending of WiFi data.
skylarceaf50cb2017-02-14 01:16:28383TEST_P(SimpleGeolocationWirelessTest, WiFiExists) {
alemate9af1ade2016-03-23 23:21:44384 NetworkHandler::Initialize();
385
skylarceaf50cb2017-02-14 01:16:28386 WirelessTestMonitor requests_monitor;
alemate9af1ade2016-03-23 23:21:44387 SimpleGeolocationRequest::SetTestMonitor(&requests_monitor);
388
Sergio Villar Senin5a1ed4782018-08-21 19:26:40389 TestGeolocationAPILoaderFactory url_factory(GURL(kTestGeolocationProviderUrl),
390 kSimpleResponseBody,
391 0 /* require_retries */);
392 SimpleGeolocationProvider provider(
393 base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
394 &url_factory),
395 GURL(kTestGeolocationProviderUrl));
396 url_factory.SetSimpleGeolocationProvider(&provider);
Jun Mukaidf9e5f7d2019-01-07 23:47:06397 provider.set_geolocation_handler(geolocation_handler_.get());
alemate9af1ade2016-03-23 23:21:44398 {
399 GeolocationReceiver receiver;
400 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), GetParam(),
skylarceaf50cb2017-02-14 01:16:28401 false,
alemate9af1ade2016-03-23 23:21:44402 base::Bind(&GeolocationReceiver::OnRequestDone,
403 base::Unretained(&receiver)));
404 receiver.WaitUntilRequestDone();
405 EXPECT_EQ(kIPOnlyRequestBody, requests_monitor.last_request_body());
406
407 EXPECT_EQ(kExpectedPosition, receiver.position().ToString());
408 EXPECT_FALSE(receiver.server_error());
409 EXPECT_EQ(1U, url_factory.attempts());
410 }
411
skylarceaf50cb2017-02-14 01:16:28412 // Add cell and wifi to ensure only wifi is sent when cellular disabled.
alemate9af1ade2016-03-23 23:21:44413 AddAccessPoint(1);
skylarceaf50cb2017-02-14 01:16:28414 AddCellTower(1);
fdorayf5b47fd12016-09-13 14:12:36415 base::RunLoop().RunUntilIdle();
skylarceaf50cb2017-02-14 01:16:28416 // Initial call should return false and request access points.
alemate9af1ade2016-03-23 23:21:44417 EXPECT_FALSE(GetWifiAccessPoints());
fdorayf5b47fd12016-09-13 14:12:36418 base::RunLoop().RunUntilIdle();
alemate9af1ade2016-03-23 23:21:44419 // Second call should return true since we have an access point.
420 EXPECT_TRUE(GetWifiAccessPoints());
421 ASSERT_EQ(1u, wifi_access_points_.size());
422 EXPECT_EQ(kWiFiAP1MacAddress, wifi_access_points_[0].mac_address);
423 EXPECT_EQ(1, wifi_access_points_[0].channel);
424
425 {
426 GeolocationReceiver receiver;
427 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), GetParam(),
skylarceaf50cb2017-02-14 01:16:28428 false,
alemate9af1ade2016-03-23 23:21:44429 base::Bind(&GeolocationReceiver::OnRequestDone,
430 base::Unretained(&receiver)));
431 receiver.WaitUntilRequestDone();
432 if (GetParam()) {
433 // Sending WiFi data is enabled.
434 EXPECT_EQ(kOneWiFiAPRequestBody, requests_monitor.last_request_body());
435 } else {
436 // Sending WiFi data is disabled.
437 EXPECT_EQ(kIPOnlyRequestBody, requests_monitor.last_request_body());
438 }
439
440 EXPECT_EQ(kExpectedPosition, receiver.position().ToString());
441 EXPECT_FALSE(receiver.server_error());
442 // This is total.
443 EXPECT_EQ(2U, url_factory.attempts());
444 }
445 NetworkHandler::Shutdown();
446}
447
448// This test verifies that WiFi data is sent only if sending was requested.
Victor Costanfdc907e82019-01-28 18:10:40449INSTANTIATE_TEST_SUITE_P(EnableDisableSendingWifiData,
450 SimpleGeolocationWirelessTest,
451 testing::Bool());
alemate9af1ade2016-03-23 23:21:44452
skylarceaf50cb2017-02-14 01:16:28453TEST_P(SimpleGeolocationWirelessTest, CellularExists) {
454 NetworkHandler::Initialize();
455
456 WirelessTestMonitor requests_monitor;
457 SimpleGeolocationRequest::SetTestMonitor(&requests_monitor);
458
Sergio Villar Senin5a1ed4782018-08-21 19:26:40459 TestGeolocationAPILoaderFactory url_factory(GURL(kTestGeolocationProviderUrl),
460 kSimpleResponseBody,
461 0 /* require_retries */);
462 SimpleGeolocationProvider provider(
463 base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
464 &url_factory),
465 GURL(kTestGeolocationProviderUrl));
466 url_factory.SetSimpleGeolocationProvider(&provider);
Jun Mukaidf9e5f7d2019-01-07 23:47:06467 provider.set_geolocation_handler(geolocation_handler_.get());
skylarceaf50cb2017-02-14 01:16:28468 {
469 GeolocationReceiver receiver;
470 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), false,
471 GetParam(),
472 base::Bind(&GeolocationReceiver::OnRequestDone,
473 base::Unretained(&receiver)));
474 receiver.WaitUntilRequestDone();
475 EXPECT_EQ(kIPOnlyRequestBody, requests_monitor.last_request_body());
476
477 EXPECT_EQ(kExpectedPosition, receiver.position().ToString());
478 EXPECT_FALSE(receiver.server_error());
479 EXPECT_EQ(1U, url_factory.attempts());
480 }
481
482 AddCellTower(1);
483 base::RunLoop().RunUntilIdle();
484 // Initial call should return false and request cell towers.
485 EXPECT_FALSE(GetCellTowers());
486 base::RunLoop().RunUntilIdle();
487 // Second call should return true since we have a tower.
488 EXPECT_TRUE(GetCellTowers());
489 ASSERT_EQ(1u, cell_towers_.size());
490 EXPECT_EQ(kCellTower1MNC, cell_towers_[0].mnc);
Raul Tambred252a662019-02-11 20:29:42491 EXPECT_EQ(base::NumberToString(1), cell_towers_[0].ci);
skylarceaf50cb2017-02-14 01:16:28492
493 {
494 GeolocationReceiver receiver;
495 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), false,
496 GetParam(),
497 base::Bind(&GeolocationReceiver::OnRequestDone,
498 base::Unretained(&receiver)));
499 receiver.WaitUntilRequestDone();
500 if (GetParam()) {
501 // Sending Cellular data is enabled.
502 EXPECT_EQ(kOneCellTowerRequestBody, requests_monitor.last_request_body());
503 } else {
504 // Sending Cellular data is disabled.
505 EXPECT_EQ(kIPOnlyRequestBody, requests_monitor.last_request_body());
506 }
507
508 EXPECT_EQ(kExpectedPosition, receiver.position().ToString());
509 EXPECT_FALSE(receiver.server_error());
510 // This is total.
511 EXPECT_EQ(2U, url_factory.attempts());
512 }
513 NetworkHandler::Shutdown();
514}
515
[email protected]4e7fcd382014-05-19 22:00:33516} // namespace chromeos