| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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. | |
| 4 | |
| 5 #include <string> | |
| 6 #include <vector> | |
| 7 | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/strings/string_number_conversions.h" | |
| 10 #include "components/autofill/browser/autofill_xml_parser.h" | |
| 11 #include "components/autofill/browser/field_types.h" | |
| 12 #include "components/autofill/content/browser/autocheckout_page_meta_data.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 #include "third_party/libjingle/source/talk/xmllite/xmlparser.h" | |
| 15 | |
| 16 namespace autofill { | |
| 17 namespace { | |
| 18 | |
| 19 class AutofillQueryXmlParserTest : public testing::Test { | |
| 20 public: | |
| 21 AutofillQueryXmlParserTest(): upload_required_(USE_UPLOAD_RATES) {}; | |
| 22 virtual ~AutofillQueryXmlParserTest() {}; | |
| 23 | |
| 24 protected: | |
| 25 void ParseQueryXML(const std::string& xml, bool should_succeed) { | |
| 26 // Create a parser. | |
| 27 AutofillQueryXmlParser parse_handler(&field_infos_, | |
| 28 &upload_required_, | |
| 29 &experiment_id_, | |
| 30 &page_meta_data_); | |
| 31 buzz::XmlParser parser(&parse_handler); | |
| 32 parser.Parse(xml.c_str(), xml.length(), true); | |
| 33 EXPECT_EQ(should_succeed, parse_handler.succeeded()); | |
| 34 } | |
| 35 | |
| 36 std::vector<AutofillServerFieldInfo> field_infos_; | |
| 37 UploadRequired upload_required_; | |
| 38 std::string experiment_id_; | |
| 39 autofill::AutocheckoutPageMetaData page_meta_data_; | |
| 40 }; | |
| 41 | |
| 42 class AutofillUploadXmlParserTest : public testing::Test { | |
| 43 public: | |
| 44 AutofillUploadXmlParserTest(): positive_(0), negative_(0) {}; | |
| 45 virtual ~AutofillUploadXmlParserTest() {}; | |
| 46 | |
| 47 protected: | |
| 48 void ParseUploadXML(const std::string& xml, bool should_succeed) { | |
| 49 // Create a parser. | |
| 50 AutofillUploadXmlParser parse_handler(&positive_, &negative_); | |
| 51 buzz::XmlParser parser(&parse_handler); | |
| 52 parser.Parse(xml.c_str(), xml.length(), true); | |
| 53 | |
| 54 EXPECT_EQ(should_succeed, parse_handler.succeeded()); | |
| 55 } | |
| 56 | |
| 57 double positive_; | |
| 58 double negative_; | |
| 59 }; | |
| 60 | |
| 61 TEST_F(AutofillQueryXmlParserTest, BasicQuery) { | |
| 62 // An XML string representing a basic query response. | |
| 63 std::string xml = "<autofillqueryresponse>" | |
| 64 "<field autofilltype=\"0\" />" | |
| 65 "<field autofilltype=\"1\" />" | |
| 66 "<field autofilltype=\"3\" />" | |
| 67 "<field autofilltype=\"2\" />" | |
| 68 "<field autofilltype=\"61\" defaultvalue=\"default\"/>" | |
| 69 "</autofillqueryresponse>"; | |
| 70 ParseQueryXML(xml, true); | |
| 71 | |
| 72 EXPECT_EQ(USE_UPLOAD_RATES, upload_required_); | |
| 73 ASSERT_EQ(5U, field_infos_.size()); | |
| 74 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type); | |
| 75 EXPECT_EQ(UNKNOWN_TYPE, field_infos_[1].field_type); | |
| 76 EXPECT_EQ(NAME_FIRST, field_infos_[2].field_type); | |
| 77 EXPECT_EQ(EMPTY_TYPE, field_infos_[3].field_type); | |
| 78 EXPECT_TRUE(field_infos_[3].default_value.empty()); | |
| 79 EXPECT_EQ(FIELD_WITH_DEFAULT_VALUE, field_infos_[4].field_type); | |
| 80 EXPECT_EQ("default", field_infos_[4].default_value); | |
| 81 EXPECT_TRUE(experiment_id_.empty()); | |
| 82 } | |
| 83 | |
| 84 // Test parsing the upload required attribute. | |
| 85 TEST_F(AutofillQueryXmlParserTest, TestUploadRequired) { | |
| 86 std::string xml = "<autofillqueryresponse uploadrequired=\"true\">" | |
| 87 "<field autofilltype=\"0\" />" | |
| 88 "</autofillqueryresponse>"; | |
| 89 | |
| 90 ParseQueryXML(xml, true); | |
| 91 | |
| 92 EXPECT_EQ(upload_required_, upload_required_); | |
| 93 ASSERT_EQ(1U, field_infos_.size()); | |
| 94 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type); | |
| 95 EXPECT_TRUE(experiment_id_.empty()); | |
| 96 | |
| 97 field_infos_.clear(); | |
| 98 xml = "<autofillqueryresponse uploadrequired=\"false\">" | |
| 99 "<field autofilltype=\"0\" />" | |
| 100 "</autofillqueryresponse>"; | |
| 101 | |
| 102 ParseQueryXML(xml, true); | |
| 103 | |
| 104 EXPECT_EQ(UPLOAD_NOT_REQUIRED, upload_required_); | |
| 105 ASSERT_EQ(1U, field_infos_.size()); | |
| 106 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type); | |
| 107 EXPECT_TRUE(experiment_id_.empty()); | |
| 108 | |
| 109 field_infos_.clear(); | |
| 110 xml = "<autofillqueryresponse uploadrequired=\"bad_value\">" | |
| 111 "<field autofilltype=\"0\" />" | |
| 112 "</autofillqueryresponse>"; | |
| 113 | |
| 114 ParseQueryXML(xml, true); | |
| 115 | |
| 116 EXPECT_EQ(USE_UPLOAD_RATES, upload_required_); | |
| 117 ASSERT_EQ(1U, field_infos_.size()); | |
| 118 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type); | |
| 119 EXPECT_TRUE(experiment_id_.empty()); | |
| 120 } | |
| 121 | |
| 122 // Test parsing the experiment id attribute | |
| 123 TEST_F(AutofillQueryXmlParserTest, ParseExperimentId) { | |
| 124 // When the attribute is missing, we should get back the default value -- the | |
| 125 // empty string. | |
| 126 std::string xml = "<autofillqueryresponse>" | |
| 127 "<field autofilltype=\"0\" />" | |
| 128 "</autofillqueryresponse>"; | |
| 129 | |
| 130 ParseQueryXML(xml, true); | |
| 131 | |
| 132 EXPECT_EQ(USE_UPLOAD_RATES, upload_required_); | |
| 133 ASSERT_EQ(1U, field_infos_.size()); | |
| 134 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type); | |
| 135 EXPECT_TRUE(experiment_id_.empty()); | |
| 136 | |
| 137 field_infos_.clear(); | |
| 138 | |
| 139 // When the attribute is present, make sure we parse it. | |
| 140 xml = "<autofillqueryresponse experimentid=\"FancyNewAlgorithm\">" | |
| 141 "<field autofilltype=\"0\" />" | |
| 142 "</autofillqueryresponse>"; | |
| 143 | |
| 144 ParseQueryXML(xml, true); | |
| 145 | |
| 146 EXPECT_EQ(USE_UPLOAD_RATES, upload_required_); | |
| 147 ASSERT_EQ(1U, field_infos_.size()); | |
| 148 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type); | |
| 149 EXPECT_EQ(std::string("FancyNewAlgorithm"), experiment_id_); | |
| 150 | |
| 151 field_infos_.clear(); | |
| 152 | |
| 153 // Make sure that we can handle parsing both the upload required and the | |
| 154 // experiment id attribute together. | |
| 155 xml = "<autofillqueryresponse uploadrequired=\"false\"" | |
| 156 " experimentid=\"ServerSmartyPants\">" | |
| 157 "<field autofilltype=\"0\" />" | |
| 158 "</autofillqueryresponse>"; | |
| 159 | |
| 160 ParseQueryXML(xml, true); | |
| 161 | |
| 162 EXPECT_EQ(UPLOAD_NOT_REQUIRED, upload_required_); | |
| 163 ASSERT_EQ(1U, field_infos_.size()); | |
| 164 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type); | |
| 165 EXPECT_EQ("ServerSmartyPants", experiment_id_); | |
| 166 } | |
| 167 | |
| 168 // Test XML response with autofill_flow information. | |
| 169 TEST_F(AutofillQueryXmlParserTest, ParseAutofillFlow) { | |
| 170 std::string xml = "<autofillqueryresponse>" | |
| 171 "<field autofilltype=\"55\"/>" | |
| 172 "<autofill_flow page_no=\"1\" total_pages=\"10\">" | |
| 173 "<page_advance_button id=\"foo\"/>" | |
| 174 "</autofill_flow>" | |
| 175 "</autofillqueryresponse>"; | |
| 176 | |
| 177 ParseQueryXML(xml, true); | |
| 178 | |
| 179 EXPECT_EQ(1U, field_infos_.size()); | |
| 180 EXPECT_EQ(1, page_meta_data_.current_page_number); | |
| 181 EXPECT_EQ(10, page_meta_data_.total_pages); | |
| 182 EXPECT_EQ("foo", page_meta_data_.proceed_element_descriptor.descriptor); | |
| 183 EXPECT_EQ(autofill::WebElementDescriptor::ID, | |
| 184 page_meta_data_.proceed_element_descriptor.retrieval_method); | |
| 185 | |
| 186 // Clear |field_infos_| for the next test; | |
| 187 field_infos_.clear(); | |
| 188 | |
| 189 // Test css_selector as page_advance_button. | |
| 190 xml = "<autofillqueryresponse>" | |
| 191 "<field autofilltype=\"55\"/>" | |
| 192 "<autofill_flow page_no=\"1\" total_pages=\"10\">" | |
| 193 "<page_advance_button css_selector=\"[name="foo"]\"/>" | |
| 194 "</autofill_flow>" | |
| 195 "</autofillqueryresponse>"; | |
| 196 | |
| 197 ParseQueryXML(xml, true); | |
| 198 | |
| 199 EXPECT_EQ(1U, field_infos_.size()); | |
| 200 EXPECT_EQ(1, page_meta_data_.current_page_number); | |
| 201 EXPECT_EQ(10, page_meta_data_.total_pages); | |
| 202 EXPECT_EQ("[name=\"foo\"]", | |
| 203 page_meta_data_.proceed_element_descriptor.descriptor); | |
| 204 EXPECT_EQ(autofill::WebElementDescriptor::CSS_SELECTOR, | |
| 205 page_meta_data_.proceed_element_descriptor.retrieval_method); | |
| 206 | |
| 207 // Clear |field_infos_| for the next test; | |
| 208 field_infos_.clear(); | |
| 209 | |
| 210 // Test first attribute is always the one set. | |
| 211 xml = "<autofillqueryresponse>" | |
| 212 "<field autofilltype=\"55\"/>" | |
| 213 "<autofill_flow page_no=\"1\" total_pages=\"10\">" | |
| 214 "<page_advance_button css_selector=\"[name="foo"]\"" | |
| 215 " id=\"foo\"/>" | |
| 216 "</autofill_flow>" | |
| 217 "</autofillqueryresponse>"; | |
| 218 | |
| 219 ParseQueryXML(xml, true); | |
| 220 | |
| 221 EXPECT_EQ(1U, field_infos_.size()); | |
| 222 EXPECT_EQ(1, page_meta_data_.current_page_number); | |
| 223 EXPECT_EQ(10, page_meta_data_.total_pages); | |
| 224 EXPECT_EQ("[name=\"foo\"]", | |
| 225 page_meta_data_.proceed_element_descriptor.descriptor); | |
| 226 EXPECT_EQ(autofill::WebElementDescriptor::CSS_SELECTOR, | |
| 227 page_meta_data_.proceed_element_descriptor.retrieval_method); | |
| 228 | |
| 229 // Clear |field_infos_| for the next test; | |
| 230 field_infos_.clear(); | |
| 231 | |
| 232 // Test parsing click_elements_before_formfill correctly. | |
| 233 xml = "<autofillqueryresponse>" | |
| 234 "<field autofilltype=\"55\"/>" | |
| 235 "<autofill_flow page_no=\"1\" total_pages=\"10\">" | |
| 236 "<click_elements_before_formfill>" | |
| 237 "<web_element id=\"btn1\" /></click_elements_before_formfill>" | |
| 238 "<click_elements_before_formfill>" | |
| 239 "<web_element css_selector=\"[name="btn2"]\"/>" | |
| 240 "</click_elements_before_formfill>" | |
| 241 "</autofill_flow>" | |
| 242 "</autofillqueryresponse>"; | |
| 243 | |
| 244 ParseQueryXML(xml, true); | |
| 245 | |
| 246 EXPECT_EQ(1U, field_infos_.size()); | |
| 247 EXPECT_EQ(1, page_meta_data_.current_page_number); | |
| 248 EXPECT_EQ(10, page_meta_data_.total_pages); | |
| 249 ASSERT_EQ(2U, page_meta_data_.click_elements_before_form_fill.size()); | |
| 250 autofill::WebElementDescriptor& click_elment = | |
| 251 page_meta_data_.click_elements_before_form_fill[0]; | |
| 252 EXPECT_EQ("btn1", click_elment.descriptor); | |
| 253 EXPECT_EQ(autofill::WebElementDescriptor::ID, click_elment.retrieval_method); | |
| 254 click_elment = page_meta_data_.click_elements_before_form_fill[1]; | |
| 255 EXPECT_EQ("[name=\"btn2\"]", click_elment.descriptor); | |
| 256 EXPECT_EQ(autofill::WebElementDescriptor::CSS_SELECTOR, | |
| 257 click_elment.retrieval_method); | |
| 258 | |
| 259 // Clear |field_infos_| for the next test; | |
| 260 field_infos_.clear(); | |
| 261 | |
| 262 // Test parsing click_elements_after_formfill correctly. | |
| 263 xml = "<autofillqueryresponse>" | |
| 264 "<field autofilltype=\"55\"/>" | |
| 265 "<autofill_flow page_no=\"1\" total_pages=\"10\">" | |
| 266 "<click_elements_after_formfill>" | |
| 267 "<web_element id=\"btn1\" /></click_elements_after_formfill>" | |
| 268 "</autofill_flow>" | |
| 269 "</autofillqueryresponse>"; | |
| 270 | |
| 271 ParseQueryXML(xml, true); | |
| 272 | |
| 273 EXPECT_EQ(1U, field_infos_.size()); | |
| 274 EXPECT_EQ(1, page_meta_data_.current_page_number); | |
| 275 EXPECT_EQ(10, page_meta_data_.total_pages); | |
| 276 ASSERT_EQ(1U, page_meta_data_.click_elements_after_form_fill.size()); | |
| 277 click_elment = page_meta_data_.click_elements_after_form_fill[0]; | |
| 278 EXPECT_EQ("btn1", click_elment.descriptor); | |
| 279 EXPECT_EQ(autofill::WebElementDescriptor::ID, click_elment.retrieval_method); | |
| 280 } | |
| 281 | |
| 282 // Test badly formed XML queries. | |
| 283 TEST_F(AutofillQueryXmlParserTest, ParseErrors) { | |
| 284 // Test no Autofill type. | |
| 285 std::string xml = "<autofillqueryresponse>" | |
| 286 "<field/>" | |
| 287 "</autofillqueryresponse>"; | |
| 288 | |
| 289 ParseQueryXML(xml, false); | |
| 290 | |
| 291 EXPECT_EQ(USE_UPLOAD_RATES, upload_required_); | |
| 292 EXPECT_EQ(0U, field_infos_.size()); | |
| 293 EXPECT_TRUE(experiment_id_.empty()); | |
| 294 | |
| 295 // Test an incorrect Autofill type. | |
| 296 xml = "<autofillqueryresponse>" | |
| 297 "<field autofilltype=\"-1\"/>" | |
| 298 "</autofillqueryresponse>"; | |
| 299 | |
| 300 ParseQueryXML(xml, true); | |
| 301 | |
| 302 EXPECT_EQ(USE_UPLOAD_RATES, upload_required_); | |
| 303 ASSERT_EQ(1U, field_infos_.size()); | |
| 304 // AutofillType was out of range and should be set to NO_SERVER_DATA. | |
| 305 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type); | |
| 306 EXPECT_TRUE(experiment_id_.empty()); | |
| 307 | |
| 308 // Test upper bound for the field type, MAX_VALID_FIELD_TYPE. | |
| 309 field_infos_.clear(); | |
| 310 xml = "<autofillqueryresponse><field autofilltype=\"" + | |
| 311 base::IntToString(MAX_VALID_FIELD_TYPE) + "\"/></autofillqueryresponse>"; | |
| 312 | |
| 313 ParseQueryXML(xml, true); | |
| 314 | |
| 315 EXPECT_EQ(USE_UPLOAD_RATES, upload_required_); | |
| 316 ASSERT_EQ(1U, field_infos_.size()); | |
| 317 // AutofillType was out of range and should be set to NO_SERVER_DATA. | |
| 318 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type); | |
| 319 EXPECT_TRUE(experiment_id_.empty()); | |
| 320 | |
| 321 // Test an incorrect Autofill type. | |
| 322 field_infos_.clear(); | |
| 323 xml = "<autofillqueryresponse>" | |
| 324 "<field autofilltype=\"No Type\"/>" | |
| 325 "</autofillqueryresponse>"; | |
| 326 | |
| 327 // Parse fails but an entry is still added to field_infos_. | |
| 328 ParseQueryXML(xml, false); | |
| 329 | |
| 330 EXPECT_EQ(USE_UPLOAD_RATES, upload_required_); | |
| 331 ASSERT_EQ(1U, field_infos_.size()); | |
| 332 EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type); | |
| 333 EXPECT_TRUE(experiment_id_.empty()); | |
| 334 } | |
| 335 | |
| 336 // Test successfull upload response. | |
| 337 TEST_F(AutofillUploadXmlParserTest, TestSuccessfulResponse) { | |
| 338 ParseUploadXML("<autofilluploadresponse positiveuploadrate=\"0.5\" " | |
| 339 "negativeuploadrate=\"0.3\"/>", | |
| 340 true); | |
| 341 | |
| 342 EXPECT_DOUBLE_EQ(0.5, positive_); | |
| 343 EXPECT_DOUBLE_EQ(0.3, negative_); | |
| 344 } | |
| 345 | |
| 346 // Test failed upload response. | |
| 347 TEST_F(AutofillUploadXmlParserTest, TestFailedResponse) { | |
| 348 ParseUploadXML("<autofilluploadresponse positiveuploadrate=\"\" " | |
| 349 "negativeuploadrate=\"0.3\"/>", | |
| 350 false); | |
| 351 | |
| 352 EXPECT_DOUBLE_EQ(0, positive_); | |
| 353 EXPECT_DOUBLE_EQ(0.3, negative_); // Partially parsed. | |
| 354 negative_ = 0; | |
| 355 | |
| 356 ParseUploadXML("<autofilluploadresponse positiveuploadrate=\"0.5\" " | |
| 357 "negativeuploadrate=\"0.3\"", | |
| 358 false); | |
| 359 | |
| 360 EXPECT_DOUBLE_EQ(0, positive_); | |
| 361 EXPECT_DOUBLE_EQ(0, negative_); | |
| 362 | |
| 363 ParseUploadXML("bad data", false); | |
| 364 | |
| 365 EXPECT_DOUBLE_EQ(0, positive_); | |
| 366 EXPECT_DOUBLE_EQ(0, negative_); | |
| 367 | |
| 368 ParseUploadXML(std::string(), false); | |
| 369 | |
| 370 EXPECT_DOUBLE_EQ(0, positive_); | |
| 371 EXPECT_DOUBLE_EQ(0, negative_); | |
| 372 } | |
| 373 | |
| 374 } // namespace | |
| 375 } // namespace autofill | |
| OLD | NEW |