summaryrefslogtreecommitdiffstats
path: root/chromium/ash/system/session_length_limit/tray_session_length_limit_unittest.cc
blob: 2b05de9c381e1923bf3e400536b83f815edff465 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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