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

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

Issue 603040: Add support for top level geolocation arbitrator, and access token persistenc... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 10 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 | Annotate | Revision Log
OLDNEW
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 <map> 7 #include <map>
8 8
9 #include "base/json/json_reader.h" 9 #include "base/json/json_reader.h"
10 #include "base/scoped_ptr.h" 10 #include "base/scoped_ptr.h"
11 #include "base/string_util.h" 11 #include "base/string_util.h"
12 #include "base/values.h" 12 #include "base/values.h"
13 #include "chrome/browser/geolocation/access_token_store.h"
13 #include "chrome/browser/net/test_url_fetcher_factory.h" 14 #include "chrome/browser/net/test_url_fetcher_factory.h"
14 #include "net/url_request/url_request_status.h" 15 #include "net/url_request/url_request_status.h"
15 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
16 17
17 namespace { 18 namespace {
18 const char kTestServerUrl[] = "https://www.geolocation.test/service"; 19 const char kTestServerUrl[] = "https://www.geolocation.test/service";
19 const char kTestHost[] = "myclienthost.test"; 20 const char kTestHost[] = "myclienthost.test";
20 } // namespace 21 } // namespace
21 22
22 // Stops the specified (nested) message loop when the listener is called back. 23 // Stops the specified (nested) message loop when the listener is called back.
(...skipping 15 matching lines...) Expand all
38 virtual void MovementDetected(LocationProviderBase* provider) { 39 virtual void MovementDetected(LocationProviderBase* provider) {
39 EXPECT_EQ(client_message_loop_, MessageLoop::current()); 40 EXPECT_EQ(client_message_loop_, MessageLoop::current());
40 movement_provider_ = provider; 41 movement_provider_ = provider;
41 client_message_loop_->Quit(); 42 client_message_loop_->Quit();
42 } 43 }
43 MessageLoop* client_message_loop_; 44 MessageLoop* client_message_loop_;
44 LocationProviderBase* updated_provider_; 45 LocationProviderBase* updated_provider_;
45 LocationProviderBase* movement_provider_; 46 LocationProviderBase* movement_provider_;
46 }; 47 };
47 48
48 class FakeAccessTokenStore : public LocationProviderBase::AccessTokenStore { 49 class FakeAccessTokenStore : public AccessTokenStore {
49 public: 50 public:
50 FakeAccessTokenStore() : allow_set_(true) {} 51 FakeAccessTokenStore() : allow_set_(true) {}
51 52
52 virtual bool SetAccessToken(const GURL& url, 53 virtual bool SetAccessToken(const GURL& url,
53 const string16& access_token) { 54 const string16& access_token) {
54 if (!allow_set_) 55 if (!allow_set_)
55 return false; 56 return false;
56 token_map_[url] = access_token; 57 token_map_[url] = access_token;
57 return true; 58 return true;
58 } 59 }
59 virtual bool GetAccessToken(const GURL& url, string16* access_token) { 60 virtual bool GetAccessToken(const GURL& url, string16* access_token) {
60 std::map<GURL, string16>::iterator item = token_map_.find(url); 61 std::map<GURL, string16>::iterator item = token_map_.find(url);
61 if (item == token_map_.end()) 62 if (item == token_map_.end())
62 return false; 63 return false;
63 *access_token = item->second; 64 *access_token = item->second;
64 return true; 65 return true;
65 } 66 }
66 bool allow_set_; 67 bool allow_set_;
67 std::map<GURL, string16> token_map_; 68 std::map<GURL, string16> token_map_;
69
70 private:
71 ~FakeAccessTokenStore() {}
68 }; 72 };
69 73
70 74
71 // A mock implementation of DeviceDataProviderImplBase for testing. Adapted from 75 // A mock implementation of DeviceDataProviderImplBase for testing. Adapted from
72 // http://gears.googlecode.com/svn/trunk/gears/geolocation/geolocation_test.cc 76 // http://gears.googlecode.com/svn/trunk/gears/geolocation/geolocation_test.cc
73 template<typename DataType> 77 template<typename DataType>
74 class MockDeviceDataProviderImpl 78 class MockDeviceDataProviderImpl
75 : public DeviceDataProviderImplBase<DataType> { 79 : public DeviceDataProviderImplBase<DataType> {
76 public: 80 public:
77 // Factory method for use with DeviceDataProvider::SetFactory. 81 // Factory method for use with DeviceDataProvider::SetFactory.
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 129
126 template<typename DataType> 130 template<typename DataType>
127 MockDeviceDataProviderImpl<DataType>* 131 MockDeviceDataProviderImpl<DataType>*
128 MockDeviceDataProviderImpl<DataType>::instance_ = NULL; 132 MockDeviceDataProviderImpl<DataType>::instance_ = NULL;
129 133
130 // Main test fixture 134 // Main test fixture
131 class GeolocationNetworkProviderTest : public testing::Test { 135 class GeolocationNetworkProviderTest : public testing::Test {
132 public: 136 public:
133 virtual void SetUp() { 137 virtual void SetUp() {
134 URLFetcher::set_factory(&url_fetcher_factory_); 138 URLFetcher::set_factory(&url_fetcher_factory_);
139 access_token_store_ = new FakeAccessTokenStore;
135 } 140 }
136 141
137 virtual void TearDown() { 142 virtual void TearDown() {
138 WifiDataProvider::ResetFactory(); 143 WifiDataProvider::ResetFactory();
139 RadioDataProvider::ResetFactory(); 144 RadioDataProvider::ResetFactory();
140 URLFetcher::set_factory(NULL); 145 URLFetcher::set_factory(NULL);
141 base::LeakTracker<URLFetcher>::CheckForLeaks(); 146 base::LeakTracker<URLFetcher>::CheckForLeaks();
142 } 147 }
143 148
144 LocationProviderBase* CreateProvider() { 149 LocationProviderBase* CreateProvider() {
145 return NewNetworkLocationProvider( 150 return NewNetworkLocationProvider(
146 &access_token_store_, 151 access_token_store_.get(),
147 NULL, // No URLContextGetter needed, as using test urlfecther factory. 152 NULL, // No URLContextGetter needed, as using test urlfecther factory.
148 test_server_url_, 153 test_server_url_,
149 ASCIIToUTF16(kTestHost)); 154 ASCIIToUTF16(kTestHost));
150 } 155 }
151 156
152 protected: 157 protected:
153 GeolocationNetworkProviderTest() : test_server_url_(kTestServerUrl) { 158 GeolocationNetworkProviderTest() : test_server_url_(kTestServerUrl) {
154 // TODO(joth): Really these should be in SetUp, not here, but they take no 159 // TODO(joth): Really these should be in SetUp, not here, but they take no
155 // effect on Mac OS Release builds if done there. I kid not. Figure out why. 160 // effect on Mac OS Release builds if done there. I kid not. Figure out why.
156 RadioDataProvider::SetFactory( 161 RadioDataProvider::SetFactory(
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 EXPECT_EQ(expected->signal_to_noise, actual->signal_to_noise) << i; 251 EXPECT_EQ(expected->signal_to_noise, actual->signal_to_noise) << i;
247 EXPECT_EQ(expected->ssid, actual->ssid) << i; 252 EXPECT_EQ(expected->ssid, actual->ssid) << i;
248 ++expected; 253 ++expected;
249 ++actual; 254 ++actual;
250 } 255 }
251 EXPECT_EQ(expected_access_token, access_token); 256 EXPECT_EQ(expected_access_token, access_token);
252 } 257 }
253 258
254 const GURL test_server_url_; 259 const GURL test_server_url_;
255 MessageLoop main_message_loop_; 260 MessageLoop main_message_loop_;
256 FakeAccessTokenStore access_token_store_; 261 scoped_refptr<FakeAccessTokenStore> access_token_store_;
257 TestURLFetcherFactory url_fetcher_factory_; 262 TestURLFetcherFactory url_fetcher_factory_;
258 }; 263 };
259 264
260 265
261 TEST_F(GeolocationNetworkProviderTest, CreateDestroy) { 266 TEST_F(GeolocationNetworkProviderTest, CreateDestroy) {
262 // Test fixture members were SetUp correctly. 267 // Test fixture members were SetUp correctly.
263 EXPECT_EQ(&main_message_loop_, MessageLoop::current()); 268 EXPECT_EQ(&main_message_loop_, MessageLoop::current());
264 scoped_ptr<LocationProviderBase> provider(CreateProvider()); 269 scoped_ptr<LocationProviderBase> provider(CreateProvider());
265 EXPECT_TRUE(NULL != provider.get()); 270 EXPECT_TRUE(NULL != provider.get());
266 provider.reset(); 271 provider.reset();
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 const char* kNoFixNetworkResponse = 313 const char* kNoFixNetworkResponse =
309 "{" 314 "{"
310 " \"location\": null," 315 " \"location\": null,"
311 " \"access_token\": \"" REFERENCE_ACCESS_TOKEN "\"" 316 " \"access_token\": \"" REFERENCE_ACCESS_TOKEN "\""
312 "}"; 317 "}";
313 fetcher->delegate()->OnURLFetchComplete( 318 fetcher->delegate()->OnURLFetchComplete(
314 fetcher, test_server_url_, URLRequestStatus(), 200, // OK 319 fetcher, test_server_url_, URLRequestStatus(), 200, // OK
315 ResponseCookies(), kNoFixNetworkResponse); 320 ResponseCookies(), kNoFixNetworkResponse);
316 321
317 // This should have set the access token anyhow 322 // This should have set the access token anyhow
318 EXPECT_EQ(1, static_cast<int>(access_token_store_.token_map_.size())); 323 EXPECT_EQ(1, static_cast<int>(access_token_store_->token_map_.size()));
319 string16 token; 324 string16 token;
320 EXPECT_TRUE(access_token_store_.GetAccessToken(test_server_url_, &token)); 325 EXPECT_TRUE(access_token_store_->GetAccessToken(test_server_url_, &token));
321 EXPECT_EQ(REFERENCE_ACCESS_TOKEN, UTF16ToUTF8(token)); 326 EXPECT_EQ(REFERENCE_ACCESS_TOKEN, UTF16ToUTF8(token));
322 327
323 Position position; 328 Position position;
324 provider->GetPosition(&position); 329 provider->GetPosition(&position);
325 EXPECT_FALSE(position.IsValidFix()); 330 EXPECT_FALSE(position.IsValidFix());
326 331
327 // Now wifi data arrives 332 // Now wifi data arrives
328 const int kFirstScanAps = 6; 333 const int kFirstScanAps = 6;
329 MockDeviceDataProviderImpl<WifiData>::instance()->SetData( 334 MockDeviceDataProviderImpl<WifiData>::instance()->SetData(
330 CreateReferenceWifiScanData(kFirstScanAps)); // Will notify listeners 335 CreateReferenceWifiScanData(kFirstScanAps)); // Will notify listeners
(...skipping 23 matching lines...) Expand all
354 provider->GetPosition(&position); 359 provider->GetPosition(&position);
355 EXPECT_EQ(51.0, position.latitude); 360 EXPECT_EQ(51.0, position.latitude);
356 EXPECT_EQ(-0.1, position.longitude); 361 EXPECT_EQ(-0.1, position.longitude);
357 EXPECT_EQ(30.1, position.altitude); 362 EXPECT_EQ(30.1, position.altitude);
358 EXPECT_EQ(1200.4, position.accuracy); 363 EXPECT_EQ(1200.4, position.accuracy);
359 EXPECT_EQ(10.6, position.altitude_accuracy); 364 EXPECT_EQ(10.6, position.altitude_accuracy);
360 EXPECT_TRUE(position.is_valid_timestamp()); 365 EXPECT_TRUE(position.is_valid_timestamp());
361 EXPECT_TRUE(position.IsValidFix()); 366 EXPECT_TRUE(position.IsValidFix());
362 367
363 // Token should still be in the store. 368 // Token should still be in the store.
364 EXPECT_EQ(1, static_cast<int>(access_token_store_.token_map_.size())); 369 EXPECT_EQ(1, static_cast<int>(access_token_store_->token_map_.size()));
365 EXPECT_TRUE(access_token_store_.GetAccessToken(test_server_url_, &token)); 370 EXPECT_TRUE(access_token_store_->GetAccessToken(test_server_url_, &token));
366 EXPECT_EQ(REFERENCE_ACCESS_TOKEN, UTF16ToUTF8(token)); 371 EXPECT_EQ(REFERENCE_ACCESS_TOKEN, UTF16ToUTF8(token));
367 372
368 // Wifi updated again, with one less AP. This is 'close enough' to the 373 // Wifi updated again, with one less AP. This is 'close enough' to the
369 // previous scan, so no new request made. 374 // previous scan, so no new request made.
370 const int kSecondScanAps = kFirstScanAps - 1; 375 const int kSecondScanAps = kFirstScanAps - 1;
371 MockDeviceDataProviderImpl<WifiData>::instance()->SetData( 376 MockDeviceDataProviderImpl<WifiData>::instance()->SetData(
372 CreateReferenceWifiScanData(kSecondScanAps)); 377 CreateReferenceWifiScanData(kSecondScanAps));
373 main_message_loop_.RunAllPending(); 378 main_message_loop_.RunAllPending();
374 fetcher = url_fetcher_factory_.GetFetcherByID(kSecondScanAps); 379 fetcher = url_fetcher_factory_.GetFetcherByID(kSecondScanAps);
375 EXPECT_FALSE(fetcher); 380 EXPECT_FALSE(fetcher);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 EXPECT_EQ(orig_fetcher, fetcher); // No new request created. 413 EXPECT_EQ(orig_fetcher, fetcher); // No new request created.
409 414
410 provider->GetPosition(&position); 415 provider->GetPosition(&position);
411 EXPECT_EQ(51.0, position.latitude); 416 EXPECT_EQ(51.0, position.latitude);
412 EXPECT_EQ(-0.1, position.longitude); 417 EXPECT_EQ(-0.1, position.longitude);
413 EXPECT_TRUE(position.IsValidFix()); 418 EXPECT_TRUE(position.IsValidFix());
414 } 419 }
415 420
416 // TODO(joth): Add tests for corner cases around the 2 second startup delay 421 // TODO(joth): Add tests for corner cases around the 2 second startup delay
417 // (e.g. timer firing, or being pre-empted by data arriving) 422 // (e.g. timer firing, or being pre-empted by data arriving)
OLDNEW
« no previous file with comments | « chrome/browser/geolocation/network_location_provider.cc ('k') | chrome/browser/geolocation/network_location_request.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698