1 // Copyright 2014 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.
5 #include "components/search/search.h"
7 #include "base/metrics/field_trial.h"
8 #include "base/metrics/statistics_recorder.h"
9 #include "components/variations/entropy_provider.h"
10 #include "testing/gtest/include/gtest/gtest.h"
14 class EmbeddedSearchFieldTrialTest
: public testing::Test
{
16 void SetUp() override
{
17 field_trial_list_
.reset(new base::FieldTrialList(
18 new metrics::SHA1EntropyProvider("42")));
19 base::StatisticsRecorder::Initialize();
23 scoped_ptr
<base::FieldTrialList
> field_trial_list_
;
26 TEST_F(EmbeddedSearchFieldTrialTest
, GetFieldTrialInfoEmptyAndValid
) {
27 FieldTrialFlags flags
;
29 EXPECT_TRUE(GetFieldTrialInfo(&flags
));
30 EXPECT_EQ(0ul, flags
.size());
32 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
34 EXPECT_TRUE(GetFieldTrialInfo(&flags
));
35 EXPECT_EQ(0ul, flags
.size());
38 TEST_F(EmbeddedSearchFieldTrialTest
, GetFieldTrialInfoInvalidNumber
) {
39 FieldTrialFlags flags
;
41 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
43 EXPECT_TRUE(GetFieldTrialInfo(&flags
));
44 EXPECT_EQ(0ul, flags
.size());
47 TEST_F(EmbeddedSearchFieldTrialTest
, GetFieldTrialInfoInvalidName
) {
48 FieldTrialFlags flags
;
50 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
52 EXPECT_TRUE(GetFieldTrialInfo(&flags
));
53 EXPECT_EQ(0ul, flags
.size());
56 TEST_F(EmbeddedSearchFieldTrialTest
, GetFieldTrialInfoValidGroup
) {
57 FieldTrialFlags flags
;
59 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
61 EXPECT_TRUE(GetFieldTrialInfo(&flags
));
62 EXPECT_EQ(0ul, flags
.size());
65 TEST_F(EmbeddedSearchFieldTrialTest
, GetFieldTrialInfoValidFlag
) {
66 FieldTrialFlags flags
;
68 EXPECT_EQ(9999ul, GetUInt64ValueForFlagWithDefault("foo", 9999, flags
));
69 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
71 EXPECT_TRUE(GetFieldTrialInfo(&flags
));
72 EXPECT_EQ(1ul, flags
.size());
73 EXPECT_EQ(6ul, GetUInt64ValueForFlagWithDefault("foo", 9999, flags
));
76 TEST_F(EmbeddedSearchFieldTrialTest
, GetFieldTrialInfoNewName
) {
77 FieldTrialFlags flags
;
79 EXPECT_EQ(9999ul, GetUInt64ValueForFlagWithDefault("foo", 9999, flags
));
80 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
82 EXPECT_TRUE(GetFieldTrialInfo(&flags
));
83 EXPECT_EQ(1ul, flags
.size());
84 EXPECT_EQ(6ul, GetUInt64ValueForFlagWithDefault("foo", 9999, flags
));
87 TEST_F(EmbeddedSearchFieldTrialTest
, GetFieldTrialInfoNewNameOverridesOld
) {
88 FieldTrialFlags flags
;
90 EXPECT_EQ(9999ul, GetUInt64ValueForFlagWithDefault("foo", 9999, flags
));
91 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
93 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("InstantExtended",
95 EXPECT_TRUE(GetFieldTrialInfo(&flags
));
96 EXPECT_EQ(1ul, flags
.size());
97 EXPECT_EQ(6ul, GetUInt64ValueForFlagWithDefault("foo", 9999, flags
));
100 TEST_F(EmbeddedSearchFieldTrialTest
, GetFieldTrialInfoLotsOfFlags
) {
101 FieldTrialFlags flags
;
103 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
104 "EmbeddedSearch", "Group77 bar:1 baz:7 cat:dogs"));
105 EXPECT_TRUE(GetFieldTrialInfo(&flags
));
106 EXPECT_EQ(3ul, flags
.size());
107 EXPECT_EQ(true, GetBoolValueForFlagWithDefault("bar", false, flags
));
108 EXPECT_EQ(7ul, GetUInt64ValueForFlagWithDefault("baz", 0, flags
));
110 GetStringValueForFlagWithDefault("cat", std::string(), flags
));
112 GetStringValueForFlagWithDefault("moose", "default", flags
));
115 TEST_F(EmbeddedSearchFieldTrialTest
, GetFieldTrialInfoDisabled
) {
116 FieldTrialFlags flags
;
118 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
119 "EmbeddedSearch", "Group77 bar:1 baz:7 cat:dogs DISABLED"));
120 EXPECT_FALSE(GetFieldTrialInfo(&flags
));
121 EXPECT_EQ(0ul, flags
.size());
124 TEST_F(EmbeddedSearchFieldTrialTest
, GetFieldTrialInfoControlFlags
) {
125 FieldTrialFlags flags
;
127 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
128 "EmbeddedSearch", "Control77 bar:1 baz:7 cat:dogs"));
129 EXPECT_TRUE(GetFieldTrialInfo(&flags
));
130 EXPECT_EQ(3ul, flags
.size());
133 typedef EmbeddedSearchFieldTrialTest ShouldHideTopVerbatimTest
;
135 TEST_F(ShouldHideTopVerbatimTest
, DoNotHideByDefault
) {
136 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
138 EXPECT_FALSE(ShouldHideTopVerbatimMatch());
141 TEST_F(ShouldHideTopVerbatimTest
, DoNotHideInInstantExtended
) {
142 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
144 EXPECT_FALSE(ShouldHideTopVerbatimMatch());
147 TEST_F(ShouldHideTopVerbatimTest
, EnableByFlagInInstantExtended
) {
148 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
149 "Group1 hide_verbatim:1"));
150 EXPECT_TRUE(ShouldHideTopVerbatimMatch());
153 TEST_F(ShouldHideTopVerbatimTest
, EnableByFlagOutsideInstantExtended
) {
154 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
155 "EmbeddedSearch", "Controll1 hide_verbatim:1"));
156 EXPECT_TRUE(ShouldHideTopVerbatimMatch());
159 TEST_F(ShouldHideTopVerbatimTest
, DisableByFlag
) {
160 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
161 "Group1 hide_verbatim:0"));
162 EXPECT_FALSE(ShouldHideTopVerbatimMatch());
165 } // namespace chrome