blob: d51dd48c380ddd0a9e239c417c925be827318c96 [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 "ui/base/l10n/l10n_util.h"
11
12namespace flags_ui {
13
vabr0215a8e2017-03-28 12:47:3414const char kGenericExperimentChoiceDefault[] = "Default";
15const char kGenericExperimentChoiceEnabled[] = "Enabled";
16const char kGenericExperimentChoiceDisabled[] = "Disabled";
17const char kGenericExperimentChoiceAutomatic[] = "Automatic";
18
jkrcal1383d1d2016-06-17 12:40:5619std::string FeatureEntry::NameForOption(int index) const {
sdefresne246c5642015-11-16 21:47:2920 DCHECK(type == FeatureEntry::MULTI_VALUE ||
21 type == FeatureEntry::ENABLE_DISABLE_VALUE ||
jkrcal1383d1d2016-06-17 12:40:5622 type == FeatureEntry::FEATURE_VALUE ||
jkrcal531f36752017-03-22 13:44:2523 type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE);
jkrcal1383d1d2016-06-17 12:40:5624 DCHECK_LT(index, num_options);
sdefresne246c5642015-11-16 21:47:2925 return std::string(internal_name) + testing::kMultiSeparator +
26 base::IntToString(index);
27}
28
jkrcal1383d1d2016-06-17 12:40:5629base::string16 FeatureEntry::DescriptionForOption(int index) const {
sdefresne246c5642015-11-16 21:47:2930 DCHECK(type == FeatureEntry::MULTI_VALUE ||
31 type == FeatureEntry::ENABLE_DISABLE_VALUE ||
jkrcal1383d1d2016-06-17 12:40:5632 type == FeatureEntry::FEATURE_VALUE ||
jkrcal531f36752017-03-22 13:44:2533 type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE);
jkrcal1383d1d2016-06-17 12:40:5634 DCHECK_LT(index, num_options);
vabr0215a8e2017-03-28 12:47:3435 const char* description = nullptr;
sdefresne246c5642015-11-16 21:47:2936 if (type == FeatureEntry::ENABLE_DISABLE_VALUE ||
37 type == FeatureEntry::FEATURE_VALUE) {
vabr0215a8e2017-03-28 12:47:3438 const char* kEnableDisableDescriptions[] = {
39 kGenericExperimentChoiceDefault, kGenericExperimentChoiceEnabled,
40 kGenericExperimentChoiceDisabled,
sdefresne246c5642015-11-16 21:47:2941 };
vabr0215a8e2017-03-28 12:47:3442 description = kEnableDisableDescriptions[index];
jkrcal531f36752017-03-22 13:44:2543 } else if (type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE) {
jkrcal1383d1d2016-06-17 12:40:5644 if (index == 0) {
vabr0215a8e2017-03-28 12:47:3445 description = kGenericExperimentChoiceDefault;
jkrcal1383d1d2016-06-17 12:40:5646 } else if (index == 1) {
vabr0215a8e2017-03-28 12:47:3447 description = kGenericExperimentChoiceEnabled;
jkrcal1383d1d2016-06-17 12:40:5648 } else if (index < num_options - 1) {
jkrcal05f7ce32016-07-13 16:42:0949 // First two options do not have variations params.
50 int variation_index = index - 2;
vabr0215a8e2017-03-28 12:47:3451 return base::ASCIIToUTF16(
52 base::StringPiece(kGenericExperimentChoiceEnabled)) +
jkrcal1383d1d2016-06-17 12:40:5653 base::ASCIIToUTF16(" ") +
54 base::ASCIIToUTF16(
55 feature_variations[variation_index].description_text);
56 } else {
57 DCHECK_EQ(num_options - 1, index);
vabr0215a8e2017-03-28 12:47:3458 description = kGenericExperimentChoiceDisabled;
jkrcal1383d1d2016-06-17 12:40:5659 }
sdefresne246c5642015-11-16 21:47:2960 } else {
vabr0215a8e2017-03-28 12:47:3461 description = choices[index].description;
sdefresne246c5642015-11-16 21:47:2962 }
vabr0215a8e2017-03-28 12:47:3463 return base::ASCIIToUTF16(base::StringPiece(description));
sdefresne246c5642015-11-16 21:47:2964}
65
jkrcal1383d1d2016-06-17 12:40:5666const 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
73FeatureEntry::FeatureState FeatureEntry::StateForOption(int index) const {
74 DCHECK(type == FeatureEntry::FEATURE_VALUE ||
jkrcal531f36752017-03-22 13:44:2575 type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE);
jkrcal1383d1d2016-06-17 12:40:5676 DCHECK_LT(index, num_options);
77
78 if (index == 0)
79 return FeatureEntry::FeatureState::DEFAULT;
80 else if (index == num_options - 1)
81 return FeatureEntry::FeatureState::DISABLED;
82 else
83 return FeatureEntry::FeatureState::ENABLED;
84}
85
86const FeatureEntry::FeatureVariation* FeatureEntry::VariationForOption(
87 int index) const {
88 DCHECK(type == FeatureEntry::FEATURE_VALUE ||
jkrcal531f36752017-03-22 13:44:2589 type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE);
jkrcal1383d1d2016-06-17 12:40:5690 DCHECK_LT(index, num_options);
91
jkrcal531f36752017-03-22 13:44:2592 if (type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE && index > 1 &&
jkrcal1383d1d2016-06-17 12:40:5693 index < num_options - 1) {
94 // We have no variations for FEATURE_VALUE type. Option at |index|
jkrcal05f7ce32016-07-13 16:42:0995 // corresponds to variation at |index| - 2 as the list starts with "Default"
96 // and "Enabled" (with default parameters).
97 return &feature_variations[index - 2];
jkrcal1383d1d2016-06-17 12:40:5698 }
99 return nullptr;
100}
101
sdefresne246c5642015-11-16 21:47:29102namespace testing {
103
104// WARNING: '@' is also used in the html file. If you update this constant you
105// also need to update the html file.
106const char kMultiSeparator[] = "@";
107
108} // namespace testing
109
110} // namespace flags_ui