accept-ch-test.js (5582B)
1 const echo = "/client-hints/accept-ch-stickiness/resources/echo-client-hints-received.py"; 2 const accept = "/client-hints/accept-ch-stickiness/resources/accept-ch.html"; 3 const accept_blank = "/client-hints/accept-ch-stickiness/resources/accept-ch-blank.html"; 4 const no_accept = "/client-hints/accept-ch-stickiness/resources/no-accept-ch.html"; 5 const httpequiv_accept = "/client-hints/accept-ch-stickiness/resources/http-equiv-accept-ch.html"; 6 const metaequiv_delegate = "/client-hints/accept-ch-stickiness/resources/meta-equiv-delegate-ch.html"; 7 const expect = "/client-hints/accept-ch-stickiness/resources/expect-client-hints-headers.html" 8 const do_not_expect = "/client-hints/accept-ch-stickiness/resources/do-not-expect-client-hints-headers.html" 9 10 const host_info = get_host_info(); 11 12 function verify_initial_state(initial_url, test_name) { 13 promise_test(t => { 14 return fetch(initial_url).then(r => { 15 assert_equals(r.status, 200) 16 // Verify that the browser did not include client hints in the request 17 // headers when fetching echo-client-hints-received.py. 18 assert_false(r.headers.has("device-memory-received"), 19 "device-memory-received"); 20 assert_false(r.headers.has("device-memory-deprecated-received"), 21 "device-memory-deprecated-received"); 22 }); 23 }, test_name + " precondition: Test that the browser does not have client " + 24 "hints preferences cached"); 25 } 26 27 function verify_iframe_state(expect_url, test_name) { 28 promise_test(t => { 29 return new Promise(resolve => { 30 window.addEventListener('message', t.step_func(function(e) { 31 assert_equals(e.data, "PASS", "message from opened frame"); 32 fetch("/client-hints/accept-ch-stickiness/resources/clear-site-data.html").then(resolve); 33 })); 34 const iframe = document.createElement("iframe"); 35 iframe.src = expect_url; 36 document.body.appendChild(iframe); 37 }); 38 }, test_name + " got client hints according to expectations."); 39 } 40 41 function verify_navigation_state(expect_url, test_name) { 42 promise_test(t => { 43 return new Promise(resolve => { 44 let win; 45 window.addEventListener('message', t.step_func(function(e) { 46 win.close(); 47 assert_equals(e.data, "PASS", "message from opened page"); 48 fetch("/client-hints/accept-ch-stickiness/resources/clear-site-data.html").then(resolve); 49 })); 50 // Open a new window. Verify that the user agent attaches client hints. 51 win = window.open(expect_url); 52 assert_not_equals(win, null, "Popup windows not allowed?"); 53 }); 54 }, test_name + " got client hints according to expectations."); 55 } 56 57 function verify_subresource_state(expect_url, test_name) { 58 promise_test(t => { 59 return new Promise(resolve => { 60 fetch(expect_url).then(response => response.text()).then(t.step_func(text => { 61 assert_true(text.includes("PASS")); 62 fetch("/client-hints/accept-ch-stickiness/resources/clear-site-data.html").then(resolve); 63 })); 64 }); 65 }, test_name + " got client hints according to expectations."); 66 } 67 68 function verify_syncxhr_state(expect_url, test_name) { 69 promise_test(t => { 70 return new Promise(resolve => { 71 const xhr = new XMLHttpRequest(); 72 xhr.onreadystatechange = t.step_func(() => { 73 if (xhr.readyState != XMLHttpRequest.DONE) { 74 return; 75 } 76 assert_true(xhr.responseText.includes("PASS")); 77 fetch("/client-hints/accept-ch-stickiness/resources/clear-site-data.html").then(resolve); 78 }); 79 xhr.open("GET", expect_url, false /* async */); 80 xhr.send(); 81 }); 82 }, test_name + " got client hints according to expectations."); 83 } 84 85 function attempt_set(test_type, accept_url, test_name, test_name_suffix) { 86 promise_test(t => { 87 return new Promise(resolve => { 88 if (test_type == "navigation") { 89 const win = window.open(accept_url); 90 assert_not_equals(win, null, "Popup windows not allowed?"); 91 addEventListener('message', t.step_func(() => { 92 win.close(); 93 resolve(); 94 }), false); 95 } else if (test_type == "iframe") { 96 const iframe = document.createElement("iframe"); 97 iframe.addEventListener('load', t.step_func(() => { 98 resolve(); 99 }), false); 100 iframe.src = accept_url; 101 document.body.appendChild(iframe); 102 } else if (test_type == "subresource") { 103 fetch(accept_url).then(r => { 104 assert_equals(r.status, 200, "subresource response status") 105 // Verify that the browser did not include client hints in the request 106 // headers, just because we can.. 107 assert_false(r.headers.has("device-memory-received"), 108 "device-memory-received", 109 "subresource request had no client hints"); 110 assert_false(r.headers.has("device-memory-deprecated-received"), 111 "device-memory-deprecated-received", 112 "subresource request had no client hints"); 113 resolve(); 114 }); 115 } else { 116 assert_unreached("unknown test type"); 117 } 118 }); 119 }, test_name + " set Accept-CH" + test_name_suffix); 120 } 121 122 const run_test = test => { 123 // First, verify the initial state to make sure that the browser does not have 124 // client hints preferences cached from a previous run of the test. 125 verify_initial_state(test.initial_url, test.name); 126 127 // Then, attempt to set Accept-CH 128 attempt_set(test.type, test.accept_url, test.name, ""); 129 130 // Finally, verify that CH are actually sent (or not) on requests 131 verify_navigation_state(test.expect_url, test.name); 132 };