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