<!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>
</head>
<body>
<script>

description("Test basic creation of an ApplePaySession object.");

window.jsTestIsAsync = true;

function validRequest() {
    return {
          countryCode: 'US',
          currencyCode: 'USD',
          supportedNetworks: ['visa', 'masterCard', 'carteBancaire'],
          merchantCapabilities: ['supports3DS'],
          total: { label: 'Your Label', amount: '10.00' },
    }
}

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

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

function go() {
    debug("Testing supportsVersion");
    debug("");
    shouldThrow("ApplePaySession.supportsVersion(0)");
    shouldBeTrue("ApplePaySession.supportsVersion(1)");
    shouldBeTrue("ApplePaySession.supportsVersion(2)");
    shouldBeTrue("ApplePaySession.supportsVersion(3)");
    debug("");

    debug("Testing PaymentRequest (v3)")
    debug("");
    shouldThrow("new ApplePaySession(3, { })");
    shouldNotThrow("new ApplePaySession(3, validRequest())");
    debug("");
    
    debug("Testing PaymentRequest.supportedCountries");
    debug("");

    logAndShouldThrow("request = validRequest(); request.supportedCountries = ['invalid'];", "new ApplePaySession(3, request)");
    logAndShouldThrow("request = validRequest(); request.supportedCountries = ['US', 'invalid'];", "new ApplePaySession(3, request)");
    logAndShouldThrow("request = validRequest(); request.supportedCountries = ['US', 7];", "new ApplePaySession(3, request)");
    logAndShouldThrow("request = validRequest(); request.supportedCountries = '';", "new ApplePaySession(3, request)");
    logAndShouldThrow("request = validRequest(); request.supportedCountries = 7;", "new ApplePaySession(3, request)");
    logAndShouldThrow("request = validRequest(); request.supportedCountries = null;", "new ApplePaySession(3, request)");
    logAndShouldNotThrow("request = validRequest(); delete request.supportedCountries;", "new ApplePaySession(3, request)");
    logAndShouldNotThrow("request = validRequest(); request.supportedCountries = undefined;", "new ApplePaySession(3, request)");
    logAndShouldNotThrow("request = validRequest(); request.supportedCountries = [];", "new ApplePaySession(3, request)");
    logAndShouldNotThrow("request = validRequest(); request.supportedCountries = ['US'];", "new ApplePaySession(3, request)");
    logAndShouldNotThrow("request = validRequest(); request.supportedCountries = ['US', 'US'];", "new ApplePaySession(3, request)");
    logAndShouldNotThrow("request = validRequest(); request.requiredBillingContactFields = ['phoneticName'];", "new ApplePaySession(3, request)")
    logAndShouldNotThrow("request = validRequest(); request.requiredShippingContactFields = ['phoneticName'];", "new ApplePaySession(3, request)")
    debug("");

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

    finishJSTest();
}

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

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