blob: 3c26302378382ce385ea27c0f09e44e798dff0b6 [file] [log] [blame]
Ben Murdocheb525c52013-07-10 11:40:50 +01001// 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"
Ben Murdoch32409262013-08-07 11:04:47 +01008#include "components/autofill/core/browser/autofill_type.h"
Ben Murdocheb525c52013-07-10 11:40:50 +01009#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.
Ben Murdochbb1529c2013-08-08 10:24:53 +010021 EXPECT_EQ(UNKNOWN_TYPE, field.Type().GetStorableType());
Ben Murdocheb525c52013-07-10 11:40:50 +010022
23 // Set the heuristic type and check it.
24 field.set_heuristic_type(NAME_FIRST);
Ben Murdochbb1529c2013-08-08 10:24:53 +010025 EXPECT_EQ(NAME_FIRST, field.Type().GetStorableType());
26 EXPECT_EQ(NAME, field.Type().group());
Ben Murdocheb525c52013-07-10 11:40:50 +010027
28 // Set the server type and check it.
29 field.set_server_type(ADDRESS_BILLING_LINE1);
Ben Murdochbb1529c2013-08-08 10:24:53 +010030 EXPECT_EQ(ADDRESS_HOME_LINE1, field.Type().GetStorableType());
31 EXPECT_EQ(ADDRESS_BILLING, field.Type().group());
Ben Murdocheb525c52013-07-10 11:40:50 +010032
33 // Remove the server type to make sure the heuristic type is preserved.
34 field.set_server_type(NO_SERVER_DATA);
Ben Murdochbb1529c2013-08-08 10:24:53 +010035 EXPECT_EQ(NAME_FIRST, field.Type().GetStorableType());
36 EXPECT_EQ(NAME, field.Type().group());
Ben Murdocheb525c52013-07-10 11:40:50 +010037}
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;
Ben Murdochbb1529c2013-08-08 10:24:53 +010078 ASSERT_EQ(UNKNOWN_TYPE, field.Type().GetStorableType());
Ben Murdocheb525c52013-07-10 11:40:50 +010079
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