OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/chromeos/status/power_menu_button.h" | 5 #include "chrome/browser/chromeos/status/power_menu_button.h" |
6 | 6 |
7 #include "chrome/browser/chromeos/cros/cros_in_process_browser_test.h" | 7 #include "chrome/browser/chromeos/cros/cros_in_process_browser_test.h" |
8 #include "chrome/browser/chromeos/cros/mock_power_library.h" | 8 #include "chrome/browser/chromeos/cros/mock_power_library.h" |
9 #include "chrome/browser/chromeos/frame/browser_view.h" | 9 #include "chrome/browser/chromeos/frame/browser_view.h" |
10 #include "chrome/browser/chromeos/status/status_area_view.h" | 10 #include "chrome/browser/chromeos/status/status_area_view.h" |
11 #include "chrome/browser/chromeos/view_ids.h" | 11 #include "chrome/browser/chromeos/view_ids.h" |
12 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
13 #include "chrome/browser/ui/browser_window.h" | 13 #include "chrome/browser/ui/browser_window.h" |
14 #include "grit/theme_resources.h" | 14 #include "grit/theme_resources.h" |
15 | 15 |
16 namespace { | 16 namespace { |
17 const int kNumBatteryStates = 20; | 17 const int kNumBatteryStates = 20; |
18 } | 18 } |
19 namespace chromeos { | 19 namespace chromeos { |
20 using ::testing::AnyNumber; | 20 using ::testing::AnyNumber; |
21 using ::testing::InvokeWithoutArgs; | 21 using ::testing::InvokeWithoutArgs; |
22 using ::testing::Return; | 22 using ::testing::Return; |
23 using ::testing::ReturnRef; | 23 using ::testing::ReturnRef; |
24 using ::testing::_; | 24 using ::testing::_; |
25 | 25 |
26 class PowerMenuButtonTest : public CrosInProcessBrowserTest { | 26 class PowerMenuButtonTest : public CrosInProcessBrowserTest { |
27 protected: | 27 protected: |
28 MockPowerLibrary *mock_power_library_; | 28 PowerMenuButtonTest() : CrosInProcessBrowserTest() { |
29 | |
30 PowerMenuButtonTest() : CrosInProcessBrowserTest(), | |
31 mock_power_library_(NULL) { | |
32 } | 29 } |
33 | 30 |
34 virtual void SetUpInProcessBrowserTestFixture() { | 31 virtual void SetUpInProcessBrowserTestFixture() { |
35 cros_mock_->InitStatusAreaMocks(); | 32 cros_mock_->InitStatusAreaMocks(); |
36 cros_mock_->SetStatusAreaMocksExpectations(); | 33 cros_mock_->SetStatusAreaMocksExpectations(); |
37 mock_power_library_ = cros_mock_->mock_power_library(); | |
38 } | 34 } |
39 | 35 |
40 PowerMenuButton* GetPowerMenuButton() { | 36 PowerMenuButton* GetPowerMenuButton() { |
41 BrowserView* view = static_cast<BrowserView*>(browser()->window()); | 37 BrowserView* view = static_cast<BrowserView*>(browser()->window()); |
42 PowerMenuButton* power = static_cast<StatusAreaView*>(view-> | 38 PowerMenuButton* power = static_cast<StatusAreaView*>(view-> |
43 GetViewByID(VIEW_ID_STATUS_AREA))->power_view(); | 39 GetViewByID(VIEW_ID_STATUS_AREA))->power_view(); |
44 return power; | 40 return power; |
45 } | 41 } |
46 | 42 |
47 int CallPowerChangedAndGetBatteryIndex() { | 43 int CallPowerChangedAndGetBatteryIndex(const PowerSupplyStatus& status) { |
48 PowerMenuButton* power = GetPowerMenuButton(); | 44 PowerMenuButton* power = GetPowerMenuButton(); |
49 power->PowerChanged(mock_power_library_); | 45 power->PowerChanged(status); |
50 return power->battery_index(); | 46 return power->battery_index(); |
51 } | 47 } |
52 }; | 48 }; |
53 | 49 |
54 IN_PROC_BROWSER_TEST_F(PowerMenuButtonTest, BatteryMissingTest) { | 50 IN_PROC_BROWSER_TEST_F(PowerMenuButtonTest, BatteryMissingTest) { |
55 EXPECT_CALL(*mock_power_library_, IsBatteryPresent()) | 51 PowerSupplyStatus status = {}; |
56 .WillOnce((Return(false))) // no battery | 52 // No battery present. |
57 .RetiresOnSaturation(); | 53 status.battery_is_present = false; |
58 EXPECT_CALL(*mock_power_library_, GetBatteryPercentage()) | 54 status.battery_percentage = 42.0; |
59 .WillOnce((Return(42.0))) | 55 status.battery_is_full = false; |
60 .RetiresOnSaturation(); | 56 status.line_power_on = false; |
61 EXPECT_CALL(*mock_power_library_, IsBatteryFullyCharged()) | 57 status.battery_seconds_to_empty = 42; |
62 .WillOnce((Return(false))) | 58 status.battery_seconds_to_full = 24; |
63 .RetiresOnSaturation(); | 59 |
64 EXPECT_CALL(*mock_power_library_, IsLinePowerOn()) | 60 EXPECT_EQ(-1, CallPowerChangedAndGetBatteryIndex(status)); |
65 .WillOnce((Return(false))) | |
66 .RetiresOnSaturation(); | |
67 EXPECT_CALL(*mock_power_library_, GetBatteryTimeToEmpty()) | |
68 .WillOnce((Return(base::TimeDelta::FromMinutes(42)))) | |
69 .RetiresOnSaturation(); | |
70 EXPECT_CALL(*mock_power_library_, GetBatteryTimeToFull()) | |
71 .WillOnce((Return(base::TimeDelta::FromMinutes(24)))) | |
72 .RetiresOnSaturation(); | |
73 EXPECT_EQ(-1, CallPowerChangedAndGetBatteryIndex()); | |
74 } | 61 } |
75 | 62 |
76 IN_PROC_BROWSER_TEST_F(PowerMenuButtonTest, BatteryChargedTest) { | 63 IN_PROC_BROWSER_TEST_F(PowerMenuButtonTest, BatteryChargedTest) { |
77 EXPECT_CALL(*mock_power_library_, IsBatteryPresent()) | 64 PowerSupplyStatus status = {}; |
78 .WillOnce((Return(true))) | 65 // The battery is fully charged, and line power is plugged in. |
79 .RetiresOnSaturation(); | 66 status.battery_is_present = true; |
80 EXPECT_CALL(*mock_power_library_, GetBatteryPercentage()) | 67 status.battery_percentage = 42.0; |
81 .WillOnce((Return(42.0))) | 68 status.battery_is_full = true; |
82 .RetiresOnSaturation(); | 69 status.line_power_on = true; |
83 EXPECT_CALL(*mock_power_library_, IsBatteryFullyCharged()) | 70 status.battery_seconds_to_empty = 42; |
84 .WillOnce((Return(true))) // fully charged | 71 status.battery_seconds_to_full = 24; |
85 .RetiresOnSaturation(); | 72 |
86 EXPECT_CALL(*mock_power_library_, IsLinePowerOn()) | 73 EXPECT_EQ(kNumBatteryStates - 1, CallPowerChangedAndGetBatteryIndex(status)); |
87 .WillOnce((Return(true))) // plugged in | |
88 .RetiresOnSaturation(); | |
89 EXPECT_CALL(*mock_power_library_, GetBatteryTimeToEmpty()) | |
90 .WillOnce((Return(base::TimeDelta::FromMinutes(42)))) | |
91 .RetiresOnSaturation(); | |
92 EXPECT_CALL(*mock_power_library_, GetBatteryTimeToFull()) | |
93 .WillOnce((Return(base::TimeDelta::FromMinutes(0)))) | |
94 .RetiresOnSaturation(); | |
95 EXPECT_EQ(kNumBatteryStates - 1, CallPowerChangedAndGetBatteryIndex()); | |
96 } | 74 } |
97 | 75 |
98 IN_PROC_BROWSER_TEST_F(PowerMenuButtonTest, BatteryChargingTest) { | 76 IN_PROC_BROWSER_TEST_F(PowerMenuButtonTest, BatteryChargingTest) { |
99 const int NUM_TIMES = 19; | 77 PowerSupplyStatus status = {}; |
100 EXPECT_CALL(*mock_power_library_, IsBatteryPresent()) | 78 status.battery_is_present = true; |
101 .Times(NUM_TIMES) | 79 status.battery_is_full = false; |
102 .WillRepeatedly((Return(true))) | 80 status.line_power_on = true; |
103 .RetiresOnSaturation(); | 81 status.battery_seconds_to_empty = 42; |
104 EXPECT_CALL(*mock_power_library_, IsBatteryFullyCharged()) | 82 status.battery_seconds_to_full = 24; |
105 .Times(NUM_TIMES) | |
106 .WillRepeatedly((Return(false))) | |
107 .RetiresOnSaturation(); | |
108 EXPECT_CALL(*mock_power_library_, IsLinePowerOn()) | |
109 .Times(NUM_TIMES) | |
110 .WillRepeatedly((Return(true))) // plugged in | |
111 .RetiresOnSaturation(); | |
112 EXPECT_CALL(*mock_power_library_, GetBatteryTimeToEmpty()) | |
113 .Times(NUM_TIMES) | |
114 .WillRepeatedly((Return(base::TimeDelta::FromMinutes(42)))) | |
115 .RetiresOnSaturation(); | |
116 EXPECT_CALL(*mock_power_library_, GetBatteryTimeToFull()) | |
117 .Times(NUM_TIMES) | |
118 .WillRepeatedly((Return(base::TimeDelta::FromMinutes(24)))) | |
119 .RetiresOnSaturation(); | |
120 | 83 |
| 84 // Simulate various levels of charging. |
121 int index = 0; | 85 int index = 0; |
122 for (float percent = 5.0; percent < 100.0; percent += 5.0) { | 86 for (double percent = 5.0; percent < 100.0; percent += 5.0) { |
123 EXPECT_CALL(*mock_power_library_, GetBatteryPercentage()) | 87 status.battery_percentage = percent; |
124 .WillOnce((Return(percent))) | 88 EXPECT_EQ(index, CallPowerChangedAndGetBatteryIndex(status)); |
125 .RetiresOnSaturation(); | |
126 EXPECT_EQ(index, CallPowerChangedAndGetBatteryIndex()); | |
127 index++; | 89 index++; |
128 } | 90 } |
129 } | 91 } |
130 | 92 |
131 IN_PROC_BROWSER_TEST_F(PowerMenuButtonTest, BatteryDischargingTest) { | 93 IN_PROC_BROWSER_TEST_F(PowerMenuButtonTest, BatteryDischargingTest) { |
132 const int NUM_TIMES = 19; | 94 PowerSupplyStatus status = {}; |
133 EXPECT_CALL(*mock_power_library_, IsBatteryPresent()) | 95 status.battery_is_present = true; |
134 .Times(NUM_TIMES) | 96 status.battery_is_full = false; |
135 .WillRepeatedly((Return(true))) | 97 status.line_power_on = false; |
136 .RetiresOnSaturation(); | 98 status.battery_seconds_to_empty = 42; |
137 EXPECT_CALL(*mock_power_library_, IsBatteryFullyCharged()) | 99 status.battery_seconds_to_full = 24; |
138 .Times(NUM_TIMES) | |
139 .WillRepeatedly((Return(false))) | |
140 .RetiresOnSaturation(); | |
141 EXPECT_CALL(*mock_power_library_, IsLinePowerOn()) | |
142 .Times(NUM_TIMES) | |
143 .WillRepeatedly((Return(false))) | |
144 .RetiresOnSaturation(); | |
145 EXPECT_CALL(*mock_power_library_, GetBatteryTimeToEmpty()) | |
146 .Times(NUM_TIMES) | |
147 .WillRepeatedly((Return(base::TimeDelta::FromMinutes(42)))) | |
148 .RetiresOnSaturation(); | |
149 EXPECT_CALL(*mock_power_library_, GetBatteryTimeToFull()) | |
150 .Times(NUM_TIMES) | |
151 .WillRepeatedly((Return(base::TimeDelta::FromMinutes(24)))) | |
152 .RetiresOnSaturation(); | |
153 | 100 |
| 101 // Simulate various levels of discharging. |
154 int index = 0; | 102 int index = 0; |
155 for (float percent = 5.0; percent < 100.0; percent += 5.0) { | 103 for (float percent = 5.0; percent < 100.0; percent += 5.0) { |
156 EXPECT_CALL(*mock_power_library_, GetBatteryPercentage()) | 104 status.battery_percentage = percent; |
157 .WillOnce((Return(percent))) | 105 EXPECT_EQ(index, CallPowerChangedAndGetBatteryIndex(status)); |
158 .RetiresOnSaturation(); | |
159 EXPECT_EQ(index, CallPowerChangedAndGetBatteryIndex()); | |
160 index++; | 106 index++; |
161 } | 107 } |
162 } | 108 } |
163 | 109 |
164 } // namespace chromeos | 110 } // namespace chromeos |
OLD | NEW |