cleanup: Use ExtensionRegistryObserver in Uber
[chromium-blink-merge.git] / content / browser / power_usage_monitor_impl_unittest.cc
blobefa46f2593ae690f5cbf526d65f04344fd537cd0
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/power_usage_monitor_impl.h"
7 #include "content/public/browser/notification_types.h"
8 #include "content/public/test/test_browser_thread_bundle.h"
9 #include "device/battery/battery_monitor.mojom.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace content {
14 // Dummy ID to identify a phantom RenderProcessHost in tests.
15 const int kDummyRenderProcessHostID = 1;
17 class SystemInterfaceForTest : public PowerUsageMonitor::SystemInterface {
18 public:
19 SystemInterfaceForTest()
20 : num_pending_histogram_reports_(0),
21 discharge_percent_per_hour_(0),
22 now_(base::Time::FromInternalValue(1000)) {}
23 ~SystemInterfaceForTest() override {}
25 int num_pending_histogram_reports() const {
26 return num_pending_histogram_reports_;
29 int discharge_percent_per_hour() const {
30 return discharge_percent_per_hour_;
33 void AdvanceClockSeconds(int seconds) {
34 now_ += base::TimeDelta::FromSeconds(seconds);
37 void AdvanceClockMinutes(int minutes) {
38 now_ += base::TimeDelta::FromMinutes(minutes);
41 void ScheduleHistogramReport(base::TimeDelta delay) override {
42 num_pending_histogram_reports_++;
45 void CancelPendingHistogramReports() override {
46 num_pending_histogram_reports_ = 0;
49 void RecordDischargePercentPerHour(int percent_per_hour) override {
50 discharge_percent_per_hour_ = percent_per_hour;
53 base::Time Now() override { return now_; }
55 private:
56 int num_pending_histogram_reports_;
57 int discharge_percent_per_hour_;
58 base::Time now_;
61 class PowerUsageMonitorTest : public testing::Test {
62 protected:
63 void SetUp() override {
64 monitor_.reset(new PowerUsageMonitor);
65 // PowerUsageMonitor assumes ownership.
66 scoped_ptr<SystemInterfaceForTest> test_interface(
67 new SystemInterfaceForTest());
68 system_interface_ = test_interface.get();
69 monitor_->SetSystemInterfaceForTest(test_interface.Pass());
71 // Without live renderers, the monitor won't do anything.
72 monitor_->OnRenderProcessNotification(NOTIFICATION_RENDERER_PROCESS_CREATED,
73 kDummyRenderProcessHostID);
76 void UpdateBatteryStatus(bool charging, double battery_level) {
77 device::BatteryStatus battery_status;
78 battery_status.charging = charging;
79 battery_status.level = battery_level;
80 monitor_->OnBatteryStatusUpdate(battery_status);
83 void KillTestRenderer() {
84 monitor_->OnRenderProcessNotification(
85 NOTIFICATION_RENDERER_PROCESS_CLOSED, kDummyRenderProcessHostID);
88 scoped_ptr<PowerUsageMonitor> monitor_;
89 SystemInterfaceForTest* system_interface_;
90 TestBrowserThreadBundle thread_bundle_;
93 TEST_F(PowerUsageMonitorTest, StartStopQuickly) {
94 // Going on battery power.
95 UpdateBatteryStatus(false, 1.0);
96 int initial_num_histogram_reports =
97 system_interface_->num_pending_histogram_reports();
98 ASSERT_GT(initial_num_histogram_reports, 0);
100 // Battery level goes down a bit.
101 system_interface_->AdvanceClockSeconds(1);
102 UpdateBatteryStatus(false, 0.9);
103 ASSERT_EQ(initial_num_histogram_reports,
104 system_interface_->num_pending_histogram_reports());
105 ASSERT_EQ(0, system_interface_->discharge_percent_per_hour());
107 // Wall power connected.
108 system_interface_->AdvanceClockSeconds(30);
109 UpdateBatteryStatus(true, 0);
110 ASSERT_EQ(0, system_interface_->num_pending_histogram_reports());
111 ASSERT_EQ(0, system_interface_->discharge_percent_per_hour());
114 TEST_F(PowerUsageMonitorTest, DischargePercentReported) {
115 // Going on battery power.
116 UpdateBatteryStatus(false, 1.0);
117 int initial_num_histogram_reports =
118 system_interface_->num_pending_histogram_reports();
119 ASSERT_GT(initial_num_histogram_reports, 0);
121 // Battery level goes down a bit.
122 system_interface_->AdvanceClockSeconds(30);
123 UpdateBatteryStatus(false, 0.9);
124 ASSERT_EQ(initial_num_histogram_reports,
125 system_interface_->num_pending_histogram_reports());
126 ASSERT_EQ(0, system_interface_->discharge_percent_per_hour());
128 // Wall power connected.
129 system_interface_->AdvanceClockMinutes(31);
130 UpdateBatteryStatus(true, 0);
131 ASSERT_EQ(0, system_interface_->num_pending_histogram_reports());
132 ASSERT_GT(system_interface_->discharge_percent_per_hour(), 0);
135 TEST_F(PowerUsageMonitorTest, NoRenderersDisablesMonitoring) {
136 KillTestRenderer();
138 // Going on battery power.
139 UpdateBatteryStatus(false, 1.0);
140 ASSERT_EQ(0, system_interface_->num_pending_histogram_reports());
141 ASSERT_EQ(0, system_interface_->discharge_percent_per_hour());
143 // Wall power connected.
144 system_interface_->AdvanceClockSeconds(30);
145 UpdateBatteryStatus(true, 0.5);
146 ASSERT_EQ(0, system_interface_->num_pending_histogram_reports());
147 ASSERT_EQ(0, system_interface_->discharge_percent_per_hour());
150 TEST_F(PowerUsageMonitorTest, NoRenderersCancelsInProgressMonitoring) {
151 // Going on battery power.
152 UpdateBatteryStatus(false, 1.0);
153 ASSERT_GT(system_interface_->num_pending_histogram_reports(), 0);
154 ASSERT_EQ(0, system_interface_->discharge_percent_per_hour());
156 // All renderers killed.
157 KillTestRenderer();
158 ASSERT_EQ(0, system_interface_->num_pending_histogram_reports());
160 // Wall power connected.
161 system_interface_->AdvanceClockMinutes(31);
162 UpdateBatteryStatus(true, 0);
163 ASSERT_EQ(0, system_interface_->num_pending_histogram_reports());
164 ASSERT_EQ(0, system_interface_->discharge_percent_per_hour());
167 } // namespace content