Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Side by Side Diff: chrome/browser/extensions/api/omnibox/omnibox_unittest.cc

Issue 12314164: Modified Omnibox extension api to use JSON Schema Compiler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: kalman's requests Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/values.h" 5 #include "base/values.h"
6 #include "chrome/browser/extensions/api/omnibox/omnibox_api.h" 6 #include "chrome/browser/extensions/api/omnibox/omnibox_api.h"
7 #include "chrome/common/extensions/api/omnibox.h"
8 #include "chrome/common/extensions/value_builder.h"
7 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
8 #include "testing/platform_test.h" 10 #include "testing/platform_test.h"
9 11
10 namespace extensions { 12 namespace extensions {
11 13
14 namespace omnibox = api::omnibox;
15 namespace SendSuggestions = omnibox::SendSuggestions;
16
12 namespace { 17 namespace {
13 18
14 const int kNone = ACMatchClassification::NONE; 19 const int kNone = ACMatchClassification::NONE;
15 const int kUrl = ACMatchClassification::URL; 20 const int kUrl = ACMatchClassification::URL;
16 const int kMatch = ACMatchClassification::MATCH; 21 const int kMatch = ACMatchClassification::MATCH;
17 const int kDim = ACMatchClassification::DIM; 22 const int kDim = ACMatchClassification::DIM;
18 23
19 void AppendStyle(const std::string& type,
20 int offset, int length,
21 ListValue* styles) {
22 DictionaryValue* style = new DictionaryValue;
23 style->SetString("type", type);
24 style->SetInteger("offset", offset);
25 style->SetInteger("length", length);
26 styles->Append(style);
27 }
28
29 void CompareClassification(const ACMatchClassifications& expected, 24 void CompareClassification(const ACMatchClassifications& expected,
30 const ACMatchClassifications& actual) { 25 const ACMatchClassifications& actual) {
31 EXPECT_EQ(expected.size(), actual.size()); 26 EXPECT_EQ(expected.size(), actual.size());
32 for (size_t i = 0; i < expected.size() && i < actual.size(); ++i) { 27 for (size_t i = 0; i < expected.size() && i < actual.size(); ++i) {
33 EXPECT_EQ(expected[i].offset, actual[i].offset) << "Index:" << i; 28 EXPECT_EQ(expected[i].offset, actual[i].offset) << "Index:" << i;
34 EXPECT_EQ(expected[i].style, actual[i].style) << "Index:" << i; 29 EXPECT_EQ(expected[i].style, actual[i].style) << "Index:" << i;
35 } 30 }
36 } 31 }
37 32
38 } // namespace 33 } // namespace
39 34
40 // Test output key: n = character with no styling, d = dim, m = match, u = url 35 // Test output key: n = character with no styling, d = dim, m = match, u = url
41 // u = 1, m = 2, d = 4. u+d = 5, etc. 36 // u = 1, m = 2, d = 4. u+d = 5, etc.
42 37
43 // 0123456789 38 // 0123456789
44 // mmmm 39 // mmmm
45 // + ddd 40 // + ddd
46 // = nmmmmndddn 41 // = nmmmmndddn
47 TEST(ExtensionOmniboxTest, DescriptionStylesSimple) { 42 TEST(ExtensionOmniboxTest, DescriptionStylesSimple) {
48 ListValue styles_value; 43 ListValue* list = ListBuilder().Append(42).Append(ListBuilder().Append(
not at google - send to devlin 2013/03/22 17:33:30 looks like a memory leak? you're releasing this (a
Aaron Jacobs 2013/03/22 19:54:15 Done.
49 AppendStyle("match", 1, 4, &styles_value); 44 DictionaryBuilder().Set("content", "content")
50 AppendStyle("dim", 6, 3, &styles_value); 45 .Set("description", "description")
46 .Set("descriptionStyles", ListBuilder()
47 .Append(DictionaryBuilder()
48 .Set("type", "match")
49 .Set("offset", 1)
50 .Set("length", 4))
51 .Append(DictionaryBuilder()
52 .Set("type", "dim")
53 .Set("offset", 6)
54 .Set("length", 3))))).Build().release();
not at google - send to devlin 2013/03/22 17:33:30 using some indentation here would be nice to make
Aaron Jacobs 2013/03/22 19:54:15 Done.
51 55
52 ACMatchClassifications styles_expected; 56 ACMatchClassifications styles_expected;
53 styles_expected.push_back(ACMatchClassification(0, kNone)); 57 styles_expected.push_back(ACMatchClassification(0, kNone));
54 styles_expected.push_back(ACMatchClassification(1, kMatch)); 58 styles_expected.push_back(ACMatchClassification(1, kMatch));
55 styles_expected.push_back(ACMatchClassification(5, kNone)); 59 styles_expected.push_back(ACMatchClassification(5, kNone));
56 styles_expected.push_back(ACMatchClassification(6, kDim)); 60 styles_expected.push_back(ACMatchClassification(6, kDim));
57 styles_expected.push_back(ACMatchClassification(9, kNone)); 61 styles_expected.push_back(ACMatchClassification(9, kNone));
58 62
59 ExtensionOmniboxSuggestion suggestions; 63 scoped_ptr<SendSuggestions::Params> params(
60 suggestions.description.resize(10); 64 SendSuggestions::Params::Create(*list));
61 EXPECT_TRUE(suggestions.ReadStylesFromValue(styles_value)); 65 EXPECT_TRUE(params);
62 CompareClassification(styles_expected, suggestions.description_styles); 66 EXPECT_TRUE(params->suggest_results[0].get());
67 CompareClassification(styles_expected, StyleTypesToACMatchClassifications(
68 *params->suggest_results[0]));
63 69
64 // Same input, but swap the order. Ensure it still works. 70 // Same input, but swap the order. Ensure it still works.
65 styles_value.Clear(); 71 ListValue* swap_list = ListBuilder().Append(42).Append(ListBuilder().Append(
not at google - send to devlin 2013/03/22 17:33:30 same comments
Aaron Jacobs 2013/03/22 19:54:15 Done.
66 AppendStyle("dim", 6, 3, &styles_value); 72 DictionaryBuilder().Set("content", "content")
67 AppendStyle("match", 1, 4, &styles_value); 73 .Set("description", "description")
68 EXPECT_TRUE(suggestions.ReadStylesFromValue(styles_value)); 74 .Set("descriptionStyles", ListBuilder()
69 CompareClassification(styles_expected, suggestions.description_styles); 75 .Append(DictionaryBuilder()
76 .Set("type", "dim")
77 .Set("offset", 6)
78 .Set("length", 3))
79 .Append(DictionaryBuilder()
80 .Set("type", "match")
81 .Set("offset", 1)
82 .Set("length", 4))))).Build().release();
83
84 scoped_ptr<SendSuggestions::Params> swapped_params(
85 SendSuggestions::Params::Create(*swap_list));
86 EXPECT_TRUE(swapped_params);
87 EXPECT_TRUE(swapped_params->suggest_results[0].get());
88 CompareClassification(styles_expected, StyleTypesToACMatchClassifications(
89 *swapped_params->suggest_results[0]));
70 } 90 }
71 91
72 // 0123456789 92 // 0123456789
73 // uuuuu 93 // uuuuu
74 // + dd 94 // + dd
75 // + mm 95 // + mm
76 // + mmmm 96 // + mmmm
77 // + dd 97 // + dd
78 // = 3773unnnn66 98 // = 3773unnnn66
79 TEST(ExtensionOmniboxTest, DescriptionStylesCombine) { 99 TEST(ExtensionOmniboxTest, DescriptionStylesCombine) {
80 ListValue styles_value; 100 ListValue* list = ListBuilder().Append(42).Append(ListBuilder().Append(
not at google - send to devlin 2013/03/22 17:33:30 same comments
Aaron Jacobs 2013/03/22 19:54:15 Done.
81 AppendStyle("url", 0, 5, &styles_value); 101 DictionaryBuilder().Set("content", "content")
82 AppendStyle("dim", 9, 2, &styles_value); 102 .Set("description", "description")
83 AppendStyle("match", 9, 2, &styles_value); 103 .Set("descriptionStyles", ListBuilder()
84 AppendStyle("match", 0, 4, &styles_value); 104 .Append(DictionaryBuilder()
85 AppendStyle("dim", 1, 2, &styles_value); 105 .Set("type", "url")
106 .Set("offset", 0)
107 .Set("length", 5))
108 .Append(DictionaryBuilder()
109 .Set("type", "dim")
110 .Set("offset", 9)
111 .Set("length", 2))
112 .Append(DictionaryBuilder()
113 .Set("type", "match")
114 .Set("offset", 9)
115 .Set("length", 2))
116 .Append(DictionaryBuilder()
117 .Set("type", "match")
118 .Set("offset", 0)
119 .Set("length", 4))
120 .Append(DictionaryBuilder()
121 .Set("type", "dim")
122 .Set("offset", 1)
123 .Set("length", 2))))).Build().release();
86 124
87 ACMatchClassifications styles_expected; 125 ACMatchClassifications styles_expected;
88 styles_expected.push_back(ACMatchClassification(0, kUrl | kMatch)); 126 styles_expected.push_back(ACMatchClassification(0, kUrl | kMatch));
89 styles_expected.push_back(ACMatchClassification(1, kUrl | kMatch | kDim)); 127 styles_expected.push_back(ACMatchClassification(1, kUrl | kMatch | kDim));
90 styles_expected.push_back(ACMatchClassification(3, kUrl | kMatch)); 128 styles_expected.push_back(ACMatchClassification(3, kUrl | kMatch));
91 styles_expected.push_back(ACMatchClassification(4, kUrl)); 129 styles_expected.push_back(ACMatchClassification(4, kUrl));
92 styles_expected.push_back(ACMatchClassification(5, kNone)); 130 styles_expected.push_back(ACMatchClassification(5, kNone));
93 styles_expected.push_back(ACMatchClassification(9, kMatch | kDim)); 131 styles_expected.push_back(ACMatchClassification(9, kMatch | kDim));
94 132
95 ExtensionOmniboxSuggestion suggestions; 133 scoped_ptr<SendSuggestions::Params> params(
96 suggestions.description.resize(10); 134 SendSuggestions::Params::Create(*list));
97 EXPECT_TRUE(suggestions.ReadStylesFromValue(styles_value)); 135 EXPECT_TRUE(params);
98 CompareClassification(styles_expected, suggestions.description_styles); 136 EXPECT_TRUE(params->suggest_results[0].get());
137 CompareClassification(styles_expected, StyleTypesToACMatchClassifications(
138 *params->suggest_results[0]));
99 139
100 // Try moving the "dim/match" style pair at offset 9. Output should be the 140 // Try moving the "dim/match" style pair at offset 9. Output should be the
101 // same. 141 // same.
102 styles_value.Clear(); 142 ListValue* moved_list = ListBuilder().Append(42).Append(ListBuilder().Append(
not at google - send to devlin 2013/03/22 17:33:30 same comments
Aaron Jacobs 2013/03/22 19:54:15 Done.
103 AppendStyle("url", 0, 5, &styles_value); 143 DictionaryBuilder().Set("content", "content")
104 AppendStyle("match", 0, 4, &styles_value); 144 .Set("description", "description")
105 AppendStyle("dim", 9, 2, &styles_value); 145 .Set("descriptionStyles", ListBuilder()
106 AppendStyle("match", 9, 2, &styles_value); 146 .Append(DictionaryBuilder()
107 AppendStyle("dim", 1, 2, &styles_value); 147 .Set("type", "url")
108 EXPECT_TRUE(suggestions.ReadStylesFromValue(styles_value)); 148 .Set("offset", 0)
109 CompareClassification(styles_expected, suggestions.description_styles); 149 .Set("length", 5))
150 .Append(DictionaryBuilder()
151 .Set("type", "match")
152 .Set("offset", 0)
153 .Set("length", 4))
154 .Append(DictionaryBuilder()
155 .Set("type", "dim")
156 .Set("offset", 9)
157 .Set("length", 2))
158 .Append(DictionaryBuilder()
159 .Set("type", "match")
160 .Set("offset", 9)
161 .Set("length", 2))
162 .Append(DictionaryBuilder()
163 .Set("type", "dim")
164 .Set("offset", 1)
165 .Set("length", 2))))).Build().release();
166
167 scoped_ptr<SendSuggestions::Params> moved_params(
168 SendSuggestions::Params::Create(*moved_list));
169 EXPECT_TRUE(moved_params);
170 EXPECT_TRUE(moved_params->suggest_results[0].get());
171 CompareClassification(styles_expected, StyleTypesToACMatchClassifications(
172 *moved_params->suggest_results[0]));
110 } 173 }
111 174
112 // 0123456789 175 // 0123456789
113 // uuuuu 176 // uuuuu
114 // + mmmmm 177 // + mmmmm
115 // + mmm 178 // + mmm
116 // + ddd 179 // + ddd
117 // + ddd 180 // + ddd
118 // = 77777nnnnn 181 // = 77777nnnnn
119 TEST(ExtensionOmniboxTest, DescriptionStylesCombine2) { 182 TEST(ExtensionOmniboxTest, DescriptionStylesCombine2) {
120 ListValue styles_value; 183 ListValue* list = ListBuilder().Append(42).Append(ListBuilder().Append(
not at google - send to devlin 2013/03/22 17:33:30 same comments
Aaron Jacobs 2013/03/22 19:54:15 Done.
121 AppendStyle("url", 0, 5, &styles_value); 184 DictionaryBuilder().Set("content", "content")
122 AppendStyle("match", 0, 5, &styles_value); 185 .Set("description", "description")
123 AppendStyle("match", 0, 3, &styles_value); 186 .Set("descriptionStyles", ListBuilder()
124 AppendStyle("dim", 2, 3, &styles_value); 187 .Append(DictionaryBuilder()
125 AppendStyle("dim", 0, 3, &styles_value); 188 .Set("type", "url")
189 .Set("offset", 0)
190 .Set("length", 5))
191 .Append(DictionaryBuilder()
192 .Set("type", "match")
193 .Set("offset", 0)
194 .Set("length", 5))
195 .Append(DictionaryBuilder()
196 .Set("type", "match")
197 .Set("offset", 0)
198 .Set("length", 3))
199 .Append(DictionaryBuilder()
200 .Set("type", "dim")
201 .Set("offset", 2)
202 .Set("length", 3))
203 .Append(DictionaryBuilder()
204 .Set("type", "dim")
205 .Set("offset", 0)
206 .Set("length", 3))))).Build().release();
126 207
127 ACMatchClassifications styles_expected; 208 ACMatchClassifications styles_expected;
128 styles_expected.push_back(ACMatchClassification(0, kUrl | kMatch | kDim)); 209 styles_expected.push_back(ACMatchClassification(0, kUrl | kMatch | kDim));
129 styles_expected.push_back(ACMatchClassification(5, kNone)); 210 styles_expected.push_back(ACMatchClassification(5, kNone));
130 211
131 ExtensionOmniboxSuggestion suggestions; 212 scoped_ptr<SendSuggestions::Params> params(
132 suggestions.description.resize(10); 213 SendSuggestions::Params::Create(*list));
133 EXPECT_TRUE(suggestions.ReadStylesFromValue(styles_value)); 214 EXPECT_TRUE(params);
134 CompareClassification(styles_expected, suggestions.description_styles); 215 EXPECT_TRUE(params->suggest_results[0].get());
216 CompareClassification(styles_expected, StyleTypesToACMatchClassifications(
217 *params->suggest_results[0]));
135 } 218 }
136 219
137 } // namespace extensions 220 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698