| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/message_center/views/message_center_view.h" | 5 #include "ui/message_center/views/message_center_view.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 bool remove_all_closable_notification_called_ = false; | 110 bool remove_all_closable_notification_called_ = false; |
| 111 NotificationList::Notifications visible_notifications_; | 111 NotificationList::Notifications visible_notifications_; |
| 112 }; | 112 }; |
| 113 | 113 |
| 114 /* Test fixture ***************************************************************/ | 114 /* Test fixture ***************************************************************/ |
| 115 | 115 |
| 116 class MessageCenterViewTest : public views::ViewsTestBase, | 116 class MessageCenterViewTest : public views::ViewsTestBase, |
| 117 public MockNotificationView::Test, | 117 public MockNotificationView::Test, |
| 118 public MessageCenterController { | 118 public MessageCenterController { |
| 119 public: | 119 public: |
| 120 // Expose the private enum class MessageCenter::Mode for this test. |
| 121 typedef MessageCenterView::Mode Mode; |
| 122 |
| 120 MessageCenterViewTest(); | 123 MessageCenterViewTest(); |
| 121 ~MessageCenterViewTest() override; | 124 ~MessageCenterViewTest() override; |
| 122 | 125 |
| 123 void SetUp() override; | 126 void SetUp() override; |
| 124 void TearDown() override; | 127 void TearDown() override; |
| 125 | 128 |
| 126 MessageCenterView* GetMessageCenterView(); | 129 MessageCenterView* GetMessageCenterView(); |
| 127 MessageListView* GetMessageListView(); | 130 MessageListView* GetMessageListView(); |
| 128 FakeMessageCenterImpl* GetMessageCenter() const; | 131 FakeMessageCenterImpl* GetMessageCenter() const; |
| 129 MessageView* GetNotificationView(const std::string& id); | 132 MessageView* GetNotificationView(const std::string& id); |
| 130 views::BoundsAnimator* GetAnimator(); | 133 views::BoundsAnimator* GetAnimator(); |
| 131 int GetNotificationCount(); | 134 int GetNotificationCount(); |
| 132 int GetCallCount(CallType type); | 135 int GetCallCount(CallType type); |
| 133 int GetCalculatedMessageListViewHeight(); | 136 int GetCalculatedMessageListViewHeight(); |
| 137 Mode GetMessageCenterViewInternalMode(); |
| 134 void AddNotification(std::unique_ptr<Notification> notification); | 138 void AddNotification(std::unique_ptr<Notification> notification); |
| 135 void UpdateNotification(const std::string& notification_id, | 139 void UpdateNotification(const std::string& notification_id, |
| 136 std::unique_ptr<Notification> notification); | 140 std::unique_ptr<Notification> notification); |
| 137 | 141 |
| 138 // Overridden from MessageCenterController: | 142 // Overridden from MessageCenterController: |
| 139 void ClickOnNotification(const std::string& notification_id) override; | 143 void ClickOnNotification(const std::string& notification_id) override; |
| 140 void RemoveNotification(const std::string& notification_id, | 144 void RemoveNotification(const std::string& notification_id, |
| 141 bool by_user) override; | 145 bool by_user) override; |
| 142 std::unique_ptr<ui::MenuModel> CreateMenuModel( | 146 std::unique_ptr<ui::MenuModel> CreateMenuModel( |
| 143 const NotifierId& notifier_id, | 147 const NotifierId& notifier_id, |
| 144 const base::string16& display_source) override; | 148 const base::string16& display_source) override; |
| 145 bool HasClickedListener(const std::string& notification_id) override; | 149 bool HasClickedListener(const std::string& notification_id) override; |
| 146 void ClickOnNotificationButton(const std::string& notification_id, | 150 void ClickOnNotificationButton(const std::string& notification_id, |
| 147 int button_index) override; | 151 int button_index) override; |
| 148 void ClickOnSettingsButton(const std::string& notification_id) override; | 152 void ClickOnSettingsButton(const std::string& notification_id) override; |
| 149 | 153 |
| 150 // Overridden from MockNotificationView::Test | 154 // Overridden from MockNotificationView::Test |
| 151 void RegisterCall(CallType type) override; | 155 void RegisterCall(CallType type) override; |
| 152 | 156 |
| 153 void FireOnMouseExitedEvent(); | 157 void FireOnMouseExitedEvent(); |
| 154 | 158 |
| 155 void LogBounds(int depth, views::View* view); | 159 void LogBounds(int depth, views::View* view); |
| 156 | 160 |
| 157 MessageCenterButtonBar* GetButtonBar() const; | 161 MessageCenterButtonBar* GetButtonBar() const; |
| 158 | 162 |
| 163 void RemoveDefaultNotifications(); |
| 164 |
| 159 private: | 165 private: |
| 160 views::View* MakeParent(views::View* child1, views::View* child2); | 166 views::View* MakeParent(views::View* child1, views::View* child2); |
| 161 | 167 |
| 162 NotificationList::Notifications notifications_; | 168 NotificationList::Notifications notifications_; |
| 163 std::unique_ptr<MessageCenterView> message_center_view_; | 169 std::unique_ptr<MessageCenterView> message_center_view_; |
| 164 std::unique_ptr<FakeMessageCenterImpl> message_center_; | 170 std::unique_ptr<FakeMessageCenterImpl> message_center_; |
| 165 std::map<CallType,int> callCounts_; | 171 std::map<CallType,int> callCounts_; |
| 166 | 172 |
| 167 DISALLOW_COPY_AND_ASSIGN(MessageCenterViewTest); | 173 DISALLOW_COPY_AND_ASSIGN(MessageCenterViewTest); |
| 168 }; | 174 }; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 } | 234 } |
| 229 | 235 |
| 230 MessageView* MessageCenterViewTest::GetNotificationView(const std::string& id) { | 236 MessageView* MessageCenterViewTest::GetNotificationView(const std::string& id) { |
| 231 return message_center_view_->notification_views_[id]; | 237 return message_center_view_->notification_views_[id]; |
| 232 } | 238 } |
| 233 | 239 |
| 234 int MessageCenterViewTest::GetCalculatedMessageListViewHeight() { | 240 int MessageCenterViewTest::GetCalculatedMessageListViewHeight() { |
| 235 return GetMessageListView()->GetHeightForWidth(GetMessageListView()->width()); | 241 return GetMessageListView()->GetHeightForWidth(GetMessageListView()->width()); |
| 236 } | 242 } |
| 237 | 243 |
| 244 MessageCenterViewTest::Mode |
| 245 MessageCenterViewTest::GetMessageCenterViewInternalMode() { |
| 246 return GetMessageCenterView()->mode_; |
| 247 } |
| 248 |
| 238 views::BoundsAnimator* MessageCenterViewTest::GetAnimator() { | 249 views::BoundsAnimator* MessageCenterViewTest::GetAnimator() { |
| 239 return &GetMessageListView()->animator_; | 250 return &GetMessageListView()->animator_; |
| 240 } | 251 } |
| 241 | 252 |
| 242 int MessageCenterViewTest::GetNotificationCount() { | 253 int MessageCenterViewTest::GetNotificationCount() { |
| 243 return 2; | 254 return 2; |
| 244 } | 255 } |
| 245 | 256 |
| 246 int MessageCenterViewTest::GetCallCount(CallType type) { | 257 int MessageCenterViewTest::GetCallCount(CallType type) { |
| 247 return callCounts_[type]; | 258 return callCounts_[type]; |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 DVLOG(0) << inset << bounds.width() << " x " << bounds.height() | 351 DVLOG(0) << inset << bounds.width() << " x " << bounds.height() |
| 341 << " @ " << bounds.x() << ", " << bounds.y(); | 352 << " @ " << bounds.x() << ", " << bounds.y(); |
| 342 for (int i = 0; i < view->child_count(); ++i) | 353 for (int i = 0; i < view->child_count(); ++i) |
| 343 LogBounds(depth + 1, view->child_at(i)); | 354 LogBounds(depth + 1, view->child_at(i)); |
| 344 } | 355 } |
| 345 | 356 |
| 346 MessageCenterButtonBar* MessageCenterViewTest::GetButtonBar() const { | 357 MessageCenterButtonBar* MessageCenterViewTest::GetButtonBar() const { |
| 347 return message_center_view_->button_bar_; | 358 return message_center_view_->button_bar_; |
| 348 } | 359 } |
| 349 | 360 |
| 361 void MessageCenterViewTest::RemoveDefaultNotifications() { |
| 362 RemoveNotification(kNotificationId1, false); |
| 363 RemoveNotification(kNotificationId2, false); |
| 364 } |
| 365 |
| 350 /* Unit tests *****************************************************************/ | 366 /* Unit tests *****************************************************************/ |
| 351 | 367 |
| 352 TEST_F(MessageCenterViewTest, CallTest) { | 368 TEST_F(MessageCenterViewTest, CallTest) { |
| 353 // Verify that this didn't generate more than 2 Layout() call per descendant | 369 // Verify that this didn't generate more than 2 Layout() call per descendant |
| 354 // NotificationView or more than a total of 20 GetPreferredSize() and | 370 // NotificationView or more than a total of 20 GetPreferredSize() and |
| 355 // GetHeightForWidth() calls per descendant NotificationView. 20 is a very | 371 // GetHeightForWidth() calls per descendant NotificationView. 20 is a very |
| 356 // large number corresponding to the current reality. That number will be | 372 // large number corresponding to the current reality. That number will be |
| 357 // ratcheted down over time as the code improves. | 373 // ratcheted down over time as the code improves. |
| 358 EXPECT_LE(GetCallCount(LAYOUT), GetNotificationCount() * 2); | 374 EXPECT_LE(GetCallCount(LAYOUT), GetNotificationCount() * 2); |
| 359 EXPECT_LE(GetCallCount(GET_PREFERRED_SIZE) + | 375 EXPECT_LE(GetCallCount(GET_PREFERRED_SIZE) + |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 591 | 607 |
| 592 AddNotification( | 608 AddNotification( |
| 593 std::unique_ptr<Notification>(new Notification(pinned_notification))); | 609 std::unique_ptr<Notification>(new Notification(pinned_notification))); |
| 594 | 610 |
| 595 // There should be 1 pinned notification. | 611 // There should be 1 pinned notification. |
| 596 EXPECT_EQ(1u, GetMessageCenter()->GetVisibleNotifications().size()); | 612 EXPECT_EQ(1u, GetMessageCenter()->GetVisibleNotifications().size()); |
| 597 EXPECT_FALSE(close_button->enabled()); | 613 EXPECT_FALSE(close_button->enabled()); |
| 598 #endif // defined(OS_CHROMEOS) | 614 #endif // defined(OS_CHROMEOS) |
| 599 } | 615 } |
| 600 | 616 |
| 617 TEST_F(MessageCenterViewTest, CheckModeWithSettingsVisibleAndHidden) { |
| 618 // Check the initial state. |
| 619 EXPECT_EQ(Mode::NOTIFICATIONS, GetMessageCenterViewInternalMode()); |
| 620 // Show the settings. |
| 621 GetMessageCenterView()->SetSettingsVisible(true); |
| 622 EXPECT_EQ(Mode::SETTINGS, GetMessageCenterViewInternalMode()); |
| 623 // Hide the settings. |
| 624 GetMessageCenterView()->SetSettingsVisible(false); |
| 625 EXPECT_EQ(Mode::NOTIFICATIONS, GetMessageCenterViewInternalMode()); |
| 626 } |
| 627 |
| 628 TEST_F(MessageCenterViewTest, CheckModeWithRemovingAndAddingNotifications) { |
| 629 // Check the initial state. |
| 630 EXPECT_EQ(Mode::NOTIFICATIONS, GetMessageCenterViewInternalMode()); |
| 631 |
| 632 // Remove notifications. |
| 633 RemoveDefaultNotifications(); |
| 634 EXPECT_EQ(Mode::BUTTONS_ONLY, GetMessageCenterViewInternalMode()); |
| 635 |
| 636 // Add a notification. |
| 637 Notification normal_notification( |
| 638 NOTIFICATION_TYPE_SIMPLE, std::string(kNotificationId1), |
| 639 base::UTF8ToUTF16("title2"), |
| 640 base::UTF8ToUTF16("message\nwhich\nis\nvertically\nlong\n."), |
| 641 gfx::Image(), base::UTF8ToUTF16("display source"), GURL(), |
| 642 NotifierId(NotifierId::APPLICATION, "extension_id"), |
| 643 message_center::RichNotificationData(), NULL); |
| 644 AddNotification( |
| 645 std::unique_ptr<Notification>(new Notification(normal_notification))); |
| 646 EXPECT_EQ(Mode::NOTIFICATIONS, GetMessageCenterViewInternalMode()); |
| 647 } |
| 648 |
| 649 TEST_F(MessageCenterViewTest, CheckModeWithSettingsVisibleAndHiddenOnEmpty) { |
| 650 // Set up by removing all existing notifications. |
| 651 RemoveDefaultNotifications(); |
| 652 |
| 653 // Check the initial state. |
| 654 EXPECT_EQ(Mode::BUTTONS_ONLY, GetMessageCenterViewInternalMode()); |
| 655 // Show the settings. |
| 656 GetMessageCenterView()->SetSettingsVisible(true); |
| 657 EXPECT_EQ(Mode::SETTINGS, GetMessageCenterViewInternalMode()); |
| 658 // Hide the settings. |
| 659 GetMessageCenterView()->SetSettingsVisible(false); |
| 660 EXPECT_EQ(Mode::BUTTONS_ONLY, GetMessageCenterViewInternalMode()); |
| 661 } |
| 662 |
| 663 TEST_F(MessageCenterViewTest, |
| 664 CheckModeWithRemovingNotificationDuringSettingsVisible) { |
| 665 // Check the initial state. |
| 666 EXPECT_EQ(Mode::NOTIFICATIONS, GetMessageCenterViewInternalMode()); |
| 667 |
| 668 // Show the settings. |
| 669 GetMessageCenterView()->SetSettingsVisible(true); |
| 670 EXPECT_EQ(Mode::SETTINGS, GetMessageCenterViewInternalMode()); |
| 671 |
| 672 // Remove a notification during settings is visible. |
| 673 RemoveDefaultNotifications(); |
| 674 EXPECT_EQ(Mode::SETTINGS, GetMessageCenterViewInternalMode()); |
| 675 |
| 676 // Hide the settings. |
| 677 GetMessageCenterView()->SetSettingsVisible(false); |
| 678 EXPECT_EQ(Mode::BUTTONS_ONLY, GetMessageCenterViewInternalMode()); |
| 679 } |
| 680 |
| 681 TEST_F(MessageCenterViewTest, |
| 682 CheckModeWithAddingNotificationDuringSettingsVisible) { |
| 683 // Set up by removing all existing notifications. |
| 684 RemoveDefaultNotifications(); |
| 685 |
| 686 // Check the initial state. |
| 687 EXPECT_EQ(Mode::BUTTONS_ONLY, GetMessageCenterViewInternalMode()); |
| 688 |
| 689 // Show the settings. |
| 690 GetMessageCenterView()->SetSettingsVisible(true); |
| 691 EXPECT_EQ(Mode::SETTINGS, GetMessageCenterViewInternalMode()); |
| 692 |
| 693 // Add a notification during settings is visible. |
| 694 Notification normal_notification( |
| 695 NOTIFICATION_TYPE_SIMPLE, std::string(kNotificationId1), |
| 696 base::UTF8ToUTF16("title2"), |
| 697 base::UTF8ToUTF16("message\nwhich\nis\nvertically\nlong\n."), |
| 698 gfx::Image(), base::UTF8ToUTF16("display source"), GURL(), |
| 699 NotifierId(NotifierId::APPLICATION, "extension_id"), |
| 700 message_center::RichNotificationData(), NULL); |
| 701 AddNotification( |
| 702 std::unique_ptr<Notification>(new Notification(normal_notification))); |
| 703 EXPECT_EQ(Mode::SETTINGS, GetMessageCenterViewInternalMode()); |
| 704 |
| 705 // Hide the settings. |
| 706 GetMessageCenterView()->SetSettingsVisible(false); |
| 707 EXPECT_EQ(Mode::NOTIFICATIONS, GetMessageCenterViewInternalMode()); |
| 708 } |
| 709 |
| 601 } // namespace message_center | 710 } // namespace message_center |
| OLD | NEW |