text-node-modifications.html (3276B)
1 <!DOCTYPE html> 2 <meta charset="utf-8" /> 3 <script src="/resources/testharness.js"></script> 4 <script src="/resources/testharnessreport.js"></script> 5 <script src="/resources/testdriver.js"></script> 6 <script src="/resources/testdriver-vendor.js"></script> 7 <script src="/soft-navigation-heuristics/resources/soft-navigation-test-helper.js"></script> 8 9 <div id="divTarget">Initial Text</div> 10 <div><input type="button" value="Initial Input Text" id="inputTarget" style="width: 300px"></input></div> 11 <div><button id="navigateButton">Navigate!</button></div> 12 13 <script> 14 const textNode = divTarget.childNodes[0]; 15 16 async function runTest(t, url, modifyText, targetId) { 17 navigateButton.addEventListener('click', () => { 18 modifyText(); 19 history.pushState({}, '', url); 20 }, {once: true}); 21 22 // Set up the PerformanceObservers before clicking to avoid races. 23 const softNavPromise = 24 SoftNavigationTestHelper.getPerformanceEntries('soft-navigation'); 25 const icpPromise = 26 SoftNavigationTestHelper.getPerformanceEntries('interaction-contentful-paint'); 27 28 if (test_driver) { 29 test_driver.click(navigateButton); 30 } 31 32 const helper = new SoftNavigationTestHelper(t); 33 34 const softNavs = await helper.withTimeoutMessage( 35 softNavPromise, 'Soft navigation not detected.', /*timeout=*/ 3000); 36 assert_equals(softNavs.length, 1, 'Expected exactly one soft navigation.'); 37 assert_true( 38 softNavs[0].name.endsWith(url), 39 `Unexpected Soft Navigation URL. Expected url to end with ${url} but got ${softNavs[0].name}`); 40 41 const icps = await helper.withTimeoutMessage( 42 icpPromise, 'ICP not detected.', /*timeout=*/ 3000); 43 assert_equals(icps.length, 1, 'Expected exactly one ICP entry.'); 44 assert_equals(icps[0].id, targetId, `Expected ICP candidate to be "${targetId}"`); 45 } 46 47 promise_test(t => { 48 const url = '/nodeValue'; 49 const modifyText = () => { 50 textNode.nodeValue = "New text set via .nodeValue"; 51 }; 52 return runTest(t, url, modifyText, 'divTarget'); 53 }, 'Soft Navigation Detection supports replacing node text via .nodeValue'); 54 55 promise_test(t => { 56 const url = '/data'; 57 const modifyText = () => { 58 textNode.data= 'New text set via .data'; 59 }; 60 return runTest(t, url, modifyText, 'divTarget'); 61 }, 'Soft Navigation Detection supports replacing node text via .data'); 62 63 promise_test(t => { 64 const url = '/textContent'; 65 const modifyText = () => { 66 textNode.textContent = 'New text set via .textContent'; 67 }; 68 return runTest(t, url, modifyText, 'divTarget'); 69 }, 'Soft Navigation Detection supports replacing node text via .textContent'); 70 71 promise_test(t => { 72 const url = '/value'; 73 const modifyText = () => { 74 inputTarget.value = 'Input text set via .value'; 75 }; 76 return runTest(t, url, modifyText, 'inputTarget'); 77 }, 'Soft Navigation Detection supports replacing input text via .input'); 78 79 promise_test(t => { 80 const url = '/value-attribute'; 81 const modifyText = () => { 82 inputTarget.setAttribute('value', 'Input text set via .value (attr)'); 83 }; 84 return runTest(t, url, modifyText, 'inputTarget'); 85 }, 'Soft Navigation Detection supports replacing input text via setAttribute(value)'); 86 </script>