1 // Copyright 2020 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/data_model/autofill_structured_address_test_utils.h"
6
7 #include <ostream>
8 #include "base/strings/stringprintf.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "components/autofill/core/browser/autofill_type.h"
11
12 namespace autofill {
13 namespace structured_address {
14
15 using AddressComponentTestValues = std::vector<AddressComponentTestValue>;
16
operator <<(std::ostream & out,const AddressComponent & component)17 std::ostream& operator<<(std::ostream& out, const AddressComponent& component) {
18 out << "type=" << component.GetStorageTypeName()
19 << ", value=" << base::UTF16ToUTF8(component.GetValue())
20 << ", status=" << static_cast<int>(component.GetVerificationStatus())
21 << std::endl;
22 for (const auto* sub_component : component.Subcomponents()) {
23 out << "\t" << *sub_component;
24 }
25 return out;
26 }
27
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)28 void TestMerging(
29 AddressComponent* older_component,
30 AddressComponent* newer_component,
31 const std::vector<AddressComponentTestValue>& merge_expectation,
32 bool is_mergeable,
33 int merge_modes,
34 bool newer_was_more_recently_used) {
35 older_component->SetMergeModeForTesting(merge_modes);
36
37 SCOPED_TRACE(is_mergeable);
38 SCOPED_TRACE(merge_modes);
39 SCOPED_TRACE(*older_component);
40 SCOPED_TRACE(*newer_component);
41
42 EXPECT_EQ(is_mergeable,
43 older_component->IsMergeableWithComponent(*newer_component));
44 EXPECT_EQ(is_mergeable, older_component->MergeWithComponent(
45 *newer_component, newer_was_more_recently_used));
46 VerifyTestValues(older_component, merge_expectation);
47 }
48
SetTestValues(AddressComponent * component,const AddressComponentTestValues & test_values,bool finalize)49 void SetTestValues(AddressComponent* component,
50 const AddressComponentTestValues& test_values,
51 bool finalize) {
52 for (const auto& test_value : test_values) {
53 component->SetValueForTypeIfPossible(test_value.type,
54 base::UTF8ToUTF16(test_value.value),
55 test_value.status);
56 }
57 if (finalize)
58 component->CompleteFullTree();
59 }
60
VerifyTestValues(AddressComponent * component,const AddressComponentTestValues test_values)61 void VerifyTestValues(AddressComponent* component,
62 const AddressComponentTestValues test_values) {
63 for (const auto& test_value : test_values) {
64 SCOPED_TRACE(base::StringPrintf(
65 "Failed type=%s, value=%s, status=%d",
66 AutofillType(test_value.type).ToString().c_str(),
67 test_value.value.c_str(), static_cast<int>(test_value.status)));
68
69 EXPECT_EQ(component->GetValueForType(test_value.type),
70 base::UTF8ToUTF16(test_value.value));
71
72 // Omit testing the status if the value is empty.
73 if (!test_value.value.empty()) {
74 EXPECT_EQ(component->GetVerificationStatusForType(test_value.type),
75 test_value.status);
76 }
77 }
78 }
79
80 } // namespace structured_address
81 } // namespace autofill
82