element-timing-helpers.js (4362B)
1 // Common checks between checkElement() and checkElementWithoutResourceTiming(). 2 function checkElementInternal(entry, expectedUrl, expectedIdentifier, expectedID, beforeRender, 3 expectedElement) { 4 assert_equals(entry.entryType, 'element', 'entryType does not match'); 5 assert_equals(entry.url, expectedUrl, 'url does not match'); 6 assert_equals(entry.identifier, expectedIdentifier, 'identifier does not match'); 7 if (beforeRender != 0) { 8 // In this case, renderTime is not 0. 9 assert_greater_than(entry.renderTime, 0, 'renderTime should be nonzero'); 10 assert_equals(entry.startTime, entry.renderTime, 'startTime should equal renderTime'); 11 } else { 12 // In this case, renderTime is 0, so compare to loadTime. 13 assert_equals(entry.renderTime, 0, 'renderTime should be zero'); 14 assert_equals(entry.startTime, entry.loadTime, 'startTime should equal loadTime'); 15 } 16 assert_equals(entry.duration, 0, 'duration should be 0'); 17 assert_equals(entry.id, expectedID, 'id does not match'); 18 assert_greater_than_equal(entry.renderTime, beforeRender, 'renderTime greater than beforeRender'); 19 assert_greater_than_equal(entry.paintTime, beforeRender, 'paintTime should represent the time when the UA started painting'); 20 21 // PaintTimingMixin 22 if ("presentationTime" in entry && entry.presentationTime !== null) { 23 assert_greater_than(entry.presentationTime, entry.paintTime); 24 assert_equals(entry.presentationTime, entry.renderTime); 25 } else { 26 assert_equals(entry.renderTime, entry.paintTime); 27 } 28 assert_greater_than_equal(performance.now(), entry.renderTime, 'renderTime bounded by now()'); 29 if (expectedElement !== null) { 30 assert_equals(entry.element, expectedElement, 'element does not match'); 31 assert_equals(entry.identifier, expectedElement.elementTiming, 32 'identifier must be the elementtiming of the element'); 33 assert_equals(entry.id, expectedElement.id, 'id must be the id of the element'); 34 } 35 } 36 37 // Checks that this is an ElementTiming entry with url |expectedUrl|. It also 38 // does a very basic check on |renderTime|: after |beforeRender| and before now(). 39 function checkElement(entry, expectedUrl, expectedIdentifier, expectedID, beforeRender, 40 expectedElement) { 41 checkElementInternal(entry, expectedUrl, expectedIdentifier, expectedID, beforeRender, 42 expectedElement); 43 assert_equals(entry.name, 'image-paint', 'The entry name should be image-paint.'); 44 const rt_entries = performance.getEntriesByName(expectedUrl, 'resource'); 45 assert_equals(rt_entries.length, 1, 'There should be only 1 resource entry.'); 46 assert_greater_than_equal(entry.loadTime, rt_entries[0].responseEnd, 47 'Image loadTime is after the resource responseEnd'); 48 } 49 50 function checkElementWithoutResourceTiming(entry, expectedUrl, expectedIdentifier, 51 expectedID, beforeRender, expectedElement) { 52 checkElementInternal(entry, expectedUrl, expectedIdentifier, expectedID, beforeRender, 53 expectedElement); 54 assert_equals(entry.name, 'image-paint'); 55 // No associated resource from ResourceTiming, so not much to compare loadTime with. 56 assert_greater_than(entry.loadTime, 0, 'The entry loadTime should be greater than 0.'); 57 } 58 59 // Checks that the rect matches the desired values [left right top bottom]. 60 function checkRect(entry, expected, description="") { 61 assert_equals(entry.intersectionRect.left, expected[0], 62 'left of rect ' + description); 63 assert_equals(entry.intersectionRect.right, expected[1], 64 'right of rect ' + description); 65 assert_equals(entry.intersectionRect.top, expected[2], 66 'top of rect ' + description); 67 assert_equals(entry.intersectionRect.bottom, expected[3], 68 'bottom of rect ' + description); 69 } 70 71 // Checks that the intrinsic size matches the desired values. 72 function checkNaturalSize(entry, width, height) { 73 assert_equals(entry.naturalWidth, width, 'The entry naturalWidth is not as expected.'); 74 assert_equals(entry.naturalHeight, height, 'The entry naturalHeight is not as expected.'); 75 } 76 77 function checkTextElement(entry, expectedIdentifier, expectedID, beforeRender, 78 expectedElement) { 79 checkElementInternal(entry, '', expectedIdentifier, expectedID, beforeRender, 80 expectedElement); 81 assert_equals(entry.name, 'text-paint', 'The entry name should be text-paint.'); 82 assert_equals(entry.loadTime, 0, 'The entry loadTime should be 0.'); 83 }