OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "ash/system/chromeos/power/power_status.h" | 5 #include "ash/system/chromeos/power/power_status.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
13 #include "chromeos/dbus/dbus_thread_manager.h" | 13 #include "chromeos/dbus/dbus_thread_manager.h" |
14 #include "chromeos/dbus/power_supply_status.h" | 14 #include "chromeos/dbus/power_supply_status.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "third_party/cros_system_api/dbus/service_constants.h" | 16 #include "third_party/cros_system_api/dbus/service_constants.h" |
17 | 17 |
18 namespace ash { | 18 namespace ash { |
19 namespace internal { | 19 namespace internal { |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 void ValidatePowerSupplyStatus(const chromeos::PowerSupplyStatus& status) { | |
24 EXPECT_TRUE(status.battery_is_present); | |
25 EXPECT_GT(status.battery_percentage, 0.0); | |
26 if (status.battery_state == chromeos::PowerSupplyStatus::DISCHARGING) { | |
27 EXPECT_GT(status.battery_seconds_to_empty, 0); | |
28 EXPECT_EQ(status.battery_seconds_to_full, 0); | |
29 } else { | |
30 EXPECT_GT(status.battery_seconds_to_full, 0); | |
31 EXPECT_EQ(status.battery_seconds_to_empty, 0); | |
32 } | |
33 } | |
34 | |
35 class TestObserver : public PowerStatus::Observer { | 23 class TestObserver : public PowerStatus::Observer { |
36 public: | 24 public: |
37 TestObserver() : power_changed_count_(0) { | 25 TestObserver() : power_changed_count_(0) {} |
38 } | |
39 virtual ~TestObserver() {} | 26 virtual ~TestObserver() {} |
40 | 27 |
41 virtual void OnPowerStatusChanged( | 28 int power_changed_count() const { return power_changed_count_; } |
42 const chromeos::PowerSupplyStatus& power_status) OVERRIDE { | |
43 ++power_changed_count_; | |
44 ValidatePowerSupplyStatus(power_status); | |
45 } | |
46 | 29 |
47 int power_changed_count() const { return power_changed_count_; } | 30 // PowerStatus::Observer overrides: |
| 31 virtual void OnPowerStatusChanged() OVERRIDE { ++power_changed_count_; } |
48 | 32 |
49 private: | 33 private: |
50 int power_changed_count_; | 34 int power_changed_count_; |
51 | 35 |
52 DISALLOW_COPY_AND_ASSIGN(TestObserver); | 36 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
53 }; | 37 }; |
54 | 38 |
55 } // namespace | 39 } // namespace |
56 | 40 |
57 class PowerStatusTest : public testing::Test { | 41 class PowerStatusTest : public testing::Test { |
(...skipping 26 matching lines...) Expand all Loading... |
84 }; | 68 }; |
85 | 69 |
86 TEST_F(PowerStatusTest, PowerStatusInitializeAndUpdate) { | 70 TEST_F(PowerStatusTest, PowerStatusInitializeAndUpdate) { |
87 // Test that the initial power supply state should be acquired after | 71 // Test that the initial power supply state should be acquired after |
88 // PowerStatus is instantiated. This depends on | 72 // PowerStatus is instantiated. This depends on |
89 // PowerManagerClientStubImpl, which responds to power status update | 73 // PowerManagerClientStubImpl, which responds to power status update |
90 // requests, pretends there is a battery present, and generates some valid | 74 // requests, pretends there is a battery present, and generates some valid |
91 // power supply status data. | 75 // power supply status data. |
92 message_loop_.RunUntilIdle(); | 76 message_loop_.RunUntilIdle(); |
93 EXPECT_EQ(1, test_observer_->power_changed_count()); | 77 EXPECT_EQ(1, test_observer_->power_changed_count()); |
94 chromeos::PowerSupplyStatus init_status = | |
95 power_status_->GetPowerSupplyStatus(); | |
96 ValidatePowerSupplyStatus(init_status); | |
97 | 78 |
98 // Test RequestUpdate, test_obsever_ should be notified for power suuply | 79 // Test RequestUpdate, test_obsever_ should be notified for power suuply |
99 // status change. | 80 // status change. |
100 power_status_->RequestStatusUpdate(); | 81 power_status_->RequestStatusUpdate(); |
101 message_loop_.RunUntilIdle(); | 82 message_loop_.RunUntilIdle(); |
102 EXPECT_EQ(2, test_observer_->power_changed_count()); | 83 EXPECT_EQ(2, test_observer_->power_changed_count()); |
103 } | 84 } |
104 | 85 |
105 } // namespace internal | 86 } // namespace internal |
106 } // namespace ash | 87 } // namespace ash |
OLD | NEW |