| 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/json/json_writer.h" | 6 #include "base/json/json_writer.h" |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "content/browser/geolocation/fake_access_token_store.h" | 13 #include "content/browser/geolocation/fake_access_token_store.h" |
| 14 #include "content/browser/geolocation/location_arbitrator_impl.h" | 14 #include "content/browser/geolocation/location_arbitrator_impl.h" |
| 15 #include "content/browser/geolocation/network_location_provider.h" | 15 #include "content/browser/geolocation/network_location_provider.h" |
| 16 #include "content/browser/geolocation/wifi_data_provider.h" |
| 16 #include "net/url_request/test_url_fetcher_factory.h" | 17 #include "net/url_request/test_url_fetcher_factory.h" |
| 17 #include "net/url_request/url_request_status.h" | 18 #include "net/url_request/url_request_status.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 20 |
| 20 namespace content { | 21 namespace content { |
| 21 | 22 |
| 22 // Constants used in multiple tests. | 23 // Constants used in multiple tests. |
| 23 const char kTestServerUrl[] = "https://www.geolocation.test/service"; | 24 const char kTestServerUrl[] = "https://www.geolocation.test/service"; |
| 24 const char kAccessTokenString[] = "accessToken"; | 25 const char kAccessTokenString[] = "accessToken"; |
| 25 | 26 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 39 const Geoposition& position) { | 40 const Geoposition& position) { |
| 40 EXPECT_EQ(client_message_loop_, base::MessageLoop::current()); | 41 EXPECT_EQ(client_message_loop_, base::MessageLoop::current()); |
| 41 updated_provider_ = provider; | 42 updated_provider_ = provider; |
| 42 client_message_loop_->Quit(); | 43 client_message_loop_->Quit(); |
| 43 } | 44 } |
| 44 | 45 |
| 45 base::MessageLoop* client_message_loop_; | 46 base::MessageLoop* client_message_loop_; |
| 46 const LocationProvider* updated_provider_; | 47 const LocationProvider* updated_provider_; |
| 47 }; | 48 }; |
| 48 | 49 |
| 49 // A mock implementation of WifiDataProviderImplBase for testing. Adapted from | 50 // A mock implementation of WifiDataProvider for testing. Adapted from |
| 50 // http://gears.googlecode.com/svn/trunk/gears/geolocation/geolocation_test.cc | 51 // http://gears.googlecode.com/svn/trunk/gears/geolocation/geolocation_test.cc |
| 51 class MockWifiDataProviderImpl : public WifiDataProviderImplBase { | 52 class MockWifiDataProvider : public WifiDataProvider { |
| 52 public: | 53 public: |
| 53 // Factory method for use with WifiDataProvider::SetFactory. | 54 // Factory method for use with WifiDataProvider::SetFactory. |
| 54 static WifiDataProviderImplBase* GetInstance() { | 55 static WifiDataProvider* GetInstance() { |
| 55 CHECK(instance_); | 56 CHECK(instance_); |
| 56 return instance_; | 57 return instance_; |
| 57 } | 58 } |
| 58 | 59 |
| 59 static MockWifiDataProviderImpl* CreateInstance() { | 60 static MockWifiDataProvider* CreateInstance() { |
| 60 CHECK(!instance_); | 61 CHECK(!instance_); |
| 61 instance_ = new MockWifiDataProviderImpl; | 62 instance_ = new MockWifiDataProvider; |
| 62 return instance_; | 63 return instance_; |
| 63 } | 64 } |
| 64 | 65 |
| 65 MockWifiDataProviderImpl() | 66 MockWifiDataProvider() : start_calls_(0), stop_calls_(0), got_data_(true) {} |
| 66 : start_calls_(0), | |
| 67 stop_calls_(0), | |
| 68 got_data_(true) { | |
| 69 } | |
| 70 | 67 |
| 71 // WifiDataProviderImplBase implementation. | 68 // WifiDataProvider implementation. |
| 72 virtual void StartDataProvider() OVERRIDE { | 69 virtual void StartDataProvider() OVERRIDE { |
| 73 ++start_calls_; | 70 ++start_calls_; |
| 74 } | 71 } |
| 75 | 72 |
| 76 virtual void StopDataProvider() OVERRIDE { | 73 virtual void StopDataProvider() OVERRIDE { |
| 77 ++stop_calls_; | 74 ++stop_calls_; |
| 78 } | 75 } |
| 79 | 76 |
| 80 virtual bool GetData(WifiData* data_out) OVERRIDE { | 77 virtual bool GetData(WifiData* data_out) OVERRIDE { |
| 81 CHECK(data_out); | 78 CHECK(data_out); |
| 82 *data_out = data_; | 79 *data_out = data_; |
| 83 return got_data_; | 80 return got_data_; |
| 84 } | 81 } |
| 85 | 82 |
| 86 void SetData(const WifiData& new_data) { | 83 void SetData(const WifiData& new_data) { |
| 87 got_data_ = true; | 84 got_data_ = true; |
| 88 const bool differs = data_.DiffersSignificantly(new_data); | 85 const bool differs = data_.DiffersSignificantly(new_data); |
| 89 data_ = new_data; | 86 data_ = new_data; |
| 90 if (differs) | 87 if (differs) |
| 91 this->RunCallbacks(); | 88 this->RunCallbacks(); |
| 92 } | 89 } |
| 93 | 90 |
| 94 void set_got_data(bool got_data) { got_data_ = got_data; } | 91 void set_got_data(bool got_data) { got_data_ = got_data; } |
| 95 int start_calls_; | 92 int start_calls_; |
| 96 int stop_calls_; | 93 int stop_calls_; |
| 97 | 94 |
| 98 private: | 95 private: |
| 99 virtual ~MockWifiDataProviderImpl() { | 96 virtual ~MockWifiDataProvider() { |
| 100 CHECK(this == instance_); | 97 CHECK(this == instance_); |
| 101 instance_ = NULL; | 98 instance_ = NULL; |
| 102 } | 99 } |
| 103 | 100 |
| 104 static MockWifiDataProviderImpl* instance_; | 101 static MockWifiDataProvider* instance_; |
| 105 | 102 |
| 106 WifiData data_; | 103 WifiData data_; |
| 107 bool got_data_; | 104 bool got_data_; |
| 108 | 105 |
| 109 DISALLOW_COPY_AND_ASSIGN(MockWifiDataProviderImpl); | 106 DISALLOW_COPY_AND_ASSIGN(MockWifiDataProvider); |
| 110 }; | 107 }; |
| 111 | 108 |
| 112 MockWifiDataProviderImpl* MockWifiDataProviderImpl::instance_ = NULL; | 109 MockWifiDataProvider* MockWifiDataProvider::instance_ = NULL; |
| 113 | 110 |
| 114 // Main test fixture | 111 // Main test fixture |
| 115 class GeolocationNetworkProviderTest : public testing::Test { | 112 class GeolocationNetworkProviderTest : public testing::Test { |
| 116 public: | 113 public: |
| 117 virtual void SetUp() { | 114 virtual void SetUp() { |
| 118 test_server_url_ = GURL(kTestServerUrl); | 115 test_server_url_ = GURL(kTestServerUrl); |
| 119 access_token_store_ = new FakeAccessTokenStore; | 116 access_token_store_ = new FakeAccessTokenStore; |
| 120 wifi_data_provider_ = | 117 wifi_data_provider_ = MockWifiDataProvider::CreateInstance(); |
| 121 MockWifiDataProviderImpl::CreateInstance(); | |
| 122 } | 118 } |
| 123 | 119 |
| 124 virtual void TearDown() { | 120 virtual void TearDown() { WifiDataProviderManager::ResetFactory(); } |
| 125 WifiDataProvider::ResetFactory(); | |
| 126 } | |
| 127 | 121 |
| 128 LocationProvider* CreateProvider(bool set_permission_granted) { | 122 LocationProvider* CreateProvider(bool set_permission_granted) { |
| 129 LocationProvider* provider = NewNetworkLocationProvider( | 123 LocationProvider* provider = NewNetworkLocationProvider( |
| 130 access_token_store_.get(), | 124 access_token_store_.get(), |
| 131 NULL, // No URLContextGetter needed, as using test urlfecther factory. | 125 NULL, // No URLContextGetter needed, as using test urlfecther factory. |
| 132 test_server_url_, | 126 test_server_url_, |
| 133 access_token_store_->access_token_set_[test_server_url_]); | 127 access_token_store_->access_token_set_[test_server_url_]); |
| 134 if (set_permission_granted) | 128 if (set_permission_granted) |
| 135 provider->OnPermissionGranted(); | 129 provider->OnPermissionGranted(); |
| 136 return provider; | 130 return provider; |
| 137 } | 131 } |
| 138 | 132 |
| 139 protected: | 133 protected: |
| 140 GeolocationNetworkProviderTest() { | 134 GeolocationNetworkProviderTest() { |
| 141 // TODO(joth): Really these should be in SetUp, not here, but they take no | 135 // TODO(joth): Really these should be in SetUp, not here, but they take no |
| 142 // effect on Mac OS Release builds if done there. I kid not. Figure out why. | 136 // effect on Mac OS Release builds if done there. I kid not. Figure out why. |
| 143 WifiDataProvider::SetFactory(MockWifiDataProviderImpl::GetInstance); | 137 WifiDataProviderManager::SetFactory(MockWifiDataProvider::GetInstance); |
| 144 } | 138 } |
| 145 | 139 |
| 146 // Returns the current url fetcher (if any) and advances the id ready for the | 140 // Returns the current url fetcher (if any) and advances the id ready for the |
| 147 // next test step. | 141 // next test step. |
| 148 net::TestURLFetcher* get_url_fetcher_and_advance_id() { | 142 net::TestURLFetcher* get_url_fetcher_and_advance_id() { |
| 149 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID( | 143 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID( |
| 150 NetworkLocationRequest::url_fetcher_id_for_tests); | 144 NetworkLocationRequest::url_fetcher_id_for_tests); |
| 151 if (fetcher) | 145 if (fetcher) |
| 152 ++NetworkLocationRequest::url_fetcher_id_for_tests; | 146 ++NetworkLocationRequest::url_fetcher_id_for_tests; |
| 153 return fetcher; | 147 return fetcher; |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 } else { | 308 } else { |
| 315 ASSERT_FALSE(request_json->HasKey("wifiAccessPoints")); | 309 ASSERT_FALSE(request_json->HasKey("wifiAccessPoints")); |
| 316 } | 310 } |
| 317 EXPECT_TRUE(request_url.is_valid()); | 311 EXPECT_TRUE(request_url.is_valid()); |
| 318 } | 312 } |
| 319 | 313 |
| 320 GURL test_server_url_; | 314 GURL test_server_url_; |
| 321 base::MessageLoop main_message_loop_; | 315 base::MessageLoop main_message_loop_; |
| 322 scoped_refptr<FakeAccessTokenStore> access_token_store_; | 316 scoped_refptr<FakeAccessTokenStore> access_token_store_; |
| 323 net::TestURLFetcherFactory url_fetcher_factory_; | 317 net::TestURLFetcherFactory url_fetcher_factory_; |
| 324 scoped_refptr<MockWifiDataProviderImpl> wifi_data_provider_; | 318 scoped_refptr<MockWifiDataProvider> wifi_data_provider_; |
| 325 }; | 319 }; |
| 326 | 320 |
| 327 TEST_F(GeolocationNetworkProviderTest, CreateDestroy) { | 321 TEST_F(GeolocationNetworkProviderTest, CreateDestroy) { |
| 328 // Test fixture members were SetUp correctly. | 322 // Test fixture members were SetUp correctly. |
| 329 EXPECT_EQ(&main_message_loop_, base::MessageLoop::current()); | 323 EXPECT_EQ(&main_message_loop_, base::MessageLoop::current()); |
| 330 scoped_ptr<LocationProvider> provider(CreateProvider(true)); | 324 scoped_ptr<LocationProvider> provider(CreateProvider(true)); |
| 331 EXPECT_TRUE(NULL != provider.get()); | 325 EXPECT_TRUE(NULL != provider.get()); |
| 332 provider.reset(); | 326 provider.reset(); |
| 333 SUCCEED(); | 327 SUCCEED(); |
| 334 } | 328 } |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 555 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(1))); | 549 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(1))); |
| 556 } else { | 550 } else { |
| 557 const int evicted = i - kCacheSize; | 551 const int evicted = i - kCacheSize; |
| 558 EXPECT_FALSE(cache.FindPosition(CreateReferenceWifiScanData(evicted))); | 552 EXPECT_FALSE(cache.FindPosition(CreateReferenceWifiScanData(evicted))); |
| 559 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(evicted + 1))); | 553 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(evicted + 1))); |
| 560 } | 554 } |
| 561 } | 555 } |
| 562 } | 556 } |
| 563 | 557 |
| 564 } // namespace content | 558 } // namespace content |
| OLD | NEW |