Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(402)

Unified Diff: content/browser/power_usage_monitor_impl_unittest.cc

Issue 560553005: Battery impact UMA (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove rate limiting + Move everything to main thread. Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/power_usage_monitor_impl_unittest.cc
diff --git a/content/browser/power_usage_monitor_impl_unittest.cc b/content/browser/power_usage_monitor_impl_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f9f5773a5d963bbb3c0cf2f5a75e8b179f85dcb0
--- /dev/null
+++ b/content/browser/power_usage_monitor_impl_unittest.cc
@@ -0,0 +1,101 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/power_usage_monitor_impl.h"
+
+#include "content/public/test/test_browser_thread_bundle.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace content {
+
+class SystemInterfaceForTest : public PowerUsageMonitor::SystemInterface {
+ public:
+ SystemInterfaceForTest()
+ : num_pending_histogram_reports_(0), now_(base::Time::Now()) {}
Daniel Erat 2014/11/04 17:04:03 nit: one per line since they don't all fit on the
jeremy 2014/11/05 11:40:19 Done.
+ virtual ~SystemInterfaceForTest() {}
Daniel Erat 2014/11/04 17:04:03 use override instead of virtual here
jeremy 2014/11/05 11:40:19 Done.
+
+ int num_pending_histogram_reports() { return num_pending_histogram_reports_; }
Daniel Erat 2014/11/04 17:04:03 nit: make this method const
jeremy 2014/11/05 11:40:19 Done.
+
+ void AdvanceClockSeconds(int seconds) {
+ now_ += base::TimeDelta::FromSeconds(seconds);
+ }
+
+ void AdvanceClockHours(int hours) {
Daniel Erat 2014/11/04 17:04:03 this doesn't get called; delete it
jeremy 2014/11/05 11:40:19 Done.
+ now_ += base::TimeDelta::FromHours(hours);
+ }
+
+ void ScheduleHistogramReport(base::TimeDelta delay) override {
+ num_pending_histogram_reports_++;
+ }
+
+ void CancelPendingHistogramReports() override {
+ num_pending_histogram_reports_ = 0;
+ }
+
+ virtual base::Time Now() override { return now_; }
Daniel Erat 2014/11/04 17:04:03 remove 'virtual'
jeremy 2014/11/05 11:40:19 Done.
+
+ private:
+ int num_pending_histogram_reports_;
+ base::Time now_;
+};
+
+class PowerUsageMonitorTest : public testing::Test {
+ protected:
+ virtual void SetUp() {
+ monitor_.reset(new PowerUsageMonitor);
+ // PowerUsageMonitor assumes ownership.
+ scoped_ptr<SystemInterfaceForTest> test_interface(
+ new SystemInterfaceForTest());
+ system_interface_ = test_interface.get();
+ monitor_->SetSystemInterfaceForTest(test_interface.Pass());
+
+ // Without live renderers, the monitor won't do anything.
+ monitor_->SetRenderersForTest(true);
+ }
+
+ void UpdateBatteryStatus(bool charging, double battery_level) {
+ device::BatteryStatus battery_status;
+ battery_status.charging = charging;
+ battery_status.level = battery_level;
+ monitor_->OnBatteryStatusUpdate(battery_status);
+ }
+
+ scoped_ptr<PowerUsageMonitor> monitor_;
+ SystemInterfaceForTest* system_interface_;
+ TestBrowserThreadBundle thread_bundle_;
+};
+
+TEST_F(PowerUsageMonitorTest, StartStop) {
+ // Going on battery power.
+ UpdateBatteryStatus(false, 1.0);
+ int initial_num_histogram_reports =
+ system_interface_->num_pending_histogram_reports();
+ ASSERT_GT(initial_num_histogram_reports, 0);
+
+ // Battery level goes down a bit.
+ system_interface_->AdvanceClockSeconds(1);
+ UpdateBatteryStatus(false, 0.9);
+ ASSERT_EQ(initial_num_histogram_reports,
+ system_interface_->num_pending_histogram_reports());
+
+ // Wall power connected.
+ system_interface_->AdvanceClockSeconds(30);
+ UpdateBatteryStatus(true, 0.5);
+ ASSERT_EQ(system_interface_->num_pending_histogram_reports(), 0);
+}
+
+TEST_F(PowerUsageMonitorTest, NoRenderersDisablesMonitoring) {
+ monitor_->SetRenderersForTest(false);
+
+ // Going on battery power.
+ UpdateBatteryStatus(false, 1.0);
+ ASSERT_EQ(system_interface_->num_pending_histogram_reports(), 0);
+
+ // Wall power connected.
+ system_interface_->AdvanceClockSeconds(30);
+ UpdateBatteryStatus(true, 0.5);
+ ASSERT_EQ(system_interface_->num_pending_histogram_reports(), 0);
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698