diff options
author | mukai@chromium.org <mukai@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-12-14 01:32:55 +0000 |
---|---|---|
committer | mukai@chromium.org <mukai@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-12-14 01:32:55 +0000 |
commit | 763b393364d4950921192aff1c061f123af60591 (patch) | |
tree | 48bd75a5171c338f73f07607733ae79786a7ad53 /ash | |
parent | ed98333cfc22adfd3f07b0d2b08123090e9325aa (diff) | |
download | chromium_src-763b393364d4950921192aff1c061f123af60591.zip chromium_src-763b393364d4950921192aff1c061f123af60591.tar.gz chromium_src-763b393364d4950921192aff1c061f123af60591.tar.bz2 |
Adds TraySessionLengthLimitTest to ash_unittests.
BUG=305812
R=bartfab@chromium.org, stevenjb@chromium.org, oshima@chromium.org
TEST=new tests
Review URL: https://codereview.chromium.org/108213009
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@240838 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ash')
-rw-r--r-- | ash/ash.gyp | 1 | ||||
-rw-r--r-- | ash/system/session_length_limit/session_length_limit_observer.h | 4 | ||||
-rw-r--r-- | ash/system/session_length_limit/tray_session_length_limit.cc | 23 | ||||
-rw-r--r-- | ash/system/session_length_limit/tray_session_length_limit.h | 15 | ||||
-rw-r--r-- | ash/system/session_length_limit/tray_session_length_limit_unittest.cc | 195 | ||||
-rw-r--r-- | ash/test/ash_test_base.cc | 6 | ||||
-rw-r--r-- | ash/test/ash_test_base.h | 2 | ||||
-rw-r--r-- | ash/test/test_system_tray_delegate.cc | 29 | ||||
-rw-r--r-- | ash/test/test_system_tray_delegate.h | 14 |
9 files changed, 277 insertions, 12 deletions
diff --git a/ash/ash.gyp b/ash/ash.gyp index 08202d2..bb35400 100644 --- a/ash/ash.gyp +++ b/ash/ash.gyp @@ -823,6 +823,7 @@ 'system/chromeos/screen_security/screen_tray_item_unittest.cc', 'system/chromeos/tray_display_unittest.cc', 'system/date/date_view_unittest.cc', + 'system/session_length_limit/tray_session_length_limit_unittest.cc', 'system/tray/system_tray_unittest.cc', 'system/tray/tray_details_view_unittest.cc', 'system/user/tray_user_unittest.cc', diff --git a/ash/system/session_length_limit/session_length_limit_observer.h b/ash/system/session_length_limit/session_length_limit_observer.h index 2a4f5275..3fcba64 100644 --- a/ash/system/session_length_limit/session_length_limit_observer.h +++ b/ash/system/session_length_limit/session_length_limit_observer.h @@ -5,10 +5,12 @@ #ifndef ASH_SYSTEM_SESSION_LENGTH_LIMIT_SESSION_LENGTH_LIMIT_OBSERVER_H_ #define ASH_SYSTEM_SESSION_LENGTH_LIMIT_SESSION_LENGTH_LIMIT_OBSERVER_H_ +#include "ash/ash_export.h" + namespace ash { // Observer for the session length limit. -class SessionLengthLimitObserver { +class ASH_EXPORT SessionLengthLimitObserver { public: virtual ~SessionLengthLimitObserver() {} diff --git a/ash/system/session_length_limit/tray_session_length_limit.cc b/ash/system/session_length_limit/tray_session_length_limit.cc index cdf69d5..81425af 100644 --- a/ash/system/session_length_limit/tray_session_length_limit.cc +++ b/ash/system/session_length_limit/tray_session_length_limit.cc @@ -50,8 +50,6 @@ const SkColor kRemainingTimeColor = SK_ColorWHITE; // Color in which the remaining session time is shown when it is expiring soon. const SkColor kRemainingTimeExpiringSoonColor = SK_ColorRED; -const char kSessionLengthTimeoutNotificationId[] = "chrome://session/timeout"; - views::Label* CreateAndSetupLabel() { views::Label* label = new views::Label; label->SetHorizontalAlignment(gfx::ALIGN_LEFT); @@ -79,17 +77,16 @@ base::string16 FormatRemainingSessionTimeNotification( // Creates, or updates the notification for session length timeout with // |remaining_time|. |state_changed| is true when its internal state has been // changed from another. -void CreateOrUpdateNotification(const base::TimeDelta& remaining_time, +void CreateOrUpdateNotification(const std::string& notification_id, + const base::TimeDelta& remaining_time, bool state_changed) { message_center::MessageCenter* message_center = message_center::MessageCenter::Get(); // Do not create a new notification if no state has changed. It may happen // when the notification is already closed by the user, see crbug.com/285941. - if (!state_changed && - !message_center->HasNotification(kSessionLengthTimeoutNotificationId)) { + if (!state_changed && !message_center->HasNotification(notification_id)) return; - } ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); message_center::RichNotificationData data; @@ -97,7 +94,7 @@ void CreateOrUpdateNotification(const base::TimeDelta& remaining_time, data.should_make_spoken_feedback_for_popup_updates = state_changed; scoped_ptr<Notification> notification(new Notification( message_center::NOTIFICATION_TYPE_SIMPLE, - kSessionLengthTimeoutNotificationId, + notification_id, FormatRemainingSessionTimeNotification(remaining_time), base::string16() /* message */, bundle.GetImageNamed(IDR_AURA_UBER_TRAY_SESSION_LENGTH_LIMIT_TIMER), @@ -278,6 +275,10 @@ void RemainingSessionTimeTrayView::SetBorder(ShelfAlignment shelf_alignment) { } // namespace tray +// static +const char TraySessionLengthLimit::kNotificationId[] = + "chrome://session/timeout"; + TraySessionLengthLimit::TraySessionLengthLimit(SystemTray* system_tray) : SystemTrayItem(system_tray), tray_view_(NULL), @@ -354,15 +355,17 @@ void TraySessionLengthLimit::Update() { switch (limit_state_) { case LIMIT_NONE: message_center::MessageCenter::Get()->RemoveNotification( - kSessionLengthTimeoutNotificationId, false /* by_user */); + kNotificationId, false /* by_user */); break; case LIMIT_SET: CreateOrUpdateNotification( + kNotificationId, remaining_session_time_, previous_limit_state == LIMIT_NONE); break; case LIMIT_EXPIRING_SOON: CreateOrUpdateNotification( + kNotificationId, remaining_session_time_, previous_limit_state == LIMIT_NONE || previous_limit_state == LIMIT_SET); @@ -375,5 +378,9 @@ void TraySessionLengthLimit::Update() { tray_view_->Update(); } +bool TraySessionLengthLimit::IsTrayViewVisibleForTest() { + return tray_view_ && tray_view_->visible(); +} + } // namespace internal } // namespace ash diff --git a/ash/system/session_length_limit/tray_session_length_limit.h b/ash/system/session_length_limit/tray_session_length_limit.h index 94ce137..6fcd76e 100644 --- a/ash/system/session_length_limit/tray_session_length_limit.h +++ b/ash/system/session_length_limit/tray_session_length_limit.h @@ -14,6 +14,11 @@ #include "base/timer/timer.h" namespace ash { + +namespace test { +class TraySessionLengthLimitTest; +} + namespace internal { namespace tray { @@ -21,8 +26,8 @@ class RemainingSessionTimeTrayView; } // Adds a countdown timer to the system tray if the session length is limited. -class TraySessionLengthLimit : public SystemTrayItem, - public SessionLengthLimitObserver { +class ASH_EXPORT TraySessionLengthLimit : public SystemTrayItem, + public SessionLengthLimitObserver { public: enum LimitState { LIMIT_NONE, @@ -47,8 +52,14 @@ class TraySessionLengthLimit : public SystemTrayItem, base::TimeDelta GetRemainingSessionTime() const; private: + friend class test::TraySessionLengthLimitTest; + + static const char kNotificationId[]; + void Update(); + bool IsTrayViewVisibleForTest(); + tray::RemainingSessionTimeTrayView* tray_view_; LimitState limit_state_; diff --git a/ash/system/session_length_limit/tray_session_length_limit_unittest.cc b/ash/system/session_length_limit/tray_session_length_limit_unittest.cc new file mode 100644 index 0000000..2b05de9 --- /dev/null +++ b/ash/system/session_length_limit/tray_session_length_limit_unittest.cc @@ -0,0 +1,195 @@ +// Copyright 2013 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 "ash/system/session_length_limit/tray_session_length_limit.h" + +#include "ash/root_window_controller.h" +#include "ash/shell.h" +#include "ash/system/tray/system_tray.h" +#include "ash/test/ash_test_base.h" +#include "ash/test/test_system_tray_delegate.h" +#include "base/time/time.h" +#include "ui/message_center/message_center.h" +#include "ui/message_center/notification.h" +#include "ui/message_center/notification_types.h" + +namespace ash { +namespace test { + +class TraySessionLengthLimitTest : public AshTestBase { + public: + TraySessionLengthLimitTest() {} + virtual ~TraySessionLengthLimitTest() {} + + virtual void SetUp() OVERRIDE { + AshTestBase::SetUp(); + SystemTray* system_tray = + Shell::GetPrimaryRootWindowController()->GetSystemTray(); + tray_session_length_limit_ = new internal::TraySessionLengthLimit( + system_tray); + system_tray->AddTrayItem(tray_session_length_limit_); + } + + virtual void TearDown() OVERRIDE { + AshTestBase::TearDown(); + } + + protected: + void UpdateSessionLengthLimitInMin(int mins) { + GetSystemTrayDelegate()->SetSessionLengthLimitForTest( + base::TimeDelta::FromMinutes(mins)); + tray_session_length_limit_->OnSessionLengthLimitChanged(); + } + + message_center::Notification* GetNotification() { + const message_center::NotificationList::Notifications& notifications = + message_center::MessageCenter::Get()->GetVisibleNotifications(); + for (message_center::NotificationList::Notifications::const_iterator iter = + notifications.begin(); iter != notifications.end(); ++iter) { + if ((*iter)->id() == internal::TraySessionLengthLimit::kNotificationId) + return *iter; + } + return NULL; + } + + void ClearSessionLengthLimit() { + GetSystemTrayDelegate()->ClearSessionLengthLimit(); + tray_session_length_limit_->OnSessionLengthLimitChanged(); + } + + void RemoveNotification() { + message_center::MessageCenter::Get()->RemoveNotification( + internal::TraySessionLengthLimit::kNotificationId, true /* by_user */); + } + + internal::TraySessionLengthLimit* tray_session_length_limit() { + return tray_session_length_limit_; + } + + bool IsTrayViewVisible() { + return tray_session_length_limit_->IsTrayViewVisibleForTest(); + } + + private: + // Weak reference, owned by the SystemTray. + internal::TraySessionLengthLimit* tray_session_length_limit_; + + DISALLOW_COPY_AND_ASSIGN(TraySessionLengthLimitTest); +}; + +TEST_F(TraySessionLengthLimitTest, TrayView) { + // No session limit. + EXPECT_FALSE(IsTrayViewVisible()); + + // Limit is 15 min. + UpdateSessionLengthLimitInMin(15); + EXPECT_EQ(internal::TraySessionLengthLimit::LIMIT_SET, + tray_session_length_limit()->GetLimitState()); + EXPECT_TRUE(IsTrayViewVisible()); + + // Limit is 3 min. + UpdateSessionLengthLimitInMin(3); + EXPECT_EQ(internal::TraySessionLengthLimit::LIMIT_EXPIRING_SOON, + tray_session_length_limit()->GetLimitState()); + EXPECT_TRUE(IsTrayViewVisible()); + + // Nothing left. + UpdateSessionLengthLimitInMin(0); + EXPECT_EQ(internal::TraySessionLengthLimit::LIMIT_EXPIRING_SOON, + tray_session_length_limit()->GetLimitState()); + EXPECT_TRUE(IsTrayViewVisible()); + + // Checks the behavior in case the limit goes negative. + UpdateSessionLengthLimitInMin(-5); + EXPECT_EQ(internal::TraySessionLengthLimit::LIMIT_EXPIRING_SOON, + tray_session_length_limit()->GetLimitState()); + EXPECT_TRUE(IsTrayViewVisible()); + + // Clears the session length limit, the TrayView should get invisible. + ClearSessionLengthLimit(); + ASSERT_EQ(internal::TraySessionLengthLimit::LIMIT_NONE, + tray_session_length_limit()->GetLimitState()); + EXPECT_FALSE(IsTrayViewVisible()); +} + +TEST_F(TraySessionLengthLimitTest, Notification) { + // No notifications when no session limit. + EXPECT_FALSE(GetNotification()); + + // Limit is 15 min. + UpdateSessionLengthLimitInMin(15); + message_center::Notification* notification = GetNotification(); + EXPECT_TRUE(notification); + EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority()); + base::string16 first_content = notification->title(); + // Should read the content. + EXPECT_TRUE(notification->rich_notification_data(). + should_make_spoken_feedback_for_popup_updates); + + // Limit is 10 min. + UpdateSessionLengthLimitInMin(10); + notification = GetNotification(); + EXPECT_TRUE(notification); + EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority()); + // The content should be updated. + EXPECT_NE(first_content, notification->title()); + // Should NOT read, because just update the remaining time. + EXPECT_FALSE(notification->rich_notification_data(). + should_make_spoken_feedback_for_popup_updates); + + // Limit is 3 min. + UpdateSessionLengthLimitInMin(3); + notification = GetNotification(); + EXPECT_TRUE(notification); + EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority()); + // Should read the content again because the state has changed. + EXPECT_TRUE(notification->rich_notification_data(). + should_make_spoken_feedback_for_popup_updates); + + // Session length limit is updated to longer. This should not read the + // notification content again. + UpdateSessionLengthLimitInMin(15); + notification = GetNotification(); + EXPECT_TRUE(notification); + EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority()); + // Should not read again because the state has changed to longer. + EXPECT_FALSE(notification->rich_notification_data(). + should_make_spoken_feedback_for_popup_updates); + + // Clears the limit: the notification should be gone. + ClearSessionLengthLimit(); + EXPECT_FALSE(GetNotification()); +} + +TEST_F(TraySessionLengthLimitTest, RemoveNotification) { + // Limit is 15 min. + UpdateSessionLengthLimitInMin(15); + EXPECT_TRUE(GetNotification()); + + // Limit is 14 min. + UpdateSessionLengthLimitInMin(14); + EXPECT_TRUE(GetNotification()); + + // Removes the notification. + RemoveNotification(); + EXPECT_FALSE(GetNotification()); + + // Limit is 13 min. The notification should not re-appear. + UpdateSessionLengthLimitInMin(13); + EXPECT_FALSE(GetNotification()); + + // Limit is 3 min. The notification should re-appear because of state change. + UpdateSessionLengthLimitInMin(3); + EXPECT_TRUE(GetNotification()); + + RemoveNotification(); + + // Session length limit is updated to longer state. This should not re-appear + // the notification. + UpdateSessionLengthLimitInMin(15); + EXPECT_FALSE(GetNotification()); +} + +} // namespace test +} // namespace ash diff --git a/ash/test/ash_test_base.cc b/ash/test/ash_test_base.cc index 335972d..30ca1be 100644 --- a/ash/test/ash_test_base.cc +++ b/ash/test/ash_test_base.cc @@ -16,6 +16,7 @@ #include "ash/test/display_manager_test_api.h" #include "ash/test/test_session_state_delegate.h" #include "ash/test/test_shell_delegate.h" +#include "ash/test/test_system_tray_delegate.h" #include "ash/wm/coordinate_conversion.h" #include "ash/wm/window_positioner.h" #include "base/command_line.h" @@ -286,6 +287,11 @@ TestScreenshotDelegate* AshTestBase::GetScreenshotDelegate() { return ash_test_helper_->test_screenshot_delegate(); } +TestSystemTrayDelegate* AshTestBase::GetSystemTrayDelegate() { + return static_cast<TestSystemTrayDelegate*>( + Shell::GetInstance()->system_tray_delegate()); +} + void AshTestBase::SetSessionStarted(bool session_started) { ash_test_helper_->test_shell_delegate()->test_session_state_delegate()-> SetActiveUserSessionStarted(session_started); diff --git a/ash/test/ash_test_base.h b/ash/test/ash_test_base.h index 7b5bb44..acb7d46 100644 --- a/ash/test/ash_test_base.h +++ b/ash/test/ash_test_base.h @@ -39,6 +39,7 @@ namespace test { class AshTestHelper; class TestScreenshotDelegate; +class TestSystemTrayDelegate; #if defined(OS_WIN) class TestMetroViewerProcessHost; #endif @@ -116,6 +117,7 @@ class AshTestBase : public testing::Test { void RunAllPendingInMessageLoop(); TestScreenshotDelegate* GetScreenshotDelegate(); + TestSystemTrayDelegate* GetSystemTrayDelegate(); // Utility methods to emulate user logged in or not, session started or not // and user able to lock screen or not cases. diff --git a/ash/test/test_system_tray_delegate.cc b/ash/test/test_system_tray_delegate.cc index 0f47aea..473802d 100644 --- a/ash/test/test_system_tray_delegate.cc +++ b/ash/test/test_system_tray_delegate.cc @@ -22,7 +22,8 @@ user::LoginStatus g_initial_status = user::LOGGED_IN_USER; TestSystemTrayDelegate::TestSystemTrayDelegate() : should_show_display_notification_(false), - login_status_(g_initial_status) { + login_status_(g_initial_status), + session_length_limit_set_(false) { } TestSystemTrayDelegate::~TestSystemTrayDelegate() { @@ -39,6 +40,16 @@ void TestSystemTrayDelegate::SetLoginStatus(user::LoginStatus login_status) { Shell::GetInstance()->UpdateAfterLoginStatusChange(login_status); } +void TestSystemTrayDelegate::SetSessionLengthLimitForTest( + const base::TimeDelta& new_limit) { + session_length_limit_ = new_limit; + session_length_limit_set_ = true; +} + +void TestSystemTrayDelegate::ClearSessionLengthLimit() { + session_length_limit_set_ = false; +} + user::LoginStatus TestSystemTrayDelegate::GetUserLoginStatus() const { // Initial login status has been changed for testing. if (g_initial_status != user::LOGGED_IN_USER && @@ -62,6 +73,22 @@ bool TestSystemTrayDelegate::ShouldShowDisplayNotification() { return should_show_display_notification_; } +bool TestSystemTrayDelegate::GetSessionStartTime( + base::TimeTicks* session_start_time) { + // Just returns TimeTicks::Now(), so the remaining time is always the + // specified limit. This is useful for testing. + if (session_length_limit_set_) + *session_start_time = base::TimeTicks::Now(); + return session_length_limit_set_; +} + +bool TestSystemTrayDelegate::GetSessionLengthLimit( + base::TimeDelta* session_length_limit) { + if (session_length_limit_set_) + *session_length_limit = session_length_limit_; + return session_length_limit_set_; +} + void TestSystemTrayDelegate::ShutDown() { base::MessageLoop::current()->Quit(); } diff --git a/ash/test/test_system_tray_delegate.h b/ash/test/test_system_tray_delegate.h index 335f68d..9b85d97 100644 --- a/ash/test/test_system_tray_delegate.h +++ b/ash/test/test_system_tray_delegate.h @@ -6,6 +6,7 @@ #define ASH_TEST_TEST_SYSTEM_TRAY_DELEGATE_H_ #include "ash/system/tray/default_system_tray_delegate.h" +#include "base/time/time.h" namespace ash { namespace test { @@ -33,15 +34,28 @@ class TestSystemTrayDelegate : public DefaultSystemTrayDelegate { should_show_display_notification_ = should_show; } + // Updates the session length limit so that the limit will come from now in + // |new_limit|. + void SetSessionLengthLimitForTest(const base::TimeDelta& new_limit); + + // Clears the session length limit. + void ClearSessionLengthLimit(); + // Overridden from SystemTrayDelegate: virtual user::LoginStatus GetUserLoginStatus() const OVERRIDE; virtual bool ShouldShowDisplayNotification() OVERRIDE; + virtual bool GetSessionStartTime( + base::TimeTicks* session_start_time) OVERRIDE; + virtual bool GetSessionLengthLimit( + base::TimeDelta* session_length_limit) OVERRIDE; virtual void ShutDown() OVERRIDE; virtual void SignOut() OVERRIDE; private: bool should_show_display_notification_; user::LoginStatus login_status_; + base::TimeDelta session_length_limit_; + bool session_length_limit_set_; DISALLOW_COPY_AND_ASSIGN(TestSystemTrayDelegate); }; |