| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/payments/content/payment_request_spec.h" | 5 #include "components/payments/content/payment_request_spec.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 10 #include "components/payments/content/payment_request.mojom.h" | 10 #include "components/payments/content/payment_request.mojom.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 12 |
| 13 namespace payments { | 13 namespace payments { |
| 14 | 14 |
| 15 class PaymentRequestSpecTest : public testing::Test, | 15 class PaymentRequestSpecTest : public testing::Test, |
| 16 public PaymentRequestSpec::Observer { | 16 public PaymentRequestSpec::Observer { |
| 17 protected: | 17 protected: |
| 18 ~PaymentRequestSpecTest() override {} | 18 ~PaymentRequestSpecTest() override {} |
| 19 | 19 |
| 20 void OnInvalidSpecProvided() override { | 20 void OnInvalidSpecProvided() override { |
| 21 on_invalid_spec_provided_called_ = true; | 21 on_invalid_spec_provided_called_ = true; |
| 22 } | 22 } |
| 23 void OnSpecUpdated() override { on_spec_updated_called_ = true; } |
| 23 | 24 |
| 24 void RecreateSpecWithMethodData( | 25 void RecreateSpecWithMethodData( |
| 25 std::vector<payments::mojom::PaymentMethodDataPtr> method_data) { | 26 std::vector<mojom::PaymentMethodDataPtr> method_data) { |
| 26 spec_ = base::MakeUnique<PaymentRequestSpec>( | 27 spec_ = base::MakeUnique<PaymentRequestSpec>( |
| 27 nullptr, nullptr, std::move(method_data), this, "en-US"); | 28 mojom::PaymentOptions::New(), mojom::PaymentDetails::New(), |
| 29 std::move(method_data), this, "en-US"); |
| 30 } |
| 31 |
| 32 void RecreateSpecWithOptionsAndDetails(mojom::PaymentOptionsPtr options, |
| 33 mojom::PaymentDetailsPtr details) { |
| 34 spec_ = base::MakeUnique<PaymentRequestSpec>( |
| 35 std::move(options), std::move(details), |
| 36 std::vector<mojom::PaymentMethodDataPtr>(), this, "en-US"); |
| 28 } | 37 } |
| 29 | 38 |
| 30 PaymentRequestSpec* spec() { return spec_.get(); } | 39 PaymentRequestSpec* spec() { return spec_.get(); } |
| 31 bool on_invalid_spec_provided_called() { | 40 bool on_invalid_spec_provided_called() { |
| 32 return on_invalid_spec_provided_called_; | 41 return on_invalid_spec_provided_called_; |
| 33 } | 42 } |
| 34 | 43 |
| 35 private: | 44 private: |
| 36 std::unique_ptr<PaymentRequestSpec> spec_; | 45 std::unique_ptr<PaymentRequestSpec> spec_; |
| 37 bool on_invalid_spec_provided_called_ = false; | 46 bool on_invalid_spec_provided_called_ = false; |
| 47 bool on_spec_updated_called_ = false; |
| 38 }; | 48 }; |
| 39 | 49 |
| 40 // Test that empty method data notifies observers of an invalid spec. | 50 // Test that empty method data notifies observers of an invalid spec. |
| 41 TEST_F(PaymentRequestSpecTest, EmptyMethodData) { | 51 TEST_F(PaymentRequestSpecTest, EmptyMethodData) { |
| 42 std::vector<mojom::PaymentMethodDataPtr> method_data; | 52 std::vector<mojom::PaymentMethodDataPtr> method_data; |
| 43 RecreateSpecWithMethodData(std::move(method_data)); | 53 RecreateSpecWithMethodData(std::move(method_data)); |
| 44 EXPECT_TRUE(on_invalid_spec_provided_called()); | 54 EXPECT_TRUE(on_invalid_spec_provided_called()); |
| 45 | 55 |
| 46 // No supported card networks. | 56 // No supported card networks. |
| 47 EXPECT_EQ(0u, spec()->supported_card_networks().size()); | 57 EXPECT_EQ(0u, spec()->supported_card_networks().size()); |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 | 269 |
| 260 RecreateSpecWithMethodData(std::move(method_data)); | 270 RecreateSpecWithMethodData(std::move(method_data)); |
| 261 EXPECT_FALSE(on_invalid_spec_provided_called()); | 271 EXPECT_FALSE(on_invalid_spec_provided_called()); |
| 262 | 272 |
| 263 // Only the specified networks are supported. | 273 // Only the specified networks are supported. |
| 264 EXPECT_EQ(2u, spec()->supported_card_networks().size()); | 274 EXPECT_EQ(2u, spec()->supported_card_networks().size()); |
| 265 EXPECT_EQ("visa", spec()->supported_card_networks()[0]); | 275 EXPECT_EQ("visa", spec()->supported_card_networks()[0]); |
| 266 EXPECT_EQ("unionpay", spec()->supported_card_networks()[1]); | 276 EXPECT_EQ("unionpay", spec()->supported_card_networks()[1]); |
| 267 } | 277 } |
| 268 | 278 |
| 279 // Test that the last shipping option is selected, even in the case of |
| 280 // updateWith. |
| 281 TEST_F(PaymentRequestSpecTest, ShippingOptionsSelection) { |
| 282 std::vector<mojom::PaymentShippingOptionPtr> shipping_options; |
| 283 mojom::PaymentShippingOptionPtr option = mojom::PaymentShippingOption::New(); |
| 284 option->id = "option:1"; |
| 285 option->selected = false; |
| 286 shipping_options.push_back(std::move(option)); |
| 287 mojom::PaymentShippingOptionPtr option2 = mojom::PaymentShippingOption::New(); |
| 288 option2->id = "option:2"; |
| 289 option2->selected = true; |
| 290 shipping_options.push_back(std::move(option2)); |
| 291 mojom::PaymentDetailsPtr details = mojom::PaymentDetails::New(); |
| 292 details->shipping_options = std::move(shipping_options); |
| 293 |
| 294 mojom::PaymentOptionsPtr options = mojom::PaymentOptions::New(); |
| 295 options->request_shipping = true; |
| 296 RecreateSpecWithOptionsAndDetails(std::move(options), std::move(details)); |
| 297 |
| 298 EXPECT_EQ("option:2", spec()->selected_shipping_option()->id); |
| 299 |
| 300 std::vector<mojom::PaymentShippingOptionPtr> new_shipping_options; |
| 301 mojom::PaymentShippingOptionPtr new_option = |
| 302 mojom::PaymentShippingOption::New(); |
| 303 new_option->id = "option:1"; |
| 304 new_option->selected = false; |
| 305 shipping_options.push_back(std::move(new_option)); |
| 306 mojom::PaymentShippingOptionPtr new_option2 = |
| 307 mojom::PaymentShippingOption::New(); |
| 308 new_option2->id = "option:2"; |
| 309 new_option2->selected = true; |
| 310 new_shipping_options.push_back(std::move(new_option2)); |
| 311 mojom::PaymentDetailsPtr new_details = mojom::PaymentDetails::New(); |
| 312 new_details->shipping_options = std::move(new_shipping_options); |
| 313 |
| 314 spec()->UpdateWith(std::move(new_details)); |
| 315 } |
| 316 |
| 269 } // namespace payments | 317 } // namespace payments |
| OLD | NEW |