diff options
-rw-r--r-- | chrome/browser/chromeos/status/power_menu_button.cc | 15 | ||||
-rw-r--r-- | chrome/browser/chromeos/status/power_menu_button.h | 5 | ||||
-rw-r--r-- | chrome/browser/chromeos/status/power_menu_button_browsertest.cc | 93 | ||||
-rw-r--r-- | chrome/chrome_tests.gypi | 1 |
4 files changed, 107 insertions, 7 deletions
diff --git a/chrome/browser/chromeos/status/power_menu_button.cc b/chrome/browser/chromeos/status/power_menu_button.cc index 26f43c1..6b9db5f 100644 --- a/chrome/browser/chromeos/status/power_menu_button.cc +++ b/chrome/browser/chromeos/status/power_menu_button.cc @@ -22,7 +22,8 @@ const int PowerMenuButton::kNumPowerImages = 12; PowerMenuButton::PowerMenuButton() : StatusAreaButton(this), - ALLOW_THIS_IN_INITIALIZER_LIST(power_menu_(this)) { + ALLOW_THIS_IN_INITIALIZER_LIST(power_menu_(this)), + icon_id_(-1) { UpdateIcon(); CrosLibrary::Get()->GetPowerLibrary()->AddObserver(this); } @@ -123,12 +124,12 @@ void PowerMenuButton::DrawPowerIcon(gfx::Canvas* canvas, SkBitmap icon) { void PowerMenuButton::UpdateIcon() { PowerLibrary* cros = CrosLibrary::Get()->GetPowerLibrary(); - int id = IDR_STATUSBAR_BATTERY_UNKNOWN; + icon_id_ = IDR_STATUSBAR_BATTERY_UNKNOWN; if (CrosLibrary::Get()->EnsureLoaded()) { if (!cros->battery_is_present()) { - id = IDR_STATUSBAR_BATTERY_MISSING; + icon_id_ = IDR_STATUSBAR_BATTERY_MISSING; } else if (cros->line_power_on() && cros->battery_fully_charged()) { - id = IDR_STATUSBAR_BATTERY_CHARGED; + icon_id_ = IDR_STATUSBAR_BATTERY_CHARGED; } else { // If fully charged, always show 100% even if percentage is a bit less. double percent = cros->battery_fully_charged() ? 100 : @@ -143,12 +144,12 @@ void PowerMenuButton::UpdateIcon() { if (index >= kNumPowerImages) index = kNumPowerImages - 1; if (cros->line_power_on()) - id = IDR_STATUSBAR_BATTERY_CHARGING_1 + index; + icon_id_ = IDR_STATUSBAR_BATTERY_CHARGING_1 + index; else - id = IDR_STATUSBAR_BATTERY_DISCHARGING_1 + index; + icon_id_ = IDR_STATUSBAR_BATTERY_DISCHARGING_1 + index; } } - SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed(id)); + SetIcon(*ResourceBundle::GetSharedInstance().GetBitmapNamed(icon_id_)); SchedulePaint(); } diff --git a/chrome/browser/chromeos/status/power_menu_button.h b/chrome/browser/chromeos/status/power_menu_button.h index 0a0c98d..8cff5f1 100644 --- a/chrome/browser/chromeos/status/power_menu_button.h +++ b/chrome/browser/chromeos/status/power_menu_button.h @@ -46,6 +46,8 @@ class PowerMenuButton : public StatusAreaButton, // PowerLibrary::Observer implementation. virtual void PowerChanged(PowerLibrary* obj); + const int icon_id() const { return icon_id_; } + protected: // StatusAreaButton implementation. virtual void DrawPressed(gfx::Canvas* canvas); @@ -67,6 +69,9 @@ class PowerMenuButton : public StatusAreaButton, // The power menu. views::Menu2 power_menu_; + // The currently showing icon bitmap id. + int icon_id_; + DISALLOW_COPY_AND_ASSIGN(PowerMenuButton); }; diff --git a/chrome/browser/chromeos/status/power_menu_button_browsertest.cc b/chrome/browser/chromeos/status/power_menu_button_browsertest.cc new file mode 100644 index 0000000..fe627df --- /dev/null +++ b/chrome/browser/chromeos/status/power_menu_button_browsertest.cc @@ -0,0 +1,93 @@ +// Copyright (c) 2010 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 "chrome/browser/chromeos/status/power_menu_button.h" + +#include "chrome/browser/browser.h" +#include "chrome/browser/browser_window.h" +#include "chrome/browser/chromeos/cros/cros_in_process_browser_test.h" +#include "chrome/browser/chromeos/cros/mock_power_library.h" +#include "chrome/browser/chromeos/frame/browser_view.h" +#include "chrome/browser/chromeos/status/browser_status_area_view.h" +#include "chrome/browser/chromeos/view_ids.h" +#include "grit/theme_resources.h" + +namespace chromeos { +using ::testing::AnyNumber; +using ::testing::InvokeWithoutArgs; +using ::testing::Return; +using ::testing::ReturnRef; +using ::testing::_; + +class PowerMenuButtonTest : public CrosInProcessBrowserTest { + protected: + PowerMenuButtonTest() : CrosInProcessBrowserTest() {} + + PowerMenuButton* GetPowerMenuButton() { + BrowserView* view = static_cast<BrowserView*>(browser()->window()); + PowerMenuButton* power = static_cast<BrowserStatusAreaView*>(view-> + GetViewByID(VIEW_ID_STATUS_AREA))->power_view(); + return power; + } + + int CallPowerChangedAndGetIconId() { + PowerMenuButton* power = GetPowerMenuButton(); + power->PowerChanged(mock_power_library_); + return power->icon_id(); + } +}; + +IN_PROC_BROWSER_TEST_F(PowerMenuButtonTest, BatteryMissingTest) { + EXPECT_CALL(*mock_power_library_, battery_is_present()) + .WillRepeatedly((Return(false))); + EXPECT_EQ(IDR_STATUSBAR_BATTERY_MISSING, CallPowerChangedAndGetIconId()); +} + +IN_PROC_BROWSER_TEST_F(PowerMenuButtonTest, BatteryChargedTest) { + EXPECT_CALL(*mock_power_library_, battery_is_present()) + .WillRepeatedly((Return(true))); + EXPECT_CALL(*mock_power_library_, battery_fully_charged()) + .WillRepeatedly((Return(true))); + EXPECT_CALL(*mock_power_library_, line_power_on()) + .WillRepeatedly((Return(true))); + EXPECT_EQ(IDR_STATUSBAR_BATTERY_CHARGED, CallPowerChangedAndGetIconId()); +} + +IN_PROC_BROWSER_TEST_F(PowerMenuButtonTest, BatteryChargingTest) { + EXPECT_CALL(*mock_power_library_, battery_is_present()) + .WillRepeatedly((Return(true))); + EXPECT_CALL(*mock_power_library_, battery_fully_charged()) + .WillRepeatedly((Return(false))); + EXPECT_CALL(*mock_power_library_, line_power_on()) + .WillRepeatedly((Return(true))); + + // Test the 12 battery charging states. + int id = IDR_STATUSBAR_BATTERY_CHARGING_1; + for (float precent = 6.0; precent < 100.0; precent += 8.0) { + EXPECT_CALL(*mock_power_library_, battery_percentage()) + .WillRepeatedly((Return(precent))); + EXPECT_EQ(id, CallPowerChangedAndGetIconId()); + id++; + } +} + +IN_PROC_BROWSER_TEST_F(PowerMenuButtonTest, BatteryDischargingTest) { + EXPECT_CALL(*mock_power_library_, battery_is_present()) + .WillRepeatedly((Return(true))); + EXPECT_CALL(*mock_power_library_, battery_fully_charged()) + .WillRepeatedly((Return(false))); + EXPECT_CALL(*mock_power_library_, line_power_on()) + .WillRepeatedly((Return(false))); + + // Test the 12 battery discharing states. + int id = IDR_STATUSBAR_BATTERY_DISCHARGING_1; + for (float precent = 6.0; precent < 100.0; precent += 8.0) { + EXPECT_CALL(*mock_power_library_, battery_percentage()) + .WillRepeatedly((Return(precent))); + EXPECT_EQ(id, CallPowerChangedAndGetIconId()); + id++; + } +} + +} // namespace chromeos diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 33cf940..a5cb502 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -1336,6 +1336,7 @@ ['OS=="linux" and chromeos==1', { 'sources': [ 'browser/chromeos/status/clock_menu_button_browsertest.cc', + 'browser/chromeos/status/power_menu_button_browsertest.cc', ], }], ['OS=="linux" and toolkit_views==0 and chromeos==0', { |