autofill_field_unittest.cc revision bb1529ce867d8845a77ec7cdf3e3003ef1771a40
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().GetStorableType()); 22 23 // Set the heuristic type and check it. 24 field.set_heuristic_type(NAME_FIRST); 25 EXPECT_EQ(NAME_FIRST, field.Type().GetStorableType()); 26 EXPECT_EQ(NAME, field.Type().group()); 27 28 // Set the server type and check it. 29 field.set_server_type(ADDRESS_BILLING_LINE1); 30 EXPECT_EQ(ADDRESS_HOME_LINE1, field.Type().GetStorableType()); 31 EXPECT_EQ(ADDRESS_BILLING, field.Type().group()); 32 33 // Remove the server type to make sure the heuristic type is preserved. 34 field.set_server_type(NO_SERVER_DATA); 35 EXPECT_EQ(NAME_FIRST, field.Type().GetStorableType()); 36 EXPECT_EQ(NAME, field.Type().group()); 37} 38 39TEST(AutofillFieldTest, IsEmpty) { 40 AutofillField field; 41 ASSERT_EQ(base::string16(), field.value); 42 43 // Field value is empty. 44 EXPECT_TRUE(field.IsEmpty()); 45 46 // Field value is non-empty. 47 field.value = ASCIIToUTF16("Value"); 48 EXPECT_FALSE(field.IsEmpty()); 49} 50 51TEST(AutofillFieldTest, FieldSignature) { 52 AutofillField field; 53 ASSERT_EQ(base::string16(), field.name); 54 ASSERT_EQ(std::string(), field.form_control_type); 55 56 // Signature is empty. 57 EXPECT_EQ("2085434232", field.FieldSignature()); 58 59 // Field name is set. 60 field.name = ASCIIToUTF16("Name"); 61 EXPECT_EQ("1606968241", field.FieldSignature()); 62 63 // Field form control type is set. 64 field.form_control_type = "text"; 65 EXPECT_EQ("502192749", field.FieldSignature()); 66 67 // Heuristic type does not affect FieldSignature. 68 field.set_heuristic_type(NAME_FIRST); 69 EXPECT_EQ("502192749", field.FieldSignature()); 70 71 // Server type does not affect FieldSignature. 72 field.set_server_type(NAME_LAST); 73 EXPECT_EQ("502192749", field.FieldSignature()); 74} 75 76TEST(AutofillFieldTest, IsFieldFillable) { 77 AutofillField field; 78 ASSERT_EQ(UNKNOWN_TYPE, field.Type().GetStorableType()); 79 80 // Type is unknown. 81 EXPECT_FALSE(field.IsFieldFillable()); 82 83 // Only heuristic type is set. 84 field.set_heuristic_type(NAME_FIRST); 85 EXPECT_TRUE(field.IsFieldFillable()); 86 87 // Only server type is set. 88 field.set_heuristic_type(UNKNOWN_TYPE); 89 field.set_server_type(NAME_LAST); 90 EXPECT_TRUE(field.IsFieldFillable()); 91 92 // Both types set. 93 field.set_heuristic_type(NAME_FIRST); 94 field.set_server_type(NAME_LAST); 95 EXPECT_TRUE(field.IsFieldFillable()); 96} 97 98} // namespace 99} // namespace autofill 100