#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 { … };
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 { … };
class PowerMetricsReporterUnitTestBase : public testing::Test { … };
class PowerMetricsReporterUnitTest : public PowerMetricsReporterUnitTestBase { … };
class PowerMetricsReporterWithoutBatteryLevelProviderUnitTest
: public PowerMetricsReporterUnitTestBase { … };
}
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)
TEST_F(PowerMetricsReporterUnitTest, UKMsMacFullyCharged) {
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
TEST_F(PowerMetricsReporterUnitTest, UKMsBatteryStateIncrease) { … }
TEST_F(PowerMetricsReporterUnitTest, UKMsNoTab) { … }
TEST_F(PowerMetricsReporterUnitTest, DurationsLongerThanIntervalAreCapped) { … }
TEST_F(PowerMetricsReporterUnitTest, UKMsWithSleepEvent) { … }
TEST_F(PowerMetricsReporterUnitTest, UKMsWithoutCPU) { … }