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

#include "chrome/browser/ui/waap/waap_ui_metrics_service.h"

#include "base/test/metrics/histogram_tester.h"
#include "base/time/time.h"
#include "chrome/browser/ui/waap/waap_ui_metrics_recorder.h"
#include "chrome/browser/ui/waap/waap_ui_metrics_service_factory.h"
#include "chrome/test/base/testing_profile.h"
#include "components/startup_metric_utils/browser/startup_metric_utils.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

const base::TimeDelta kTestLatency = base::Milliseconds(100);

}  // namespace

class WaapUIMetricsServiceTest : public testing::Test {
 public:
  WaapUIMetricsServiceTest() = default;
  ~WaapUIMetricsServiceTest() override = default;

  void SetUp() override {
    startup_metric_utils::GetBrowser().ResetSessionForTesting();
    WaapUIMetricsService::ResetForTesting();
  }

 protected:
  base::HistogramTester* histogram_tester() { return &histogram_tester_; }
  TestingProfile* profile() { return &profile_; }

 private:
  content::BrowserTaskEnvironment task_environment_;
  base::HistogramTester histogram_tester_;
  TestingProfile profile_;
};

#if !BUILDFLAG(IS_CHROMEOS)
// Tests that the OnFirstPaint method records a histogram on the first call,
// and does not record it again on subsequent calls.
TEST_F(WaapUIMetricsServiceTest, OnFirstPaint) {
  WaapUIMetricsService* service =
      WaapUIMetricsServiceFactory::GetForProfile(profile());
  ASSERT_TRUE(service);

  const base::TimeTicks paint_time = base::TimeTicks::Now();
  service->OnFirstPaint(paint_time);

  // For tests, startup temperature is undetermined.
  histogram_tester()->ExpectTotalCount(
      "InitialWebUI.Startup.ReloadButton.FirstPaint", 1);

  // Call a second time to ensure it's not recorded again.
  service->OnFirstPaint(paint_time);
  histogram_tester()->ExpectTotalCount(
      "InitialWebUI.Startup.ReloadButton.FirstPaint", 1);
}

// Tests that the OnFirstContentfulPaint method records a histogram on the
// first call, and does not record it again on subsequent calls.
TEST_F(WaapUIMetricsServiceTest, OnFirstContentfulPaint) {
  WaapUIMetricsService* service =
      WaapUIMetricsServiceFactory::GetForProfile(profile());
  ASSERT_TRUE(service);

  const base::TimeTicks paint_time = base::TimeTicks::Now();
  service->OnFirstContentfulPaint(paint_time);

  // For tests, startup temperature is undetermined.
  histogram_tester()->ExpectTotalCount(
      "InitialWebUI.Startup.ReloadButton.FirstContentfulPaint", 1);

  // Call a second time to ensure it's not recorded again.
  service->OnFirstContentfulPaint(paint_time);
  histogram_tester()->ExpectTotalCount(
      "InitialWebUI.Startup.ReloadButton.FirstContentfulPaint", 1);
}
#endif  // !BUILDFLAG(IS_CHROMEOS)

// Tests that the OnReloadButtonInput method records a histogram.
TEST_F(WaapUIMetricsServiceTest, OnReloadButtonInput) {
  WaapUIMetricsService* service =
      WaapUIMetricsServiceFactory::GetForProfile(profile());
  ASSERT_TRUE(service);

  base::HistogramTester histogram_tester;
  service->OnReloadButtonInput(
      WaapUIMetricsRecorder::ReloadButtonInputType::kMouseRelease);
  histogram_tester.ExpectBucketCount(
      "InitialWebUI.ReloadButton.InputCount",
      WaapUIMetricsRecorder::ReloadButtonInputType::kMouseRelease, 1);

  service->OnReloadButtonInput(
      WaapUIMetricsRecorder::ReloadButtonInputType::kKeyPress);
  histogram_tester.ExpectBucketCount(
      "InitialWebUI.ReloadButton.InputCount",
      WaapUIMetricsRecorder::ReloadButtonInputType::kKeyPress, 1);

  histogram_tester.ExpectTotalCount("InitialWebUI.ReloadButton.InputCount", 2);
}

TEST_F(WaapUIMetricsServiceTest, RecordReloadButtonInteractionToReload) {
  WaapUIMetricsService* service =
      WaapUIMetricsServiceFactory::GetForProfile(profile());
  ASSERT_TRUE(service);

  base::HistogramTester histogram_tester;

  const auto start_ticks = base::TimeTicks::Now();
  const base::TimeDelta latency = base::Milliseconds(50);

  // Test WebUI mouse release
  service->RecordReloadButtonInteractionToReload(
      start_ticks, start_ticks + latency,
      WaapUIMetricsRecorder::ReloadButtonInputType::kMouseRelease);

  histogram_tester.ExpectUniqueTimeSample(
      "InitialWebUI.ReloadButton.InteractionToReload.MouseRelease", latency, 1);
  histogram_tester.ExpectUniqueTimeSample(
      "InitialWebUI.ReloadButton.InteractionToReload", latency, 1);

  // Test Views key press
  service->RecordReloadButtonInteractionToReload(
      start_ticks, start_ticks + latency,
      WaapUIMetricsRecorder::ReloadButtonInputType::kKeyPress);

  histogram_tester.ExpectUniqueTimeSample(
      "InitialWebUI.ReloadButton.InteractionToReload.KeyPress", latency, 1);
  histogram_tester.ExpectUniqueTimeSample(
      "InitialWebUI.ReloadButton.InteractionToReload", latency, 2);
}

TEST_F(WaapUIMetricsServiceTest, OnNewWindowBrowserWindowFirstPaint) {
  WaapUIMetricsService* service =
      WaapUIMetricsServiceFactory::GetForProfile(profile());
  ASSERT_TRUE(service);

  base::HistogramTester histogram_tester;
  const auto start_ticks = base::TimeTicks::Now();

  // Test SessionRestore
  service->OnNewWindowBrowserWindowFirstPresentation(
      waap::NewWindowCreationSource::kSessionRestore,
      /*with_existing_window=*/false, start_ticks, start_ticks + kTestLatency);

  histogram_tester.ExpectUniqueTimeSample(
      "InitialWebUI.NewWindow.AllSources.WithoutExistingWindow.BrowserWindow."
      "FirstPaint.FromConstructor2",
      kTestLatency, 1);
  histogram_tester.ExpectUniqueTimeSample(
      "InitialWebUI.NewWindow.SessionRestore.WithoutExistingWindow."
      "BrowserWindow.FirstPaint.FromConstructor2",
      kTestLatency, 1);
}

TEST_F(WaapUIMetricsServiceTest, OnNewWindowReloadButtonFirstPaint) {
  WaapUIMetricsService* service =
      WaapUIMetricsServiceFactory::GetForProfile(profile());
  ASSERT_TRUE(service);

  base::HistogramTester histogram_tester;
  const auto start_ticks = base::TimeTicks::Now();

  // Test BrowserInitiated
  service->OnNewWindowReloadButtonFirstPaint(
      waap::NewWindowCreationSource::kBrowserInitiated,
      /*with_existing_window=*/false, start_ticks, start_ticks + kTestLatency);

  histogram_tester.ExpectUniqueTimeSample(
      "InitialWebUI.NewWindow.AllSources.WithoutExistingWindow.ReloadButton."
      "FirstPaint.FromConstructor2",
      kTestLatency, 1);
  histogram_tester.ExpectUniqueTimeSample(
      "InitialWebUI.NewWindow.BrowserInitiated.WithoutExistingWindow."
      "ReloadButton.FirstPaint.FromConstructor2",
      kTestLatency, 1);
}

TEST_F(WaapUIMetricsServiceTest, OnNewWindowReloadButtonFirstContentfulPaint) {
  WaapUIMetricsService* service =
      WaapUIMetricsServiceFactory::GetForProfile(profile());
  ASSERT_TRUE(service);

  base::HistogramTester histogram_tester;
  const auto start_ticks = base::TimeTicks::Now();

  // Test BrowserInitiated
  service->OnNewWindowReloadButtonFirstContentfulPaint(
      waap::NewWindowCreationSource::kBrowserInitiated,
      /*with_existing_window=*/false, start_ticks, start_ticks + kTestLatency);

  histogram_tester.ExpectUniqueTimeSample(
      "InitialWebUI.NewWindow.AllSources.WithoutExistingWindow.ReloadButton."
      "FirstContentfulPaint.FromConstructor2",
      kTestLatency, 1);
  histogram_tester.ExpectUniqueTimeSample(
      "InitialWebUI.NewWindow.BrowserInitiated.WithoutExistingWindow."
      "ReloadButton.FirstContentfulPaint.FromConstructor2",
      kTestLatency, 1);
}

TEST_F(WaapUIMetricsServiceTest, OnCreatedCounts) {
  WaapUIMetricsService* service =
      WaapUIMetricsServiceFactory::GetForProfile(profile());
  ASSERT_TRUE(service);

  base::HistogramTester histogram_tester;

  service->OnBrowserWindowCreated();
  histogram_tester.ExpectBucketCount("InitialWebUI.View.Creation", 0, 1);

  service->OnReloadButtonCreated();
  histogram_tester.ExpectBucketCount("InitialWebUI.View.Creation", 1, 1);
}
