1 // Copyright (c) 2012 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 "base/values.h"
6 #include "chrome/browser/extensions/api/omnibox/omnibox_api.h"
7 #include "chrome/common/extensions/api/omnibox.h"
8 #include "extensions/common/value_builder.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "testing/platform_test.h"
12 namespace extensions
{
14 namespace omnibox
= api::omnibox
;
15 namespace SendSuggestions
= omnibox::SendSuggestions
;
16 namespace SetDefaultSuggestion
= omnibox::SetDefaultSuggestion
;
20 const int kNone
= ACMatchClassification::NONE
;
21 const int kUrl
= ACMatchClassification::URL
;
22 const int kMatch
= ACMatchClassification::MATCH
;
23 const int kDim
= ACMatchClassification::DIM
;
25 void CompareClassification(const ACMatchClassifications
& expected
,
26 const ACMatchClassifications
& actual
) {
27 EXPECT_EQ(expected
.size(), actual
.size());
28 for (size_t i
= 0; i
< expected
.size() && i
< actual
.size(); ++i
) {
29 EXPECT_EQ(expected
[i
].offset
, actual
[i
].offset
) << "Index:" << i
;
30 EXPECT_EQ(expected
[i
].style
, actual
[i
].style
) << "Index:" << i
;
36 // Test output key: n = character with no styling, d = dim, m = match, u = url
37 // u = 1, m = 2, d = 4. u+d = 5, etc.
43 TEST(ExtensionOmniboxTest
, DescriptionStylesSimple
) {
44 scoped_ptr
<base::ListValue
> list
= ListBuilder()
47 .Append(DictionaryBuilder()
48 .Set("content", "content")
49 .Set("description", "description")
50 .Set("descriptionStyles", ListBuilder()
51 .Append(DictionaryBuilder()
55 .Append(DictionaryBuilder()
58 .Set("length", 3))))).Build();
60 ACMatchClassifications styles_expected
;
61 styles_expected
.push_back(ACMatchClassification(0, kNone
));
62 styles_expected
.push_back(ACMatchClassification(1, kMatch
));
63 styles_expected
.push_back(ACMatchClassification(5, kNone
));
64 styles_expected
.push_back(ACMatchClassification(6, kDim
));
65 styles_expected
.push_back(ACMatchClassification(9, kNone
));
67 scoped_ptr
<SendSuggestions::Params
> params(
68 SendSuggestions::Params::Create(*list
));
70 EXPECT_TRUE(params
->suggest_results
[0].get());
71 CompareClassification(styles_expected
, StyleTypesToACMatchClassifications(
72 *params
->suggest_results
[0]));
74 // Same input, but swap the order. Ensure it still works.
75 scoped_ptr
<base::ListValue
> swap_list
= ListBuilder()
78 .Append(DictionaryBuilder()
79 .Set("content", "content")
80 .Set("description", "description")
81 .Set("descriptionStyles", ListBuilder()
82 .Append(DictionaryBuilder()
86 .Append(DictionaryBuilder()
89 .Set("length", 4))))).Build();
91 scoped_ptr
<SendSuggestions::Params
> swapped_params(
92 SendSuggestions::Params::Create(*swap_list
));
93 EXPECT_TRUE(swapped_params
);
94 EXPECT_TRUE(swapped_params
->suggest_results
[0].get());
95 CompareClassification(styles_expected
, StyleTypesToACMatchClassifications(
96 *swapped_params
->suggest_results
[0]));
106 TEST(ExtensionOmniboxTest
, DescriptionStylesCombine
) {
107 scoped_ptr
<base::ListValue
> list
= ListBuilder()
109 .Append(ListBuilder()
110 .Append(DictionaryBuilder()
111 .Set("content", "content")
112 .Set("description", "description")
113 .Set("descriptionStyles", ListBuilder()
114 .Append(DictionaryBuilder()
118 .Append(DictionaryBuilder()
122 .Append(DictionaryBuilder()
123 .Set("type", "match")
126 .Append(DictionaryBuilder()
127 .Set("type", "match")
130 .Append(DictionaryBuilder()
133 .Set("length", 2))))).Build();
135 ACMatchClassifications styles_expected
;
136 styles_expected
.push_back(ACMatchClassification(0, kUrl
| kMatch
));
137 styles_expected
.push_back(ACMatchClassification(1, kUrl
| kMatch
| kDim
));
138 styles_expected
.push_back(ACMatchClassification(3, kUrl
| kMatch
));
139 styles_expected
.push_back(ACMatchClassification(4, kUrl
));
140 styles_expected
.push_back(ACMatchClassification(5, kNone
));
141 styles_expected
.push_back(ACMatchClassification(9, kMatch
| kDim
));
143 scoped_ptr
<SendSuggestions::Params
> params(
144 SendSuggestions::Params::Create(*list
));
146 EXPECT_TRUE(params
->suggest_results
[0].get());
147 CompareClassification(styles_expected
, StyleTypesToACMatchClassifications(
148 *params
->suggest_results
[0]));
150 // Try moving the "dim/match" style pair at offset 9. Output should be the
152 scoped_ptr
<base::ListValue
> moved_list
= ListBuilder()
154 .Append(ListBuilder()
155 .Append(DictionaryBuilder()
156 .Set("content", "content")
157 .Set("description", "description")
158 .Set("descriptionStyles", ListBuilder()
159 .Append(DictionaryBuilder()
163 .Append(DictionaryBuilder()
164 .Set("type", "match")
167 .Append(DictionaryBuilder()
171 .Append(DictionaryBuilder()
172 .Set("type", "match")
175 .Append(DictionaryBuilder()
178 .Set("length", 2))))).Build();
180 scoped_ptr
<SendSuggestions::Params
> moved_params(
181 SendSuggestions::Params::Create(*moved_list
));
182 EXPECT_TRUE(moved_params
);
183 EXPECT_TRUE(moved_params
->suggest_results
[0].get());
184 CompareClassification(styles_expected
, StyleTypesToACMatchClassifications(
185 *moved_params
->suggest_results
[0]));
195 TEST(ExtensionOmniboxTest
, DescriptionStylesCombine2
) {
196 scoped_ptr
<base::ListValue
> list
= ListBuilder()
198 .Append(ListBuilder()
199 .Append(DictionaryBuilder()
200 .Set("content", "content")
201 .Set("description", "description")
202 .Set("descriptionStyles", ListBuilder()
203 .Append(DictionaryBuilder()
207 .Append(DictionaryBuilder()
208 .Set("type", "match")
211 .Append(DictionaryBuilder()
212 .Set("type", "match")
215 .Append(DictionaryBuilder()
219 .Append(DictionaryBuilder()
222 .Set("length", 3))))).Build();
224 ACMatchClassifications styles_expected
;
225 styles_expected
.push_back(ACMatchClassification(0, kUrl
| kMatch
| kDim
));
226 styles_expected
.push_back(ACMatchClassification(5, kNone
));
228 scoped_ptr
<SendSuggestions::Params
> params(
229 SendSuggestions::Params::Create(*list
));
231 EXPECT_TRUE(params
->suggest_results
[0].get());
232 CompareClassification(styles_expected
, StyleTypesToACMatchClassifications(
233 *params
->suggest_results
[0]));
243 TEST(ExtensionOmniboxTest
, DefaultSuggestResult
) {
244 // Default suggestions should not have a content parameter.
245 scoped_ptr
<base::ListValue
> list
= ListBuilder()
246 .Append(DictionaryBuilder()
247 .Set("description", "description")
248 .Set("descriptionStyles", ListBuilder()
249 .Append(DictionaryBuilder()
253 .Append(DictionaryBuilder()
254 .Set("type", "match")
257 .Append(DictionaryBuilder()
258 .Set("type", "match")
261 .Append(DictionaryBuilder()
265 .Append(DictionaryBuilder()
268 .Set("length", 3)))).Build();
270 scoped_ptr
<SetDefaultSuggestion::Params
> params(
271 SetDefaultSuggestion::Params::Create(*list
));
275 } // namespace extensions