Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/metrics/proto/study.pb.h" | |
| 6 #include "chrome/browser/metrics/variations_service.h" | |
| 7 #include "chrome/common/chrome_version_info.h" | |
| 8 #include "testing/gtest/include/gtest/gtest.h" | |
| 9 | |
| 10 namespace { | |
| 11 | |
| 12 // Converts |time| to chrome_variations::Study proto format. | |
| 13 int64 TimeToProtoTime(const base::Time& time) { | |
| 14 return (time - base::Time::UnixEpoch()).InMilliseconds(); | |
| 15 } | |
| 16 | |
| 17 } // namespace | |
| 18 | |
| 19 | |
| 20 TEST(VariationsServiceTest, CheckStudyChannel) { | |
| 21 const chrome::VersionInfo::Channel channels[] = { | |
| 22 chrome::VersionInfo::CHANNEL_CANARY, | |
| 23 chrome::VersionInfo::CHANNEL_DEV, | |
| 24 chrome::VersionInfo::CHANNEL_BETA, | |
| 25 chrome::VersionInfo::CHANNEL_STABLE, | |
| 26 }; | |
| 27 const chrome_variations::Study_Channel study_channels[] = { | |
| 28 chrome_variations::Study_Channel_CANARY, | |
| 29 chrome_variations::Study_Channel_DEV, | |
| 30 chrome_variations::Study_Channel_BETA, | |
| 31 chrome_variations::Study_Channel_STABLE, | |
| 32 }; | |
| 33 ASSERT_EQ(arraysize(channels), arraysize(study_channels)); | |
| 34 | |
| 35 chrome_variations::Study study; | |
| 36 for (size_t i = 0; i < arraysize(study_channels); ++i) { | |
| 37 for (size_t j = 0; j < arraysize(channels); ++j) { | |
| 38 // All channels up to, but not including |channels[i]| have been added. | |
|
MAD
2012/05/05 13:51:17
I would also do it backwards, so that we check tha
Alexei Svitkine (slow)
2012/05/07 15:49:52
Done.
| |
| 39 const bool expected = (i > j); | |
| 40 const bool result = VariationsService::CheckStudyChannel(study, | |
| 41 channels[j]); | |
| 42 EXPECT_EQ(expected, result) << "Case " << i << "," << j << " failed!"; | |
| 43 } | |
| 44 study.add_channel(study_channels[i]); | |
|
MAD
2012/05/05 13:51:17
Is it me or you won't be checking the last channel
Alexei Svitkine (slow)
2012/05/07 15:49:52
You're right. Fixed.
| |
| 45 } | |
| 46 } | |
| 47 | |
| 48 TEST(VariationsServiceTest, CheckStudyVersion) { | |
| 49 const struct { | |
| 50 const char* min_version; | |
| 51 const char* version; | |
| 52 bool expected_result; | |
| 53 } min_test_cases[] = { | |
| 54 { "1.2.2", "1.2.3", true }, | |
| 55 { "1.2.3", "1.2.3", true }, | |
| 56 { "1.2.4", "1.2.3", false }, | |
| 57 { "1.3.2", "1.2.3", false }, | |
| 58 { "2.1.2", "1.2.3", false }, | |
| 59 { "0.3.4", "1.2.3", true }, | |
| 60 }; | |
| 61 | |
| 62 const struct { | |
| 63 const char* max_version; | |
| 64 const char* version; | |
| 65 bool expected_result; | |
| 66 } max_test_cases[] = { | |
| 67 { "1.2.2", "1.2.3", false }, | |
| 68 { "1.2.3", "1.2.3", true }, | |
| 69 { "1.2.4", "1.2.3", true }, | |
| 70 { "2.1.1", "1.2.3", true }, | |
| 71 { "2.1.1", "2.3.4", false }, | |
| 72 }; | |
| 73 | |
| 74 chrome_variations::Study study; | |
| 75 | |
| 76 // Min/max version not set should result in true. | |
| 77 EXPECT_TRUE(VariationsService::CheckStudyVersion(study, "1.2.3")); | |
| 78 | |
| 79 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(min_test_cases); ++i) { | |
| 80 study.set_min_version(min_test_cases[i].min_version); | |
| 81 const bool result = | |
| 82 VariationsService::CheckStudyVersion(study, min_test_cases[i].version); | |
| 83 EXPECT_EQ(min_test_cases[i].expected_result, result) << | |
| 84 "Case " << i << " failed!"; | |
| 85 } | |
| 86 study.clear_min_version(); | |
| 87 | |
| 88 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(max_test_cases); ++i) { | |
| 89 study.set_max_version(max_test_cases[i].max_version); | |
| 90 const bool result = | |
| 91 VariationsService::CheckStudyVersion(study, max_test_cases[i].version); | |
| 92 EXPECT_EQ(max_test_cases[i].expected_result, result) << | |
| 93 "Case " << i << " failed!"; | |
| 94 } | |
| 95 | |
| 96 // Check intersection semantics. | |
|
MAD
2012/05/05 13:51:17
Excellent! :-)
| |
| 97 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(min_test_cases); ++i) { | |
| 98 for (size_t j = 0; j < ARRAYSIZE_UNSAFE(max_test_cases); ++j) { | |
| 99 study.set_min_version(min_test_cases[i].min_version); | |
| 100 study.set_max_version(max_test_cases[j].max_version); | |
| 101 | |
| 102 if (!min_test_cases[i].expected_result) { | |
|
MAD
2012/05/05 13:51:17
Why not use the same scheme as below for the dates
Alexei Svitkine (slow)
2012/05/05 15:14:37
That one only works if the input values are the sa
| |
| 103 const bool result = | |
| 104 VariationsService::CheckStudyVersion(study, | |
| 105 min_test_cases[i].version); | |
| 106 EXPECT_FALSE(result) << "Case " << i << "," << j << " failed!"; | |
| 107 } | |
| 108 | |
| 109 if (!max_test_cases[j].expected_result) { | |
| 110 const bool result = | |
| 111 VariationsService::CheckStudyVersion(study, | |
| 112 max_test_cases[j].version); | |
| 113 EXPECT_FALSE(result) << "Case " << i << "," << j << " failed!"; | |
| 114 } | |
| 115 } | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 // The current client logic does not version numbers containing wildcards. | |
|
MAD
2012/05/05 13:51:17
[...]does not "handle" version[...]?
Alexei Svitkine (slow)
2012/05/07 15:49:52
Done.
| |
| 120 // Check that any such values received from the server result in the study | |
| 121 // being disqualified. | |
| 122 TEST(VariationsServiceTest, CheckStudyVersionWildcards) { | |
| 123 chrome_variations::Study study; | |
| 124 | |
| 125 study.set_min_version("1.0.*"); | |
| 126 EXPECT_FALSE(VariationsService::CheckStudyVersion(study, "1.2.3")); | |
| 127 | |
| 128 study.clear_min_version(); | |
| 129 study.set_max_version("2.0.*"); | |
| 130 EXPECT_FALSE(VariationsService::CheckStudyVersion(study, "1.2.3")); | |
| 131 } | |
| 132 | |
| 133 TEST(VariationsServiceTest, CheckStudyDate) { | |
| 134 const base::Time now = base::Time::Now(); | |
| 135 const base::TimeDelta delta = base::TimeDelta::FromHours(1); | |
| 136 const struct { | |
| 137 const base::Time start_date; | |
| 138 bool expected_result; | |
| 139 } start_test_cases[] = { | |
| 140 { now - delta, true }, | |
| 141 { now, true }, | |
| 142 { now + delta, false }, | |
| 143 }; | |
| 144 const struct { | |
| 145 const base::Time expiry_date; | |
| 146 bool expected_result; | |
| 147 } expiry_test_cases[] = { | |
| 148 { now - delta, false }, | |
| 149 { now, false }, | |
| 150 { now + delta, true }, | |
| 151 }; | |
| 152 | |
| 153 chrome_variations::Study study; | |
| 154 | |
| 155 // Start/expiry date not set should result in true. | |
| 156 EXPECT_TRUE(VariationsService::CheckStudyDate(study, now)); | |
| 157 | |
| 158 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(start_test_cases); ++i) { | |
| 159 study.set_start_date(TimeToProtoTime(start_test_cases[i].start_date)); | |
| 160 const bool result = VariationsService::CheckStudyDate(study, now); | |
| 161 EXPECT_EQ(start_test_cases[i].expected_result, result) | |
| 162 << "Case " << i << " failed!"; | |
| 163 } | |
| 164 study.clear_start_date(); | |
| 165 | |
| 166 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expiry_test_cases); ++i) { | |
| 167 study.set_expiry_date(TimeToProtoTime(expiry_test_cases[i].expiry_date)); | |
| 168 const bool result = VariationsService::CheckStudyDate(study, now); | |
| 169 EXPECT_EQ(expiry_test_cases[i].expected_result, result) | |
| 170 << "Case " << i << " failed!"; | |
| 171 } | |
| 172 | |
| 173 // Check intersection semantics. | |
| 174 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(start_test_cases); ++i) { | |
| 175 for (size_t j = 0; j < ARRAYSIZE_UNSAFE(expiry_test_cases); ++j) { | |
| 176 study.set_start_date(TimeToProtoTime(start_test_cases[i].start_date)); | |
| 177 study.set_expiry_date(TimeToProtoTime(expiry_test_cases[j].expiry_date)); | |
| 178 const bool expected = start_test_cases[i].expected_result && | |
| 179 expiry_test_cases[j].expected_result; | |
| 180 const bool result = VariationsService::CheckStudyDate(study, now); | |
| 181 EXPECT_EQ(expected, result) << "Case " << i << "," << j << " failed!"; | |
| 182 } | |
| 183 } | |
| 184 } | |
| OLD | NEW |