| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/json/json_reader.h" | 5 #include "base/json/json_reader.h" |
| 6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 | 122 |
| 123 template<typename DataType> | 123 template<typename DataType> |
| 124 MockDeviceDataProviderImpl<DataType>* | 124 MockDeviceDataProviderImpl<DataType>* |
| 125 MockDeviceDataProviderImpl<DataType>::instance_ = NULL; | 125 MockDeviceDataProviderImpl<DataType>::instance_ = NULL; |
| 126 | 126 |
| 127 // Main test fixture | 127 // Main test fixture |
| 128 class GeolocationNetworkProviderTest : public testing::Test { | 128 class GeolocationNetworkProviderTest : public testing::Test { |
| 129 public: | 129 public: |
| 130 virtual void SetUp() { | 130 virtual void SetUp() { |
| 131 access_token_store_ = new FakeAccessTokenStore; | 131 access_token_store_ = new FakeAccessTokenStore; |
| 132 radio_data_provider_ = | |
| 133 MockDeviceDataProviderImpl<RadioData>::CreateInstance(); | |
| 134 wifi_data_provider_ = | 132 wifi_data_provider_ = |
| 135 MockDeviceDataProviderImpl<WifiData>::CreateInstance(); | 133 MockDeviceDataProviderImpl<WifiData>::CreateInstance(); |
| 136 } | 134 } |
| 137 | 135 |
| 138 virtual void TearDown() { | 136 virtual void TearDown() { |
| 139 WifiDataProvider::ResetFactory(); | 137 WifiDataProvider::ResetFactory(); |
| 140 RadioDataProvider::ResetFactory(); | |
| 141 } | 138 } |
| 142 | 139 |
| 143 LocationProviderBase* CreateProvider(bool set_permission_granted) { | 140 LocationProviderBase* CreateProvider(bool set_permission_granted) { |
| 144 LocationProviderBase* provider = NewNetworkLocationProvider( | 141 LocationProviderBase* provider = NewNetworkLocationProvider( |
| 145 access_token_store_.get(), | 142 access_token_store_.get(), |
| 146 NULL, // No URLContextGetter needed, as using test urlfecther factory. | 143 NULL, // No URLContextGetter needed, as using test urlfecther factory. |
| 147 test_server_url_, | 144 test_server_url_, |
| 148 access_token_store_->access_token_set_[test_server_url_]); | 145 access_token_store_->access_token_set_[test_server_url_]); |
| 149 if (set_permission_granted) | 146 if (set_permission_granted) |
| 150 provider->OnPermissionGranted(); | 147 provider->OnPermissionGranted(); |
| 151 return provider; | 148 return provider; |
| 152 } | 149 } |
| 153 | 150 |
| 154 protected: | 151 protected: |
| 155 GeolocationNetworkProviderTest() : test_server_url_(kTestServerUrl) { | 152 GeolocationNetworkProviderTest() : test_server_url_(kTestServerUrl) { |
| 156 // TODO(joth): Really these should be in SetUp, not here, but they take no | 153 // TODO(joth): Really these should be in SetUp, not here, but they take no |
| 157 // effect on Mac OS Release builds if done there. I kid not. Figure out why. | 154 // effect on Mac OS Release builds if done there. I kid not. Figure out why. |
| 158 RadioDataProvider::SetFactory( | |
| 159 MockDeviceDataProviderImpl<RadioData>::GetInstance); | |
| 160 WifiDataProvider::SetFactory( | 155 WifiDataProvider::SetFactory( |
| 161 MockDeviceDataProviderImpl<WifiData>::GetInstance); | 156 MockDeviceDataProviderImpl<WifiData>::GetInstance); |
| 162 } | 157 } |
| 163 | 158 |
| 164 // Returns the current url fetcher (if any) and advances the id ready for the | 159 // Returns the current url fetcher (if any) and advances the id ready for the |
| 165 // next test step. | 160 // next test step. |
| 166 TestURLFetcher* get_url_fetcher_and_advance_id() { | 161 TestURLFetcher* get_url_fetcher_and_advance_id() { |
| 167 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID( | 162 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID( |
| 168 NetworkLocationRequest::url_fetcher_id_for_tests); | 163 NetworkLocationRequest::url_fetcher_id_for_tests); |
| 169 if (fetcher) | 164 if (fetcher) |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 EXPECT_EQ(expected_wifi_tokens[5], actual_wifi_tokens[5]); | 261 EXPECT_EQ(expected_wifi_tokens[5], actual_wifi_tokens[5]); |
| 267 EXPECT_EQ(expected_wifi_tokens[6], actual_wifi_tokens[6]); | 262 EXPECT_EQ(expected_wifi_tokens[6], actual_wifi_tokens[6]); |
| 268 } | 263 } |
| 269 EXPECT_TRUE(GURL(request_url).is_valid()); | 264 EXPECT_TRUE(GURL(request_url).is_valid()); |
| 270 } | 265 } |
| 271 | 266 |
| 272 const GURL test_server_url_; | 267 const GURL test_server_url_; |
| 273 MessageLoop main_message_loop_; | 268 MessageLoop main_message_loop_; |
| 274 scoped_refptr<FakeAccessTokenStore> access_token_store_; | 269 scoped_refptr<FakeAccessTokenStore> access_token_store_; |
| 275 TestURLFetcherFactory url_fetcher_factory_; | 270 TestURLFetcherFactory url_fetcher_factory_; |
| 276 scoped_refptr<MockDeviceDataProviderImpl<RadioData> > radio_data_provider_; | |
| 277 scoped_refptr<MockDeviceDataProviderImpl<WifiData> > wifi_data_provider_; | 271 scoped_refptr<MockDeviceDataProviderImpl<WifiData> > wifi_data_provider_; |
| 278 }; | 272 }; |
| 279 | 273 |
| 280 | 274 |
| 281 TEST_F(GeolocationNetworkProviderTest, CreateDestroy) { | 275 TEST_F(GeolocationNetworkProviderTest, CreateDestroy) { |
| 282 // Test fixture members were SetUp correctly. | 276 // Test fixture members were SetUp correctly. |
| 283 EXPECT_EQ(&main_message_loop_, MessageLoop::current()); | 277 EXPECT_EQ(&main_message_loop_, MessageLoop::current()); |
| 284 scoped_ptr<LocationProviderBase> provider(CreateProvider(true)); | 278 scoped_ptr<LocationProviderBase> provider(CreateProvider(true)); |
| 285 EXPECT_TRUE(NULL != provider.get()); | 279 EXPECT_TRUE(NULL != provider.get()); |
| 286 provider.reset(); | 280 provider.reset(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 307 main_message_loop_.RunAllPending(); | 301 main_message_loop_.RunAllPending(); |
| 308 TestURLFetcher* fetcher = get_url_fetcher_and_advance_id(); | 302 TestURLFetcher* fetcher = get_url_fetcher_and_advance_id(); |
| 309 ASSERT_TRUE(fetcher != NULL); | 303 ASSERT_TRUE(fetcher != NULL); |
| 310 // The request url should have been shortened to less than 2048 characters | 304 // The request url should have been shortened to less than 2048 characters |
| 311 // in length by not including access points with the lowest signal strength | 305 // in length by not including access points with the lowest signal strength |
| 312 // in the request. | 306 // in the request. |
| 313 EXPECT_LT(fetcher->GetOriginalURL().spec().size(), size_t(2048)); | 307 EXPECT_LT(fetcher->GetOriginalURL().spec().size(), size_t(2048)); |
| 314 CheckRequestIsValid(fetcher->GetOriginalURL().spec(), 0, 16, 4, ""); | 308 CheckRequestIsValid(fetcher->GetOriginalURL().spec(), 0, 16, 4, ""); |
| 315 } | 309 } |
| 316 | 310 |
| 317 TEST_F(GeolocationNetworkProviderTest, MultipleStartProvider) { | |
| 318 scoped_ptr<LocationProviderBase> provider_1(CreateProvider(true)); | |
| 319 scoped_ptr<LocationProviderBase> provider_2(CreateProvider(true)); | |
| 320 ASSERT_TRUE(radio_data_provider_); | |
| 321 ASSERT_TRUE(wifi_data_provider_); | |
| 322 EXPECT_EQ(0, radio_data_provider_->start_calls_); | |
| 323 EXPECT_EQ(0, wifi_data_provider_->start_calls_); | |
| 324 EXPECT_EQ(0, radio_data_provider_->stop_calls_); | |
| 325 EXPECT_EQ(0, wifi_data_provider_->stop_calls_); | |
| 326 | |
| 327 // Start first provider. | |
| 328 EXPECT_TRUE(provider_1->StartProvider(false)); | |
| 329 EXPECT_EQ(1, radio_data_provider_->start_calls_); | |
| 330 EXPECT_EQ(1, wifi_data_provider_->start_calls_); | |
| 331 // Start second provider. | |
| 332 EXPECT_TRUE(provider_2->StartProvider(false)); | |
| 333 EXPECT_EQ(1, radio_data_provider_->start_calls_); | |
| 334 EXPECT_EQ(1, wifi_data_provider_->start_calls_); | |
| 335 | |
| 336 // Stop first provider. | |
| 337 provider_1->StopProvider(); | |
| 338 EXPECT_EQ(0, radio_data_provider_->stop_calls_); | |
| 339 EXPECT_EQ(0, wifi_data_provider_->stop_calls_); | |
| 340 // Stop second provider. | |
| 341 provider_2->StopProvider(); | |
| 342 EXPECT_EQ(1, radio_data_provider_->stop_calls_); | |
| 343 EXPECT_EQ(1, wifi_data_provider_->stop_calls_); | |
| 344 } | |
| 345 | |
| 346 TEST_F(GeolocationNetworkProviderTest, MultiRegistrations) { | 311 TEST_F(GeolocationNetworkProviderTest, MultiRegistrations) { |
| 347 // TODO(joth): Strictly belongs in a base-class unit test file. | 312 // TODO(joth): Strictly belongs in a base-class unit test file. |
| 348 MessageLoopQuitListener listener; | 313 MessageLoopQuitListener listener; |
| 349 scoped_ptr<LocationProviderBase> provider(CreateProvider(true)); | 314 scoped_ptr<LocationProviderBase> provider(CreateProvider(true)); |
| 350 EXPECT_FALSE(provider->has_listeners()); | 315 EXPECT_FALSE(provider->has_listeners()); |
| 351 provider->RegisterListener(&listener); | 316 provider->RegisterListener(&listener); |
| 352 EXPECT_TRUE(provider->has_listeners()); | 317 EXPECT_TRUE(provider->has_listeners()); |
| 353 provider->RegisterListener(&listener); | 318 provider->RegisterListener(&listener); |
| 354 EXPECT_TRUE(provider->has_listeners()); | 319 EXPECT_TRUE(provider->has_listeners()); |
| 355 | 320 |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 554 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(1))); | 519 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(1))); |
| 555 } else { | 520 } else { |
| 556 const int evicted = i - kCacheSize; | 521 const int evicted = i - kCacheSize; |
| 557 EXPECT_FALSE(cache.FindPosition(CreateReferenceWifiScanData(evicted))); | 522 EXPECT_FALSE(cache.FindPosition(CreateReferenceWifiScanData(evicted))); |
| 558 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(evicted + 1))); | 523 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(evicted + 1))); |
| 559 } | 524 } |
| 560 } | 525 } |
| 561 } | 526 } |
| 562 | 527 |
| 563 } // namespace | 528 } // namespace |
| OLD | NEW |