File: notification_counter_view_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (191 lines) | stat: -rw-r--r-- 7,649 bytes parent folder | download | duplicates (6)
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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/system/unified/notification_counter_view.h"

#include "ash/shelf/shelf.h"
#include "ash/system/notification_center/notification_center_tray.h"
#include "ash/system/unified/notification_icons_controller.h"
#include "ash/test/ash_test_base.h"
#include "base/strings/string_number_conversions.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/public/cpp/notification_types.h"
#include "ui/message_center/public/cpp/notifier_id.h"
#include "url/gurl.h"

namespace ash {

namespace {

void AddNotification(const std::string& notification_id,
                     bool is_pinned = false) {
  message_center::RichNotificationData rich_notification_data;
  rich_notification_data.pinned = is_pinned;
  message_center::MessageCenter::Get()->AddNotification(
      std::make_unique<message_center::Notification>(
          message_center::NOTIFICATION_TYPE_SIMPLE, notification_id,
          u"test_title", u"test message", ui::ImageModel(),
          /*display_source=*/std::u16string(), GURL(),
          message_center::NotifierId(message_center::NotifierType::APPLICATION,
                                     "app"),
          rich_notification_data, new message_center::NotificationDelegate()));
}

}  // namespace

class NotificationCounterViewTest : public AshTestBase {
 public:
  NotificationCounterViewTest()
      : AshTestBase(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
  NotificationCounterViewTest(const NotificationCounterViewTest&) = delete;
  NotificationCounterViewTest& operator=(const NotificationCounterViewTest&) =
      delete;
  ~NotificationCounterViewTest() override = default;

 protected:
  NotificationCounterView* GetNotificationCounterView() {
    auto* status_area_widget = GetPrimaryShelf()->status_area_widget();
    return status_area_widget->notification_center_tray()
        ->notification_icons_controller_->notification_counter_view();
  }

  QuietModeView* GetDoNotDisturbIconView() {
    auto* status_area_widget = GetPrimaryShelf()->status_area_widget();
    return status_area_widget->notification_center_tray()
        ->notification_icons_controller_->quiet_mode_view();
  }
};

TEST_F(NotificationCounterViewTest, CountForDisplay) {
  // Not visible when count == 0.
  GetNotificationCounterView()->Update();
  EXPECT_EQ(0, GetNotificationCounterView()->count_for_display_for_testing());
  EXPECT_FALSE(GetNotificationCounterView()->GetVisible());

  // Count is visible and updates between 1..max+1.
  int max = static_cast<int>(kTrayNotificationMaxCount);
  for (int i = 1; i <= max + 1; i++) {
    AddNotification(base::NumberToString(i));
    GetNotificationCounterView()->Update();
    EXPECT_EQ(i, GetNotificationCounterView()->count_for_display_for_testing());
    EXPECT_TRUE(GetNotificationCounterView()->GetVisible());
  }

  // Count does not change after max+1.
  AddNotification(base::NumberToString(max + 2));
  GetNotificationCounterView()->Update();
  EXPECT_EQ(max + 1,
            GetNotificationCounterView()->count_for_display_for_testing());
  EXPECT_TRUE(GetNotificationCounterView()->GetVisible());
}

TEST_F(NotificationCounterViewTest, HiddenNotificationCount) {
  // Not visible when count == 0.
  GetNotificationCounterView()->Update();
  EXPECT_EQ(0, GetNotificationCounterView()->count_for_display_for_testing());
  EXPECT_FALSE(GetNotificationCounterView()->GetVisible());

  // Added a pinned notification, counter should not be visible.
  AddNotification("1", true /* is_pinned */);
  GetNotificationCounterView()->Update();
  EXPECT_TRUE(!GetNotificationCounterView()->GetVisible());

  // Added a normal notification.
  AddNotification("2");
  GetNotificationCounterView()->Update();
  EXPECT_TRUE(GetNotificationCounterView()->GetVisible());
  EXPECT_EQ(1, GetNotificationCounterView()->count_for_display_for_testing());

  // Added another pinned.
  AddNotification("3", true /* is_pinned */);
  GetNotificationCounterView()->Update();
  EXPECT_TRUE(GetNotificationCounterView()->GetVisible());
  EXPECT_EQ(1, GetNotificationCounterView()->count_for_display_for_testing());

  message_center::MessageCenter::Get()->RemoveNotification("1",
                                                           false /* by_user */);
  message_center::MessageCenter::Get()->RemoveNotification("3",
                                                           false /* by_user */);
  GetNotificationCounterView()->Update();
  EXPECT_EQ(1, GetNotificationCounterView()->count_for_display_for_testing());
}

TEST_F(NotificationCounterViewTest, DisplayChanged) {
  AddNotification("1", true /* is_pinned */);
  GetNotificationCounterView()->Update();

  // In medium size screen, the counter should not be displayed since pinned
  // notification icon is shown (if the feature is enabled).
  UpdateDisplay("800x700");
  EXPECT_TRUE(!GetNotificationCounterView()->GetVisible());

  // The counter should not be shown when we remove the pinned notification.
  message_center::MessageCenter::Get()->RemoveNotification("1",
                                                           false /* by_user */);
  GetNotificationCounterView()->Update();
  EXPECT_FALSE(GetNotificationCounterView()->GetVisible());

  AddNotification("1", true /* is_pinned */);
  GetNotificationCounterView()->Update();

  // In small display, the counter show be shown with pinned notification.
  UpdateDisplay("600x500");
  EXPECT_TRUE(GetNotificationCounterView()->GetVisible());

  // In large screen size, expected the same behavior like medium screen size.
  UpdateDisplay("1680x800");
  EXPECT_TRUE(!GetNotificationCounterView()->GetVisible());

  message_center::MessageCenter::Get()->RemoveNotification("1",
                                                           false /* by_user */);
  GetNotificationCounterView()->Update();
  EXPECT_FALSE(GetNotificationCounterView()->GetVisible());
}

TEST_F(NotificationCounterViewTest, DoNotDisturbIconVisibility) {
  ASSERT_FALSE(GetDoNotDisturbIconView()->GetVisible());

  // Turn on Do not disturb mode.
  message_center::MessageCenter::Get()->SetQuietMode(true);
  EXPECT_TRUE(GetDoNotDisturbIconView()->GetVisible());

  // Show the lock screen.
  BlockUserSession(BLOCKED_BY_LOCK_SCREEN);
  EXPECT_FALSE(GetDoNotDisturbIconView()->GetVisible());

  // Log in.
  UnblockUserSession();
  EXPECT_TRUE(GetDoNotDisturbIconView()->GetVisible());
}

TEST_F(NotificationCounterViewTest, LockScreenCounter) {
  for (size_t i = 0; i < kTrayNotificationMaxCount; i++) {
    AddNotification(base::NumberToString(i));
  }

  // Make sure we show the full count of notifications on the lock screen.
  BlockUserSession(BLOCKED_BY_LOCK_SCREEN);
  EXPECT_TRUE(GetNotificationCounterView()->GetVisible());
  EXPECT_EQ(static_cast<int>(kTrayNotificationMaxCount),
            GetNotificationCounterView()->count_for_display_for_testing());
}

TEST_F(NotificationCounterViewTest, LockScreenCounterInDoNotDisturbMode) {
  for (size_t i = 0; i < kTrayNotificationMaxCount; i++) {
    AddNotification(base::NumberToString(i));
  }

  // Turn on Do not disturb mode.
  message_center::MessageCenter::Get()->SetQuietMode(true);

  // Counter not shown when Do not disturb mode is enabled.
  EXPECT_FALSE(GetNotificationCounterView()->GetVisible());

  // Counter should become visible if the screen is locked.
  BlockUserSession(BLOCKED_BY_LOCK_SCREEN);
  EXPECT_TRUE(GetNotificationCounterView()->GetVisible());
}

}  // namespace ash