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

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: Comments addressed. 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/resource_request_allowed_notifier_te st_util.h"
8 #include "chrome/browser/metrics/variations/variations_service.h" 9 #include "chrome/browser/metrics/variations/variations_service.h"
9 #include "chrome/common/chrome_version_info.h" 10 #include "chrome/common/chrome_version_info.h"
10 #include "chrome/common/pref_names.h" 11 #include "chrome/common/pref_names.h"
12 #include "chrome/test/base/testing_browser_process.h"
11 #include "chrome/test/base/testing_pref_service.h" 13 #include "chrome/test/base/testing_pref_service.h"
12 #include "content/public/test/test_browser_thread.h" 14 #include "content/public/test/test_browser_thread.h"
13 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
14 16
15 namespace chrome_variations { 17 namespace chrome_variations {
16 18
17 namespace { 19 namespace {
18 20
19 // A test class used to validate expected functionality in VariationsService. 21 // A test class used to validate expected functionality in VariationsService.
20 class TestVariationsService : public VariationsService { 22 class TestVariationsService : public VariationsService {
21 public: 23 public:
22 TestVariationsService() : VariationsService(), 24 explicit TestVariationsService();
23 fetch_attempted_(false) { 25 virtual ~TestVariationsService();
24 }
25 virtual ~TestVariationsService() {}
26 26
27 bool fetch_attempted() const { return fetch_attempted_; } 27 bool fetch_attempted() const { return fetch_attempted_; }
28 void SetFetchAttempted(bool attempted) { fetch_attempted_ = attempted; } 28
29 void SetRequestsAllowed(bool allowed);
Alexei Svitkine (slow) 2012/09/20 21:39:03 It seems both SetRequestsAllowed() and NotifyObser
SteveT 2012/09/21 15:16:17 Thanks for that suggestion - made things a lot nic
30
31 void NotifyObservers();
29 32
30 protected: 33 protected:
31 virtual void FetchVariationsSeed() OVERRIDE { 34 virtual void DoActualFetch() OVERRIDE {
32 fetch_attempted_ = true; 35 fetch_attempted_ = true;
33 } 36 }
34 37
35 private: 38 private:
36 bool fetch_attempted_; 39 bool fetch_attempted_;
37 40
41 // Weak pointer to the base class' copy of the notifier.
42 TestRequestAllowedNotifier* test_notifier_;
43
38 DISALLOW_COPY_AND_ASSIGN(TestVariationsService); 44 DISALLOW_COPY_AND_ASSIGN(TestVariationsService);
39 }; 45 };
40 46
41 // Override NetworkChangeNotifier to simulate connection type changes for tests. 47 TestVariationsService::TestVariationsService()
42 class TestNetworkChangeNotifier : public net::NetworkChangeNotifier { 48 : VariationsService(new TestRequestAllowedNotifier),
43 public: 49 fetch_attempted_(false),
44 TestNetworkChangeNotifier() 50 // This is known to return a TestRequestAllowedNotifier* since it was
45 : net::NetworkChangeNotifier(), 51 // created just above.
46 connection_type_to_return_( 52 test_notifier_(static_cast<TestRequestAllowedNotifier*>(
47 net::NetworkChangeNotifier::CONNECTION_UNKNOWN) { 53 GetResourceRequestAllowedNotifierForTesting())) {
48 } 54 // Set this so StartRepeatedVariationsSeedFetch can be called in tests.
55 SetCreateTrialsFromSeedCalledForTesting(true);
56 }
49 57
50 void SimulateNetworkConnectionChange( 58 TestVariationsService::~TestVariationsService() {
51 net::NetworkChangeNotifier::ConnectionType type) { 59 }
52 connection_type_to_return_ = type;
53 net::NetworkChangeNotifier::NotifyObserversOfConnectionTypeChange();
54 MessageLoop::current()->RunAllPending();
55 }
56 60
57 private: 61 void TestVariationsService::SetRequestsAllowed(bool allowed) {
58 virtual ConnectionType GetCurrentConnectionType() const OVERRIDE { 62 test_notifier_->SetRequestsAllowed(allowed);
59 return connection_type_to_return_; 63 }
60 }
61 64
62 net::NetworkChangeNotifier::ConnectionType connection_type_to_return_; 65 void TestVariationsService::NotifyObservers() {
63 66 test_notifier_->NotifyObservers();
64 DISALLOW_COPY_AND_ASSIGN(TestNetworkChangeNotifier); 67 }
65 };
66 68
67 // Converts |time| to Study proto format. 69 // Converts |time| to Study proto format.
68 int64 TimeToProtoTime(const base::Time& time) { 70 int64 TimeToProtoTime(const base::Time& time) {
69 return (time - base::Time::UnixEpoch()).InSeconds(); 71 return (time - base::Time::UnixEpoch()).InSeconds();
70 } 72 }
71 73
72 // Populates |seed| with simple test data. The resulting seed will contain one 74 // Populates |seed| with simple test data. The resulting seed will contain one
73 // study called "test", which contains one experiment called "abc" with 75 // study called "test", which contains one experiment called "abc" with
74 // probability weight 100. |seed|'s study field will be cleared before adding 76 // probability weight 100. |seed|'s study field will be cleared before adding
75 // the new study. 77 // the new study.
76 TrialsSeed CreateTestSeed() { 78 TrialsSeed CreateTestSeed() {
77 TrialsSeed seed; 79 TrialsSeed seed;
78 Study* study = seed.add_study(); 80 Study* study = seed.add_study();
79 study->set_name("test"); 81 study->set_name("test");
80 study->set_default_experiment_name("abc"); 82 study->set_default_experiment_name("abc");
81 Study_Experiment* experiment = study->add_experiment(); 83 Study_Experiment* experiment = study->add_experiment();
82 experiment->set_name("abc"); 84 experiment->set_name("abc");
83 experiment->set_probability_weight(100); 85 experiment->set_probability_weight(100);
84 return seed; 86 return seed;
85 } 87 }
86 88
87 } // namespace 89 } // namespace
88 90
89 // A test fixture class for VariationsService tests that require network state
90 // simulations.
91 class VariationsServiceNetworkTest : public testing::Test {
92 public:
93 VariationsServiceNetworkTest()
94 : ui_thread(content::BrowserThread::UI, &message_loop) { }
95 ~VariationsServiceNetworkTest() { }
96
97 void SetWasOfflineDuringLastRequestAttempt(bool offline) {
98 test_service.SetWasOfflineDuringLastRequestAttemptForTesting(offline);
99 }
100
101 void SimulateNetworkConnectionChange(
102 net::NetworkChangeNotifier::ConnectionType type) {
103 notifier.SimulateNetworkConnectionChange(type);
104 }
105
106 bool fetch_attempted() const {
107 return test_service.fetch_attempted();
108 }
109
110 private:
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
119 TEST(VariationsServiceTest, CheckStudyChannel) { 91 TEST(VariationsServiceTest, CheckStudyChannel) {
120 const chrome::VersionInfo::Channel channels[] = { 92 const chrome::VersionInfo::Channel channels[] = {
121 chrome::VersionInfo::CHANNEL_CANARY, 93 chrome::VersionInfo::CHANNEL_CANARY,
122 chrome::VersionInfo::CHANNEL_DEV, 94 chrome::VersionInfo::CHANNEL_DEV,
123 chrome::VersionInfo::CHANNEL_BETA, 95 chrome::VersionInfo::CHANNEL_BETA,
124 chrome::VersionInfo::CHANNEL_STABLE, 96 chrome::VersionInfo::CHANNEL_STABLE,
125 }; 97 };
126 const Study_Channel study_channels[] = { 98 const Study_Channel study_channels[] = {
127 Study_Channel_CANARY, 99 Study_Channel_CANARY,
128 Study_Channel_DEV, 100 Study_Channel_DEV,
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 366
395 // Store good seed data to test if loading from prefs works. 367 // Store good seed data to test if loading from prefs works.
396 TrialsSeed seed = CreateTestSeed(); 368 TrialsSeed seed = CreateTestSeed();
397 369
398 std::string serialized_seed; 370 std::string serialized_seed;
399 seed.SerializeToString(&serialized_seed); 371 seed.SerializeToString(&serialized_seed);
400 std::string base64_serialized_seed; 372 std::string base64_serialized_seed;
401 ASSERT_TRUE(base::Base64Encode(serialized_seed, &base64_serialized_seed)); 373 ASSERT_TRUE(base::Base64Encode(serialized_seed, &base64_serialized_seed));
402 pref_service.SetString(prefs::kVariationsSeed, base64_serialized_seed); 374 pref_service.SetString(prefs::kVariationsSeed, base64_serialized_seed);
403 375
404 VariationsService variations_service; 376 TestVariationsService variations_service;
405 TrialsSeed loaded_seed; 377 TrialsSeed loaded_seed;
406 EXPECT_TRUE( 378 EXPECT_TRUE(
407 variations_service.LoadTrialsSeedFromPref(&pref_service, &loaded_seed)); 379 variations_service.LoadTrialsSeedFromPref(&pref_service, &loaded_seed));
408 380
409 std::string serialized_loaded_seed; 381 std::string serialized_loaded_seed;
410 loaded_seed.SerializeToString(&serialized_loaded_seed); 382 loaded_seed.SerializeToString(&serialized_loaded_seed);
411 // Check that the loaded data is the same as the original. 383 // Check that the loaded data is the same as the original.
412 EXPECT_EQ(serialized_seed, serialized_loaded_seed); 384 EXPECT_EQ(serialized_seed, serialized_loaded_seed);
413 // Make sure the pref hasn't been changed. 385 // Make sure the pref hasn't been changed.
414 EXPECT_FALSE( 386 EXPECT_FALSE(
(...skipping 13 matching lines...) Expand all
428 } 400 }
429 401
430 TEST(VariationsServiceTest, StoreSeed) { 402 TEST(VariationsServiceTest, StoreSeed) {
431 TestingPrefService pref_service; 403 TestingPrefService pref_service;
432 404
433 VariationsService::RegisterPrefs(&pref_service); 405 VariationsService::RegisterPrefs(&pref_service);
434 const base::Time now = base::Time::Now(); 406 const base::Time now = base::Time::Now();
435 407
436 TrialsSeed seed = CreateTestSeed(); 408 TrialsSeed seed = CreateTestSeed();
437 409
438 VariationsService variations_service; 410 TestVariationsService variations_service;
439 std::string serialized_seed; 411 std::string serialized_seed;
440 seed.SerializeToString(&serialized_seed); 412 seed.SerializeToString(&serialized_seed);
441 EXPECT_TRUE( 413 EXPECT_TRUE(
442 variations_service.StoreSeedData(serialized_seed, now, &pref_service)); 414 variations_service.StoreSeedData(serialized_seed, now, &pref_service));
443 // Make sure the pref was actually set. 415 // Make sure the pref was actually set.
444 EXPECT_FALSE( 416 EXPECT_FALSE(
445 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); 417 pref_service.FindPreference(prefs::kVariationsSeed)->IsDefaultValue());
446 418
447 std::string loaded_serialized_seed = 419 std::string loaded_serialized_seed =
448 pref_service.GetString(prefs::kVariationsSeed); 420 pref_service.GetString(prefs::kVariationsSeed);
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
527 &total_probability); 499 &total_probability);
528 ASSERT_TRUE(valid); 500 ASSERT_TRUE(valid);
529 Study_Experiment* repeated_group = study.add_experiment(); 501 Study_Experiment* repeated_group = study.add_experiment();
530 repeated_group->set_name("abc"); 502 repeated_group->set_name("abc");
531 repeated_group->set_probability_weight(1); 503 repeated_group->set_probability_weight(1);
532 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, 504 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study,
533 &total_probability); 505 &total_probability);
534 EXPECT_FALSE(valid); 506 EXPECT_FALSE(valid);
535 } 507 }
536 508
537 TEST_F(VariationsServiceNetworkTest, DoNotFetchIfOffline) { 509 TEST(VariationsServiceTest, ResourceRequestAllowedNotifierTest) {
538 SetWasOfflineDuringLastRequestAttempt(true); 510 MessageLoopForUI message_loop;
539 SimulateNetworkConnectionChange(net::NetworkChangeNotifier::CONNECTION_NONE); 511 content::TestBrowserThread ui_thread(content::BrowserThread::UI,
540 EXPECT_FALSE(fetch_attempted()); 512 &message_loop);
541 }
542 513
543 TEST_F(VariationsServiceNetworkTest, DoNotFetchIfOnlineToOnline) { 514 // Pass ownership to TestVariationsService, but keep a weak pointer to
544 SetWasOfflineDuringLastRequestAttempt(false); 515 // manipulate it for this test.
545 SimulateNetworkConnectionChange( 516 TestVariationsService test_service;
546 net::NetworkChangeNotifier::CONNECTION_ETHERNET); 517 test_service.SetRequestsAllowed(false);
547 EXPECT_FALSE(fetch_attempted()); 518 test_service.StartRepeatedVariationsSeedFetch();
548 } 519 EXPECT_FALSE(test_service.fetch_attempted());
549 520 test_service.NotifyObservers();
550 TEST_F(VariationsServiceNetworkTest, FetchOnReconnect) { 521 EXPECT_TRUE(test_service.fetch_attempted());
551 SetWasOfflineDuringLastRequestAttempt(true);
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 } 522 }
585 523
586 } // namespace chrome_variations 524 } // namespace chrome_variations
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698