Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ash / system / chromeos / session / tray_session_length_limit_unittest.cc
1 // Copyright 2014 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/session/tray_session_length_limit.h"
6
7 #include "ash/root_window_controller.h"
8 #include "ash/shell.h"
9 #include "ash/system/tray/system_tray.h"
10 #include "ash/test/ash_test_base.h"
11 #include "ash/test/test_system_tray_delegate.h"
12 #include "base/time/time.h"
13 #include "ui/message_center/message_center.h"
14 #include "ui/message_center/notification.h"
15 #include "ui/message_center/notification_types.h"
16
17 namespace ash {
18 namespace test {
19
20 class TraySessionLengthLimitTest : public AshTestBase {
21  public:
22   TraySessionLengthLimitTest() {}
23   virtual ~TraySessionLengthLimitTest() {}
24
25   virtual void SetUp() override {
26     AshTestBase::SetUp();
27     SystemTray* system_tray =
28         Shell::GetPrimaryRootWindowController()->GetSystemTray();
29     tray_session_length_limit_ = new TraySessionLengthLimit(system_tray);
30     system_tray->AddTrayItem(tray_session_length_limit_);
31   }
32
33   virtual void TearDown() override {
34     AshTestBase::TearDown();
35   }
36
37  protected:
38   void UpdateSessionLengthLimitInMin(int mins) {
39     GetSystemTrayDelegate()->SetSessionLengthLimitForTest(
40         base::TimeDelta::FromMinutes(mins));
41     tray_session_length_limit_->OnSessionLengthLimitChanged();
42   }
43
44   message_center::Notification* GetNotification() {
45     const message_center::NotificationList::Notifications& notifications =
46         message_center::MessageCenter::Get()->GetVisibleNotifications();
47     for (message_center::NotificationList::Notifications::const_iterator iter =
48              notifications.begin(); iter != notifications.end(); ++iter) {
49       if ((*iter)->id() == TraySessionLengthLimit::kNotificationId)
50         return *iter;
51     }
52     return NULL;
53   }
54
55   void ClearSessionLengthLimit() {
56     GetSystemTrayDelegate()->ClearSessionLengthLimit();
57     tray_session_length_limit_->OnSessionLengthLimitChanged();
58   }
59
60   void RemoveNotification() {
61     message_center::MessageCenter::Get()->RemoveNotification(
62         TraySessionLengthLimit::kNotificationId, false /* by_user */);
63   }
64
65   TraySessionLengthLimit* tray_session_length_limit() {
66     return tray_session_length_limit_;
67   }
68
69  private:
70   // Weak reference, owned by the SystemTray.
71   TraySessionLengthLimit* tray_session_length_limit_;
72
73   DISALLOW_COPY_AND_ASSIGN(TraySessionLengthLimitTest);
74 };
75
76 TEST_F(TraySessionLengthLimitTest, Notification) {
77   // No notifications when no session limit.
78   EXPECT_FALSE(GetNotification());
79
80   // Limit is 15 min.
81   UpdateSessionLengthLimitInMin(15);
82   message_center::Notification* notification = GetNotification();
83   EXPECT_TRUE(notification);
84   EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority());
85   base::string16 first_content = notification->message();
86   // Should read the content.
87   EXPECT_TRUE(notification->rich_notification_data().
88               should_make_spoken_feedback_for_popup_updates);
89
90   // Limit is 10 min.
91   UpdateSessionLengthLimitInMin(10);
92   notification = GetNotification();
93   EXPECT_TRUE(notification);
94   EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority());
95   // The content should be updated.
96   EXPECT_NE(first_content, notification->message());
97   // Should NOT read, because just update the remaining time.
98   EXPECT_FALSE(notification->rich_notification_data().
99                should_make_spoken_feedback_for_popup_updates);
100
101   // Limit is 3 min.
102   UpdateSessionLengthLimitInMin(3);
103   notification = GetNotification();
104   EXPECT_TRUE(notification);
105   EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority());
106   // Should read the content again because the state has changed.
107   EXPECT_TRUE(notification->rich_notification_data().
108               should_make_spoken_feedback_for_popup_updates);
109
110   // Session length limit is updated to longer: 15 min.
111   UpdateSessionLengthLimitInMin(15);
112   notification = GetNotification();
113   EXPECT_TRUE(notification);
114   EXPECT_EQ(message_center::SYSTEM_PRIORITY, notification->priority());
115   // Should read again because an increase of the remaining time is noteworthy.
116   EXPECT_TRUE(notification->rich_notification_data().
117               should_make_spoken_feedback_for_popup_updates);
118
119   // Clears the limit: the notification should be gone.
120   ClearSessionLengthLimit();
121   EXPECT_FALSE(GetNotification());
122 }
123
124 TEST_F(TraySessionLengthLimitTest, RemoveNotification) {
125   message_center::Notification* notification;
126
127   // Limit is 15 min.
128   UpdateSessionLengthLimitInMin(15);
129   EXPECT_TRUE(GetNotification());
130
131   // Removes the notification.
132   RemoveNotification();
133   EXPECT_FALSE(GetNotification());
134
135   // Limit is 10 min. The notification should not re-appear.
136   UpdateSessionLengthLimitInMin(10);
137   EXPECT_FALSE(GetNotification());
138
139   // Limit is 3 min. The notification should re-appear and should be re-read
140   // because of state change.
141   UpdateSessionLengthLimitInMin(3);
142   notification = GetNotification();
143   EXPECT_TRUE(notification);
144   EXPECT_TRUE(notification->rich_notification_data().
145               should_make_spoken_feedback_for_popup_updates);
146
147   RemoveNotification();
148
149   // Session length limit is updated to longer state. Notification should
150   // re-appear and be re-read.
151   UpdateSessionLengthLimitInMin(15);
152   notification = GetNotification();
153   EXPECT_TRUE(notification);
154   EXPECT_TRUE(notification->rich_notification_data().
155               should_make_spoken_feedback_for_popup_updates);
156 }
157
158 }  // namespace test
159 }  // namespace ash