| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/power_usage_monitor_impl.h" | 5 #include "chrome/browser/power_usage_monitor/power_usage_monitor.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "content/public/browser/notification_types.h" | 9 #include "content/public/browser/notification_types.h" |
| 10 #include "content/public/test/test_browser_thread_bundle.h" | 10 #include "content/public/test/test_browser_thread_bundle.h" |
| 11 #include "device/battery/battery_monitor.mojom.h" | 11 #include "device/battery/battery_monitor.mojom.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace content { | |
| 15 | |
| 16 // Dummy ID to identify a phantom RenderProcessHost in tests. | 14 // Dummy ID to identify a phantom RenderProcessHost in tests. |
| 17 const int kDummyRenderProcessHostID = 1; | 15 const int kDummyRenderProcessHostID = 1; |
| 18 | 16 |
| 19 class SystemInterfaceForTest : public PowerUsageMonitor::SystemInterface { | 17 class SystemInterfaceForTest : public PowerUsageMonitor::SystemInterface { |
| 20 public: | 18 public: |
| 21 SystemInterfaceForTest() | 19 SystemInterfaceForTest() |
| 22 : num_pending_histogram_reports_(0), | 20 : num_pending_histogram_reports_(0), |
| 23 discharge_percent_per_hour_(0), | 21 discharge_percent_per_hour_(0), |
| 24 now_(base::Time::FromInternalValue(1000)) {} | 22 now_(base::Time::FromInternalValue(1000)) {} |
| 25 ~SystemInterfaceForTest() override {} | 23 ~SystemInterfaceForTest() override {} |
| 26 | 24 |
| 27 int num_pending_histogram_reports() const { | 25 int num_pending_histogram_reports() const { |
| 28 return num_pending_histogram_reports_; | 26 return num_pending_histogram_reports_; |
| 29 } | 27 } |
| 30 | 28 |
| 31 int discharge_percent_per_hour() const { | 29 int discharge_percent_per_hour() const { return discharge_percent_per_hour_; } |
| 32 return discharge_percent_per_hour_; | |
| 33 } | |
| 34 | 30 |
| 35 void AdvanceClockSeconds(int seconds) { | 31 void AdvanceClockSeconds(int seconds) { |
| 36 now_ += base::TimeDelta::FromSeconds(seconds); | 32 now_ += base::TimeDelta::FromSeconds(seconds); |
| 37 } | 33 } |
| 38 | 34 |
| 39 void AdvanceClockMinutes(int minutes) { | 35 void AdvanceClockMinutes(int minutes) { |
| 40 now_ += base::TimeDelta::FromMinutes(minutes); | 36 now_ += base::TimeDelta::FromMinutes(minutes); |
| 41 } | 37 } |
| 42 | 38 |
| 43 void ScheduleHistogramReport(base::TimeDelta delay) override { | 39 void ScheduleHistogramReport(base::TimeDelta delay) override { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 64 protected: | 60 protected: |
| 65 void SetUp() override { | 61 void SetUp() override { |
| 66 monitor_.reset(new PowerUsageMonitor); | 62 monitor_.reset(new PowerUsageMonitor); |
| 67 // PowerUsageMonitor assumes ownership. | 63 // PowerUsageMonitor assumes ownership. |
| 68 std::unique_ptr<SystemInterfaceForTest> test_interface( | 64 std::unique_ptr<SystemInterfaceForTest> test_interface( |
| 69 new SystemInterfaceForTest()); | 65 new SystemInterfaceForTest()); |
| 70 system_interface_ = test_interface.get(); | 66 system_interface_ = test_interface.get(); |
| 71 monitor_->SetSystemInterfaceForTest(std::move(test_interface)); | 67 monitor_->SetSystemInterfaceForTest(std::move(test_interface)); |
| 72 | 68 |
| 73 // Without live renderers, the monitor won't do anything. | 69 // Without live renderers, the monitor won't do anything. |
| 74 monitor_->OnRenderProcessNotification(NOTIFICATION_RENDERER_PROCESS_CREATED, | 70 monitor_->OnRenderProcessNotification( |
| 75 kDummyRenderProcessHostID); | 71 content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
| 72 kDummyRenderProcessHostID); |
| 76 } | 73 } |
| 77 | 74 |
| 78 void UpdateBatteryStatus(bool charging, double battery_level) { | 75 void UpdateBatteryStatus(bool charging, double battery_level) { |
| 79 device::BatteryStatus battery_status; | 76 device::BatteryStatus battery_status; |
| 80 battery_status.charging = charging; | 77 battery_status.charging = charging; |
| 81 battery_status.level = battery_level; | 78 battery_status.level = battery_level; |
| 82 monitor_->OnBatteryStatusUpdate(battery_status); | 79 monitor_->OnBatteryStatusUpdate(battery_status); |
| 83 } | 80 } |
| 84 | 81 |
| 85 void KillTestRenderer() { | 82 void KillTestRenderer() { |
| 86 monitor_->OnRenderProcessNotification( | 83 monitor_->OnRenderProcessNotification( |
| 87 NOTIFICATION_RENDERER_PROCESS_CLOSED, kDummyRenderProcessHostID); | 84 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 85 kDummyRenderProcessHostID); |
| 88 } | 86 } |
| 89 | 87 |
| 90 std::unique_ptr<PowerUsageMonitor> monitor_; | 88 std::unique_ptr<PowerUsageMonitor> monitor_; |
| 91 SystemInterfaceForTest* system_interface_; | 89 SystemInterfaceForTest* system_interface_; |
| 92 TestBrowserThreadBundle thread_bundle_; | 90 content::TestBrowserThreadBundle thread_bundle_; |
| 93 }; | 91 }; |
| 94 | 92 |
| 95 TEST_F(PowerUsageMonitorTest, StartStopQuickly) { | 93 TEST_F(PowerUsageMonitorTest, StartStopQuickly) { |
| 96 // Going on battery power. | 94 // Going on battery power. |
| 97 UpdateBatteryStatus(false, 1.0); | 95 UpdateBatteryStatus(false, 1.0); |
| 98 int initial_num_histogram_reports = | 96 int initial_num_histogram_reports = |
| 99 system_interface_->num_pending_histogram_reports(); | 97 system_interface_->num_pending_histogram_reports(); |
| 100 ASSERT_GT(initial_num_histogram_reports, 0); | 98 ASSERT_GT(initial_num_histogram_reports, 0); |
| 101 | 99 |
| 102 // Battery level goes down a bit. | 100 // Battery level goes down a bit. |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 // All renderers killed. | 156 // All renderers killed. |
| 159 KillTestRenderer(); | 157 KillTestRenderer(); |
| 160 ASSERT_EQ(0, system_interface_->num_pending_histogram_reports()); | 158 ASSERT_EQ(0, system_interface_->num_pending_histogram_reports()); |
| 161 | 159 |
| 162 // Wall power connected. | 160 // Wall power connected. |
| 163 system_interface_->AdvanceClockMinutes(31); | 161 system_interface_->AdvanceClockMinutes(31); |
| 164 UpdateBatteryStatus(true, 0); | 162 UpdateBatteryStatus(true, 0); |
| 165 ASSERT_EQ(0, system_interface_->num_pending_histogram_reports()); | 163 ASSERT_EQ(0, system_interface_->num_pending_histogram_reports()); |
| 166 ASSERT_EQ(0, system_interface_->discharge_percent_per_hour()); | 164 ASSERT_EQ(0, system_interface_->discharge_percent_per_hour()); |
| 167 } | 165 } |
| 168 | |
| 169 } // namespace content | |
| OLD | NEW |