1 // Copyright 2018 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 "components/autofill/core/browser/proto/legacy_proto_bridge.h"
6
7 #include "components/autofill/core/browser/proto/api_v1.pb.h"
8 #include "components/autofill/core/browser/proto/password_requirements.pb.h"
9 #include "components/autofill/core/browser/proto/server.pb.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace autofill {
14 namespace {
15
16 using ::testing::ElementsAre;
17 using ::testing::Eq;
18 using ::testing::Property;
19
20 // Makes an arbitrary field metadata proto to be used for testing.
GetFieldMetadata()21 AutofillRandomizedFieldMetadata GetFieldMetadata() {
22 AutofillRandomizedFieldMetadata metadata;
23 AutofillRandomizedValue random_value;
24 random_value.set_encoding_type(AutofillRandomizedValue::BIT_0);
25 random_value.set_encoded_bits("1234");
26 *metadata.mutable_id() = std::move(random_value);
27 return metadata;
28 }
29
30 // Makes an arbitrary form metadata proto to be used for testing.
GetformMetadata()31 AutofillRandomizedFormMetadata GetformMetadata() {
32 AutofillRandomizedFormMetadata metadata;
33 AutofillRandomizedValue random_value;
34 random_value.set_encoding_type(AutofillRandomizedValue::BIT_1);
35 random_value.set_encoded_bits("5678");
36 *metadata.mutable_id() = std::move(random_value);
37 return metadata;
38 }
39
MakeLegacyField(uint32_t signature,const std::string & name,const std::string & type)40 AutofillQueryContents::Form::Field MakeLegacyField(uint32_t signature,
41 const std::string& name,
42 const std::string& type) {
43 AutofillQueryContents::Form::Field field;
44 field.set_signature(signature);
45 field.set_name(name);
46 field.set_type(type);
47 *field.mutable_field_metadata() = GetFieldMetadata();
48 return field;
49 }
50
MakeFieldSuggestion(uint32_t field_signature,uint32_t primary_type_prediction,std::vector<uint32_t> predictions,bool may_use_prefilled_placeholder,PasswordRequirementsSpec password_requirements)51 AutofillQueryResponse::FormSuggestion::FieldSuggestion MakeFieldSuggestion(
52 uint32_t field_signature,
53 uint32_t primary_type_prediction,
54 std::vector<uint32_t> predictions,
55 bool may_use_prefilled_placeholder,
56 PasswordRequirementsSpec password_requirements) {
57 AutofillQueryResponse::FormSuggestion::FieldSuggestion field_suggestion;
58 field_suggestion.set_field_signature(field_signature);
59 field_suggestion.set_primary_type_prediction(primary_type_prediction);
60 for (auto prediction : predictions) {
61 field_suggestion.add_predictions()->set_type(prediction);
62 }
63 field_suggestion.set_may_use_prefilled_placeholder(
64 may_use_prefilled_placeholder);
65 *field_suggestion.mutable_password_requirements() =
66 std::move(password_requirements);
67 return field_suggestion;
68 }
69
TEST(ProtoBridgeTest,TestCreateApiRequestFromLegacyRequest)70 TEST(ProtoBridgeTest, TestCreateApiRequestFromLegacyRequest) {
71 AutofillQueryContents legacy_request;
72 legacy_request.set_client_version("dummy client v1");
73 legacy_request.add_experiments(1234);
74 legacy_request.add_experiments(5678);
75 AutofillQueryContents::Form* new_form = legacy_request.add_form();
76 new_form->set_signature(1234U);
77 *new_form->mutable_form_metadata() = GetformMetadata();
78 *new_form->add_field() = MakeLegacyField(1234U, "First Name", "text");
79 *new_form->add_field() = MakeLegacyField(5678U, "Last Name", "text");
80
81 new_form = legacy_request.add_form();
82 new_form->set_signature(5678U);
83 *new_form->mutable_form_metadata() = GetformMetadata();
84 *new_form->add_field() = MakeLegacyField(1234U, "Street Address", "text");
85 *new_form->add_field() = MakeLegacyField(5678U, "Zip Code", "text");
86
87 AutofillPageQueryRequest api_request =
88 CreateApiRequestFromLegacyRequest(legacy_request);
89
90 EXPECT_EQ(api_request.client_version(), "dummy client v1");
91 EXPECT_EQ(api_request.experiments(0), 1234);
92 EXPECT_EQ(api_request.experiments(1), 5678);
93 EXPECT_EQ(api_request.forms(0).signature(), 1234U);
94 EXPECT_EQ(api_request.forms(0).metadata().id().encoding_type(),
95 AutofillRandomizedValue::BIT_1);
96 EXPECT_EQ(api_request.forms(0).metadata().id().encoded_bits(), "5678");
97 EXPECT_EQ(api_request.forms(1).signature(), 5678U);
98 EXPECT_EQ(api_request.forms(1).metadata().id().encoding_type(),
99 AutofillRandomizedValue::BIT_1);
100 EXPECT_EQ(api_request.forms(1).metadata().id().encoded_bits(), "5678");
101 // Assert fields of form 0.
102 EXPECT_EQ(api_request.forms(0).fields(0).signature(), 1234U);
103 EXPECT_EQ(api_request.forms(0).fields(0).name(), "First Name");
104 EXPECT_EQ(api_request.forms(0).fields(0).control_type(), "text");
105 EXPECT_EQ(api_request.forms(0).fields(0).metadata().id().encoding_type(),
106 AutofillRandomizedValue::BIT_0);
107 EXPECT_EQ(api_request.forms(0).fields(0).metadata().id().encoded_bits(),
108 "1234");
109 EXPECT_EQ(api_request.forms(0).fields(1).signature(), 5678U);
110 EXPECT_EQ(api_request.forms(0).fields(1).name(), "Last Name");
111 EXPECT_EQ(api_request.forms(0).fields(1).control_type(), "text");
112 EXPECT_EQ(api_request.forms(0).fields(1).metadata().id().encoding_type(),
113 AutofillRandomizedValue::BIT_0);
114 EXPECT_EQ(api_request.forms(0).fields(1).metadata().id().encoded_bits(),
115 "1234");
116 // Assert fields of form 1.
117 EXPECT_EQ(api_request.forms(1).fields(0).signature(), 1234U);
118 EXPECT_EQ(api_request.forms(1).fields(0).name(), "Street Address");
119 EXPECT_EQ(api_request.forms(1).fields(0).control_type(), "text");
120 EXPECT_EQ(api_request.forms(1).fields(0).metadata().id().encoding_type(),
121 AutofillRandomizedValue::BIT_0);
122 EXPECT_EQ(api_request.forms(1).fields(0).metadata().id().encoded_bits(),
123 "1234");
124 EXPECT_EQ(api_request.forms(1).fields(1).signature(), 5678U);
125 EXPECT_EQ(api_request.forms(1).fields(1).name(), "Zip Code");
126 EXPECT_EQ(api_request.forms(1).fields(1).control_type(), "text");
127 EXPECT_EQ(api_request.forms(1).fields(1).metadata().id().encoding_type(),
128 AutofillRandomizedValue::BIT_0);
129 EXPECT_EQ(api_request.forms(1).fields(1).metadata().id().encoded_bits(),
130 "1234");
131 }
132
TEST(ProtoBridgeTest,CreateLegacyResponseFromApiResponse)133 TEST(ProtoBridgeTest, CreateLegacyResponseFromApiResponse) {
134 constexpr uint32_t dummy_password_type = 1U;
135 constexpr uint32_t dummy_address_type = 2U;
136 constexpr uint32_t dummy_password_priority = 3U;
137
138 PasswordRequirementsSpec dummy_password_requirement_specs;
139 dummy_password_requirement_specs.set_priority(dummy_password_priority);
140
141 AutofillQueryResponse api_response;
142 // Add suggestions for form 0.
143 auto* form_suggestion = api_response.add_form_suggestions();
144 *form_suggestion->add_field_suggestions() = MakeFieldSuggestion(
145 /*field_signature=*/1234U,
146 /*primary_type_prediction=*/dummy_password_type,
147 /*predictions=*/{dummy_password_type, dummy_address_type},
148 /*may_use_prefilled_placeholder=*/true,
149 /*password_requirements=*/dummy_password_requirement_specs);
150 // Add suggestions for form 1.
151 form_suggestion = api_response.add_form_suggestions();
152 *form_suggestion->add_field_suggestions() = MakeFieldSuggestion(
153 /*field_signature=*/5678U, /*primary_type_prediction=*/dummy_address_type,
154 /*predictions=*/{dummy_address_type, dummy_password_type},
155 /*may_use_prefilled_placeholder=*/false,
156 /*password_requirements=*/dummy_password_requirement_specs);
157
158 AutofillQueryResponseContents legacy_response =
159 CreateLegacyResponseFromApiResponse(api_response);
160
161 // Assert fields of form 0 in legacy response.
162 EXPECT_EQ(legacy_response.field(0).overall_type_prediction(),
163 dummy_password_type);
164 EXPECT_THAT(
165 legacy_response.field(0).predictions(),
166 ElementsAre(
167 Property(&AutofillQueryResponseContents::Field::FieldPrediction::type,
168 Eq(dummy_password_type)),
169 Property(&AutofillQueryResponseContents::Field::FieldPrediction::type,
170 Eq(dummy_address_type))));
171 EXPECT_THAT(legacy_response.field(0).password_requirements(),
172 Property(&PasswordRequirementsSpec::priority,
173 Eq(dummy_password_priority)));
174
175 // Assert fields of form 1 in legacy response.
176 EXPECT_EQ(legacy_response.field(1).overall_type_prediction(),
177 dummy_address_type);
178 EXPECT_THAT(
179 legacy_response.field(1).predictions(),
180 ElementsAre(
181 Property(&AutofillQueryResponseContents::Field::FieldPrediction::type,
182 Eq(dummy_address_type)),
183 Property(&AutofillQueryResponseContents::Field::FieldPrediction::type,
184 Eq(dummy_password_type))));
185 EXPECT_THAT(legacy_response.field(1).password_requirements(),
186 Property(&PasswordRequirementsSpec::priority,
187 Eq(dummy_password_priority)));
188 }
189
190 } // namespace
191 } // namespace autofill
192