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

Side by Side Diff: chrome/browser/metrics/variations/variations_service_unittest.cc

Issue 10917120: Activate the VariationsService for ChromeOS and ensure that it does not ping the server until the E… (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: asvit nit 2 Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/base64.h" 5 #include "base/base64.h"
6 #include "base/string_split.h" 6 #include "base/string_split.h"
7 #include "chrome/browser/metrics/proto/study.pb.h" 7 #include "chrome/browser/metrics/proto/study.pb.h"
8 #include "chrome/browser/metrics/variations/variations_service.h" 8 #include "chrome/browser/metrics/variations/variations_service.h"
9 #include "chrome/common/chrome_version_info.h" 9 #include "chrome/common/chrome_version_info.h"
10 #include "chrome/common/pref_names.h" 10 #include "chrome/common/pref_names.h"
11 #include "chrome/test/base/testing_browser_process.h"
11 #include "chrome/test/base/testing_pref_service.h" 12 #include "chrome/test/base/testing_pref_service.h"
12 #include "content/public/test/test_browser_thread.h" 13 #include "content/public/test/test_browser_thread.h"
13 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
14 15
15 namespace chrome_variations { 16 namespace chrome_variations {
16 17
17 namespace { 18 namespace {
18 19
20 class TestRequestAllowedNotifier : public ResourceRequestAllowedNotifier {
21 public:
22 TestRequestAllowedNotifier();
23 virtual ~TestRequestAllowedNotifier();
24
25 virtual bool ResourceRequestsAllowed() OVERRIDE;
26
27 void SetRequestsAllowed(bool allowed);
28
29 // Notify observers that requests are allowed.
30 void NotifyObservers();
31
32 private:
33 bool requests_allowed_;
34 };
35
36 TestRequestAllowedNotifier::TestRequestAllowedNotifier()
37 : requests_allowed_(true) {
38 }
39
40 TestRequestAllowedNotifier::~TestRequestAllowedNotifier() {
41 }
42
43 bool TestRequestAllowedNotifier::ResourceRequestsAllowed() {
44 return requests_allowed_;
45 }
46
47 void TestRequestAllowedNotifier::SetRequestsAllowed(bool allowed) {
48 requests_allowed_ = allowed;
49 }
50
51 // Notify observers that requests are allowed.
52 void TestRequestAllowedNotifier::NotifyObservers() {
53 requests_allowed_ = true;
54 MaybeNotifyObservers();
55 }
56
19 // A test class used to validate expected functionality in VariationsService. 57 // A test class used to validate expected functionality in VariationsService.
20 class TestVariationsService : public VariationsService { 58 class TestVariationsService : public VariationsService {
21 public: 59 public:
22 TestVariationsService() : VariationsService(), 60 explicit TestVariationsService(ResourceRequestAllowedNotifier* notifier)
23 fetch_attempted_(false) { 61 : VariationsService(),
62 fetch_attempted_(false) {
63 SetResourceRequestAllowedNotifierForTesting(notifier);
64 // Set this so StartRepeatedVariationsSeedFetch can be called.
65 create_trials_from_seed_called_ = true;
24 } 66 }
25 virtual ~TestVariationsService() {} 67 virtual ~TestVariationsService() {}
26 68
27 bool fetch_attempted() const { return fetch_attempted_; } 69 bool fetch_attempted() const { return fetch_attempted_; }
28 void SetFetchAttempted(bool attempted) { fetch_attempted_ = attempted; }
29 70
30 protected: 71 protected:
31 virtual void FetchVariationsSeed() OVERRIDE { 72 virtual void DoActualFetch() OVERRIDE {
32 fetch_attempted_ = true; 73 fetch_attempted_ = true;
33 } 74 }
34 75
35 private: 76 private:
36 bool fetch_attempted_; 77 bool fetch_attempted_;
37 78
38 DISALLOW_COPY_AND_ASSIGN(TestVariationsService); 79 DISALLOW_COPY_AND_ASSIGN(TestVariationsService);
39 }; 80 };
40 81
41 // Override NetworkChangeNotifier to simulate connection type changes for tests.
42 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier {
43 public:
44 TestNetworkChangeNotifier()
45 : net::NetworkChangeNotifier(),
46 connection_type_to_return_(
47 net::NetworkChangeNotifier::CONNECTION_UNKNOWN) {
48 }
49
50 void SimulateNetworkConnectionChange(
51 net::NetworkChangeNotifier::ConnectionType type) {
52 connection_type_to_return_ = type;
53 net::NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange();
54 MessageLoop::current()->RunAllPending();
55 }
56
57 private:
58 virtual ConnectionType GetCurrentConnectionType() const OVERRIDE {
59 return connection_type_to_return_;
60 }
61
62 net::NetworkChangeNotifier::ConnectionType connection_type_to_return_;
63
64 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier);
65 };
66
67 // Converts |time| to Study proto format. 82 // Converts |time| to Study proto format.
68 int64 TimeToProtoTime(const base::Time& time) { 83 int64 TimeToProtoTime(const base::Time& time) {
69 return (time - base::Time::UnixEpoch()).InSeconds(); 84 return (time - base::Time::UnixEpoch()).InSeconds();
70 } 85 }
71 86
72 // Populates |seed| with simple test data. The resulting seed will contain one 87 // Populates |seed| with simple test data. The resulting seed will contain one
73 // study called "test", which contains one experiment called "abc" with 88 // study called "test", which contains one experiment called "abc" with
74 // probability weight 100. |seed|'s study field will be cleared before adding 89 // probability weight 100. |seed|'s study field will be cleared before adding
75 // the new study. 90 // the new study.
76 TrialsSeed CreateTestSeed() { 91 TrialsSeed CreateTestSeed() {
77 TrialsSeed seed; 92 TrialsSeed seed;
78 Study* study = seed.add_study(); 93 Study* study = seed.add_study();
79 study->set_name("test"); 94 study->set_name("test");
80 study->set_default_experiment_name("abc"); 95 study->set_default_experiment_name("abc");
81 Study_Experiment* experiment = study->add_experiment(); 96 Study_Experiment* experiment = study->add_experiment();
82 experiment->set_name("abc"); 97 experiment->set_name("abc");
83 experiment->set_probability_weight(100); 98 experiment->set_probability_weight(100);
84 return seed; 99 return seed;
85 } 100 }
86 101
87 } // namespace 102 // Tests that create any form of VariationsService should use this as a fixture.
88 103 // TODO(stevet): This is not great since it creates useless overhead for non-
89 // A test fixture class for VariationsService tests that require network state 104 // ChromeOS tests. Find a better way to handle this.
90 // simulations. 105 class VariationsServiceCreatedTest : public testing::Test {
91 class VariationsServiceNetworkTest : public testing::Test {
92 public: 106 public:
93 VariationsServiceNetworkTest() 107 VariationsServiceCreatedTest()
94 : ui_thread(content::BrowserThread::UI, &message_loop) { } 108 : scoped_testing_local_state_(
95 ~VariationsServiceNetworkTest() { } 109 static_cast<TestingBrowserProcess*>(g_browser_process)),
96 110 local_state_(scoped_testing_local_state_.Get()) {
97 void SetWasOfflineDuringLastRequestAttempt(bool offline) {
98 test_service.SetWasOfflineDuringLastRequestAttemptForTesting(offline);
99 } 111 }
100 112
101 void SimulateNetworkConnectionChange( 113 // Expose local_state for tests that need to touch the VariationsSeed.
102 net::NetworkChangeNotifier::ConnectionType type) { 114 TestingPrefService* local_state() { return local_state_; }
103 notifier.SimulateNetworkConnectionChange(type); 115
116 #if defined(OS_CHROMEOS)
117 void SetUp() {
118 // Have to set the Eula state before creating the notifier as it checks this
119 // at startup.
120 // TODO(stevet): Share this string with the wizard_controller file that
121 // defines it.
122 local_state_->SetUserPref("EulaAccepted", Value::CreateBooleanValue(true));
104 } 123 }
124 #endif
125 private:
126 ScopedTestingLocalState scoped_testing_local_state_;
127 TestingPrefService* local_state_;
105 128
106 bool fetch_attempted() const { 129 DISALLOW_COPY_AND_ASSIGN(VariationsServiceCreatedTest);
107 return test_service.fetch_attempted(); 130 };
108 }
109 131
110 private: 132 } // namespace
111 MessageLoopForUI message_loop;
112 content::TestBrowserThread ui_thread;
113 TestNetworkChangeNotifier notifier;
114 TestVariationsService test_service;
115
116 DISALLOW_COPY_AND_ASSIGN(VariationsServiceNetworkTest);
117 };
118 133
119 TEST(VariationsServiceTest, CheckStudyChannel) { 134 TEST(VariationsServiceTest, CheckStudyChannel) {
120 const chrome::VersionInfo::Channel channels[] = { 135 const chrome::VersionInfo::Channel channels[] = {
121 chrome::VersionInfo::CHANNEL_CANARY, 136 chrome::VersionInfo::CHANNEL_CANARY,
122 chrome::VersionInfo::CHANNEL_DEV, 137 chrome::VersionInfo::CHANNEL_DEV,
123 chrome::VersionInfo::CHANNEL_BETA, 138 chrome::VersionInfo::CHANNEL_BETA,
124 chrome::VersionInfo::CHANNEL_STABLE, 139 chrome::VersionInfo::CHANNEL_STABLE,
125 }; 140 };
126 const Study_Channel study_channels[] = { 141 const Study_Channel study_channels[] = {
127 Study_Channel_CANARY, 142 Study_Channel_CANARY,
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 EXPECT_FALSE(VariationsService::IsStudyExpired(study, now)); 395 EXPECT_FALSE(VariationsService::IsStudyExpired(study, now));
381 396
382 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expiry_test_cases); ++i) { 397 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expiry_test_cases); ++i) {
383 study.set_expiry_date(TimeToProtoTime(expiry_test_cases[i].expiry_date)); 398 study.set_expiry_date(TimeToProtoTime(expiry_test_cases[i].expiry_date));
384 const bool result = VariationsService::IsStudyExpired(study, now); 399 const bool result = VariationsService::IsStudyExpired(study, now);
385 EXPECT_EQ(expiry_test_cases[i].expected_result, result) 400 EXPECT_EQ(expiry_test_cases[i].expected_result, result)
386 << "Case " << i << " failed!"; 401 << "Case " << i << " failed!";
387 } 402 }
388 } 403 }
389 404
390 TEST(VariationsServiceTest, LoadSeed) { 405 TEST_F(VariationsServiceCreatedTest, LoadSeed) {
391 TestingPrefService pref_service;
392
393 VariationsService::RegisterPrefs(&pref_service);
394
395 // Store good seed data to test if loading from prefs works. 406 // Store good seed data to test if loading from prefs works.
396 TrialsSeed seed = CreateTestSeed(); 407 TrialsSeed seed = CreateTestSeed();
397 408
398 std::string serialized_seed; 409 std::string serialized_seed;
399 seed.SerializeToString(&serialized_seed); 410 seed.SerializeToString(&serialized_seed);
400 std::string base64_serialized_seed; 411 std::string base64_serialized_seed;
401 ASSERT_TRUE(base::Base64Encode(serialized_seed, &base64_serialized_seed)); 412 ASSERT_TRUE(base::Base64Encode(serialized_seed, &base64_serialized_seed));
402 pref_service.SetString(prefs::kVariationsSeed, base64_serialized_seed); 413 local_state()->SetString(prefs::kVariationsSeed, base64_serialized_seed);
403 414
404 VariationsService variations_service; 415 VariationsService variations_service;
405 TrialsSeed loaded_seed; 416 TrialsSeed loaded_seed;
406 EXPECT_TRUE( 417 EXPECT_TRUE(
407 variations_service.LoadTrialsSeedFromPref(&pref_service, &loaded_seed)); 418 variations_service.LoadTrialsSeedFromPref(local_state(), &loaded_seed));
408 419
409 std::string serialized_loaded_seed; 420 std::string serialized_loaded_seed;
410 loaded_seed.SerializeToString(&serialized_loaded_seed); 421 loaded_seed.SerializeToString(&serialized_loaded_seed);
411 // Check that the loaded data is the same as the original. 422 // Check that the loaded data is the same as the original.
412 EXPECT_EQ(serialized_seed, serialized_loaded_seed); 423 EXPECT_EQ(serialized_seed, serialized_loaded_seed);
413 // Make sure the pref hasn't been changed. 424 // Make sure the pref hasn't been changed.
414 EXPECT_FALSE( 425 EXPECT_FALSE(
415 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); 426 local_state()->FindPreference(prefs::kVariationsSeed)->IsDefaultValue());
416 EXPECT_EQ(base64_serialized_seed, 427 EXPECT_EQ(base64_serialized_seed,
417 pref_service.GetString(prefs::kVariationsSeed)); 428 local_state()->GetString(prefs::kVariationsSeed));
418 429
419 // Check that loading a bad seed returns false and clears the pref. 430 // Check that loading a bad seed returns false and clears the pref.
420 pref_service.ClearPref(prefs::kVariationsSeed); 431 local_state()->ClearPref(prefs::kVariationsSeed);
421 pref_service.SetString(prefs::kVariationsSeed, "this should fail"); 432 local_state()->SetString(prefs::kVariationsSeed, "this should fail");
422 EXPECT_FALSE( 433 EXPECT_FALSE(
423 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); 434 local_state()->FindPreference(prefs::kVariationsSeed)->IsDefaultValue());
424 EXPECT_FALSE( 435 EXPECT_FALSE(
425 variations_service.LoadTrialsSeedFromPref(&pref_service, &loaded_seed)); 436 variations_service.LoadTrialsSeedFromPref(local_state(), &loaded_seed));
426 EXPECT_TRUE( 437 EXPECT_TRUE(
427 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); 438 local_state()->FindPreference(prefs::kVariationsSeed)->IsDefaultValue());
428 } 439 }
429 440
430 TEST(VariationsServiceTest, StoreSeed) { 441 TEST_F(VariationsServiceCreatedTest, StoreSeed) {
431 TestingPrefService pref_service;
432
433 VariationsService::RegisterPrefs(&pref_service);
434 const base::Time now = base::Time::Now(); 442 const base::Time now = base::Time::Now();
435 443
436 TrialsSeed seed = CreateTestSeed(); 444 TrialsSeed seed = CreateTestSeed();
437 445
438 VariationsService variations_service; 446 VariationsService variations_service;
439 std::string serialized_seed; 447 std::string serialized_seed;
440 seed.SerializeToString(&serialized_seed); 448 seed.SerializeToString(&serialized_seed);
441 EXPECT_TRUE( 449 EXPECT_TRUE(
442 variations_service.StoreSeedData(serialized_seed, now, &pref_service)); 450 variations_service.StoreSeedData(serialized_seed, now, local_state()));
443 // Make sure the pref was actually set. 451 // Make sure the pref was actually set.
444 EXPECT_FALSE( 452 EXPECT_FALSE(
445 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); 453 local_state()->FindPreference(prefs::kVariationsSeed)->IsDefaultValue());
446 454
447 std::string loaded_serialized_seed = 455 std::string loaded_serialized_seed =
448 pref_service.GetString(prefs::kVariationsSeed); 456 local_state()->GetString(prefs::kVariationsSeed);
449 std::string decoded_serialized_seed; 457 std::string decoded_serialized_seed;
450 ASSERT_TRUE(base::Base64Decode(loaded_serialized_seed, 458 ASSERT_TRUE(base::Base64Decode(loaded_serialized_seed,
451 &decoded_serialized_seed)); 459 &decoded_serialized_seed));
452 // Make sure the stored seed from pref is the same as the seed we created. 460 // Make sure the stored seed from pref is the same as the seed we created.
453 EXPECT_EQ(serialized_seed, decoded_serialized_seed); 461 EXPECT_EQ(serialized_seed, decoded_serialized_seed);
454 462
455 // Check if trying to store a bad seed leaves the pref unchanged. 463 // Check if trying to store a bad seed leaves the pref unchanged.
456 pref_service.ClearPref(prefs::kVariationsSeed); 464 local_state()->ClearPref(prefs::kVariationsSeed);
457 EXPECT_FALSE( 465 EXPECT_FALSE(
458 variations_service.StoreSeedData("this should fail", now, &pref_service)); 466 variations_service.StoreSeedData("this should fail", now, local_state()));
459 EXPECT_TRUE( 467 EXPECT_TRUE(
460 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); 468 local_state()->FindPreference(prefs::kVariationsSeed)->IsDefaultValue());
461 } 469 }
462 470
463 TEST(VariationsServiceTest, ValidateStudy) { 471 TEST(VariationsServiceTest, ValidateStudy) {
464 Study study; 472 Study study;
465 study.set_default_experiment_name("def"); 473 study.set_default_experiment_name("def");
466 474
467 Study_Experiment* experiment = study.add_experiment(); 475 Study_Experiment* experiment = study.add_experiment();
468 experiment->set_name("abc"); 476 experiment->set_name("abc");
469 experiment->set_probability_weight(100); 477 experiment->set_probability_weight(100);
470 478
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
527 &total_probability); 535 &total_probability);
528 ASSERT_TRUE(valid); 536 ASSERT_TRUE(valid);
529 Study_Experiment* repeated_group = study.add_experiment(); 537 Study_Experiment* repeated_group = study.add_experiment();
530 repeated_group->set_name("abc"); 538 repeated_group->set_name("abc");
531 repeated_group->set_probability_weight(1); 539 repeated_group->set_probability_weight(1);
532 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, 540 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study,
533 &total_probability); 541 &total_probability);
534 EXPECT_FALSE(valid); 542 EXPECT_FALSE(valid);
535 } 543 }
536 544
537 TEST_F(VariationsServiceNetworkTest, DoNotFetchIfOffline) { 545 TEST_F(VariationsServiceCreatedTest, ResourceRequestAllowedNotifierTest) {
538 SetWasOfflineDuringLastRequestAttempt(true); 546 MessageLoopForUI message_loop;
539 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE); 547 content::TestBrowserThread ui_thread(content::BrowserThread::UI,
540 EXPECT_FALSE(fetch_attempted()); 548 &message_loop);
541 }
542 549
543 TEST_F(VariationsServiceNetworkTest, DoNotFetchIfOnlineToOnline) { 550 TestRequestAllowedNotifier* notifier = new TestRequestAllowedNotifier;
544 SetWasOfflineDuringLastRequestAttempt(false); 551 // Pass ownership to TestVariationsService, but keep a weak pointer to
545 SimulateNetworkConnectionChange( 552 // manipulate it for this test.
546 net::NetworkChangeNotifier::CONNECTION_ETHERNET); 553 TestVariationsService test_service(notifier);
547 EXPECT_FALSE(fetch_attempted()); 554 notifier->SetRequestsAllowed(false);
548 } 555 test_service.StartRepeatedVariationsSeedFetch();
549 556 EXPECT_FALSE(test_service.fetch_attempted());
550 TEST_F(VariationsServiceNetworkTest, FetchOnReconnect) { 557 notifier->NotifyObservers();
551 SetWasOfflineDuringLastRequestAttempt(true); 558 EXPECT_TRUE(test_service.fetch_attempted());
552 SimulateNetworkConnectionChange(
553 net::NetworkChangeNotifier::CONNECTION_ETHERNET);
554 EXPECT_TRUE(fetch_attempted());
555 }
556
557 TEST_F(VariationsServiceNetworkTest, NoFetchOnWardriving) {
558 SetWasOfflineDuringLastRequestAttempt(false);
559 SimulateNetworkConnectionChange(
560 net::NetworkChangeNotifier::CONNECTION_WIFI);
561 EXPECT_FALSE(fetch_attempted());
562 SimulateNetworkConnectionChange(
563 net::NetworkChangeNotifier::CONNECTION_3G);
564 EXPECT_FALSE(fetch_attempted());
565 SimulateNetworkConnectionChange(
566 net::NetworkChangeNotifier::CONNECTION_4G);
567 EXPECT_FALSE(fetch_attempted());
568 SimulateNetworkConnectionChange(
569 net::NetworkChangeNotifier::CONNECTION_WIFI);
570 EXPECT_FALSE(fetch_attempted());
571 }
572
573 TEST_F(VariationsServiceNetworkTest, NoFetchOnFlakyConnection) {
574 SetWasOfflineDuringLastRequestAttempt(false);
575 SimulateNetworkConnectionChange(
576 net::NetworkChangeNotifier::CONNECTION_WIFI);
577 EXPECT_FALSE(fetch_attempted());
578 SimulateNetworkConnectionChange(
579 net::NetworkChangeNotifier::CONNECTION_NONE);
580 EXPECT_FALSE(fetch_attempted());
581 SimulateNetworkConnectionChange(
582 net::NetworkChangeNotifier::CONNECTION_WIFI);
583 EXPECT_FALSE(fetch_attempted());
584 } 559 }
585 560
586 } // namespace chrome_variations 561 } // namespace chrome_variations
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698