sdefresne | 246c564 | 2015-11-16 21:47:29 | [diff] [blame] | 1 | // Copyright 2015 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 "components/flags_ui/feature_entry.h" |
| 6 | |
| 7 | #include "base/logging.h" |
| 8 | #include "base/strings/string_number_conversions.h" |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 9 | #include "base/strings/utf_string_conversions.h" |
sdefresne | 246c564 | 2015-11-16 21:47:29 | [diff] [blame] | 10 | #include "ui/base/l10n/l10n_util.h" |
| 11 | |
| 12 | namespace flags_ui { |
| 13 | |
vabr | 0215a8e | 2017-03-28 12:47:34 | [diff] [blame] | 14 | const char kGenericExperimentChoiceDefault[] = "Default"; |
| 15 | const char kGenericExperimentChoiceEnabled[] = "Enabled"; |
| 16 | const char kGenericExperimentChoiceDisabled[] = "Disabled"; |
| 17 | const char kGenericExperimentChoiceAutomatic[] = "Automatic"; |
| 18 | |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 19 | std::string FeatureEntry::NameForOption(int index) const { |
sdefresne | 246c564 | 2015-11-16 21:47:29 | [diff] [blame] | 20 | DCHECK(type == FeatureEntry::MULTI_VALUE || |
| 21 | type == FeatureEntry::ENABLE_DISABLE_VALUE || |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 22 | type == FeatureEntry::FEATURE_VALUE || |
jkrcal | 531f3675 | 2017-03-22 13:44:25 | [diff] [blame] | 23 | type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE); |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 24 | DCHECK_LT(index, num_options); |
sdefresne | 246c564 | 2015-11-16 21:47:29 | [diff] [blame] | 25 | return std::string(internal_name) + testing::kMultiSeparator + |
| 26 | base::IntToString(index); |
| 27 | } |
| 28 | |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 29 | base::string16 FeatureEntry::DescriptionForOption(int index) const { |
sdefresne | 246c564 | 2015-11-16 21:47:29 | [diff] [blame] | 30 | DCHECK(type == FeatureEntry::MULTI_VALUE || |
| 31 | type == FeatureEntry::ENABLE_DISABLE_VALUE || |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 32 | type == FeatureEntry::FEATURE_VALUE || |
jkrcal | 531f3675 | 2017-03-22 13:44:25 | [diff] [blame] | 33 | type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE); |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 34 | DCHECK_LT(index, num_options); |
vabr | 0215a8e | 2017-03-28 12:47:34 | [diff] [blame] | 35 | const char* description = nullptr; |
sdefresne | 246c564 | 2015-11-16 21:47:29 | [diff] [blame] | 36 | if (type == FeatureEntry::ENABLE_DISABLE_VALUE || |
| 37 | type == FeatureEntry::FEATURE_VALUE) { |
vabr | 0215a8e | 2017-03-28 12:47:34 | [diff] [blame] | 38 | const char* kEnableDisableDescriptions[] = { |
| 39 | kGenericExperimentChoiceDefault, kGenericExperimentChoiceEnabled, |
| 40 | kGenericExperimentChoiceDisabled, |
sdefresne | 246c564 | 2015-11-16 21:47:29 | [diff] [blame] | 41 | }; |
vabr | 0215a8e | 2017-03-28 12:47:34 | [diff] [blame] | 42 | description = kEnableDisableDescriptions[index]; |
jkrcal | 531f3675 | 2017-03-22 13:44:25 | [diff] [blame] | 43 | } else if (type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE) { |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 44 | if (index == 0) { |
vabr | 0215a8e | 2017-03-28 12:47:34 | [diff] [blame] | 45 | description = kGenericExperimentChoiceDefault; |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 46 | } else if (index == 1) { |
vabr | 0215a8e | 2017-03-28 12:47:34 | [diff] [blame] | 47 | description = kGenericExperimentChoiceEnabled; |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 48 | } else if (index < num_options - 1) { |
jkrcal | 05f7ce3 | 2016-07-13 16:42:09 | [diff] [blame] | 49 | // First two options do not have variations params. |
| 50 | int variation_index = index - 2; |
vabr | 0215a8e | 2017-03-28 12:47:34 | [diff] [blame] | 51 | return base::ASCIIToUTF16( |
| 52 | base::StringPiece(kGenericExperimentChoiceEnabled)) + |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 53 | base::ASCIIToUTF16(" ") + |
| 54 | base::ASCIIToUTF16( |
| 55 | feature_variations[variation_index].description_text); |
| 56 | } else { |
| 57 | DCHECK_EQ(num_options - 1, index); |
vabr | 0215a8e | 2017-03-28 12:47:34 | [diff] [blame] | 58 | description = kGenericExperimentChoiceDisabled; |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 59 | } |
sdefresne | 246c564 | 2015-11-16 21:47:29 | [diff] [blame] | 60 | } else { |
vabr | 0215a8e | 2017-03-28 12:47:34 | [diff] [blame] | 61 | description = choices[index].description; |
sdefresne | 246c564 | 2015-11-16 21:47:29 | [diff] [blame] | 62 | } |
vabr | 0215a8e | 2017-03-28 12:47:34 | [diff] [blame] | 63 | return base::ASCIIToUTF16(base::StringPiece(description)); |
sdefresne | 246c564 | 2015-11-16 21:47:29 | [diff] [blame] | 64 | } |
| 65 | |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 66 | const FeatureEntry::Choice& FeatureEntry::ChoiceForOption(int index) const { |
| 67 | DCHECK_EQ(FeatureEntry::MULTI_VALUE, type); |
| 68 | DCHECK_LT(index, num_options); |
| 69 | |
| 70 | return choices[index]; |
| 71 | } |
| 72 | |
| 73 | FeatureEntry::FeatureState FeatureEntry::StateForOption(int index) const { |
| 74 | DCHECK(type == FeatureEntry::FEATURE_VALUE || |
jkrcal | 531f3675 | 2017-03-22 13:44:25 | [diff] [blame] | 75 | type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE); |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 76 | DCHECK_LT(index, num_options); |
| 77 | |
| 78 | if (index == 0) |
| 79 | return FeatureEntry::FeatureState::DEFAULT; |
Lei Zhang | 6779c82 | 2017-11-28 01:00:18 | [diff] [blame^] | 80 | if (index == num_options - 1) |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 81 | return FeatureEntry::FeatureState::DISABLED; |
Lei Zhang | 6779c82 | 2017-11-28 01:00:18 | [diff] [blame^] | 82 | return FeatureEntry::FeatureState::ENABLED; |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | const FeatureEntry::FeatureVariation* FeatureEntry::VariationForOption( |
| 86 | int index) const { |
| 87 | DCHECK(type == FeatureEntry::FEATURE_VALUE || |
jkrcal | 531f3675 | 2017-03-22 13:44:25 | [diff] [blame] | 88 | type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE); |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 89 | DCHECK_LT(index, num_options); |
| 90 | |
jkrcal | 531f3675 | 2017-03-22 13:44:25 | [diff] [blame] | 91 | if (type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE && index > 1 && |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 92 | index < num_options - 1) { |
| 93 | // We have no variations for FEATURE_VALUE type. Option at |index| |
jkrcal | 05f7ce3 | 2016-07-13 16:42:09 | [diff] [blame] | 94 | // corresponds to variation at |index| - 2 as the list starts with "Default" |
| 95 | // and "Enabled" (with default parameters). |
| 96 | return &feature_variations[index - 2]; |
jkrcal | 1383d1d | 2016-06-17 12:40:56 | [diff] [blame] | 97 | } |
| 98 | return nullptr; |
| 99 | } |
| 100 | |
sdefresne | 246c564 | 2015-11-16 21:47:29 | [diff] [blame] | 101 | namespace testing { |
| 102 | |
| 103 | // WARNING: '@' is also used in the html file. If you update this constant you |
| 104 | // also need to update the html file. |
| 105 | const char kMultiSeparator[] = "@"; |
| 106 | |
| 107 | } // namespace testing |
| 108 | |
| 109 | } // namespace flags_ui |