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