| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/geolocation/network_location_provider.h" | 5 #include "chrome/browser/geolocation/network_location_provider.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/scoped_ptr.h" | 8 #include "base/scoped_ptr.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 template<typename DataType> | 99 template<typename DataType> |
| 100 MockDeviceDataProviderImpl<DataType>* | 100 MockDeviceDataProviderImpl<DataType>* |
| 101 MockDeviceDataProviderImpl<DataType>::instance_ = NULL; | 101 MockDeviceDataProviderImpl<DataType>::instance_ = NULL; |
| 102 | 102 |
| 103 // Main test fixture | 103 // Main test fixture |
| 104 class GeolocationNetworkProviderTest : public testing::Test { | 104 class GeolocationNetworkProviderTest : public testing::Test { |
| 105 public: | 105 public: |
| 106 virtual void SetUp() { | 106 virtual void SetUp() { |
| 107 URLFetcher::set_factory(&url_fetcher_factory_); | 107 URLFetcher::set_factory(&url_fetcher_factory_); |
| 108 access_token_store_ = new FakeAccessTokenStore; | 108 access_token_store_ = new FakeAccessTokenStore; |
| 109 gateway_data_provider_ = |
| 110 MockDeviceDataProviderImpl<GatewayData>::CreateInstance(); |
| 109 radio_data_provider_ = | 111 radio_data_provider_ = |
| 110 MockDeviceDataProviderImpl<RadioData>::CreateInstance(); | 112 MockDeviceDataProviderImpl<RadioData>::CreateInstance(); |
| 111 wifi_data_provider_ = | 113 wifi_data_provider_ = |
| 112 MockDeviceDataProviderImpl<WifiData>::CreateInstance(); | 114 MockDeviceDataProviderImpl<WifiData>::CreateInstance(); |
| 113 } | 115 } |
| 114 | 116 |
| 115 virtual void TearDown() { | 117 virtual void TearDown() { |
| 116 WifiDataProvider::ResetFactory(); | 118 WifiDataProvider::ResetFactory(); |
| 117 RadioDataProvider::ResetFactory(); | 119 RadioDataProvider::ResetFactory(); |
| 118 URLFetcher::set_factory(NULL); | 120 URLFetcher::set_factory(NULL); |
| 119 } | 121 } |
| 120 | 122 |
| 121 LocationProviderBase* CreateProvider(bool set_permission_granted) { | 123 LocationProviderBase* CreateProvider(bool set_permission_granted) { |
| 122 LocationProviderBase* provider = NewNetworkLocationProvider( | 124 LocationProviderBase* provider = NewNetworkLocationProvider( |
| 123 access_token_store_.get(), | 125 access_token_store_.get(), |
| 124 NULL, // No URLContextGetter needed, as using test urlfecther factory. | 126 NULL, // No URLContextGetter needed, as using test urlfecther factory. |
| 125 test_server_url_, | 127 test_server_url_, |
| 126 access_token_store_->access_token_set_[test_server_url_]); | 128 access_token_store_->access_token_set_[test_server_url_]); |
| 127 if (set_permission_granted) | 129 if (set_permission_granted) |
| 128 provider->OnPermissionGranted(GURL(kTestHostUrl)); | 130 provider->OnPermissionGranted(GURL(kTestHostUrl)); |
| 129 return provider; | 131 return provider; |
| 130 } | 132 } |
| 131 | 133 |
| 132 protected: | 134 protected: |
| 133 GeolocationNetworkProviderTest() : test_server_url_(kTestServerUrl) { | 135 GeolocationNetworkProviderTest() : test_server_url_(kTestServerUrl) { |
| 134 // TODO(joth): Really these should be in SetUp, not here, but they take no | 136 // TODO(joth): Really these should be in SetUp, not here, but they take no |
| 135 // effect on Mac OS Release builds if done there. I kid not. Figure out why. | 137 // effect on Mac OS Release builds if done there. I kid not. Figure out why. |
| 138 GatewayDataProvider::SetFactory( |
| 139 MockDeviceDataProviderImpl<GatewayData>::GetInstance); |
| 136 RadioDataProvider::SetFactory( | 140 RadioDataProvider::SetFactory( |
| 137 MockDeviceDataProviderImpl<RadioData>::GetInstance); | 141 MockDeviceDataProviderImpl<RadioData>::GetInstance); |
| 138 WifiDataProvider::SetFactory( | 142 WifiDataProvider::SetFactory( |
| 139 MockDeviceDataProviderImpl<WifiData>::GetInstance); | 143 MockDeviceDataProviderImpl<WifiData>::GetInstance); |
| 140 } | 144 } |
| 141 | 145 |
| 142 // Returns the current url fetcher (if any) and advances the id ready for the | 146 // Returns the current url fetcher (if any) and advances the id ready for the |
| 143 // next test step. | 147 // next test step. |
| 144 TestURLFetcher* get_url_fetcher_and_advance_id() { | 148 TestURLFetcher* get_url_fetcher_and_advance_id() { |
| 145 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID( | 149 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID( |
| (...skipping 14 matching lines...) Expand all Loading... |
| 160 ap.mac_address = ASCIIToUTF16(StringPrintf("%02d-34-56-78-54-32", i)); | 164 ap.mac_address = ASCIIToUTF16(StringPrintf("%02d-34-56-78-54-32", i)); |
| 161 ap.radio_signal_strength = i; | 165 ap.radio_signal_strength = i; |
| 162 ap.channel = IndexToChannal(i); | 166 ap.channel = IndexToChannal(i); |
| 163 ap.signal_to_noise = i + 42; | 167 ap.signal_to_noise = i + 42; |
| 164 ap.ssid = ASCIIToUTF16("Some nice network"); | 168 ap.ssid = ASCIIToUTF16("Some nice network"); |
| 165 data.access_point_data.insert(ap); | 169 data.access_point_data.insert(ap); |
| 166 } | 170 } |
| 167 return data; | 171 return data; |
| 168 } | 172 } |
| 169 | 173 |
| 170 static void ParseRequest(const std::string& request_data, | 174 // Creates gateway data containing the specified number of routers, with |
| 171 WifiData* wifi_data_out, | 175 // some differentiating charactistics in each. |
| 172 int* max_age_out, | 176 static GatewayData CreateReferenceRouterData(int router_count) { |
| 173 std::string* access_token_out) { | 177 GatewayData data; |
| 178 for (int i = 0; i < router_count; ++i) { |
| 179 RouterData router; |
| 180 router.mac_address = |
| 181 ASCIIToUTF16(StringPrintf("%02d-34-56-78-54-32", i)); |
| 182 data.router_data.insert(router); |
| 183 } |
| 184 return data; |
| 185 } |
| 186 |
| 187 static void ParseGatewayRequest(const std::string& request_data, |
| 188 GatewayData* gateway_data_out) { |
| 189 scoped_ptr<Value> value(base::JSONReader::Read(request_data, false)); |
| 190 EXPECT_TRUE(value != NULL); |
| 191 EXPECT_EQ(Value::TYPE_DICTIONARY, value->GetType()); |
| 192 DictionaryValue* dictionary = static_cast<DictionaryValue*>(value.get()); |
| 193 std::string attr_value; |
| 194 EXPECT_TRUE(dictionary->GetString("version", &attr_value)); |
| 195 EXPECT_EQ(attr_value, "1.1.0"); |
| 196 EXPECT_TRUE(dictionary->GetString("host", &attr_value)); |
| 197 EXPECT_EQ(attr_value, kTestHost); |
| 198 // Everything else is optional. |
| 199 ListValue* gateways; |
| 200 if (dictionary->GetList("gateways", &gateways)) { |
| 201 int i = 0; |
| 202 for (ListValue::const_iterator it = gateways->begin(); |
| 203 it < gateways->end(); ++it, ++i) { |
| 204 EXPECT_EQ(Value::TYPE_DICTIONARY, (*it)->GetType()); |
| 205 DictionaryValue* gateway = static_cast<DictionaryValue*>(*it); |
| 206 RouterData data; |
| 207 gateway->GetString("mac_address", &data.mac_address); |
| 208 gateway_data_out->router_data.insert(data); |
| 209 } |
| 210 } else { |
| 211 gateway_data_out->router_data.clear(); |
| 212 } |
| 213 } |
| 214 |
| 215 static void ParseWifiRequest(const std::string& request_data, |
| 216 WifiData* wifi_data_out, |
| 217 int* max_age_out, |
| 218 std::string* access_token_out) { |
| 174 CHECK(wifi_data_out && max_age_out && access_token_out); | 219 CHECK(wifi_data_out && max_age_out && access_token_out); |
| 175 scoped_ptr<Value> value(base::JSONReader::Read(request_data, false)); | 220 scoped_ptr<Value> value(base::JSONReader::Read(request_data, false)); |
| 176 EXPECT_TRUE(value != NULL); | 221 EXPECT_TRUE(value != NULL); |
| 177 EXPECT_EQ(Value::TYPE_DICTIONARY, value->GetType()); | 222 EXPECT_EQ(Value::TYPE_DICTIONARY, value->GetType()); |
| 178 DictionaryValue* dictionary = static_cast<DictionaryValue*>(value.get()); | 223 DictionaryValue* dictionary = static_cast<DictionaryValue*>(value.get()); |
| 179 std::string attr_value; | 224 std::string attr_value; |
| 180 EXPECT_TRUE(dictionary->GetString("version", &attr_value)); | 225 EXPECT_TRUE(dictionary->GetString("version", &attr_value)); |
| 181 EXPECT_EQ(attr_value, "1.1.0"); | 226 EXPECT_EQ(attr_value, "1.1.0"); |
| 182 EXPECT_TRUE(dictionary->GetString("host", &attr_value)); | 227 EXPECT_TRUE(dictionary->GetString("host", &attr_value)); |
| 183 EXPECT_EQ(attr_value, kTestHost); | 228 EXPECT_EQ(attr_value, kTestHost); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 206 wifi_data_out->access_point_data.clear(); | 251 wifi_data_out->access_point_data.clear(); |
| 207 } | 252 } |
| 208 if (!dictionary->GetString("access_token", access_token_out)) | 253 if (!dictionary->GetString("access_token", access_token_out)) |
| 209 access_token_out->clear(); | 254 access_token_out->clear(); |
| 210 } | 255 } |
| 211 | 256 |
| 212 static void CheckEmptyRequestIsValid(const std::string& request_data) { | 257 static void CheckEmptyRequestIsValid(const std::string& request_data) { |
| 213 WifiData wifi_aps; | 258 WifiData wifi_aps; |
| 214 std::string access_token; | 259 std::string access_token; |
| 215 int max_age; | 260 int max_age; |
| 216 ParseRequest(request_data, &wifi_aps, &max_age, &access_token); | 261 ParseWifiRequest(request_data, &wifi_aps, &max_age, &access_token); |
| 217 EXPECT_EQ(kint32min, max_age); | 262 EXPECT_EQ(kint32min, max_age); |
| 218 EXPECT_EQ(0, static_cast<int>(wifi_aps.access_point_data.size())); | 263 EXPECT_EQ(0, static_cast<int>(wifi_aps.access_point_data.size())); |
| 219 EXPECT_TRUE(access_token.empty()); | 264 EXPECT_TRUE(access_token.empty()); |
| 220 } | 265 } |
| 221 | 266 |
| 222 static void CheckRequestIsValid(const std::string& request_data, | 267 static void CheckRequestIsValid(const std::string& request_data, |
| 268 int expected_routers, |
| 223 int expected_wifi_aps, | 269 int expected_wifi_aps, |
| 224 const std::string& expected_access_token) { | 270 const std::string& expected_access_token) { |
| 225 WifiData wifi_aps; | 271 WifiData wifi_aps; |
| 226 std::string access_token; | 272 std::string access_token; |
| 227 int max_age; | 273 int max_age; |
| 228 ParseRequest(request_data, &wifi_aps, &max_age, &access_token); | 274 ParseWifiRequest(request_data, &wifi_aps, &max_age, &access_token); |
| 229 EXPECT_GE(max_age, 0) << "Age must not be negative."; | |
| 230 EXPECT_LT(max_age, 10 * 1000) << "This test really shouldn't take 10s."; | |
| 231 EXPECT_EQ(expected_wifi_aps, | 275 EXPECT_EQ(expected_wifi_aps, |
| 232 static_cast<int>(wifi_aps.access_point_data.size())); | 276 static_cast<int>(wifi_aps.access_point_data.size())); |
| 233 WifiData expected_data = CreateReferenceWifiScanData(expected_wifi_aps); | 277 if (expected_wifi_aps > 0) { |
| 234 WifiData::AccessPointDataSet::const_iterator expected = | 278 EXPECT_GE(max_age, 0) << "Age must not be negative."; |
| 235 expected_data.access_point_data.begin(); | 279 EXPECT_LT(max_age, 10 * 1000) << "This test really shouldn't take 10s."; |
| 236 WifiData::AccessPointDataSet::const_iterator actual = | 280 WifiData expected_data = CreateReferenceWifiScanData(expected_wifi_aps); |
| 237 wifi_aps.access_point_data.begin(); | 281 WifiData::AccessPointDataSet::const_iterator expected = |
| 238 for (int i = 0; i < expected_wifi_aps; ++i) { | 282 expected_data.access_point_data.begin(); |
| 283 WifiData::AccessPointDataSet::const_iterator actual = |
| 284 wifi_aps.access_point_data.begin(); |
| 285 for (int i = 0; i < expected_wifi_aps; ++i) { |
| 286 EXPECT_EQ(expected->mac_address, actual->mac_address) << i; |
| 287 EXPECT_EQ(expected->radio_signal_strength, |
| 288 actual->radio_signal_strength) << i; |
| 289 EXPECT_EQ(expected->channel, actual->channel) << i; |
| 290 EXPECT_EQ(expected->signal_to_noise, actual->signal_to_noise) << i; |
| 291 EXPECT_EQ(expected->ssid, actual->ssid) << i; |
| 292 ++expected; |
| 293 ++actual; |
| 294 } |
| 295 } else { |
| 296 EXPECT_EQ(max_age, kint32min); |
| 297 } |
| 298 EXPECT_EQ(expected_access_token, access_token); |
| 299 |
| 300 GatewayData gateway_data; |
| 301 ParseGatewayRequest(request_data, &gateway_data); |
| 302 EXPECT_EQ(expected_routers, |
| 303 static_cast<int>(gateway_data.router_data.size())); |
| 304 GatewayData expected_data = CreateReferenceRouterData(expected_routers); |
| 305 GatewayData::RouterDataSet::const_iterator expected = |
| 306 expected_data.router_data.begin(); |
| 307 GatewayData::RouterDataSet::const_iterator actual = |
| 308 gateway_data.router_data.begin(); |
| 309 for (int i = 0; i < expected_routers; ++i) { |
| 239 EXPECT_EQ(expected->mac_address, actual->mac_address) << i; | 310 EXPECT_EQ(expected->mac_address, actual->mac_address) << i; |
| 240 EXPECT_EQ(expected->radio_signal_strength, actual->radio_signal_strength) | |
| 241 << i; | |
| 242 EXPECT_EQ(expected->channel, actual->channel) << i; | |
| 243 EXPECT_EQ(expected->signal_to_noise, actual->signal_to_noise) << i; | |
| 244 EXPECT_EQ(expected->ssid, actual->ssid) << i; | |
| 245 ++expected; | 311 ++expected; |
| 246 ++actual; | 312 ++actual; |
| 247 } | 313 } |
| 248 EXPECT_EQ(expected_access_token, access_token); | |
| 249 } | 314 } |
| 250 | 315 |
| 251 const GURL test_server_url_; | 316 const GURL test_server_url_; |
| 252 MessageLoop main_message_loop_; | 317 MessageLoop main_message_loop_; |
| 253 scoped_refptr<FakeAccessTokenStore> access_token_store_; | 318 scoped_refptr<FakeAccessTokenStore> access_token_store_; |
| 254 TestURLFetcherFactory url_fetcher_factory_; | 319 TestURLFetcherFactory url_fetcher_factory_; |
| 320 scoped_refptr<MockDeviceDataProviderImpl<GatewayData> > |
| 321 gateway_data_provider_; |
| 255 scoped_refptr<MockDeviceDataProviderImpl<RadioData> > radio_data_provider_; | 322 scoped_refptr<MockDeviceDataProviderImpl<RadioData> > radio_data_provider_; |
| 256 scoped_refptr<MockDeviceDataProviderImpl<WifiData> > wifi_data_provider_; | 323 scoped_refptr<MockDeviceDataProviderImpl<WifiData> > wifi_data_provider_; |
| 257 }; | 324 }; |
| 258 | 325 |
| 259 | 326 |
| 260 TEST_F(GeolocationNetworkProviderTest, CreateDestroy) { | 327 TEST_F(GeolocationNetworkProviderTest, CreateDestroy) { |
| 261 // Test fixture members were SetUp correctly. | 328 // Test fixture members were SetUp correctly. |
| 262 EXPECT_EQ(&main_message_loop_, MessageLoop::current()); | 329 EXPECT_EQ(&main_message_loop_, MessageLoop::current()); |
| 263 scoped_ptr<LocationProviderBase> provider(CreateProvider(true)); | 330 scoped_ptr<LocationProviderBase> provider(CreateProvider(true)); |
| 264 EXPECT_TRUE(NULL != provider.get()); | 331 EXPECT_TRUE(NULL != provider.get()); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 provider->GetPosition(&position); | 388 provider->GetPosition(&position); |
| 322 EXPECT_FALSE(position.IsValidFix()); | 389 EXPECT_FALSE(position.IsValidFix()); |
| 323 | 390 |
| 324 // Now wifi data arrives -- SetData will notify listeners. | 391 // Now wifi data arrives -- SetData will notify listeners. |
| 325 const int kFirstScanAps = 6; | 392 const int kFirstScanAps = 6; |
| 326 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kFirstScanAps)); | 393 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kFirstScanAps)); |
| 327 main_message_loop_.RunAllPending(); | 394 main_message_loop_.RunAllPending(); |
| 328 fetcher = get_url_fetcher_and_advance_id(); | 395 fetcher = get_url_fetcher_and_advance_id(); |
| 329 ASSERT_TRUE(fetcher != NULL); | 396 ASSERT_TRUE(fetcher != NULL); |
| 330 // The request should have access token (set previously) and the wifi data. | 397 // The request should have access token (set previously) and the wifi data. |
| 331 CheckRequestIsValid(fetcher->upload_data(), | 398 CheckRequestIsValid(fetcher->upload_data(), 0, |
| 332 kFirstScanAps, | 399 kFirstScanAps, |
| 333 REFERENCE_ACCESS_TOKEN); | 400 REFERENCE_ACCESS_TOKEN); |
| 334 | 401 |
| 335 // Send a reply with good position fix. | 402 // Send a reply with good position fix. |
| 336 const char* kReferenceNetworkResponse = | 403 const char* kReferenceNetworkResponse = |
| 337 "{" | 404 "{" |
| 338 " \"location\": {" | 405 " \"location\": {" |
| 339 " \"latitude\": 51.0," | 406 " \"latitude\": 51.0," |
| 340 " \"longitude\": -0.1," | 407 " \"longitude\": -0.1," |
| 341 " \"altitude\": 30.1," | 408 " \"altitude\": 30.1," |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kFirstScanAps)); | 462 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kFirstScanAps)); |
| 396 main_message_loop_.RunAllPending(); | 463 main_message_loop_.RunAllPending(); |
| 397 EXPECT_FALSE(get_url_fetcher_and_advance_id()); // No new request created. | 464 EXPECT_FALSE(get_url_fetcher_and_advance_id()); // No new request created. |
| 398 | 465 |
| 399 provider->GetPosition(&position); | 466 provider->GetPosition(&position); |
| 400 EXPECT_EQ(51.0, position.latitude); | 467 EXPECT_EQ(51.0, position.latitude); |
| 401 EXPECT_EQ(-0.1, position.longitude); | 468 EXPECT_EQ(-0.1, position.longitude); |
| 402 EXPECT_TRUE(position.IsValidFix()); | 469 EXPECT_TRUE(position.IsValidFix()); |
| 403 } | 470 } |
| 404 | 471 |
| 472 TEST_F(GeolocationNetworkProviderTest, GatewayAndWifiScans) { |
| 473 scoped_ptr<LocationProviderBase> provider(CreateProvider(true)); |
| 474 EXPECT_TRUE(provider->StartProvider(false)); |
| 475 |
| 476 TestURLFetcher* fetcher = get_url_fetcher_and_advance_id(); |
| 477 ASSERT_TRUE(fetcher != NULL); |
| 478 CheckEmptyRequestIsValid(fetcher->upload_data()); |
| 479 // Complete the network request with bad position fix (using #define so we |
| 480 // can paste this into various other strings below) |
| 481 #define REFERENCE_ACCESS_TOKEN "2:k7j3G6LaL6u_lafw:4iXOeOpTh1glSXe" |
| 482 const char* kNoFixNetworkResponse = |
| 483 "{" |
| 484 " \"location\": null," |
| 485 " \"access_token\": \"" REFERENCE_ACCESS_TOKEN "\"" |
| 486 "}"; |
| 487 fetcher->delegate()->OnURLFetchComplete( |
| 488 fetcher, test_server_url_, URLRequestStatus(), 200, // OK |
| 489 ResponseCookies(), kNoFixNetworkResponse); |
| 490 |
| 491 // This should have set the access token anyhow |
| 492 EXPECT_EQ(UTF8ToUTF16(REFERENCE_ACCESS_TOKEN), |
| 493 access_token_store_->access_token_set_[test_server_url_]); |
| 494 |
| 495 Geoposition position; |
| 496 provider->GetPosition(&position); |
| 497 EXPECT_FALSE(position.IsValidFix()); |
| 498 |
| 499 // Now gateway data arrives -- SetData will notify listeners. |
| 500 const int kFirstScanRouters = 1; |
| 501 gateway_data_provider_->SetData( |
| 502 CreateReferenceRouterData(kFirstScanRouters)); |
| 503 main_message_loop_.RunAllPending(); |
| 504 fetcher = get_url_fetcher_and_advance_id(); |
| 505 ASSERT_TRUE(fetcher != NULL); |
| 506 // The request should have access token (set previously) and the |
| 507 // gateway data. |
| 508 CheckRequestIsValid(fetcher->upload_data(), kFirstScanRouters, |
| 509 0, REFERENCE_ACCESS_TOKEN); |
| 510 |
| 511 // Send a reply with good position fix. |
| 512 const char* kReferenceNetworkResponse_1 = |
| 513 "{" |
| 514 " \"location\": {" |
| 515 " \"latitude\": 51.0," |
| 516 " \"longitude\": -0.1," |
| 517 " \"altitude\": 30.1," |
| 518 " \"accuracy\": 1200.4," |
| 519 " \"altitude_accuracy\": 10.6" |
| 520 " }" |
| 521 "}"; |
| 522 fetcher->delegate()->OnURLFetchComplete( |
| 523 fetcher, test_server_url_, URLRequestStatus(), 200, // OK |
| 524 ResponseCookies(), kReferenceNetworkResponse_1); |
| 525 |
| 526 provider->GetPosition(&position); |
| 527 EXPECT_EQ(51.0, position.latitude); |
| 528 EXPECT_EQ(-0.1, position.longitude); |
| 529 EXPECT_EQ(30.1, position.altitude); |
| 530 EXPECT_EQ(1200.4, position.accuracy); |
| 531 EXPECT_EQ(10.6, position.altitude_accuracy); |
| 532 EXPECT_TRUE(position.is_valid_timestamp()); |
| 533 EXPECT_TRUE(position.IsValidFix()); |
| 534 |
| 535 // Token should still be in the store. |
| 536 EXPECT_EQ(UTF8ToUTF16(REFERENCE_ACCESS_TOKEN), |
| 537 access_token_store_->access_token_set_[test_server_url_]); |
| 538 |
| 539 // Gateway updated again, with one more router. This is a significant change |
| 540 // so a new request is made. |
| 541 const int kSecondScanRouters = kFirstScanRouters + 1; |
| 542 gateway_data_provider_->SetData( |
| 543 CreateReferenceRouterData(kSecondScanRouters)); |
| 544 main_message_loop_.RunAllPending(); |
| 545 fetcher = get_url_fetcher_and_advance_id(); |
| 546 EXPECT_TRUE(fetcher); |
| 547 |
| 548 CheckRequestIsValid(fetcher->upload_data(), kSecondScanRouters, |
| 549 0, REFERENCE_ACCESS_TOKEN); |
| 550 |
| 551 // Send a reply with good position fix. |
| 552 const char* kReferenceNetworkResponse_2 = |
| 553 "{" |
| 554 " \"location\": {" |
| 555 " \"latitude\": 51.1," |
| 556 " \"longitude\": -0.1," |
| 557 " \"altitude\": 30.2," |
| 558 " \"accuracy\": 1100.4," |
| 559 " \"altitude_accuracy\": 10.6" |
| 560 " }" |
| 561 "}"; |
| 562 fetcher->delegate()->OnURLFetchComplete( |
| 563 fetcher, test_server_url_, URLRequestStatus(), 200, // OK |
| 564 ResponseCookies(), kReferenceNetworkResponse_2); |
| 565 |
| 566 provider->GetPosition(&position); |
| 567 EXPECT_EQ(51.1, position.latitude); |
| 568 EXPECT_EQ(-0.1, position.longitude); |
| 569 EXPECT_EQ(30.2, position.altitude); |
| 570 EXPECT_EQ(1100.4, position.accuracy); |
| 571 EXPECT_EQ(10.6, position.altitude_accuracy); |
| 572 EXPECT_TRUE(position.is_valid_timestamp()); |
| 573 EXPECT_TRUE(position.IsValidFix()); |
| 574 |
| 575 // Now add new wifi scan data. |
| 576 const int kScanAps = 4; |
| 577 wifi_data_provider_->SetData(CreateReferenceWifiScanData(kScanAps)); |
| 578 main_message_loop_.RunAllPending(); |
| 579 fetcher = get_url_fetcher_and_advance_id(); |
| 580 EXPECT_TRUE(fetcher); |
| 581 CheckRequestIsValid(fetcher->upload_data(), kSecondScanRouters, |
| 582 kScanAps, REFERENCE_ACCESS_TOKEN); |
| 583 |
| 584 // Send a reply with good position fix. |
| 585 const char* kReferenceNetworkResponse_3 = |
| 586 "{" |
| 587 " \"location\": {" |
| 588 " \"latitude\": 51.3," |
| 589 " \"longitude\": -0.1," |
| 590 " \"altitude\": 30.2," |
| 591 " \"accuracy\": 50.4," |
| 592 " \"altitude_accuracy\": 10.6" |
| 593 " }" |
| 594 "}"; |
| 595 fetcher->delegate()->OnURLFetchComplete( |
| 596 fetcher, test_server_url_, URLRequestStatus(), 200, // OK |
| 597 ResponseCookies(), kReferenceNetworkResponse_3); |
| 598 |
| 599 provider->GetPosition(&position); |
| 600 EXPECT_EQ(51.3, position.latitude); |
| 601 EXPECT_EQ(-0.1, position.longitude); |
| 602 EXPECT_EQ(30.2, position.altitude); |
| 603 EXPECT_EQ(50.4, position.accuracy); |
| 604 EXPECT_EQ(10.6, position.altitude_accuracy); |
| 605 EXPECT_TRUE(position.is_valid_timestamp()); |
| 606 EXPECT_TRUE(position.IsValidFix()); |
| 607 |
| 608 // Wifi scan returns no access points found: should be serviced from cache. |
| 609 wifi_data_provider_->SetData(CreateReferenceWifiScanData(0)); |
| 610 main_message_loop_.RunAllPending(); |
| 611 EXPECT_FALSE(get_url_fetcher_and_advance_id()); // No new request created. |
| 612 |
| 613 provider->GetPosition(&position); |
| 614 EXPECT_EQ(51.1, position.latitude); |
| 615 EXPECT_EQ(-0.1, position.longitude); |
| 616 EXPECT_EQ(30.2, position.altitude); |
| 617 EXPECT_EQ(1100.4, position.accuracy); |
| 618 EXPECT_EQ(10.6, position.altitude_accuracy); |
| 619 EXPECT_TRUE(position.is_valid_timestamp()); |
| 620 EXPECT_TRUE(position.IsValidFix()); |
| 621 } |
| 622 |
| 405 TEST_F(GeolocationNetworkProviderTest, NoRequestOnStartupUntilWifiData) { | 623 TEST_F(GeolocationNetworkProviderTest, NoRequestOnStartupUntilWifiData) { |
| 406 MessageLoopQuitListener listener; | 624 MessageLoopQuitListener listener; |
| 407 wifi_data_provider_->set_got_data(false); | 625 wifi_data_provider_->set_got_data(false); |
| 408 scoped_ptr<LocationProviderBase> provider(CreateProvider(true)); | 626 scoped_ptr<LocationProviderBase> provider(CreateProvider(true)); |
| 409 EXPECT_TRUE(provider->StartProvider(false)); | 627 EXPECT_TRUE(provider->StartProvider(false)); |
| 410 provider->RegisterListener(&listener); | 628 provider->RegisterListener(&listener); |
| 411 | 629 |
| 412 main_message_loop_.RunAllPending(); | 630 main_message_loop_.RunAllPending(); |
| 413 EXPECT_FALSE(get_url_fetcher_and_advance_id()) | 631 EXPECT_FALSE(get_url_fetcher_and_advance_id()) |
| 414 << "Network request should not be created right away on startup when " | 632 << "Network request should not be created right away on startup when " |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 fetcher = get_url_fetcher_and_advance_id(); | 683 fetcher = get_url_fetcher_and_advance_id(); |
| 466 EXPECT_FALSE(fetcher); | 684 EXPECT_FALSE(fetcher); |
| 467 | 685 |
| 468 provider->OnPermissionGranted(GURL(kTestHostUrl)); | 686 provider->OnPermissionGranted(GURL(kTestHostUrl)); |
| 469 | 687 |
| 470 fetcher = get_url_fetcher_and_advance_id(); | 688 fetcher = get_url_fetcher_and_advance_id(); |
| 471 ASSERT_TRUE(fetcher != NULL); | 689 ASSERT_TRUE(fetcher != NULL); |
| 472 | 690 |
| 473 EXPECT_EQ(test_server_url_, fetcher->original_url()); | 691 EXPECT_EQ(test_server_url_, fetcher->original_url()); |
| 474 | 692 |
| 475 CheckRequestIsValid(fetcher->upload_data(), kScanCount, | 693 CheckRequestIsValid(fetcher->upload_data(), 0, |
| 476 REFERENCE_ACCESS_TOKEN); | 694 kScanCount, REFERENCE_ACCESS_TOKEN); |
| 477 } | 695 } |
| OLD | NEW |