File: payment_handler_change_shipping_address_option_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (142 lines) | stat: -rw-r--r-- 6,266 bytes parent folder | download | duplicates (6)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "base/strings/utf_string_conversions.h"
#include "chrome/test/payments/payment_request_platform_browsertest_base.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace payments {
namespace {

constexpr const char* kNoMerchantResponseExpectedOutput =
    "PaymentRequest.show(): changeShipping[Address|Option]() returned: null";
constexpr const char* kPromiseRejectedExpectedOutput =
    "PaymentRequest.show() rejected with: Error for test";
constexpr const char* kExeptionThrownExpectedOutput =
    "PaymentRequest.show() rejected with: Error: Error for test";
constexpr const char* kSuccessfulMerchantResponseExpectedOutput =
    "PaymentRequest.show(): changeShipping[Address|Option]() returned: "
    "{\"error\":\"Error for "
    "test\",\"modifiers\":[{\"data\":{\"soup\":\"potato\"},"
    "\"supportedMethods\":\"https://a.com"
    "\",\"total\":{\"amount\":{\"currency\":\"EUR\",\"value\":\"0.03\"},"
    "\"label\":\"\",\"pending\":false}}],\"paymentMethodErrors\":{\"country\":"
    "\"Unsupported "
    "country\"},\"shippingAddressErrors\":{\"addressLine\":\"\",\"city\":\"\","
    "\"country\":\"US only "
    "shipping\",\"dependentLocality\":\"\",\"organization\":\"\",\"phone\":"
    "\"\",\"postalCode\":\"\",\"recipient\":\"\",\"region\":\"\","
    "\"sortingCode\":\"\"},\"shippingOptions\":[{\"amount\":{\"currency\":"
    "\"JPY\",\"value\":\"0.05\"},\"id\":\"id\",\"label\":\"Shipping "
    "option\",\"selected\":true}],\"total\":{\"currency\":\"GBP\",\"value\":"
    "\"0.02\"}}";

enum class ChangeType { kAddressChange, kOptionChange };

struct TestCase {
  TestCase(const std::string& init_test_code,
           const std::string& expected_output,
           ChangeType change_type)
      : init_test_code(init_test_code),
        expected_output(expected_output),
        change_type(change_type) {}

  ~TestCase() = default;

  const std::string init_test_code;
  const std::string expected_output;
  const ChangeType change_type;
};

class PaymentHandlerChangeShippingAddressOptionTest
    : public PaymentRequestPlatformBrowserTestBase,
      public testing::WithParamInterface<TestCase> {
 protected:
  PaymentHandlerChangeShippingAddressOptionTest() = default;
  ~PaymentHandlerChangeShippingAddressOptionTest() override = default;

  void SetUpOnMainThread() override {
    PaymentRequestPlatformBrowserTestBase::SetUpOnMainThread();
    NavigateTo("a.com", "/change_shipping_address_option.html");
  }

  std::string getTestType() {
    return GetParam().change_type == ChangeType::kOptionChange ? "option"
                                                               : "address";
  }
};

IN_PROC_BROWSER_TEST_P(PaymentHandlerChangeShippingAddressOptionTest, Test) {
  std::string method_name;
  InstallPaymentApp("a.com", "/change_shipping_" + getTestType() + "_app.js",
                    &method_name);

  EXPECT_TRUE(content::ExecJs(GetActiveWebContents(),
                              "delegateShippingAddressToPaymentHandler()"));

  EXPECT_TRUE(
      content::ExecJs(GetActiveWebContents(), GetParam().init_test_code));

  std::string actual_output =
      content::EvalJs(GetActiveWebContents(),
                      "outputChangeShippingAddressOptionReturnValue(request);")
          .ExtractString();

  // The test expectations are hard-coded, but the embedded test server changes
  // its port number in every test, e.g., https://a.com:34548.
  EXPECT_EQ(ClearPortNumber(actual_output), GetParam().expected_output)
      << "When executing " << GetParam().init_test_code;
}

// If the merchant does not have a "shipping(address|option)change" event
// handler, then calling PaymentRequestEvent.changeShipping(Address|Option)() in
// the payment handler will return null.
INSTANTIATE_TEST_SUITE_P(
    NoMerchantResponse,
    PaymentHandlerChangeShippingAddressOptionTest,
    testing::Values(TestCase("initTestNoHandler();",
                             kNoMerchantResponseExpectedOutput,
                             ChangeType::kAddressChange),
                    TestCase("initTestNoHandler();",
                             kNoMerchantResponseExpectedOutput,
                             ChangeType::kOptionChange)));

// If the merchant responds to the "payment(address|option)change" event with a
// rejected promise or throws an "Error" inside the promise, then
// PaymentRequest.show() gets rejected.
INSTANTIATE_TEST_SUITE_P(
    ErrorCases,
    PaymentHandlerChangeShippingAddressOptionTest,
    testing::Values(TestCase("initTestReject('shippingaddresschange')",
                             kPromiseRejectedExpectedOutput,
                             ChangeType::kAddressChange),
                    TestCase("initTestReject('shippingoptionchange')",
                             kPromiseRejectedExpectedOutput,
                             ChangeType::kOptionChange),
                    TestCase("initTestThrow('shippingaddresschange')",
                             kExeptionThrownExpectedOutput,
                             ChangeType::kAddressChange),
                    TestCase("initTestThrow('shippingoptionchange')",
                             kExeptionThrownExpectedOutput,
                             ChangeType::kOptionChange)));

// If the merchant responds to a "payment(address|option)change" event with
// updated details, including modifiers for multiple payment method
// names, then the invoked payment handler receives the updated details,
// except the modifiers for non-matching payment method names.
INSTANTIATE_TEST_SUITE_P(
    MerchantResponse,
    PaymentHandlerChangeShippingAddressOptionTest,
    testing::Values(TestCase("initTestDetails('shippingaddresschange')",
                             kSuccessfulMerchantResponseExpectedOutput,
                             ChangeType::kAddressChange),
                    TestCase("initTestDetails('shippingoptionchange')",
                             kSuccessfulMerchantResponseExpectedOutput,
                             ChangeType::kOptionChange)));

}  // namespace
}  // namespace payments