Commit 1adc855a authored by Andre Le's avatar Andre Le Committed by Commit Bot

Ash Notification: Add unpinned notification count in ash message center.

Add the logic to update visibility for "Clear all" button depends on the
count of total and unpinned notification.

BUG=1111167

Change-Id: Ie609ae1225c0041e3c45815b1f4c2550136ace77
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2339942
Commit-Queue: Andre Le <leandre@chromium.org>
Reviewed-by: default avatarTim Song <tengs@chromium.org>
Cr-Commit-Position: refs/heads/master@{#799890}
parent 6facaf56
......@@ -295,25 +295,29 @@ StackedNotificationBar::~StackedNotificationBar() {
bool StackedNotificationBar::Update(
int total_notification_count,
int pinned_notification_count,
std::vector<message_center::Notification*> stacked_notifications) {
int stacked_notification_count = stacked_notifications.size();
if (total_notification_count == total_notification_count_ &&
pinned_notification_count == pinned_notification_count_ &&
stacked_notification_count == stacked_notification_count_)
return false;
total_notification_count_ = total_notification_count;
pinned_notification_count_ = pinned_notification_count;
UpdateStackedNotifications(stacked_notifications);
UpdateVisibility();
int unpinned_count = total_notification_count_ - pinned_notification_count_;
auto tooltip = l10n_util::GetStringFUTF16Int(
IDS_ASH_MESSAGE_CENTER_STACKING_BAR_CLEAR_ALL_BUTTON_TOOLTIP,
total_notification_count_);
unpinned_count);
clear_all_button_->SetTooltipText(tooltip);
clear_all_button_->SetAccessibleName(tooltip);
UpdateStackedNotifications(stacked_notifications);
return true;
}
......@@ -484,16 +488,26 @@ const char* StackedNotificationBar::GetClassName() const {
}
void StackedNotificationBar::UpdateVisibility() {
int unpinned_count = total_notification_count_ - pinned_notification_count_;
// In expanded state, clear all button should be visible when (rule is subject
// to change):
// 1. There are more than one notification.
// 2. There is at least one unpinned notification
bool show_clear_all = total_notification_count_ > 1 && unpinned_count >= 1;
if (!expand_all_button_->GetVisible())
clear_all_button_->SetVisible(show_clear_all);
switch (animation_state_) {
case UnifiedMessageCenterAnimationState::IDLE:
SetVisible(total_notification_count_ > 1 ||
SetVisible(stacked_notification_count_ || show_clear_all ||
expand_all_button_->GetVisible());
break;
case UnifiedMessageCenterAnimationState::HIDE_STACKING_BAR:
SetVisible(true);
break;
case UnifiedMessageCenterAnimationState::COLLAPSE:
SetVisible(total_notification_count_ > 1 ||
SetVisible(stacked_notification_count_ || show_clear_all ||
expand_all_button_->GetVisible());
break;
}
......
......@@ -37,9 +37,10 @@ class StackedNotificationBar : public views::View,
~StackedNotificationBar() override;
// Sets the icons and overflow count for hidden notifications as well as the
// total notifications count. Returns true if the state of the bar has
// changed.
// total/pinned notifications count. Returns true if the state of the bar
// has changed.
bool Update(int total_notification_count,
int pinned_notification_count,
std::vector<message_center::Notification*> stacked_notifications);
// Sets the current animation state.
......@@ -98,6 +99,7 @@ class StackedNotificationBar : public views::View,
std::vector<message_center::Notification*> stacked_notifications);
int total_notification_count_ = 0;
int pinned_notification_count_ = 0;
int stacked_notification_count_ = 0;
UnifiedMessageCenterAnimationState animation_state_ =
......
......@@ -94,8 +94,10 @@ UnifiedMessageCenterView::UnifiedMessageCenterView(
scroller_->SetDrawOverflowIndicator(false);
AddChildView(scroller_);
notification_bar_->Update(message_list_view_->GetTotalNotificationCount(),
GetStackedNotifications());
notification_bar_->Update(
message_list_view_->GetTotalNotificationCount(),
message_list_view_->GetTotalPinnedNotificationCount(),
GetStackedNotifications());
}
UnifiedMessageCenterView::~UnifiedMessageCenterView() {
......@@ -163,16 +165,16 @@ bool UnifiedMessageCenterView::IsNotificationBarVisible() {
}
void UnifiedMessageCenterView::OnNotificationSlidOut() {
if (collapsed_) {
if (!message_list_view_->GetTotalNotificationCount())
if (notification_bar_->GetVisible()) {
notification_bar_->Update(
message_list_view_->GetTotalNotificationCount(),
message_list_view_->GetTotalPinnedNotificationCount(),
GetStackedNotifications());
if (!notification_bar_->GetVisible())
StartHideStackingBarAnimation();
return;
}
if (notification_bar_->GetVisible() &&
message_list_view_->GetTotalNotificationCount() <= 1) {
StartHideStackingBarAnimation();
} else if (!message_list_view_->GetTotalNotificationCount()) {
if (!message_list_view_->GetTotalNotificationCount()) {
StartCollapseAnimation();
}
}
......@@ -270,9 +272,10 @@ void UnifiedMessageCenterView::OnMessageCenterScrolled() {
model_->set_notification_target_mode(
UnifiedSystemTrayModel::NotificationTargetMode::LAST_POSITION);
bool was_count_updated =
notification_bar_->Update(message_list_view_->GetTotalNotificationCount(),
GetStackedNotifications());
bool was_count_updated = notification_bar_->Update(
message_list_view_->GetTotalNotificationCount(),
message_list_view_->GetTotalPinnedNotificationCount(),
GetStackedNotifications());
if (was_count_updated) {
const int previous_y = scroller_->y();
Layout();
......@@ -451,8 +454,10 @@ void UnifiedMessageCenterView::ScrollToTarget() {
}
scroller_->ScrollToPosition(scroll_bar_, position);
notification_bar_->Update(message_list_view_->GetTotalNotificationCount(),
GetStackedNotifications());
notification_bar_->Update(
message_list_view_->GetTotalNotificationCount(),
message_list_view_->GetTotalPinnedNotificationCount(),
GetStackedNotifications());
last_scroll_position_from_bottom_ =
scroll_bar_->GetMaxPosition() - scroller_->GetVisibleRect().y();
}
......@@ -464,6 +469,13 @@ UnifiedMessageCenterView::GetStackedNotifications() const {
if (scroller_->bounds().IsEmpty())
scroller_->SetBoundsRect(GetContentsBounds());
// If nothing is hidden in the scroller view, this means notification bar is
// not shown. Thus, we should not consider it in the calculation.
int notification_bar_height =
scroller_->GetVisibleRect().y() == scroller_->y()
? 0
: kStackedNotificationBarHeight;
// Use this y offset to count number of hidden notifications.
// Set to the bottom of the last notification when message center is
// collapsed. Set below stacked notification bar when message center is
......@@ -474,12 +486,11 @@ UnifiedMessageCenterView::GetStackedNotifications() const {
y_offset = last_bounds.y() + last_bounds.height();
} else {
y_offset = scroller_->GetVisibleRect().y() - scroller_->y() +
kStackedNotificationBarHeight;
notification_bar_height;
}
return message_list_view_->GetNotificationsAboveY(y_offset);
}
void UnifiedMessageCenterView::FocusOut(bool reverse) {
if (message_center_bubble_ && message_center_bubble_->FocusOut(reverse)) {
GetFocusManager()->ClearFocus();
......
......@@ -85,13 +85,15 @@ class UnifiedMessageCenterViewTest : public AshTestBase,
}
protected:
std::string AddNotification() {
std::string AddNotification(bool pinned) {
std::string id = base::NumberToString(id_++);
message_center::RichNotificationData data;
data.pinned = pinned;
MessageCenter::Get()->AddNotification(std::make_unique<Notification>(
message_center::NOTIFICATION_TYPE_BASE_FORMAT, id,
base::UTF8ToUTF16("test title"), base::UTF8ToUTF16("test message"),
gfx::Image(), base::string16() /* display_source */, GURL(),
message_center::NotifierId(), message_center::RichNotificationData(),
message_center::NotifierId(), data,
new message_center::NotificationDelegate()));
return id;
}
......@@ -253,7 +255,7 @@ TEST_F(UnifiedMessageCenterViewTest, AddAndRemoveNotification) {
CreateMessageCenterView();
EXPECT_FALSE(message_center_view()->GetVisible());
auto id0 = AddNotification();
auto id0 = AddNotification(false /* pinned */);
EXPECT_TRUE(message_center_view()->GetVisible());
// The notification first slides out of the list.
......@@ -276,13 +278,13 @@ TEST_F(UnifiedMessageCenterViewTest, AddAndRemoveNotification) {
TEST_F(UnifiedMessageCenterViewTest, RemoveNotificationAtTail) {
// Show message center with multiple notifications.
for (int i = 0; i < 10; ++i)
AddNotification();
AddNotification(false /* pinned */);
CreateMessageCenterView();
EXPECT_TRUE(message_center_view()->GetVisible());
// The message center should autoscroll to the bottom of the list (with some
// padding) after adding a new notification.
auto id_to_remove = AddNotification();
auto id_to_remove = AddNotification(false /* pinned */);
int spacing = 0;
int scroll_position = GetScroller()->GetVisibleRect().y();
EXPECT_EQ(GetMessageListView()->height() - GetScroller()->height() + spacing,
......@@ -306,7 +308,7 @@ TEST_F(UnifiedMessageCenterViewTest, RemoveNotificationAtTail) {
TEST_F(UnifiedMessageCenterViewTest, ContentsRelayout) {
std::vector<std::string> ids;
for (size_t i = 0; i < 10; ++i)
ids.push_back(AddNotification());
ids.push_back(AddNotification(false /* pinned */));
CreateMessageCenterView();
EXPECT_TRUE(message_center_view()->GetVisible());
// MessageCenterView is maxed out.
......@@ -324,7 +326,7 @@ TEST_F(UnifiedMessageCenterViewTest, ContentsRelayout) {
TEST_F(UnifiedMessageCenterViewTest, InsufficientHeight) {
CreateMessageCenterView();
AddNotification();
AddNotification(false /* pinned */);
EXPECT_TRUE(message_center_view()->GetVisible());
message_center_view()->SetAvailableHeight(kUnifiedNotificationMinimumHeight -
......@@ -344,8 +346,8 @@ TEST_F(UnifiedMessageCenterViewTest, NotVisibleWhenLocked) {
ASSERT_FALSE(AshMessageCenterLockScreenController::IsEnabled());
AddNotification();
AddNotification();
AddNotification(false /* pinned */);
AddNotification(false /* pinned */);
BlockUserSession(BLOCKED_BY_LOCK_SCREEN);
CreateMessageCenterView();
......@@ -367,8 +369,8 @@ TEST_F(UnifiedMessageCenterViewTest, VisibleWhenLocked) {
ASSERT_TRUE(AshMessageCenterLockScreenController::IsEnabled());
AddNotification();
AddNotification();
AddNotification(false /* pinned */);
AddNotification(false /* pinned */);
BlockUserSession(BLOCKED_BY_LOCK_SCREEN);
CreateMessageCenterView();
......@@ -377,8 +379,8 @@ TEST_F(UnifiedMessageCenterViewTest, VisibleWhenLocked) {
}
TEST_F(UnifiedMessageCenterViewTest, ClearAllPressed) {
AddNotification();
AddNotification();
AddNotification(false /* pinned */);
AddNotification(false /* pinned */);
CreateMessageCenterView();
EXPECT_TRUE(message_center_view()->GetVisible());
EXPECT_TRUE(GetNotificationBar()->GetVisible());
......@@ -391,8 +393,8 @@ TEST_F(UnifiedMessageCenterViewTest, ClearAllPressed) {
}
TEST_F(UnifiedMessageCenterViewTest, InitialPosition) {
AddNotification();
AddNotification();
AddNotification(false /* pinned */);
AddNotification(false /* pinned */);
CreateMessageCenterView();
EXPECT_TRUE(message_center_view()->GetVisible());
......@@ -403,7 +405,7 @@ TEST_F(UnifiedMessageCenterViewTest, InitialPosition) {
TEST_F(UnifiedMessageCenterViewTest, InitialPositionMaxOut) {
for (size_t i = 0; i < 6; ++i)
AddNotification();
AddNotification(false /* pinned */);
CreateMessageCenterView();
EXPECT_TRUE(message_center_view()->GetVisible());
......@@ -413,8 +415,8 @@ TEST_F(UnifiedMessageCenterViewTest, InitialPositionMaxOut) {
}
TEST_F(UnifiedMessageCenterViewTest, InitialPositionWithLargeNotification) {
AddNotification();
AddNotification();
AddNotification(false /* pinned */);
AddNotification(false /* pinned */);
CreateMessageCenterView(80 /* max_height */);
EXPECT_TRUE(message_center_view()->GetVisible());
......@@ -429,7 +431,7 @@ TEST_F(UnifiedMessageCenterViewTest, InitialPositionWithLargeNotification) {
TEST_F(UnifiedMessageCenterViewTest, ScrollPositionWhenResized) {
for (size_t i = 0; i < 6; ++i)
AddNotification();
AddNotification(false /* pinned */);
CreateMessageCenterView();
EXPECT_TRUE(message_center_view()->GetVisible());
......@@ -460,7 +462,7 @@ TEST_F(UnifiedMessageCenterViewTest, ScrollPositionWhenResized) {
TEST_F(UnifiedMessageCenterViewTest, StackingCounterLayout) {
for (size_t i = 0; i < 10; ++i)
AddNotification();
AddNotification(false /* pinned */);
// MessageCenterView is maxed out.
CreateMessageCenterView();
......@@ -486,7 +488,7 @@ TEST_F(UnifiedMessageCenterViewTest, StackingCounterLayout) {
TEST_F(UnifiedMessageCenterViewTest, StackingCounterMessageListScrolled) {
for (size_t i = 0; i < 10; ++i)
AddNotification();
AddNotification(false /* pinned */);
CreateMessageCenterView();
EXPECT_TRUE(message_center_view()->GetVisible());
EXPECT_TRUE(GetNotificationBarLabel()->GetVisible());
......@@ -522,7 +524,7 @@ TEST_F(UnifiedMessageCenterViewTest, StackingCounterMessageListScrolled) {
TEST_F(UnifiedMessageCenterViewTest, StackingCounterNotificationRemoval) {
std::vector<std::string> ids;
for (size_t i = 0; i < 6; ++i)
ids.push_back(AddNotification());
ids.push_back(AddNotification(false /* pinned */));
CreateMessageCenterView();
EXPECT_TRUE(message_center_view()->GetVisible());
......@@ -571,9 +573,9 @@ TEST_F(UnifiedMessageCenterViewTest, StackingCounterNotificationRemoval) {
TEST_F(UnifiedMessageCenterViewTest, StackingCounterLabelRelaidOutOnScroll) {
// Open the message center at the top of the notification list so the stacking
// bar is hidden by default.
std::string id = AddNotification();
std::string id = AddNotification(false /* pinned */);
for (size_t i = 0; i < 30; ++i)
AddNotification();
AddNotification(false /* pinned */);
model()->SetTargetNotification(id);
CreateMessageCenterView();
......@@ -595,6 +597,44 @@ TEST_F(UnifiedMessageCenterViewTest, StackingCounterLabelRelaidOutOnScroll) {
EXPECT_GT(GetNotificationBarLabel()->bounds().width(), label_width);
}
TEST_F(UnifiedMessageCenterViewTest, StackingCounterVisibility) {
std::string id0 = AddNotification(false /* pinned */);
std::string id1 = AddNotification(false /* pinned */);
CreateMessageCenterView();
// The bar should be visible with 2 unpinned notifications.
EXPECT_TRUE(GetNotificationBar()->GetVisible());
EXPECT_TRUE(GetNotificationBarClearAllButton()->GetVisible());
MessageCenter::Get()->RemoveNotification(id0, true /* by_user */);
AnimateMessageListToEnd();
auto* hide_animation = GetMessageCenterAnimation();
hide_animation->End();
// The bar should be hidden with 1 notification.
EXPECT_FALSE(GetNotificationBar()->GetVisible());
MessageCenter::Get()->RemoveNotification(id1, true /* by_user */);
AddNotification(true /* pinned */);
AddNotification(true /* pinned */);
// The bar should not be visible with 2 pinned notifications (none of the
// notifications are hidden).
EXPECT_FALSE(GetNotificationBar()->GetVisible());
for (size_t i = 0; i < 4; ++i)
AddNotification(true /* pinned */);
// The bar should be visible with 6 pinned notifications (some of the
// notifications are hidden). However, clear all button should not be shown.
EXPECT_TRUE(GetNotificationBar()->GetVisible());
EXPECT_FALSE(GetNotificationBarClearAllButton()->GetVisible());
// Add 1 unpinned notifications. Clear all should now be shown.
AddNotification(false /* pinned */);
EXPECT_TRUE(GetNotificationBarClearAllButton()->GetVisible());
}
// We need a widget to initialize a FocusManager.
TEST_F(UnifiedMessageCenterViewInWidgetTest,
FocusClearedAfterNotificationRemoval) {
......@@ -603,8 +643,8 @@ TEST_F(UnifiedMessageCenterViewInWidgetTest,
widget()->Show();
// Add notifications and focus on a child view in the last notification.
AddNotification();
auto id1 = AddNotification();
AddNotification(false /* pinned */);
auto id1 = AddNotification(false /* pinned */);
// Toggle focus to the last notification MessageView.
auto* focused_message_view =
......@@ -619,8 +659,8 @@ TEST_F(UnifiedMessageCenterViewInWidgetTest,
}
TEST_F(UnifiedMessageCenterViewTest, CollapseAndExpand_NonAnimated) {
AddNotification();
AddNotification();
AddNotification(false /* pinned */);
AddNotification(false /* pinned */);
CreateMessageCenterView();
EXPECT_TRUE(GetScroller()->GetVisible());
EXPECT_TRUE(GetNotificationBarClearAllButton()->GetVisible());
......@@ -641,8 +681,8 @@ TEST_F(UnifiedMessageCenterViewTest, CollapseAndExpand_NonAnimated) {
}
TEST_F(UnifiedMessageCenterViewTest, CollapseAndExpand_Animated) {
AddNotification();
AddNotification();
AddNotification(false /* pinned */);
AddNotification(false /* pinned */);
CreateMessageCenterView();
EXPECT_TRUE(GetScroller()->GetVisible());
......
......@@ -304,6 +304,15 @@ int UnifiedMessageListView::GetTotalNotificationCount() const {
return int{children().size()};
}
int UnifiedMessageListView::GetTotalPinnedNotificationCount() const {
int count = 0;
for (auto* child : children()) {
if (AsMVC(child)->IsPinned())
count++;
}
return count;
}
bool UnifiedMessageListView::IsAnimating() const {
return animation_->is_animating();
}
......
......@@ -66,6 +66,9 @@ class ASH_EXPORT UnifiedMessageListView
// Returns the total number of notifications in the list.
int GetTotalNotificationCount() const;
// Returns the total number of pinned notifications in the list.
int GetTotalPinnedNotificationCount() const;
// Returns true if an animation is currently in progress.
bool IsAnimating() const;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment