Repository: servo/servo
File: components/servo/tests/largest_contentful_paint.rs (Rust)

Code before:
                <a href=\"https://servo.org\"><div style=\"width: 50px; height: 50px;\">Link</div></a> \
                <div><img src=\"https://servo.org/img/home-servo-hero.png\" style=\"width: 50px; height: 50px;\"></div>"
            )
            .unwrap(),
        )
        .build();

    show_webview_and_wait_for_rendering_to_be_ready(&servo_test, &webview, &delegate);

    // Use a global variable to store the result since evaluate_javascript doesn't handle Promises
    let script = "(() => {
            window.__lcp_result = null;
            const observer = new PerformanceObserver((list) => {
                const entries = list.getEntries();
                if (entries.length > 0) {
                    const lastEntry = entries[entries.length - 1];
                    if (lastEntry) {
                        window.__lcp_result = lastEntry;
                    }
                }
            });
            observer.observe({ type: 'largest-contentful-paint', buffered: true });
            // Check for buffered entries that might not have triggered callback
            const buffered = observer.takeRecords();
            if (buffered.length > 0) {
                const lastEntry = buffered[buffered.length - 1];
                if (lastEntry) {
                    window.__lcp_result = lastEntry;
                }
            }
            return window.__lcp_result.toJSON();
        })()";

    let lcp_value = evaluate_javascript(&servo_test, webview.clone(), script);

    if let Ok(JSValue::Object(obj)) = lcp_value {
        assert_eq!(obj.get("name"), Some(JSValue::String("".into())).as_ref());
        assert_eq!(obj.get("duration"), Some(JSValue::Number(0.0)).as_ref());
        assert_eq!(
            obj.get("entryType"),
            Some(JSValue::String("largest-contentful-paint".into())).as_ref()
        );
        assert_eq!(obj.get("size"), Some(JSValue::Number(2500.0)).as_ref());
        assert!(obj.get("renderTime").is_some());
        assert!(obj.get("loadTime").is_some());
    } else {
        panic!("Failed to evaluate LCP script: {:?}", lcp_value);
    }
}


Review comment (suggestion):
This could be a little simpler maybe?

```suggestion
                const lastEntry = buffered.pop();
```

Code after:
                <a href=\"https://servo.org\"><div style=\"width: 50px; height: 50px;\">Link</div></a> \
                <div><img src=\"data:image/svg+xml,<svg width='50' height='50'><circle cx='25' cy='25' r='20' fill='green'/></svg>\"\
                style=\"width: 50px; height: 50px;\"></div>"
            )
            .unwrap(),
        )
        .build();

    let lcp_script = "(async () => {
        window.lcp = await new Promise(resolve => {
            (new PerformanceObserver(entryList => {
                resolve(entryList.getEntries()[0]);
            }))
            .observe({type: 'largest-contentful-paint', buffered: true});
        })
    })();";

    if let Err(err) = evaluate_javascript(&servo_test, webview.clone(), lcp_script) {
        panic!("Failed to evaluate LCP setup script: {:?}", err);
    }

    show_webview_and_wait_for_rendering_to_be_ready(&servo_test, &webview, &delegate);

    // Read from a global variable used to store the result since evaluate_javascript doesn't handle Promises
    let lcp = evaluate_javascript(&servo_test, webview.clone(), "window.lcp.toJSON();");

    if let Ok(JSValue::Object(obj)) = lcp {
        assert_eq!(obj.get("name"), Some(JSValue::String("".into())).as_ref());
        assert_eq!(obj.get("duration"), Some(JSValue::Number(0.0)).as_ref());
        assert_eq!(
            obj.get("entryType"),
            Some(JSValue::String("largest-contentful-paint".into())).as_ref()
        );
        assert_eq!(obj.get("size"), Some(JSValue::Number(2500.0)).as_ref());
        assert!(obj.get("renderTime").is_some());
        assert!(obj.get("loadTime").is_some());
    } else {
        panic!("No entries for Largest Contentful Paint were recorded.");
    }
}