blob: bfda221b4038ee299fcadf456ac5df5d39d70bbb [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;
Lei Zhang6779c822017-11-28 01:00:1880 if (index == num_options - 1)
jkrcal1383d1d2016-06-17 12:40:5681 return FeatureEntry::FeatureState::DISABLED;
Lei Zhang6779c822017-11-28 01:00:1882 return FeatureEntry::FeatureState::ENABLED;
jkrcal1383d1d2016-06-17 12:40:5683}
84
85const FeatureEntry::FeatureVariation* FeatureEntry::VariationForOption(
86 int index) const {
87 DCHECK(type == FeatureEntry::FEATURE_VALUE ||
jkrcal531f36752017-03-22 13:44:2588 type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE);
jkrcal1383d1d2016-06-17 12:40:5689 DCHECK_LT(index, num_options);
90
jkrcal531f36752017-03-22 13:44:2591 if (type == FeatureEntry::FEATURE_WITH_PARAMS_VALUE && index > 1 &&
jkrcal1383d1d2016-06-17 12:40:5692 index < num_options - 1) {
93 // We have no variations for FEATURE_VALUE type. Option at |index|
jkrcal05f7ce32016-07-13 16:42:0994 // corresponds to variation at |index| - 2 as the list starts with "Default"
95 // and "Enabled" (with default parameters).
96 return &feature_variations[index - 2];
jkrcal1383d1d2016-06-17 12:40:5697 }
98 return nullptr;
99}
100
sdefresne246c5642015-11-16 21:47:29101namespace 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.
105const char kMultiSeparator[] = "@";
106
107} // namespace testing
108
109} // namespace flags_ui