1 // Copyright (c) 2013 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. 4 5 #include "ash/system/chromeos/power/power_status.h" 6 7 #include "base/memory/scoped_ptr.h" 8 #include "base/message_loop/message_loop.h" 9 #include "chromeos/dbus/dbus_thread_manager.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 #include "third_party/cros_system_api/dbus/service_constants.h" 12 13 namespace ash { 14 namespace { 15 16 class TestObserver : public PowerStatus::Observer { 17 public: 18 TestObserver() : power_changed_count_(0) {} 19 virtual ~TestObserver() {} 20 21 int power_changed_count() const { return power_changed_count_; } 22 23 // PowerStatus::Observer overrides: 24 virtual void OnPowerStatusChanged() OVERRIDE { ++power_changed_count_; } 25 26 private: 27 int power_changed_count_; 28 29 DISALLOW_COPY_AND_ASSIGN(TestObserver); 30 }; 31 32 } // namespace 33 34 class PowerStatusTest : public testing::Test { 35 public: 36 PowerStatusTest() : power_status_(NULL) {} 37 virtual ~PowerStatusTest() {} 38 39 virtual void SetUp() OVERRIDE { 40 chromeos::DBusThreadManager::InitializeWithStub(); 41 PowerStatus::Initialize(); 42 power_status_ = PowerStatus::Get(); 43 test_observer_.reset(new TestObserver); 44 power_status_->AddObserver(test_observer_.get()); 45 } 46 47 virtual void TearDown() OVERRIDE { 48 power_status_->RemoveObserver(test_observer_.get()); 49 test_observer_.reset(); 50 PowerStatus::Shutdown(); 51 chromeos::DBusThreadManager::Shutdown(); 52 } 53 54 protected: 55 base::MessageLoopForUI message_loop_; 56 PowerStatus* power_status_; // Not owned. 57 scoped_ptr<TestObserver> test_observer_; 58 59 private: 60 DISALLOW_COPY_AND_ASSIGN(PowerStatusTest); 61 }; 62 63 TEST_F(PowerStatusTest, InitializeAndUpdate) { 64 // Test that the initial power supply state should be acquired after 65 // PowerStatus is instantiated. This depends on 66 // PowerManagerClientStubImpl, which responds to power status update 67 // requests, pretends there is a battery present, and generates some valid 68 // power supply status data. 69 message_loop_.RunUntilIdle(); 70 EXPECT_EQ(1, test_observer_->power_changed_count()); 71 72 // Test RequestUpdate, test_obsever_ should be notified for power suuply 73 // status change. 74 power_status_->RequestStatusUpdate(); 75 message_loop_.RunUntilIdle(); 76 EXPECT_EQ(2, test_observer_->power_changed_count()); 77 } 78 79 TEST_F(PowerStatusTest, ShouldDisplayBatteryTime) { 80 EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime( 81 base::TimeDelta::FromSeconds(-1))); 82 EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime( 83 base::TimeDelta::FromSeconds(0))); 84 EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime( 85 base::TimeDelta::FromSeconds(59))); 86 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime( 87 base::TimeDelta::FromSeconds(60))); 88 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime( 89 base::TimeDelta::FromSeconds(600))); 90 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime( 91 base::TimeDelta::FromSeconds(3600))); 92 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime( 93 base::TimeDelta::FromSeconds( 94 PowerStatus::kMaxBatteryTimeToDisplaySec))); 95 EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime( 96 base::TimeDelta::FromSeconds( 97 PowerStatus::kMaxBatteryTimeToDisplaySec + 1))); 98 } 99 100 TEST_F(PowerStatusTest, SplitTimeIntoHoursAndMinutes) { 101 int hours = 0, minutes = 0; 102 PowerStatus::SplitTimeIntoHoursAndMinutes( 103 base::TimeDelta::FromSeconds(0), &hours, &minutes); 104 EXPECT_EQ(0, hours); 105 EXPECT_EQ(0, minutes); 106 107 PowerStatus::SplitTimeIntoHoursAndMinutes( 108 base::TimeDelta::FromSeconds(60), &hours, &minutes); 109 EXPECT_EQ(0, hours); 110 EXPECT_EQ(1, minutes); 111 112 PowerStatus::SplitTimeIntoHoursAndMinutes( 113 base::TimeDelta::FromSeconds(3600), &hours, &minutes); 114 EXPECT_EQ(1, hours); 115 EXPECT_EQ(0, minutes); 116 117 PowerStatus::SplitTimeIntoHoursAndMinutes( 118 base::TimeDelta::FromSeconds(3600 + 60), &hours, &minutes); 119 EXPECT_EQ(1, hours); 120 EXPECT_EQ(1, minutes); 121 122 PowerStatus::SplitTimeIntoHoursAndMinutes( 123 base::TimeDelta::FromSeconds(7 * 3600 + 23 * 60), &hours, &minutes); 124 EXPECT_EQ(7, hours); 125 EXPECT_EQ(23, minutes); 126 127 // Check that minutes are rounded. 128 PowerStatus::SplitTimeIntoHoursAndMinutes( 129 base::TimeDelta::FromSeconds(2 * 3600 + 3 * 60 + 30), &hours, &minutes); 130 EXPECT_EQ(2, hours); 131 EXPECT_EQ(4, minutes); 132 133 PowerStatus::SplitTimeIntoHoursAndMinutes( 134 base::TimeDelta::FromSeconds(2 * 3600 + 3 * 60 + 29), &hours, &minutes); 135 EXPECT_EQ(2, hours); 136 EXPECT_EQ(3, minutes); 137 138 // Check that times close to hour boundaries aren't incorrectly rounded such 139 // that they display 60 minutes: http://crbug.com/368261 140 PowerStatus::SplitTimeIntoHoursAndMinutes( 141 base::TimeDelta::FromSecondsD(3599.9), &hours, &minutes); 142 EXPECT_EQ(1, hours); 143 EXPECT_EQ(0, minutes); 144 145 PowerStatus::SplitTimeIntoHoursAndMinutes( 146 base::TimeDelta::FromSecondsD(3600.1), &hours, &minutes); 147 EXPECT_EQ(1, hours); 148 EXPECT_EQ(0, minutes); 149 } 150 151 } // namespace ash 152