chromium/chrome/browser/metrics/power/power_metrics_reporter_unittest.cc

// Copyright 2021 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/metrics/power/power_metrics_reporter.h"

#include <memory>
#include <optional>

#include "base/memory/raw_ptr.h"
#include "base/strings/strcat.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/lifetime/browser_shutdown.h"
#include "chrome/browser/metrics/power/power_metrics_constants.h"
#include "chrome/browser/metrics/power/process_monitor.h"
#include "chrome/browser/metrics/usage_scenario/usage_scenario_data_store.h"
#include "components/ukm/test_ukm_recorder.h"
#include "content/public/test/browser_task_environment.h"
#include "services/metrics/public/cpp/metrics_utils.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

base::BatteryLevelProvider::BatteryState MakeBatteryDischargingState(
    int battery_percent) {}

ProcessMonitor::Metrics GetFakeProcessMetrics(bool with_cpu_usage = true) {}

struct HistogramSampleExpectation {};

// For each histogram named after the combination of prefixes from
// `expectations` and suffixes from `suffixes`, verifies that there is a unique
// sample `expectation.sample`, or no sample if `expectation.sample` is nullopt.
void ExpectHistogramSamples(
    base::HistogramTester* histogram_tester,
    const std::vector<const char*>& suffixes,
    const std::vector<HistogramSampleExpectation>& expectations) {}

UkmEntry;

class FakeBatteryLevelProvider : public base::BatteryLevelProvider {};

class TestProcessMonitor : public ProcessMonitor {};

class TestUsageScenarioDataStoreImpl : public UsageScenarioDataStoreImpl {};

// This doesn't use the typical {class being tested}Test name pattern because
// there's already a PowerMetricsReporterTest class in the chromeos namespace
// and this conflicts with it.
class PowerMetricsReporterUnitTestBase : public testing::Test {};

class PowerMetricsReporterUnitTest : public PowerMetricsReporterUnitTestBase {};

class PowerMetricsReporterWithoutBatteryLevelProviderUnitTest
    : public PowerMetricsReporterUnitTestBase {};

}  // namespace

TEST_F(PowerMetricsReporterWithoutBatteryLevelProviderUnitTest,
       CPUTimeRecorded) {}

TEST_F(PowerMetricsReporterWithoutBatteryLevelProviderUnitTest,
       CPUTimeMissing) {}

TEST_F(PowerMetricsReporterUnitTest, LongIntervalHistograms) {}

TEST_F(PowerMetricsReporterUnitTest, UKMs) {}

TEST_F(PowerMetricsReporterUnitTest, UKMsBrowserShuttingDown) {}

TEST_F(PowerMetricsReporterUnitTest, UKMsPluggedIn) {}

TEST_F(PowerMetricsReporterUnitTest, UKMsBatteryStateChanges) {}

TEST_F(PowerMetricsReporterUnitTest, UKMsBatteryStateUnavailable) {}

TEST_F(PowerMetricsReporterUnitTest, UKMsNoBattery) {}

#if BUILDFLAG(IS_MAC)
// Tests that on MacOS, a full |charge_level| while not plugged does not result
// in a kDischarging value emitted. See https://crbug.com/1249830.
TEST_F(PowerMetricsReporterUnitTest, UKMsMacFullyCharged) {
  // Set the initial battery level at 100%.
  power_metrics_reporter_->battery_state_for_testing()->current_capacity = 100;

  process_monitor_.SetMetricsToReturn({});
  battery_states_.push(MakeBatteryDischargingState(100));

  UsageScenarioDataStore::IntervalData fake_interval_data;
  fake_interval_data.source_id_for_longest_visible_origin =
      ukm::ConvertToSourceId(42, ukm::SourceIdType::NAVIGATION_ID);
  long_data_store_.SetIntervalDataToReturn(fake_interval_data);

  task_environment_.FastForwardBy(kLongPowerMetricsIntervalDuration);

  auto entries = test_ukm_recorder_.GetEntriesByName(
      ukm::builders::PowerUsageScenariosIntervalData::kEntryName);
  EXPECT_EQ(1u, entries.size());
  EXPECT_FALSE(test_ukm_recorder_.EntryHasMetric(
      entries[0], UkmEntry::kBatteryDischargeRateName));
  test_ukm_recorder_.ExpectEntryMetric(
      entries[0], UkmEntry::kBatteryDischargeModeName,
      static_cast<int64_t>(BatteryDischargeMode::kMacFullyCharged));
}
#endif  // BUILDFLAG(IS_MAC)

TEST_F(PowerMetricsReporterUnitTest, UKMsBatteryStateIncrease) {}

TEST_F(PowerMetricsReporterUnitTest, UKMsNoTab) {}

TEST_F(PowerMetricsReporterUnitTest, DurationsLongerThanIntervalAreCapped) {}

TEST_F(PowerMetricsReporterUnitTest, UKMsWithSleepEvent) {}

TEST_F(PowerMetricsReporterUnitTest, UKMsWithoutCPU) {}