chromium/components/autofill/core/browser/crowdsourcing/autofill_crowdsourcing_encoding_unittest.cc

// 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/autofill/core/browser/crowdsourcing/autofill_crowdsourcing_encoding.h"

#include <algorithm>
#include <functional>
#include <memory>
#include <string>
#include <vector>

#include "base/base64.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "components/autofill/core/browser/autofill_field.h"
#include "components/autofill/core/browser/autofill_form_test_utils.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/form_parsing/form_field_parser.h"
#include "components/autofill/core/browser/proto/api_v1.pb.h"
#include "components/autofill/core/browser/randomized_encoder.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_prefs.h"
#include "components/autofill/core/common/autofill_test_utils.h"
#include "components/autofill/core/common/form_data.h"
#include "components/autofill/core/common/form_data_test_api.h"
#include "components/autofill/core/common/form_field_data.h"
#include "components/autofill/core/common/html_field_types.h"
#include "components/autofill/core/common/signatures.h"
#include "components/autofill/core/common/unique_ids.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/testing_pref_service.h"
#include "components/version_info/version_info.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/re2/src/re2/re2.h"
#include "url/gurl.h"

namespace autofill {

namespace {

SubmissionIndicatorEvent;
SubmissionSource;
AddFieldPredictionsToForm;
AddFieldPredictionToForm;
CreateFieldPrediction;
CreateTestFormField;
ASCIIToUTF16;
AllOf;
AnyOf;
Each;
ElementsAre;
IsEmpty;
Matcher;
Not;
Pointee;
Property;
Truly;
GetProductNameAndVersionForUserAgent;
FieldPrediction;

// Helper struct to specify manual overrides.
struct ManualOverride {};

// Matches any protobuf `actual` whose serialization is equal to the
// string-serialization of the protobuf `expected`.
template <typename T>
auto SerializesSameAs(const T& expected) {}

template <typename... Matchers>
auto ElementsSerializeSameAs(Matchers... element_matchers) {}

template <typename... Matchers>
auto UnorderedElementsSerializeSameAs(Matchers... element_matchers) {}

Matcher<FieldPrediction> EqualsPrediction(const FieldPrediction& prediction) {}

Matcher<FieldPrediction> EqualsPrediction(FieldType type) {}

#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
Matcher<FieldPrediction> EqualsPrediction(FieldType type,
                                          FieldPrediction::Source source) {}
#endif

std::string SerializeAndEncode(const AutofillQueryResponse& response) {}

void AddFieldOverrideToForm(
    autofill::FormFieldData field_data,
    FieldType field_type,
    ::autofill::AutofillQueryResponse_FormSuggestion* form_suggestion) {}

#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
// Creates the override specification passed as a parameter to
// `features::test::kAutofillOverridePredictions`.
std::string CreateManualOverridePrediction(
    const std::vector<ManualOverride>& overrides) {}
#endif

}  // namespace

class AutofillCrowdsourcingEncoding : public testing::Test {};

TEST_F(AutofillCrowdsourcingEncoding,
       EncodeUploadRequest_SubmissionIndicatorEvents_Match) {}

TEST_F(AutofillCrowdsourcingEncoding, EncodeUploadRequest) {}

TEST_F(AutofillCrowdsourcingEncoding,
       EncodeUploadRequestWithAdditionalPasswordFormSignature) {}

TEST_F(AutofillCrowdsourcingEncoding, EncodeUploadRequestWithPropertiesMask) {}

TEST_F(AutofillCrowdsourcingEncoding,
       EncodeUploadRequest_ObservedSubmissionFalse) {}

TEST_F(AutofillCrowdsourcingEncoding, EncodeUploadRequest_WithLabels) {}
// Tests the EMAIL_ADDRESS exception on `FirstNonCapturedType`, where email is
// not required as part of the `contained_types`.
TEST_F(AutofillCrowdsourcingEncoding,
       EncodeUploadRequest_EmailFieldContentMatched) {}

// Tests that when the form is the result of flattening multiple forms into one,
// EncodeUploadRequest() returns multiple uploads: one for the entire form and
// one for each of the original forms.
TEST_F(AutofillCrowdsourcingEncoding, EncodeUploadRequest_WithSubForms) {}

// Check that we compute the "datapresent" string correctly for the given
// |available_types|.
TEST_F(AutofillCrowdsourcingEncoding, CheckDataPresence) {}

TEST_F(AutofillCrowdsourcingEncoding, CheckMultipleTypes) {}

TEST_F(AutofillCrowdsourcingEncoding, EncodeUploadRequest_PasswordsRevealed) {}

TEST_F(AutofillCrowdsourcingEncoding, EncodeUploadRequest_IsFormTag) {}

TEST_F(AutofillCrowdsourcingEncoding, EncodeUploadRequest_RichMetadata) {}

TEST_F(AutofillCrowdsourcingEncoding, Metadata_OnlySendFullUrlWithUserConsent) {}

TEST_F(AutofillCrowdsourcingEncoding,
       EncodeUploadRequest_WithSingleUsernameVoteType) {}

TEST_F(AutofillCrowdsourcingEncoding,
       EncodeUploadRequest_WithSingleUsernameData) {}

// Checks that CreateForPasswordManagerUpload builds FormStructure
// which is encodable (i.e. ready for uploading).
TEST_F(AutofillCrowdsourcingEncoding, CreateForPasswordManagerUpload) {}

// Milestone number must be set to correct actual value, as autofill server
// relies on this. If this is planning to change, inform Autofill team. This
// must be set to avoid situations similar to dropping branch number in M101,
// which yielded cl/513794193 and cl/485660167.
TEST_F(AutofillCrowdsourcingEncoding, EncodeUploadRequest_MilestoneSet) {}

TEST_F(AutofillCrowdsourcingEncoding,
       EncodeUploadRequest_SetsInitialValueChanged) {}

// Tests that Autofill does not send votes for a field that was filled with
// fallback.
TEST_F(AutofillCrowdsourcingEncoding,
       EncodeUploadRequest_SkipFieldsFilledWithFallback) {}

TEST_F(AutofillCrowdsourcingEncoding, EncodeAutofillPageQueryRequest) {}

TEST_F(AutofillCrowdsourcingEncoding, SkipFieldTest) {}

TEST_F(AutofillCrowdsourcingEncoding,
       EncodeAutofillPageQueryRequest_WithLabels) {}

TEST_F(AutofillCrowdsourcingEncoding,
       EncodeAutofillPageQueryRequest_WithLongLabels) {}

// One name is missing from one field.
TEST_F(AutofillCrowdsourcingEncoding,
       EncodeAutofillPageQueryRequest_MissingNames) {}

TEST_F(AutofillCrowdsourcingEncoding, AllowBigForms) {}

// Test that server overrides get precedence over HTML types.
TEST_F(AutofillCrowdsourcingEncoding,
       ParseQueryResponse_ServerPredictionIsOverride) {}

// Test the heuristic prediction for NAME_LAST_SECOND overrides server
// predictions.
TEST_F(AutofillCrowdsourcingEncoding,
       ParseQueryResponse_HeuristicsOverrideSpanishLastNameTypes) {}

// Test the heuristic prediction for ADDRESS_HOME_STREET_NAME and
// ADDRESS_HOME_HOUSE_NUMBER overrides server predictions.
TEST_F(AutofillCrowdsourcingEncoding,
       ParseQueryResponse_HeuristicsOverrideStreetNameAndHouseNumberTypes) {}

// Tests proper resolution heuristic, server and html field types when the
// server returns NO_SERVER_DATA, UNKNOWN_TYPE, and a valid type.
TEST_F(AutofillCrowdsourcingEncoding, ParseQueryResponse_TooManyTypes) {}

// Tests proper resolution heuristic, server and html field types when the
// server returns NO_SERVER_DATA, UNKNOWN_TYPE, and a valid type.
TEST_F(AutofillCrowdsourcingEncoding, ParseQueryResponse_UnknownType) {}

// Tests that precedence of server's query response is indeed: Main frame
// overrides > iframe overrides > main frame crowdsourcing > iframe
// crowdsourcing
TEST_F(
    AutofillCrowdsourcingEncoding,
    ParseServerPredictionsQueryResponse_PrecedenceRulesBetweenMainFrameAndIframe) {}

TEST_F(AutofillCrowdsourcingEncoding,
       ParseQueryResponse_MergeAutofillAndPasswordsPredictions) {}

// Tests that the signatures of a field's FormFieldData::host_form_signature are
// used as a fallback if the form's signature does not contain useful type
// predictions.
TEST_F(AutofillCrowdsourcingEncoding,
       ParseServerPredictionsQueryResponse_FallbackToHostFormSignature) {}

TEST_F(AutofillCrowdsourcingEncoding, ParseServerPredictionsQueryResponse) {}

#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
// Tests that manually specified (i.e. passed as a feature parameter) field type
// predictions override server predictions.
TEST_F(AutofillCrowdsourcingEncoding,
       ParseServerPredictionsQueryResponseWithManualOverrides) {}

// Tests that specifying manual field type prediction overrides also works in
// the absence of any server predictions.
TEST_F(
    AutofillCrowdsourcingEncoding,
    ParseServerPredictionsQueryResponseWithManualOverridesAndNoServerPredictions) {}

// Tests that (in the case of colliding form and field signatures) specifying a
// pass-through (i.e. no prediction at all) in the last override for that
// form / field signature pair leads to defaulting back to server predictions
// at that position and all other fields with the same form / field signature
// pair that follow.
TEST_F(
    AutofillCrowdsourcingEncoding,
    ParseServerPredictionsQueryResponseWithManualOverridesAndPassthroughInLastPosition) {}

// Tests that (in the case of colliding form and field signatures) specifying a
// pass-through (i.e. no prediction at all) in a middle override for that
// form / field signature pair leads to defaulting back to server predictions
// only for that middle field.
TEST_F(
    AutofillCrowdsourcingEncoding,
    ParseServerPredictionsQueryResponseWithManualOverridesAndPassthroughInMiddlePosition) {}

// Tests that manually specified (i.e. passed as a feature parameter)
// alternative_form_signature based field type predictions override
// alternative_form_signature server predictions.
TEST_F(AutofillCrowdsourcingEncoding,
       ParseServerPredictionsQueryResponseOverridesAlternativeFormSignature) {}

// Tests that manually specified (i.e. passed as a feature parameter)
// alternative_form_signature based field type predictions override
// form_signature server predictions.
TEST_F(
    AutofillCrowdsourcingEncoding,
    ParseServerPredictionsQueryResponseServerOverridesAlternativeFormSignature) {}

// Tests that server overrides have lower priority than manual overrides.
TEST_F(
    AutofillCrowdsourcingEncoding,
    ParseServerPredictionsQueryResponseReplaceServerOverrideWithManualOverride) {}
#endif

// Tests ParseServerPredictionsQueryResponse when the payload cannot be parsed
// to an AutofillQueryResponse where we expect an early return of the function.
TEST_F(AutofillCrowdsourcingEncoding,
       ParseServerPredictionsQueryResponseWhenCannotParseProtoFromString) {}

// Tests ParseServerPredictionsQueryResponse when the payload is not base64
// where we expect an early return of the function.
TEST_F(AutofillCrowdsourcingEncoding,
       ParseServerPredictionsQueryResponseWhenPayloadNotBase64) {}

TEST_F(AutofillCrowdsourcingEncoding, ParseQueryResponse_AuthorDefinedTypes) {}

// Tests that, when the flag is off, we will not set the predicted type to
// unknown for fields that have no server data and autocomplete off, and when
// the flag is ON, we will overwrite the predicted type.
TEST_F(AutofillCrowdsourcingEncoding,
       NoServerData_AutocompleteOff_FlagDisabled_NoOverwrite) {}

// Tests that we never overwrite the CVC heuristic-predicted type, even if there
// is no server data (votes) for every CC fields.
TEST_F(AutofillCrowdsourcingEncoding, NoServerDataCCFields_CVC_NoOverwrite) {}

// Tests that we never overwrite the CVC heuristic-predicted type, even if there
// is server data (votes) for every other CC fields.
TEST_F(AutofillCrowdsourcingEncoding, WithServerDataCCFields_CVC_NoOverwrite) {}

// When two fields have the same signature and the server response has multiple
// predictions for that signature, apply the server predictions in the order
// that they were received.
TEST_F(AutofillCrowdsourcingEncoding, ParseQueryResponse_RankEqualSignatures) {}

// When two fields have the same signature and the server response has one
// prediction, apply the prediction to every field with that signature.
TEST_F(AutofillCrowdsourcingEncoding,
       ParseQueryResponse_EqualSignaturesFewerPredictions) {}

// Test that experimental server predictions are not used.
TEST_F(AutofillCrowdsourcingEncoding,
       ExperimentalServerPredictionsAreSeparate) {}

}  // namespace autofill