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