File: form_field_data_android_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (217 lines) | stat: -rw-r--r-- 8,162 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/android_autofill/browser/form_field_data_android.h"

#include <memory>

#include "base/test/bind.h"
#include "components/android_autofill/browser/android_autofill_bridge_factory.h"
#include "components/android_autofill/browser/form_data_android.h"
#include "components/android_autofill/browser/mock_form_field_data_android_bridge.h"
#include "components/autofill/core/browser/autofill_field.h"
#include "components/autofill/core/browser/autofill_type.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/common/form_field_data.h"
#include "components/autofill/core/common/unique_ids.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace autofill {
namespace {

FormFieldData CreateTestField() {
  FormFieldData f;
  f.set_name(u"SomeName");
  f.set_name_attribute(f.name());
  f.set_id_attribute(u"some_id");
  f.set_form_control_type(FormControlType::kInputText);
  f.set_check_status(FormFieldData::CheckStatus::kChecked);
  return f;
}

class FormFieldDataAndroidTest : public ::testing::Test {
 public:
  FormFieldDataAndroidTest() = default;
  ~FormFieldDataAndroidTest() override = default;

  void SetUp() override {
    AndroidAutofillBridgeFactory::GetInstance()
        .SetFormFieldDataAndroidTestingFactory(base::BindLambdaForTesting(
            [this]() -> std::unique_ptr<FormFieldDataAndroidBridge> {
              auto bridge = std::make_unique<MockFormFieldDataAndroidBridge>();
              last_bridge_ = bridge.get();
              return bridge;
            }));
  }

  void TearDown() override {
    last_bridge_ = nullptr;
    AndroidAutofillBridgeFactory::GetInstance()
        .SetFormFieldDataAndroidTestingFactory({});
  }

 protected:
  MockFormFieldDataAndroidBridge& bridge() { return *last_bridge_; }

 private:
  raw_ptr<MockFormFieldDataAndroidBridge> last_bridge_ = nullptr;
};

// Tests that the equality operator of FieldTypes requires that all FieldTypes
// members match the FieldType it is compared with.
TEST_F(FormFieldDataAndroidTest, FieldTypesEquality) {
  using FieldTypes = FormFieldDataAndroid::FieldTypes;

  const FieldTypes mixed_types(/*heuristic_type=*/USERNAME,
                               /*server_type=*/NAME_FIRST,
                               /*computed_type=*/"NAME_FIRST",
                               /*server_predictions=*/{NAME_FIRST});
  const FieldTypes same_types(/*heuristic_type=*/USERNAME,
                              /*server_type=*/USERNAME,
                              /*computed_type=*/"USERNAME",
                              /*server_predictions=*/{USERNAME});
  EXPECT_NE(mixed_types, USERNAME);
  EXPECT_NE(mixed_types, NAME_FIRST);
  EXPECT_NE(same_types, NAME_FIRST);
  EXPECT_EQ(same_types, USERNAME);
}

// Tests that updating the autofill types calls the Java bridge.
TEST_F(FormFieldDataAndroidTest, UpdateAutofillTypes) {
  FormFieldData field;
  FormFieldDataAndroid field_android(&field);
  EXPECT_CALL(bridge(), UpdateFieldTypes);
  field_android.UpdateFieldTypes(FormFieldDataAndroid::FieldTypes());
}

// Tests that calling `UpdateAutofillTypes` updates the AutofillTypes member.
TEST_F(FormFieldDataAndroidTest, UpdateAutofillTypesUpdatesFieldTypes) {
  FormFieldData field;
  FormFieldDataAndroid field_android(&field);
  EXPECT_NE(field_android.field_types(), NAME_FIRST);
  field_android.UpdateFieldTypes(FormFieldDataAndroid::FieldTypes(NAME_FIRST));
  EXPECT_EQ(field_android.field_types(), NAME_FIRST);
}

// Tests that updating the field value calls the Java bridge and also updates
// the underlying `FormFieldData` object.
TEST_F(FormFieldDataAndroidTest, OnFormFieldDidChange) {
  constexpr std::u16string_view kSampleValue = u"SomeValue";

  FormFieldData field;
  field.set_is_autofilled(true);
  FormFieldDataAndroid field_android(&field);
  EXPECT_CALL(bridge(), UpdateValue(kSampleValue));
  field_android.OnFormFieldDidChange(kSampleValue);
  EXPECT_FALSE(field.is_autofilled());
  EXPECT_EQ(field.value(), kSampleValue);
}

// Tests that updating the field visibility calls the Java bridge and also
// updates the underlying `FormFieldData` object.
TEST_F(FormFieldDataAndroidTest, OnFormFieldVisibilityDidChange) {
  FormFieldData field;
  field.set_is_focusable(false);
  field.set_role(FormFieldData::RoleAttribute::kOther);
  EXPECT_FALSE(field.IsFocusable());

  FormFieldDataAndroid field_android(&field);
  FormFieldData field_copy = field;

  // A field with `is_focusable=true` and a non-presentation role is focusable
  // in Autofill terms and therefore visible in Android Autofill terms.
  EXPECT_CALL(bridge(), UpdateVisible(true));
  field_copy.set_is_focusable(true);
  field_android.OnFormFieldVisibilityDidChange(field_copy);
  EXPECT_TRUE(FormFieldData::DeepEqual(field, field_copy));

  // A field with a presentation role is not focusable in Autofill terms.
  EXPECT_CALL(bridge(), UpdateVisible(false));
  field_copy.set_role(FormFieldData::RoleAttribute::kPresentation);
  field_android.OnFormFieldVisibilityDidChange(field_copy);
  EXPECT_TRUE(FormFieldData::DeepEqual(field, field_copy));
}

// Tests that field similarity checks include name, name_attribute, id_attribute
// and form control type.
TEST_F(FormFieldDataAndroidTest, SimilarFieldsAs) {
  FormFieldData f1 = CreateTestField();
  FormFieldData f2 = CreateTestField();
  FormFieldDataAndroid af(&f1);

  // If the fields are the same, they are also similar.
  EXPECT_TRUE(af.SimilarFieldAs(f2));

  // If names differ, they are not similar.
  f2.set_name(f1.name() + u"x");
  EXPECT_FALSE(af.SimilarFieldAs(f2));

  // If name attributes differ, they are not similar.
  f2 = f1;
  f2.set_name_attribute(f1.name_attribute() + u"x");
  EXPECT_FALSE(af.SimilarFieldAs(f2));

  // If id attributes differ, they are not similar.
  f2 = f1;
  f2.set_id_attribute(f1.id_attribute() + u"x");
  EXPECT_FALSE(af.SimilarFieldAs(f2));

  // If form control types differ, they are not similar.
  f2 = f1;
  f2.set_form_control_type(FormControlType::kInputPassword);
  EXPECT_FALSE(af.SimilarFieldAs(f2));

  // If global ids differ, they are not similar.
  f2 = f1;
  f2.set_renderer_id(FieldRendererId(f1.renderer_id().value() + 1));
  EXPECT_FALSE(af.SimilarFieldAs(f2));
}

// Tests that field similarity checks whether a field is checkable, but not
// whether it is checked.
TEST_F(FormFieldDataAndroidTest, SimilarFieldsAs_Checkable) {
  FormFieldData f1 = CreateTestField();
  FormFieldData f2 = CreateTestField();
  f1.set_check_status(FormFieldData::CheckStatus::kCheckableButUnchecked);
  FormFieldDataAndroid af(&f1);

  // If they are both checkable, they are similar (even if one is checked and
  // the other is not).
  f2.set_check_status(FormFieldData::CheckStatus::kChecked);
  EXPECT_TRUE(af.SimilarFieldAs(f2));

  f2.set_check_status(FormFieldData::CheckStatus::kNotCheckable);
  EXPECT_FALSE(af.SimilarFieldAs(f2));
}

// Tests that field labels are similar if they have the same value or were
// inferred from the same source and that source is not a label tag.
TEST_F(FormFieldDataAndroidTest, SimilarFieldsAs_Labels) {
  FormFieldData f1 = CreateTestField();
  FormFieldData f2 = CreateTestField();
  FormFieldDataAndroid af(&f1);

  f1.set_label(u"SomeLabel");
  f1.set_label_source(FormFieldData::LabelSource::kTdTag);
  f2.set_label(f1.label());
  f2.set_label_source(FormFieldData::LabelSource::kAriaLabel);

  EXPECT_TRUE(af.SimilarFieldAs(f2));

  // Not similar because both label text and label source differ.
  f2.set_label(f1.label() + u"x");
  EXPECT_FALSE(af.SimilarFieldAs(f2));

  // Similar because the label source are equal not not a label tag.
  f2.set_label_source(f1.label_source());
  EXPECT_TRUE(af.SimilarFieldAs(f2));

  // Not similar because the labels differ and the label sources are label tags.
  f1.set_label_source(FormFieldData::LabelSource::kLabelTag);
  f2.set_label_source(FormFieldData::LabelSource::kLabelTag);
  EXPECT_FALSE(af.SimilarFieldAs(f2));
}

}  // namespace
}  // namespace autofill