Phantom focus/blur events fire on clicking between text input fields when listening...
[WebKit-https.git] / Source / WebCore / testing / MockPaymentCoordinator.h
1 /*
2  * Copyright (C) 2017-2018 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #pragma once
27
28 #if ENABLE(APPLE_PAY)
29
30 #include "ApplePayLineItem.h"
31 #include "ApplePayShippingMethod.h"
32 #include "MockPaymentAddress.h"
33 #include "MockPaymentError.h"
34 #include "PaymentCoordinatorClient.h"
35 #include <wtf/HashSet.h>
36 #include <wtf/text/StringHash.h>
37
38 namespace WebCore {
39
40 class Page;
41 struct ApplePayPaymentMethod;
42
43 class MockPaymentCoordinator final : public PaymentCoordinatorClient {
44 public:
45     explicit MockPaymentCoordinator(Page&);
46
47     void setCanMakePayments(bool canMakePayments) { m_canMakePayments = canMakePayments; }
48     void setCanMakePaymentsWithActiveCard(bool canMakePaymentsWithActiveCard) { m_canMakePaymentsWithActiveCard = canMakePaymentsWithActiveCard; }
49     void setShippingAddress(MockPaymentAddress&& shippingAddress) { m_shippingAddress = WTFMove(shippingAddress); }
50     void changeShippingOption(String&& shippingOption);
51     void changePaymentMethod(ApplePayPaymentMethod&&);
52     void acceptPayment();
53     void cancelPayment();
54
55     const ApplePayLineItem& total() const { return m_total; }
56     const Vector<ApplePayLineItem>& lineItems() const { return m_lineItems; }
57     const Vector<MockPaymentError>& errors() const { return m_errors; }
58     const Vector<ApplePayShippingMethod>& shippingMethods() const { return m_shippingMethods; }
59
60     void ref() const { }
61     void deref() const { }
62
63 private:
64     bool supportsVersion(unsigned) final;
65     std::optional<String> validatedPaymentNetwork(const String&) final;
66     bool canMakePayments() final;
67     void canMakePaymentsWithActiveCard(const String&, const String&, WTF::Function<void(bool)>&&);
68     void openPaymentSetup(const String&, const String&, WTF::Function<void(bool)>&&);
69     bool showPaymentUI(const URL&, const Vector<URL>&, const ApplePaySessionPaymentRequest&) final;
70     void completeMerchantValidation(const PaymentMerchantSession&) final;
71     void completeShippingMethodSelection(std::optional<ShippingMethodUpdate>&&) final;
72     void completeShippingContactSelection(std::optional<ShippingContactUpdate>&&) final;
73     void completePaymentMethodSelection(std::optional<PaymentMethodUpdate>&&) final;
74     void completePaymentSession(std::optional<PaymentAuthorizationResult>&&) final;
75     void abortPaymentSession() final;
76     void cancelPaymentSession() final;
77     void paymentCoordinatorDestroyed() final;
78
79     bool isMockPaymentCoordinator() const final { return true; }
80
81     void updateTotalAndLineItems(const ApplePaySessionPaymentRequest::TotalAndLineItems&);
82
83     Page& m_page;
84     bool m_canMakePayments { true };
85     bool m_canMakePaymentsWithActiveCard { true };
86     ApplePayPaymentContact m_shippingAddress;
87     ApplePayLineItem m_total;
88     Vector<ApplePayLineItem> m_lineItems;
89     Vector<MockPaymentError> m_errors;
90     Vector<ApplePayShippingMethod> m_shippingMethods;
91     HashSet<String, ASCIICaseInsensitiveHash> m_availablePaymentNetworks;
92 };
93
94 } // namespace WebCore
95
96 SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::MockPaymentCoordinator)
97     static bool isType(const WebCore::PaymentCoordinatorClient& paymentCoordinatorClient) { return paymentCoordinatorClient.isMockPaymentCoordinator(); }
98 SPECIALIZE_TYPE_TRAITS_END()
99
100 #endif // ENABLE(APPLE_PAY)