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()).InSeconds(); |
| 15 } |
| 16 |
| 17 } // namespace |
| 18 |
| 19 TEST(VariationsServiceTest, CheckStudyChannel) { |
| 20 const chrome::VersionInfo::Channel channels[] = { |
| 21 chrome::VersionInfo::CHANNEL_CANARY, |
| 22 chrome::VersionInfo::CHANNEL_DEV, |
| 23 chrome::VersionInfo::CHANNEL_BETA, |
| 24 chrome::VersionInfo::CHANNEL_STABLE, |
| 25 }; |
| 26 const chrome_variations::Study_Channel study_channels[] = { |
| 27 chrome_variations::Study_Channel_CANARY, |
| 28 chrome_variations::Study_Channel_DEV, |
| 29 chrome_variations::Study_Channel_BETA, |
| 30 chrome_variations::Study_Channel_STABLE, |
| 31 }; |
| 32 ASSERT_EQ(arraysize(channels), arraysize(study_channels)); |
| 33 bool channel_added[arraysize(channels)] = { 0 }; |
| 34 |
| 35 chrome_variations::Study study; |
| 36 |
| 37 // Check in the forwarded order. The loop cond is <= arraysize(study_channels) |
| 38 // instead of < so that the result of adding the last channel gets checked. |
| 39 for (size_t i = 0; i <= arraysize(study_channels); ++i) { |
| 40 for (size_t j = 0; j < arraysize(channels); ++j) { |
| 41 const bool expected = channel_added[j] || study.channel_size() == 0; |
| 42 const bool result = VariationsService::CheckStudyChannel(study, |
| 43 channels[j]); |
| 44 EXPECT_EQ(expected, result) << "Case " << i << "," << j << " failed!"; |
| 45 } |
| 46 |
| 47 if (i < arraysize(study_channels)) |
| 48 { |
| 49 study.add_channel(study_channels[i]); |
| 50 channel_added[i] = true; |
| 51 } |
| 52 } |
| 53 |
| 54 // Do the same check in the reverse order. |
| 55 study.clear_channel(); |
| 56 memset(&channel_added, 0, sizeof(channel_added)); |
| 57 for (size_t i = 0; i <= arraysize(study_channels); ++i) { |
| 58 for (size_t j = 0; j < arraysize(channels); ++j) { |
| 59 const bool expected = channel_added[j] || study.channel_size() == 0; |
| 60 const bool result = VariationsService::CheckStudyChannel(study, |
| 61 channels[j]); |
| 62 EXPECT_EQ(expected, result) << "Case " << i << "," << j << " failed!"; |
| 63 } |
| 64 |
| 65 if (i < arraysize(study_channels)) |
| 66 { |
| 67 const int index = arraysize(study_channels) - i - 1; |
| 68 study.add_channel(study_channels[index]); |
| 69 channel_added[index] = true; |
| 70 } |
| 71 } |
| 72 } |
| 73 |
| 74 TEST(VariationsServiceTest, CheckStudyVersion) { |
| 75 const struct { |
| 76 const char* min_version; |
| 77 const char* version; |
| 78 bool expected_result; |
| 79 } min_test_cases[] = { |
| 80 { "1.2.2", "1.2.3", true }, |
| 81 { "1.2.3", "1.2.3", true }, |
| 82 { "1.2.4", "1.2.3", false }, |
| 83 { "1.3.2", "1.2.3", false }, |
| 84 { "2.1.2", "1.2.3", false }, |
| 85 { "0.3.4", "1.2.3", true }, |
| 86 }; |
| 87 |
| 88 const struct { |
| 89 const char* max_version; |
| 90 const char* version; |
| 91 bool expected_result; |
| 92 } max_test_cases[] = { |
| 93 { "1.2.2", "1.2.3", false }, |
| 94 { "1.2.3", "1.2.3", true }, |
| 95 { "1.2.4", "1.2.3", true }, |
| 96 { "2.1.1", "1.2.3", true }, |
| 97 { "2.1.1", "2.3.4", false }, |
| 98 }; |
| 99 |
| 100 chrome_variations::Study study; |
| 101 |
| 102 // Min/max version not set should result in true. |
| 103 EXPECT_TRUE(VariationsService::CheckStudyVersion(study, "1.2.3")); |
| 104 |
| 105 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(min_test_cases); ++i) { |
| 106 study.set_min_version(min_test_cases[i].min_version); |
| 107 const bool result = |
| 108 VariationsService::CheckStudyVersion(study, min_test_cases[i].version); |
| 109 EXPECT_EQ(min_test_cases[i].expected_result, result) << |
| 110 "Case " << i << " failed!"; |
| 111 } |
| 112 study.clear_min_version(); |
| 113 |
| 114 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(max_test_cases); ++i) { |
| 115 study.set_max_version(max_test_cases[i].max_version); |
| 116 const bool result = |
| 117 VariationsService::CheckStudyVersion(study, max_test_cases[i].version); |
| 118 EXPECT_EQ(max_test_cases[i].expected_result, result) << |
| 119 "Case " << i << " failed!"; |
| 120 } |
| 121 |
| 122 // Check intersection semantics. |
| 123 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(min_test_cases); ++i) { |
| 124 for (size_t j = 0; j < ARRAYSIZE_UNSAFE(max_test_cases); ++j) { |
| 125 study.set_min_version(min_test_cases[i].min_version); |
| 126 study.set_max_version(max_test_cases[j].max_version); |
| 127 |
| 128 if (!min_test_cases[i].expected_result) { |
| 129 const bool result = |
| 130 VariationsService::CheckStudyVersion(study, |
| 131 min_test_cases[i].version); |
| 132 EXPECT_FALSE(result) << "Case " << i << "," << j << " failed!"; |
| 133 } |
| 134 |
| 135 if (!max_test_cases[j].expected_result) { |
| 136 const bool result = |
| 137 VariationsService::CheckStudyVersion(study, |
| 138 max_test_cases[j].version); |
| 139 EXPECT_FALSE(result) << "Case " << i << "," << j << " failed!"; |
| 140 } |
| 141 } |
| 142 } |
| 143 } |
| 144 |
| 145 // The current client logic does not handle version number strings containing |
| 146 // wildcards. Check that any such values received from the server result in the |
| 147 // study being disqualified. |
| 148 TEST(VariationsServiceTest, CheckStudyVersionWildcards) { |
| 149 chrome_variations::Study study; |
| 150 |
| 151 study.set_min_version("1.0.*"); |
| 152 EXPECT_FALSE(VariationsService::CheckStudyVersion(study, "1.2.3")); |
| 153 |
| 154 study.clear_min_version(); |
| 155 study.set_max_version("2.0.*"); |
| 156 EXPECT_FALSE(VariationsService::CheckStudyVersion(study, "1.2.3")); |
| 157 } |
| 158 |
| 159 TEST(VariationsServiceTest, CheckStudyDate) { |
| 160 const base::Time now = base::Time::Now(); |
| 161 const base::TimeDelta delta = base::TimeDelta::FromHours(1); |
| 162 const struct { |
| 163 const base::Time start_date; |
| 164 bool expected_result; |
| 165 } start_test_cases[] = { |
| 166 { now - delta, true }, |
| 167 { now, true }, |
| 168 { now + delta, false }, |
| 169 }; |
| 170 const struct { |
| 171 const base::Time expiry_date; |
| 172 bool expected_result; |
| 173 } expiry_test_cases[] = { |
| 174 { now - delta, false }, |
| 175 { now, false }, |
| 176 { now + delta, true }, |
| 177 }; |
| 178 |
| 179 chrome_variations::Study study; |
| 180 |
| 181 // Start/expiry date not set should result in true. |
| 182 EXPECT_TRUE(VariationsService::CheckStudyDate(study, now)); |
| 183 |
| 184 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(start_test_cases); ++i) { |
| 185 study.set_start_date(TimeToProtoTime(start_test_cases[i].start_date)); |
| 186 const bool result = VariationsService::CheckStudyDate(study, now); |
| 187 EXPECT_EQ(start_test_cases[i].expected_result, result) |
| 188 << "Case " << i << " failed!"; |
| 189 } |
| 190 study.clear_start_date(); |
| 191 |
| 192 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(expiry_test_cases); ++i) { |
| 193 study.set_expiry_date(TimeToProtoTime(expiry_test_cases[i].expiry_date)); |
| 194 const bool result = VariationsService::CheckStudyDate(study, now); |
| 195 EXPECT_EQ(expiry_test_cases[i].expected_result, result) |
| 196 << "Case " << i << " failed!"; |
| 197 } |
| 198 |
| 199 // Check intersection semantics. |
| 200 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(start_test_cases); ++i) { |
| 201 for (size_t j = 0; j < ARRAYSIZE_UNSAFE(expiry_test_cases); ++j) { |
| 202 study.set_start_date(TimeToProtoTime(start_test_cases[i].start_date)); |
| 203 study.set_expiry_date(TimeToProtoTime(expiry_test_cases[j].expiry_date)); |
| 204 const bool expected = start_test_cases[i].expected_result && |
| 205 expiry_test_cases[j].expected_result; |
| 206 const bool result = VariationsService::CheckStudyDate(study, now); |
| 207 EXPECT_EQ(expected, result) << "Case " << i << "," << j << " failed!"; |
| 208 } |
| 209 } |
| 210 } |
OLD | NEW |