Chromium Code Reviews| OLD | NEW |
|---|---|
| 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" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "chromeos/dbus/dbus_thread_manager.h" | 12 #include "chromeos/dbus/dbus_thread_manager.h" |
| 13 #include "chromeos/dbus/shill_manager_client.h" | 13 #include "chromeos/dbus/shill_manager_client.h" |
| 14 #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" | 15 #include "chromeos/geolocation/simple_geolocation_request_test_monitor.h" |
| 16 #include "chromeos/network/geolocation_handler.h" | 16 #include "chromeos/network/geolocation_handler.h" |
| 17 #include "chromeos/network/network_handler.h" | 17 #include "chromeos/network/network_handler.h" |
| 18 #include "net/http/http_response_headers.h" | 18 #include "net/http/http_response_headers.h" |
| 19 #include "net/http/http_status_code.h" | 19 #include "net/http/http_status_code.h" |
| 20 #include "net/url_request/test_url_fetcher_factory.h" | 20 #include "net/url_request/test_url_fetcher_factory.h" |
| 21 #include "net/url_request/url_fetcher_impl.h" | 21 #include "net/url_request/url_fetcher_impl.h" |
| 22 #include "net/url_request/url_request_status.h" | 22 #include "net/url_request/url_request_status.h" |
| 23 #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" | 24 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 const int kRequestRetryIntervalMilliSeconds = 200; | 28 constexpr int kRequestRetryIntervalMilliSeconds = 200; |
| 29 | 29 |
| 30 // This should be different from default to prevent SimpleGeolocationRequest | 30 // This should be different from default to prevent SimpleGeolocationRequest |
| 31 // from modifying it. | 31 // from modifying it. |
| 32 const char kTestGeolocationProviderUrl[] = | 32 constexpr char kTestGeolocationProviderUrl[] = |
| 33 "https://localhost/geolocation/v1/geolocate?"; | 33 "https://localhost/geolocation/v1/geolocate?"; |
| 34 | 34 |
| 35 const char kSimpleResponseBody[] = | 35 constexpr char kSimpleResponseBody[] = |
| 36 "{\n" | 36 "{\n" |
| 37 " \"location\": {\n" | 37 " \"location\": {\n" |
| 38 " \"lat\": 51.0,\n" | 38 " \"lat\": 51.0,\n" |
| 39 " \"lng\": -0.1\n" | 39 " \"lng\": -0.1\n" |
| 40 " },\n" | 40 " },\n" |
| 41 " \"accuracy\": 1200.4\n" | 41 " \"accuracy\": 1200.4\n" |
| 42 "}"; | 42 "}"; |
| 43 const char kIPOnlyRequestBody[] = "{\"considerIp\": \"true\"}"; | 43 constexpr char kIPOnlyRequestBody[] = "{\"considerIp\": \"true\"}"; |
| 44 const char kOneWiFiAPRequestBody[] = | 44 constexpr char kOneWiFiAPRequestBody[] = |
| 45 "{" | 45 "{" |
| 46 "\"considerIp\":true," | 46 "\"considerIp\":true," |
| 47 "\"wifiAccessPoints\":[" | 47 "\"wifiAccessPoints\":[" |
| 48 "{" | 48 "{" |
| 49 "\"channel\":1," | 49 "\"channel\":1," |
| 50 "\"macAddress\":\"01:00:00:00:00:00\"," | 50 "\"macAddress\":\"01:00:00:00:00:00\"," |
| 51 "\"signalStrength\":10," | 51 "\"signalStrength\":10," |
| 52 "\"signalToNoiseRatio\":0" | 52 "\"signalToNoiseRatio\":0" |
| 53 "}" | 53 "}" |
| 54 "]" | 54 "]" |
| 55 "}"; | 55 "}"; |
| 56 const char kExpectedPosition[] = | 56 constexpr char kOneCellTowerRequestBody[] = |
| 57 "{" | |
| 58 "\"cellTowers\":[" | |
| 59 "{" | |
| 60 "\"cellId\":\"1\"," | |
| 61 "\"locationAreaCode\":\"10\"," | |
| 62 "\"mobileCountryCode\":\"100\"," | |
| 63 "\"mobileNetworkCode\":\"101\"" | |
| 64 "}" | |
| 65 "]," | |
| 66 "\"considerIp\":true" | |
| 67 "}"; | |
| 68 constexpr char kExpectedPosition[] = | |
| 57 "latitude=51.000000, longitude=-0.100000, accuracy=1200.400000, " | 69 "latitude=51.000000, longitude=-0.100000, accuracy=1200.400000, " |
| 58 "error_code=0, error_message='', status=1 (OK)"; | 70 "error_code=0, error_message='', status=1 (OK)"; |
| 59 | 71 |
| 60 const char kWiFiAP1MacAddress[] = "01:00:00:00:00:00"; | 72 constexpr char kWiFiAP1MacAddress[] = "01:00:00:00:00:00"; |
| 73 constexpr char kCellTower1MNC[] = "101"; | |
| 61 } // anonymous namespace | 74 } // anonymous namespace |
| 62 | 75 |
| 63 namespace chromeos { | 76 namespace chromeos { |
| 64 | 77 |
| 65 // This is helper class for net::FakeURLFetcherFactory. | 78 // This is helper class for net::FakeURLFetcherFactory. |
| 66 class TestGeolocationAPIURLFetcherCallback { | 79 class TestGeolocationAPIURLFetcherCallback { |
| 67 public: | 80 public: |
| 68 TestGeolocationAPIURLFetcherCallback(const GURL& url, | 81 TestGeolocationAPIURLFetcherCallback(const GURL& url, |
| 69 const size_t require_retries, | 82 const size_t require_retries, |
| 70 const std::string& response, | 83 const std::string& response, |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 182 bool server_error() const { return server_error_; } | 195 bool server_error() const { return server_error_; } |
| 183 base::TimeDelta elapsed() const { return elapsed_; } | 196 base::TimeDelta elapsed() const { return elapsed_; } |
| 184 | 197 |
| 185 private: | 198 private: |
| 186 Geoposition position_; | 199 Geoposition position_; |
| 187 bool server_error_; | 200 bool server_error_; |
| 188 base::TimeDelta elapsed_; | 201 base::TimeDelta elapsed_; |
| 189 std::unique_ptr<base::RunLoop> message_loop_runner_; | 202 std::unique_ptr<base::RunLoop> message_loop_runner_; |
| 190 }; | 203 }; |
| 191 | 204 |
| 192 class WiFiTestMonitor : public SimpleGeolocationRequestTestMonitor { | 205 class WirelessTestMonitor : public SimpleGeolocationRequestTestMonitor { |
| 193 public: | 206 public: |
| 194 WiFiTestMonitor() {} | 207 WirelessTestMonitor() {} |
| 195 | 208 |
| 196 void OnRequestCreated(SimpleGeolocationRequest* request) override {} | 209 void OnRequestCreated(SimpleGeolocationRequest* request) override {} |
| 197 void OnStart(SimpleGeolocationRequest* request) override { | 210 void OnStart(SimpleGeolocationRequest* request) override { |
| 198 last_request_body_ = request->FormatRequestBodyForTesting(); | 211 last_request_body_ = request->FormatRequestBodyForTesting(); |
| 199 } | 212 } |
| 200 | 213 |
| 201 const std::string& last_request_body() const { return last_request_body_; } | 214 const std::string& last_request_body() const { return last_request_body_; } |
| 202 | 215 |
| 203 private: | 216 private: |
| 204 std::string last_request_body_; | 217 std::string last_request_body_; |
| 205 | 218 |
| 206 DISALLOW_COPY_AND_ASSIGN(WiFiTestMonitor); | 219 DISALLOW_COPY_AND_ASSIGN(WirelessTestMonitor); |
| 207 }; | 220 }; |
| 208 | 221 |
| 209 class SimpleGeolocationTest : public testing::Test { | 222 class SimpleGeolocationTest : public testing::Test { |
| 210 private: | 223 private: |
| 211 base::MessageLoop message_loop_; | 224 base::MessageLoop message_loop_; |
| 212 }; | 225 }; |
| 213 | 226 |
| 214 TEST_F(SimpleGeolocationTest, ResponseOK) { | 227 TEST_F(SimpleGeolocationTest, ResponseOK) { |
| 215 SimpleGeolocationProvider provider(nullptr, | 228 SimpleGeolocationProvider provider(nullptr, |
| 216 GURL(kTestGeolocationProviderUrl)); | 229 GURL(kTestGeolocationProviderUrl)); |
| 217 | 230 |
| 218 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl), | 231 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl), |
| 219 std::string(kSimpleResponseBody), | 232 std::string(kSimpleResponseBody), |
| 220 0 /* require_retries */, | 233 0 /* require_retries */, |
| 221 &provider); | 234 &provider); |
| 222 | 235 |
| 223 GeolocationReceiver receiver; | 236 GeolocationReceiver receiver; |
| 224 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), false, | 237 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), false, false, |
| 225 base::Bind(&GeolocationReceiver::OnRequestDone, | 238 base::Bind(&GeolocationReceiver::OnRequestDone, |
| 226 base::Unretained(&receiver))); | 239 base::Unretained(&receiver))); |
| 227 receiver.WaitUntilRequestDone(); | 240 receiver.WaitUntilRequestDone(); |
| 228 | 241 |
| 229 EXPECT_EQ(kExpectedPosition, receiver.position().ToString()); | 242 EXPECT_EQ(kExpectedPosition, receiver.position().ToString()); |
| 230 EXPECT_FALSE(receiver.server_error()); | 243 EXPECT_FALSE(receiver.server_error()); |
| 231 EXPECT_EQ(1U, url_factory.attempts()); | 244 EXPECT_EQ(1U, url_factory.attempts()); |
| 232 } | 245 } |
| 233 | 246 |
| 234 TEST_F(SimpleGeolocationTest, ResponseOKWithRetries) { | 247 TEST_F(SimpleGeolocationTest, ResponseOKWithRetries) { |
| 235 SimpleGeolocationProvider provider(nullptr, | 248 SimpleGeolocationProvider provider(nullptr, |
| 236 GURL(kTestGeolocationProviderUrl)); | 249 GURL(kTestGeolocationProviderUrl)); |
| 237 | 250 |
| 238 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl), | 251 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl), |
| 239 std::string(kSimpleResponseBody), | 252 std::string(kSimpleResponseBody), |
| 240 3 /* require_retries */, | 253 3 /* require_retries */, |
| 241 &provider); | 254 &provider); |
| 242 | 255 |
| 243 GeolocationReceiver receiver; | 256 GeolocationReceiver receiver; |
| 244 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), false, | 257 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), false, false, |
| 245 base::Bind(&GeolocationReceiver::OnRequestDone, | 258 base::Bind(&GeolocationReceiver::OnRequestDone, |
| 246 base::Unretained(&receiver))); | 259 base::Unretained(&receiver))); |
| 247 receiver.WaitUntilRequestDone(); | 260 receiver.WaitUntilRequestDone(); |
| 248 EXPECT_EQ(kExpectedPosition, receiver.position().ToString()); | 261 EXPECT_EQ(kExpectedPosition, receiver.position().ToString()); |
| 249 EXPECT_FALSE(receiver.server_error()); | 262 EXPECT_FALSE(receiver.server_error()); |
| 250 EXPECT_EQ(4U, url_factory.attempts()); | 263 EXPECT_EQ(4U, url_factory.attempts()); |
| 251 } | 264 } |
| 252 | 265 |
| 253 TEST_F(SimpleGeolocationTest, InvalidResponse) { | 266 TEST_F(SimpleGeolocationTest, InvalidResponse) { |
| 254 SimpleGeolocationProvider provider(nullptr, | 267 SimpleGeolocationProvider provider(nullptr, |
| 255 GURL(kTestGeolocationProviderUrl)); | 268 GURL(kTestGeolocationProviderUrl)); |
| 256 | 269 |
| 257 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl), | 270 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl), |
| 258 "invalid JSON string", | 271 "invalid JSON string", |
| 259 0 /* require_retries */, | 272 0 /* require_retries */, |
| 260 &provider); | 273 &provider); |
| 261 | 274 |
| 262 GeolocationReceiver receiver; | 275 GeolocationReceiver receiver; |
| 263 | 276 |
| 264 const int timeout_seconds = 1; | 277 const int timeout_seconds = 1; |
| 265 size_t expected_retries = static_cast<size_t>( | 278 size_t expected_retries = static_cast<size_t>( |
| 266 timeout_seconds * 1000 / kRequestRetryIntervalMilliSeconds); | 279 timeout_seconds * 1000 / kRequestRetryIntervalMilliSeconds); |
| 267 ASSERT_GE(expected_retries, 2U); | 280 ASSERT_GE(expected_retries, 2U); |
| 268 | 281 |
| 269 provider.RequestGeolocation(base::TimeDelta::FromSeconds(timeout_seconds), | 282 provider.RequestGeolocation(base::TimeDelta::FromSeconds(timeout_seconds), |
| 270 false, | 283 false, false, |
| 271 base::Bind(&GeolocationReceiver::OnRequestDone, | 284 base::Bind(&GeolocationReceiver::OnRequestDone, |
| 272 base::Unretained(&receiver))); | 285 base::Unretained(&receiver))); |
| 273 receiver.WaitUntilRequestDone(); | 286 receiver.WaitUntilRequestDone(); |
| 274 | 287 |
| 275 EXPECT_EQ( | 288 EXPECT_EQ( |
| 276 "latitude=200.000000, longitude=200.000000, accuracy=-1.000000, " | 289 "latitude=200.000000, longitude=200.000000, accuracy=-1.000000, " |
| 277 "error_code=0, error_message='SimpleGeolocation provider at " | 290 "error_code=0, error_message='SimpleGeolocation provider at " |
| 278 "'https://localhost/' : JSONReader failed: Line: 1, column: 1, " | 291 "'https://localhost/' : JSONReader failed: Line: 1, column: 1, " |
| 279 "Unexpected token..', status=4 (TIMEOUT)", | 292 "Unexpected token..', status=4 (TIMEOUT)", |
| 280 receiver.position().ToString()); | 293 receiver.position().ToString()); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 292 << url_factory.attempts() << "), greater than " << expected_retries - 1 | 305 << url_factory.attempts() << "), greater than " << expected_retries - 1 |
| 293 << " expected."; | 306 << " expected."; |
| 294 } | 307 } |
| 295 } | 308 } |
| 296 | 309 |
| 297 TEST_F(SimpleGeolocationTest, NoWiFi) { | 310 TEST_F(SimpleGeolocationTest, NoWiFi) { |
| 298 // This initializes DBusThreadManager and markes it "for tests only". | 311 // This initializes DBusThreadManager and markes it "for tests only". |
| 299 DBusThreadManager::GetSetterForTesting(); | 312 DBusThreadManager::GetSetterForTesting(); |
| 300 NetworkHandler::Initialize(); | 313 NetworkHandler::Initialize(); |
| 301 | 314 |
| 302 WiFiTestMonitor requests_monitor; | 315 WirelessTestMonitor requests_monitor; |
| 303 SimpleGeolocationRequest::SetTestMonitor(&requests_monitor); | 316 SimpleGeolocationRequest::SetTestMonitor(&requests_monitor); |
| 304 | 317 |
| 305 SimpleGeolocationProvider provider(nullptr, | 318 SimpleGeolocationProvider provider(nullptr, |
| 306 GURL(kTestGeolocationProviderUrl)); | 319 GURL(kTestGeolocationProviderUrl)); |
| 307 | 320 |
| 308 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl), | 321 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl), |
| 309 std::string(kSimpleResponseBody), | 322 std::string(kSimpleResponseBody), |
| 310 0 /* require_retries */, &provider); | 323 0 /* require_retries */, &provider); |
| 311 | 324 |
| 312 GeolocationReceiver receiver; | 325 GeolocationReceiver receiver; |
| 313 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), true, | 326 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), true, false, |
| 314 base::Bind(&GeolocationReceiver::OnRequestDone, | 327 base::Bind(&GeolocationReceiver::OnRequestDone, |
| 315 base::Unretained(&receiver))); | 328 base::Unretained(&receiver))); |
| 316 receiver.WaitUntilRequestDone(); | 329 receiver.WaitUntilRequestDone(); |
| 317 EXPECT_EQ(kIPOnlyRequestBody, requests_monitor.last_request_body()); | 330 EXPECT_EQ(kIPOnlyRequestBody, requests_monitor.last_request_body()); |
| 318 | 331 |
| 319 EXPECT_EQ(kExpectedPosition, receiver.position().ToString()); | 332 EXPECT_EQ(kExpectedPosition, receiver.position().ToString()); |
| 320 EXPECT_FALSE(receiver.server_error()); | 333 EXPECT_FALSE(receiver.server_error()); |
| 321 EXPECT_EQ(1U, url_factory.attempts()); | 334 EXPECT_EQ(1U, url_factory.attempts()); |
| 322 | 335 |
| 323 NetworkHandler::Shutdown(); | 336 NetworkHandler::Shutdown(); |
| 324 DBusThreadManager::Shutdown(); | 337 DBusThreadManager::Shutdown(); |
| 325 } | 338 } |
| 326 | 339 |
| 327 // Test sending of WiFi Access points. | 340 // Test sending of WiFi Access points and Cell Towers. |
| 328 // (This is mostly derived from GeolocationHandlerTest.) | 341 // (This is mostly derived from GeolocationHandlerTest.) |
| 329 class SimpleGeolocationWiFiTest : public ::testing::TestWithParam<bool> { | 342 class SimpleGeolocationWirelessTest : public ::testing::TestWithParam<bool> { |
| 330 public: | 343 public: |
| 331 SimpleGeolocationWiFiTest() : manager_test_(nullptr) {} | 344 SimpleGeolocationWirelessTest() : manager_test_(nullptr) {} |
| 332 | 345 |
| 333 ~SimpleGeolocationWiFiTest() override {} | 346 ~SimpleGeolocationWirelessTest() override {} |
| 334 | 347 |
| 335 void SetUp() override { | 348 void SetUp() override { |
| 336 // This initializes DBusThreadManager and markes it "for tests only". | 349 // This initializes DBusThreadManager and markes it "for tests only". |
| 337 DBusThreadManager::GetSetterForTesting(); | 350 DBusThreadManager::GetSetterForTesting(); |
| 338 // Get the test interface for manager / device. | 351 // Get the test interface for manager / device. |
| 339 manager_test_ = | 352 manager_test_ = |
| 340 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface(); | 353 DBusThreadManager::Get()->GetShillManagerClient()->GetTestInterface(); |
| 341 ASSERT_TRUE(manager_test_); | 354 ASSERT_TRUE(manager_test_); |
| 342 geolocation_handler_.reset(new GeolocationHandler()); | 355 geolocation_handler_.reset(new GeolocationHandler()); |
| 343 geolocation_handler_->Init(); | 356 geolocation_handler_->Init(); |
| 344 base::RunLoop().RunUntilIdle(); | 357 base::RunLoop().RunUntilIdle(); |
| 345 } | 358 } |
| 346 | 359 |
| 347 void TearDown() override { | 360 void TearDown() override { |
| 348 geolocation_handler_.reset(); | 361 geolocation_handler_.reset(); |
| 349 DBusThreadManager::Shutdown(); | 362 DBusThreadManager::Shutdown(); |
| 350 } | 363 } |
| 351 | 364 |
| 352 bool GetWifiAccessPoints() { | 365 bool GetWifiAccessPoints() { |
| 353 return geolocation_handler_->GetWifiAccessPoints(&wifi_access_points_, | 366 return geolocation_handler_->GetWifiAccessPoints(&wifi_access_points_, |
| 354 nullptr); | 367 nullptr); |
| 355 } | 368 } |
| 356 | 369 |
| 370 bool GetCellTowers() { | |
| 371 return geolocation_handler_->GetNetworkInformation(nullptr, &cell_towers_); | |
| 372 } | |
| 373 | |
| 374 // This should remain in sync with the format of shill (chromeos) dict entries | |
| 357 void AddAccessPoint(int idx) { | 375 void AddAccessPoint(int idx) { |
| 358 base::DictionaryValue properties; | 376 base::DictionaryValue properties; |
| 359 std::string mac_address = | 377 std::string mac_address = |
| 360 base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X", idx, 0, 0, 0, 0, 0); | 378 base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X", idx, 0, 0, 0, 0, 0); |
| 361 std::string channel = base::IntToString(idx); | 379 std::string channel = base::IntToString(idx); |
| 362 std::string strength = base::IntToString(idx * 10); | 380 std::string strength = base::IntToString(idx * 10); |
| 363 properties.SetStringWithoutPathExpansion(shill::kGeoMacAddressProperty, | 381 properties.SetStringWithoutPathExpansion(shill::kGeoMacAddressProperty, |
| 364 mac_address); | 382 mac_address); |
| 365 properties.SetStringWithoutPathExpansion(shill::kGeoChannelProperty, | 383 properties.SetStringWithoutPathExpansion(shill::kGeoChannelProperty, |
| 366 channel); | 384 channel); |
| 367 properties.SetStringWithoutPathExpansion(shill::kGeoSignalStrengthProperty, | 385 properties.SetStringWithoutPathExpansion(shill::kGeoSignalStrengthProperty, |
| 368 strength); | 386 strength); |
| 369 manager_test_->AddGeoNetwork(shill::kTypeWifi, properties); | 387 manager_test_->AddGeoNetwork(shill::kGeoWifiAccessPointsProperty, |
| 388 properties); | |
| 370 base::RunLoop().RunUntilIdle(); | 389 base::RunLoop().RunUntilIdle(); |
| 371 } | 390 } |
| 372 | 391 |
| 392 // This should remain in sync with the format of shill (chromeos) dict entries | |
| 393 void AddCellTower(int idx) { | |
| 394 base::DictionaryValue properties; | |
| 395 std::string ci = base::IntToString(idx); | |
| 396 std::string lac = base::IntToString(idx * 10); | |
| 397 std::string mcc = base::IntToString(idx * 100); | |
| 398 std::string mnc = base::IntToString(idx * 100 + 1); | |
| 399 | |
| 400 properties.SetStringWithoutPathExpansion(shill::kGeoCellIdProperty, ci); | |
| 401 properties.SetStringWithoutPathExpansion( | |
| 402 shill::kGeoLocationAreaCodeProperty, lac); | |
| 403 properties.SetStringWithoutPathExpansion( | |
| 404 shill::kGeoMobileCountryCodeProperty, mcc); | |
| 405 properties.SetStringWithoutPathExpansion( | |
| 406 shill::kGeoMobileNetworkCodeProperty, mnc); | |
| 407 | |
| 408 manager_test_->AddGeoNetwork(shill::kGeoCellTowersProperty, properties); | |
| 409 base::RunLoop().RunUntilIdle(); | |
| 410 } | |
| 411 | |
| 373 protected: | 412 protected: |
| 374 base::MessageLoopForUI message_loop_; | 413 base::MessageLoopForUI message_loop_; |
| 375 std::unique_ptr<GeolocationHandler> geolocation_handler_; | 414 std::unique_ptr<GeolocationHandler> geolocation_handler_; |
| 376 ShillManagerClient::TestInterface* manager_test_; | 415 ShillManagerClient::TestInterface* manager_test_; |
| 377 WifiAccessPointVector wifi_access_points_; | 416 WifiAccessPointVector wifi_access_points_; |
| 417 CellTowerVector cell_towers_; | |
| 378 | 418 |
| 379 private: | 419 private: |
| 380 DISALLOW_COPY_AND_ASSIGN(SimpleGeolocationWiFiTest); | 420 DISALLOW_COPY_AND_ASSIGN(SimpleGeolocationWirelessTest); |
| 381 }; | 421 }; |
| 382 | 422 |
| 383 // Parameter is enable/disable sending of WiFi data. | 423 // Parameter is enable/disable sending of WiFi data. |
| 384 TEST_P(SimpleGeolocationWiFiTest, WiFiExists) { | 424 TEST_P(SimpleGeolocationWirelessTest, WiFiExists) { |
| 385 NetworkHandler::Initialize(); | 425 NetworkHandler::Initialize(); |
| 386 | 426 |
| 387 WiFiTestMonitor requests_monitor; | 427 WirelessTestMonitor requests_monitor; |
| 388 SimpleGeolocationRequest::SetTestMonitor(&requests_monitor); | 428 SimpleGeolocationRequest::SetTestMonitor(&requests_monitor); |
| 389 | 429 |
| 390 SimpleGeolocationProvider provider(nullptr, | 430 SimpleGeolocationProvider provider(nullptr, |
| 391 GURL(kTestGeolocationProviderUrl)); | 431 GURL(kTestGeolocationProviderUrl)); |
| 392 | 432 |
| 393 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl), | 433 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl), |
| 394 std::string(kSimpleResponseBody), | 434 std::string(kSimpleResponseBody), |
| 395 0 /* require_retries */, &provider); | 435 0 /* require_retries */, &provider); |
| 396 { | 436 { |
| 397 GeolocationReceiver receiver; | 437 GeolocationReceiver receiver; |
| 398 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), GetParam(), | 438 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), GetParam(), |
| 439 false, | |
| 399 base::Bind(&GeolocationReceiver::OnRequestDone, | 440 base::Bind(&GeolocationReceiver::OnRequestDone, |
| 400 base::Unretained(&receiver))); | 441 base::Unretained(&receiver))); |
| 401 receiver.WaitUntilRequestDone(); | 442 receiver.WaitUntilRequestDone(); |
| 402 EXPECT_EQ(kIPOnlyRequestBody, requests_monitor.last_request_body()); | 443 EXPECT_EQ(kIPOnlyRequestBody, requests_monitor.last_request_body()); |
| 403 | 444 |
| 404 EXPECT_EQ(kExpectedPosition, receiver.position().ToString()); | 445 EXPECT_EQ(kExpectedPosition, receiver.position().ToString()); |
| 405 EXPECT_FALSE(receiver.server_error()); | 446 EXPECT_FALSE(receiver.server_error()); |
| 406 EXPECT_EQ(1U, url_factory.attempts()); | 447 EXPECT_EQ(1U, url_factory.attempts()); |
| 407 } | 448 } |
| 408 | 449 |
| 409 // Add an acces point. | 450 // Add an access point. |
| 410 AddAccessPoint(1); | 451 AddAccessPoint(1); |
| 411 base::RunLoop().RunUntilIdle(); | 452 base::RunLoop().RunUntilIdle(); |
| 412 // Inititial call should return false and request access points. | 453 // Initial call should return false and request access points. |
| 413 EXPECT_FALSE(GetWifiAccessPoints()); | 454 EXPECT_FALSE(GetWifiAccessPoints()); |
| 414 base::RunLoop().RunUntilIdle(); | 455 base::RunLoop().RunUntilIdle(); |
| 415 // Second call should return true since we have an access point. | 456 // Second call should return true since we have an access point. |
| 416 EXPECT_TRUE(GetWifiAccessPoints()); | 457 EXPECT_TRUE(GetWifiAccessPoints()); |
|
Alexander Alekseev
2017/02/08 22:43:55
Could we also Add Cell tower here and check that i
can Skylar cook
2017/02/09 19:44:30
Done.
Alexander Alekseev
2017/02/09 19:54:56
I cannot find a check itself here, like:
EXPECT
can Skylar cook
2017/02/09 19:59:35
The `EXPECT_EQ(kOneWiFiAPRequestBody, requests_mon
Alexander Alekseev
2017/02/09 20:30:39
Yes, thank you, I missed this.
| |
| 417 ASSERT_EQ(1u, wifi_access_points_.size()); | 458 ASSERT_EQ(1u, wifi_access_points_.size()); |
| 418 EXPECT_EQ(kWiFiAP1MacAddress, wifi_access_points_[0].mac_address); | 459 EXPECT_EQ(kWiFiAP1MacAddress, wifi_access_points_[0].mac_address); |
| 419 EXPECT_EQ(1, wifi_access_points_[0].channel); | 460 EXPECT_EQ(1, wifi_access_points_[0].channel); |
| 420 | 461 |
| 421 { | 462 { |
| 422 GeolocationReceiver receiver; | 463 GeolocationReceiver receiver; |
| 423 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), GetParam(), | 464 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), GetParam(), |
| 465 false, | |
| 424 base::Bind(&GeolocationReceiver::OnRequestDone, | 466 base::Bind(&GeolocationReceiver::OnRequestDone, |
| 425 base::Unretained(&receiver))); | 467 base::Unretained(&receiver))); |
| 426 receiver.WaitUntilRequestDone(); | 468 receiver.WaitUntilRequestDone(); |
| 427 if (GetParam()) { | 469 if (GetParam()) { |
| 428 // Sending WiFi data is enabled. | 470 // Sending WiFi data is enabled. |
| 429 EXPECT_EQ(kOneWiFiAPRequestBody, requests_monitor.last_request_body()); | 471 EXPECT_EQ(kOneWiFiAPRequestBody, requests_monitor.last_request_body()); |
| 430 } else { | 472 } else { |
| 431 // Sending WiFi data is disabled. | 473 // Sending WiFi data is disabled. |
| 432 EXPECT_EQ(kIPOnlyRequestBody, requests_monitor.last_request_body()); | 474 EXPECT_EQ(kIPOnlyRequestBody, requests_monitor.last_request_body()); |
| 433 } | 475 } |
| 434 | 476 |
| 435 EXPECT_EQ(kExpectedPosition, receiver.position().ToString()); | 477 EXPECT_EQ(kExpectedPosition, receiver.position().ToString()); |
| 436 EXPECT_FALSE(receiver.server_error()); | 478 EXPECT_FALSE(receiver.server_error()); |
| 437 // This is total. | 479 // This is total. |
| 438 EXPECT_EQ(2U, url_factory.attempts()); | 480 EXPECT_EQ(2U, url_factory.attempts()); |
| 439 } | 481 } |
| 440 NetworkHandler::Shutdown(); | 482 NetworkHandler::Shutdown(); |
| 441 } | 483 } |
| 442 | 484 |
| 443 // This test verifies that WiFi data is sent only if sending was requested. | 485 // This test verifies that WiFi data is sent only if sending was requested. |
| 444 INSTANTIATE_TEST_CASE_P(EnableDisableSendingWifiData, | 486 INSTANTIATE_TEST_CASE_P(EnableDisableSendingWifiData, |
| 445 SimpleGeolocationWiFiTest, | 487 SimpleGeolocationWirelessTest, |
| 446 testing::Bool()); | 488 testing::Bool()); |
| 447 | 489 |
| 490 TEST_P(SimpleGeolocationWirelessTest, CellularExists) { | |
| 491 NetworkHandler::Initialize(); | |
| 492 | |
| 493 WirelessTestMonitor requests_monitor; | |
| 494 SimpleGeolocationRequest::SetTestMonitor(&requests_monitor); | |
| 495 | |
| 496 SimpleGeolocationProvider provider(nullptr, | |
| 497 GURL(kTestGeolocationProviderUrl)); | |
| 498 | |
| 499 GeolocationAPIFetcherFactory url_factory(GURL(kTestGeolocationProviderUrl), | |
| 500 std::string(kSimpleResponseBody), | |
| 501 0 /* require_retries */, &provider); | |
| 502 { | |
| 503 GeolocationReceiver receiver; | |
| 504 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), false, | |
| 505 GetParam(), | |
| 506 base::Bind(&GeolocationReceiver::OnRequestDone, | |
| 507 base::Unretained(&receiver))); | |
| 508 receiver.WaitUntilRequestDone(); | |
| 509 EXPECT_EQ(kIPOnlyRequestBody, requests_monitor.last_request_body()); | |
| 510 | |
| 511 EXPECT_EQ(kExpectedPosition, receiver.position().ToString()); | |
| 512 EXPECT_FALSE(receiver.server_error()); | |
| 513 EXPECT_EQ(1U, url_factory.attempts()); | |
| 514 } | |
| 515 | |
| 516 // Add a cell tower. | |
| 517 AddCellTower(1); | |
| 518 base::RunLoop().RunUntilIdle(); | |
| 519 // Initial call should return false and request cell towers. | |
| 520 EXPECT_FALSE(GetCellTowers()); | |
| 521 base::RunLoop().RunUntilIdle(); | |
| 522 // Second call should return true since we have a tower. | |
| 523 EXPECT_TRUE(GetCellTowers()); | |
| 524 ASSERT_EQ(1u, cell_towers_.size()); | |
| 525 EXPECT_EQ(kCellTower1MNC, cell_towers_[0].mnc); | |
| 526 EXPECT_EQ(base::IntToString(1), cell_towers_[0].ci); | |
| 527 | |
| 528 { | |
| 529 GeolocationReceiver receiver; | |
| 530 provider.RequestGeolocation(base::TimeDelta::FromSeconds(1), false, | |
| 531 GetParam(), | |
| 532 base::Bind(&GeolocationReceiver::OnRequestDone, | |
| 533 base::Unretained(&receiver))); | |
| 534 receiver.WaitUntilRequestDone(); | |
| 535 if (GetParam()) { | |
| 536 // Sending Cellular data is enabled. | |
| 537 EXPECT_EQ(kOneCellTowerRequestBody, requests_monitor.last_request_body()); | |
| 538 } else { | |
| 539 // Sending Cellular data is disabled. | |
| 540 EXPECT_EQ(kIPOnlyRequestBody, requests_monitor.last_request_body()); | |
| 541 } | |
| 542 | |
| 543 EXPECT_EQ(kExpectedPosition, receiver.position().ToString()); | |
| 544 EXPECT_FALSE(receiver.server_error()); | |
| 545 // This is total. | |
| 546 EXPECT_EQ(2U, url_factory.attempts()); | |
| 547 } | |
| 548 NetworkHandler::Shutdown(); | |
| 549 } | |
| 550 | |
| 448 } // namespace chromeos | 551 } // namespace chromeos |
| OLD | NEW |