File: test_votes_uploader.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (101 lines) | stat: -rw-r--r-- 4,267 bytes parent folder | download | duplicates (5)
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
// Copyright 2024 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/test_votes_uploader.h"

#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/crowdsourcing/votes_uploader_test_api.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace autofill {

TestVotesUploader::TestVotesUploader(AutofillClient* client)
    : VotesUploader(client) {}
TestVotesUploader::~TestVotesUploader() = default;

void TestVotesUploader::UploadVote(
    std::unique_ptr<FormStructure> submitted_form,
    std::vector<AutofillUploadContents> upload_contents,
    base::TimeTicks initial_interaction_timestamp,
    base::TimeTicks submission_timestamp,
    bool observed_submission,
    const std::u16string& last_unlocked_credit_card_cvc,
    const ukm::SourceId source_id) {
  submitted_form_signature_ = submitted_form->FormSignatureAsStr();

  if (expected_observed_submission_ != std::nullopt) {
    EXPECT_EQ(expected_observed_submission_, observed_submission);
  }

  // If we have expected field types set, make sure they match.
  if (!expected_submitted_field_types_.empty()) {
    ASSERT_EQ(expected_submitted_field_types_.size(),
              submitted_form->field_count());
    for (size_t i = 0; i < expected_submitted_field_types_.size(); ++i) {
      SCOPED_TRACE(base::StringPrintf(
          "Field %d with value %s", static_cast<int>(i),
          base::UTF16ToUTF8(submitted_form->field(i)->value()).c_str()));
      const FieldTypeSet& possible_types =
          submitted_form->field(i)->possible_types();
      EXPECT_EQ(expected_submitted_field_types_[i].size(),
                possible_types.size());
      for (auto it : expected_submitted_field_types_[i]) {
        EXPECT_TRUE(possible_types.count(it))
            << "Expected type: " << FieldTypeToStringView(it);
      }
    }
  }

  last_uploaded_form_associations_ = {};
  for (const AutofillUploadContents& upload : upload_contents) {
    if (!last_uploaded_form_associations_.last_address_form_submitted &&
        upload.has_last_address_form_submitted()) {
      last_uploaded_form_associations_.last_address_form_submitted =
          FormSignature(upload.last_address_form_submitted());
    }
    if (!last_uploaded_form_associations_.second_last_address_form_submitted &&
        upload.has_second_last_address_form_submitted()) {
      last_uploaded_form_associations_.second_last_address_form_submitted =
          FormSignature(upload.second_last_address_form_submitted());
    }
    if (!last_uploaded_form_associations_.last_credit_card_form_submitted &&
        upload.last_credit_card_form_submitted()) {
      last_uploaded_form_associations_.last_credit_card_form_submitted =
          FormSignature(upload.last_credit_card_form_submitted());
    }
  }

  VotesUploader::UploadVote(
      std::move(submitted_form), std::move(upload_contents),
      initial_interaction_timestamp, submission_timestamp, observed_submission,
      last_unlocked_credit_card_cvc, source_id);
}

bool TestVotesUploader::MaybeStartVoteUploadProcess(
    std::unique_ptr<FormStructure> form,
    bool observed_submission,
    LanguageCode current_page_language,
    base::TimeTicks initial_interaction_timestamp,
    const std::u16string& last_unlocked_credit_card_cvc,
    ukm::SourceId ukm_source_id) {
  if (VotesUploader::MaybeStartVoteUploadProcess(
          std::move(form), observed_submission, current_page_language,
          initial_interaction_timestamp, last_unlocked_credit_card_cvc,
          ukm_source_id)) {
    // The purpose of this runloop is to ensure that the field type
    // determination finishes.
    base::RunLoop run_loop;
    // Since the `vote_upload_task_runner()` is a `base::SequencedTaskRunner`,
    // the `run_loop` is quit only after the task and reply posted by
    // MaybeStartVoteUploadProcess() is finished.
    test_api(*this).task_runner().PostTaskAndReply(FROM_HERE, base::DoNothing(),
                                                   run_loop.QuitClosure());
    run_loop.Run();
    return true;
  }
  return false;
}

}  // namespace autofill