summaryrefslogtreecommitdiff
path: root/chromium/components/autofill/core/browser/data_model/autofill_structured_address_test_utils.cc
blob: 3d8f0a1b8487d53bcac7ffea8b28c692d3c36792 (plain)
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
// Copyright 2020 The Chromium Authors. All rights reserved.
// 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/data_model/autofill_structured_address_test_utils.h"

#include <ostream>
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/autofill_type.h"

namespace autofill {
namespace structured_address {

using AddressComponentTestValues = std::vector<AddressComponentTestValue>;

std::ostream& operator<<(std::ostream& out, const AddressComponent& component) {
  out << "type=" << component.GetStorageTypeName()
      << ", value=" << base::UTF16ToUTF8(component.GetValue())
      << ", status=" << static_cast<int>(component.GetVerificationStatus())
      << std::endl;
  for (const auto* sub_component : component.Subcomponents()) {
    out << "\t" << *sub_component;
  }
  return out;
}

void TestMerging(
    AddressComponent* older_component,
    AddressComponent* newer_component,
    const std::vector<AddressComponentTestValue>& merge_expectation,
    bool is_mergeable,
    int merge_modes,
    bool newer_was_more_recently_used) {
  older_component->SetMergeModeForTesting(merge_modes);

  SCOPED_TRACE(is_mergeable);
  SCOPED_TRACE(merge_modes);
  SCOPED_TRACE(*older_component);
  SCOPED_TRACE(*newer_component);

  EXPECT_EQ(is_mergeable,
            older_component->IsMergeableWithComponent(*newer_component));
  EXPECT_EQ(is_mergeable, older_component->MergeWithComponent(
                              *newer_component, newer_was_more_recently_used));
  VerifyTestValues(older_component, merge_expectation);
}

void SetTestValues(AddressComponent* component,
                   const AddressComponentTestValues& test_values,
                   bool finalize) {
  for (const auto& test_value : test_values) {
    component->SetValueForTypeIfPossible(test_value.type,
                                         base::UTF8ToUTF16(test_value.value),
                                         test_value.status);
  }
  if (finalize)
    component->CompleteFullTree();
}

void VerifyTestValues(AddressComponent* component,
                      const AddressComponentTestValues test_values) {
  for (const auto& test_value : test_values) {
    SCOPED_TRACE(base::StringPrintf(
        "Failed type=%s, value=%s, status=%d",
        AutofillType(test_value.type).ToString().c_str(),
        test_value.value.c_str(), static_cast<int>(test_value.status)));

    EXPECT_EQ(base::UTF16ToUTF8(component->GetValueForType(test_value.type)),
              test_value.value);

    // Omit testing the status if the value is empty.
    if (!test_value.value.empty()) {
      EXPECT_EQ(component->GetVerificationStatusForType(test_value.type),
                test_value.status);
    }
  }
}

}  // namespace structured_address
}  // namespace autofill