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 #if !defined(OS_IOS) && !defined(OS_ANDROID)
134 typedef EmbeddedSearchFieldTrialTest SearchTest
;
136 TEST_F(SearchTest
, ShouldPrefetchSearchResults_InstantExtendedAPIEnabled
) {
137 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
139 EXPECT_EQ(2ul, EmbeddedSearchPageVersion());
140 EXPECT_TRUE(ShouldPrefetchSearchResults());
143 TEST_F(SearchTest
, ShouldPrefetchSearchResults_Default
) {
144 EXPECT_TRUE(ShouldPrefetchSearchResults());
147 TEST_F(SearchTest
, ShouldReuseInstantSearchBasePage_Default
) {
148 EXPECT_TRUE(ShouldReuseInstantSearchBasePage());
151 TEST_F(SearchTest
, ShouldAllowPrefetchNonDefaultMatch_DisabledViaFieldTrial
) {
152 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
153 "EmbeddedSearch", "Group1 espv:89 allow_prefetch_non_default_match:0"));
154 EXPECT_FALSE(ShouldAllowPrefetchNonDefaultMatch());
155 EXPECT_EQ(89ul, EmbeddedSearchPageVersion());
158 TEST_F(SearchTest
, ShouldAllowPrefetchNonDefaultMatch_EnabledViaFieldTrial
) {
159 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
160 "EmbeddedSearch", "Group1 espv:80 allow_prefetch_non_default_match:1"));
161 EXPECT_TRUE(ShouldAllowPrefetchNonDefaultMatch());
162 EXPECT_EQ(80ul, EmbeddedSearchPageVersion());
165 TEST_F(SearchTest
, ForceInstantResultsParam
) {
166 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
168 EXPECT_TRUE(IsInstantExtendedAPIEnabled());
169 EXPECT_EQ("ion=1&", ForceInstantResultsParam(true));
170 EXPECT_EQ(std::string(), ForceInstantResultsParam(false));
173 typedef EmbeddedSearchFieldTrialTest InstantExtendedEnabledParamTest
;
175 TEST_F(InstantExtendedEnabledParamTest
, QueryExtractionDisabled
) {
176 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
178 // Make sure InstantExtendedEnabledParam() returns an empty string for search
180 EXPECT_FALSE(IsQueryExtractionEnabled());
181 EXPECT_EQ("", InstantExtendedEnabledParam(true));
182 EXPECT_EQ("espv=12&", InstantExtendedEnabledParam(false));
185 TEST_F(InstantExtendedEnabledParamTest
, QueryExtractionEnabled
) {
186 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
187 "EmbeddedSearch", "Group1 espv:10 query_extraction:1"));
188 EXPECT_TRUE(IsQueryExtractionEnabled());
189 // Make sure InstantExtendedEnabledParam() returns a non-empty param string
190 // for search requests.
191 EXPECT_EQ("espv=10&", InstantExtendedEnabledParam(true));
192 EXPECT_EQ("espv=10&", InstantExtendedEnabledParam(false));
195 TEST_F(InstantExtendedEnabledParamTest
, UseDefaultEmbeddedSearchPageVersion
) {
196 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
197 "EmbeddedSearch", "Group1 espv:-1 query_extraction:1"));
198 EXPECT_TRUE(IsQueryExtractionEnabled());
199 EXPECT_EQ("espv=2&", InstantExtendedEnabledParam(true));
200 EXPECT_EQ("espv=2&", InstantExtendedEnabledParam(false));
203 typedef EmbeddedSearchFieldTrialTest IsQueryExtractionEnabledTest
;
205 TEST_F(IsQueryExtractionEnabledTest
, NotSet
) {
206 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial("EmbeddedSearch",
208 EXPECT_TRUE(IsInstantExtendedAPIEnabled());
209 EXPECT_FALSE(IsQueryExtractionEnabled());
210 EXPECT_EQ(2ul, EmbeddedSearchPageVersion());
213 TEST_F(IsQueryExtractionEnabledTest
, EnabledViaFieldTrial
) {
214 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
215 "EmbeddedSearch", "Group1 espv:2 query_extraction:1"));
216 EXPECT_TRUE(IsInstantExtendedAPIEnabled());
217 EXPECT_TRUE(IsQueryExtractionEnabled());
218 EXPECT_EQ(2ul, EmbeddedSearchPageVersion());
221 TEST_F(IsQueryExtractionEnabledTest
, DisabledViaFieldTrial
) {
222 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
223 "EmbeddedSearch", "Group1 espv:2 query_extraction:0"));
224 EXPECT_TRUE(IsInstantExtendedAPIEnabled());
225 EXPECT_FALSE(IsQueryExtractionEnabled());
226 EXPECT_EQ(2ul, EmbeddedSearchPageVersion());
229 TEST_F(IsQueryExtractionEnabledTest
, EnabledViaCommandLine
) {
230 EnableQueryExtractionForTesting();
231 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
232 "EmbeddedSearch", "Group1 espv:2 query_extraction:0"));
233 EXPECT_TRUE(IsInstantExtendedAPIEnabled());
234 EXPECT_TRUE(IsQueryExtractionEnabled());
235 EXPECT_EQ(2ul, EmbeddedSearchPageVersion());
237 #endif // !defined(OS_IOS) && !defined(OS_ANDROID)
239 } // namespace search