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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
|
// 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 "ui/message_center/views/message_center_view.h"
#include <map>
#include <memory>
#include <utility>
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/message_center/fake_message_center.h"
#include "ui/message_center/message_center_style.h"
#include "ui/message_center/notification.h"
#include "ui/message_center/notification_list.h"
#include "ui/message_center/notification_types.h"
#include "ui/message_center/views/message_center_button_bar.h"
#include "ui/message_center/views/message_center_controller.h"
#include "ui/message_center/views/message_list_view.h"
#include "ui/message_center/views/notification_view.h"
#include "ui/views/controls/slide_out_view.h"
#include "ui/views/test/views_test_base.h"
namespace message_center {
static const char* kNotificationId1 = "notification id 1";
static const char* kNotificationId2 = "notification id 2";
/* Types **********************************************************************/
enum CallType {
GET_PREFERRED_SIZE,
GET_HEIGHT_FOR_WIDTH,
LAYOUT
};
class DummyEvent : public ui::Event {
public:
DummyEvent() : Event(ui::ET_UNKNOWN, base::TimeTicks(), 0) {}
~DummyEvent() override {}
};
/* Instrumented/Mock NotificationView subclass ********************************/
class MockNotificationView : public NotificationView {
public:
class Test {
public:
virtual void RegisterCall(CallType type) = 0;
};
explicit MockNotificationView(MessageCenterController* controller,
const Notification& notification,
Test* test);
~MockNotificationView() override;
gfx::Size GetPreferredSize() const override;
int GetHeightForWidth(int w) const override;
void Layout() override;
private:
Test* test_;
DISALLOW_COPY_AND_ASSIGN(MockNotificationView);
};
MockNotificationView::MockNotificationView(MessageCenterController* controller,
const Notification& notification,
Test* test)
: NotificationView(controller, notification),
test_(test) {
}
MockNotificationView::~MockNotificationView() {
}
gfx::Size MockNotificationView::GetPreferredSize() const {
test_->RegisterCall(GET_PREFERRED_SIZE);
DCHECK(child_count() > 0);
return NotificationView::GetPreferredSize();
}
int MockNotificationView::GetHeightForWidth(int width) const {
test_->RegisterCall(GET_HEIGHT_FOR_WIDTH);
DCHECK(child_count() > 0);
return NotificationView::GetHeightForWidth(width);
}
void MockNotificationView::Layout() {
test_->RegisterCall(LAYOUT);
DCHECK(child_count() > 0);
NotificationView::Layout();
}
class FakeMessageCenterImpl : public FakeMessageCenter {
public:
const NotificationList::Notifications& GetVisibleNotifications() override {
return visible_notifications_;
}
void SetVisibleNotifications(NotificationList::Notifications notifications) {
visible_notifications_ = notifications;
}
void RemoveAllNotifications(bool by_user, RemoveType type) override {
if (type == RemoveType::NON_PINNED)
remove_all_closable_notification_called_ = true;
}
bool IsLockedState() const override { return locked_; }
bool remove_all_closable_notification_called_ = false;
NotificationList::Notifications visible_notifications_;
bool locked_ = false;
};
/* Test fixture ***************************************************************/
class MessageCenterViewTest : public views::ViewsTestBase,
public MockNotificationView::Test,
public MessageCenterController {
public:
// Expose the private enum class MessageCenter::Mode for this test.
using Mode = MessageCenterView::Mode;
MessageCenterViewTest();
~MessageCenterViewTest() override;
void SetUp() override;
void TearDown() override;
NotificationList::Notifications Notifications();
MessageCenterView* GetMessageCenterView();
MessageListView* GetMessageListView();
FakeMessageCenterImpl* GetMessageCenter() const;
MessageView* GetNotificationView(const std::string& id);
views::BoundsAnimator* GetAnimator();
int GetNotificationCount();
int GetCallCount(CallType type);
int GetCalculatedMessageListViewHeight();
void SetLockedState(bool locked);
Mode GetMessageCenterViewInternalMode();
void AddNotification(std::unique_ptr<Notification> notification);
void UpdateNotification(const std::string& notification_id,
std::unique_ptr<Notification> notification);
// Overridden from MessageCenterController:
void ClickOnNotification(const std::string& notification_id) override;
void RemoveNotification(const std::string& notification_id,
bool by_user) override;
std::unique_ptr<ui::MenuModel> CreateMenuModel(
const NotifierId& notifier_id,
const base::string16& display_source) override;
bool HasClickedListener(const std::string& notification_id) override;
void ClickOnNotificationButton(const std::string& notification_id,
int button_index) override;
void ClickOnSettingsButton(const std::string& notification_id) override;
// Overridden from MockNotificationView::Test
void RegisterCall(CallType type) override;
void FireOnMouseExitedEvent();
void LogBounds(int depth, views::View* view);
MessageCenterButtonBar* GetButtonBar() const;
void RemoveDefaultNotifications();
private:
views::View* MakeParent(views::View* child1, views::View* child2);
// The ownership map of notifications; the key is the id.
std::map<std::string, std::unique_ptr<Notification>> notifications_;
std::unique_ptr<views::Widget> widget_;
std::unique_ptr<MessageCenterView> message_center_view_;
std::unique_ptr<FakeMessageCenterImpl> message_center_;
std::map<CallType,int> callCounts_;
DISALLOW_COPY_AND_ASSIGN(MessageCenterViewTest);
};
MessageCenterViewTest::MessageCenterViewTest() {
}
MessageCenterViewTest::~MessageCenterViewTest() {
}
void MessageCenterViewTest::SetUp() {
views::ViewsTestBase::SetUp();
MessageCenterView::disable_animation_for_testing = true;
message_center_.reset(new FakeMessageCenterImpl());
// Create a dummy notification.
std::unique_ptr<Notification> notification1 = base::MakeUnique<Notification>(
NOTIFICATION_TYPE_SIMPLE, std::string(kNotificationId1),
base::UTF8ToUTF16("title"), base::UTF8ToUTF16("message1"), gfx::Image(),
base::UTF8ToUTF16("display source"), GURL(),
NotifierId(NotifierId::APPLICATION, "extension_id"),
message_center::RichNotificationData(), nullptr);
std::unique_ptr<Notification> notification2 = base::MakeUnique<Notification>(
NOTIFICATION_TYPE_SIMPLE, std::string(kNotificationId2),
base::UTF8ToUTF16("title2"), base::UTF8ToUTF16("message2"), gfx::Image(),
base::UTF8ToUTF16("display source"), GURL(),
NotifierId(NotifierId::APPLICATION, "extension_id"),
message_center::RichNotificationData(), nullptr);
// ...and a list for it.
notifications_[std::string(kNotificationId1)] = std::move(notification1);
notifications_[std::string(kNotificationId2)] = std::move(notification2);
NotificationList::Notifications notifications = Notifications();
message_center_->SetVisibleNotifications(notifications);
// Then create a new MessageCenterView with that single notification.
message_center_view_.reset(new MessageCenterView(
message_center_.get(), NULL, 100, false));
GetMessageListView()->quit_message_loop_after_animation_for_test_ = true;
GetMessageCenterView()->SetBounds(0, 0, 380, 600);
message_center_view_->SetNotifications(notifications);
message_center_view_->set_owned_by_client();
widget_.reset(new views::Widget());
views::Widget::InitParams params =
CreateParams(views::Widget::InitParams::TYPE_POPUP);
params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.bounds = gfx::Rect(50, 50, 650, 650);
widget_->Init(params);
views::View* root = widget_->GetRootView();
root->AddChildView(message_center_view_.get());
widget_->Show();
widget_->Activate();
// Wait until the animation finishes if available.
if (GetAnimator()->IsAnimating())
base::RunLoop().Run();
}
void MessageCenterViewTest::TearDown() {
widget_->CloseNow();
widget_.reset();
message_center_view_.reset();
notifications_.clear();
views::ViewsTestBase::TearDown();
}
NotificationList::Notifications MessageCenterViewTest::Notifications() {
NotificationList::Notifications result;
for (const auto& notification_pair : notifications_)
result.insert(notification_pair.second.get());
return result;
}
MessageCenterView* MessageCenterViewTest::GetMessageCenterView() {
return message_center_view_.get();
}
MessageListView* MessageCenterViewTest::GetMessageListView() {
return message_center_view_->message_list_view_.get();
}
FakeMessageCenterImpl* MessageCenterViewTest::GetMessageCenter() const {
return message_center_.get();
}
MessageView* MessageCenterViewTest::GetNotificationView(const std::string& id) {
return message_center_view_->notification_views_[id];
}
int MessageCenterViewTest::GetCalculatedMessageListViewHeight() {
return GetMessageListView()->GetHeightForWidth(GetMessageListView()->width());
}
MessageCenterViewTest::Mode
MessageCenterViewTest::GetMessageCenterViewInternalMode() {
return GetMessageCenterView()->mode_;
}
views::BoundsAnimator* MessageCenterViewTest::GetAnimator() {
return &GetMessageListView()->animator_;
}
int MessageCenterViewTest::GetNotificationCount() {
return 2;
}
int MessageCenterViewTest::GetCallCount(CallType type) {
return callCounts_[type];
}
void MessageCenterViewTest::SetLockedState(bool locked) {
GetMessageCenterView()->OnLockedStateChanged(locked);
}
void MessageCenterViewTest::ClickOnNotification(
const std::string& notification_id) {
// For this test, this method should not be invoked.
NOTREACHED();
}
void MessageCenterViewTest::AddNotification(
std::unique_ptr<Notification> notification) {
std::string notification_id = notification->id();
notifications_[notification_id] = std::move(notification);
message_center_->SetVisibleNotifications(Notifications());
message_center_view_->OnNotificationAdded(notification_id);
}
void MessageCenterViewTest::UpdateNotification(
const std::string& notification_id,
std::unique_ptr<Notification> notification) {
DCHECK_EQ(notification_id, notification->id());
notifications_[notification_id] = std::move(notification);
message_center_->SetVisibleNotifications(Notifications());
message_center_view_->OnNotificationUpdated(notification_id);
}
void MessageCenterViewTest::RemoveNotification(
const std::string& notification_id,
bool by_user) {
notifications_.erase(notification_id);
message_center_->SetVisibleNotifications(Notifications());
message_center_view_->OnNotificationRemoved(notification_id, by_user);
}
std::unique_ptr<ui::MenuModel> MessageCenterViewTest::CreateMenuModel(
const NotifierId& notifier_id,
const base::string16& display_source) {
// For this test, this method should not be invoked.
NOTREACHED();
return nullptr;
}
bool MessageCenterViewTest::HasClickedListener(
const std::string& notification_id) {
return true;
}
void MessageCenterViewTest::ClickOnNotificationButton(
const std::string& notification_id,
int button_index) {
// For this test, this method should not be invoked.
NOTREACHED();
}
void MessageCenterViewTest::ClickOnSettingsButton(
const std::string& notification_id) {
// For this test, this method should not be invoked.
NOTREACHED();
}
void MessageCenterViewTest::RegisterCall(CallType type) {
callCounts_[type] += 1;
}
void MessageCenterViewTest::FireOnMouseExitedEvent() {
ui::MouseEvent dummy_event(
ui::ET_MOUSE_EXITED /* type */, gfx::Point(0, 0) /* location */,
gfx::Point(0, 0) /* root location */, base::TimeTicks() /* time_stamp */,
0 /* flags */, 0 /*changed_button_flags */);
message_center_view_->OnMouseExited(dummy_event);
}
void MessageCenterViewTest::LogBounds(int depth, views::View* view) {
base::string16 inset;
for (int i = 0; i < depth; ++i)
inset.append(base::UTF8ToUTF16(" "));
gfx::Rect bounds = view->bounds();
DVLOG(0) << inset << bounds.width() << " x " << bounds.height()
<< " @ " << bounds.x() << ", " << bounds.y();
for (int i = 0; i < view->child_count(); ++i)
LogBounds(depth + 1, view->child_at(i));
}
MessageCenterButtonBar* MessageCenterViewTest::GetButtonBar() const {
return message_center_view_->button_bar_;
}
void MessageCenterViewTest::RemoveDefaultNotifications() {
RemoveNotification(kNotificationId1, false);
RemoveNotification(kNotificationId2, false);
}
/* Unit tests *****************************************************************/
TEST_F(MessageCenterViewTest, CallTest) {
// Verify that this didn't generate more than 2 Layout() call per descendant
// NotificationView or more than a total of 20 GetPreferredSize() and
// GetHeightForWidth() calls per descendant NotificationView. 20 is a very
// large number corresponding to the current reality. That number will be
// ratcheted down over time as the code improves.
EXPECT_LE(GetCallCount(LAYOUT), GetNotificationCount() * 2);
EXPECT_LE(GetCallCount(GET_PREFERRED_SIZE) +
GetCallCount(GET_HEIGHT_FOR_WIDTH),
GetNotificationCount() * 20);
}
TEST_F(MessageCenterViewTest, Size) {
EXPECT_EQ(2, GetMessageListView()->child_count());
EXPECT_EQ(GetMessageListView()->height(),
GetCalculatedMessageListViewHeight());
int width =
GetMessageListView()->width() - GetMessageListView()->GetInsets().width();
EXPECT_EQ(
GetMessageListView()->height(),
GetNotificationView(kNotificationId1)->GetHeightForWidth(width) +
(kMarginBetweenItems - MessageView::GetShadowInsets().bottom()) +
GetNotificationView(kNotificationId2)->GetHeightForWidth(width) +
GetMessageListView()->GetInsets().height());
}
TEST_F(MessageCenterViewTest, SizeAfterUpdate) {
EXPECT_EQ(2, GetMessageListView()->child_count());
int width =
GetMessageListView()->width() - GetMessageListView()->GetInsets().width();
std::unique_ptr<Notification> notification = base::MakeUnique<Notification>(
NOTIFICATION_TYPE_SIMPLE, std::string(kNotificationId2),
base::UTF8ToUTF16("title2"),
base::UTF8ToUTF16("message\nwhich\nis\nvertically\nlong\n."),
gfx::Image(), base::UTF8ToUTF16("display source"), GURL(),
NotifierId(NotifierId::APPLICATION, "extension_id"),
message_center::RichNotificationData(), nullptr);
EXPECT_EQ(
GetMessageListView()->height(),
GetNotificationView(kNotificationId1)->GetHeightForWidth(width) +
(kMarginBetweenItems - MessageView::GetShadowInsets().bottom()) +
GetNotificationView(kNotificationId2)->GetHeightForWidth(width) +
GetMessageListView()->GetInsets().height());
int previous_height = GetMessageListView()->height();
UpdateNotification(kNotificationId2, std::move(notification));
// Wait until the animation finishes if available.
if (GetAnimator()->IsAnimating())
base::RunLoop().Run();
EXPECT_EQ(2, GetMessageListView()->child_count());
EXPECT_EQ(GetMessageListView()->height(),
GetCalculatedMessageListViewHeight());
// The size must be changed, since the new string is longer than the old one.
EXPECT_NE(previous_height, GetMessageListView()->height());
EXPECT_EQ(
GetMessageListView()->height(),
GetNotificationView(kNotificationId1)->GetHeightForWidth(width) +
(kMarginBetweenItems - MessageView::GetShadowInsets().bottom()) +
GetNotificationView(kNotificationId2)->GetHeightForWidth(width) +
GetMessageListView()->GetInsets().height());
}
TEST_F(MessageCenterViewTest, SizeAfterRemove) {
EXPECT_EQ(2, GetMessageListView()->child_count());
RemoveNotification(kNotificationId1, false);
// Wait until the animation finishes if available.
if (GetAnimator()->IsAnimating())
base::RunLoop().Run();
EXPECT_EQ(1, GetMessageListView()->child_count());
int width =
GetMessageListView()->width() - GetMessageListView()->GetInsets().width();
EXPECT_FALSE(GetNotificationView(kNotificationId1));
EXPECT_TRUE(GetNotificationView(kNotificationId2));
EXPECT_EQ(GetMessageListView()->height(),
GetNotificationView(kNotificationId2)->GetHeightForWidth(width) +
GetMessageListView()->GetInsets().height());
}
TEST_F(MessageCenterViewTest, PositionAfterUpdate) {
// Make sure that the notification 2 is placed above the notification 1.
EXPECT_LT(GetNotificationView(kNotificationId2)->bounds().y(),
GetNotificationView(kNotificationId1)->bounds().y());
int previous_vertical_pos_from_bottom =
GetMessageListView()->height() -
GetNotificationView(kNotificationId1)->bounds().y();
GetMessageListView()->SetRepositionTargetForTest(
GetNotificationView(kNotificationId1)->bounds());
std::unique_ptr<Notification> notification = base::MakeUnique<Notification>(
NOTIFICATION_TYPE_SIMPLE, std::string(kNotificationId2),
base::UTF8ToUTF16("title2"),
base::UTF8ToUTF16("message\nwhich\nis\nvertically\nlong\n."),
gfx::Image(), base::UTF8ToUTF16("display source"), GURL(),
NotifierId(NotifierId::APPLICATION, "extension_id"),
message_center::RichNotificationData(), nullptr);
UpdateNotification(kNotificationId2, std::move(notification));
// Wait until the animation finishes if available.
if (GetAnimator()->IsAnimating())
base::RunLoop().Run();
// The vertical position of the target from bottom should be kept over change.
int current_vertical_pos_from_bottom =
GetMessageListView()->height() -
GetNotificationView(kNotificationId1)->bounds().y();
EXPECT_EQ(previous_vertical_pos_from_bottom,
current_vertical_pos_from_bottom);
}
TEST_F(MessageCenterViewTest, PositionAfterRemove) {
// Make sure that the notification 2 is placed above the notification 1.
EXPECT_LT(GetNotificationView(kNotificationId2)->bounds().y(),
GetNotificationView(kNotificationId1)->bounds().y());
GetMessageListView()->SetRepositionTargetForTest(
GetNotificationView(kNotificationId2)->bounds());
int previous_height = GetMessageListView()->height();
int previous_notification2_y =
GetNotificationView(kNotificationId2)->bounds().y();
int previous_notification2_height =
GetNotificationView(kNotificationId2)->bounds().height();
EXPECT_EQ(2, GetMessageListView()->child_count());
RemoveNotification(kNotificationId2, false);
// Wait until the animation finishes if available.
if (GetAnimator()->IsAnimating())
base::RunLoop().Run();
EXPECT_EQ(1, GetMessageListView()->child_count());
// Confirm that notification 1 is moved up to the place on which the
// notification 2 was.
EXPECT_EQ(previous_notification2_y,
GetNotificationView(kNotificationId1)->bounds().y());
// The size should be kept.
EXPECT_EQ(previous_height, GetMessageListView()->height());
// Notification 2 is successfully removed.
EXPECT_FALSE(GetNotificationView(kNotificationId2));
EXPECT_TRUE(GetNotificationView(kNotificationId1));
// Mouse cursor moves out of the message center. This resets the reposition
// target in the message list.
FireOnMouseExitedEvent();
// The height should shrink from the height of the removed notification 2.
EXPECT_EQ(previous_height - previous_notification2_height -
(kMarginBetweenItems - MessageView::GetShadowInsets().bottom()),
GetMessageListView()->height());
}
TEST_F(MessageCenterViewTest, CloseButton) {
views::Button* close_button = GetButtonBar()->GetCloseAllButtonForTest();
EXPECT_NE(nullptr, close_button);
((views::ButtonListener*)GetButtonBar())
->ButtonPressed(close_button, DummyEvent());
base::RunLoop().Run();
EXPECT_TRUE(GetMessageCenter()->remove_all_closable_notification_called_);
}
TEST_F(MessageCenterViewTest, CloseButtonEnablity) {
views::Button* close_button = GetButtonBar()->GetCloseAllButtonForTest();
EXPECT_NE(nullptr, close_button);
// There should be 2 non-pinned notifications.
EXPECT_EQ(2u, GetMessageCenter()->GetVisibleNotifications().size());
EXPECT_TRUE(close_button->enabled());
RemoveNotification(kNotificationId1, false);
base::RunLoop().Run();
// There should be 1 non-pinned notification.
EXPECT_EQ(1u, GetMessageCenter()->GetVisibleNotifications().size());
EXPECT_TRUE(close_button->enabled());
RemoveNotification(kNotificationId2, false);
base::RunLoop().Run();
// There should be no notification.
EXPECT_EQ(0u, GetMessageCenter()->GetVisibleNotifications().size());
EXPECT_FALSE(close_button->enabled());
#if defined(OS_CHROMEOS)
// Non-pinned version of notification #1
Notification normal_notification1(
NOTIFICATION_TYPE_SIMPLE, std::string(kNotificationId1),
base::UTF8ToUTF16("title2"),
base::UTF8ToUTF16("message\nwhich\nis\nvertically\nlong\n."),
gfx::Image(), base::UTF8ToUTF16("display source"), GURL(),
NotifierId(NotifierId::APPLICATION, "extension_id"),
message_center::RichNotificationData(), NULL);
// Pinned version of notification #1
Notification pinned_notification1(
NOTIFICATION_TYPE_SIMPLE, std::string(kNotificationId1),
base::UTF8ToUTF16("title2"),
base::UTF8ToUTF16("message\nwhich\nis\nvertically\nlong\n."),
gfx::Image(), base::UTF8ToUTF16("display source"), GURL(),
NotifierId(NotifierId::APPLICATION, "extension_id"),
message_center::RichNotificationData(), NULL);
pinned_notification1.set_pinned(true);
// Pinned notification #2
Notification pinned_notification2(
NOTIFICATION_TYPE_SIMPLE, std::string(kNotificationId2),
base::UTF8ToUTF16("title2"),
base::UTF8ToUTF16("message\nwhich\nis\nvertically\nlong\n."),
gfx::Image(), base::UTF8ToUTF16("display source"), GURL(),
NotifierId(NotifierId::APPLICATION, "extension_id"),
message_center::RichNotificationData(), NULL);
pinned_notification2.set_pinned(true);
AddNotification(base::MakeUnique<Notification>(normal_notification1));
// There should be 1 non-pinned notification.
EXPECT_EQ(1u, GetMessageCenter()->GetVisibleNotifications().size());
EXPECT_TRUE(close_button->enabled());
UpdateNotification(kNotificationId1,
base::MakeUnique<Notification>(pinned_notification1));
// There should be 1 pinned notification.
EXPECT_EQ(1u, GetMessageCenter()->GetVisibleNotifications().size());
EXPECT_FALSE(close_button->enabled());
// Adds 1 pinned notification.
AddNotification(base::MakeUnique<Notification>(pinned_notification2));
// There should be 1 pinned notification.
EXPECT_EQ(2u, GetMessageCenter()->GetVisibleNotifications().size());
EXPECT_FALSE(close_button->enabled());
UpdateNotification(kNotificationId1,
base::MakeUnique<Notification>(normal_notification1));
// There should be 1 normal notification and 1 pinned notification.
EXPECT_EQ(2u, GetMessageCenter()->GetVisibleNotifications().size());
EXPECT_TRUE(close_button->enabled());
RemoveNotification(kNotificationId1, false);
// There should be 1 pinned notification.
EXPECT_EQ(1u, GetMessageCenter()->GetVisibleNotifications().size());
EXPECT_FALSE(close_button->enabled());
RemoveNotification(kNotificationId2, false);
// There should be no notification.
EXPECT_EQ(0u, GetMessageCenter()->GetVisibleNotifications().size());
EXPECT_FALSE(close_button->enabled());
AddNotification(base::MakeUnique<Notification>(pinned_notification2));
// There should be 1 pinned notification.
EXPECT_EQ(1u, GetMessageCenter()->GetVisibleNotifications().size());
EXPECT_FALSE(close_button->enabled());
#endif // defined(OS_CHROMEOS)
}
TEST_F(MessageCenterViewTest, CheckModeWithSettingsVisibleAndHidden) {
// Check the initial state.
EXPECT_EQ(Mode::NOTIFICATIONS, GetMessageCenterViewInternalMode());
// Show the settings.
GetMessageCenterView()->SetSettingsVisible(true);
EXPECT_EQ(Mode::SETTINGS, GetMessageCenterViewInternalMode());
// Hide the settings.
GetMessageCenterView()->SetSettingsVisible(false);
EXPECT_EQ(Mode::NOTIFICATIONS, GetMessageCenterViewInternalMode());
}
TEST_F(MessageCenterViewTest, CheckModeWithRemovingAndAddingNotifications) {
// Check the initial state.
EXPECT_EQ(Mode::NOTIFICATIONS, GetMessageCenterViewInternalMode());
// Remove notifications.
RemoveDefaultNotifications();
EXPECT_EQ(Mode::BUTTONS_ONLY, GetMessageCenterViewInternalMode());
// Add a notification.
AddNotification(base::MakeUnique<Notification>(
NOTIFICATION_TYPE_SIMPLE, std::string(kNotificationId1),
base::UTF8ToUTF16("title2"),
base::UTF8ToUTF16("message\nwhich\nis\nvertically\nlong\n."),
gfx::Image(), base::UTF8ToUTF16("display source"), GURL(),
NotifierId(NotifierId::APPLICATION, "extension_id"),
message_center::RichNotificationData(), nullptr));
EXPECT_EQ(Mode::NOTIFICATIONS, GetMessageCenterViewInternalMode());
}
TEST_F(MessageCenterViewTest, CheckModeWithSettingsVisibleAndHiddenOnEmpty) {
// Set up by removing all existing notifications.
RemoveDefaultNotifications();
// Check the initial state.
EXPECT_EQ(Mode::BUTTONS_ONLY, GetMessageCenterViewInternalMode());
// Show the settings.
GetMessageCenterView()->SetSettingsVisible(true);
EXPECT_EQ(Mode::SETTINGS, GetMessageCenterViewInternalMode());
// Hide the settings.
GetMessageCenterView()->SetSettingsVisible(false);
EXPECT_EQ(Mode::BUTTONS_ONLY, GetMessageCenterViewInternalMode());
}
TEST_F(MessageCenterViewTest,
CheckModeWithRemovingNotificationDuringSettingsVisible) {
// Check the initial state.
EXPECT_EQ(Mode::NOTIFICATIONS, GetMessageCenterViewInternalMode());
// Show the settings.
GetMessageCenterView()->SetSettingsVisible(true);
EXPECT_EQ(Mode::SETTINGS, GetMessageCenterViewInternalMode());
// Remove a notification during settings is visible.
RemoveDefaultNotifications();
EXPECT_EQ(Mode::SETTINGS, GetMessageCenterViewInternalMode());
// Hide the settings.
GetMessageCenterView()->SetSettingsVisible(false);
EXPECT_EQ(Mode::BUTTONS_ONLY, GetMessageCenterViewInternalMode());
}
TEST_F(MessageCenterViewTest,
CheckModeWithAddingNotificationDuringSettingsVisible) {
// Set up by removing all existing notifications.
RemoveDefaultNotifications();
// Check the initial state.
EXPECT_EQ(Mode::BUTTONS_ONLY, GetMessageCenterViewInternalMode());
// Show the settings.
GetMessageCenterView()->SetSettingsVisible(true);
EXPECT_EQ(Mode::SETTINGS, GetMessageCenterViewInternalMode());
// Add a notification during settings is visible.
AddNotification(base::MakeUnique<Notification>(
NOTIFICATION_TYPE_SIMPLE, std::string(kNotificationId1),
base::UTF8ToUTF16("title2"),
base::UTF8ToUTF16("message\nwhich\nis\nvertically\nlong\n."),
gfx::Image(), base::UTF8ToUTF16("display source"), GURL(),
NotifierId(NotifierId::APPLICATION, "extension_id"),
message_center::RichNotificationData(), nullptr));
EXPECT_EQ(Mode::SETTINGS, GetMessageCenterViewInternalMode());
// Hide the settings.
GetMessageCenterView()->SetSettingsVisible(false);
EXPECT_EQ(Mode::NOTIFICATIONS, GetMessageCenterViewInternalMode());
}
TEST_F(MessageCenterViewTest, LockScreen) {
const int kLockedMessageCenterViewHeight = 50;
EXPECT_TRUE(GetNotificationView(kNotificationId1)->IsDrawn());
EXPECT_TRUE(GetNotificationView(kNotificationId2)->IsDrawn());
// Lock!
SetLockedState(true);
EXPECT_FALSE(GetNotificationView(kNotificationId1)->IsDrawn());
EXPECT_FALSE(GetNotificationView(kNotificationId2)->IsDrawn());
GetMessageCenterView()->SizeToPreferredSize();
EXPECT_EQ(kLockedMessageCenterViewHeight, GetMessageCenterView()->height());
RemoveNotification(kNotificationId1, false);
GetMessageCenterView()->SizeToPreferredSize();
EXPECT_EQ(kLockedMessageCenterViewHeight, GetMessageCenterView()->height());
RemoveNotification(kNotificationId2, false);
GetMessageCenterView()->SizeToPreferredSize();
EXPECT_EQ(kLockedMessageCenterViewHeight, GetMessageCenterView()->height());
AddNotification(base::MakeUnique<Notification>(
NOTIFICATION_TYPE_SIMPLE, std::string(kNotificationId1),
base::UTF8ToUTF16("title1"), base::UTF8ToUTF16("message"), gfx::Image(),
base::UTF8ToUTF16("display source"), GURL(),
NotifierId(NotifierId::APPLICATION, "extension_id"),
message_center::RichNotificationData(), nullptr));
EXPECT_FALSE(GetNotificationView(kNotificationId1)->IsDrawn());
GetMessageCenterView()->SizeToPreferredSize();
EXPECT_EQ(kLockedMessageCenterViewHeight, GetMessageCenterView()->height());
// Unlock!
SetLockedState(false);
EXPECT_TRUE(GetNotificationView(kNotificationId1)->IsDrawn());
GetMessageCenterView()->SizeToPreferredSize();
EXPECT_NE(kLockedMessageCenterViewHeight, GetMessageCenterView()->height());
// Lock!
SetLockedState(true);
EXPECT_FALSE(GetNotificationView(kNotificationId1)->IsDrawn());
GetMessageCenterView()->SizeToPreferredSize();
EXPECT_EQ(kLockedMessageCenterViewHeight, GetMessageCenterView()->height());
}
TEST_F(MessageCenterViewTest, NoNotification) {
const int kEmptyMessageCenterViewHeight = 50;
GetMessageCenterView()->SizeToPreferredSize();
EXPECT_NE(kEmptyMessageCenterViewHeight, GetMessageCenterView()->height());
RemoveNotification(kNotificationId1, false);
GetMessageCenterView()->SizeToPreferredSize();
EXPECT_NE(kEmptyMessageCenterViewHeight, GetMessageCenterView()->height());
RemoveNotification(kNotificationId2, false);
GetMessageCenterView()->SizeToPreferredSize();
EXPECT_EQ(kEmptyMessageCenterViewHeight, GetMessageCenterView()->height());
AddNotification(base::MakeUnique<Notification>(
NOTIFICATION_TYPE_SIMPLE, std::string(kNotificationId1),
base::UTF8ToUTF16("title1"), base::UTF8ToUTF16("message"), gfx::Image(),
base::UTF8ToUTF16("display source"), GURL(),
NotifierId(NotifierId::APPLICATION, "extension_id"),
message_center::RichNotificationData(), nullptr));
GetMessageCenterView()->SizeToPreferredSize();
EXPECT_NE(kEmptyMessageCenterViewHeight, GetMessageCenterView()->height());
}
} // namespace message_center
|