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 "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_service.h" | 8 #include "chrome/browser/metrics/variations_service.h" |
9 #include "chrome/browser/upgrade_detector.h" | |
10 #include "chrome/common/chrome_notification_types.h" | |
11 #include "chrome/common/chrome_version_info.h" | 9 #include "chrome/common/chrome_version_info.h" |
12 #include "chrome/common/pref_names.h" | 10 #include "chrome/common/pref_names.h" |
13 #include "chrome/test/base/testing_pref_service.h" | 11 #include "chrome/test/base/testing_pref_service.h" |
14 #include "content/public/browser/notification_service.h" | |
15 #include "content/public/test/test_browser_thread.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
17 | 13 |
18 namespace chrome_variations { | 14 namespace chrome_variations { |
19 | 15 |
20 namespace { | 16 namespace { |
21 | 17 |
22 // A test class used to validate expected functionality in VariationsService. | |
23 class TestVariationsService : public VariationsService { | |
24 public: | |
25 TestVariationsService() : fetch_attempted_(false) {} | |
26 virtual ~TestVariationsService() {} | |
27 | |
28 bool fetch_attempted() { return fetch_attempted_; } | |
29 | |
30 protected: | |
31 virtual void FetchVariationsSeed() OVERRIDE { | |
32 fetch_attempted_ = true; | |
33 } | |
34 | |
35 private: | |
36 bool fetch_attempted_; | |
37 | |
38 DISALLOW_COPY_AND_ASSIGN(TestVariationsService); | |
39 }; | |
40 | |
41 // Converts |time| to Study proto format. | 18 // Converts |time| to Study proto format. |
42 int64 TimeToProtoTime(const base::Time& time) { | 19 int64 TimeToProtoTime(const base::Time& time) { |
43 return (time - base::Time::UnixEpoch()).InSeconds(); | 20 return (time - base::Time::UnixEpoch()).InSeconds(); |
44 } | 21 } |
45 | 22 |
46 // Populates |seed| with simple test data. The resulting seed will contain one | 23 // Populates |seed| with simple test data. The resulting seed will contain one |
47 // study called "test", which contains one experiment called "abc" with | 24 // study called "test", which contains one experiment called "abc" with |
48 // probability weight 100. |seed|'s study field will be cleared before adding | 25 // probability weight 100. |seed|'s study field will be cleared before adding |
49 // the new study. | 26 // the new study. |
50 chrome_variations::TrialsSeed CreateTestSeed() { | 27 chrome_variations::TrialsSeed CreateTestSeed() { |
51 chrome_variations::TrialsSeed seed; | 28 chrome_variations::TrialsSeed seed; |
52 chrome_variations::Study* study = seed.add_study(); | 29 chrome_variations::Study* study = seed.add_study(); |
53 study->set_name("test"); | 30 study->set_name("test"); |
54 study->set_default_experiment_name("abc"); | 31 study->set_default_experiment_name("abc"); |
55 chrome_variations::Study_Experiment* experiment = study->add_experiment(); | 32 chrome_variations::Study_Experiment* experiment = study->add_experiment(); |
56 experiment->set_name("abc"); | 33 experiment->set_name("abc"); |
57 experiment->set_probability_weight(100); | 34 experiment->set_probability_weight(100); |
58 return seed; | 35 return seed; |
59 } | 36 } |
60 | 37 |
61 } // namespace | 38 } // namespace |
62 | 39 |
63 TEST(VariationsServiceTest, AttemptFetchOnAutoUpdate) { | |
64 // Simulate an auto-update and ensure that the VariationsService attempts | |
65 // to fetch the Variations seed. | |
66 MessageLoopForUI message_loop; | |
67 content::TestBrowserThread ui_thread(content::BrowserThread::UI, | |
68 &message_loop); | |
69 TestVariationsService test_service; | |
70 EXPECT_FALSE(test_service.fetch_attempted()); | |
71 content::NotificationService::current()->Notify( | |
72 chrome::NOTIFICATION_UPGRADE_RECOMMENDED, | |
73 content::Source<UpgradeDetector>(UpgradeDetector::GetInstance()), | |
74 content::NotificationService::NoDetails()); | |
75 EXPECT_TRUE(test_service.fetch_attempted()); | |
76 } | |
77 | |
78 TEST(VariationsServiceTest, CheckStudyChannel) { | 40 TEST(VariationsServiceTest, CheckStudyChannel) { |
79 const chrome::VersionInfo::Channel channels[] = { | 41 const chrome::VersionInfo::Channel channels[] = { |
80 chrome::VersionInfo::CHANNEL_CANARY, | 42 chrome::VersionInfo::CHANNEL_CANARY, |
81 chrome::VersionInfo::CHANNEL_DEV, | 43 chrome::VersionInfo::CHANNEL_DEV, |
82 chrome::VersionInfo::CHANNEL_BETA, | 44 chrome::VersionInfo::CHANNEL_BETA, |
83 chrome::VersionInfo::CHANNEL_STABLE, | 45 chrome::VersionInfo::CHANNEL_STABLE, |
84 }; | 46 }; |
85 const Study_Channel study_channels[] = { | 47 const Study_Channel study_channels[] = { |
86 Study_Channel_CANARY, | 48 Study_Channel_CANARY, |
87 Study_Channel_DEV, | 49 Study_Channel_DEV, |
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
487 ASSERT_TRUE(valid); | 449 ASSERT_TRUE(valid); |
488 Study_Experiment* repeated_group = study.add_experiment(); | 450 Study_Experiment* repeated_group = study.add_experiment(); |
489 repeated_group->set_name("abc"); | 451 repeated_group->set_name("abc"); |
490 repeated_group->set_probability_weight(1); | 452 repeated_group->set_probability_weight(1); |
491 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, | 453 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, |
492 &total_probability); | 454 &total_probability); |
493 EXPECT_FALSE(valid); | 455 EXPECT_FALSE(valid); |
494 } | 456 } |
495 | 457 |
496 } // namespace chrome_variations | 458 } // namespace chrome_variations |
OLD | NEW |