| 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 "chrome/browser/metrics/variations/variations_service.h" | 5 #include "chrome/browser/metrics/variations/variations_service.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/json/json_string_value_serializer.h" | 10 #include "base/json/json_string_value_serializer.h" |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 DISALLOW_COPY_AND_ASSIGN(VariationsServiceTest); | 257 DISALLOW_COPY_AND_ASSIGN(VariationsServiceTest); |
| 258 }; | 258 }; |
| 259 | 259 |
| 260 TEST_F(VariationsServiceTest, GetVariationsServerURL) { | 260 TEST_F(VariationsServiceTest, GetVariationsServerURL) { |
| 261 TestVariationsPrefsStore prefs_store; | 261 TestVariationsPrefsStore prefs_store; |
| 262 PrefService* prefs = prefs_store.prefs(); | 262 PrefService* prefs = prefs_store.prefs(); |
| 263 const std::string default_variations_url = | 263 const std::string default_variations_url = |
| 264 VariationsService::GetDefaultVariationsServerURLForTesting(); | 264 VariationsService::GetDefaultVariationsServerURLForTesting(); |
| 265 | 265 |
| 266 std::string value; | 266 std::string value; |
| 267 GURL url = VariationsService::GetVariationsServerURL(prefs, std::string()); | 267 TestVariationsService service( |
| 268 new web_resource::TestRequestAllowedNotifier(prefs), prefs); |
| 269 GURL url = service.GetVariationsServerURL(prefs, std::string()); |
| 268 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url, | 270 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url, |
| 269 base::CompareCase::SENSITIVE)); | 271 base::CompareCase::SENSITIVE)); |
| 270 EXPECT_FALSE(net::GetValueForKeyInQuery(url, "restrict", &value)); | 272 EXPECT_FALSE(net::GetValueForKeyInQuery(url, "restrict", &value)); |
| 271 | 273 |
| 272 prefs_store.SetVariationsRestrictParameterPolicyValue("restricted"); | 274 prefs_store.SetVariationsRestrictParameterPolicyValue("restricted"); |
| 273 url = VariationsService::GetVariationsServerURL(prefs, std::string()); | 275 url = service.GetVariationsServerURL(prefs, std::string()); |
| 274 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url, | 276 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url, |
| 275 base::CompareCase::SENSITIVE)); | 277 base::CompareCase::SENSITIVE)); |
| 276 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "restrict", &value)); | 278 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "restrict", &value)); |
| 277 EXPECT_EQ("restricted", value); | 279 EXPECT_EQ("restricted", value); |
| 278 | 280 |
| 279 // The override value should take precedence over what's in prefs. | 281 // The override value should take precedence over what's in prefs. |
| 280 url = VariationsService::GetVariationsServerURL(prefs, "override"); | 282 url = service.GetVariationsServerURL(prefs, "override"); |
| 281 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url, | 283 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url, |
| 282 base::CompareCase::SENSITIVE)); | 284 base::CompareCase::SENSITIVE)); |
| 283 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "restrict", &value)); | 285 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "restrict", &value)); |
| 284 EXPECT_EQ("override", value); | 286 EXPECT_EQ("override", value); |
| 285 } | 287 } |
| 286 | 288 |
| 287 TEST_F(VariationsServiceTest, VariationsURLHasOSNameParam) { | 289 TEST_F(VariationsServiceTest, VariationsURLHasOSNameParam) { |
| 288 TestingPrefServiceSimple prefs; | 290 TestingPrefServiceSimple prefs; |
| 289 VariationsService::RegisterPrefs(prefs.registry()); | 291 VariationsService::RegisterPrefs(prefs.registry()); |
| 290 const GURL url = | 292 TestVariationsService service( |
| 291 VariationsService::GetVariationsServerURL(&prefs, std::string()); | 293 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs); |
| 294 const GURL url = service.GetVariationsServerURL(&prefs, std::string()); |
| 292 | 295 |
| 293 std::string value; | 296 std::string value; |
| 294 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "osname", &value)); | 297 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "osname", &value)); |
| 295 EXPECT_FALSE(value.empty()); | 298 EXPECT_FALSE(value.empty()); |
| 296 } | 299 } |
| 297 | 300 |
| 298 TEST_F(VariationsServiceTest, RequestsInitiallyNotAllowed) { | 301 TEST_F(VariationsServiceTest, RequestsInitiallyNotAllowed) { |
| 299 TestingPrefServiceSimple prefs; | 302 TestingPrefServiceSimple prefs; |
| 300 VariationsService::RegisterPrefs(prefs.registry()); | 303 VariationsService::RegisterPrefs(prefs.registry()); |
| 301 | 304 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 329 EXPECT_TRUE(test_service.fetch_attempted()); | 332 EXPECT_TRUE(test_service.fetch_attempted()); |
| 330 } | 333 } |
| 331 | 334 |
| 332 TEST_F(VariationsServiceTest, SeedStoredWhenOKStatus) { | 335 TEST_F(VariationsServiceTest, SeedStoredWhenOKStatus) { |
| 333 TestingPrefServiceSimple prefs; | 336 TestingPrefServiceSimple prefs; |
| 334 VariationsService::RegisterPrefs(prefs.registry()); | 337 VariationsService::RegisterPrefs(prefs.registry()); |
| 335 | 338 |
| 336 TestVariationsService service( | 339 TestVariationsService service( |
| 337 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs); | 340 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs); |
| 338 service.variations_server_url_ = | 341 service.variations_server_url_ = |
| 339 VariationsService::GetVariationsServerURL(&prefs, std::string()); | 342 service.GetVariationsServerURL(&prefs, std::string()); |
| 340 service.set_intercepts_fetch(false); | 343 service.set_intercepts_fetch(false); |
| 341 | 344 |
| 342 net::TestURLFetcherFactory factory; | 345 net::TestURLFetcherFactory factory; |
| 343 service.DoActualFetch(); | 346 service.DoActualFetch(); |
| 344 | 347 |
| 345 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 348 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 346 SimulateServerResponse(net::HTTP_OK, fetcher); | 349 SimulateServerResponse(net::HTTP_OK, fetcher); |
| 347 fetcher->SetResponseString(SerializeSeed(CreateTestSeed())); | 350 fetcher->SetResponseString(SerializeSeed(CreateTestSeed())); |
| 348 | 351 |
| 349 EXPECT_FALSE(service.seed_stored()); | 352 EXPECT_FALSE(service.seed_stored()); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 360 net::HTTP_SERVICE_UNAVAILABLE, | 363 net::HTTP_SERVICE_UNAVAILABLE, |
| 361 }; | 364 }; |
| 362 | 365 |
| 363 TestingPrefServiceSimple prefs; | 366 TestingPrefServiceSimple prefs; |
| 364 VariationsService::RegisterPrefs(prefs.registry()); | 367 VariationsService::RegisterPrefs(prefs.registry()); |
| 365 | 368 |
| 366 VariationsService service( | 369 VariationsService service( |
| 367 make_scoped_ptr(new ChromeVariationsServiceClient()), | 370 make_scoped_ptr(new ChromeVariationsServiceClient()), |
| 368 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs, NULL); | 371 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs, NULL); |
| 369 service.variations_server_url_ = | 372 service.variations_server_url_ = |
| 370 VariationsService::GetVariationsServerURL(&prefs, std::string()); | 373 service.GetVariationsServerURL(&prefs, std::string()); |
| 371 for (size_t i = 0; i < arraysize(non_ok_status_codes); ++i) { | 374 for (size_t i = 0; i < arraysize(non_ok_status_codes); ++i) { |
| 372 net::TestURLFetcherFactory factory; | 375 net::TestURLFetcherFactory factory; |
| 373 service.DoActualFetch(); | 376 service.DoActualFetch(); |
| 374 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 377 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 375 | 378 |
| 376 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 379 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 377 SimulateServerResponse(non_ok_status_codes[i], fetcher); | 380 SimulateServerResponse(non_ok_status_codes[i], fetcher); |
| 378 service.OnURLFetchComplete(fetcher); | 381 service.OnURLFetchComplete(fetcher); |
| 379 | 382 |
| 380 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 383 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
| 381 } | 384 } |
| 382 } | 385 } |
| 383 | 386 |
| 384 TEST_F(VariationsServiceTest, CountryHeader) { | 387 TEST_F(VariationsServiceTest, CountryHeader) { |
| 385 TestingPrefServiceSimple prefs; | 388 TestingPrefServiceSimple prefs; |
| 386 VariationsService::RegisterPrefs(prefs.registry()); | 389 VariationsService::RegisterPrefs(prefs.registry()); |
| 387 | 390 |
| 388 TestVariationsService service( | 391 TestVariationsService service( |
| 389 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs); | 392 new web_resource::TestRequestAllowedNotifier(&prefs), &prefs); |
| 390 service.variations_server_url_ = | 393 service.variations_server_url_ = |
| 391 VariationsService::GetVariationsServerURL(&prefs, std::string()); | 394 service.GetVariationsServerURL(&prefs, std::string()); |
| 392 service.set_intercepts_fetch(false); | 395 service.set_intercepts_fetch(false); |
| 393 | 396 |
| 394 net::TestURLFetcherFactory factory; | 397 net::TestURLFetcherFactory factory; |
| 395 service.DoActualFetch(); | 398 service.DoActualFetch(); |
| 396 | 399 |
| 397 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 400 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 398 scoped_refptr<net::HttpResponseHeaders> headers = | 401 scoped_refptr<net::HttpResponseHeaders> headers = |
| 399 SimulateServerResponse(net::HTTP_OK, fetcher); | 402 SimulateServerResponse(net::HTTP_OK, fetcher); |
| 400 headers->AddHeader("X-Country: test"); | 403 headers->AddHeader("X-Country: test"); |
| 401 fetcher->SetResponseString(SerializeSeed(CreateTestSeed())); | 404 fetcher->SetResponseString(SerializeSeed(CreateTestSeed())); |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 547 << test.pref_value_before << ", " << test.version << ", " | 550 << test.pref_value_before << ", " << test.version << ", " |
| 548 << test.latest_country_code; | 551 << test.latest_country_code; |
| 549 | 552 |
| 550 histogram_tester.ExpectUniqueSample( | 553 histogram_tester.ExpectUniqueSample( |
| 551 "Variations.LoadPermanentConsistencyCountryResult", | 554 "Variations.LoadPermanentConsistencyCountryResult", |
| 552 test.expected_result, 1); | 555 test.expected_result, 1); |
| 553 } | 556 } |
| 554 } | 557 } |
| 555 | 558 |
| 556 } // namespace chrome_variations | 559 } // namespace chrome_variations |
| OLD | NEW |