1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/android/contextualsearch/contextual_search_field_trial.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "testing/gtest/include/gtest/gtest.h"
// Tests ContextualSearchFieldTrail class.
class ContextualSearchFieldTrailTest : public testing::Test {
public:
ContextualSearchFieldTrailTest() {}
~ContextualSearchFieldTrailTest() override {}
// Inner class that stubs out access to Variations and command-line switches.
class ContextualSearchFieldTrailStubbed : public ContextualSearchFieldTrial {
public:
// Use these to set a non-empty value to override return of a Get method.
void SetSwitchValue(const std::string& value);
void SetParamValue(const std::string& value);
protected:
// These are overridden to return the Set value above.
bool HasSwitch(const std::string& name) override;
std::string GetSwitch(const std::string& name) override;
std::string GetParam(const std::string& name) override;
private:
bool does_have_switch_;
std::string switch_value_;
std::string param_value_;
};
// The class under test.
scoped_ptr<ContextualSearchFieldTrailStubbed> field_trial_;
protected:
void SetUp() override {
field_trial_.reset(new ContextualSearchFieldTrailStubbed());
}
void TearDown() override {}
private:
DISALLOW_COPY_AND_ASSIGN(ContextualSearchFieldTrailTest);
};
bool ContextualSearchFieldTrailTest::ContextualSearchFieldTrailStubbed::
HasSwitch(const std::string& name) {
return does_have_switch_;
}
void ContextualSearchFieldTrailTest::ContextualSearchFieldTrailStubbed::
SetSwitchValue(const std::string& value) {
switch_value_ = value;
does_have_switch_ = true;
}
std::string
ContextualSearchFieldTrailTest::ContextualSearchFieldTrailStubbed::GetSwitch(
const std::string& name) {
return switch_value_;
}
void ContextualSearchFieldTrailTest::ContextualSearchFieldTrailStubbed::
SetParamValue(const std::string& value) {
param_value_ = value;
}
std::string
ContextualSearchFieldTrailTest::ContextualSearchFieldTrailStubbed::GetParam(
const std::string& name) {
return param_value_;
}
TEST_F(ContextualSearchFieldTrailTest, IntegerDefaultValue) {
// Should return this default value.
EXPECT_EQ(
ContextualSearchFieldTrial::kContextualSearchDefaultIcingSurroundingSize,
field_trial_->GetIcingSurroundingSize());
}
TEST_F(ContextualSearchFieldTrailTest, IntegerParamOverrides) {
// Params override defaults.
field_trial_->SetParamValue("500");
EXPECT_EQ(500, field_trial_->GetIcingSurroundingSize());
}
TEST_F(ContextualSearchFieldTrailTest, IntegerSwitchOverrides) {
field_trial_->SetParamValue("500");
// Switches override params.
field_trial_->SetSwitchValue("200");
EXPECT_EQ(200, field_trial_->GetIcingSurroundingSize());
}
TEST_F(ContextualSearchFieldTrailTest, IntegerJunkIgnored) {
// A junk value effectively resets the switch.
field_trial_->SetSwitchValue("foo");
EXPECT_EQ(
ContextualSearchFieldTrial::kContextualSearchDefaultIcingSurroundingSize,
field_trial_->GetIcingSurroundingSize());
}
TEST_F(ContextualSearchFieldTrailTest, BooleanDefaultValue) {
// Should return this default value.
EXPECT_FALSE(field_trial_->IsSendBasePageURLDisabled());
}
TEST_F(ContextualSearchFieldTrailTest, BooleanParamOverrides) {
// Params override defaults.
field_trial_->SetParamValue("any");
EXPECT_TRUE(field_trial_->IsSendBasePageURLDisabled());
}
TEST_F(ContextualSearchFieldTrailTest, BooleanFalseParam) {
field_trial_->SetParamValue("false");
EXPECT_FALSE(field_trial_->IsSendBasePageURLDisabled());
}
TEST_F(ContextualSearchFieldTrailTest, BooleanSwitchOverrides) {
field_trial_->SetParamValue("false");
// Switches override params.
field_trial_->SetSwitchValue("any");
EXPECT_TRUE(field_trial_->IsSendBasePageURLDisabled());
}
TEST_F(ContextualSearchFieldTrailTest, BooleanEmptySwitch) {
// An empty switch that's present should return true;
field_trial_->SetSwitchValue("");
EXPECT_TRUE(field_trial_->IsSendBasePageURLDisabled());
}
TEST_F(ContextualSearchFieldTrailTest, StringDefaultEmpty) {
// Default should return an empty string.
EXPECT_TRUE(field_trial_->GetResolverURLPrefix().empty());
}
TEST_F(ContextualSearchFieldTrailTest, StringParamOverrides) {
// Params override.
field_trial_->SetParamValue("param");
EXPECT_EQ("param", field_trial_->GetResolverURLPrefix());
}
TEST_F(ContextualSearchFieldTrailTest, StringSwitchOverrides) {
field_trial_->SetParamValue("param");
// Switches override params.
field_trial_->SetSwitchValue("switch");
EXPECT_EQ("switch", field_trial_->GetResolverURLPrefix());
}
|