blob: 44ea704f1b95fe99528a5300eff51934c0fbe827 [file] [log] [blame]
[email protected]7b37fbb2011-03-07 16:16:031// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]ec64212b2010-03-18 01:02:432// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <string>
6#include <vector>
7
[email protected]8bb746372012-04-26 04:20:128#include "base/memory/scoped_ptr.h"
[email protected]ec64212b2010-03-18 01:02:439#include "chrome/browser/autofill/autofill_xml_parser.h"
10#include "chrome/browser/autofill/field_types.h"
11#include "testing/gtest/include/gtest/gtest.h"
[email protected]7b7a16b2010-06-03 04:08:1812#include "third_party/libjingle/source/talk/xmllite/xmlparser.h"
[email protected]ec64212b2010-03-18 01:02:4313
14namespace {
15
[email protected]663bd9e2011-03-21 01:07:0116TEST(AutofillQueryXmlParserTest, BasicQuery) {
[email protected]ec64212b2010-03-18 01:02:4317 // An XML string representing a basic query response.
18 std::string xml = "<autofillqueryresponse>"
19 "<field autofilltype=\"0\" />"
20 "<field autofilltype=\"1\" />"
21 "<field autofilltype=\"3\" />"
22 "<field autofilltype=\"2\" />"
23 "</autofillqueryresponse>";
24
[email protected]606d10802013-01-14 18:14:5725 // Create a vector of AutofillFieldTypes, to assign the parsed field types to.
26 std::vector<AutofillFieldType> field_types;
[email protected]ec64212b2010-03-18 01:02:4327 UploadRequired upload_required = USE_UPLOAD_RATES;
[email protected]5f372f82011-01-31 23:20:5028 std::string experiment_id;
[email protected]ec64212b2010-03-18 01:02:4329
30 // Create a parser.
[email protected]606d10802013-01-14 18:14:5731 AutofillQueryXmlParser parse_handler(&field_types, &upload_required,
[email protected]5f372f82011-01-31 23:20:5032 &experiment_id);
[email protected]ec64212b2010-03-18 01:02:4333 buzz::XmlParser parser(&parse_handler);
34 parser.Parse(xml.c_str(), xml.length(), true);
35 EXPECT_TRUE(parse_handler.succeeded());
[email protected]5f372f82011-01-31 23:20:5036 EXPECT_EQ(USE_UPLOAD_RATES, upload_required);
[email protected]606d10802013-01-14 18:14:5737 ASSERT_EQ(4U, field_types.size());
38 EXPECT_EQ(NO_SERVER_DATA, field_types[0]);
39 EXPECT_EQ(UNKNOWN_TYPE, field_types[1]);
40 EXPECT_EQ(NAME_FIRST, field_types[2]);
41 EXPECT_EQ(EMPTY_TYPE, field_types[3]);
[email protected]5f372f82011-01-31 23:20:5042 EXPECT_EQ(std::string(), experiment_id);
[email protected]ec64212b2010-03-18 01:02:4343}
44
45// Test parsing the upload required attribute.
[email protected]663bd9e2011-03-21 01:07:0146TEST(AutofillQueryXmlParserTest, TestUploadRequired) {
[email protected]606d10802013-01-14 18:14:5747 std::vector<AutofillFieldType> field_types;
[email protected]ec64212b2010-03-18 01:02:4348 UploadRequired upload_required = USE_UPLOAD_RATES;
[email protected]5f372f82011-01-31 23:20:5049 std::string experiment_id;
[email protected]ec64212b2010-03-18 01:02:4350
51 std::string xml = "<autofillqueryresponse uploadrequired=\"true\">"
52 "<field autofilltype=\"0\" />"
53 "</autofillqueryresponse>";
54
[email protected]663bd9e2011-03-21 01:07:0155 scoped_ptr<AutofillQueryXmlParser> parse_handler(
[email protected]606d10802013-01-14 18:14:5756 new AutofillQueryXmlParser(&field_types, &upload_required,
[email protected]5f372f82011-01-31 23:20:5057 &experiment_id));
[email protected]ec64212b2010-03-18 01:02:4358 scoped_ptr<buzz::XmlParser> parser(new buzz::XmlParser(parse_handler.get()));
59 parser->Parse(xml.c_str(), xml.length(), true);
60 EXPECT_TRUE(parse_handler->succeeded());
61 EXPECT_EQ(UPLOAD_REQUIRED, upload_required);
[email protected]606d10802013-01-14 18:14:5762 ASSERT_EQ(1U, field_types.size());
63 EXPECT_EQ(NO_SERVER_DATA, field_types[0]);
[email protected]5f372f82011-01-31 23:20:5064 EXPECT_EQ(std::string(), experiment_id);
[email protected]ec64212b2010-03-18 01:02:4365
[email protected]606d10802013-01-14 18:14:5766 field_types.clear();
[email protected]ec64212b2010-03-18 01:02:4367 xml = "<autofillqueryresponse uploadrequired=\"false\">"
68 "<field autofilltype=\"0\" />"
69 "</autofillqueryresponse>";
70
[email protected]606d10802013-01-14 18:14:5771 parse_handler.reset(new AutofillQueryXmlParser(&field_types, &upload_required,
[email protected]5f372f82011-01-31 23:20:5072 &experiment_id));
[email protected]ec64212b2010-03-18 01:02:4373 parser.reset(new buzz::XmlParser(parse_handler.get()));
74 parser->Parse(xml.c_str(), xml.length(), true);
75 EXPECT_TRUE(parse_handler->succeeded());
[email protected]5f372f82011-01-31 23:20:5076 EXPECT_EQ(UPLOAD_NOT_REQUIRED, upload_required);
[email protected]606d10802013-01-14 18:14:5777 ASSERT_EQ(1U, field_types.size());
78 EXPECT_EQ(NO_SERVER_DATA, field_types[0]);
[email protected]5f372f82011-01-31 23:20:5079 EXPECT_EQ(std::string(), experiment_id);
[email protected]ec64212b2010-03-18 01:02:4380
[email protected]606d10802013-01-14 18:14:5781 field_types.clear();
[email protected]ec64212b2010-03-18 01:02:4382 xml = "<autofillqueryresponse uploadrequired=\"bad_value\">"
83 "<field autofilltype=\"0\" />"
84 "</autofillqueryresponse>";
85
[email protected]606d10802013-01-14 18:14:5786 parse_handler.reset(new AutofillQueryXmlParser(&field_types, &upload_required,
[email protected]5f372f82011-01-31 23:20:5087 &experiment_id));
[email protected]ec64212b2010-03-18 01:02:4388 parser.reset(new buzz::XmlParser(parse_handler.get()));
89 parser->Parse(xml.c_str(), xml.length(), true);
90 EXPECT_TRUE(parse_handler->succeeded());
[email protected]5f372f82011-01-31 23:20:5091 EXPECT_EQ(USE_UPLOAD_RATES, upload_required);
[email protected]606d10802013-01-14 18:14:5792 ASSERT_EQ(1U, field_types.size());
93 EXPECT_EQ(NO_SERVER_DATA, field_types[0]);
[email protected]5f372f82011-01-31 23:20:5094 EXPECT_EQ(std::string(), experiment_id);
95}
96
97// Test parsing the experiment id attribute
[email protected]663bd9e2011-03-21 01:07:0198TEST(AutofillQueryXmlParserTest, ParseExperimentId) {
[email protected]606d10802013-01-14 18:14:5799 std::vector<AutofillFieldType> field_types;
[email protected]5f372f82011-01-31 23:20:50100 UploadRequired upload_required = USE_UPLOAD_RATES;
101 std::string experiment_id;
102
103 // When the attribute is missing, we should get back the default value -- the
104 // empty string.
105 std::string xml = "<autofillqueryresponse>"
106 "<field autofilltype=\"0\" />"
107 "</autofillqueryresponse>";
108
[email protected]663bd9e2011-03-21 01:07:01109 scoped_ptr<AutofillQueryXmlParser> parse_handler(
[email protected]606d10802013-01-14 18:14:57110 new AutofillQueryXmlParser(&field_types, &upload_required,
[email protected]5f372f82011-01-31 23:20:50111 &experiment_id));
112 scoped_ptr<buzz::XmlParser> parser(new buzz::XmlParser(parse_handler.get()));
113 parser->Parse(xml.c_str(), xml.length(), true);
114 EXPECT_TRUE(parse_handler->succeeded());
115 EXPECT_EQ(USE_UPLOAD_RATES, upload_required);
[email protected]606d10802013-01-14 18:14:57116 ASSERT_EQ(1U, field_types.size());
117 EXPECT_EQ(NO_SERVER_DATA, field_types[0]);
[email protected]5f372f82011-01-31 23:20:50118 EXPECT_EQ(std::string(), experiment_id);
119
[email protected]606d10802013-01-14 18:14:57120 field_types.clear();
[email protected]5f372f82011-01-31 23:20:50121
122 // When the attribute is present, make sure we parse it.
123 xml = "<autofillqueryresponse experimentid=\"FancyNewAlgorithm\">"
124 "<field autofilltype=\"0\" />"
125 "</autofillqueryresponse>";
126
[email protected]606d10802013-01-14 18:14:57127 parse_handler.reset(new AutofillQueryXmlParser(&field_types, &upload_required,
[email protected]5f372f82011-01-31 23:20:50128 &experiment_id));
129 parser.reset(new buzz::XmlParser(parse_handler.get()));
130 parser->Parse(xml.c_str(), xml.length(), true);
131 EXPECT_TRUE(parse_handler->succeeded());
132 EXPECT_EQ(USE_UPLOAD_RATES, upload_required);
[email protected]606d10802013-01-14 18:14:57133 ASSERT_EQ(1U, field_types.size());
134 EXPECT_EQ(NO_SERVER_DATA, field_types[0]);
[email protected]5f372f82011-01-31 23:20:50135 EXPECT_EQ(std::string("FancyNewAlgorithm"), experiment_id);
136
[email protected]606d10802013-01-14 18:14:57137 field_types.clear();
[email protected]5f372f82011-01-31 23:20:50138
139 // Make sure that we can handle parsing both the upload required and the
140 // experiment id attribute together.
141 xml = "<autofillqueryresponse uploadrequired=\"false\""
142 " experimentid=\"ServerSmartyPants\">"
143 "<field autofilltype=\"0\" />"
144 "</autofillqueryresponse>";
145
[email protected]606d10802013-01-14 18:14:57146 parse_handler.reset(new AutofillQueryXmlParser(&field_types, &upload_required,
[email protected]5f372f82011-01-31 23:20:50147 &experiment_id));
148 parser.reset(new buzz::XmlParser(parse_handler.get()));
149 parser->Parse(xml.c_str(), xml.length(), true);
150 EXPECT_TRUE(parse_handler->succeeded());
151 EXPECT_EQ(UPLOAD_NOT_REQUIRED, upload_required);
[email protected]606d10802013-01-14 18:14:57152 ASSERT_EQ(1U, field_types.size());
153 EXPECT_EQ(NO_SERVER_DATA, field_types[0]);
[email protected]5f372f82011-01-31 23:20:50154 EXPECT_EQ(std::string("ServerSmartyPants"), experiment_id);
[email protected]ec64212b2010-03-18 01:02:43155}
156
[email protected]e899b962013-01-18 20:52:08157// Test XML response with autofill_flow information.
158TEST(AutofillQueryXmlParserTest, ParseAutofillFlow) {
159 std::vector<AutofillFieldType> field_types;
160 UploadRequired upload_required = USE_UPLOAD_RATES;
161 std::string experiment_id;
162
163 std::string xml = "<autofillqueryresponse>"
164 "<field autofilltype=\"55\"/>"
165 "<autofill_flow page_no=\"1\" total_pages=\"10\"/>"
166 "</autofillqueryresponse>";
167
168 scoped_ptr<AutofillQueryXmlParser> parse_handler(
169 new AutofillQueryXmlParser(&field_types, &upload_required,
170 &experiment_id));
171 scoped_ptr<buzz::XmlParser> parser(new buzz::XmlParser(parse_handler.get()));
172 parser->Parse(xml.c_str(), xml.length(), true);
173 EXPECT_TRUE(parse_handler->succeeded());
174 EXPECT_EQ(1U, field_types.size());
175 EXPECT_EQ(1, parse_handler->current_page_number());
176 EXPECT_EQ(10, parse_handler->total_pages());
177}
178
[email protected]ec64212b2010-03-18 01:02:43179// Test badly formed XML queries.
[email protected]663bd9e2011-03-21 01:07:01180TEST(AutofillQueryXmlParserTest, ParseErrors) {
[email protected]606d10802013-01-14 18:14:57181 std::vector<AutofillFieldType> field_types;
[email protected]ec64212b2010-03-18 01:02:43182 UploadRequired upload_required = USE_UPLOAD_RATES;
[email protected]5f372f82011-01-31 23:20:50183 std::string experiment_id;
[email protected]ec64212b2010-03-18 01:02:43184
[email protected]663bd9e2011-03-21 01:07:01185 // Test no Autofill type.
[email protected]ec64212b2010-03-18 01:02:43186 std::string xml = "<autofillqueryresponse>"
187 "<field/>"
188 "</autofillqueryresponse>";
189
[email protected]663bd9e2011-03-21 01:07:01190 scoped_ptr<AutofillQueryXmlParser> parse_handler(
[email protected]606d10802013-01-14 18:14:57191 new AutofillQueryXmlParser(&field_types, &upload_required,
[email protected]5f372f82011-01-31 23:20:50192 &experiment_id));
[email protected]ec64212b2010-03-18 01:02:43193 scoped_ptr<buzz::XmlParser> parser(new buzz::XmlParser(parse_handler.get()));
194 parser->Parse(xml.c_str(), xml.length(), true);
195 EXPECT_FALSE(parse_handler->succeeded());
[email protected]5f372f82011-01-31 23:20:50196 EXPECT_EQ(USE_UPLOAD_RATES, upload_required);
[email protected]606d10802013-01-14 18:14:57197 EXPECT_EQ(0U, field_types.size());
[email protected]5f372f82011-01-31 23:20:50198 EXPECT_EQ(std::string(), experiment_id);
[email protected]ec64212b2010-03-18 01:02:43199
[email protected]663bd9e2011-03-21 01:07:01200 // Test an incorrect Autofill type.
[email protected]ec64212b2010-03-18 01:02:43201 xml = "<autofillqueryresponse>"
[email protected]606d10802013-01-14 18:14:57202 "<field autofilltype=\"307\"/>"
[email protected]ec64212b2010-03-18 01:02:43203 "</autofillqueryresponse>";
204
[email protected]606d10802013-01-14 18:14:57205 parse_handler.reset(new AutofillQueryXmlParser(&field_types, &upload_required,
[email protected]5f372f82011-01-31 23:20:50206 &experiment_id));
[email protected]ec64212b2010-03-18 01:02:43207 parser.reset(new buzz::XmlParser(parse_handler.get()));
208 parser->Parse(xml.c_str(), xml.length(), true);
209 EXPECT_TRUE(parse_handler->succeeded());
[email protected]5f372f82011-01-31 23:20:50210 EXPECT_EQ(USE_UPLOAD_RATES, upload_required);
[email protected]606d10802013-01-14 18:14:57211 ASSERT_EQ(1U, field_types.size());
[email protected]7b37fbb2011-03-07 16:16:03212 // AutofillType was out of range and should be set to NO_SERVER_DATA.
[email protected]606d10802013-01-14 18:14:57213 EXPECT_EQ(NO_SERVER_DATA, field_types[0]);
[email protected]5f372f82011-01-31 23:20:50214 EXPECT_EQ(std::string(), experiment_id);
[email protected]ec64212b2010-03-18 01:02:43215
[email protected]663bd9e2011-03-21 01:07:01216 // Test an incorrect Autofill type.
[email protected]606d10802013-01-14 18:14:57217 field_types.clear();
[email protected]ec64212b2010-03-18 01:02:43218 xml = "<autofillqueryresponse>"
219 "<field autofilltype=\"No Type\"/>"
220 "</autofillqueryresponse>";
221
[email protected]606d10802013-01-14 18:14:57222 // Parse fails but an entry is still added to field_types.
223 parse_handler.reset(new AutofillQueryXmlParser(&field_types, &upload_required,
[email protected]5f372f82011-01-31 23:20:50224 &experiment_id));
[email protected]ec64212b2010-03-18 01:02:43225 parser.reset(new buzz::XmlParser(parse_handler.get()));
226 parser->Parse(xml.c_str(), xml.length(), true);
227 EXPECT_FALSE(parse_handler->succeeded());
[email protected]5f372f82011-01-31 23:20:50228 EXPECT_EQ(USE_UPLOAD_RATES, upload_required);
[email protected]606d10802013-01-14 18:14:57229 ASSERT_EQ(1U, field_types.size());
230 EXPECT_EQ(NO_SERVER_DATA, field_types[0]);
[email protected]5f372f82011-01-31 23:20:50231 EXPECT_EQ(std::string(), experiment_id);
[email protected]ec64212b2010-03-18 01:02:43232}
233
[email protected]db163472010-04-02 22:01:20234// Test successfull upload response.
[email protected]663bd9e2011-03-21 01:07:01235TEST(AutofillUploadXmlParser, TestSuccessfulResponse) {
[email protected]db163472010-04-02 22:01:20236 std::string xml = "<autofilluploadresponse positiveuploadrate=\"0.5\" "
237 "negativeuploadrate=\"0.3\"/>";
238 double positive = 0;
239 double negative = 0;
[email protected]663bd9e2011-03-21 01:07:01240 AutofillUploadXmlParser parse_handler(&positive, &negative);
[email protected]db163472010-04-02 22:01:20241 buzz::XmlParser parser(&parse_handler);
242 parser.Parse(xml.c_str(), xml.length(), true);
243 EXPECT_TRUE(parse_handler.succeeded());
244 EXPECT_DOUBLE_EQ(0.5, positive);
245 EXPECT_DOUBLE_EQ(0.3, negative);
246}
247
248// Test failed upload response.
[email protected]663bd9e2011-03-21 01:07:01249TEST(AutofillUploadXmlParser, TestFailedResponse) {
[email protected]db163472010-04-02 22:01:20250 std::string xml = "<autofilluploadresponse positiveuploadrate=\"\" "
251 "negativeuploadrate=\"0.3\"/>";
252 double positive = 0;
253 double negative = 0;
[email protected]663bd9e2011-03-21 01:07:01254 scoped_ptr<AutofillUploadXmlParser> parse_handler(
255 new AutofillUploadXmlParser(&positive, &negative));
[email protected]db163472010-04-02 22:01:20256 scoped_ptr<buzz::XmlParser> parser(new buzz::XmlParser(parse_handler.get()));
257 parser->Parse(xml.c_str(), xml.length(), true);
258 EXPECT_TRUE(!parse_handler->succeeded());
259 EXPECT_DOUBLE_EQ(0, positive);
260 EXPECT_DOUBLE_EQ(0.3, negative); // Partially parsed.
261 negative = 0;
262
263 xml = "<autofilluploadresponse positiveuploadrate=\"0.5\" "
264 "negativeuploadrate=\"0.3\"";
[email protected]663bd9e2011-03-21 01:07:01265 parse_handler.reset(new AutofillUploadXmlParser(&positive, &negative));
[email protected]db163472010-04-02 22:01:20266 parser.reset(new buzz::XmlParser(parse_handler.get()));
267 parser->Parse(xml.c_str(), xml.length(), true);
268 EXPECT_TRUE(!parse_handler->succeeded());
269 EXPECT_DOUBLE_EQ(0, positive);
270 EXPECT_DOUBLE_EQ(0, negative);
271
272 xml = "bad data";
[email protected]663bd9e2011-03-21 01:07:01273 parse_handler.reset(new AutofillUploadXmlParser(&positive, &negative));
[email protected]db163472010-04-02 22:01:20274 parser.reset(new buzz::XmlParser(parse_handler.get()));
275 parser->Parse(xml.c_str(), xml.length(), true);
276 EXPECT_TRUE(!parse_handler->succeeded());
277 EXPECT_DOUBLE_EQ(0, positive);
278 EXPECT_DOUBLE_EQ(0, negative);
279
280 xml = "";
[email protected]663bd9e2011-03-21 01:07:01281 parse_handler.reset(new AutofillUploadXmlParser(&positive, &negative));
[email protected]db163472010-04-02 22:01:20282 parser.reset(new buzz::XmlParser(parse_handler.get()));
283 parser->Parse(xml.c_str(), xml.length(), true);
284 EXPECT_TRUE(!parse_handler->succeeded());
285 EXPECT_DOUBLE_EQ(0, positive);
286 EXPECT_DOUBLE_EQ(0, negative);
287}
288
[email protected]ec64212b2010-03-18 01:02:43289} // namespace