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