| 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/metrics/field_trial.h" | 5 #include "base/metrics/field_trial.h" |
| 6 | 6 |
| 7 #include "base/build_time.h" |
| 7 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 8 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
| 9 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 10 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 14 |
| 14 namespace base { | 15 namespace base { |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 // Default group name used by several tests. | 19 // Default group name used by several tests. |
| 19 const char kDefaultGroupName[] = "DefaultGroup"; | 20 const char kDefaultGroupName[] = "DefaultGroup"; |
| 20 | 21 |
| 21 // Call FieldTrialList::FactoryGetFieldTrial() with a future expiry date. | 22 // Call FieldTrialList::FactoryGetFieldTrial() with a future expiry date. |
| 22 scoped_refptr<base::FieldTrial> CreateFieldTrial( | 23 scoped_refptr<base::FieldTrial> CreateFieldTrial( |
| 23 const std::string& trial_name, | 24 const std::string& trial_name, |
| 24 int total_probability, | 25 int total_probability, |
| 25 const std::string& default_group_name, | 26 const std::string& default_group_name, |
| 26 int* default_group_number) { | 27 int* default_group_number) { |
| 27 return FieldTrialList::FactoryGetFieldTrial( | 28 return FieldTrialList::FactoryGetFieldTrial( |
| 28 trial_name, total_probability, default_group_name, | 29 trial_name, total_probability, default_group_name, |
| 29 base::FieldTrialList::kNoExpirationYear, 1, 1, | 30 base::FieldTrialList::kNoExpirationYear, 1, 1, |
| 30 base::FieldTrial::SESSION_RANDOMIZED, default_group_number); | 31 base::FieldTrial::SESSION_RANDOMIZED, default_group_number); |
| 31 } | 32 } |
| 32 | 33 |
| 33 int GetLastYear() { | 34 int OneYearBeforeBuildTime() { |
| 34 Time last_year_time = Time::NowFromSystemTime() - TimeDelta::FromDays(365); | 35 Time one_year_before_build_time = GetBuildTime() - TimeDelta::FromDays(365); |
| 35 Time::Exploded exploded; | 36 Time::Exploded exploded; |
| 36 last_year_time.LocalExplode(&exploded); | 37 one_year_before_build_time.LocalExplode(&exploded); |
| 37 return exploded.year; | 38 return exploded.year; |
| 38 } | 39 } |
| 39 | 40 |
| 40 // FieldTrialList::Observer implementation for testing. | 41 // FieldTrialList::Observer implementation for testing. |
| 41 class TestFieldTrialObserver : public FieldTrialList::Observer { | 42 class TestFieldTrialObserver : public FieldTrialList::Observer { |
| 42 public: | 43 public: |
| 43 TestFieldTrialObserver() { | 44 TestFieldTrialObserver() { |
| 44 FieldTrialList::AddObserver(this); | 45 FieldTrialList::AddObserver(this); |
| 45 } | 46 } |
| 46 | 47 |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 } | 243 } |
| 243 | 244 |
| 244 TEST_F(FieldTrialTest, DisableProbability) { | 245 TEST_F(FieldTrialTest, DisableProbability) { |
| 245 const std::string default_group_name = "Default group"; | 246 const std::string default_group_name = "Default group"; |
| 246 const std::string loser = "Loser"; | 247 const std::string loser = "Loser"; |
| 247 const std::string name = "Trial"; | 248 const std::string name = "Trial"; |
| 248 | 249 |
| 249 // Create a field trail that has expired. | 250 // Create a field trail that has expired. |
| 250 int default_group_number = -1; | 251 int default_group_number = -1; |
| 251 FieldTrial* trial = FieldTrialList::FactoryGetFieldTrial( | 252 FieldTrial* trial = FieldTrialList::FactoryGetFieldTrial( |
| 252 name, 1000000000, default_group_name, GetLastYear(), 1, 1, | 253 name, 1000000000, default_group_name, OneYearBeforeBuildTime(), 1, 1, |
| 253 FieldTrial::SESSION_RANDOMIZED, | 254 FieldTrial::SESSION_RANDOMIZED, |
| 254 &default_group_number); | 255 &default_group_number); |
| 255 trial->AppendGroup(loser, 999999999); // 99.9999999% chance of being chosen. | 256 trial->AppendGroup(loser, 999999999); // 99.9999999% chance of being chosen. |
| 256 | 257 |
| 257 // Because trial has expired, we should always be in the default group. | 258 // Because trial has expired, we should always be in the default group. |
| 258 EXPECT_EQ(default_group_number, trial->group()); | 259 EXPECT_EQ(default_group_number, trial->group()); |
| 259 | 260 |
| 260 // And that default_group_name should ALWAYS win. | 261 // And that default_group_name should ALWAYS win. |
| 261 EXPECT_EQ(default_group_name, trial->group_name()); | 262 EXPECT_EQ(default_group_name, trial->group_name()); |
| 262 } | 263 } |
| (...skipping 732 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 995 // Trying to instantiate a one-time randomized field trial before the | 996 // Trying to instantiate a one-time randomized field trial before the |
| 996 // FieldTrialList is created should crash. | 997 // FieldTrialList is created should crash. |
| 997 EXPECT_DEATH(FieldTrialList::FactoryGetFieldTrial( | 998 EXPECT_DEATH(FieldTrialList::FactoryGetFieldTrial( |
| 998 "OneTimeRandomizedTrialWithoutFieldTrialList", 100, kDefaultGroupName, | 999 "OneTimeRandomizedTrialWithoutFieldTrialList", 100, kDefaultGroupName, |
| 999 base::FieldTrialList::kNoExpirationYear, 1, 1, | 1000 base::FieldTrialList::kNoExpirationYear, 1, 1, |
| 1000 base::FieldTrial::ONE_TIME_RANDOMIZED, NULL), ""); | 1001 base::FieldTrial::ONE_TIME_RANDOMIZED, NULL), ""); |
| 1001 } | 1002 } |
| 1002 #endif | 1003 #endif |
| 1003 | 1004 |
| 1004 } // namespace base | 1005 } // namespace base |
| OLD | NEW |