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