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 "components/variations/service/variations_service.h" | 5 #include "components/variations/service/variations_service.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 |
| 9 #include <memory> |
8 #include <utility> | 10 #include <utility> |
9 #include <vector> | 11 #include <vector> |
10 | 12 |
11 #include "base/base64.h" | 13 #include "base/base64.h" |
12 #include "base/feature_list.h" | 14 #include "base/feature_list.h" |
13 #include "base/json/json_string_value_serializer.h" | 15 #include "base/json/json_string_value_serializer.h" |
14 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/ptr_util.h" |
15 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
16 #include "base/sha1.h" | 19 #include "base/sha1.h" |
17 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
18 #include "base/strings/string_split.h" | 21 #include "base/strings/string_split.h" |
19 #include "base/strings/string_util.h" | 22 #include "base/strings/string_util.h" |
20 #include "base/test/histogram_tester.h" | 23 #include "base/test/histogram_tester.h" |
21 #include "base/version.h" | 24 #include "base/version.h" |
22 #include "components/prefs/testing_pref_service.h" | 25 #include "components/prefs/testing_pref_service.h" |
23 #include "components/variations/pref_names.h" | 26 #include "components/variations/pref_names.h" |
24 #include "components/variations/proto/study.pb.h" | 27 #include "components/variations/proto/study.pb.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 private: | 73 private: |
71 std::string restrict_parameter_; | 74 std::string restrict_parameter_; |
72 | 75 |
73 DISALLOW_COPY_AND_ASSIGN(TestVariationsServiceClient); | 76 DISALLOW_COPY_AND_ASSIGN(TestVariationsServiceClient); |
74 }; | 77 }; |
75 | 78 |
76 // A test class used to validate expected functionality in VariationsService. | 79 // A test class used to validate expected functionality in VariationsService. |
77 class TestVariationsService : public VariationsService { | 80 class TestVariationsService : public VariationsService { |
78 public: | 81 public: |
79 TestVariationsService( | 82 TestVariationsService( |
80 scoped_ptr<web_resource::TestRequestAllowedNotifier> test_notifier, | 83 std::unique_ptr<web_resource::TestRequestAllowedNotifier> test_notifier, |
81 PrefService* local_state) | 84 PrefService* local_state) |
82 : VariationsService(make_scoped_ptr(new TestVariationsServiceClient()), | 85 : VariationsService(base::WrapUnique(new TestVariationsServiceClient()), |
83 std::move(test_notifier), | 86 std::move(test_notifier), |
84 local_state, | 87 local_state, |
85 NULL, | 88 NULL, |
86 UIStringOverrider()), | 89 UIStringOverrider()), |
87 intercepts_fetch_(true), | 90 intercepts_fetch_(true), |
88 fetch_attempted_(false), | 91 fetch_attempted_(false), |
89 seed_stored_(false), | 92 seed_stored_(false), |
90 delta_compressed_seed_(false), | 93 delta_compressed_seed_(false), |
91 gzip_compressed_seed_(false) { | 94 gzip_compressed_seed_(false) { |
92 // Set this so StartRepeatedVariationsSeedFetch can be called in tests. | 95 // Set this so StartRepeatedVariationsSeedFetch can be called in tests. |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 | 262 |
260 DISALLOW_COPY_AND_ASSIGN(VariationsServiceTest); | 263 DISALLOW_COPY_AND_ASSIGN(VariationsServiceTest); |
261 }; | 264 }; |
262 | 265 |
263 TEST_F(VariationsServiceTest, CreateTrialsFromSeed) { | 266 TEST_F(VariationsServiceTest, CreateTrialsFromSeed) { |
264 TestingPrefServiceSimple prefs; | 267 TestingPrefServiceSimple prefs; |
265 VariationsService::RegisterPrefs(prefs.registry()); | 268 VariationsService::RegisterPrefs(prefs.registry()); |
266 | 269 |
267 // Setup base::FeatureList. | 270 // Setup base::FeatureList. |
268 base::FeatureList::ClearInstanceForTesting(); | 271 base::FeatureList::ClearInstanceForTesting(); |
269 base::FeatureList::SetInstance(make_scoped_ptr(new base::FeatureList())); | 272 base::FeatureList::SetInstance(base::WrapUnique(new base::FeatureList())); |
270 | 273 |
271 // Create a local base::FieldTrialList, to hold the field trials created in | 274 // Create a local base::FieldTrialList, to hold the field trials created in |
272 // this test. | 275 // this test. |
273 base::FieldTrialList field_trial_list(nullptr); | 276 base::FieldTrialList field_trial_list(nullptr); |
274 | 277 |
275 // Create a variations service. | 278 // Create a variations service. |
276 TestVariationsService service( | 279 TestVariationsService service( |
277 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), | 280 base::WrapUnique(new web_resource::TestRequestAllowedNotifier(&prefs)), |
278 &prefs); | 281 &prefs); |
279 service.SetCreateTrialsFromSeedCalledForTesting(false); | 282 service.SetCreateTrialsFromSeedCalledForTesting(false); |
280 | 283 |
281 // Store a seed. | 284 // Store a seed. |
282 service.StoreSeed(SerializeSeed(CreateTestSeed()), std::string(), | 285 service.StoreSeed(SerializeSeed(CreateTestSeed()), std::string(), |
283 std::string(), base::Time::Now(), false, false); | 286 std::string(), base::Time::Now(), false, false); |
284 prefs.SetInt64(prefs::kVariationsLastFetchTime, | 287 prefs.SetInt64(prefs::kVariationsLastFetchTime, |
285 base::Time::Now().ToInternalValue()); | 288 base::Time::Now().ToInternalValue()); |
286 | 289 |
287 // Check that field trials are created from the seed. Since the test study has | 290 // Check that field trials are created from the seed. Since the test study has |
288 // only 1 experiment with 100% probability weight, we must be part of it. | 291 // only 1 experiment with 100% probability weight, we must be part of it. |
289 EXPECT_TRUE(service.CreateTrialsFromSeed(base::FeatureList::GetInstance())); | 292 EXPECT_TRUE(service.CreateTrialsFromSeed(base::FeatureList::GetInstance())); |
290 EXPECT_EQ(base::FieldTrialList::FindFullName(kTestSeedStudyName), | 293 EXPECT_EQ(base::FieldTrialList::FindFullName(kTestSeedStudyName), |
291 kTestSeedExperimentName); | 294 kTestSeedExperimentName); |
292 } | 295 } |
293 | 296 |
294 TEST_F(VariationsServiceTest, CreateTrialsFromSeedNoLastFetchTime) { | 297 TEST_F(VariationsServiceTest, CreateTrialsFromSeedNoLastFetchTime) { |
295 TestingPrefServiceSimple prefs; | 298 TestingPrefServiceSimple prefs; |
296 VariationsService::RegisterPrefs(prefs.registry()); | 299 VariationsService::RegisterPrefs(prefs.registry()); |
297 | 300 |
298 // Setup base::FeatureList. | 301 // Setup base::FeatureList. |
299 base::FeatureList::ClearInstanceForTesting(); | 302 base::FeatureList::ClearInstanceForTesting(); |
300 base::FeatureList::SetInstance(make_scoped_ptr(new base::FeatureList())); | 303 base::FeatureList::SetInstance(base::WrapUnique(new base::FeatureList())); |
301 | 304 |
302 // Create a local base::FieldTrialList, to hold the field trials created in | 305 // Create a local base::FieldTrialList, to hold the field trials created in |
303 // this test. | 306 // this test. |
304 base::FieldTrialList field_trial_list(nullptr); | 307 base::FieldTrialList field_trial_list(nullptr); |
305 | 308 |
306 // Create a variations service | 309 // Create a variations service |
307 TestVariationsService service( | 310 TestVariationsService service( |
308 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), | 311 base::WrapUnique(new web_resource::TestRequestAllowedNotifier(&prefs)), |
309 &prefs); | 312 &prefs); |
310 service.SetCreateTrialsFromSeedCalledForTesting(false); | 313 service.SetCreateTrialsFromSeedCalledForTesting(false); |
311 | 314 |
312 // Store a seed. To simulate a first run, |prefs::kVariationsLastFetchTime| | 315 // Store a seed. To simulate a first run, |prefs::kVariationsLastFetchTime| |
313 // is left empty. | 316 // is left empty. |
314 service.StoreSeed(SerializeSeed(CreateTestSeed()), std::string(), | 317 service.StoreSeed(SerializeSeed(CreateTestSeed()), std::string(), |
315 std::string(), base::Time::Now(), false, false); | 318 std::string(), base::Time::Now(), false, false); |
316 EXPECT_EQ(0, prefs.GetInt64(prefs::kVariationsLastFetchTime)); | 319 EXPECT_EQ(0, prefs.GetInt64(prefs::kVariationsLastFetchTime)); |
317 | 320 |
318 // Check that field trials are created from the seed. Since the test study has | 321 // Check that field trials are created from the seed. Since the test study has |
319 // only 1 experiment with 100% probability weight, we must be part of it. | 322 // only 1 experiment with 100% probability weight, we must be part of it. |
320 EXPECT_TRUE(service.CreateTrialsFromSeed(base::FeatureList::GetInstance())); | 323 EXPECT_TRUE(service.CreateTrialsFromSeed(base::FeatureList::GetInstance())); |
321 EXPECT_EQ(base::FieldTrialList::FindFullName(kTestSeedStudyName), | 324 EXPECT_EQ(base::FieldTrialList::FindFullName(kTestSeedStudyName), |
322 kTestSeedExperimentName); | 325 kTestSeedExperimentName); |
323 } | 326 } |
324 | 327 |
325 TEST_F(VariationsServiceTest, CreateTrialsFromOutdatedSeed) { | 328 TEST_F(VariationsServiceTest, CreateTrialsFromOutdatedSeed) { |
326 TestingPrefServiceSimple prefs; | 329 TestingPrefServiceSimple prefs; |
327 VariationsService::RegisterPrefs(prefs.registry()); | 330 VariationsService::RegisterPrefs(prefs.registry()); |
328 | 331 |
329 // Setup base::FeatureList. | 332 // Setup base::FeatureList. |
330 base::FeatureList::ClearInstanceForTesting(); | 333 base::FeatureList::ClearInstanceForTesting(); |
331 base::FeatureList::SetInstance(make_scoped_ptr(new base::FeatureList())); | 334 base::FeatureList::SetInstance(base::WrapUnique(new base::FeatureList())); |
332 | 335 |
333 // Create a local base::FieldTrialList, to hold the field trials created in | 336 // Create a local base::FieldTrialList, to hold the field trials created in |
334 // this test. | 337 // this test. |
335 base::FieldTrialList field_trial_list(nullptr); | 338 base::FieldTrialList field_trial_list(nullptr); |
336 | 339 |
337 // Create a variations service. | 340 // Create a variations service. |
338 TestVariationsService service( | 341 TestVariationsService service( |
339 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), | 342 base::WrapUnique(new web_resource::TestRequestAllowedNotifier(&prefs)), |
340 &prefs); | 343 &prefs); |
341 service.SetCreateTrialsFromSeedCalledForTesting(false); | 344 service.SetCreateTrialsFromSeedCalledForTesting(false); |
342 | 345 |
343 // Store a seed, with a fetch time 31 days in the past. | 346 // Store a seed, with a fetch time 31 days in the past. |
344 const base::Time seed_date = | 347 const base::Time seed_date = |
345 base::Time::Now() - base::TimeDelta::FromDays(31); | 348 base::Time::Now() - base::TimeDelta::FromDays(31); |
346 service.StoreSeed(SerializeSeed(CreateTestSeed()), std::string(), | 349 service.StoreSeed(SerializeSeed(CreateTestSeed()), std::string(), |
347 std::string(), seed_date, false, false); | 350 std::string(), seed_date, false, false); |
348 prefs.SetInt64(prefs::kVariationsLastFetchTime, seed_date.ToInternalValue()); | 351 prefs.SetInt64(prefs::kVariationsLastFetchTime, seed_date.ToInternalValue()); |
349 | 352 |
350 // Check that field trials are not created from the seed. | 353 // Check that field trials are not created from the seed. |
351 EXPECT_FALSE(service.CreateTrialsFromSeed(base::FeatureList::GetInstance())); | 354 EXPECT_FALSE(service.CreateTrialsFromSeed(base::FeatureList::GetInstance())); |
352 EXPECT_TRUE(base::FieldTrialList::FindFullName(kTestSeedStudyName).empty()); | 355 EXPECT_TRUE(base::FieldTrialList::FindFullName(kTestSeedStudyName).empty()); |
353 } | 356 } |
354 | 357 |
355 TEST_F(VariationsServiceTest, GetVariationsServerURL) { | 358 TEST_F(VariationsServiceTest, GetVariationsServerURL) { |
356 TestingPrefServiceSimple prefs; | 359 TestingPrefServiceSimple prefs; |
357 VariationsService::RegisterPrefs(prefs.registry()); | 360 VariationsService::RegisterPrefs(prefs.registry()); |
358 const std::string default_variations_url = | 361 const std::string default_variations_url = |
359 VariationsService::GetDefaultVariationsServerURLForTesting(); | 362 VariationsService::GetDefaultVariationsServerURLForTesting(); |
360 | 363 |
361 std::string value; | 364 std::string value; |
362 scoped_ptr<TestVariationsServiceClient> client = | 365 std::unique_ptr<TestVariationsServiceClient> client = |
363 make_scoped_ptr(new TestVariationsServiceClient()); | 366 base::WrapUnique(new TestVariationsServiceClient()); |
364 TestVariationsServiceClient* raw_client = client.get(); | 367 TestVariationsServiceClient* raw_client = client.get(); |
365 VariationsService service( | 368 VariationsService service( |
366 std::move(client), | 369 std::move(client), |
367 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), | 370 base::WrapUnique(new web_resource::TestRequestAllowedNotifier(&prefs)), |
368 &prefs, NULL, UIStringOverrider()); | 371 &prefs, NULL, UIStringOverrider()); |
369 GURL url = service.GetVariationsServerURL(&prefs, std::string()); | 372 GURL url = service.GetVariationsServerURL(&prefs, std::string()); |
370 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url, | 373 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url, |
371 base::CompareCase::SENSITIVE)); | 374 base::CompareCase::SENSITIVE)); |
372 EXPECT_FALSE(net::GetValueForKeyInQuery(url, "restrict", &value)); | 375 EXPECT_FALSE(net::GetValueForKeyInQuery(url, "restrict", &value)); |
373 | 376 |
374 prefs.SetString(prefs::kVariationsRestrictParameter, "restricted"); | 377 prefs.SetString(prefs::kVariationsRestrictParameter, "restricted"); |
375 url = service.GetVariationsServerURL(&prefs, std::string()); | 378 url = service.GetVariationsServerURL(&prefs, std::string()); |
376 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url, | 379 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url, |
377 base::CompareCase::SENSITIVE)); | 380 base::CompareCase::SENSITIVE)); |
(...skipping 14 matching lines...) Expand all Loading... |
392 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url, | 395 EXPECT_TRUE(base::StartsWith(url.spec(), default_variations_url, |
393 base::CompareCase::SENSITIVE)); | 396 base::CompareCase::SENSITIVE)); |
394 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "restrict", &value)); | 397 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "restrict", &value)); |
395 EXPECT_EQ("override", value); | 398 EXPECT_EQ("override", value); |
396 } | 399 } |
397 | 400 |
398 TEST_F(VariationsServiceTest, VariationsURLHasOSNameParam) { | 401 TEST_F(VariationsServiceTest, VariationsURLHasOSNameParam) { |
399 TestingPrefServiceSimple prefs; | 402 TestingPrefServiceSimple prefs; |
400 VariationsService::RegisterPrefs(prefs.registry()); | 403 VariationsService::RegisterPrefs(prefs.registry()); |
401 TestVariationsService service( | 404 TestVariationsService service( |
402 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), | 405 base::WrapUnique(new web_resource::TestRequestAllowedNotifier(&prefs)), |
403 &prefs); | 406 &prefs); |
404 const GURL url = service.GetVariationsServerURL(&prefs, std::string()); | 407 const GURL url = service.GetVariationsServerURL(&prefs, std::string()); |
405 | 408 |
406 std::string value; | 409 std::string value; |
407 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "osname", &value)); | 410 EXPECT_TRUE(net::GetValueForKeyInQuery(url, "osname", &value)); |
408 EXPECT_FALSE(value.empty()); | 411 EXPECT_FALSE(value.empty()); |
409 } | 412 } |
410 | 413 |
411 TEST_F(VariationsServiceTest, RequestsInitiallyNotAllowed) { | 414 TEST_F(VariationsServiceTest, RequestsInitiallyNotAllowed) { |
412 TestingPrefServiceSimple prefs; | 415 TestingPrefServiceSimple prefs; |
413 VariationsService::RegisterPrefs(prefs.registry()); | 416 VariationsService::RegisterPrefs(prefs.registry()); |
414 | 417 |
415 // Pass ownership to TestVariationsService, but keep a weak pointer to | 418 // Pass ownership to TestVariationsService, but keep a weak pointer to |
416 // manipulate it for this test. | 419 // manipulate it for this test. |
417 scoped_ptr<web_resource::TestRequestAllowedNotifier> test_notifier = | 420 std::unique_ptr<web_resource::TestRequestAllowedNotifier> test_notifier = |
418 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)); | 421 base::WrapUnique(new web_resource::TestRequestAllowedNotifier(&prefs)); |
419 web_resource::TestRequestAllowedNotifier* raw_notifier = test_notifier.get(); | 422 web_resource::TestRequestAllowedNotifier* raw_notifier = test_notifier.get(); |
420 TestVariationsService test_service(std::move(test_notifier), &prefs); | 423 TestVariationsService test_service(std::move(test_notifier), &prefs); |
421 | 424 |
422 // Force the notifier to initially disallow requests. | 425 // Force the notifier to initially disallow requests. |
423 raw_notifier->SetRequestsAllowedOverride(false); | 426 raw_notifier->SetRequestsAllowedOverride(false); |
424 test_service.StartRepeatedVariationsSeedFetch(); | 427 test_service.StartRepeatedVariationsSeedFetch(); |
425 EXPECT_FALSE(test_service.fetch_attempted()); | 428 EXPECT_FALSE(test_service.fetch_attempted()); |
426 | 429 |
427 raw_notifier->NotifyObserver(); | 430 raw_notifier->NotifyObserver(); |
428 EXPECT_TRUE(test_service.fetch_attempted()); | 431 EXPECT_TRUE(test_service.fetch_attempted()); |
429 } | 432 } |
430 | 433 |
431 TEST_F(VariationsServiceTest, RequestsInitiallyAllowed) { | 434 TEST_F(VariationsServiceTest, RequestsInitiallyAllowed) { |
432 TestingPrefServiceSimple prefs; | 435 TestingPrefServiceSimple prefs; |
433 VariationsService::RegisterPrefs(prefs.registry()); | 436 VariationsService::RegisterPrefs(prefs.registry()); |
434 | 437 |
435 // Pass ownership to TestVariationsService, but keep a weak pointer to | 438 // Pass ownership to TestVariationsService, but keep a weak pointer to |
436 // manipulate it for this test. | 439 // manipulate it for this test. |
437 scoped_ptr<web_resource::TestRequestAllowedNotifier> test_notifier = | 440 std::unique_ptr<web_resource::TestRequestAllowedNotifier> test_notifier = |
438 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)); | 441 base::WrapUnique(new web_resource::TestRequestAllowedNotifier(&prefs)); |
439 web_resource::TestRequestAllowedNotifier* raw_notifier = test_notifier.get(); | 442 web_resource::TestRequestAllowedNotifier* raw_notifier = test_notifier.get(); |
440 TestVariationsService test_service(std::move(test_notifier), &prefs); | 443 TestVariationsService test_service(std::move(test_notifier), &prefs); |
441 | 444 |
442 raw_notifier->SetRequestsAllowedOverride(true); | 445 raw_notifier->SetRequestsAllowedOverride(true); |
443 test_service.StartRepeatedVariationsSeedFetch(); | 446 test_service.StartRepeatedVariationsSeedFetch(); |
444 EXPECT_TRUE(test_service.fetch_attempted()); | 447 EXPECT_TRUE(test_service.fetch_attempted()); |
445 } | 448 } |
446 | 449 |
447 TEST_F(VariationsServiceTest, SeedStoredWhenOKStatus) { | 450 TEST_F(VariationsServiceTest, SeedStoredWhenOKStatus) { |
448 TestingPrefServiceSimple prefs; | 451 TestingPrefServiceSimple prefs; |
449 VariationsService::RegisterPrefs(prefs.registry()); | 452 VariationsService::RegisterPrefs(prefs.registry()); |
450 | 453 |
451 TestVariationsService service( | 454 TestVariationsService service( |
452 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), | 455 base::WrapUnique(new web_resource::TestRequestAllowedNotifier(&prefs)), |
453 &prefs); | 456 &prefs); |
454 service.set_intercepts_fetch(false); | 457 service.set_intercepts_fetch(false); |
455 | 458 |
456 net::TestURLFetcherFactory factory; | 459 net::TestURLFetcherFactory factory; |
457 service.DoActualFetch(); | 460 service.DoActualFetch(); |
458 | 461 |
459 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 462 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
460 SimulateServerResponse(net::HTTP_OK, fetcher); | 463 SimulateServerResponse(net::HTTP_OK, fetcher); |
461 fetcher->SetResponseString(SerializeSeed(CreateTestSeed())); | 464 fetcher->SetResponseString(SerializeSeed(CreateTestSeed())); |
462 | 465 |
463 EXPECT_FALSE(service.seed_stored()); | 466 EXPECT_FALSE(service.seed_stored()); |
464 service.OnURLFetchComplete(fetcher); | 467 service.OnURLFetchComplete(fetcher); |
465 EXPECT_TRUE(service.seed_stored()); | 468 EXPECT_TRUE(service.seed_stored()); |
466 } | 469 } |
467 | 470 |
468 TEST_F(VariationsServiceTest, SeedNotStoredWhenNonOKStatus) { | 471 TEST_F(VariationsServiceTest, SeedNotStoredWhenNonOKStatus) { |
469 const int non_ok_status_codes[] = { | 472 const int non_ok_status_codes[] = { |
470 net::HTTP_NO_CONTENT, | 473 net::HTTP_NO_CONTENT, |
471 net::HTTP_NOT_MODIFIED, | 474 net::HTTP_NOT_MODIFIED, |
472 net::HTTP_NOT_FOUND, | 475 net::HTTP_NOT_FOUND, |
473 net::HTTP_INTERNAL_SERVER_ERROR, | 476 net::HTTP_INTERNAL_SERVER_ERROR, |
474 net::HTTP_SERVICE_UNAVAILABLE, | 477 net::HTTP_SERVICE_UNAVAILABLE, |
475 }; | 478 }; |
476 | 479 |
477 TestingPrefServiceSimple prefs; | 480 TestingPrefServiceSimple prefs; |
478 VariationsService::RegisterPrefs(prefs.registry()); | 481 VariationsService::RegisterPrefs(prefs.registry()); |
479 | 482 |
480 TestVariationsService service( | 483 TestVariationsService service( |
481 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), | 484 base::WrapUnique(new web_resource::TestRequestAllowedNotifier(&prefs)), |
482 &prefs); | 485 &prefs); |
483 service.set_intercepts_fetch(false); | 486 service.set_intercepts_fetch(false); |
484 for (size_t i = 0; i < arraysize(non_ok_status_codes); ++i) { | 487 for (size_t i = 0; i < arraysize(non_ok_status_codes); ++i) { |
485 net::TestURLFetcherFactory factory; | 488 net::TestURLFetcherFactory factory; |
486 service.DoActualFetch(); | 489 service.DoActualFetch(); |
487 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 490 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
488 | 491 |
489 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 492 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
490 SimulateServerResponse(non_ok_status_codes[i], fetcher); | 493 SimulateServerResponse(non_ok_status_codes[i], fetcher); |
491 service.OnURLFetchComplete(fetcher); | 494 service.OnURLFetchComplete(fetcher); |
492 | 495 |
493 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); | 496 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); |
494 } | 497 } |
495 } | 498 } |
496 | 499 |
497 TEST_F(VariationsServiceTest, RequestGzipCompressedSeed) { | 500 TEST_F(VariationsServiceTest, RequestGzipCompressedSeed) { |
498 TestingPrefServiceSimple prefs; | 501 TestingPrefServiceSimple prefs; |
499 VariationsService::RegisterPrefs(prefs.registry()); | 502 VariationsService::RegisterPrefs(prefs.registry()); |
500 net::TestURLFetcherFactory factory; | 503 net::TestURLFetcherFactory factory; |
501 | 504 |
502 TestVariationsService service( | 505 TestVariationsService service( |
503 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), | 506 base::WrapUnique(new web_resource::TestRequestAllowedNotifier(&prefs)), |
504 &prefs); | 507 &prefs); |
505 service.set_intercepts_fetch(false); | 508 service.set_intercepts_fetch(false); |
506 service.DoActualFetch(); | 509 service.DoActualFetch(); |
507 | 510 |
508 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 511 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
509 net::HttpRequestHeaders headers; | 512 net::HttpRequestHeaders headers; |
510 fetcher->GetExtraRequestHeaders(&headers); | 513 fetcher->GetExtraRequestHeaders(&headers); |
511 std::string field; | 514 std::string field; |
512 ASSERT_TRUE(headers.GetHeader("A-IM", &field)); | 515 ASSERT_TRUE(headers.GetHeader("A-IM", &field)); |
513 EXPECT_EQ("gzip", field); | 516 EXPECT_EQ("gzip", field); |
(...skipping 15 matching lines...) Expand all Loading... |
529 {"IM:deflate,x-bm,gzip", false, false, false}, | 532 {"IM:deflate,x-bm,gzip", false, false, false}, |
530 }; | 533 }; |
531 | 534 |
532 TestingPrefServiceSimple prefs; | 535 TestingPrefServiceSimple prefs; |
533 VariationsService::RegisterPrefs(prefs.registry()); | 536 VariationsService::RegisterPrefs(prefs.registry()); |
534 std::string serialized_seed = SerializeSeed(CreateTestSeed()); | 537 std::string serialized_seed = SerializeSeed(CreateTestSeed()); |
535 net::TestURLFetcherFactory factory; | 538 net::TestURLFetcherFactory factory; |
536 | 539 |
537 for (size_t i = 0; i < arraysize(cases); ++i) { | 540 for (size_t i = 0; i < arraysize(cases); ++i) { |
538 TestVariationsService service( | 541 TestVariationsService service( |
539 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), | 542 base::WrapUnique(new web_resource::TestRequestAllowedNotifier(&prefs)), |
540 &prefs); | 543 &prefs); |
541 service.set_intercepts_fetch(false); | 544 service.set_intercepts_fetch(false); |
542 service.DoActualFetch(); | 545 service.DoActualFetch(); |
543 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 546 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
544 | 547 |
545 if (cases[i].im.empty()) | 548 if (cases[i].im.empty()) |
546 SimulateServerResponse(net::HTTP_OK, fetcher); | 549 SimulateServerResponse(net::HTTP_OK, fetcher); |
547 else | 550 else |
548 SimulateServerResponseWithHeader(net::HTTP_OK, fetcher, &cases[i].im); | 551 SimulateServerResponseWithHeader(net::HTTP_OK, fetcher, &cases[i].im); |
549 fetcher->SetResponseString(serialized_seed); | 552 fetcher->SetResponseString(serialized_seed); |
550 service.OnURLFetchComplete(fetcher); | 553 service.OnURLFetchComplete(fetcher); |
551 | 554 |
552 EXPECT_EQ(cases[i].seed_stored, service.seed_stored()); | 555 EXPECT_EQ(cases[i].seed_stored, service.seed_stored()); |
553 EXPECT_EQ(cases[i].delta_compressed, service.delta_compressed_seed()); | 556 EXPECT_EQ(cases[i].delta_compressed, service.delta_compressed_seed()); |
554 EXPECT_EQ(cases[i].gzip_compressed, service.gzip_compressed_seed()); | 557 EXPECT_EQ(cases[i].gzip_compressed, service.gzip_compressed_seed()); |
555 } | 558 } |
556 } | 559 } |
557 | 560 |
558 TEST_F(VariationsServiceTest, CountryHeader) { | 561 TEST_F(VariationsServiceTest, CountryHeader) { |
559 TestingPrefServiceSimple prefs; | 562 TestingPrefServiceSimple prefs; |
560 VariationsService::RegisterPrefs(prefs.registry()); | 563 VariationsService::RegisterPrefs(prefs.registry()); |
561 | 564 |
562 TestVariationsService service( | 565 TestVariationsService service( |
563 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), | 566 base::WrapUnique(new web_resource::TestRequestAllowedNotifier(&prefs)), |
564 &prefs); | 567 &prefs); |
565 service.set_intercepts_fetch(false); | 568 service.set_intercepts_fetch(false); |
566 | 569 |
567 net::TestURLFetcherFactory factory; | 570 net::TestURLFetcherFactory factory; |
568 service.DoActualFetch(); | 571 service.DoActualFetch(); |
569 | 572 |
570 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 573 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
571 scoped_refptr<net::HttpResponseHeaders> headers = | 574 scoped_refptr<net::HttpResponseHeaders> headers = |
572 SimulateServerResponse(net::HTTP_OK, fetcher); | 575 SimulateServerResponse(net::HTTP_OK, fetcher); |
573 headers->AddHeader("X-Country: test"); | 576 headers->AddHeader("X-Country: test"); |
574 fetcher->SetResponseString(SerializeSeed(CreateTestSeed())); | 577 fetcher->SetResponseString(SerializeSeed(CreateTestSeed())); |
575 | 578 |
576 EXPECT_FALSE(service.seed_stored()); | 579 EXPECT_FALSE(service.seed_stored()); |
577 service.OnURLFetchComplete(fetcher); | 580 service.OnURLFetchComplete(fetcher); |
578 EXPECT_TRUE(service.seed_stored()); | 581 EXPECT_TRUE(service.seed_stored()); |
579 EXPECT_EQ("test", service.stored_country()); | 582 EXPECT_EQ("test", service.stored_country()); |
580 } | 583 } |
581 | 584 |
582 TEST_F(VariationsServiceTest, Observer) { | 585 TEST_F(VariationsServiceTest, Observer) { |
583 TestingPrefServiceSimple prefs; | 586 TestingPrefServiceSimple prefs; |
584 VariationsService::RegisterPrefs(prefs.registry()); | 587 VariationsService::RegisterPrefs(prefs.registry()); |
585 VariationsService service( | 588 VariationsService service( |
586 make_scoped_ptr(new TestVariationsServiceClient()), | 589 base::WrapUnique(new TestVariationsServiceClient()), |
587 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), | 590 base::WrapUnique(new web_resource::TestRequestAllowedNotifier(&prefs)), |
588 &prefs, NULL, UIStringOverrider()); | 591 &prefs, NULL, UIStringOverrider()); |
589 | 592 |
590 struct { | 593 struct { |
591 int normal_count; | 594 int normal_count; |
592 int best_effort_count; | 595 int best_effort_count; |
593 int critical_count; | 596 int critical_count; |
594 int expected_best_effort_notifications; | 597 int expected_best_effort_notifications; |
595 int expected_crtical_notifications; | 598 int expected_crtical_notifications; |
596 } cases[] = { | 599 } cases[] = { |
597 {0, 0, 0, 0, 0}, | 600 {0, 0, 0, 0, 0}, |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
676 {"badversion,ca", "20.0.0.0", "us", "20.0.0.0,us", "us", | 679 {"badversion,ca", "20.0.0.0", "us", "20.0.0.0,us", "us", |
677 VariationsService::LOAD_COUNTRY_INVALID_PREF_HAS_SEED}, | 680 VariationsService::LOAD_COUNTRY_INVALID_PREF_HAS_SEED}, |
678 {"badversion,ca", "20.0.0.0", nullptr, "", "", | 681 {"badversion,ca", "20.0.0.0", nullptr, "", "", |
679 VariationsService::LOAD_COUNTRY_INVALID_PREF_NO_SEED}, | 682 VariationsService::LOAD_COUNTRY_INVALID_PREF_NO_SEED}, |
680 }; | 683 }; |
681 | 684 |
682 for (const auto& test : test_cases) { | 685 for (const auto& test : test_cases) { |
683 TestingPrefServiceSimple prefs; | 686 TestingPrefServiceSimple prefs; |
684 VariationsService::RegisterPrefs(prefs.registry()); | 687 VariationsService::RegisterPrefs(prefs.registry()); |
685 VariationsService service( | 688 VariationsService service( |
686 make_scoped_ptr(new TestVariationsServiceClient()), | 689 base::WrapUnique(new TestVariationsServiceClient()), |
687 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), | 690 base::WrapUnique(new web_resource::TestRequestAllowedNotifier(&prefs)), |
688 &prefs, NULL, UIStringOverrider()); | 691 &prefs, NULL, UIStringOverrider()); |
689 | 692 |
690 if (test.pref_value_before) { | 693 if (test.pref_value_before) { |
691 base::ListValue list_value; | 694 base::ListValue list_value; |
692 for (const std::string& component : | 695 for (const std::string& component : |
693 base::SplitString(test.pref_value_before, ",", base::TRIM_WHITESPACE, | 696 base::SplitString(test.pref_value_before, ",", base::TRIM_WHITESPACE, |
694 base::SPLIT_WANT_ALL)) { | 697 base::SPLIT_WANT_ALL)) { |
695 list_value.AppendString(component); | 698 list_value.AppendString(component); |
696 } | 699 } |
697 prefs.Set(prefs::kVariationsPermanentConsistencyCountry, list_value); | 700 prefs.Set(prefs::kVariationsPermanentConsistencyCountry, list_value); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
748 {"", "ca", kPrefCa, true}, | 751 {"", "ca", kPrefCa, true}, |
749 {"", "", "", false}, | 752 {"", "", "", false}, |
750 {"19.0.0.0,us", "ca", kPrefCa, true}, | 753 {"19.0.0.0,us", "ca", kPrefCa, true}, |
751 {"19.0.0.0,us", "us", "19.0.0.0,us", false}, | 754 {"19.0.0.0,us", "us", "19.0.0.0,us", false}, |
752 }; | 755 }; |
753 | 756 |
754 for (const auto& test : test_cases) { | 757 for (const auto& test : test_cases) { |
755 TestingPrefServiceSimple prefs; | 758 TestingPrefServiceSimple prefs; |
756 VariationsService::RegisterPrefs(prefs.registry()); | 759 VariationsService::RegisterPrefs(prefs.registry()); |
757 TestVariationsService service( | 760 TestVariationsService service( |
758 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), | 761 base::WrapUnique(new web_resource::TestRequestAllowedNotifier(&prefs)), |
759 &prefs); | 762 &prefs); |
760 | 763 |
761 if (!test.pref_value_before.empty()) { | 764 if (!test.pref_value_before.empty()) { |
762 base::ListValue list_value; | 765 base::ListValue list_value; |
763 for (const std::string& component : | 766 for (const std::string& component : |
764 base::SplitString(test.pref_value_before, ",", base::TRIM_WHITESPACE, | 767 base::SplitString(test.pref_value_before, ",", base::TRIM_WHITESPACE, |
765 base::SPLIT_WANT_ALL)) { | 768 base::SPLIT_WANT_ALL)) { |
766 list_value.AppendString(component); | 769 list_value.AppendString(component); |
767 } | 770 } |
768 prefs.Set(prefs::kVariationsPermanentConsistencyCountry, list_value); | 771 prefs.Set(prefs::kVariationsPermanentConsistencyCountry, list_value); |
(...skipping 13 matching lines...) Expand all Loading... |
782 } | 785 } |
783 const base::ListValue* pref_value = | 786 const base::ListValue* pref_value = |
784 prefs.GetList(prefs::kVariationsPermanentConsistencyCountry); | 787 prefs.GetList(prefs::kVariationsPermanentConsistencyCountry); |
785 EXPECT_EQ(ListValueToString(expected_list_value), | 788 EXPECT_EQ(ListValueToString(expected_list_value), |
786 ListValueToString(*pref_value)) | 789 ListValueToString(*pref_value)) |
787 << test.pref_value_before << ", " << test.country_code_override; | 790 << test.pref_value_before << ", " << test.country_code_override; |
788 } | 791 } |
789 } | 792 } |
790 | 793 |
791 } // namespace variations | 794 } // namespace variations |
OLD | NEW |