Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(589)

Side by Side Diff: content/browser/geolocation/network_location_provider_unittest.cc

Issue 10535157: [WIP] attempt to allow chrome OS to inject its wifi data provider (Closed) Base URL: http://git.chromium.org/chromium/src.git@remove_radio
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 updated_provider_ = provider; 51 updated_provider_ = provider;
52 client_message_loop_->Quit(); 52 client_message_loop_->Quit();
53 } 53 }
54 MessageLoop* client_message_loop_; 54 MessageLoop* client_message_loop_;
55 LocationProviderBase* updated_provider_; 55 LocationProviderBase* updated_provider_;
56 LocationProviderBase* movement_provider_; 56 LocationProviderBase* movement_provider_;
57 }; 57 };
58 58
59 // A mock implementation of DeviceDataProviderImplBase for testing. Adapted from 59 // A mock implementation of DeviceDataProviderImplBase for testing. Adapted from
60 // http://gears.googlecode.com/svn/trunk/gears/geolocation/geolocation_test.cc 60 // http://gears.googlecode.com/svn/trunk/gears/geolocation/geolocation_test.cc
61 template<typename DataType> 61 class MockWifiDataProviderImpl : public WifiDataProviderImplBase {
62 class MockDeviceDataProviderImpl
63 : public DeviceDataProviderImplBase<DataType> {
64 public: 62 public:
65 // Factory method for use with DeviceDataProvider::SetFactory. 63 // Factory method for use with DeviceDataProvider::SetFactory.
66 static DeviceDataProviderImplBase<DataType>* GetInstance() { 64 static WifiDataProviderImplBase* GetInstance() {
67 CHECK(instance_); 65 CHECK(instance_);
68 return instance_; 66 return instance_;
69 } 67 }
70 68
71 static MockDeviceDataProviderImpl<DataType>* CreateInstance() { 69 static MockWifiDataProviderImpl* CreateInstance() {
72 CHECK(!instance_); 70 CHECK(!instance_);
73 instance_ = new MockDeviceDataProviderImpl<DataType>; 71 instance_ = new MockWifiDataProviderImpl;
74 return instance_; 72 return instance_;
75 } 73 }
76 74
77 MockDeviceDataProviderImpl() 75 MockWifiDataProviderImpl()
78 : start_calls_(0), 76 : start_calls_(0),
79 stop_calls_(0), 77 stop_calls_(0),
80 got_data_(true) { 78 got_data_(true) {
81 } 79 }
82 80
83 virtual ~MockDeviceDataProviderImpl() { 81 virtual ~MockWifiDataProviderImpl() {
84 CHECK(this == instance_); 82 CHECK(this == instance_);
85 instance_ = NULL; 83 instance_ = NULL;
86 } 84 }
87 85
88 // DeviceDataProviderImplBase implementation. 86 // DeviceDataProviderImplBase implementation.
89 virtual bool StartDataProvider() { 87 virtual bool StartDataProvider() {
90 ++start_calls_; 88 ++start_calls_;
91 return true; 89 return true;
92 } 90 }
93 virtual void StopDataProvider() { 91 virtual void StopDataProvider() {
94 ++stop_calls_; 92 ++stop_calls_;
95 } 93 }
96 virtual bool GetData(DataType* data_out) { 94 virtual bool GetData(WifiData* data_out) {
97 CHECK(data_out); 95 CHECK(data_out);
98 *data_out = data_; 96 *data_out = data_;
99 return got_data_; 97 return got_data_;
100 } 98 }
101 99
102 void SetData(const DataType& new_data) { 100 void SetData(const WifiData& new_data) {
103 got_data_ = true; 101 got_data_ = true;
104 const bool differs = data_.DiffersSignificantly(new_data); 102 const bool differs = data_.DiffersSignificantly(new_data);
105 data_ = new_data; 103 data_ = new_data;
106 if (differs) 104 if (differs)
107 this->NotifyListeners(); 105 this->NotifyListeners();
108 } 106 }
109 107
110 void set_got_data(bool got_data) { got_data_ = got_data; } 108 void set_got_data(bool got_data) { got_data_ = got_data; }
111 int start_calls_; 109 int start_calls_;
112 int stop_calls_; 110 int stop_calls_;
113 111
114 private: 112 private:
115 static MockDeviceDataProviderImpl<DataType>* instance_; 113 static MockWifiDataProviderImpl* instance_;
116 114
117 DataType data_; 115 WifiData data_;
118 bool got_data_; 116 bool got_data_;
119 117
120 DISALLOW_COPY_AND_ASSIGN(MockDeviceDataProviderImpl); 118 DISALLOW_COPY_AND_ASSIGN(MockWifiDataProviderImpl);
121 }; 119 };
122 120
123 template<typename DataType> 121 MockWifiDataProviderImpl* MockWifiDataProviderImpl::instance_ = NULL;
124 MockDeviceDataProviderImpl<DataType>*
125 MockDeviceDataProviderImpl<DataType>::instance_ = NULL;
126 122
127 // Main test fixture 123 // Main test fixture
128 class GeolocationNetworkProviderTest : public testing::Test { 124 class GeolocationNetworkProviderTest : public testing::Test {
129 public: 125 public:
130 virtual void SetUp() { 126 virtual void SetUp() {
131 access_token_store_ = new FakeAccessTokenStore; 127 access_token_store_ = new FakeAccessTokenStore;
132 wifi_data_provider_ = 128 wifi_data_provider_ = MockWifiDataProviderImpl::CreateInstance();
133 MockDeviceDataProviderImpl<WifiData>::CreateInstance();
134 } 129 }
135 130
136 virtual void TearDown() { 131 virtual void TearDown() {
137 WifiDataProvider::ResetFactory(); 132 WifiDataProvider::ResetFactory();
138 } 133 }
139 134
140 LocationProviderBase* CreateProvider(bool set_permission_granted) { 135 LocationProviderBase* CreateProvider(bool set_permission_granted) {
141 LocationProviderBase* provider = NewNetworkLocationProvider( 136 LocationProviderBase* provider = NewNetworkLocationProvider(
142 access_token_store_.get(), 137 access_token_store_.get(),
143 NULL, // No URLContextGetter needed, as using test urlfecther factory. 138 NULL, // No URLContextGetter needed, as using test urlfecther factory.
144 test_server_url_, 139 test_server_url_,
145 access_token_store_->access_token_set_[test_server_url_]); 140 access_token_store_->access_token_set_[test_server_url_]);
146 if (set_permission_granted) 141 if (set_permission_granted)
147 provider->OnPermissionGranted(); 142 provider->OnPermissionGranted();
148 return provider; 143 return provider;
149 } 144 }
150 145
151 protected: 146 protected:
152 GeolocationNetworkProviderTest() : test_server_url_(kTestServerUrl) { 147 GeolocationNetworkProviderTest() : test_server_url_(kTestServerUrl) {
153 // TODO(joth): Really these should be in SetUp, not here, but they take no 148 // TODO(joth): Really these should be in SetUp, not here, but they take no
154 // effect on Mac OS Release builds if done there. I kid not. Figure out why. 149 // effect on Mac OS Release builds if done there. I kid not. Figure out why.
155 WifiDataProvider::SetFactory( 150 WifiDataProvider::SetFactory(MockWifiDataProviderImpl::GetInstance);
156 MockDeviceDataProviderImpl<WifiData>::GetInstance);
157 } 151 }
158 152
159 // Returns the current url fetcher (if any) and advances the id ready for the 153 // Returns the current url fetcher (if any) and advances the id ready for the
160 // next test step. 154 // next test step.
161 TestURLFetcher* get_url_fetcher_and_advance_id() { 155 TestURLFetcher* get_url_fetcher_and_advance_id() {
162 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID( 156 TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(
163 NetworkLocationRequest::url_fetcher_id_for_tests); 157 NetworkLocationRequest::url_fetcher_id_for_tests);
164 if (fetcher) 158 if (fetcher)
165 ++NetworkLocationRequest::url_fetcher_id_for_tests; 159 ++NetworkLocationRequest::url_fetcher_id_for_tests;
166 return fetcher; 160 return fetcher;
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 EXPECT_EQ(expected_wifi_tokens[5], actual_wifi_tokens[5]); 255 EXPECT_EQ(expected_wifi_tokens[5], actual_wifi_tokens[5]);
262 EXPECT_EQ(expected_wifi_tokens[6], actual_wifi_tokens[6]); 256 EXPECT_EQ(expected_wifi_tokens[6], actual_wifi_tokens[6]);
263 } 257 }
264 EXPECT_TRUE(GURL(request_url).is_valid()); 258 EXPECT_TRUE(GURL(request_url).is_valid());
265 } 259 }
266 260
267 const GURL test_server_url_; 261 const GURL test_server_url_;
268 MessageLoop main_message_loop_; 262 MessageLoop main_message_loop_;
269 scoped_refptr<FakeAccessTokenStore> access_token_store_; 263 scoped_refptr<FakeAccessTokenStore> access_token_store_;
270 TestURLFetcherFactory url_fetcher_factory_; 264 TestURLFetcherFactory url_fetcher_factory_;
271 scoped_refptr<MockDeviceDataProviderImpl<WifiData> > wifi_data_provider_; 265 scoped_refptr<MockWifiDataProviderImpl> wifi_data_provider_;
272 }; 266 };
273 267
274 268
275 TEST_F(GeolocationNetworkProviderTest, CreateDestroy) { 269 TEST_F(GeolocationNetworkProviderTest, CreateDestroy) {
276 // Test fixture members were SetUp correctly. 270 // Test fixture members were SetUp correctly.
277 EXPECT_EQ(&main_message_loop_, MessageLoop::current()); 271 EXPECT_EQ(&main_message_loop_, MessageLoop::current());
278 scoped_ptr<LocationProviderBase> provider(CreateProvider(true)); 272 scoped_ptr<LocationProviderBase> provider(CreateProvider(true));
279 EXPECT_TRUE(NULL != provider.get()); 273 EXPECT_TRUE(NULL != provider.get());
280 provider.reset(); 274 provider.reset();
281 SUCCEED(); 275 SUCCEED();
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
519 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(1))); 513 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(1)));
520 } else { 514 } else {
521 const int evicted = i - kCacheSize; 515 const int evicted = i - kCacheSize;
522 EXPECT_FALSE(cache.FindPosition(CreateReferenceWifiScanData(evicted))); 516 EXPECT_FALSE(cache.FindPosition(CreateReferenceWifiScanData(evicted)));
523 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(evicted + 1))); 517 EXPECT_TRUE(cache.FindPosition(CreateReferenceWifiScanData(evicted + 1)));
524 } 518 }
525 } 519 }
526 } 520 }
527 521
528 } // namespace 522 } // namespace
OLDNEW
« no previous file with comments | « content/browser/geolocation/network_location_provider.h ('k') | content/browser/geolocation/network_location_request.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698