<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="/js-test-resources/ui-helper.js"></script>
<script src="/resources/js-test-pre.js"></script>
<script src="/resources/payment-request.js"></script>
</head>
<body>
<script>

description("Test basic creation of a PaymentRequest object with an Apple Pay AMS UI payment method.");

window.jsTestIsAsync = true;

function validPaymentMethod() {
    return {
        supportedMethods: 'https://apple.com/ams-ui',
        data: {
            engagementRequest: { value: 42 },
        },
    }
}

function validPaymentDetails() {
    return {
        total: { label: 'Your label', amount: { currency: 'USD', value: '10.00' } },
    }
}

function logAndShouldThrow(setup, test) {
    return activateThen(() => {
        debug("SETUP: " + setup);
        eval(setup);
        shouldThrow(test);
        debug("");
    });
}

function logAndShouldNotThrow(setup, test) {
    return activateThen(() => {
        debug("SETUP: " + setup);
        eval(setup);
        shouldNotThrow(test);
        debug("");
    });
}

async function go() {
    debug("Testing PaymentRequest")
    debug("")
    shouldThrow("new PaymentRequest(null, null)")
    shouldNotThrow("new PaymentRequest([validPaymentMethod()], validPaymentDetails())")
    debug("")

    debug("Testing ApplePayAMSUIRequest.engagementRequest")
    debug("")
    await logAndShouldThrow("paymentMethod = validPaymentMethod(); delete paymentMethod.data.engagementRequest", "new PaymentRequest([paymentMethod], validPaymentDetails())")
    await logAndShouldThrow("paymentMethod = validPaymentMethod(); paymentMethod.data.engagementRequest = ''", "new PaymentRequest([paymentMethod], validPaymentDetails())")
    await logAndShouldThrow("paymentMethod = validPaymentMethod(); paymentMethod.data.engagementRequest = 'invalid'", "new PaymentRequest([paymentMethod], validPaymentDetails())")
    await logAndShouldThrow("paymentMethod = validPaymentMethod(); paymentMethod.data.engagementRequest = null", "new PaymentRequest([paymentMethod], validPaymentDetails())")
    await logAndShouldThrow("paymentMethod = validPaymentMethod(); paymentMethod.data.engagementRequest = 7", "new PaymentRequest([paymentMethod], validPaymentDetails())")
    await logAndShouldThrow("paymentMethod = validPaymentMethod(); paymentMethod.data.engagementRequest = [ ]", "new PaymentRequest([paymentMethod], validPaymentDetails())")
    await logAndShouldThrow("paymentMethod = validPaymentMethod(); paymentMethod.data.engagementRequest = false", "new PaymentRequest([paymentMethod], validPaymentDetails())")
    await logAndShouldNotThrow("paymentMethod = validPaymentMethod(); paymentMethod.data.engagementRequest = { }", "new PaymentRequest([paymentMethod], validPaymentDetails())")
    debug("")

    document.querySelector("button").remove();

    finishJSTest();
}

function clickElement(element) {
    let x = element.offsetLeft + 2;
    let y = element.offsetTop + 2;

    var supportsTouchEvents = "TouchEvent" in window;
    if (supportsTouchEvents && window.testRunner && testRunner.runUIScript) {
        testRunner.runUIScript(`(function() { uiController.singleTapAtPoint(${x}, ${y}, function() { }); })();`, function () { });
    } else if (window.eventSender) {
        eventSender.mouseMoveTo(x, y);
        eventSender.mouseDown();
        eventSender.mouseUp();
    }
}

window.onload = function() {
    clickElement(document.querySelector("button"));
}

</script>
<button onclick='go()'>Go</button>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>
