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