blob: 53a219c3d55c59d51ca41be641e86eec610b9aa6 [file] [log] [blame]
sdefresne246c5642015-11-16 21:47:291// 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"
jkrcal1383d1d2016-06-17 12:40:569#include "base/strings/utf_string_conversions.h"
sdefresne246c5642015-11-16 21:47:2910#include "grit/components_strings.h"
11#include "ui/base/l10n/l10n_util.h"
12
13namespace flags_ui {
14
jkrcal1383d1d2016-06-17 12:40:5615std::string FeatureEntry::NameForOption(int index) const {
sdefresne246c5642015-11-16 21:47:2916 DCHECK(type == FeatureEntry::MULTI_VALUE ||
17 type == FeatureEntry::ENABLE_DISABLE_VALUE ||
jkrcal1383d1d2016-06-17 12:40:5618 type == FeatureEntry::FEATURE_VALUE ||
19 type == FeatureEntry::FEATURE_WITH_VARIATIONS_VALUE);
20 DCHECK_LT(index, num_options);
sdefresne246c5642015-11-16 21:47:2921 return std::string(internal_name) + testing::kMultiSeparator +
22 base::IntToString(index);
23}
24
jkrcal1383d1d2016-06-17 12:40:5625base::string16 FeatureEntry::DescriptionForOption(int index) const {
sdefresne246c5642015-11-16 21:47:2926 DCHECK(type == FeatureEntry::MULTI_VALUE ||
27 type == FeatureEntry::ENABLE_DISABLE_VALUE ||
jkrcal1383d1d2016-06-17 12:40:5628 type == FeatureEntry::FEATURE_VALUE ||
29 type == FeatureEntry::FEATURE_WITH_VARIATIONS_VALUE);
30 DCHECK_LT(index, num_options);
sdefresne246c5642015-11-16 21:47:2931 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];
jkrcal1383d1d2016-06-17 12:40:5640 } 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) {
44 // Variation 1: the default enabled variation => "Enabled".
45 description_id = IDS_GENERIC_EXPERIMENT_CHOICE_ENABLED;
46 } else if (index < num_options - 1) {
47 // Variations 2 .. n => "Enabled <description_text>".
48 int variation_index = index - 1;
49 return l10n_util::GetStringUTF16(IDS_GENERIC_EXPERIMENT_CHOICE_ENABLED) +
50 base::ASCIIToUTF16(" ") +
51 base::ASCIIToUTF16(
52 feature_variations[variation_index].description_text);
53 } else {
54 DCHECK_EQ(num_options - 1, index);
55 description_id = IDS_GENERIC_EXPERIMENT_CHOICE_DISABLED;
56 }
sdefresne246c5642015-11-16 21:47:2957 } else {
58 description_id = choices[index].description_id;
59 }
60 return l10n_util::GetStringUTF16(description_id);
61}
62
jkrcal1383d1d2016-06-17 12:40:5663const FeatureEntry::Choice& FeatureEntry::ChoiceForOption(int index) const {
64 DCHECK_EQ(FeatureEntry::MULTI_VALUE, type);
65 DCHECK_LT(index, num_options);
66
67 return choices[index];
68}
69
70FeatureEntry::FeatureState FeatureEntry::StateForOption(int index) const {
71 DCHECK(type == FeatureEntry::FEATURE_VALUE ||
72 type == FeatureEntry::FEATURE_WITH_VARIATIONS_VALUE);
73 DCHECK_LT(index, num_options);
74
75 if (index == 0)
76 return FeatureEntry::FeatureState::DEFAULT;
77 else if (index == num_options - 1)
78 return FeatureEntry::FeatureState::DISABLED;
79 else
80 return FeatureEntry::FeatureState::ENABLED;
81}
82
83const FeatureEntry::FeatureVariation* FeatureEntry::VariationForOption(
84 int index) const {
85 DCHECK(type == FeatureEntry::FEATURE_VALUE ||
86 type == FeatureEntry::FEATURE_WITH_VARIATIONS_VALUE);
87 DCHECK_LT(index, num_options);
88
89 if (type == FeatureEntry::FEATURE_WITH_VARIATIONS_VALUE && index > 0 &&
90 index < num_options - 1) {
91 // We have no variations for FEATURE_VALUE type. Option at |index|
92 // corresponds to variation at |index| - 1 as the first option is "Default".
93 return &feature_variations[index - 1];
94 }
95 return nullptr;
96}
97
sdefresne246c5642015-11-16 21:47:2998namespace 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.
102const char kMultiSeparator[] = "@";
103
104} // namespace testing
105
106} // namespace flags_ui