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

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: notification situation solution 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 virtual ~TestVariationsService();
24 }
25 virtual ~TestVariationsService() {}
26 62
27 bool fetch_attempted() const { return fetch_attempted_; } 63 bool fetch_attempted() const { return fetch_attempted_; }
28 void SetFetchAttempted(bool attempted) { fetch_attempted_ = attempted; }
29 64
30 protected: 65 protected:
31 virtual void FetchVariationsSeed() OVERRIDE { 66 virtual void DoActualFetch() OVERRIDE {
32 fetch_attempted_ = true; 67 fetch_attempted_ = true;
33 } 68 }
34 69
35 private: 70 private:
36 bool fetch_attempted_; 71 bool fetch_attempted_;
37 72
38 DISALLOW_COPY_AND_ASSIGN(TestVariationsService); 73 DISALLOW_COPY_AND_ASSIGN(TestVariationsService);
39 }; 74 };
40 75
41 // Override NetworkChangeNotifier to simulate connection type changes for tests. 76 TestVariationsService::TestVariationsService(
42 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier { 77 ResourceRequestAllowedNotifier* notifier)
43 public: 78 : VariationsService(),
44 TestNetworkChangeNotifier() 79 fetch_attempted_(false) {
45 : net::NetworkChangeNotifier(), 80 SetResourceRequestAllowedNotifierForTesting(notifier);
46 connection_type_to_return_( 81 // Set this so StartRepeatedVariationsSeedFetch can be called in tests.
47 net::NetworkChangeNotifier::CONNECTION_UNKNOWN) { 82 create_trials_from_seed_called_ = true;
48 } 83 }
49 84
50 void SimulateNetworkConnectionChange( 85 TestVariationsService::~TestVariationsService() {
51 net::NetworkChangeNotifier::ConnectionType type) { 86 }
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 87
67 // Converts |time| to Study proto format. 88 // Converts |time| to Study proto format.
68 int64 TimeToProtoTime(const base::Time& time) { 89 int64 TimeToProtoTime(const base::Time& time) {
69 return (time - base::Time::UnixEpoch()).InSeconds(); 90 return (time - base::Time::UnixEpoch()).InSeconds();
70 } 91 }
71 92
72 // Populates |seed| with simple test data. The resulting seed will contain one 93 // Populates |seed| with simple test data. The resulting seed will contain one
73 // study called "test", which contains one experiment called "abc" with 94 // study called "test", which contains one experiment called "abc" with
74 // probability weight 100. |seed|'s study field will be cleared before adding 95 // probability weight 100. |seed|'s study field will be cleared before adding
75 // the new study. 96 // the new study.
76 TrialsSeed CreateTestSeed() { 97 TrialsSeed CreateTestSeed() {
77 TrialsSeed seed; 98 TrialsSeed seed;
78 Study* study = seed.add_study(); 99 Study* study = seed.add_study();
79 study->set_name("test"); 100 study->set_name("test");
80 study->set_default_experiment_name("abc"); 101 study->set_default_experiment_name("abc");
81 Study_Experiment* experiment = study->add_experiment(); 102 Study_Experiment* experiment = study->add_experiment();
82 experiment->set_name("abc"); 103 experiment->set_name("abc");
83 experiment->set_probability_weight(100); 104 experiment->set_probability_weight(100);
84 return seed; 105 return seed;
85 } 106 }
86 107
87 } // namespace 108 // Fixture for tests that instantiate VariationsService.
88 109 // TODO(stevet): This is not great since it creates useless overhead for non-
SteveT 2012/09/19 19:27:09 REVIEWERS: Any thoughts here? I might be able to j
Ilya Sherman 2012/09/19 19:49:01 Either way seems fine to me, or Alexei's suggestio
SteveT 2012/09/20 19:40:18 This is gone now since we're using that virtual Is
89 // A test fixture class for VariationsService tests that require network state 110 // ChromeOS tests. Find a better way to handle this.
90 // simulations. 111 class VariationsServiceCreatedTest : public testing::Test {
91 class VariationsServiceNetworkTest : public testing::Test {
92 public: 112 public:
93 VariationsServiceNetworkTest() 113 VariationsServiceCreatedTest()
94 : ui_thread(content::BrowserThread::UI, &message_loop) { } 114 : scoped_testing_local_state_(
95 ~VariationsServiceNetworkTest() { } 115 static_cast<TestingBrowserProcess*>(g_browser_process)),
96 116 local_state_(scoped_testing_local_state_.Get()) {
97 void SetWasOfflineDuringLastRequestAttempt(bool offline) {
98 test_service.SetWasOfflineDuringLastRequestAttemptForTesting(offline);
99 } 117 }
100 118
101 void SimulateNetworkConnectionChange( 119 // Expose local_state for tests that need to touch the VariationsSeed.
102 net::NetworkChangeNotifier::ConnectionType type) { 120 TestingPrefService* local_state() { return local_state_; }
103 notifier.SimulateNetworkConnectionChange(type); 121
122 #if defined(OS_CHROMEOS)
123 void SetUp() {
Ilya Sherman 2012/09/19 19:49:01 nit: OVERRIDE
SteveT 2012/09/20 19:40:18 This class has been removed.
124 // Have to set the Eula state before creating the notifier as it checks this
125 // at startup.
126 // TODO(stevet): Share this pref name with the wizard_controller file that
127 // defines it.
128 local_state_->SetUserPref("EulaAccepted", Value::CreateBooleanValue(true));
Alexei Svitkine (slow) 2012/09/19 19:44:28 I made a comment about this in the test for new no
SteveT 2012/09/20 19:40:18 This class has been removed.
104 } 129 }
130 #endif
131 private:
132 ScopedTestingLocalState scoped_testing_local_state_;
133 TestingPrefService* local_state_;
Ilya Sherman 2012/09/19 19:49:01 nit: This doesn't look like it needs to be a separ
SteveT 2012/09/20 19:40:18 This class has been removed.
105 134
106 bool fetch_attempted() const { 135 DISALLOW_COPY_AND_ASSIGN(VariationsServiceCreatedTest);
107 return test_service.fetch_attempted(); 136 };
108 }
109 137
110 private: 138 } // 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 139
119 TEST(VariationsServiceTest, CheckStudyChannel) { 140 TEST(VariationsServiceTest, CheckStudyChannel) {
120 const chrome::VersionInfo::Channel channels[] = { 141 const chrome::VersionInfo::Channel channels[] = {
121 chrome::VersionInfo::CHANNEL_CANARY, 142 chrome::VersionInfo::CHANNEL_CANARY,
122 chrome::VersionInfo::CHANNEL_DEV, 143 chrome::VersionInfo::CHANNEL_DEV,
123 chrome::VersionInfo::CHANNEL_BETA, 144 chrome::VersionInfo::CHANNEL_BETA,
124 chrome::VersionInfo::CHANNEL_STABLE, 145 chrome::VersionInfo::CHANNEL_STABLE,
125 }; 146 };
126 const Study_Channel study_channels[] = { 147 const Study_Channel study_channels[] = {
127 Study_Channel_CANARY, 148 Study_Channel_CANARY,
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 EXPECT_FALSE(VariationsService::IsStudyExpired(study, now)); 401 EXPECT_FALSE(VariationsService::IsStudyExpired(study, now));
381 402
382 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expiry_test_cases); ++i) { 403 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expiry_test_cases); ++i) {
383 study.set_expiry_date(TimeToProtoTime(expiry_test_cases[i].expiry_date)); 404 study.set_expiry_date(TimeToProtoTime(expiry_test_cases[i].expiry_date));
384 const bool result = VariationsService::IsStudyExpired(study, now); 405 const bool result = VariationsService::IsStudyExpired(study, now);
385 EXPECT_EQ(expiry_test_cases[i].expected_result, result) 406 EXPECT_EQ(expiry_test_cases[i].expected_result, result)
386 << "Case " << i << " failed!"; 407 << "Case " << i << " failed!";
387 } 408 }
388 } 409 }
389 410
390 TEST(VariationsServiceTest, LoadSeed) { 411 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. 412 // Store good seed data to test if loading from prefs works.
396 TrialsSeed seed = CreateTestSeed(); 413 TrialsSeed seed = CreateTestSeed();
397 414
398 std::string serialized_seed; 415 std::string serialized_seed;
399 seed.SerializeToString(&serialized_seed); 416 seed.SerializeToString(&serialized_seed);
400 std::string base64_serialized_seed; 417 std::string base64_serialized_seed;
401 ASSERT_TRUE(base::Base64Encode(serialized_seed, &base64_serialized_seed)); 418 ASSERT_TRUE(base::Base64Encode(serialized_seed, &base64_serialized_seed));
402 pref_service.SetString(prefs::kVariationsSeed, base64_serialized_seed); 419 local_state()->SetString(prefs::kVariationsSeed, base64_serialized_seed);
403 420
404 VariationsService variations_service; 421 VariationsService variations_service;
405 TrialsSeed loaded_seed; 422 TrialsSeed loaded_seed;
406 EXPECT_TRUE( 423 EXPECT_TRUE(
407 variations_service.LoadTrialsSeedFromPref(&pref_service, &loaded_seed)); 424 variations_service.LoadTrialsSeedFromPref(local_state(), &loaded_seed));
408 425
409 std::string serialized_loaded_seed; 426 std::string serialized_loaded_seed;
410 loaded_seed.SerializeToString(&serialized_loaded_seed); 427 loaded_seed.SerializeToString(&serialized_loaded_seed);
411 // Check that the loaded data is the same as the original. 428 // Check that the loaded data is the same as the original.
412 EXPECT_EQ(serialized_seed, serialized_loaded_seed); 429 EXPECT_EQ(serialized_seed, serialized_loaded_seed);
413 // Make sure the pref hasn't been changed. 430 // Make sure the pref hasn't been changed.
414 EXPECT_FALSE( 431 EXPECT_FALSE(
415 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); 432 local_state()->FindPreference(prefs::kVariationsSeed)->IsDefaultValue());
416 EXPECT_EQ(base64_serialized_seed, 433 EXPECT_EQ(base64_serialized_seed,
417 pref_service.GetString(prefs::kVariationsSeed)); 434 local_state()->GetString(prefs::kVariationsSeed));
418 435
419 // Check that loading a bad seed returns false and clears the pref. 436 // Check that loading a bad seed returns false and clears the pref.
420 pref_service.ClearPref(prefs::kVariationsSeed); 437 local_state()->ClearPref(prefs::kVariationsSeed);
421 pref_service.SetString(prefs::kVariationsSeed, "this should fail"); 438 local_state()->SetString(prefs::kVariationsSeed, "this should fail");
422 EXPECT_FALSE( 439 EXPECT_FALSE(
423 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); 440 local_state()->FindPreference(prefs::kVariationsSeed)->IsDefaultValue());
424 EXPECT_FALSE( 441 EXPECT_FALSE(
425 variations_service.LoadTrialsSeedFromPref(&pref_service, &loaded_seed)); 442 variations_service.LoadTrialsSeedFromPref(local_state(), &loaded_seed));
426 EXPECT_TRUE( 443 EXPECT_TRUE(
427 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); 444 local_state()->FindPreference(prefs::kVariationsSeed)->IsDefaultValue());
428 } 445 }
429 446
430 TEST(VariationsServiceTest, StoreSeed) { 447 TEST_F(VariationsServiceCreatedTest, StoreSeed) {
431 TestingPrefService pref_service;
432
433 VariationsService::RegisterPrefs(&pref_service);
434 const base::Time now = base::Time::Now(); 448 const base::Time now = base::Time::Now();
435 449
436 TrialsSeed seed = CreateTestSeed(); 450 TrialsSeed seed = CreateTestSeed();
437 451
438 VariationsService variations_service; 452 VariationsService variations_service;
439 std::string serialized_seed; 453 std::string serialized_seed;
440 seed.SerializeToString(&serialized_seed); 454 seed.SerializeToString(&serialized_seed);
441 EXPECT_TRUE( 455 EXPECT_TRUE(
442 variations_service.StoreSeedData(serialized_seed, now, &pref_service)); 456 variations_service.StoreSeedData(serialized_seed, now, local_state()));
443 // Make sure the pref was actually set. 457 // Make sure the pref was actually set.
444 EXPECT_FALSE( 458 EXPECT_FALSE(
445 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); 459 local_state()->FindPreference(prefs::kVariationsSeed)->IsDefaultValue());
446 460
447 std::string loaded_serialized_seed = 461 std::string loaded_serialized_seed =
448 pref_service.GetString(prefs::kVariationsSeed); 462 local_state()->GetString(prefs::kVariationsSeed);
449 std::string decoded_serialized_seed; 463 std::string decoded_serialized_seed;
450 ASSERT_TRUE(base::Base64Decode(loaded_serialized_seed, 464 ASSERT_TRUE(base::Base64Decode(loaded_serialized_seed,
451 &decoded_serialized_seed)); 465 &decoded_serialized_seed));
452 // Make sure the stored seed from pref is the same as the seed we created. 466 // Make sure the stored seed from pref is the same as the seed we created.
453 EXPECT_EQ(serialized_seed, decoded_serialized_seed); 467 EXPECT_EQ(serialized_seed, decoded_serialized_seed);
454 468
455 // Check if trying to store a bad seed leaves the pref unchanged. 469 // Check if trying to store a bad seed leaves the pref unchanged.
456 pref_service.ClearPref(prefs::kVariationsSeed); 470 local_state()->ClearPref(prefs::kVariationsSeed);
457 EXPECT_FALSE( 471 EXPECT_FALSE(
458 variations_service.StoreSeedData("this should fail", now, &pref_service)); 472 variations_service.StoreSeedData("this should fail", now, local_state()));
459 EXPECT_TRUE( 473 EXPECT_TRUE(
460 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); 474 local_state()->FindPreference(prefs::kVariationsSeed)->IsDefaultValue());
461 } 475 }
462 476
463 TEST(VariationsServiceTest, ValidateStudy) { 477 TEST(VariationsServiceTest, ValidateStudy) {
464 Study study; 478 Study study;
465 study.set_default_experiment_name("def"); 479 study.set_default_experiment_name("def");
466 480
467 Study_Experiment* experiment = study.add_experiment(); 481 Study_Experiment* experiment = study.add_experiment();
468 experiment->set_name("abc"); 482 experiment->set_name("abc");
469 experiment->set_probability_weight(100); 483 experiment->set_probability_weight(100);
470 484
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
527 &total_probability); 541 &total_probability);
528 ASSERT_TRUE(valid); 542 ASSERT_TRUE(valid);
529 Study_Experiment* repeated_group = study.add_experiment(); 543 Study_Experiment* repeated_group = study.add_experiment();
530 repeated_group->set_name("abc"); 544 repeated_group->set_name("abc");
531 repeated_group->set_probability_weight(1); 545 repeated_group->set_probability_weight(1);
532 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, 546 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study,
533 &total_probability); 547 &total_probability);
534 EXPECT_FALSE(valid); 548 EXPECT_FALSE(valid);
535 } 549 }
536 550
537 TEST_F(VariationsServiceNetworkTest, DoNotFetchIfOffline) { 551 TEST_F(VariationsServiceCreatedTest, ResourceRequestAllowedNotifierTest) {
538 SetWasOfflineDuringLastRequestAttempt(true); 552 MessageLoopForUI message_loop;
539 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE); 553 content::TestBrowserThread ui_thread(content::BrowserThread::UI,
540 EXPECT_FALSE(fetch_attempted()); 554 &message_loop);
541 }
542 555
543 TEST_F(VariationsServiceNetworkTest, DoNotFetchIfOnlineToOnline) { 556 TestRequestAllowedNotifier* notifier = new TestRequestAllowedNotifier;
544 SetWasOfflineDuringLastRequestAttempt(false); 557 // Pass ownership to TestVariationsService, but keep a weak pointer to
545 SimulateNetworkConnectionChange( 558 // manipulate it for this test.
546 net::NetworkChangeNotifier::CONNECTION_ETHERNET); 559 TestVariationsService test_service(notifier);
547 EXPECT_FALSE(fetch_attempted()); 560 notifier->SetRequestsAllowed(false);
548 } 561 test_service.StartRepeatedVariationsSeedFetch();
549 562 EXPECT_FALSE(test_service.fetch_attempted());
550 TEST_F(VariationsServiceNetworkTest, FetchOnReconnect) { 563 notifier->NotifyObservers();
551 SetWasOfflineDuringLastRequestAttempt(true); 564 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 } 565 }
585 566
586 } // namespace chrome_variations 567 } // namespace chrome_variations
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698