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" | |
9 #include "chrome/common/chrome_version_info.h" | 11 #include "chrome/common/chrome_version_info.h" |
10 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.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 // 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 // Simulate that an auto-update is ready by sending the appropriate | |
31 // notification to the Observe method. Note that the source and details are | |
32 // not necessary for this notification type. | |
33 void SimulateUpgradeAvailable() { | |
34 Observe(chrome::NOTIFICATION_UPGRADE_RECOMMENDED, | |
35 content::Source<UpgradeDetector>(NULL), | |
36 content::NotificationService::NoDetails()); | |
Ilya Sherman
2012/08/07 20:31:51
nit: This seems a little odd. Why not dispatch th
SteveT
2012/08/08 18:11:39
That makes way more sense, and yes, I do like the
| |
37 } | |
38 | |
39 protected: | |
40 virtual void FetchVariationsSeed() { | |
Ilya Sherman
2012/08/07 20:31:51
nit: OVERRIDE
SteveT
2012/08/08 18:11:39
Done.
| |
41 fetch_attempted_ = true; | |
42 } | |
43 | |
44 private: | |
45 bool fetch_attempted_; | |
46 | |
47 DISALLOW_COPY_AND_ASSIGN(TestVariationsService); | |
48 }; | |
49 | |
18 // Converts |time| to Study proto format. | 50 // Converts |time| to Study proto format. |
19 int64 TimeToProtoTime(const base::Time& time) { | 51 int64 TimeToProtoTime(const base::Time& time) { |
20 return (time - base::Time::UnixEpoch()).InSeconds(); | 52 return (time - base::Time::UnixEpoch()).InSeconds(); |
21 } | 53 } |
22 | 54 |
23 // Populates |seed| with simple test data. The resulting seed will contain one | 55 // Populates |seed| with simple test data. The resulting seed will contain one |
24 // study called "test", which contains one experiment called "abc" with | 56 // study called "test", which contains one experiment called "abc" with |
25 // probability weight 100. |seed|'s study field will be cleared before adding | 57 // probability weight 100. |seed|'s study field will be cleared before adding |
26 // the new study. | 58 // the new study. |
27 chrome_variations::TrialsSeed CreateTestSeed() { | 59 chrome_variations::TrialsSeed CreateTestSeed() { |
28 chrome_variations::TrialsSeed seed; | 60 chrome_variations::TrialsSeed seed; |
29 chrome_variations::Study* study = seed.add_study(); | 61 chrome_variations::Study* study = seed.add_study(); |
30 study->set_name("test"); | 62 study->set_name("test"); |
31 study->set_default_experiment_name("abc"); | 63 study->set_default_experiment_name("abc"); |
32 chrome_variations::Study_Experiment* experiment = study->add_experiment(); | 64 chrome_variations::Study_Experiment* experiment = study->add_experiment(); |
33 experiment->set_name("abc"); | 65 experiment->set_name("abc"); |
34 experiment->set_probability_weight(100); | 66 experiment->set_probability_weight(100); |
35 return seed; | 67 return seed; |
36 } | 68 } |
37 | 69 |
38 } // namespace | 70 } // namespace |
39 | 71 |
72 TEST(VariationsServiceTest, AttemptFetchOnAutoUpdate) { | |
73 // Simulate an auto-update and ensure that the VariationsService attempts | |
74 // to fetch the variations seed. | |
75 MessageLoopForUI message_loop; | |
76 content::TestBrowserThread ui_thread(content::BrowserThread::UI, | |
77 &message_loop); | |
Ilya Sherman
2012/08/07 20:31:51
nit: Why is this needed? If it's just to satisfy
SteveT
2012/08/08 18:11:39
Yes, this was just to satisfy the DCHECKs on Brows
Ilya Sherman
2012/08/08 21:33:47
It's an ok pattern; it's just often overkill and a
| |
78 TestVariationsService test_service; | |
79 ASSERT_FALSE(test_service.fetch_attempted()); | |
Ilya Sherman
2012/08/07 20:31:51
nit: EXPECT_FALSE
SteveT
2012/08/08 18:11:39
Done.
| |
80 test_service.SimulateUpgradeAvailable(); | |
81 ASSERT_TRUE(test_service.fetch_attempted()); | |
Ilya Sherman
2012/08/07 20:31:51
nit: EXPECT_TRUE
SteveT
2012/08/08 18:11:39
Done.
| |
82 } | |
83 | |
40 TEST(VariationsServiceTest, CheckStudyChannel) { | 84 TEST(VariationsServiceTest, CheckStudyChannel) { |
41 const chrome::VersionInfo::Channel channels[] = { | 85 const chrome::VersionInfo::Channel channels[] = { |
42 chrome::VersionInfo::CHANNEL_CANARY, | 86 chrome::VersionInfo::CHANNEL_CANARY, |
43 chrome::VersionInfo::CHANNEL_DEV, | 87 chrome::VersionInfo::CHANNEL_DEV, |
44 chrome::VersionInfo::CHANNEL_BETA, | 88 chrome::VersionInfo::CHANNEL_BETA, |
45 chrome::VersionInfo::CHANNEL_STABLE, | 89 chrome::VersionInfo::CHANNEL_STABLE, |
46 }; | 90 }; |
47 const Study_Channel study_channels[] = { | 91 const Study_Channel study_channels[] = { |
48 Study_Channel_CANARY, | 92 Study_Channel_CANARY, |
49 Study_Channel_DEV, | 93 Study_Channel_DEV, |
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
449 ASSERT_TRUE(valid); | 493 ASSERT_TRUE(valid); |
450 Study_Experiment* repeated_group = study.add_experiment(); | 494 Study_Experiment* repeated_group = study.add_experiment(); |
451 repeated_group->set_name("abc"); | 495 repeated_group->set_name("abc"); |
452 repeated_group->set_probability_weight(1); | 496 repeated_group->set_probability_weight(1); |
453 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, | 497 valid = VariationsService::ValidateStudyAndComputeTotalProbability(study, |
454 &total_probability); | 498 &total_probability); |
455 EXPECT_FALSE(valid); | 499 EXPECT_FALSE(valid); |
456 } | 500 } |
457 | 501 |
458 } // namespace chrome_variations | 502 } // namespace chrome_variations |
OLD | NEW |