This source file includes following definitions.
- ParseQueryXML
- negative_
- ParseUploadXML
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include <string>
#include <vector>
#include "base/memory/scoped_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "components/autofill/core/browser/autofill_xml_parser.h"
#include "components/autofill/core/browser/field_types.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/libjingle/source/talk/xmllite/xmlparser.h"
namespace autofill {
namespace {
class AutofillQueryXmlParserTest : public testing::Test {
public:
AutofillQueryXmlParserTest(): upload_required_(USE_UPLOAD_RATES) {};
virtual ~AutofillQueryXmlParserTest() {};
protected:
void ParseQueryXML(const std::string& xml, bool should_succeed) {
AutofillQueryXmlParser parse_handler(&field_infos_,
&upload_required_);
buzz::XmlParser parser(&parse_handler);
parser.Parse(xml.c_str(), xml.length(), true);
EXPECT_EQ(should_succeed, parse_handler.succeeded());
}
std::vector<AutofillServerFieldInfo> field_infos_;
UploadRequired upload_required_;
};
class AutofillUploadXmlParserTest : public testing::Test {
public:
AutofillUploadXmlParserTest(): positive_(0), negative_(0) {};
virtual ~AutofillUploadXmlParserTest() {};
protected:
void ParseUploadXML(const std::string& xml, bool should_succeed) {
AutofillUploadXmlParser parse_handler(&positive_, &negative_);
buzz::XmlParser parser(&parse_handler);
parser.Parse(xml.c_str(), xml.length(), true);
EXPECT_EQ(should_succeed, parse_handler.succeeded());
}
double positive_;
double negative_;
};
TEST_F(AutofillQueryXmlParserTest, BasicQuery) {
std::string xml = "<autofillqueryresponse>"
"<field autofilltype=\"0\" />"
"<field autofilltype=\"1\" />"
"<field autofilltype=\"3\" />"
"<field autofilltype=\"2\" />"
"<field autofilltype=\"61\" defaultvalue=\"default\"/>"
"</autofillqueryresponse>";
ParseQueryXML(xml, true);
EXPECT_EQ(USE_UPLOAD_RATES, upload_required_);
ASSERT_EQ(5U, field_infos_.size());
EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type);
EXPECT_EQ(UNKNOWN_TYPE, field_infos_[1].field_type);
EXPECT_EQ(NAME_FIRST, field_infos_[2].field_type);
EXPECT_EQ(EMPTY_TYPE, field_infos_[3].field_type);
EXPECT_TRUE(field_infos_[3].default_value.empty());
EXPECT_EQ(FIELD_WITH_DEFAULT_VALUE, field_infos_[4].field_type);
EXPECT_EQ("default", field_infos_[4].default_value);
}
TEST_F(AutofillQueryXmlParserTest, TestUploadRequired) {
std::string xml = "<autofillqueryresponse uploadrequired=\"true\">"
"<field autofilltype=\"0\" />"
"</autofillqueryresponse>";
ParseQueryXML(xml, true);
EXPECT_EQ(upload_required_, upload_required_);
ASSERT_EQ(1U, field_infos_.size());
EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type);
field_infos_.clear();
xml = "<autofillqueryresponse uploadrequired=\"false\">"
"<field autofilltype=\"0\" />"
"</autofillqueryresponse>";
ParseQueryXML(xml, true);
EXPECT_EQ(UPLOAD_NOT_REQUIRED, upload_required_);
ASSERT_EQ(1U, field_infos_.size());
EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type);
field_infos_.clear();
xml = "<autofillqueryresponse uploadrequired=\"bad_value\">"
"<field autofilltype=\"0\" />"
"</autofillqueryresponse>";
ParseQueryXML(xml, true);
EXPECT_EQ(USE_UPLOAD_RATES, upload_required_);
ASSERT_EQ(1U, field_infos_.size());
EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type);
}
TEST_F(AutofillQueryXmlParserTest, ParseErrors) {
std::string xml = "<autofillqueryresponse>"
"<field/>"
"</autofillqueryresponse>";
ParseQueryXML(xml, false);
EXPECT_EQ(USE_UPLOAD_RATES, upload_required_);
EXPECT_EQ(0U, field_infos_.size());
xml = "<autofillqueryresponse>"
"<field autofilltype=\"-1\"/>"
"</autofillqueryresponse>";
ParseQueryXML(xml, true);
EXPECT_EQ(USE_UPLOAD_RATES, upload_required_);
ASSERT_EQ(1U, field_infos_.size());
EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type);
field_infos_.clear();
xml = "<autofillqueryresponse><field autofilltype=\"" +
base::IntToString(MAX_VALID_FIELD_TYPE) + "\"/></autofillqueryresponse>";
ParseQueryXML(xml, true);
EXPECT_EQ(USE_UPLOAD_RATES, upload_required_);
ASSERT_EQ(1U, field_infos_.size());
EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type);
field_infos_.clear();
xml = "<autofillqueryresponse>"
"<field autofilltype=\"No Type\"/>"
"</autofillqueryresponse>";
ParseQueryXML(xml, false);
EXPECT_EQ(USE_UPLOAD_RATES, upload_required_);
ASSERT_EQ(1U, field_infos_.size());
EXPECT_EQ(NO_SERVER_DATA, field_infos_[0].field_type);
}
TEST_F(AutofillUploadXmlParserTest, TestSuccessfulResponse) {
ParseUploadXML("<autofilluploadresponse positiveuploadrate=\"0.5\" "
"negativeuploadrate=\"0.3\"/>",
true);
EXPECT_DOUBLE_EQ(0.5, positive_);
EXPECT_DOUBLE_EQ(0.3, negative_);
}
TEST_F(AutofillUploadXmlParserTest, TestFailedResponse) {
ParseUploadXML("<autofilluploadresponse positiveuploadrate=\"\" "
"negativeuploadrate=\"0.3\"/>",
false);
EXPECT_DOUBLE_EQ(0, positive_);
EXPECT_DOUBLE_EQ(0.3, negative_);
negative_ = 0;
ParseUploadXML("<autofilluploadresponse positiveuploadrate=\"0.5\" "
"negativeuploadrate=\"0.3\"",
false);
EXPECT_DOUBLE_EQ(0, positive_);
EXPECT_DOUBLE_EQ(0, negative_);
ParseUploadXML("bad data", false);
EXPECT_DOUBLE_EQ(0, positive_);
EXPECT_DOUBLE_EQ(0, negative_);
ParseUploadXML(std::string(), false);
EXPECT_DOUBLE_EQ(0, positive_);
EXPECT_DOUBLE_EQ(0, negative_);
}
}
}