chromium/chrome/browser/page_load_metrics/integration_tests/largest_contentful_paint_browsertest.cc

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include <algorithm>
#include <memory>
#include <string>
#include <vector>

#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/json/json_string_value_serializer.h"
#include "base/strings/strcat.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/scoped_run_loop_timeout.h"
#include "base/test/trace_event_analyzer.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "cc/base/switches.h"
#include "chrome/browser/page_load_metrics/integration_tests/metric_integration_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/page_load_metrics/browser/page_load_metrics_test_waiter.h"
#include "components/paint_preview/buildflags/buildflags.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/back_forward_cache_util.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/hit_test_region_observer.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/performance/largest_contentful_paint_type.h"
#if defined(USE_AURA)
#include "ui/aura/window.h"
#endif
#include "ui/events/test/event_generator.h"

#if BUILDFLAG(ENABLE_PAINT_PREVIEW)
// `gn check` doesn't recognize that this is included conditionally, with the
// same condition as the dependencies.
#include "components/paint_preview/browser/paint_preview_client.h"  // nogncheck
#endif

Query;
TraceAnalyzer;
TraceEvent;
TraceEventVector;
PageLoad;

namespace {

void ValidateTraceEventHasCorrectCandidateSize(int expected_size,
                                               const TraceEvent& event) {}

void ValidateTraceEventBreakdownTimings(const TraceEvent& event,
                                        double lcp_time) {}

int GetCandidateIndex(const TraceEvent& event) {}

bool compare_candidate_index(const TraceEvent* lhs, const TraceEvent* rhs) {}

}  // namespace

IN_PROC_BROWSER_TEST_F(MetricIntegrationTest, LargestContentfulPaint) {}

// TODO(crbug.com/40936591): This test is flaky on ChromeOS and Linux.
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
#define MAYBE_LargestContentfulPaint_SubframeInput
#else
#define MAYBE_LargestContentfulPaint_SubframeInput
#endif
IN_PROC_BROWSER_TEST_F(MetricIntegrationTest,
                       MAYBE_LargestContentfulPaint_SubframeInput) {}

#if BUILDFLAG(ENABLE_PAINT_PREVIEW)
IN_PROC_BROWSER_TEST_F(MetricIntegrationTest,
                       LargestContentfulPaintPaintPreview) {}
#endif

class LCPLazyLoadingImageTest : public MetricIntegrationTest {};

IN_PROC_BROWSER_TEST_F(LCPLazyLoadingImageTest,
                       LargestContentfulPaint_EventLazyLoadingImage_Enabled) {}

IN_PROC_BROWSER_TEST_F(LCPLazyLoadingImageTest,
                       LargestContentfulPaint_EventLazyLoadingImage_Unset) {}

// TODO(crbug.com/333641374): Re-enable this test
IN_PROC_BROWSER_TEST_F(
    LCPLazyLoadingImageTest,
    DISABLED_LargestContentfulPaint_EventLazyLoadingImage_Video) {}

class PageViewportInLCPTest : public MetricIntegrationTest {};

IN_PROC_BROWSER_TEST_F(PageViewportInLCPTest, FullSizeImageInIframe) {}

// TODO(crbug.com/40866505): Flaky on lacros
#if BUILDFLAG(IS_CHROMEOS_LACROS)
#define MAYBE_IsAnimatedLCPTest
#else
#define MAYBE_IsAnimatedLCPTest
#endif
class MAYBE_IsAnimatedLCPTest : public MetricIntegrationTest {};

IN_PROC_BROWSER_TEST_F(MAYBE_IsAnimatedLCPTest,
                       LargestContentfulPaint_IsAnimated) {}

IN_PROC_BROWSER_TEST_F(MAYBE_IsAnimatedLCPTest,
                       LargestContentfulPaint_IsNotAnimated) {}

IN_PROC_BROWSER_TEST_F(
    MAYBE_IsAnimatedLCPTest,
    LargestContentfulPaint_AnimatedImageWithLargerTextFirst) {}

// crbug.com/1373885: This test is unreliable on ChromeOS, Linux and Mac
IN_PROC_BROWSER_TEST_F(MAYBE_IsAnimatedLCPTest,
                       DISABLED_LargestContentfulPaint_IsVideo) {}

class LargestContentfulPaintTypeTest : public MetricIntegrationTest {};

IN_PROC_BROWSER_TEST_F(LargestContentfulPaintTypeTest, ImageType_PNG) {}

IN_PROC_BROWSER_TEST_F(LargestContentfulPaintTypeTest, ImageType_JPG) {}

IN_PROC_BROWSER_TEST_F(LargestContentfulPaintTypeTest, ImageType_WebP) {}

// TODO(crbug.com/333963663): Flaky on Win.
#if BUILDFLAG(IS_WIN)
#define MAYBE_ImageType_GIF
#else
#define MAYBE_ImageType_GIF
#endif
IN_PROC_BROWSER_TEST_F(LargestContentfulPaintTypeTest, MAYBE_ImageType_GIF) {}

IN_PROC_BROWSER_TEST_F(LargestContentfulPaintTypeTest, ImageType_AVIF) {}

IN_PROC_BROWSER_TEST_F(LargestContentfulPaintTypeTest, ImageType_SVG) {}

IN_PROC_BROWSER_TEST_F(LargestContentfulPaintTypeTest, TextType) {}

// Case when text that is larger and comes before an image. The
// LargestContentfulPaintType should be those of a text element.
IN_PROC_BROWSER_TEST_F(LargestContentfulPaintTypeTest,
                       LargeTextAndImage_TextType) {}

// Case when text that is larger and comes after an image. The
// LargestContentfulPaintType should be those of a text element.
IN_PROC_BROWSER_TEST_F(LargestContentfulPaintTypeTest,
                       ImageAndLargeText_TextType) {}

// Case when a text that is smaller and comes before an Image. The
// LargestContentfulPaintType should be those of an image element.
IN_PROC_BROWSER_TEST_F(LargestContentfulPaintTypeTest,
                       TextAndLargeImage_ImageType) {}

// Case when a text that is smaller and comes after an Image. The
// LargestContentfulPaintType should be those of an image element.
IN_PROC_BROWSER_TEST_F(LargestContentfulPaintTypeTest,
                       LargeImageAndText_ImageType) {}

// (https://crbug.com/1385713): Flaky on mac12-arm64-rel M1 Mac CQ.
#if BUILDFLAG(IS_MAC)
#define MAYBE_DataURIType
#else
#define MAYBE_DataURIType
#endif
IN_PROC_BROWSER_TEST_F(LargestContentfulPaintTypeTest, MAYBE_DataURIType) {}

// (https://crbug.com/1385713): Flaky on mac12-arm64-rel M1 Mac CQ.
#if BUILDFLAG(IS_MAC)
#define MAYBE_DataURIType_SVG
#else
#define MAYBE_DataURIType_SVG
#endif
IN_PROC_BROWSER_TEST_F(LargestContentfulPaintTypeTest, MAYBE_DataURIType_SVG) {}

// (https://crbug.com/1385713): Flaky on mac12-arm64-rel M1 Mac CQ.
// (https://crbug.com/1405307): Flaky on ChromeOS and Linux as well.
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
#define MAYBE_DataURIType_Video
#else
#define MAYBE_DataURIType_Video
#endif
IN_PROC_BROWSER_TEST_F(LargestContentfulPaintTypeTest,
                       MAYBE_DataURIType_Video) {}

IN_PROC_BROWSER_TEST_F(MetricIntegrationTest, LCPBreakdownTimings) {}

class LcpBreakdownTimingsTest : public MetricIntegrationTest {};

IN_PROC_BROWSER_TEST_F(LcpBreakdownTimingsTest, Subframe) {}

// TODO(323888356): There is an issue in the LCP size calculation in a cross
// site subframe. This test should be enabled after that issue is resolved.
IN_PROC_BROWSER_TEST_F(LcpBreakdownTimingsTest, DISABLED_CrossSiteSubframe) {}

#if BUILDFLAG(IS_CHROMEOS_LACROS)
#define MAYBE_MemCacheServedImage
#else
#define MAYBE_MemCacheServedImage
#endif
IN_PROC_BROWSER_TEST_F(LcpBreakdownTimingsTest, MAYBE_MemCacheServedImage) {}

#if BUILDFLAG(IS_CHROMEOS_LACROS)
#define MAYBE_PreloadedImage
#else
#define MAYBE_PreloadedImage
#endif
IN_PROC_BROWSER_TEST_F(LcpBreakdownTimingsTest, MAYBE_PreloadedImage) {}

// TODO(crbug.com/333963663): Flaky on multiple platforms.
#if BUILDFLAG(IS_CHROMEOS_LACROS) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_WIN)
#define MAYBE_PreloadedCacheableImage
#else
#define MAYBE_PreloadedCacheableImage
#endif
IN_PROC_BROWSER_TEST_F(LcpBreakdownTimingsTest, MAYBE_PreloadedCacheableImage) {}

#if BUILDFLAG(IS_CHROMEOS_LACROS) || BUILDFLAG(IS_MAC)
#define MAYBE_NativeLazyLoadingImage
#else
#define MAYBE_NativeLazyLoadingImage
#endif
// TODO(crbug.com/335901379): Re-enable test
IN_PROC_BROWSER_TEST_F(LcpBreakdownTimingsTest, MAYBE_NativeLazyLoadingImage) {}

// TODO(crbug.com/40283415): This test is flaky on multiple platforms.
IN_PROC_BROWSER_TEST_F(LcpBreakdownTimingsTest,
                       DISABLED_ManualLazyLoadingImage) {}

#if BUILDFLAG(IS_CHROMEOS_LACROS)
#define MAYBE_CssBackgroundImage
#else
#define MAYBE_CssBackgroundImage
#endif
IN_PROC_BROWSER_TEST_F(LcpBreakdownTimingsTest, MAYBE_CssBackgroundImage) {}

// TODO(crbug.com/41495170): Flaky test.
#if BUILDFLAG(IS_CHROMEOS_LACROS) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
#define MAYBE_WrittenAsInnerHtmlImage
#else
#define MAYBE_WrittenAsInnerHtmlImage
#endif
IN_PROC_BROWSER_TEST_F(LcpBreakdownTimingsTest, MAYBE_WrittenAsInnerHtmlImage) {}

// TODO(crbug.com/41494085): Flaky on Mac.
#if BUILDFLAG(IS_CHROMEOS_LACROS) || BUILDFLAG(IS_MAC)
#define MAYBE_WrittenAsOuterHtmlImage
#else
#define MAYBE_WrittenAsOuterHtmlImage
#endif
IN_PROC_BROWSER_TEST_F(LcpBreakdownTimingsTest, MAYBE_WrittenAsOuterHtmlImage) {}

// Flaky timeout with ASAN (crbug.com/337012486)
#if BUILDFLAG(IS_CHROMEOS_LACROS) || defined(ADDRESS_SANITIZER)
#define MAYBE_DocumentWrittenImage
#else
#define MAYBE_DocumentWrittenImage
#endif
IN_PROC_BROWSER_TEST_F(LcpBreakdownTimingsTest, MAYBE_DocumentWrittenImage) {}

// Flaky timeout with ASAN (crbug.com/337012486)
#if BUILDFLAG(IS_CHROMEOS_LACROS) || defined(ADDRESS_SANITIZER)
#define MAYBE_SrcSetImage
#else
#define MAYBE_SrcSetImage
#endif
IN_PROC_BROWSER_TEST_F(LcpBreakdownTimingsTest, MAYBE_SrcSetImage) {}

#if BUILDFLAG(IS_CHROMEOS_LACROS) || BUILDFLAG(IS_MAC)
#define MAYBE_DomMethodAddedImage
#else
#define MAYBE_DomMethodAddedImage
#endif
IN_PROC_BROWSER_TEST_F(LcpBreakdownTimingsTest, MAYBE_DomMethodAddedImage) {}

IN_PROC_BROWSER_TEST_F(MetricIntegrationTest,
                       LCPBreakdownTimings_ImageAndLargerText) {}

IN_PROC_BROWSER_TEST_F(MetricIntegrationTest,
                       LCPBreakdownTimings_ImageAndLargerImage) {}

// TODO(1495363, 1495698): Test is flaky on all platforms.
IN_PROC_BROWSER_TEST_F(MetricIntegrationTest,
                       DISABLED_LCPBreakdownTimings_DetachedWindow) {}

IN_PROC_BROWSER_TEST_F(MetricIntegrationTest,
                       LCPBreakdownTimings_NoLcpForBrokenImage) {}

IN_PROC_BROWSER_TEST_F(MetricIntegrationTest,
                       LcpSameOriginImage_CrossOriginTypeNotSet) {}

IN_PROC_BROWSER_TEST_F(MetricIntegrationTest,
                       LcpCrossOriginImage_CrossOriginTypeIsSet) {}