Set test conditions closer to conversion redirect in LayoutTests/http/tests/adClickAt...
[WebKit-https.git] / LayoutTests / http / tests / adClickAttribution / send-attribution-conversion-request.html
1 <!DOCTYPE html> <!-- webkit-test-runner [ useFlexibleViewport=true internal:AdClickAttributionEnabled=true ] -->
2 <html lang="en">
3 <head>
4     <meta charset="UTF-8">
5     <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
6     <script src="/js-test-resources/ui-helper.js"></script>
7 </head>
8 <body onload="setTimeout(runTest, 0)">
9 <div id="description">Tests sending of ad click attribution requests after a conversion.</div>
10 <a id="targetLink" href="http://localhost:8000/adClickAttribution/send-attribution-conversion-request.html?stepTwo" adcampaignid="3" addestination="http://localhost:8000">Link</a><br>
11 <div id="output"></div>
12 <script>
13     const currentTimeMillis = (new Date()).getTime();
14     const highEntropyBits = currentTimeMillis - (Math.floor(currentTimeMillis / 1000000) * 1000000);
15     const nonce = highEntropyBits + "" + Math.floor(Math.random() * 100);
16
17     if (window.testRunner) {
18         testRunner.waitUntilDone();
19         testRunner.dumpChildFramesAsText();
20         testRunner.setAllowsAnySSLCertificate(true);
21     }
22
23     function activateElement(elementID) {
24         var element = document.getElementById(elementID);
25         var centerX = element.offsetLeft + element.offsetWidth / 2;
26         var centerY = element.offsetTop + element.offsetHeight / 2;
27         UIHelper.activateAt(centerX, centerY).then(
28             function () {
29             },
30             function () {
31                 document.getElementById("output").innerText = "FAIL Promise rejected.";
32                 testRunner.notifyDone();
33             }
34         );
35     }
36
37     function appendConversionDataIframeAndFinish() {
38         testRunner.dumpAdClickAttribution();
39         document.body.removeChild(document.getElementById("targetLink"));
40         document.body.removeChild(document.getElementById("pixel"));
41
42         let iframeElement = document.createElement("iframe");
43         iframeElement.src = "http://127.0.0.1:8000/adClickAttribution/resources/getConversionData.php?endTest=true&timeout_ms=2000&nonce=" + nonce;
44         document.body.appendChild(iframeElement);
45     }
46
47     function runTest() {
48         if (window.testRunner) {
49             if (window.location.search === "?stepTwo") {
50                 let imageElement = document.createElement("img");
51                 imageElement.src = "https://127.0.0.1:8443/adClickAttribution/resources/redirectToConversion.php?conversionData=12&nonce=" + nonce;
52                 imageElement.id = "pixel";
53                 imageElement.onerror = function() {
54                     appendConversionDataIframeAndFinish();
55                 };
56                 testRunner.setAdClickAttributionConversionURLForTesting("http://127.0.0.1:8000/adClickAttribution/resources/conversionReport.php?nonce=" + nonce);
57                 testRunner.setAdClickAttributionOverrideTimerForTesting(true);
58                 document.body.appendChild(imageElement);
59             } else {
60                 activateElement("targetLink");
61             }
62         } else {
63             document.getElementById("output").innerText = "FAIL No testRunner.";
64         }
65     }
66 </script>
67 </body>
68 </html>