autofill_field_unittest.cc revision 3240926e260ce088908e02ac07a6cf7b0c0cbf44
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. 4 5#include "base/strings/string_util.h" 6#include "base/strings/utf_string_conversions.h" 7#include "components/autofill/core/browser/autofill_field.h" 8#include "components/autofill/core/browser/autofill_type.h" 9#include "components/autofill/core/browser/field_types.h" 10#include "testing/gtest/include/gtest/gtest.h" 11 12namespace autofill { 13namespace { 14 15TEST(AutofillFieldTest, Type) { 16 AutofillField field; 17 ASSERT_EQ(NO_SERVER_DATA, field.server_type()); 18 ASSERT_EQ(UNKNOWN_TYPE, field.heuristic_type()); 19 20 // |server_type_| is NO_SERVER_DATA, so |heuristic_type_| is returned. 21 EXPECT_EQ(UNKNOWN_TYPE, field.Type().server_type()); 22 23 // Set the heuristic type and check it. 24 field.set_heuristic_type(NAME_FIRST); 25 EXPECT_EQ(NAME_FIRST, field.Type().server_type()); 26 27 // Set the server type and check it. 28 field.set_server_type(ADDRESS_BILLING_LINE1); 29 EXPECT_EQ(ADDRESS_BILLING_LINE1, field.Type().server_type()); 30 31 // Remove the server type to make sure the heuristic type is preserved. 32 field.set_server_type(NO_SERVER_DATA); 33 EXPECT_EQ(NAME_FIRST, field.Type().server_type()); 34} 35 36TEST(AutofillFieldTest, IsEmpty) { 37 AutofillField field; 38 ASSERT_EQ(base::string16(), field.value); 39 40 // Field value is empty. 41 EXPECT_TRUE(field.IsEmpty()); 42 43 // Field value is non-empty. 44 field.value = ASCIIToUTF16("Value"); 45 EXPECT_FALSE(field.IsEmpty()); 46} 47 48TEST(AutofillFieldTest, FieldSignature) { 49 AutofillField field; 50 ASSERT_EQ(base::string16(), field.name); 51 ASSERT_EQ(std::string(), field.form_control_type); 52 53 // Signature is empty. 54 EXPECT_EQ("2085434232", field.FieldSignature()); 55 56 // Field name is set. 57 field.name = ASCIIToUTF16("Name"); 58 EXPECT_EQ("1606968241", field.FieldSignature()); 59 60 // Field form control type is set. 61 field.form_control_type = "text"; 62 EXPECT_EQ("502192749", field.FieldSignature()); 63 64 // Heuristic type does not affect FieldSignature. 65 field.set_heuristic_type(NAME_FIRST); 66 EXPECT_EQ("502192749", field.FieldSignature()); 67 68 // Server type does not affect FieldSignature. 69 field.set_server_type(NAME_LAST); 70 EXPECT_EQ("502192749", field.FieldSignature()); 71} 72 73TEST(AutofillFieldTest, IsFieldFillable) { 74 AutofillField field; 75 ASSERT_EQ(UNKNOWN_TYPE, field.Type().server_type()); 76 77 // Type is unknown. 78 EXPECT_FALSE(field.IsFieldFillable()); 79 80 // Only heuristic type is set. 81 field.set_heuristic_type(NAME_FIRST); 82 EXPECT_TRUE(field.IsFieldFillable()); 83 84 // Only server type is set. 85 field.set_heuristic_type(UNKNOWN_TYPE); 86 field.set_server_type(NAME_LAST); 87 EXPECT_TRUE(field.IsFieldFillable()); 88 89 // Both types set. 90 field.set_heuristic_type(NAME_FIRST); 91 field.set_server_type(NAME_LAST); 92 EXPECT_TRUE(field.IsFieldFillable()); 93} 94 95} // namespace 96} // namespace autofill 97