Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(294)

Side by Side Diff: components/payments/content/payment_request_spec_unittest.cc

Issue 2742813004: [Payments] Refactor into PaymentRequestState and Spec (Closed)
Patch Set: don't stop rebasin' Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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/payments/content/payment_request_spec.h"
6
7 #include <utility>
8
9 #include "base/memory/weak_ptr.h"
10 #include "components/payments/content/payment_request.mojom.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace payments {
14
15 class PaymentRequestSpecTest : public testing::Test,
16 public PaymentRequestSpec::Observer {
17 protected:
18 ~PaymentRequestSpecTest() override {}
19
20 void OnInvalidSpecProvided() override {
21 on_invalid_spec_provided_called_ = true;
22 }
23
24 void RecreateSpecWithMethodData(
25 std::vector<payments::mojom::PaymentMethodDataPtr> method_data) {
26 spec_ = base::MakeUnique<PaymentRequestSpec>(nullptr, nullptr,
27 std::move(method_data), this);
28 }
29
30 PaymentRequestSpec* spec() { return spec_.get(); }
31 bool on_invalid_spec_provided_called() {
32 return on_invalid_spec_provided_called_;
33 }
34
35 private:
36 std::unique_ptr<PaymentRequestSpec> spec_;
37 bool on_invalid_spec_provided_called_ = false;
38 };
39
40 // Test that empty method data notifies observers of an invalid spec.
41 TEST_F(PaymentRequestSpecTest, EmptyMethodData) {
42 std::vector<mojom::PaymentMethodDataPtr> method_data;
43 RecreateSpecWithMethodData(std::move(method_data));
44 EXPECT_TRUE(on_invalid_spec_provided_called());
45
46 // No supported card networks.
47 EXPECT_EQ(0u, spec()->supported_card_networks().size());
48 }
49
50 // Test that parsing supported methods (with invalid values and duplicates)
51 // works as expected.
52 TEST_F(PaymentRequestSpecTest, SupportedMethods) {
53 mojom::PaymentMethodDataPtr entry = mojom::PaymentMethodData::New();
54 entry->supported_methods.push_back("visa");
55 entry->supported_methods.push_back("mastercard");
56 entry->supported_methods.push_back("invalid");
57 entry->supported_methods.push_back("");
58 entry->supported_methods.push_back("visa");
59 std::vector<mojom::PaymentMethodDataPtr> method_data;
60 method_data.push_back(std::move(entry));
61
62 RecreateSpecWithMethodData(std::move(method_data));
63 EXPECT_FALSE(on_invalid_spec_provided_called());
64
65 // Only "visa" and "mastercard" remain, in order.
66 EXPECT_EQ(2u, spec()->supported_card_networks().size());
67 EXPECT_EQ("visa", spec()->supported_card_networks()[0]);
68 EXPECT_EQ("mastercard", spec()->supported_card_networks()[1]);
69 }
70
71 // Test that parsing supported methods in different method data entries (with
72 // invalid values and duplicates) works as expected.
73 TEST_F(PaymentRequestSpecTest, SupportedMethods_MultipleEntries) {
74 mojom::PaymentMethodDataPtr entry = mojom::PaymentMethodData::New();
75 entry->supported_methods.push_back("visa");
76 mojom::PaymentMethodDataPtr entry2 = mojom::PaymentMethodData::New();
77 entry2->supported_methods.push_back("mastercard");
78 mojom::PaymentMethodDataPtr entry3 = mojom::PaymentMethodData::New();
79 entry3->supported_methods.push_back("invalid");
80
81 std::vector<mojom::PaymentMethodDataPtr> method_data;
82 method_data.push_back(std::move(entry));
83 method_data.push_back(std::move(entry2));
84 method_data.push_back(std::move(entry3));
85
86 RecreateSpecWithMethodData(std::move(method_data));
87 EXPECT_FALSE(on_invalid_spec_provided_called());
88
89 // Only "visa" and "mastercard" remain, in order.
90 EXPECT_EQ(2u, spec()->supported_card_networks().size());
91 EXPECT_EQ("visa", spec()->supported_card_networks()[0]);
92 EXPECT_EQ("mastercard", spec()->supported_card_networks()[1]);
93 }
94
95 // Test that parsing supported methods in different method data entries fails as
96 // soon as one entry doesn't specify anything in supported_methods.
97 TEST_F(PaymentRequestSpecTest, SupportedMethods_MultipleEntries_OneEmpty) {
98 // First entry is valid.
99 mojom::PaymentMethodDataPtr entry = mojom::PaymentMethodData::New();
100 entry->supported_methods.push_back("visa");
101 // Empty method data entry.
102 mojom::PaymentMethodDataPtr entry2 = mojom::PaymentMethodData::New();
103 // Valid one follows the empty.
104 mojom::PaymentMethodDataPtr entry3 = mojom::PaymentMethodData::New();
105 entry3->supported_methods.push_back("mastercard");
106
107 std::vector<mojom::PaymentMethodDataPtr> method_data;
108 method_data.push_back(std::move(entry));
109 method_data.push_back(std::move(entry2));
110 method_data.push_back(std::move(entry3));
111
112 RecreateSpecWithMethodData(std::move(method_data));
113 EXPECT_TRUE(on_invalid_spec_provided_called());
114
115 // Visa was parsed, but not mastercard.
116 EXPECT_EQ(1u, spec()->supported_card_networks().size());
117 EXPECT_EQ("visa", spec()->supported_card_networks()[0]);
118 }
119
120 // Test that only specifying basic-card means that all are supported.
121 TEST_F(PaymentRequestSpecTest, SupportedMethods_OnlyBasicCard) {
122 mojom::PaymentMethodDataPtr entry = mojom::PaymentMethodData::New();
123 entry->supported_methods.push_back("basic-card");
124 std::vector<mojom::PaymentMethodDataPtr> method_data;
125 method_data.push_back(std::move(entry));
126
127 RecreateSpecWithMethodData(std::move(method_data));
128 EXPECT_FALSE(on_invalid_spec_provided_called());
129
130 // All of the basic card networks are supported.
131 EXPECT_EQ(8u, spec()->supported_card_networks().size());
132 EXPECT_EQ("amex", spec()->supported_card_networks()[0]);
133 EXPECT_EQ("diners", spec()->supported_card_networks()[1]);
134 EXPECT_EQ("discover", spec()->supported_card_networks()[2]);
135 EXPECT_EQ("jcb", spec()->supported_card_networks()[3]);
136 EXPECT_EQ("mastercard", spec()->supported_card_networks()[4]);
137 EXPECT_EQ("mir", spec()->supported_card_networks()[5]);
138 EXPECT_EQ("unionpay", spec()->supported_card_networks()[6]);
139 EXPECT_EQ("visa", spec()->supported_card_networks()[7]);
140 }
141
142 // Test that specifying a method AND basic-card means that all are supported,
143 // but with the method as first.
144 TEST_F(PaymentRequestSpecTest, SupportedMethods_BasicCard_WithSpecificMethod) {
145 mojom::PaymentMethodDataPtr entry = mojom::PaymentMethodData::New();
146 entry->supported_methods.push_back("jcb");
147 entry->supported_methods.push_back("basic-card");
148 std::vector<mojom::PaymentMethodDataPtr> method_data;
149 method_data.push_back(std::move(entry));
150
151 RecreateSpecWithMethodData(std::move(method_data));
152 EXPECT_FALSE(on_invalid_spec_provided_called());
153
154 // All of the basic card networks are supported, but JCB is first because it
155 // was specified first.
156 EXPECT_EQ(8u, spec()->supported_card_networks().size());
157 EXPECT_EQ("jcb", spec()->supported_card_networks()[0]);
158 EXPECT_EQ("amex", spec()->supported_card_networks()[1]);
159 EXPECT_EQ("diners", spec()->supported_card_networks()[2]);
160 EXPECT_EQ("discover", spec()->supported_card_networks()[3]);
161 EXPECT_EQ("mastercard", spec()->supported_card_networks()[4]);
162 EXPECT_EQ("mir", spec()->supported_card_networks()[5]);
163 EXPECT_EQ("unionpay", spec()->supported_card_networks()[6]);
164 EXPECT_EQ("visa", spec()->supported_card_networks()[7]);
165 }
166
167 // Test that specifying basic-card with a supported network (with previous
168 // supported methods) will work as expected
169 TEST_F(PaymentRequestSpecTest, SupportedMethods_BasicCard_Overlap) {
170 mojom::PaymentMethodDataPtr entry = mojom::PaymentMethodData::New();
171 entry->supported_methods.push_back("mastercard");
172 entry->supported_methods.push_back("visa");
173 // Visa and mastercard are repeated, but in reverse order.
174 mojom::PaymentMethodDataPtr entry2 = mojom::PaymentMethodData::New();
175 entry2->supported_methods.push_back("basic-card");
176 entry2->supported_networks.push_back(mojom::BasicCardNetwork::VISA);
177 entry2->supported_networks.push_back(mojom::BasicCardNetwork::MASTERCARD);
178 entry2->supported_networks.push_back(mojom::BasicCardNetwork::UNIONPAY);
179 std::vector<mojom::PaymentMethodDataPtr> method_data;
180 method_data.push_back(std::move(entry));
181 method_data.push_back(std::move(entry2));
182
183 RecreateSpecWithMethodData(std::move(method_data));
184 EXPECT_FALSE(on_invalid_spec_provided_called());
185
186 EXPECT_EQ(3u, spec()->supported_card_networks().size());
187 EXPECT_EQ("mastercard", spec()->supported_card_networks()[0]);
188 EXPECT_EQ("visa", spec()->supported_card_networks()[1]);
189 EXPECT_EQ("unionpay", spec()->supported_card_networks()[2]);
190 }
191
192 // Test that specifying basic-card with supported networks after specifying
193 // some methods
194 TEST_F(PaymentRequestSpecTest,
195 SupportedMethods_BasicCard_WithSupportedNetworks) {
196 mojom::PaymentMethodDataPtr entry = mojom::PaymentMethodData::New();
197 entry->supported_methods.push_back("basic-card");
198 entry->supported_networks.push_back(mojom::BasicCardNetwork::VISA);
199 entry->supported_networks.push_back(mojom::BasicCardNetwork::UNIONPAY);
200 std::vector<mojom::PaymentMethodDataPtr> method_data;
201 method_data.push_back(std::move(entry));
202
203 RecreateSpecWithMethodData(std::move(method_data));
204 EXPECT_FALSE(on_invalid_spec_provided_called());
205
206 // Only the specified networks are supported.
207 EXPECT_EQ(2u, spec()->supported_card_networks().size());
208 EXPECT_EQ("visa", spec()->supported_card_networks()[0]);
209 EXPECT_EQ("unionpay", spec()->supported_card_networks()[1]);
210 }
211
212 } // namespace payments
OLDNEW
« no previous file with comments | « components/payments/content/payment_request_spec.cc ('k') | components/payments/content/payment_request_state.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698