<!DOCTYPE html>
<title>Test ctrl+click for automatic beacons</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/utils.js"></script>
<script src="/common/dispatcher/dispatcher.js"></script>
<script src="resources/utils.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<body>
<script>
promise_test(async(t) => {
const fencedframe = await attachFencedFrameContext(
{generator_api: 'fledge', register_beacon: true});
const new_url = new URL("wpt_internal/fenced_frame/resources/dummy.html",
get_host_info().HTTPS_NOTSAMESITE_ORIGIN);
const beacon_data = "This is the beacon data!";
const beacon_type = "reserved.top_navigation_commit";
await fencedframe.execute((new_url, beacon_data, beacon_type) => {
let a = document.createElement('a');
a.textContent = "Click me!";
a.href = new_url;
a.target = "_blank";
// When the anchor link is clicked, the click handler will set the data
// before the navigation happens.
a.onclick = () => {
let beacon_event = {
eventType: beacon_type,
eventData: beacon_data,
destination: ["buyer"],
}
window.fence.setReportEventDataForAutomaticBeacons(beacon_event);
};
document.body.appendChild(a);
assert_not_equals(eventSender, null);
var rect = a.getBoundingClientRect();
eventSender.mouseMoveTo(rect.x+rect.width/2, rect.y+rect.height/2);
const isMac = navigator.userAgent.search(/\bMac OS X\b/) != -1;
eventSender.mouseDown(0, [isMac ? 'metaKey' : 'ctrlKey']);
eventSender.mouseUp(0, [isMac ? 'metaKey' : 'ctrlKey']);
}, [new_url, beacon_data, beacon_type]);
const [received_beacon_origin, received_beacon_referrer] =
await nextBeacon(beacon_type, beacon_data).then(data => data.split(","));
assert_equals(received_beacon_origin, location.origin);
assert_equals(received_beacon_referrer, location.origin + "/");
// Also test automatic beacons with middle clicks
await fencedframe.execute(() => {
eventSender.mouseDown(1);
eventSender.mouseUp(1);
}, []);
const [middle_click_nav_received_beacon_origin,
middle_click_nav_received_beacon_referrer] = await
nextBeacon(beacon_type, beacon_data).then(data => data.split(","));
assert_equals(middle_click_nav_received_beacon_origin, location.origin);
assert_equals(middle_click_nav_received_beacon_referrer,
location.origin + "/");
}, 'Trigger an automatic beacon from ctrl+clicking a link.');
</script>
</body>