Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / autofill / core / browser / autofill_xml_parser_unittest.cc
blobb97c7746ccc2c1b388fd43135d72a13dca44f565
1 // Copyright 2013 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 <string>
6 #include <vector>
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "components/autofill/core/browser/autofill_xml_parser.h"
11 #include "components/autofill/core/browser/field_types.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/webrtc/libjingle/xmllite/xmlparser.h"
15 namespace autofill {
16 namespace {
18 class AutofillQueryXmlParserTest : public testing::Test {
19 public:
20 AutofillQueryXmlParserTest(): upload_required_(USE_UPLOAD_RATES) {};
21 ~AutofillQueryXmlParserTest() override{};
23 protected:
24 void ParseQueryXML(const std::string& xml, bool should_succeed) {
25 // Create a parser.
26 AutofillQueryXmlParser parse_handler(&field_infos_,
27 &upload_required_);
28 buzz::XmlParser parser(&parse_handler);
29 parser.Parse(xml.c_str(), xml.length(), true);
30 EXPECT_EQ(should_succeed, parse_handler.succeeded());
33 std::vector<AutofillServerFieldInfo> field_infos_;
34 UploadRequired upload_required_;
37 class AutofillUploadXmlParserTest : public testing::Test {
38 public:
39 AutofillUploadXmlParserTest(): positive_(0), negative_(0) {};
40 ~AutofillUploadXmlParserTest() override{};
42 protected:
43 void ParseUploadXML(const std::string& xml, bool should_succeed) {
44 // Create a parser.
45 AutofillUploadXmlParser parse_handler(&positive_, &negative_);
46 buzz::XmlParser parser(&parse_handler);
47 parser.Parse(xml.c_str(), xml.length(), true);
49 EXPECT_EQ(should_succeed, parse_handler.succeeded());
52 double positive_;
53 double negative_;
56 TEST_F(AutofillQueryXmlParserTest, BasicQuery) {
57 // An XML string representing a basic query response.
58 std::string xml = "<autofillqueryresponse>"
59 "<field autofilltype=\"0\" />"
60 "<field autofilltype=\"1\" />"
61 "<field autofilltype=\"3\" />"
62 "<field autofilltype=\"2\" />"
63 "<field autofilltype=\"61\" defaultvalue=\"default\"/>"
64 "</autofillqueryresponse>";
65 ParseQueryXML(xml, true);
67 EXPECT_EQ(USE_UPLOAD_RATES, upload_required_);
68 ASSERT_EQ(5U, field_infos_.size());
69 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type);
70 EXPECT_EQ(UNKNOWN_TYPE, field_infos_[1].field_type);
71 EXPECT_EQ(NAME_FIRST, field_infos_[2].field_type);
72 EXPECT_EQ(EMPTY_TYPE, field_infos_[3].field_type);
73 EXPECT_TRUE(field_infos_[3].default_value.empty());
74 EXPECT_EQ(FIELD_WITH_DEFAULT_VALUE, field_infos_[4].field_type);
75 EXPECT_EQ("default", field_infos_[4].default_value);
78 // Test parsing the upload required attribute.
79 TEST_F(AutofillQueryXmlParserTest, TestUploadRequired) {
80 std::string xml = "<autofillqueryresponse uploadrequired=\"true\">"
81 "<field autofilltype=\"0\" />"
82 "</autofillqueryresponse>";
84 ParseQueryXML(xml, true);
86 EXPECT_EQ(upload_required_, upload_required_);
87 ASSERT_EQ(1U, field_infos_.size());
88 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type);
90 field_infos_.clear();
91 xml = "<autofillqueryresponse uploadrequired=\"false\">"
92 "<field autofilltype=\"0\" />"
93 "</autofillqueryresponse>";
95 ParseQueryXML(xml, true);
97 EXPECT_EQ(UPLOAD_NOT_REQUIRED, upload_required_);
98 ASSERT_EQ(1U, field_infos_.size());
99 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type);
101 field_infos_.clear();
102 xml = "<autofillqueryresponse uploadrequired=\"bad_value\">"
103 "<field autofilltype=\"0\" />"
104 "</autofillqueryresponse>";
106 ParseQueryXML(xml, true);
108 EXPECT_EQ(USE_UPLOAD_RATES, upload_required_);
109 ASSERT_EQ(1U, field_infos_.size());
110 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type);
113 // Test badly formed XML queries.
114 TEST_F(AutofillQueryXmlParserTest, ParseErrors) {
115 // Test no Autofill type.
116 std::string xml = "<autofillqueryresponse>"
117 "<field/>"
118 "</autofillqueryresponse>";
120 ParseQueryXML(xml, false);
122 EXPECT_EQ(USE_UPLOAD_RATES, upload_required_);
123 EXPECT_EQ(0U, field_infos_.size());
125 // Test an incorrect Autofill type.
126 xml = "<autofillqueryresponse>"
127 "<field autofilltype=\"-1\"/>"
128 "</autofillqueryresponse>";
130 ParseQueryXML(xml, true);
132 EXPECT_EQ(USE_UPLOAD_RATES, upload_required_);
133 ASSERT_EQ(1U, field_infos_.size());
134 // AutofillType was out of range and should be set to NO_SERVER_DATA.
135 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type);
137 // Test upper bound for the field type, MAX_VALID_FIELD_TYPE.
138 field_infos_.clear();
139 xml = "<autofillqueryresponse><field autofilltype=\"" +
140 base::IntToString(MAX_VALID_FIELD_TYPE) + "\"/></autofillqueryresponse>";
142 ParseQueryXML(xml, true);
144 EXPECT_EQ(USE_UPLOAD_RATES, upload_required_);
145 ASSERT_EQ(1U, field_infos_.size());
146 // AutofillType was out of range and should be set to NO_SERVER_DATA.
147 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type);
149 // Test an incorrect Autofill type.
150 field_infos_.clear();
151 xml = "<autofillqueryresponse>"
152 "<field autofilltype=\"No Type\"/>"
153 "</autofillqueryresponse>";
155 // Parse fails but an entry is still added to field_infos_.
156 ParseQueryXML(xml, false);
158 EXPECT_EQ(USE_UPLOAD_RATES, upload_required_);
159 ASSERT_EQ(1U, field_infos_.size());
160 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type);
163 // Test successfull upload response.
164 TEST_F(AutofillUploadXmlParserTest, TestSuccessfulResponse) {
165 ParseUploadXML("<autofilluploadresponse positiveuploadrate=\"0.5\" "
166 "negativeuploadrate=\"0.3\"/>",
167 true);
169 EXPECT_DOUBLE_EQ(0.5, positive_);
170 EXPECT_DOUBLE_EQ(0.3, negative_);
173 // Test failed upload response.
174 TEST_F(AutofillUploadXmlParserTest, TestFailedResponse) {
175 ParseUploadXML("<autofilluploadresponse positiveuploadrate=\"\" "
176 "negativeuploadrate=\"0.3\"/>",
177 false);
179 EXPECT_DOUBLE_EQ(0, positive_);
180 EXPECT_DOUBLE_EQ(0.3, negative_); // Partially parsed.
181 negative_ = 0;
183 ParseUploadXML("<autofilluploadresponse positiveuploadrate=\"0.5\" "
184 "negativeuploadrate=\"0.3\"",
185 false);
187 EXPECT_DOUBLE_EQ(0, positive_);
188 EXPECT_DOUBLE_EQ(0, negative_);
190 ParseUploadXML("bad data", false);
192 EXPECT_DOUBLE_EQ(0, positive_);
193 EXPECT_DOUBLE_EQ(0, negative_);
195 ParseUploadXML(std::string(), false);
197 EXPECT_DOUBLE_EQ(0, positive_);
198 EXPECT_DOUBLE_EQ(0, negative_);
201 } // namespace
202 } // namespace autofill