|
| 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 | +<style> |
| 10 | + .content::before { |
| 11 | + content: ""; |
| 12 | + display: block; |
| 13 | + width: 256px; |
| 14 | + height: 256px; |
| 15 | + background-image: url(/images/lcp-256x256.png); |
| 16 | + background-size: contain; |
| 17 | + background-repeat: no-repeat; |
| 18 | + background-position: center; |
| 19 | + } |
| 20 | +</style> |
| 21 | + |
| 22 | +<button id="navigateButton">Click here!</button> |
| 23 | +<div id="container"></div> |
| 24 | + |
| 25 | +<script> |
| 26 | + promise_test(async t => { |
| 27 | + const url = 'soft-nav-1'; |
| 28 | + |
| 29 | + navigateButton.addEventListener('click', async () => { |
| 30 | + const firstIcpPromise = SoftNavigationTestHelper.getPerformanceEntries( |
| 31 | + 'interaction-contentful-paint', |
| 32 | + /* minNumEntries= */ 1); |
| 33 | + |
| 34 | + container.innerHTML = `<div><div id="content">Text</div></div>`; |
| 35 | + history.pushState({}, '', url); |
| 36 | + |
| 37 | + // After the ICP entry for the text is produced, add the 'content' class |
| 38 | + // to cause an image paint for the same node. We expect an ICP entry for |
| 39 | + // this as well. |
| 40 | + await (new SoftNavigationTestHelper(t)).withTimeoutMessage( |
| 41 | + firstIcpPromise, 'Timed out waiting for initial ICP', /*timeout=*/ 3000); |
| 42 | + content.classList.add('content'); |
| 43 | + }, {once: true}); |
| 44 | + |
| 45 | + // Set up the PerformanceObservers before clicking to avoid races, |
| 46 | + // and use unbuffered here so we don't get duplicate entries. |
| 47 | + const softNavPromise = SoftNavigationTestHelper.getPerformanceEntries( |
| 48 | + 'soft-navigation', |
| 49 | + /* minNumEntries= */ 1); |
| 50 | + const icpPromise = SoftNavigationTestHelper.getPerformanceEntries( |
| 51 | + 'interaction-contentful-paint', |
| 52 | + /* minNumEntries= */ 2); |
| 53 | + |
| 54 | + if (test_driver) { |
| 55 | + test_driver.click(navigateButton); |
| 56 | + } |
| 57 | + |
| 58 | + const helper = new SoftNavigationTestHelper(t); |
| 59 | + |
| 60 | + // Check if we detected a soft nav. |
| 61 | + const softNavs = await helper.withTimeoutMessage( |
| 62 | + softNavPromise, "Soft navigation not detected.", /*timeout=*/ 3000); |
| 63 | + assert_equals(softNavs.length, 1, 'Expected exactly one soft navigation.'); |
| 64 | + assert_true( |
| 65 | + softNavs[0].name.endsWith(url), |
| 66 | + `Unexpected Soft Navigation URL. Expected url to end with ${url} but got ${softNavs[0].name}`); |
| 67 | + |
| 68 | + // Check that we get two ICP entries, one for the text and one for the image. |
| 69 | + const icps = await helper.withTimeoutMessage( |
| 70 | + icpPromise, 'ICPs not detected.', /*timeout=*/ 3000); |
| 71 | + assert_equals(icps.length, 2, 'Expected exactly two ICP entries.'); |
| 72 | + |
| 73 | + assert_equals(icps[0].id, 'content', 'Expected first ICP candidate to be "content"'); |
| 74 | + assert_equals(icps[0].url, '', 'Expected first ICP url to be empty'); |
| 75 | + |
| 76 | + assert_equals(icps[1].id, 'content', 'Expected second ICP candidate to be "content"'); |
| 77 | + assert_true( |
| 78 | + icps[1].url.endsWith('/images/lcp-256x256.png'), |
| 79 | + 'Expected second ICP url to be the image url'); |
| 80 | + }, 'Soft Navigation Detection supports adding nodes with image pseudo elements and text') |
| 81 | +</script> |
0 commit comments